From: George Spelvin <lkml@sdf.org>
To: linux-kernel@vger.kernel.org, lkml@sdf.org
Cc: Uladzislau Rezki <urezki@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
Roman Gushchin <guro@fb.com>,
linux-mm@kvack.org
Subject: [RFC PATCH v1 33/50] lib/test_vmalloc.c: Use pseudorandom numbers
Date: Sun, 8 Mar 2020 06:09:17 -0400 [thread overview]
Message-ID: <202003281643.02SGhJ0C010012@sdf.org> (raw)
Test code doesn't need crypto-grade randomness.
Also:
* Simplify shuffle_array using the initialize-while-shuffling
Fisher-Yates variant.
* Shrink the data type to u8; it's currently shuffling 8 items and
exceeding 256 will not happen soon.
* Implement the sequential_test_order flag inside shuffle_array
rather than testing it at each call site. (This is implemented the
compact way using an invariant conditional branch inside the loop.
Modern branch predictors handle such things very efficiently.)
Signed-off-by: George Spelvin <lkml@sdf.org>
Cc: Uladzislau Rezki (Sony) <urezki@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Roman Gushchin <guro@fb.com>
Cc: linux-mm@kvack.org
---
lib/test_vmalloc.c | 58 ++++++++++++++++++----------------------------
1 file changed, 23 insertions(+), 35 deletions(-)
diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c
index 8bbefcaddfe84..27b06c97a2a08 100644
--- a/lib/test_vmalloc.c
+++ b/lib/test_vmalloc.c
@@ -74,22 +74,20 @@ test_report_one_done(void)
static int random_size_align_alloc_test(void)
{
- unsigned long size, align, rnd;
+ unsigned long size, align;
void *ptr;
int i;
for (i = 0; i < test_loop_count; i++) {
- get_random_bytes(&rnd, sizeof(rnd));
-
/*
* Maximum 1024 pages, if PAGE_SIZE is 4096.
*/
- align = 1 << (rnd % 23);
+ align = prandom_u32_max(23);
/*
* Maximum 10 pages.
*/
- size = ((rnd % 10) + 1) * PAGE_SIZE;
+ size = (prandom_u32_max(10) + 1) * PAGE_SIZE;
ptr = __vmalloc_node_range(size, align,
VMALLOC_START, VMALLOC_END,
@@ -157,20 +155,16 @@ static int fix_align_alloc_test(void)
static int random_size_alloc_test(void)
{
- unsigned int n;
void *p;
int i;
for (i = 0; i < test_loop_count; i++) {
- get_random_bytes(&n, sizeof(i));
- n = (n % 100) + 1;
-
- p = vmalloc(n * PAGE_SIZE);
+ p = vmalloc((prandom_u32_max(100) + 1) * PAGE_SIZE);
if (!p)
return -1;
- *((__u8 *)p) = 1;
+ *(__u8 *)p = 1;
vfree(p);
}
@@ -304,16 +298,12 @@ pcpu_alloc_test(void)
return -1;
for (i = 0; i < 35000; i++) {
- unsigned int r;
-
- get_random_bytes(&r, sizeof(i));
- size = (r % (PAGE_SIZE / 4)) + 1;
+ size = prandom_u32_max(PAGE_SIZE / 4) + 1;
/*
* Maximum PAGE_SIZE
*/
- get_random_bytes(&r, sizeof(i));
- align = 1 << ((i % 11) + 1);
+ align = 1 << (prandom_u32_max(PAGE_SHIFT-1) + 1);
pcpu[i] = __alloc_percpu(size, align);
if (!pcpu[i])
@@ -362,28 +352,30 @@ static struct test_driver {
int cpu;
} per_cpu_test_driver[NR_CPUS];
-static void shuffle_array(int *arr, int n)
+/*
+ * Generate a permutation array. Shuffled by default, or
+ * the identity mapping if sequential_test_order is set.
+ */
+static void shuffle_array(u8 *arr, int n)
{
- unsigned int rnd;
- int i, j, x;
+ int i;
- for (i = n - 1; i > 0; i--) {
- get_random_bytes(&rnd, sizeof(rnd));
+ arr[0] = 0;
- /* Cut the range. */
- j = rnd % i;
-
- /* Swap indexes. */
- x = arr[i];
- arr[i] = arr[j];
- arr[j] = x;
+ for (i = 1; i < n; i++) {
+ int j = i;
+ if (!sequential_test_order) {
+ j = prandom_u32_max(i+1);
+ arr[i] = arr[j];
+ }
+ arr[j] = i;
}
}
static int test_func(void *private)
{
struct test_driver *t = private;
- int random_array[ARRAY_SIZE(test_case_array)];
+ u8 random_array[ARRAY_SIZE(test_case_array)];
int index, i, j;
ktime_t kt;
u64 delta;
@@ -391,11 +383,7 @@ static int test_func(void *private)
if (set_cpus_allowed_ptr(current, cpumask_of(t->cpu)) < 0)
pr_err("Failed to set affinity to %d CPU\n", t->cpu);
- for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
- random_array[i] = i;
-
- if (!sequential_test_order)
- shuffle_array(random_array, ARRAY_SIZE(test_case_array));
+ shuffle_array(random_array, ARRAY_SIZE(test_case_array));
/*
* Block until initialization is done.
--
2.26.0
reply other threads:[~2020-03-28 16:45 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=202003281643.02SGhJ0C010012@sdf.org \
--to=lkml@sdf.org \
--cc=akpm@linux-foundation.org \
--cc=guro@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=urezki@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®