test-ww_mutex.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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/random.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. static int *get_random_order(int count)
  321. {
  322. int *order;
  323. int n, r, tmp;
  324. order = kmalloc_array(count, sizeof(*order), GFP_KERNEL);
  325. if (!order)
  326. return order;
  327. for (n = 0; n < count; n++)
  328. order[n] = n;
  329. for (n = count - 1; n > 1; n--) {
  330. r = prandom_u32_max(n + 1);
  331. if (r != n) {
  332. tmp = order[n];
  333. order[n] = order[r];
  334. order[r] = tmp;
  335. }
  336. }
  337. return order;
  338. }
  339. static void dummy_load(struct stress *stress)
  340. {
  341. usleep_range(1000, 2000);
  342. }
  343. static void stress_inorder_work(struct work_struct *work)
  344. {
  345. struct stress *stress = container_of(work, typeof(*stress), work);
  346. const int nlocks = stress->nlocks;
  347. struct ww_mutex *locks = stress->locks;
  348. struct ww_acquire_ctx ctx;
  349. int *order;
  350. order = get_random_order(nlocks);
  351. if (!order)
  352. return;
  353. do {
  354. int contended = -1;
  355. int n, err;
  356. ww_acquire_init(&ctx, &ww_class);
  357. retry:
  358. err = 0;
  359. for (n = 0; n < nlocks; n++) {
  360. if (n == contended)
  361. continue;
  362. err = ww_mutex_lock(&locks[order[n]], &ctx);
  363. if (err < 0)
  364. break;
  365. }
  366. if (!err)
  367. dummy_load(stress);
  368. if (contended > n)
  369. ww_mutex_unlock(&locks[order[contended]]);
  370. contended = n;
  371. while (n--)
  372. ww_mutex_unlock(&locks[order[n]]);
  373. if (err == -EDEADLK) {
  374. ww_mutex_lock_slow(&locks[order[contended]], &ctx);
  375. goto retry;
  376. }
  377. if (err) {
  378. pr_err_once("stress (%s) failed with %d\n",
  379. __func__, err);
  380. break;
  381. }
  382. ww_acquire_fini(&ctx);
  383. } while (!time_after(jiffies, stress->timeout));
  384. kfree(order);
  385. }
  386. struct reorder_lock {
  387. struct list_head link;
  388. struct ww_mutex *lock;
  389. };
  390. static void stress_reorder_work(struct work_struct *work)
  391. {
  392. struct stress *stress = container_of(work, typeof(*stress), work);
  393. LIST_HEAD(locks);
  394. struct ww_acquire_ctx ctx;
  395. struct reorder_lock *ll, *ln;
  396. int *order;
  397. int n, err;
  398. order = get_random_order(stress->nlocks);
  399. if (!order)
  400. return;
  401. for (n = 0; n < stress->nlocks; n++) {
  402. ll = kmalloc(sizeof(*ll), GFP_KERNEL);
  403. if (!ll)
  404. goto out;
  405. ll->lock = &stress->locks[order[n]];
  406. list_add(&ll->link, &locks);
  407. }
  408. kfree(order);
  409. order = NULL;
  410. do {
  411. ww_acquire_init(&ctx, &ww_class);
  412. list_for_each_entry(ll, &locks, link) {
  413. err = ww_mutex_lock(ll->lock, &ctx);
  414. if (!err)
  415. continue;
  416. ln = ll;
  417. list_for_each_entry_continue_reverse(ln, &locks, link)
  418. ww_mutex_unlock(ln->lock);
  419. if (err != -EDEADLK) {
  420. pr_err_once("stress (%s) failed with %d\n",
  421. __func__, err);
  422. break;
  423. }
  424. ww_mutex_lock_slow(ll->lock, &ctx);
  425. list_move(&ll->link, &locks); /* restarts iteration */
  426. }
  427. dummy_load(stress);
  428. list_for_each_entry(ll, &locks, link)
  429. ww_mutex_unlock(ll->lock);
  430. ww_acquire_fini(&ctx);
  431. } while (!time_after(jiffies, stress->timeout));
  432. out:
  433. list_for_each_entry_safe(ll, ln, &locks, link)
  434. kfree(ll);
  435. kfree(order);
  436. }
  437. static void stress_one_work(struct work_struct *work)
  438. {
  439. struct stress *stress = container_of(work, typeof(*stress), work);
  440. const int nlocks = stress->nlocks;
  441. struct ww_mutex *lock = stress->locks + prandom_u32_max(nlocks);
  442. int err;
  443. do {
  444. err = ww_mutex_lock(lock, NULL);
  445. if (!err) {
  446. dummy_load(stress);
  447. ww_mutex_unlock(lock);
  448. } else {
  449. pr_err_once("stress (%s) failed with %d\n",
  450. __func__, err);
  451. break;
  452. }
  453. } while (!time_after(jiffies, stress->timeout));
  454. }
  455. #define STRESS_INORDER BIT(0)
  456. #define STRESS_REORDER BIT(1)
  457. #define STRESS_ONE BIT(2)
  458. #define STRESS_ALL (STRESS_INORDER | STRESS_REORDER | STRESS_ONE)
  459. static int stress(int nlocks, int nthreads, unsigned int flags)
  460. {
  461. struct ww_mutex *locks;
  462. struct stress *stress_array;
  463. int n, count;
  464. locks = kmalloc_array(nlocks, sizeof(*locks), GFP_KERNEL);
  465. if (!locks)
  466. return -ENOMEM;
  467. stress_array = kmalloc_array(nthreads, sizeof(*stress_array),
  468. GFP_KERNEL);
  469. if (!stress_array) {
  470. kfree(locks);
  471. return -ENOMEM;
  472. }
  473. for (n = 0; n < nlocks; n++)
  474. ww_mutex_init(&locks[n], &ww_class);
  475. count = 0;
  476. for (n = 0; nthreads; n++) {
  477. struct stress *stress;
  478. void (*fn)(struct work_struct *work);
  479. fn = NULL;
  480. switch (n & 3) {
  481. case 0:
  482. if (flags & STRESS_INORDER)
  483. fn = stress_inorder_work;
  484. break;
  485. case 1:
  486. if (flags & STRESS_REORDER)
  487. fn = stress_reorder_work;
  488. break;
  489. case 2:
  490. if (flags & STRESS_ONE)
  491. fn = stress_one_work;
  492. break;
  493. }
  494. if (!fn)
  495. continue;
  496. stress = &stress_array[count++];
  497. INIT_WORK(&stress->work, fn);
  498. stress->locks = locks;
  499. stress->nlocks = nlocks;
  500. stress->timeout = jiffies + 2*HZ;
  501. queue_work(wq, &stress->work);
  502. nthreads--;
  503. }
  504. flush_workqueue(wq);
  505. for (n = 0; n < nlocks; n++)
  506. ww_mutex_destroy(&locks[n]);
  507. kfree(stress_array);
  508. kfree(locks);
  509. return 0;
  510. }
  511. static int __init test_ww_mutex_init(void)
  512. {
  513. int ncpus = num_online_cpus();
  514. int ret, i;
  515. printk(KERN_INFO "Beginning ww mutex selftests\n");
  516. wq = alloc_workqueue("test-ww_mutex", WQ_UNBOUND, 0);
  517. if (!wq)
  518. return -ENOMEM;
  519. ret = test_mutex();
  520. if (ret)
  521. return ret;
  522. ret = test_aa(false);
  523. if (ret)
  524. return ret;
  525. ret = test_aa(true);
  526. if (ret)
  527. return ret;
  528. for (i = 0; i < 4; i++) {
  529. ret = test_abba(i & 1, i & 2);
  530. if (ret)
  531. return ret;
  532. }
  533. ret = test_cycle(ncpus);
  534. if (ret)
  535. return ret;
  536. ret = stress(16, 2*ncpus, STRESS_INORDER);
  537. if (ret)
  538. return ret;
  539. ret = stress(16, 2*ncpus, STRESS_REORDER);
  540. if (ret)
  541. return ret;
  542. ret = stress(4095, hweight32(STRESS_ALL)*ncpus, STRESS_ALL);
  543. if (ret)
  544. return ret;
  545. printk(KERN_INFO "All ww mutex selftests passed\n");
  546. return 0;
  547. }
  548. static void __exit test_ww_mutex_exit(void)
  549. {
  550. destroy_workqueue(wq);
  551. }
  552. module_init(test_ww_mutex_init);
  553. module_exit(test_ww_mutex_exit);
  554. MODULE_LICENSE("GPL");
  555. MODULE_AUTHOR("Intel Corporation");