1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Module-based API test facility for ww_mutexes
6 #include <linux/kernel.h>
8 #include <linux/completion.h>
9 #include <linux/delay.h>
10 #include <linux/kthread.h>
11 #include <linux/module.h>
12 #include <linux/prandom.h>
13 #include <linux/slab.h>
14 #include <linux/ww_mutex.h>
16 static DEFINE_WD_CLASS(ww_class);
17 struct workqueue_struct *wq;
19 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
20 #define ww_acquire_init_noinject(a, b) do { \
21 ww_acquire_init((a), (b)); \
22 (a)->deadlock_inject_countdown = ~0U; \
25 #define ww_acquire_init_noinject(a, b) ww_acquire_init((a), (b))
29 struct work_struct work;
30 struct ww_mutex mutex;
31 struct completion ready, go, done;
35 #define TEST_MTX_SPIN BIT(0)
36 #define TEST_MTX_TRY BIT(1)
37 #define TEST_MTX_CTX BIT(2)
38 #define __TEST_MTX_LAST BIT(3)
40 static void test_mutex_work(struct work_struct *work)
42 struct test_mutex *mtx = container_of(work, typeof(*mtx), work);
44 complete(&mtx->ready);
45 wait_for_completion(&mtx->go);
47 if (mtx->flags & TEST_MTX_TRY) {
48 while (!ww_mutex_trylock(&mtx->mutex, NULL))
51 ww_mutex_lock(&mtx->mutex, NULL);
54 ww_mutex_unlock(&mtx->mutex);
57 static int __test_mutex(unsigned int flags)
59 #define TIMEOUT (HZ / 16)
60 struct test_mutex mtx;
61 struct ww_acquire_ctx ctx;
64 ww_mutex_init(&mtx.mutex, &ww_class);
65 ww_acquire_init(&ctx, &ww_class);
67 INIT_WORK_ONSTACK(&mtx.work, test_mutex_work);
68 init_completion(&mtx.ready);
69 init_completion(&mtx.go);
70 init_completion(&mtx.done);
73 schedule_work(&mtx.work);
75 wait_for_completion(&mtx.ready);
76 ww_mutex_lock(&mtx.mutex, (flags & TEST_MTX_CTX) ? &ctx : NULL);
78 if (flags & TEST_MTX_SPIN) {
79 unsigned long timeout = jiffies + TIMEOUT;
83 if (completion_done(&mtx.done)) {
88 } while (time_before(jiffies, timeout));
90 ret = wait_for_completion_timeout(&mtx.done, TIMEOUT);
92 ww_mutex_unlock(&mtx.mutex);
93 ww_acquire_fini(&ctx);
96 pr_err("%s(flags=%x): mutual exclusion failure\n",
101 flush_work(&mtx.work);
102 destroy_work_on_stack(&mtx.work);
107 static int test_mutex(void)
112 for (i = 0; i < __TEST_MTX_LAST; i++) {
113 ret = __test_mutex(i);
121 static int test_aa(bool trylock)
123 struct ww_mutex mutex;
124 struct ww_acquire_ctx ctx;
126 const char *from = trylock ? "trylock" : "lock";
128 ww_mutex_init(&mutex, &ww_class);
129 ww_acquire_init(&ctx, &ww_class);
132 ret = ww_mutex_lock(&mutex, &ctx);
134 pr_err("%s: initial lock failed!\n", __func__);
138 ret = !ww_mutex_trylock(&mutex, &ctx);
140 pr_err("%s: initial trylock failed!\n", __func__);
145 if (ww_mutex_trylock(&mutex, NULL)) {
146 pr_err("%s: trylocked itself without context from %s!\n", __func__, from);
147 ww_mutex_unlock(&mutex);
152 if (ww_mutex_trylock(&mutex, &ctx)) {
153 pr_err("%s: trylocked itself with context from %s!\n", __func__, from);
154 ww_mutex_unlock(&mutex);
159 ret = ww_mutex_lock(&mutex, &ctx);
160 if (ret != -EALREADY) {
161 pr_err("%s: missed deadlock for recursing, ret=%d from %s\n",
162 __func__, ret, from);
164 ww_mutex_unlock(&mutex);
169 ww_mutex_unlock(&mutex);
172 ww_acquire_fini(&ctx);
177 struct work_struct work;
178 struct ww_mutex a_mutex;
179 struct ww_mutex b_mutex;
180 struct completion a_ready;
181 struct completion b_ready;
182 bool resolve, trylock;
186 static void test_abba_work(struct work_struct *work)
188 struct test_abba *abba = container_of(work, typeof(*abba), work);
189 struct ww_acquire_ctx ctx;
192 ww_acquire_init_noinject(&ctx, &ww_class);
194 ww_mutex_lock(&abba->b_mutex, &ctx);
196 WARN_ON(!ww_mutex_trylock(&abba->b_mutex, &ctx));
198 WARN_ON(READ_ONCE(abba->b_mutex.ctx) != &ctx);
200 complete(&abba->b_ready);
201 wait_for_completion(&abba->a_ready);
203 err = ww_mutex_lock(&abba->a_mutex, &ctx);
204 if (abba->resolve && err == -EDEADLK) {
205 ww_mutex_unlock(&abba->b_mutex);
206 ww_mutex_lock_slow(&abba->a_mutex, &ctx);
207 err = ww_mutex_lock(&abba->b_mutex, &ctx);
211 ww_mutex_unlock(&abba->a_mutex);
212 ww_mutex_unlock(&abba->b_mutex);
213 ww_acquire_fini(&ctx);
218 static int test_abba(bool trylock, bool resolve)
220 struct test_abba abba;
221 struct ww_acquire_ctx ctx;
224 ww_mutex_init(&abba.a_mutex, &ww_class);
225 ww_mutex_init(&abba.b_mutex, &ww_class);
226 INIT_WORK_ONSTACK(&abba.work, test_abba_work);
227 init_completion(&abba.a_ready);
228 init_completion(&abba.b_ready);
229 abba.trylock = trylock;
230 abba.resolve = resolve;
232 schedule_work(&abba.work);
234 ww_acquire_init_noinject(&ctx, &ww_class);
236 ww_mutex_lock(&abba.a_mutex, &ctx);
238 WARN_ON(!ww_mutex_trylock(&abba.a_mutex, &ctx));
240 WARN_ON(READ_ONCE(abba.a_mutex.ctx) != &ctx);
242 complete(&abba.a_ready);
243 wait_for_completion(&abba.b_ready);
245 err = ww_mutex_lock(&abba.b_mutex, &ctx);
246 if (resolve && err == -EDEADLK) {
247 ww_mutex_unlock(&abba.a_mutex);
248 ww_mutex_lock_slow(&abba.b_mutex, &ctx);
249 err = ww_mutex_lock(&abba.a_mutex, &ctx);
253 ww_mutex_unlock(&abba.b_mutex);
254 ww_mutex_unlock(&abba.a_mutex);
255 ww_acquire_fini(&ctx);
257 flush_work(&abba.work);
258 destroy_work_on_stack(&abba.work);
262 if (err || abba.result) {
263 pr_err("%s: failed to resolve ABBA deadlock, A err=%d, B err=%d\n",
264 __func__, err, abba.result);
268 if (err != -EDEADLK && abba.result != -EDEADLK) {
269 pr_err("%s: missed ABBA deadlock, A err=%d, B err=%d\n",
270 __func__, err, abba.result);
278 struct work_struct work;
279 struct ww_mutex a_mutex;
280 struct ww_mutex *b_mutex;
281 struct completion *a_signal;
282 struct completion b_signal;
286 static void test_cycle_work(struct work_struct *work)
288 struct test_cycle *cycle = container_of(work, typeof(*cycle), work);
289 struct ww_acquire_ctx ctx;
292 ww_acquire_init_noinject(&ctx, &ww_class);
293 ww_mutex_lock(&cycle->a_mutex, &ctx);
295 complete(cycle->a_signal);
296 wait_for_completion(&cycle->b_signal);
298 err = ww_mutex_lock(cycle->b_mutex, &ctx);
299 if (err == -EDEADLK) {
301 ww_mutex_unlock(&cycle->a_mutex);
302 ww_mutex_lock_slow(cycle->b_mutex, &ctx);
303 erra = ww_mutex_lock(&cycle->a_mutex, &ctx);
307 ww_mutex_unlock(cycle->b_mutex);
309 ww_mutex_unlock(&cycle->a_mutex);
310 ww_acquire_fini(&ctx);
312 cycle->result = err ?: erra;
315 static int __test_cycle(unsigned int nthreads)
317 struct test_cycle *cycles;
318 unsigned int n, last = nthreads - 1;
321 cycles = kmalloc_array(nthreads, sizeof(*cycles), GFP_KERNEL);
325 for (n = 0; n < nthreads; n++) {
326 struct test_cycle *cycle = &cycles[n];
328 ww_mutex_init(&cycle->a_mutex, &ww_class);
330 cycle->b_mutex = &cycles[0].a_mutex;
332 cycle->b_mutex = &cycles[n + 1].a_mutex;
335 cycle->a_signal = &cycles[last].b_signal;
337 cycle->a_signal = &cycles[n - 1].b_signal;
338 init_completion(&cycle->b_signal);
340 INIT_WORK(&cycle->work, test_cycle_work);
344 for (n = 0; n < nthreads; n++)
345 queue_work(wq, &cycles[n].work);
350 for (n = 0; n < nthreads; n++) {
351 struct test_cycle *cycle = &cycles[n];
356 pr_err("cyclic deadlock not resolved, ret[%d/%d] = %d\n",
357 n, nthreads, cycle->result);
362 for (n = 0; n < nthreads; n++)
363 ww_mutex_destroy(&cycles[n].a_mutex);
368 static int test_cycle(unsigned int ncpus)
373 for (n = 2; n <= ncpus + 1; n++) {
374 ret = __test_cycle(n);
383 struct work_struct work;
384 struct ww_mutex *locks;
385 unsigned long timeout;
389 struct rnd_state rng;
390 DEFINE_SPINLOCK(rng_lock);
392 static inline u32 prandom_u32_below(u32 ceil)
396 spin_lock(&rng_lock);
397 ret = prandom_u32_state(&rng) % ceil;
398 spin_unlock(&rng_lock);
402 static int *get_random_order(int count)
407 order = kmalloc_array(count, sizeof(*order), GFP_KERNEL);
411 for (n = 0; n < count; n++)
414 for (n = count - 1; n > 1; n--) {
415 r = prandom_u32_below(n + 1);
426 static void dummy_load(struct stress *stress)
428 usleep_range(1000, 2000);
431 static void stress_inorder_work(struct work_struct *work)
433 struct stress *stress = container_of(work, typeof(*stress), work);
434 const int nlocks = stress->nlocks;
435 struct ww_mutex *locks = stress->locks;
436 struct ww_acquire_ctx ctx;
439 order = get_random_order(nlocks);
447 ww_acquire_init(&ctx, &ww_class);
450 for (n = 0; n < nlocks; n++) {
454 err = ww_mutex_lock(&locks[order[n]], &ctx);
462 ww_mutex_unlock(&locks[order[contended]]);
465 ww_mutex_unlock(&locks[order[n]]);
467 if (err == -EDEADLK) {
468 if (!time_after(jiffies, stress->timeout)) {
469 ww_mutex_lock_slow(&locks[order[contended]], &ctx);
474 ww_acquire_fini(&ctx);
476 pr_err_once("stress (%s) failed with %d\n",
480 } while (!time_after(jiffies, stress->timeout));
485 struct reorder_lock {
486 struct list_head link;
487 struct ww_mutex *lock;
490 static void stress_reorder_work(struct work_struct *work)
492 struct stress *stress = container_of(work, typeof(*stress), work);
494 struct ww_acquire_ctx ctx;
495 struct reorder_lock *ll, *ln;
499 order = get_random_order(stress->nlocks);
503 for (n = 0; n < stress->nlocks; n++) {
504 ll = kmalloc(sizeof(*ll), GFP_KERNEL);
508 ll->lock = &stress->locks[order[n]];
509 list_add(&ll->link, &locks);
515 ww_acquire_init(&ctx, &ww_class);
517 list_for_each_entry(ll, &locks, link) {
518 err = ww_mutex_lock(ll->lock, &ctx);
523 list_for_each_entry_continue_reverse(ln, &locks, link)
524 ww_mutex_unlock(ln->lock);
526 if (err != -EDEADLK) {
527 pr_err_once("stress (%s) failed with %d\n",
532 ww_mutex_lock_slow(ll->lock, &ctx);
533 list_move(&ll->link, &locks); /* restarts iteration */
537 list_for_each_entry(ll, &locks, link)
538 ww_mutex_unlock(ll->lock);
540 ww_acquire_fini(&ctx);
541 } while (!time_after(jiffies, stress->timeout));
544 list_for_each_entry_safe(ll, ln, &locks, link)
549 static void stress_one_work(struct work_struct *work)
551 struct stress *stress = container_of(work, typeof(*stress), work);
552 const int nlocks = stress->nlocks;
553 struct ww_mutex *lock = stress->locks + get_random_u32_below(nlocks);
557 err = ww_mutex_lock(lock, NULL);
560 ww_mutex_unlock(lock);
562 pr_err_once("stress (%s) failed with %d\n",
566 } while (!time_after(jiffies, stress->timeout));
569 #define STRESS_INORDER BIT(0)
570 #define STRESS_REORDER BIT(1)
571 #define STRESS_ONE BIT(2)
572 #define STRESS_ALL (STRESS_INORDER | STRESS_REORDER | STRESS_ONE)
574 static int stress(int nlocks, int nthreads, unsigned int flags)
576 struct ww_mutex *locks;
577 struct stress *stress_array;
580 locks = kmalloc_array(nlocks, sizeof(*locks), GFP_KERNEL);
584 stress_array = kmalloc_array(nthreads, sizeof(*stress_array),
591 for (n = 0; n < nlocks; n++)
592 ww_mutex_init(&locks[n], &ww_class);
595 for (n = 0; nthreads; n++) {
596 struct stress *stress;
597 void (*fn)(struct work_struct *work);
602 if (flags & STRESS_INORDER)
603 fn = stress_inorder_work;
606 if (flags & STRESS_REORDER)
607 fn = stress_reorder_work;
610 if (flags & STRESS_ONE)
611 fn = stress_one_work;
618 stress = &stress_array[count++];
620 INIT_WORK(&stress->work, fn);
621 stress->locks = locks;
622 stress->nlocks = nlocks;
623 stress->timeout = jiffies + 2*HZ;
625 queue_work(wq, &stress->work);
631 for (n = 0; n < nlocks; n++)
632 ww_mutex_destroy(&locks[n]);
639 static int __init test_ww_mutex_init(void)
641 int ncpus = num_online_cpus();
644 printk(KERN_INFO "Beginning ww mutex selftests\n");
646 prandom_seed_state(&rng, get_random_u64());
648 wq = alloc_workqueue("test-ww_mutex", WQ_UNBOUND, 0);
656 ret = test_aa(false);
664 for (i = 0; i < 4; i++) {
665 ret = test_abba(i & 1, i & 2);
670 ret = test_cycle(ncpus);
674 ret = stress(16, 2*ncpus, STRESS_INORDER);
678 ret = stress(16, 2*ncpus, STRESS_REORDER);
682 ret = stress(2047, hweight32(STRESS_ALL)*ncpus, STRESS_ALL);
686 printk(KERN_INFO "All ww mutex selftests passed\n");
690 static void __exit test_ww_mutex_exit(void)
692 destroy_workqueue(wq);
695 module_init(test_ww_mutex_init);
696 module_exit(test_ww_mutex_exit);
698 MODULE_LICENSE("GPL");
699 MODULE_AUTHOR("Intel Corporation");