Skip to content

Commit 4812c54

Browse files
johnstultz-workingomolnar
authored andcommitted
locking/ww_mutex/test: Use prng instead of rng to avoid hangs at bootup
Booting w/ qemu without kvm, and with 64 cpus, I noticed we'd sometimes hung task watchdog splats in get_random_u32_below() when using the test-ww_mutex stress test. While entropy exhaustion is no longer an issue, the RNG may be slower early in boot. The test-ww_mutex code will spawn off 128 threads (2x cpus) and each thread will call get_random_u32_below() a number of times to generate a random order of the 16 locks. This intense use takes time and without kvm, qemu can be slow enough that we trip the hung task watchdogs. For this test, we don't need true randomness, just mixed up orders for testing ww_mutex lock acquisitions, so it changes the logic to use the prng instead, which takes less time and avoids the watchdgos. Feedback would be appreciated! Signed-off-by: John Stultz <jstultz@google.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20230922043616.19282-2-jstultz@google.com
1 parent 0f4b5f9 commit 4812c54

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

kernel/locking/test-ww_mutex.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <linux/delay.h>
1010
#include <linux/kthread.h>
1111
#include <linux/module.h>
12-
#include <linux/random.h>
12+
#include <linux/prandom.h>
1313
#include <linux/slab.h>
1414
#include <linux/ww_mutex.h>
1515

@@ -386,6 +386,19 @@ struct stress {
386386
int nlocks;
387387
};
388388

389+
struct rnd_state rng;
390+
DEFINE_SPINLOCK(rng_lock);
391+
392+
static inline u32 prandom_u32_below(u32 ceil)
393+
{
394+
u32 ret;
395+
396+
spin_lock(&rng_lock);
397+
ret = prandom_u32_state(&rng) % ceil;
398+
spin_unlock(&rng_lock);
399+
return ret;
400+
}
401+
389402
static int *get_random_order(int count)
390403
{
391404
int *order;
@@ -399,7 +412,7 @@ static int *get_random_order(int count)
399412
order[n] = n;
400413

401414
for (n = count - 1; n > 1; n--) {
402-
r = get_random_u32_below(n + 1);
415+
r = prandom_u32_below(n + 1);
403416
if (r != n) {
404417
tmp = order[n];
405418
order[n] = order[r];
@@ -625,6 +638,8 @@ static int __init test_ww_mutex_init(void)
625638

626639
printk(KERN_INFO "Beginning ww mutex selftests\n");
627640

641+
prandom_seed_state(&rng, get_random_u64());
642+
628643
wq = alloc_workqueue("test-ww_mutex", WQ_UNBOUND, 0);
629644
if (!wq)
630645
return -ENOMEM;

0 commit comments

Comments
 (0)