[RFC PATCH v1 30/50] kernel/locking/test-ww_mutex.c: Use cheaper prandom_u32_max()
From: George Spelvin
Date: Sat Mar 28 2020 - 12:44:59 EST
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@xxxxxxx>
Cc: Thomas Hellstrom <thellstrom@xxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Will Deacon <will@xxxxxxxxxx>
---
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