locktorture.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. * Module-based torture test facility for locking
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright (C) IBM Corporation, 2014
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. * Davidlohr Bueso <dave@stgolabs.net>
  22. * Based on kernel/rcu/torture.c.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/kthread.h>
  27. #include <linux/sched/rt.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/rwlock.h>
  30. #include <linux/mutex.h>
  31. #include <linux/rwsem.h>
  32. #include <linux/smp.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/sched.h>
  35. #include <uapi/linux/sched/types.h>
  36. #include <linux/atomic.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/delay.h>
  39. #include <linux/slab.h>
  40. #include <linux/percpu-rwsem.h>
  41. #include <linux/torture.h>
  42. MODULE_LICENSE("GPL");
  43. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
  44. torture_param(int, nwriters_stress, -1,
  45. "Number of write-locking stress-test threads");
  46. torture_param(int, nreaders_stress, -1,
  47. "Number of read-locking stress-test threads");
  48. torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
  49. torture_param(int, onoff_interval, 0,
  50. "Time between CPU hotplugs (s), 0=disable");
  51. torture_param(int, shuffle_interval, 3,
  52. "Number of jiffies between shuffles, 0=disable");
  53. torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
  54. torture_param(int, stat_interval, 60,
  55. "Number of seconds between stats printk()s");
  56. torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
  57. torture_param(bool, verbose, true,
  58. "Enable verbose debugging printk()s");
  59. static char *torture_type = "spin_lock";
  60. module_param(torture_type, charp, 0444);
  61. MODULE_PARM_DESC(torture_type,
  62. "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
  63. static struct task_struct *stats_task;
  64. static struct task_struct **writer_tasks;
  65. static struct task_struct **reader_tasks;
  66. static bool lock_is_write_held;
  67. static bool lock_is_read_held;
  68. struct lock_stress_stats {
  69. long n_lock_fail;
  70. long n_lock_acquired;
  71. };
  72. int torture_runnable = IS_ENABLED(MODULE);
  73. module_param(torture_runnable, int, 0444);
  74. MODULE_PARM_DESC(torture_runnable, "Start locktorture at module init");
  75. /* Forward reference. */
  76. static void lock_torture_cleanup(void);
  77. /*
  78. * Operations vector for selecting different types of tests.
  79. */
  80. struct lock_torture_ops {
  81. void (*init)(void);
  82. int (*writelock)(void);
  83. void (*write_delay)(struct torture_random_state *trsp);
  84. void (*task_boost)(struct torture_random_state *trsp);
  85. void (*writeunlock)(void);
  86. int (*readlock)(void);
  87. void (*read_delay)(struct torture_random_state *trsp);
  88. void (*readunlock)(void);
  89. unsigned long flags; /* for irq spinlocks */
  90. const char *name;
  91. };
  92. struct lock_torture_cxt {
  93. int nrealwriters_stress;
  94. int nrealreaders_stress;
  95. bool debug_lock;
  96. atomic_t n_lock_torture_errors;
  97. struct lock_torture_ops *cur_ops;
  98. struct lock_stress_stats *lwsa; /* writer statistics */
  99. struct lock_stress_stats *lrsa; /* reader statistics */
  100. };
  101. static struct lock_torture_cxt cxt = { 0, 0, false,
  102. ATOMIC_INIT(0),
  103. NULL, NULL};
  104. /*
  105. * Definitions for lock torture testing.
  106. */
  107. static int torture_lock_busted_write_lock(void)
  108. {
  109. return 0; /* BUGGY, do not use in real life!!! */
  110. }
  111. static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
  112. {
  113. const unsigned long longdelay_ms = 100;
  114. /* We want a long delay occasionally to force massive contention. */
  115. if (!(torture_random(trsp) %
  116. (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
  117. mdelay(longdelay_ms);
  118. #ifdef CONFIG_PREEMPT
  119. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  120. preempt_schedule(); /* Allow test to be preempted. */
  121. #endif
  122. }
  123. static void torture_lock_busted_write_unlock(void)
  124. {
  125. /* BUGGY, do not use in real life!!! */
  126. }
  127. static void torture_boost_dummy(struct torture_random_state *trsp)
  128. {
  129. /* Only rtmutexes care about priority */
  130. }
  131. static struct lock_torture_ops lock_busted_ops = {
  132. .writelock = torture_lock_busted_write_lock,
  133. .write_delay = torture_lock_busted_write_delay,
  134. .task_boost = torture_boost_dummy,
  135. .writeunlock = torture_lock_busted_write_unlock,
  136. .readlock = NULL,
  137. .read_delay = NULL,
  138. .readunlock = NULL,
  139. .name = "lock_busted"
  140. };
  141. static DEFINE_SPINLOCK(torture_spinlock);
  142. static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
  143. {
  144. spin_lock(&torture_spinlock);
  145. return 0;
  146. }
  147. static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
  148. {
  149. const unsigned long shortdelay_us = 2;
  150. const unsigned long longdelay_ms = 100;
  151. /* We want a short delay mostly to emulate likely code, and
  152. * we want a long delay occasionally to force massive contention.
  153. */
  154. if (!(torture_random(trsp) %
  155. (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
  156. mdelay(longdelay_ms);
  157. if (!(torture_random(trsp) %
  158. (cxt.nrealwriters_stress * 2 * shortdelay_us)))
  159. udelay(shortdelay_us);
  160. #ifdef CONFIG_PREEMPT
  161. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  162. preempt_schedule(); /* Allow test to be preempted. */
  163. #endif
  164. }
  165. static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
  166. {
  167. spin_unlock(&torture_spinlock);
  168. }
  169. static struct lock_torture_ops spin_lock_ops = {
  170. .writelock = torture_spin_lock_write_lock,
  171. .write_delay = torture_spin_lock_write_delay,
  172. .task_boost = torture_boost_dummy,
  173. .writeunlock = torture_spin_lock_write_unlock,
  174. .readlock = NULL,
  175. .read_delay = NULL,
  176. .readunlock = NULL,
  177. .name = "spin_lock"
  178. };
  179. static int torture_spin_lock_write_lock_irq(void)
  180. __acquires(torture_spinlock)
  181. {
  182. unsigned long flags;
  183. spin_lock_irqsave(&torture_spinlock, flags);
  184. cxt.cur_ops->flags = flags;
  185. return 0;
  186. }
  187. static void torture_lock_spin_write_unlock_irq(void)
  188. __releases(torture_spinlock)
  189. {
  190. spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags);
  191. }
  192. static struct lock_torture_ops spin_lock_irq_ops = {
  193. .writelock = torture_spin_lock_write_lock_irq,
  194. .write_delay = torture_spin_lock_write_delay,
  195. .task_boost = torture_boost_dummy,
  196. .writeunlock = torture_lock_spin_write_unlock_irq,
  197. .readlock = NULL,
  198. .read_delay = NULL,
  199. .readunlock = NULL,
  200. .name = "spin_lock_irq"
  201. };
  202. static DEFINE_RWLOCK(torture_rwlock);
  203. static int torture_rwlock_write_lock(void) __acquires(torture_rwlock)
  204. {
  205. write_lock(&torture_rwlock);
  206. return 0;
  207. }
  208. static void torture_rwlock_write_delay(struct torture_random_state *trsp)
  209. {
  210. const unsigned long shortdelay_us = 2;
  211. const unsigned long longdelay_ms = 100;
  212. /* We want a short delay mostly to emulate likely code, and
  213. * we want a long delay occasionally to force massive contention.
  214. */
  215. if (!(torture_random(trsp) %
  216. (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
  217. mdelay(longdelay_ms);
  218. else
  219. udelay(shortdelay_us);
  220. }
  221. static void torture_rwlock_write_unlock(void) __releases(torture_rwlock)
  222. {
  223. write_unlock(&torture_rwlock);
  224. }
  225. static int torture_rwlock_read_lock(void) __acquires(torture_rwlock)
  226. {
  227. read_lock(&torture_rwlock);
  228. return 0;
  229. }
  230. static void torture_rwlock_read_delay(struct torture_random_state *trsp)
  231. {
  232. const unsigned long shortdelay_us = 10;
  233. const unsigned long longdelay_ms = 100;
  234. /* We want a short delay mostly to emulate likely code, and
  235. * we want a long delay occasionally to force massive contention.
  236. */
  237. if (!(torture_random(trsp) %
  238. (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
  239. mdelay(longdelay_ms);
  240. else
  241. udelay(shortdelay_us);
  242. }
  243. static void torture_rwlock_read_unlock(void) __releases(torture_rwlock)
  244. {
  245. read_unlock(&torture_rwlock);
  246. }
  247. static struct lock_torture_ops rw_lock_ops = {
  248. .writelock = torture_rwlock_write_lock,
  249. .write_delay = torture_rwlock_write_delay,
  250. .task_boost = torture_boost_dummy,
  251. .writeunlock = torture_rwlock_write_unlock,
  252. .readlock = torture_rwlock_read_lock,
  253. .read_delay = torture_rwlock_read_delay,
  254. .readunlock = torture_rwlock_read_unlock,
  255. .name = "rw_lock"
  256. };
  257. static int torture_rwlock_write_lock_irq(void) __acquires(torture_rwlock)
  258. {
  259. unsigned long flags;
  260. write_lock_irqsave(&torture_rwlock, flags);
  261. cxt.cur_ops->flags = flags;
  262. return 0;
  263. }
  264. static void torture_rwlock_write_unlock_irq(void)
  265. __releases(torture_rwlock)
  266. {
  267. write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
  268. }
  269. static int torture_rwlock_read_lock_irq(void) __acquires(torture_rwlock)
  270. {
  271. unsigned long flags;
  272. read_lock_irqsave(&torture_rwlock, flags);
  273. cxt.cur_ops->flags = flags;
  274. return 0;
  275. }
  276. static void torture_rwlock_read_unlock_irq(void)
  277. __releases(torture_rwlock)
  278. {
  279. read_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
  280. }
  281. static struct lock_torture_ops rw_lock_irq_ops = {
  282. .writelock = torture_rwlock_write_lock_irq,
  283. .write_delay = torture_rwlock_write_delay,
  284. .task_boost = torture_boost_dummy,
  285. .writeunlock = torture_rwlock_write_unlock_irq,
  286. .readlock = torture_rwlock_read_lock_irq,
  287. .read_delay = torture_rwlock_read_delay,
  288. .readunlock = torture_rwlock_read_unlock_irq,
  289. .name = "rw_lock_irq"
  290. };
  291. static DEFINE_MUTEX(torture_mutex);
  292. static int torture_mutex_lock(void) __acquires(torture_mutex)
  293. {
  294. mutex_lock(&torture_mutex);
  295. return 0;
  296. }
  297. static void torture_mutex_delay(struct torture_random_state *trsp)
  298. {
  299. const unsigned long longdelay_ms = 100;
  300. /* We want a long delay occasionally to force massive contention. */
  301. if (!(torture_random(trsp) %
  302. (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
  303. mdelay(longdelay_ms * 5);
  304. else
  305. mdelay(longdelay_ms / 5);
  306. #ifdef CONFIG_PREEMPT
  307. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  308. preempt_schedule(); /* Allow test to be preempted. */
  309. #endif
  310. }
  311. static void torture_mutex_unlock(void) __releases(torture_mutex)
  312. {
  313. mutex_unlock(&torture_mutex);
  314. }
  315. static struct lock_torture_ops mutex_lock_ops = {
  316. .writelock = torture_mutex_lock,
  317. .write_delay = torture_mutex_delay,
  318. .task_boost = torture_boost_dummy,
  319. .writeunlock = torture_mutex_unlock,
  320. .readlock = NULL,
  321. .read_delay = NULL,
  322. .readunlock = NULL,
  323. .name = "mutex_lock"
  324. };
  325. #include <linux/ww_mutex.h>
  326. static DEFINE_WW_CLASS(torture_ww_class);
  327. static DEFINE_WW_MUTEX(torture_ww_mutex_0, &torture_ww_class);
  328. static DEFINE_WW_MUTEX(torture_ww_mutex_1, &torture_ww_class);
  329. static DEFINE_WW_MUTEX(torture_ww_mutex_2, &torture_ww_class);
  330. static int torture_ww_mutex_lock(void)
  331. __acquires(torture_ww_mutex_0)
  332. __acquires(torture_ww_mutex_1)
  333. __acquires(torture_ww_mutex_2)
  334. {
  335. LIST_HEAD(list);
  336. struct reorder_lock {
  337. struct list_head link;
  338. struct ww_mutex *lock;
  339. } locks[3], *ll, *ln;
  340. struct ww_acquire_ctx ctx;
  341. locks[0].lock = &torture_ww_mutex_0;
  342. list_add(&locks[0].link, &list);
  343. locks[1].lock = &torture_ww_mutex_1;
  344. list_add(&locks[1].link, &list);
  345. locks[2].lock = &torture_ww_mutex_2;
  346. list_add(&locks[2].link, &list);
  347. ww_acquire_init(&ctx, &torture_ww_class);
  348. list_for_each_entry(ll, &list, link) {
  349. int err;
  350. err = ww_mutex_lock(ll->lock, &ctx);
  351. if (!err)
  352. continue;
  353. ln = ll;
  354. list_for_each_entry_continue_reverse(ln, &list, link)
  355. ww_mutex_unlock(ln->lock);
  356. if (err != -EDEADLK)
  357. return err;
  358. ww_mutex_lock_slow(ll->lock, &ctx);
  359. list_move(&ll->link, &list);
  360. }
  361. ww_acquire_fini(&ctx);
  362. return 0;
  363. }
  364. static void torture_ww_mutex_unlock(void)
  365. __releases(torture_ww_mutex_0)
  366. __releases(torture_ww_mutex_1)
  367. __releases(torture_ww_mutex_2)
  368. {
  369. ww_mutex_unlock(&torture_ww_mutex_0);
  370. ww_mutex_unlock(&torture_ww_mutex_1);
  371. ww_mutex_unlock(&torture_ww_mutex_2);
  372. }
  373. static struct lock_torture_ops ww_mutex_lock_ops = {
  374. .writelock = torture_ww_mutex_lock,
  375. .write_delay = torture_mutex_delay,
  376. .task_boost = torture_boost_dummy,
  377. .writeunlock = torture_ww_mutex_unlock,
  378. .readlock = NULL,
  379. .read_delay = NULL,
  380. .readunlock = NULL,
  381. .name = "ww_mutex_lock"
  382. };
  383. #ifdef CONFIG_RT_MUTEXES
  384. static DEFINE_RT_MUTEX(torture_rtmutex);
  385. static int torture_rtmutex_lock(void) __acquires(torture_rtmutex)
  386. {
  387. rt_mutex_lock(&torture_rtmutex);
  388. return 0;
  389. }
  390. static void torture_rtmutex_boost(struct torture_random_state *trsp)
  391. {
  392. int policy;
  393. struct sched_param param;
  394. const unsigned int factor = 50000; /* yes, quite arbitrary */
  395. if (!rt_task(current)) {
  396. /*
  397. * Boost priority once every ~50k operations. When the
  398. * task tries to take the lock, the rtmutex it will account
  399. * for the new priority, and do any corresponding pi-dance.
  400. */
  401. if (trsp && !(torture_random(trsp) %
  402. (cxt.nrealwriters_stress * factor))) {
  403. policy = SCHED_FIFO;
  404. param.sched_priority = MAX_RT_PRIO - 1;
  405. } else /* common case, do nothing */
  406. return;
  407. } else {
  408. /*
  409. * The task will remain boosted for another ~500k operations,
  410. * then restored back to its original prio, and so forth.
  411. *
  412. * When @trsp is nil, we want to force-reset the task for
  413. * stopping the kthread.
  414. */
  415. if (!trsp || !(torture_random(trsp) %
  416. (cxt.nrealwriters_stress * factor * 2))) {
  417. policy = SCHED_NORMAL;
  418. param.sched_priority = 0;
  419. } else /* common case, do nothing */
  420. return;
  421. }
  422. sched_setscheduler_nocheck(current, policy, &param);
  423. }
  424. static void torture_rtmutex_delay(struct torture_random_state *trsp)
  425. {
  426. const unsigned long shortdelay_us = 2;
  427. const unsigned long longdelay_ms = 100;
  428. /*
  429. * We want a short delay mostly to emulate likely code, and
  430. * we want a long delay occasionally to force massive contention.
  431. */
  432. if (!(torture_random(trsp) %
  433. (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
  434. mdelay(longdelay_ms);
  435. if (!(torture_random(trsp) %
  436. (cxt.nrealwriters_stress * 2 * shortdelay_us)))
  437. udelay(shortdelay_us);
  438. #ifdef CONFIG_PREEMPT
  439. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  440. preempt_schedule(); /* Allow test to be preempted. */
  441. #endif
  442. }
  443. static void torture_rtmutex_unlock(void) __releases(torture_rtmutex)
  444. {
  445. rt_mutex_unlock(&torture_rtmutex);
  446. }
  447. static struct lock_torture_ops rtmutex_lock_ops = {
  448. .writelock = torture_rtmutex_lock,
  449. .write_delay = torture_rtmutex_delay,
  450. .task_boost = torture_rtmutex_boost,
  451. .writeunlock = torture_rtmutex_unlock,
  452. .readlock = NULL,
  453. .read_delay = NULL,
  454. .readunlock = NULL,
  455. .name = "rtmutex_lock"
  456. };
  457. #endif
  458. static DECLARE_RWSEM(torture_rwsem);
  459. static int torture_rwsem_down_write(void) __acquires(torture_rwsem)
  460. {
  461. down_write(&torture_rwsem);
  462. return 0;
  463. }
  464. static void torture_rwsem_write_delay(struct torture_random_state *trsp)
  465. {
  466. const unsigned long longdelay_ms = 100;
  467. /* We want a long delay occasionally to force massive contention. */
  468. if (!(torture_random(trsp) %
  469. (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
  470. mdelay(longdelay_ms * 10);
  471. else
  472. mdelay(longdelay_ms / 10);
  473. #ifdef CONFIG_PREEMPT
  474. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  475. preempt_schedule(); /* Allow test to be preempted. */
  476. #endif
  477. }
  478. static void torture_rwsem_up_write(void) __releases(torture_rwsem)
  479. {
  480. up_write(&torture_rwsem);
  481. }
  482. static int torture_rwsem_down_read(void) __acquires(torture_rwsem)
  483. {
  484. down_read(&torture_rwsem);
  485. return 0;
  486. }
  487. static void torture_rwsem_read_delay(struct torture_random_state *trsp)
  488. {
  489. const unsigned long longdelay_ms = 100;
  490. /* We want a long delay occasionally to force massive contention. */
  491. if (!(torture_random(trsp) %
  492. (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
  493. mdelay(longdelay_ms * 2);
  494. else
  495. mdelay(longdelay_ms / 2);
  496. #ifdef CONFIG_PREEMPT
  497. if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000)))
  498. preempt_schedule(); /* Allow test to be preempted. */
  499. #endif
  500. }
  501. static void torture_rwsem_up_read(void) __releases(torture_rwsem)
  502. {
  503. up_read(&torture_rwsem);
  504. }
  505. static struct lock_torture_ops rwsem_lock_ops = {
  506. .writelock = torture_rwsem_down_write,
  507. .write_delay = torture_rwsem_write_delay,
  508. .task_boost = torture_boost_dummy,
  509. .writeunlock = torture_rwsem_up_write,
  510. .readlock = torture_rwsem_down_read,
  511. .read_delay = torture_rwsem_read_delay,
  512. .readunlock = torture_rwsem_up_read,
  513. .name = "rwsem_lock"
  514. };
  515. #include <linux/percpu-rwsem.h>
  516. static struct percpu_rw_semaphore pcpu_rwsem;
  517. void torture_percpu_rwsem_init(void)
  518. {
  519. BUG_ON(percpu_init_rwsem(&pcpu_rwsem));
  520. }
  521. static int torture_percpu_rwsem_down_write(void) __acquires(pcpu_rwsem)
  522. {
  523. percpu_down_write(&pcpu_rwsem);
  524. return 0;
  525. }
  526. static void torture_percpu_rwsem_up_write(void) __releases(pcpu_rwsem)
  527. {
  528. percpu_up_write(&pcpu_rwsem);
  529. }
  530. static int torture_percpu_rwsem_down_read(void) __acquires(pcpu_rwsem)
  531. {
  532. percpu_down_read(&pcpu_rwsem);
  533. return 0;
  534. }
  535. static void torture_percpu_rwsem_up_read(void) __releases(pcpu_rwsem)
  536. {
  537. percpu_up_read(&pcpu_rwsem);
  538. }
  539. static struct lock_torture_ops percpu_rwsem_lock_ops = {
  540. .init = torture_percpu_rwsem_init,
  541. .writelock = torture_percpu_rwsem_down_write,
  542. .write_delay = torture_rwsem_write_delay,
  543. .task_boost = torture_boost_dummy,
  544. .writeunlock = torture_percpu_rwsem_up_write,
  545. .readlock = torture_percpu_rwsem_down_read,
  546. .read_delay = torture_rwsem_read_delay,
  547. .readunlock = torture_percpu_rwsem_up_read,
  548. .name = "percpu_rwsem_lock"
  549. };
  550. /*
  551. * Lock torture writer kthread. Repeatedly acquires and releases
  552. * the lock, checking for duplicate acquisitions.
  553. */
  554. static int lock_torture_writer(void *arg)
  555. {
  556. struct lock_stress_stats *lwsp = arg;
  557. static DEFINE_TORTURE_RANDOM(rand);
  558. VERBOSE_TOROUT_STRING("lock_torture_writer task started");
  559. set_user_nice(current, MAX_NICE);
  560. do {
  561. if ((torture_random(&rand) & 0xfffff) == 0)
  562. schedule_timeout_uninterruptible(1);
  563. cxt.cur_ops->task_boost(&rand);
  564. cxt.cur_ops->writelock();
  565. if (WARN_ON_ONCE(lock_is_write_held))
  566. lwsp->n_lock_fail++;
  567. lock_is_write_held = 1;
  568. if (WARN_ON_ONCE(lock_is_read_held))
  569. lwsp->n_lock_fail++; /* rare, but... */
  570. lwsp->n_lock_acquired++;
  571. cxt.cur_ops->write_delay(&rand);
  572. lock_is_write_held = 0;
  573. cxt.cur_ops->writeunlock();
  574. stutter_wait("lock_torture_writer");
  575. } while (!torture_must_stop());
  576. cxt.cur_ops->task_boost(NULL); /* reset prio */
  577. torture_kthread_stopping("lock_torture_writer");
  578. return 0;
  579. }
  580. /*
  581. * Lock torture reader kthread. Repeatedly acquires and releases
  582. * the reader lock.
  583. */
  584. static int lock_torture_reader(void *arg)
  585. {
  586. struct lock_stress_stats *lrsp = arg;
  587. static DEFINE_TORTURE_RANDOM(rand);
  588. VERBOSE_TOROUT_STRING("lock_torture_reader task started");
  589. set_user_nice(current, MAX_NICE);
  590. do {
  591. if ((torture_random(&rand) & 0xfffff) == 0)
  592. schedule_timeout_uninterruptible(1);
  593. cxt.cur_ops->readlock();
  594. lock_is_read_held = 1;
  595. if (WARN_ON_ONCE(lock_is_write_held))
  596. lrsp->n_lock_fail++; /* rare, but... */
  597. lrsp->n_lock_acquired++;
  598. cxt.cur_ops->read_delay(&rand);
  599. lock_is_read_held = 0;
  600. cxt.cur_ops->readunlock();
  601. stutter_wait("lock_torture_reader");
  602. } while (!torture_must_stop());
  603. torture_kthread_stopping("lock_torture_reader");
  604. return 0;
  605. }
  606. /*
  607. * Create an lock-torture-statistics message in the specified buffer.
  608. */
  609. static void __torture_print_stats(char *page,
  610. struct lock_stress_stats *statp, bool write)
  611. {
  612. bool fail = 0;
  613. int i, n_stress;
  614. long max = 0;
  615. long min = statp[0].n_lock_acquired;
  616. long long sum = 0;
  617. n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
  618. for (i = 0; i < n_stress; i++) {
  619. if (statp[i].n_lock_fail)
  620. fail = true;
  621. sum += statp[i].n_lock_acquired;
  622. if (max < statp[i].n_lock_fail)
  623. max = statp[i].n_lock_fail;
  624. if (min > statp[i].n_lock_fail)
  625. min = statp[i].n_lock_fail;
  626. }
  627. page += sprintf(page,
  628. "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
  629. write ? "Writes" : "Reads ",
  630. sum, max, min, max / 2 > min ? "???" : "",
  631. fail, fail ? "!!!" : "");
  632. if (fail)
  633. atomic_inc(&cxt.n_lock_torture_errors);
  634. }
  635. /*
  636. * Print torture statistics. Caller must ensure that there is only one
  637. * call to this function at a given time!!! This is normally accomplished
  638. * by relying on the module system to only have one copy of the module
  639. * loaded, and then by giving the lock_torture_stats kthread full control
  640. * (or the init/cleanup functions when lock_torture_stats thread is not
  641. * running).
  642. */
  643. static void lock_torture_stats_print(void)
  644. {
  645. int size = cxt.nrealwriters_stress * 200 + 8192;
  646. char *buf;
  647. if (cxt.cur_ops->readlock)
  648. size += cxt.nrealreaders_stress * 200 + 8192;
  649. buf = kmalloc(size, GFP_KERNEL);
  650. if (!buf) {
  651. pr_err("lock_torture_stats_print: Out of memory, need: %d",
  652. size);
  653. return;
  654. }
  655. __torture_print_stats(buf, cxt.lwsa, true);
  656. pr_alert("%s", buf);
  657. kfree(buf);
  658. if (cxt.cur_ops->readlock) {
  659. buf = kmalloc(size, GFP_KERNEL);
  660. if (!buf) {
  661. pr_err("lock_torture_stats_print: Out of memory, need: %d",
  662. size);
  663. return;
  664. }
  665. __torture_print_stats(buf, cxt.lrsa, false);
  666. pr_alert("%s", buf);
  667. kfree(buf);
  668. }
  669. }
  670. /*
  671. * Periodically prints torture statistics, if periodic statistics printing
  672. * was specified via the stat_interval module parameter.
  673. *
  674. * No need to worry about fullstop here, since this one doesn't reference
  675. * volatile state or register callbacks.
  676. */
  677. static int lock_torture_stats(void *arg)
  678. {
  679. VERBOSE_TOROUT_STRING("lock_torture_stats task started");
  680. do {
  681. schedule_timeout_interruptible(stat_interval * HZ);
  682. lock_torture_stats_print();
  683. torture_shutdown_absorb("lock_torture_stats");
  684. } while (!torture_must_stop());
  685. torture_kthread_stopping("lock_torture_stats");
  686. return 0;
  687. }
  688. static inline void
  689. lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
  690. const char *tag)
  691. {
  692. pr_alert("%s" TORTURE_FLAG
  693. "--- %s%s: nwriters_stress=%d nreaders_stress=%d stat_interval=%d verbose=%d shuffle_interval=%d stutter=%d shutdown_secs=%d onoff_interval=%d onoff_holdoff=%d\n",
  694. torture_type, tag, cxt.debug_lock ? " [debug]": "",
  695. cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval,
  696. verbose, shuffle_interval, stutter, shutdown_secs,
  697. onoff_interval, onoff_holdoff);
  698. }
  699. static void lock_torture_cleanup(void)
  700. {
  701. int i;
  702. if (torture_cleanup_begin())
  703. return;
  704. /*
  705. * Indicates early cleanup, meaning that the test has not run,
  706. * such as when passing bogus args when loading the module. As
  707. * such, only perform the underlying torture-specific cleanups,
  708. * and avoid anything related to locktorture.
  709. */
  710. if (!cxt.lwsa)
  711. goto end;
  712. if (writer_tasks) {
  713. for (i = 0; i < cxt.nrealwriters_stress; i++)
  714. torture_stop_kthread(lock_torture_writer,
  715. writer_tasks[i]);
  716. kfree(writer_tasks);
  717. writer_tasks = NULL;
  718. }
  719. if (reader_tasks) {
  720. for (i = 0; i < cxt.nrealreaders_stress; i++)
  721. torture_stop_kthread(lock_torture_reader,
  722. reader_tasks[i]);
  723. kfree(reader_tasks);
  724. reader_tasks = NULL;
  725. }
  726. torture_stop_kthread(lock_torture_stats, stats_task);
  727. lock_torture_stats_print(); /* -After- the stats thread is stopped! */
  728. if (atomic_read(&cxt.n_lock_torture_errors))
  729. lock_torture_print_module_parms(cxt.cur_ops,
  730. "End of test: FAILURE");
  731. else if (torture_onoff_failures())
  732. lock_torture_print_module_parms(cxt.cur_ops,
  733. "End of test: LOCK_HOTPLUG");
  734. else
  735. lock_torture_print_module_parms(cxt.cur_ops,
  736. "End of test: SUCCESS");
  737. kfree(cxt.lwsa);
  738. kfree(cxt.lrsa);
  739. end:
  740. torture_cleanup_end();
  741. }
  742. static int __init lock_torture_init(void)
  743. {
  744. int i, j;
  745. int firsterr = 0;
  746. static struct lock_torture_ops *torture_ops[] = {
  747. &lock_busted_ops,
  748. &spin_lock_ops, &spin_lock_irq_ops,
  749. &rw_lock_ops, &rw_lock_irq_ops,
  750. &mutex_lock_ops,
  751. &ww_mutex_lock_ops,
  752. #ifdef CONFIG_RT_MUTEXES
  753. &rtmutex_lock_ops,
  754. #endif
  755. &rwsem_lock_ops,
  756. &percpu_rwsem_lock_ops,
  757. };
  758. if (!torture_init_begin(torture_type, verbose, &torture_runnable))
  759. return -EBUSY;
  760. /* Process args and tell the world that the torturer is on the job. */
  761. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  762. cxt.cur_ops = torture_ops[i];
  763. if (strcmp(torture_type, cxt.cur_ops->name) == 0)
  764. break;
  765. }
  766. if (i == ARRAY_SIZE(torture_ops)) {
  767. pr_alert("lock-torture: invalid torture type: \"%s\"\n",
  768. torture_type);
  769. pr_alert("lock-torture types:");
  770. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  771. pr_alert(" %s", torture_ops[i]->name);
  772. pr_alert("\n");
  773. firsterr = -EINVAL;
  774. goto unwind;
  775. }
  776. if (cxt.cur_ops->init)
  777. cxt.cur_ops->init();
  778. if (nwriters_stress >= 0)
  779. cxt.nrealwriters_stress = nwriters_stress;
  780. else
  781. cxt.nrealwriters_stress = 2 * num_online_cpus();
  782. #ifdef CONFIG_DEBUG_MUTEXES
  783. if (strncmp(torture_type, "mutex", 5) == 0)
  784. cxt.debug_lock = true;
  785. #endif
  786. #ifdef CONFIG_DEBUG_RT_MUTEXES
  787. if (strncmp(torture_type, "rtmutex", 7) == 0)
  788. cxt.debug_lock = true;
  789. #endif
  790. #ifdef CONFIG_DEBUG_SPINLOCK
  791. if ((strncmp(torture_type, "spin", 4) == 0) ||
  792. (strncmp(torture_type, "rw_lock", 7) == 0))
  793. cxt.debug_lock = true;
  794. #endif
  795. /* Initialize the statistics so that each run gets its own numbers. */
  796. lock_is_write_held = 0;
  797. cxt.lwsa = kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KERNEL);
  798. if (cxt.lwsa == NULL) {
  799. VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
  800. firsterr = -ENOMEM;
  801. goto unwind;
  802. }
  803. for (i = 0; i < cxt.nrealwriters_stress; i++) {
  804. cxt.lwsa[i].n_lock_fail = 0;
  805. cxt.lwsa[i].n_lock_acquired = 0;
  806. }
  807. if (cxt.cur_ops->readlock) {
  808. if (nreaders_stress >= 0)
  809. cxt.nrealreaders_stress = nreaders_stress;
  810. else {
  811. /*
  812. * By default distribute evenly the number of
  813. * readers and writers. We still run the same number
  814. * of threads as the writer-only locks default.
  815. */
  816. if (nwriters_stress < 0) /* user doesn't care */
  817. cxt.nrealwriters_stress = num_online_cpus();
  818. cxt.nrealreaders_stress = cxt.nrealwriters_stress;
  819. }
  820. lock_is_read_held = 0;
  821. cxt.lrsa = kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_KERNEL);
  822. if (cxt.lrsa == NULL) {
  823. VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
  824. firsterr = -ENOMEM;
  825. kfree(cxt.lwsa);
  826. cxt.lwsa = NULL;
  827. goto unwind;
  828. }
  829. for (i = 0; i < cxt.nrealreaders_stress; i++) {
  830. cxt.lrsa[i].n_lock_fail = 0;
  831. cxt.lrsa[i].n_lock_acquired = 0;
  832. }
  833. }
  834. lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
  835. /* Prepare torture context. */
  836. if (onoff_interval > 0) {
  837. firsterr = torture_onoff_init(onoff_holdoff * HZ,
  838. onoff_interval * HZ);
  839. if (firsterr)
  840. goto unwind;
  841. }
  842. if (shuffle_interval > 0) {
  843. firsterr = torture_shuffle_init(shuffle_interval);
  844. if (firsterr)
  845. goto unwind;
  846. }
  847. if (shutdown_secs > 0) {
  848. firsterr = torture_shutdown_init(shutdown_secs,
  849. lock_torture_cleanup);
  850. if (firsterr)
  851. goto unwind;
  852. }
  853. if (stutter > 0) {
  854. firsterr = torture_stutter_init(stutter);
  855. if (firsterr)
  856. goto unwind;
  857. }
  858. writer_tasks = kzalloc(cxt.nrealwriters_stress * sizeof(writer_tasks[0]),
  859. GFP_KERNEL);
  860. if (writer_tasks == NULL) {
  861. VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
  862. firsterr = -ENOMEM;
  863. goto unwind;
  864. }
  865. if (cxt.cur_ops->readlock) {
  866. reader_tasks = kzalloc(cxt.nrealreaders_stress * sizeof(reader_tasks[0]),
  867. GFP_KERNEL);
  868. if (reader_tasks == NULL) {
  869. VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
  870. kfree(writer_tasks);
  871. writer_tasks = NULL;
  872. firsterr = -ENOMEM;
  873. goto unwind;
  874. }
  875. }
  876. /*
  877. * Create the kthreads and start torturing (oh, those poor little locks).
  878. *
  879. * TODO: Note that we interleave writers with readers, giving writers a
  880. * slight advantage, by creating its kthread first. This can be modified
  881. * for very specific needs, or even let the user choose the policy, if
  882. * ever wanted.
  883. */
  884. for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
  885. j < cxt.nrealreaders_stress; i++, j++) {
  886. if (i >= cxt.nrealwriters_stress)
  887. goto create_reader;
  888. /* Create writer. */
  889. firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i],
  890. writer_tasks[i]);
  891. if (firsterr)
  892. goto unwind;
  893. create_reader:
  894. if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
  895. continue;
  896. /* Create reader. */
  897. firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
  898. reader_tasks[j]);
  899. if (firsterr)
  900. goto unwind;
  901. }
  902. if (stat_interval > 0) {
  903. firsterr = torture_create_kthread(lock_torture_stats, NULL,
  904. stats_task);
  905. if (firsterr)
  906. goto unwind;
  907. }
  908. torture_init_end();
  909. return 0;
  910. unwind:
  911. torture_init_end();
  912. lock_torture_cleanup();
  913. return firsterr;
  914. }
  915. module_init(lock_torture_init);
  916. module_exit(lock_torture_cleanup);