locktorture.c 26 KB

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