Author: gnn
Date: Thu Mar 30 02:50:21 2017
New Revision: 316210
URL: https://svnweb.freebsd.org/changeset/base/316210

Log:
  MFC: 313176, 313177, 313359
  
  Replace the implementation of DTrace's RAND subroutine for generating
  low-quality random numbers with a modern implementation (xoroshiro128+)
  that is capable of generating better quality randomness without compromising 
performance.
  
  Submitted by: Graeme Jenkinson

Added:
  
stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.c
     - copied unchanged from r313177, 
head/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.c
  
stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.h
     - copied, changed from r313177, 
head/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.h
Modified:
  stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c
  stable/11/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace_impl.h
  stable/11/sys/modules/dtrace/dtrace/Makefile
  stable/11/sys/modules/dtrace/fasttrap/Makefile
  stable/11/sys/modules/dtrace/systrace/Makefile
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c
==============================================================================
--- stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c   Thu Mar 
30 02:38:38 2017        (r316209)
+++ stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c   Thu Mar 
30 02:50:21 2017        (r316210)
@@ -124,6 +124,7 @@
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/ptrace.h>
+#include <sys/random.h>
 #include <sys/rwlock.h>
 #include <sys/sx.h>
 #include <sys/sysctl.h>
@@ -136,6 +137,8 @@
 #include "dtrace_debug.c"
 #endif
 
+#include "dtrace_xoroshiro128_plus.h"
+
 /*
  * DTrace Tunable Variables
  *
@@ -298,7 +301,6 @@ static kmutex_t             dtrace_meta_lock;       /* me
 #define vuprintf       vprintf
 #define ttoproc(_a)    ((_a)->td_proc)
 #define crgetzoneid(_a)        0
-#define        NCPU            MAXCPU
 #define SNOCD          0
 #define CPU_ON_INTR(_a)        0
 
@@ -4119,7 +4121,8 @@ dtrace_dif_subr(uint_t subr, uint_t rd, 
 
        switch (subr) {
        case DIF_SUBR_RAND:
-               regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875;
+               regs[rd] = dtrace_xoroshiro128_plus_next(
+                   state->dts_rstate[curcpu]);
                break;
 
 #ifdef illumos
@@ -14408,6 +14411,7 @@ dtrace_state_create(struct cdev *dev, st
        dtrace_state_t *state;
        dtrace_optval_t *opt;
        int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
+       int cpu_it;
 
        ASSERT(MUTEX_HELD(&dtrace_lock));
        ASSERT(MUTEX_HELD(&cpu_lock));
@@ -14463,6 +14467,21 @@ dtrace_state_create(struct cdev *dev, st
        state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
        state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
 
+       /*
+         * Allocate and initialise the per-process per-CPU random state.
+        * SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is
+         * assumed to be seeded at this point (if from Fortuna seed file).
+        */
+       (void) read_random(&state->dts_rstate[0], 2 * sizeof(uint64_t));
+       for (cpu_it = 1; cpu_it < NCPU; cpu_it++) {
+               /*
+                * Each CPU is assigned a 2^64 period, non-overlapping
+                * subsequence.
+                */
+               dtrace_xoroshiro128_plus_jump(state->dts_rstate[cpu_it-1],
+                   state->dts_rstate[cpu_it]); 
+       }
+
 #ifdef illumos
        state->dts_cleaner = CYCLIC_NONE;
        state->dts_deadman = CYCLIC_NONE;

Copied: 
stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.c
 (from r313177, 
head/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.c)
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ 
stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.c
 Thu Mar 30 02:50:21 2017        (r316210, copy of r313177, 
head/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.c)
@@ -0,0 +1,89 @@
+/*-
+ * Copyright (c) 2016 (Graeme Jenkinson)
+ * All rights reserved.
+ *
+ * This software was developed by BAE Systems, the University of Cambridge
+ * Computer Laboratory, and Memorial University under DARPA/AFRL contract
+ * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
+ * (TC) research program.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+#include <sys/types.h>
+
+#include "dtrace_xoroshiro128_plus.h"
+
+static __inline uint64_t
+rotl(const uint64_t x, int k)
+{
+       return (x << k) | (x >> (64 - k));
+}
+
+/*
+ * This is the jump function for the generator. It is equivalent to 2^64 calls
+ * to next(); it can be used to generate 2^64 non-overlapping subsequences for
+ * parallel computations.
+ */
+void
+dtrace_xoroshiro128_plus_jump(uint64_t * const state,
+       uint64_t * const jump_state)
+{
+       static const uint64_t JUMP[] = { 0xbeac0467eba5facb,
+               0xd86b048b86aa9922 };
+
+       uint64_t s0 = 0;
+       uint64_t s1 = 0;
+       int i = 0;
+       int b = 0;
+       for (i = 0; i < sizeof JUMP / sizeof *JUMP; i++) {
+               for (b = 0; b < 64; b++) {
+                       if (JUMP[i] & 1ULL << b) {
+                               s0 ^= state[0];
+                               s1 ^= state[1];
+                       }
+                       dtrace_xoroshiro128_plus_next(state);
+               }
+       }
+       jump_state[0] = s0;
+       jump_state[1] = s1;
+}
+
+/*
+ * xoroshiro128+ - XOR/rotate/shift/rotate
+ * xorshift.di.unimi.it
+ */
+uint64_t
+dtrace_xoroshiro128_plus_next(uint64_t * const state)
+{
+       const uint64_t s0 = state[0];
+       uint64_t s1 = state[1];
+       uint64_t result;
+       result = s0 + s1;
+
+       s1 ^= s0;
+       state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
+       state[1] = rotl(s1, 36);
+
+       return result;
+}

Copied and modified: 
stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.h
 (from r313177, 
head/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.h)
==============================================================================
--- 
head/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.h  
    Fri Feb  3 22:40:13 2017        (r313177, copy source)
+++ 
stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace_xoroshiro128_plus.h
 Thu Mar 30 02:50:21 2017        (r316210)
@@ -32,9 +32,10 @@
 
 #ifndef _DTRACE_XOROSHIRO128_PLUS_H
 #define _DTRACE_XOROSHIRO128_PLUS_H
-#endif
 
 #include <sys/types.h>
 
-extern void dtrace_xoroshiro128_plus_jump(uint64_t * const, uint64_t * const);
-extern uint64_t dtrace_xoroshiro128_plus_next(uint64_t * const);
+void dtrace_xoroshiro128_plus_jump(uint64_t * const, uint64_t * const);
+uint64_t dtrace_xoroshiro128_plus_next(uint64_t * const);
+
+#endif

Modified: stable/11/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace_impl.h
==============================================================================
--- stable/11/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace_impl.h Thu Mar 
30 02:38:38 2017        (r316209)
+++ stable/11/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace_impl.h Thu Mar 
30 02:50:21 2017        (r316210)
@@ -50,6 +50,7 @@ extern "C" {
  */
 
 #include <sys/dtrace.h>
+
 #ifndef illumos
 #ifdef __sparcv9
 typedef uint32_t               pc_t;
@@ -65,6 +66,10 @@ typedef      u_long                  greg_t;
 #define        DTRACE_MAXPROPLEN               128
 #define        DTRACE_DYNVAR_CHUNKSIZE         256
 
+#ifdef __FreeBSD__
+#define        NCPU            MAXCPU
+#endif /* __FreeBSD__ */
+
 struct dtrace_probe;
 struct dtrace_ecb;
 struct dtrace_predicate;
@@ -1168,6 +1173,7 @@ struct dtrace_state {
        dtrace_cred_t dts_cred;                 /* credentials */
        size_t dts_nretained;                   /* number of retained enabs */
        int dts_getf;                           /* number of getf() calls */
+       uint64_t dts_rstate[NCPU][2];           /* per-CPU random state */
 };
 
 struct dtrace_provider {

Modified: stable/11/sys/modules/dtrace/dtrace/Makefile
==============================================================================
--- stable/11/sys/modules/dtrace/dtrace/Makefile        Thu Mar 30 02:38:38 
2017        (r316209)
+++ stable/11/sys/modules/dtrace/dtrace/Makefile        Thu Mar 30 02:50:21 
2017        (r316210)
@@ -12,6 +12,7 @@ ARCHDIR=      ${MACHINE_CPUARCH}
 
 KMOD=          dtrace
 SRCS=          dtrace.c \
+               dtrace_xoroshiro128_plus.c \
                dtrace_asm.S \
                dtrace_subr.c
 
@@ -42,6 +43,7 @@ CFLAGS+=      -I${SYSDIR}/cddl/compat/opensol
                -I${SYSDIR}/cddl/dev/dtrace \
                -I${SYSDIR}/cddl/dev/dtrace/${ARCHDIR} \
                -I${SYSDIR}/cddl/contrib/opensolaris/uts/common \
+               -I${SYSDIR}/cddl/contrib/opensolaris/uts/common/dtrace \
                -I${SYSDIR}/cddl/contrib/opensolaris/common/util \
                -I${SYSDIR} -DDIS_MEM
 

Modified: stable/11/sys/modules/dtrace/fasttrap/Makefile
==============================================================================
--- stable/11/sys/modules/dtrace/fasttrap/Makefile      Thu Mar 30 02:38:38 
2017        (r316209)
+++ stable/11/sys/modules/dtrace/fasttrap/Makefile      Thu Mar 30 02:50:21 
2017        (r316210)
@@ -10,6 +10,7 @@ SRCS+=                vnode_if.h
 
 CFLAGS+=       -I${SYSDIR}/cddl/compat/opensolaris \
                -I${SYSDIR}/cddl/contrib/opensolaris/uts/common \
+               -I${SYSDIR}/cddl/contrib/opensolaris/uts/common/dtrace \
                -I${SYSDIR}
 
 .if ${MACHINE_CPUARCH} == "amd64" ||  ${MACHINE_CPUARCH} == "i386"

Modified: stable/11/sys/modules/dtrace/systrace/Makefile
==============================================================================
--- stable/11/sys/modules/dtrace/systrace/Makefile      Thu Mar 30 02:38:38 
2017        (r316209)
+++ stable/11/sys/modules/dtrace/systrace/Makefile      Thu Mar 30 02:50:21 
2017        (r316210)
@@ -10,6 +10,7 @@ SRCS+=                vnode_if.h
 
 CFLAGS+=       -I${SYSDIR}/cddl/compat/opensolaris \
                -I${SYSDIR}/cddl/contrib/opensolaris/uts/common \
+               -I${SYSDIR}/cddl/contrib/opensolaris/uts/common/dtrace \
                -I${SYSDIR}
 
 .include <bsd.kmod.mk>
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to