[Mesa-dev] [PATCH 2/3] util/rand_xor: add function to seed rand
Timothy Arceri
tarceri at itsqueeze.com
Wed Mar 22 00:01:57 UTC 2017
On 22/03/17 05:53, Grazvydas Ignotas wrote:
> On Tue, Mar 21, 2017 at 10:57 AM, Timothy Arceri <tarceri at itsqueeze.com> wrote:
>> ---
>> src/gallium/drivers/radeon/r600_test_dma.c | 3 +-
>> src/util/rand_xor.c | 62 ++++++++++++++++++++++++++++--
>> src/util/rand_xor.h | 6 ++-
>> 3 files changed, 64 insertions(+), 7 deletions(-)
>>
>> diff --git a/src/gallium/drivers/radeon/r600_test_dma.c b/src/gallium/drivers/radeon/r600_test_dma.c
>> index 7d636b9..abef8ed 100644
>> --- a/src/gallium/drivers/radeon/r600_test_dma.c
>> +++ b/src/gallium/drivers/radeon/r600_test_dma.c
>> @@ -172,22 +172,21 @@ void r600_test_dma(struct r600_common_screen *rscreen)
>>
>> max_levels = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
>> max_tex_side = 1 << (max_levels - 1);
>>
>> /* Max 128 MB allowed for both textures. */
>> max_alloc_size = 128 * 1024 * 1024;
>>
>> /* the seed for random test parameters */
>> srand(0x9b47d95b);
>> /* the seed for random pixel data */
>> - seed_xorshift128plus[0] = 0x3bffb83978e24f88;
>> - seed_xorshift128plus[1] = 0x9238d5d56c71cd35;
>> + s_rand_xorshift128plus(false);
>>
>> iterations = 1000000000; /* just kill it when you are bored */
>> num_partial_copies = 30;
>>
>> /* These parameters are randomly generated per test:
>> * - whether to do one whole-surface copy or N partial copies per test
>> * - which tiling modes to use (LINEAR_ALIGNED, 1D, 2D)
>> * - which texture dimensions to use
>> * - whether to use VRAM (all tiling modes) and GTT (staging, linear
>> * only) allocations
>> diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c
>> index adb799d..cc834fa 100644
>> --- a/src/util/rand_xor.c
>> +++ b/src/util/rand_xor.c
>> @@ -1,21 +1,77 @@
>> -/* Super fast random number generator.
>> +/*
>> + * Copyright 2017 Timothy Arceri
>> + *
>> + * Permission is hereby granted, free of charge, to any person obtaining a
>> + * copy of this software and associated documentation files (the "Software"),
>> + * to deal in the Software without restriction, including without limitation
>> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
>> + * and/or sell copies of the Software, and to permit persons to whom the
>> + * Software is furnished to do so, subject to the following conditions:
>> + *
>> + * The above copyright notice and this permission notice (including the next
>> + * paragraph) shall be included in all copies or substantial portions of the
>> + * Software.
>> + *
>> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
>> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
>> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
>> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
>> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
>> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
>> + * SOFTWARE.
>> *
>> - * This rand_xorshift128plus function by Sebastiano Vigna belongs
>> - * to the public domain.
>> */
>>
>> +#if defined(__linux__)
>> +#include <sys/file.h>
>> +#include <unistd.h>
>> +#endif
>> +
>> #include "rand_xor.h"
>>
>> +static uint64_t seed_xorshift128plus[2];
>> +
>> +/* Super fast random number generator.
>> + *
>> + * This rand_xorshift128plus function by Sebastiano Vigna belongs
>> + * to the public domain.
>> + */
>> uint64_t
>> rand_xorshift128plus(void)
>> {
>> uint64_t *s = seed_xorshift128plus;
>>
>> uint64_t s1 = s[0];
>> const uint64_t s0 = s[1];
>> s[0] = s0;
>> s1 ^= s1 << 23;
>> s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
>>
>> return s[1] + s0;
>> }
>> +
>> +void
>> +s_rand_xorshift128plus(bool randomised_seed)
>> +{
>> +#if defined(__linux__)
>> + if (!randomised_seed)
>> + goto fixed_seed;
>> +
>> + int fd = open("/dev/urandom", O_RDONLY);
>> + if (fd < 0)
>> + goto fixed_seed;
>> +
>> + size_t seed_size = sizeof(uint64_t) * 2;
>> + if (read(fd, &seed_xorshift128plus, seed_size) != seed_size)
>> + goto fixed_seed;
>
> Leaking fd in error path.
Right, thanks.
>
> Since it's Linux specific, perhaps replace this whole urandom thing with this:
> #if defined(__linux__)
> #include <unistd.h>
> #include <sys/syscall.h>
> #endif
>
> ...
>
> #if defined(SYS_getrandom)
> if (syscall(SYS_getrandom, &seed_xorshift128plus, seed_size, 0) != seed_size)
> goto fixed_seed;
> #endif
>
> Requires 3.17 kernel from 2014, but most people using latest mesa
> should have that.
>
RHEL 6 & 7 use recent Mesa versions and neither are based on that kernel
so I've left as is for now. Thanks.
More information about the mesa-dev
mailing list