Commit 1c10e674 authored by Axel Rasmussen's avatar Axel Rasmussen Committed by Linus Torvalds

userfaultfd/selftests: don't rely on GNU extensions for random numbers

Patch series "Small userfaultfd selftest fixups", v2.

This patch (of 3):

Two arguments for doing this:

First, and maybe most importantly, the resulting code is significantly
shorter / simpler.

Then, we avoid using GNU libc extensions.  Why does this matter? It
makes testing userfaultfd with the selftest easier e.g.  on distros
which use something other than glibc (e.g., Alpine, which uses musl);
basically, it makes the test more portable.

Link: https://lkml.kernel.org/r/20210930212309.4001967-2-axelrasmussen@google.comSigned-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 2c0078a7
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
#include <assert.h> #include <assert.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdint.h> #include <stdint.h>
#include <sys/random.h>
#include "../kselftest.h" #include "../kselftest.h"
...@@ -518,22 +519,10 @@ static void continue_range(int ufd, __u64 start, __u64 len) ...@@ -518,22 +519,10 @@ static void continue_range(int ufd, __u64 start, __u64 len)
static void *locking_thread(void *arg) static void *locking_thread(void *arg)
{ {
unsigned long cpu = (unsigned long) arg; unsigned long cpu = (unsigned long) arg;
struct random_data rand;
unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */ unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
int32_t rand_nr;
unsigned long long count; unsigned long long count;
char randstate[64];
unsigned int seed;
if (bounces & BOUNCE_RANDOM) { if (!(bounces & BOUNCE_RANDOM)) {
seed = (unsigned int) time(NULL) - bounces;
if (!(bounces & BOUNCE_RACINGFAULTS))
seed += cpu;
bzero(&rand, sizeof(rand));
bzero(&randstate, sizeof(randstate));
if (initstate_r(seed, randstate, sizeof(randstate), &rand))
err("initstate_r failed");
} else {
page_nr = -bounces; page_nr = -bounces;
if (!(bounces & BOUNCE_RACINGFAULTS)) if (!(bounces & BOUNCE_RACINGFAULTS))
page_nr += cpu * nr_pages_per_cpu; page_nr += cpu * nr_pages_per_cpu;
...@@ -541,15 +530,8 @@ static void *locking_thread(void *arg) ...@@ -541,15 +530,8 @@ static void *locking_thread(void *arg)
while (!finished) { while (!finished) {
if (bounces & BOUNCE_RANDOM) { if (bounces & BOUNCE_RANDOM) {
if (random_r(&rand, &rand_nr)) if (getrandom(&page_nr, sizeof(page_nr), 0) != sizeof(page_nr))
err("random_r failed"); err("getrandom failed");
page_nr = rand_nr;
if (sizeof(page_nr) > sizeof(rand_nr)) {
if (random_r(&rand, &rand_nr))
err("random_r failed");
page_nr |= (((unsigned long) rand_nr) << 16) <<
16;
}
} else } else
page_nr += 1; page_nr += 1;
page_nr %= nr_pages; page_nr %= nr_pages;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment