test-ww_mutex.c 14 KB

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