Skip to contentSkip to conversation

kernel/locking/test-ww_mutex.c: Use cheaper prandom_u32_max()

RFC PATCH In review George Spelvin 1 participant 1 message Mar 28, 2020 Locking primitives

lore
Share & tools
lore
#0 lore raw

RFC PATCH 30/50 kernel/locking/test-ww_mutex.c: Use cheaper prandom_u32_max()

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?)

Cc Thomas Hellstrom <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Will Deacon <[email protected]>
---
 kernel/locking/test-ww_mutex.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)
kernel/locking/test-ww_mutex.c +8 -15
index 3e82f449b4ff7..3fc54d88eb842 100644
@@ -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 {
Signature · 1 line
2.26.0