From patchwork Sun Mar 8 10:09:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Spelvin X-Patchwork-Id: 11463639 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2ADD5913 for ; Sat, 28 Mar 2020 16:43:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 00EB1206F6 for ; Sat, 28 Mar 2020 16:43:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 00EB1206F6 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sdf.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2AE128E0001; Sat, 28 Mar 2020 12:43:27 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 25D3B6B0037; Sat, 28 Mar 2020 12:43:27 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 176718E0001; Sat, 28 Mar 2020 12:43:27 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0080.hostedemail.com [216.40.44.80]) by kanga.kvack.org (Postfix) with ESMTP id EFDAC6B0036 for ; Sat, 28 Mar 2020 12:43:26 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 44099180AD80F for ; Sat, 28 Mar 2020 16:43:26 +0000 (UTC) X-FDA: 76645341612.02.flock97_75514d1603d33 X-Spam-Summary: 2,0,0,e718d0cf2b48e3de,d41d8cd98f00b204,lkml@sdf.org,,RULES_HIT:41:69:355:379:560:800:960:966:973:988:989:1260:1345:1431:1437:1534:1544:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2693:3138:3139:3140:3141:3142:3165:3353:3865:3867:3868:3871:3872:4321:4385:5007:6119:6120:6261:7514:7903:7904:8603:8660:9592:10004:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12663:12683:12895:13148:13230:13846:14181:14721:21080:21451:21627:21990:30012:30034:30054:30056:30064,0,RBL:205.166.94.20:@sdf.org:.lbl8.mailshell.net-62.8.0.100 64.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: flock97_75514d1603d33 X-Filterd-Recvd-Size: 4947 Received: from mx.sdf.org (mx.sdf.org [205.166.94.20]) by imf34.hostedemail.com (Postfix) with ESMTP for ; Sat, 28 Mar 2020 16:43:25 +0000 (UTC) Received: from sdf.org (IDENT:lkml@sdf.lonestar.org [205.166.94.16]) by mx.sdf.org (8.15.2/8.14.5) with ESMTPS id 02SGhJqQ014207 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO); Sat, 28 Mar 2020 16:43:20 GMT Received: (from lkml@localhost) by sdf.org (8.15.2/8.12.8/Submit) id 02SGhJ0C010012; Sat, 28 Mar 2020 16:43:19 GMT Message-Id: <202003281643.02SGhJ0C010012@sdf.org> From: George Spelvin Date: Sun, 8 Mar 2020 06:09:17 -0400 Subject: [RFC PATCH v1 33/50] lib/test_vmalloc.c: Use pseudorandom numbers To: linux-kernel@vger.kernel.org, lkml@sdf.org Cc: Uladzislau Rezki , Andrew Morton , Roman Gushchin , linux-mm@kvack.org X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Cc: Uladzislau Rezki (Sony) Cc: Andrew Morton Cc: Roman Gushchin 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.