^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Module-based API test facility for ww_mutexes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/ww_mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static DEFINE_WD_CLASS(ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct workqueue_struct *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct test_mutex {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct ww_mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct completion ready, go, done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define TEST_MTX_SPIN BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define TEST_MTX_TRY BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define TEST_MTX_CTX BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define __TEST_MTX_LAST BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void test_mutex_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct test_mutex *mtx = container_of(work, typeof(*mtx), work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) complete(&mtx->ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) wait_for_completion(&mtx->go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (mtx->flags & TEST_MTX_TRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) while (!ww_mutex_trylock(&mtx->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ww_mutex_lock(&mtx->mutex, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) complete(&mtx->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ww_mutex_unlock(&mtx->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int __test_mutex(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define TIMEOUT (HZ / 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct test_mutex mtx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct ww_acquire_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ww_mutex_init(&mtx.mutex, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ww_acquire_init(&ctx, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) INIT_WORK_ONSTACK(&mtx.work, test_mutex_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) init_completion(&mtx.ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) init_completion(&mtx.go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) init_completion(&mtx.done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) mtx.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) schedule_work(&mtx.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) wait_for_completion(&mtx.ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ww_mutex_lock(&mtx.mutex, (flags & TEST_MTX_CTX) ? &ctx : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) complete(&mtx.go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (flags & TEST_MTX_SPIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned long timeout = jiffies + TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (completion_done(&mtx.done)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) } while (time_before(jiffies, timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ret = wait_for_completion_timeout(&mtx.done, TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ww_mutex_unlock(&mtx.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ww_acquire_fini(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) pr_err("%s(flags=%x): mutual exclusion failure\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) __func__, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) flush_work(&mtx.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) destroy_work_on_stack(&mtx.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #undef TIMEOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int test_mutex(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) for (i = 0; i < __TEST_MTX_LAST; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = __test_mutex(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int test_aa(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct ww_mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct ww_acquire_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ww_mutex_init(&mutex, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ww_acquire_init(&ctx, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ww_mutex_lock(&mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (ww_mutex_trylock(&mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pr_err("%s: trylocked itself!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ww_mutex_unlock(&mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ret = ww_mutex_lock(&mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (ret != -EALREADY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pr_err("%s: missed deadlock for recursing, ret=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ww_mutex_unlock(&mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ww_mutex_unlock(&mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ww_acquire_fini(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct test_abba {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct ww_mutex a_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct ww_mutex b_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct completion a_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct completion b_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) bool resolve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void test_abba_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct test_abba *abba = container_of(work, typeof(*abba), work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct ww_acquire_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ww_acquire_init(&ctx, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ww_mutex_lock(&abba->b_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) complete(&abba->b_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) wait_for_completion(&abba->a_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) err = ww_mutex_lock(&abba->a_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (abba->resolve && err == -EDEADLK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ww_mutex_unlock(&abba->b_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ww_mutex_lock_slow(&abba->a_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) err = ww_mutex_lock(&abba->b_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ww_mutex_unlock(&abba->a_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ww_mutex_unlock(&abba->b_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ww_acquire_fini(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) abba->result = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int test_abba(bool resolve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct test_abba abba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct ww_acquire_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int err, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ww_mutex_init(&abba.a_mutex, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ww_mutex_init(&abba.b_mutex, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) INIT_WORK_ONSTACK(&abba.work, test_abba_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) init_completion(&abba.a_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) init_completion(&abba.b_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) abba.resolve = resolve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) schedule_work(&abba.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ww_acquire_init(&ctx, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ww_mutex_lock(&abba.a_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) complete(&abba.a_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) wait_for_completion(&abba.b_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) err = ww_mutex_lock(&abba.b_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (resolve && err == -EDEADLK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ww_mutex_unlock(&abba.a_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ww_mutex_lock_slow(&abba.b_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) err = ww_mutex_lock(&abba.a_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ww_mutex_unlock(&abba.b_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ww_mutex_unlock(&abba.a_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ww_acquire_fini(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) flush_work(&abba.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) destroy_work_on_stack(&abba.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (resolve) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (err || abba.result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) pr_err("%s: failed to resolve ABBA deadlock, A err=%d, B err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) __func__, err, abba.result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (err != -EDEADLK && abba.result != -EDEADLK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) pr_err("%s: missed ABBA deadlock, A err=%d, B err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) __func__, err, abba.result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct test_cycle {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct ww_mutex a_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct ww_mutex *b_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct completion *a_signal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct completion b_signal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static void test_cycle_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct test_cycle *cycle = container_of(work, typeof(*cycle), work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct ww_acquire_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) int err, erra = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ww_acquire_init(&ctx, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ww_mutex_lock(&cycle->a_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) complete(cycle->a_signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) wait_for_completion(&cycle->b_signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) err = ww_mutex_lock(cycle->b_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (err == -EDEADLK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ww_mutex_unlock(&cycle->a_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) ww_mutex_lock_slow(cycle->b_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) erra = ww_mutex_lock(&cycle->a_mutex, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) ww_mutex_unlock(cycle->b_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (!erra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ww_mutex_unlock(&cycle->a_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ww_acquire_fini(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) cycle->result = err ?: erra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int __test_cycle(unsigned int nthreads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct test_cycle *cycles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) unsigned int n, last = nthreads - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) cycles = kmalloc_array(nthreads, sizeof(*cycles), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!cycles)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) for (n = 0; n < nthreads; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct test_cycle *cycle = &cycles[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) ww_mutex_init(&cycle->a_mutex, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (n == last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) cycle->b_mutex = &cycles[0].a_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) cycle->b_mutex = &cycles[n + 1].a_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) cycle->a_signal = &cycles[last].b_signal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) cycle->a_signal = &cycles[n - 1].b_signal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) init_completion(&cycle->b_signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) INIT_WORK(&cycle->work, test_cycle_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) cycle->result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) for (n = 0; n < nthreads; n++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) queue_work(wq, &cycles[n].work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) flush_workqueue(wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) for (n = 0; n < nthreads; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct test_cycle *cycle = &cycles[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (!cycle->result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) pr_err("cyclic deadlock not resolved, ret[%d/%d] = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) n, nthreads, cycle->result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) for (n = 0; n < nthreads; n++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) ww_mutex_destroy(&cycles[n].a_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) kfree(cycles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int test_cycle(unsigned int ncpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) unsigned int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) for (n = 2; n <= ncpus + 1; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = __test_cycle(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct stress {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct ww_mutex *locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int nlocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int *get_random_order(int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int *order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int n, r, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) order = kmalloc_array(count, sizeof(*order), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!order)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) for (n = 0; n < count; n++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) order[n] = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) for (n = count - 1; n > 1; n--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) r = get_random_int() % (n + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (r != n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) tmp = order[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) order[n] = order[r];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) order[r] = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static void dummy_load(struct stress *stress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) usleep_range(1000, 2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static void stress_inorder_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct stress *stress = container_of(work, typeof(*stress), work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) const int nlocks = stress->nlocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct ww_mutex *locks = stress->locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct ww_acquire_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) int *order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) order = get_random_order(nlocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!order)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) int contended = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int n, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) ww_acquire_init(&ctx, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) for (n = 0; n < nlocks; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (n == contended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) err = ww_mutex_lock(&locks[order[n]], &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) dummy_load(stress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (contended > n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ww_mutex_unlock(&locks[order[contended]]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) contended = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) while (n--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ww_mutex_unlock(&locks[order[n]]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (err == -EDEADLK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) ww_mutex_lock_slow(&locks[order[contended]], &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) pr_err_once("stress (%s) failed with %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) ww_acquire_fini(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) } while (!time_after(jiffies, stress->timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) kfree(order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) kfree(stress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct reorder_lock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct list_head link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) struct ww_mutex *lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static void stress_reorder_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct stress *stress = container_of(work, typeof(*stress), work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) LIST_HEAD(locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) struct ww_acquire_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) struct reorder_lock *ll, *ln;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) int *order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) int n, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) order = get_random_order(stress->nlocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (!order)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) for (n = 0; n < stress->nlocks; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) ll = kmalloc(sizeof(*ll), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (!ll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ll->lock = &stress->locks[order[n]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) list_add(&ll->link, &locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) kfree(order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) order = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) ww_acquire_init(&ctx, &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) list_for_each_entry(ll, &locks, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) err = ww_mutex_lock(ll->lock, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ln = ll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) list_for_each_entry_continue_reverse(ln, &locks, link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) ww_mutex_unlock(ln->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (err != -EDEADLK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) pr_err_once("stress (%s) failed with %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) ww_mutex_lock_slow(ll->lock, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) list_move(&ll->link, &locks); /* restarts iteration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) dummy_load(stress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) list_for_each_entry(ll, &locks, link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ww_mutex_unlock(ll->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) ww_acquire_fini(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) } while (!time_after(jiffies, stress->timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) list_for_each_entry_safe(ll, ln, &locks, link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) kfree(ll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) kfree(order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) kfree(stress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static void stress_one_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct stress *stress = container_of(work, typeof(*stress), work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) const int nlocks = stress->nlocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct ww_mutex *lock = stress->locks + (get_random_int() % nlocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) err = ww_mutex_lock(lock, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) dummy_load(stress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) ww_mutex_unlock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) pr_err_once("stress (%s) failed with %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) } while (!time_after(jiffies, stress->timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) kfree(stress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) #define STRESS_INORDER BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) #define STRESS_REORDER BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) #define STRESS_ONE BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) #define STRESS_ALL (STRESS_INORDER | STRESS_REORDER | STRESS_ONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static int stress(int nlocks, int nthreads, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct ww_mutex *locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) locks = kmalloc_array(nlocks, sizeof(*locks), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (!locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) for (n = 0; n < nlocks; n++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ww_mutex_init(&locks[n], &ww_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) for (n = 0; nthreads; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) struct stress *stress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) void (*fn)(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) fn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) switch (n & 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (flags & STRESS_INORDER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) fn = stress_inorder_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (flags & STRESS_REORDER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) fn = stress_reorder_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (flags & STRESS_ONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) fn = stress_one_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) stress = kmalloc(sizeof(*stress), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (!stress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) INIT_WORK(&stress->work, fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) stress->locks = locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) stress->nlocks = nlocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) stress->timeout = jiffies + 2*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) queue_work(wq, &stress->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) nthreads--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) flush_workqueue(wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) for (n = 0; n < nlocks; n++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) ww_mutex_destroy(&locks[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) kfree(locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) static int __init test_ww_mutex_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) int ncpus = num_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) wq = alloc_workqueue("test-ww_mutex", WQ_UNBOUND, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) ret = test_mutex();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) ret = test_aa();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) ret = test_abba(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) ret = test_abba(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) ret = test_cycle(ncpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) ret = stress(16, 2*ncpus, STRESS_INORDER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) ret = stress(16, 2*ncpus, STRESS_REORDER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) ret = stress(4095, hweight32(STRESS_ALL)*ncpus, STRESS_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) static void __exit test_ww_mutex_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) destroy_workqueue(wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) module_init(test_ww_mutex_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) module_exit(test_ww_mutex_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) MODULE_AUTHOR("Intel Corporation");