[Mesa-dev] [PATCH v2] util/rand_xor: seed with getentropy when available

Nicolai Hähnle nhaehnle at gmail.com
Thu Mar 23 11:45:43 UTC 2017


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 Gray <jsg at jsg.id.au>
>
> The 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 <time.h>
>  #endif
>
> +#ifdef HAVE_ARC4RANDOM_BUF
> +#include <stdlib.h>
> +#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(&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 at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>


-- 
Lerne, wie die Welt wirklich ist,
Aber vergiss niemals, wie sie sein sollte.


More information about the mesa-dev mailing list