All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH v1 30/50] kernel/locking/test-ww_mutex.c: Use cheaper prandom_u32_max()
@ 2019-11-29 22:10 George Spelvin
  0 siblings, 0 replies; only message in thread
From: George Spelvin @ 2019-11-29 22:10 UTC (permalink / raw)
  To: linux-kernel, lkml
  Cc: Thomas Hellstrom, Peter Zijlstra, Ingo Molnar, Will Deacon

This is test code; it doesn't need crypto-quality random numbers.

Also use the simpler initialize-while-shuffling variant of the
Fisher-Yates shuffle in get_random_order().

(It's not clear that we need the order[] array at all.
Could we just shuffle the stress->locks array directly?)

Signed-off-by: George Spelvin <lkml@sdf.org>
Cc: Thomas Hellstrom <thellstrom@vmware.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Will Deacon <will@kernel.org>
---
 kernel/locking/test-ww_mutex.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c
index 3e82f449b4ff7..3fc54d88eb842 100644
--- a/kernel/locking/test-ww_mutex.c
+++ b/kernel/locking/test-ww_mutex.c
@@ -348,25 +348,18 @@ struct stress {
 
 static int *get_random_order(int count)
 {
-	int *order;
-	int n, r, tmp;
+	int n, *order;
 
 	order = kmalloc_array(count, sizeof(*order), GFP_KERNEL);
-	if (!order)
+	if (!order || !count)
 		return order;
 
-	for (n = 0; n < count; n++)
-		order[n] = n;
-
-	for (n = count - 1; n > 1; n--) {
-		r = get_random_int() % (n + 1);
-		if (r != n) {
-			tmp = order[n];
-			order[n] = order[r];
-			order[r] = tmp;
-		}
+	order[0] = 0;
+	for (n = 1; n < count; n++) {
+		int r = prandom_u32_max(n+1);
+		order[n] = order[r];
+		order[r] = n;
 	}
-
 	return order;
 }
 
@@ -498,7 +491,7 @@ static void stress_one_work(struct work_struct *work)
 {
 	struct stress *stress = container_of(work, typeof(*stress), work);
 	const int nlocks = stress->nlocks;
-	struct ww_mutex *lock = stress->locks + (get_random_int() % nlocks);
+	struct ww_mutex *lock = stress->locks + prandom_u32_max(nlocks);
 	int err;
 
 	do {
-- 
2.26.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-03-28 16:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-29 22:10 [RFC PATCH v1 30/50] kernel/locking/test-ww_mutex.c: Use cheaper prandom_u32_max() George Spelvin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.