Re: [Mesa-dev] [PATCH v2] util/rand_xor: seed with getentropy when available
On 23.03.2017 05:53, Jonathan Gray wrote: On Thu, Mar 23, 2017 at 03:24:16PM +1100, Jonathan Gray wrote: Instead of using using /dev/urandom on Linux and time(NULL) elsewhere for a seed first use getentropy() for systems that have a kernel interface to get a seed such as OpenBSD. This interface is also present in other systems such as Solaris and even Linux with a recent version of glibc. v2: check for/use the different header Solaris and glibc use Signed-off-by: Jonathan GrayThe functions should really be split for random and deterministic as well, but that is hard to cleanup without being able to assume arc4random or equivalent is present. NAK on this idea. The name "rand_xorshift128plus_deterministic" is nonsensical, because xorshift128+ is the name of a PRNG which is deterministic by definition. Conversely, providing anything but a xorshift128+ PRNG under that name would be bad style. Furthermore, Mesa simply doesn't need cryptographically secure random bits, so going to such lengths is simply unnecessary. PRNGs that are good enough for basic Monte Carlo simulation purposes are good enough for Mesa. Having a truly random _seed_ is valuable for the purposes of the disk cache, but anything more than that is overkill. Cheers, Nicolai diff --git a/configure.ac b/configure.ac index c27646ca4c..a1a62482d9 100644 --- a/configure.ac +++ b/configure.ac @@ -792,6 +792,9 @@ AC_CHECK_FUNC([posix_memalign], [DEFINES="$DEFINES -DHAVE_POSIX_MEMALIGN"]) dnl See if getentropy is available AC_CHECK_FUNC([getentropy], [DEFINES="$DEFINES -DHAVE_GETENTROPY"]) +dnl See if arc4random_buf is available +AC_CHECK_FUNC([arc4random_buf], [DEFINES="$DEFINES -DHAVE_ARC4RANDOM_BUF"]) + dnl Check for zlib PKG_CHECK_MODULES([ZLIB], [zlib >= $ZLIB_REQUIRED]) diff --git a/src/gallium/drivers/radeon/r600_test_dma.c b/src/gallium/drivers/radeon/r600_test_dma.c index 3c23b09329..0cdd24d1c0 100644 --- a/src/gallium/drivers/radeon/r600_test_dma.c +++ b/src/gallium/drivers/radeon/r600_test_dma.c @@ -77,7 +77,7 @@ static void set_random_pixels(struct pipe_context *ctx, for (x = 0; x < size; x++) { *ptr++ = *ptr_cpu++ = - rand_xorshift128plus(seed_xorshift128plus); + rand_xorshift128plus_deterministic(seed_xorshift128plus); } } } diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c index c613371f67..db642dae54 100644 --- a/src/util/rand_xor.c +++ b/src/util/rand_xor.c @@ -37,6 +37,10 @@ #include #endif +#ifdef HAVE_ARC4RANDOM_BUF +#include +#endif + #include "rand_xor.h" /* Super fast random number generator. @@ -45,7 +49,7 @@ * to the public domain. */ uint64_t -rand_xorshift128plus(uint64_t *seed) +rand_xorshift128plus_deterministic(uint64_t *seed) { uint64_t *s = seed; @@ -58,6 +62,18 @@ rand_xorshift128plus(uint64_t *seed) return s[1] + s0; } +uint64_t +rand_xorshift128plus(uint64_t *seed) +{ +#ifdef HAVE_ARC4RANDOM_BUF + uint64_t buf; + arc4random_buf(, sizeof(buf)); + return buf; +#else + return rand_xorshift128plus_deterministic(seed); +#endif +} + void s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed) { diff --git a/src/util/rand_xor.h b/src/util/rand_xor.h index 532d549bcd..0fbf4d3a8a 100644 --- a/src/util/rand_xor.h +++ b/src/util/rand_xor.h @@ -31,6 +31,9 @@ uint64_t rand_xorshift128plus(uint64_t *seed); +uint64_t +rand_xorshift128plus_deterministic(uint64_t *seed); + void s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed); ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev -- Lerne, wie die Welt wirklich ist, Aber vergiss niemals, wie sie sein sollte. ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH v2] util/rand_xor: seed with getentropy when available
On 23.03.2017 07:19, Jonathan Gray wrote: On Thu, Mar 23, 2017 at 03:24:16PM +1100, Jonathan Gray wrote: Instead of using using /dev/urandom on Linux and time(NULL) elsewhere for a seed first use getentropy() for systems that have a kernel interface to get a seed such as OpenBSD. This interface is also present in other systems such as Solaris and even Linux with a recent version of glibc. v2: check for/use the different header Solaris and glibc use Signed-off-by: Jonathan GrayEven just changing the original to have the following would be an improvement. I like this suggestion and the first patch you sent (the getentropy one). Perhaps you can provide a combined patch or a sequence of two patches for that? Thanks Nicolai diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c index de05fa64b3..9780907368 100644 --- a/src/util/rand_xor.c +++ b/src/util/rand_xor.c @@ -22,12 +22,9 @@ * */ -#if defined(__linux__) -#include +#include #include -#else #include -#endif #include "rand_xor.h" @@ -53,29 +50,34 @@ rand_xorshift128plus(uint64_t *seed) void s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed) { + time_t now; + if (!randomised_seed) goto fixed_seed; -#if defined(__linux__) int fd = open("/dev/urandom", O_RDONLY); if (fd < 0) - goto fixed_seed; + goto time_seed; size_t seed_size = sizeof(uint64_t) * 2; if (read(fd, seed, seed_size) != seed_size) { close(fd); - goto fixed_seed; + goto time_seed; } close(fd); return; -#else +time_seed: + + now = time(NULL); + if (now == -1) + goto fixed_seed; + seed[0] = 0x3bffb83978e24f88; - seed[1] = time(NULL); + seed[1] = now; return; -#endif fixed_seed: ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev -- Lerne, wie die Welt wirklich ist, Aber vergiss niemals, wie sie sein sollte. ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH v2] util/rand_xor: seed with getentropy when available
On Thu, Mar 23, 2017 at 03:24:16PM +1100, Jonathan Gray wrote: > Instead of using using /dev/urandom on Linux and time(NULL) elsewhere > for a seed first use getentropy() for systems that have a kernel > interface to get a seed such as OpenBSD. This interface is also > present in other systems such as Solaris and even Linux with a recent > version of glibc. > > v2: check for/use the different header Solaris and glibc use > > Signed-off-by: Jonathan GrayEven just changing the original to have the following would be an improvement. diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c index de05fa64b3..9780907368 100644 --- a/src/util/rand_xor.c +++ b/src/util/rand_xor.c @@ -22,12 +22,9 @@ * */ -#if defined(__linux__) -#include +#include #include -#else #include -#endif #include "rand_xor.h" @@ -53,29 +50,34 @@ rand_xorshift128plus(uint64_t *seed) void s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed) { + time_t now; + if (!randomised_seed) goto fixed_seed; -#if defined(__linux__) int fd = open("/dev/urandom", O_RDONLY); if (fd < 0) - goto fixed_seed; + goto time_seed; size_t seed_size = sizeof(uint64_t) * 2; if (read(fd, seed, seed_size) != seed_size) { close(fd); - goto fixed_seed; + goto time_seed; } close(fd); return; -#else +time_seed: + + now = time(NULL); + if (now == -1) + goto fixed_seed; + seed[0] = 0x3bffb83978e24f88; - seed[1] = time(NULL); + seed[1] = now; return; -#endif fixed_seed: ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH v2] util/rand_xor: seed with getentropy when available
On Thu, Mar 23, 2017 at 03:24:16PM +1100, Jonathan Gray wrote: > Instead of using using /dev/urandom on Linux and time(NULL) elsewhere > for a seed first use getentropy() for systems that have a kernel > interface to get a seed such as OpenBSD. This interface is also > present in other systems such as Solaris and even Linux with a recent > version of glibc. > > v2: check for/use the different header Solaris and glibc use > > Signed-off-by: Jonathan GrayThe functions should really be split for random and deterministic as well, but that is hard to cleanup without being able to assume arc4random or equivalent is present. diff --git a/configure.ac b/configure.ac index c27646ca4c..a1a62482d9 100644 --- a/configure.ac +++ b/configure.ac @@ -792,6 +792,9 @@ AC_CHECK_FUNC([posix_memalign], [DEFINES="$DEFINES -DHAVE_POSIX_MEMALIGN"]) dnl See if getentropy is available AC_CHECK_FUNC([getentropy], [DEFINES="$DEFINES -DHAVE_GETENTROPY"]) +dnl See if arc4random_buf is available +AC_CHECK_FUNC([arc4random_buf], [DEFINES="$DEFINES -DHAVE_ARC4RANDOM_BUF"]) + dnl Check for zlib PKG_CHECK_MODULES([ZLIB], [zlib >= $ZLIB_REQUIRED]) diff --git a/src/gallium/drivers/radeon/r600_test_dma.c b/src/gallium/drivers/radeon/r600_test_dma.c index 3c23b09329..0cdd24d1c0 100644 --- a/src/gallium/drivers/radeon/r600_test_dma.c +++ b/src/gallium/drivers/radeon/r600_test_dma.c @@ -77,7 +77,7 @@ static void set_random_pixels(struct pipe_context *ctx, for (x = 0; x < size; x++) { *ptr++ = *ptr_cpu++ = - rand_xorshift128plus(seed_xorshift128plus); + rand_xorshift128plus_deterministic(seed_xorshift128plus); } } } diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c index c613371f67..db642dae54 100644 --- a/src/util/rand_xor.c +++ b/src/util/rand_xor.c @@ -37,6 +37,10 @@ #include #endif +#ifdef HAVE_ARC4RANDOM_BUF +#include +#endif + #include "rand_xor.h" /* Super fast random number generator. @@ -45,7 +49,7 @@ * to the public domain. */ uint64_t -rand_xorshift128plus(uint64_t *seed) +rand_xorshift128plus_deterministic(uint64_t *seed) { uint64_t *s = seed; @@ -58,6 +62,18 @@ rand_xorshift128plus(uint64_t *seed) return s[1] + s0; } +uint64_t +rand_xorshift128plus(uint64_t *seed) +{ +#ifdef HAVE_ARC4RANDOM_BUF + uint64_t buf; + arc4random_buf(, sizeof(buf)); + return buf; +#else + return rand_xorshift128plus_deterministic(seed); +#endif +} + void s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed) { diff --git a/src/util/rand_xor.h b/src/util/rand_xor.h index 532d549bcd..0fbf4d3a8a 100644 --- a/src/util/rand_xor.h +++ b/src/util/rand_xor.h @@ -31,6 +31,9 @@ uint64_t rand_xorshift128plus(uint64_t *seed); +uint64_t +rand_xorshift128plus_deterministic(uint64_t *seed); + void s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed); ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev
[Mesa-dev] [PATCH v2] util/rand_xor: seed with getentropy when available
Instead of using using /dev/urandom on Linux and time(NULL) elsewhere for a seed first use getentropy() for systems that have a kernel interface to get a seed such as OpenBSD. This interface is also present in other systems such as Solaris and even Linux with a recent version of glibc. v2: check for/use the different header Solaris and glibc use Signed-off-by: Jonathan Gray--- configure.ac| 4 src/util/rand_xor.c | 17 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index bc9c304c8d..c27646ca4c 100644 --- a/configure.ac +++ b/configure.ac @@ -756,6 +756,7 @@ fi AC_HEADER_MAJOR AC_CHECK_HEADER([xlocale.h], [DEFINES="$DEFINES -DHAVE_XLOCALE_H"]) AC_CHECK_HEADER([sys/sysctl.h], [DEFINES="$DEFINES -DHAVE_SYS_SYSCTL_H"]) +AC_CHECK_HEADER([sys/random.h], [DEFINES="$DEFINES -DHAVE_SYS_RANDOM_H"]) AC_CHECK_FUNC([strtof], [DEFINES="$DEFINES -DHAVE_STRTOF"]) AC_CHECK_FUNC([mkostemp], [DEFINES="$DEFINES -DHAVE_MKOSTEMP"]) @@ -788,6 +789,9 @@ esac dnl See if posix_memalign is available AC_CHECK_FUNC([posix_memalign], [DEFINES="$DEFINES -DHAVE_POSIX_MEMALIGN"]) +dnl See if getentropy is available +AC_CHECK_FUNC([getentropy], [DEFINES="$DEFINES -DHAVE_GETENTROPY"]) + dnl Check for zlib PKG_CHECK_MODULES([ZLIB], [zlib >= $ZLIB_REQUIRED]) diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c index de05fa64b3..c613371f67 100644 --- a/src/util/rand_xor.c +++ b/src/util/rand_xor.c @@ -22,7 +22,15 @@ * */ -#if defined(__linux__) +#ifdef HAVE_GETENTROPY + +#ifdef HAVE_SYS_RANDOM_H +#include +#else +#include +#endif + +#elif defined(__linux__) #include #include #else @@ -56,7 +64,12 @@ s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed) if (!randomised_seed) goto fixed_seed; -#if defined(__linux__) +#ifdef HAVE_GETENTROPY + size_t seed_size = sizeof(uint64_t) * 2; + if (getentropy(seed, seed_size) == -1) + goto fixed_seed; + return; +#elif defined(__linux__) int fd = open("/dev/urandom", O_RDONLY); if (fd < 0) goto fixed_seed; -- 2.12.0 ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev