locktorture.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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, min = statp ? statp[0].n_lock_acquired : 0;
  556. long long sum = 0;
  557. n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
  558. for (i = 0; i < n_stress; i++) {
  559. if (statp[i].n_lock_fail)
  560. fail = true;
  561. sum += statp[i].n_lock_acquired;
  562. if (max < statp[i].n_lock_fail)
  563. max = statp[i].n_lock_fail;
  564. if (min > statp[i].n_lock_fail)
  565. min = statp[i].n_lock_fail;
  566. }
  567. page += sprintf(page,
  568. "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
  569. write ? "Writes" : "Reads ",
  570. sum, max, min, max / 2 > min ? "???" : "",
  571. fail, fail ? "!!!" : "");
  572. if (fail)
  573. atomic_inc(&cxt.n_lock_torture_errors);
  574. }
  575. /*
  576. * Print torture statistics. Caller must ensure that there is only one
  577. * call to this function at a given time!!! This is normally accomplished
  578. * by relying on the module system to only have one copy of the module
  579. * loaded, and then by giving the lock_torture_stats kthread full control
  580. * (or the init/cleanup functions when lock_torture_stats thread is not
  581. * running).
  582. */
  583. static void lock_torture_stats_print(void)
  584. {
  585. int size = cxt.nrealwriters_stress * 200 + 8192;
  586. char *buf;
  587. if (cxt.cur_ops->readlock)
  588. size += cxt.nrealreaders_stress * 200 + 8192;
  589. buf = kmalloc(size, GFP_KERNEL);
  590. if (!buf) {
  591. pr_err("lock_torture_stats_print: Out of memory, need: %d",
  592. size);
  593. return;
  594. }
  595. __torture_print_stats(buf, cxt.lwsa, true);
  596. pr_alert("%s", buf);
  597. kfree(buf);
  598. if (cxt.cur_ops->readlock) {
  599. buf = kmalloc(size, GFP_KERNEL);
  600. if (!buf) {
  601. pr_err("lock_torture_stats_print: Out of memory, need: %d",
  602. size);
  603. return;
  604. }
  605. __torture_print_stats(buf, cxt.lrsa, false);
  606. pr_alert("%s", buf);
  607. kfree(buf);
  608. }
  609. }
  610. /*
  611. * Periodically prints torture statistics, if periodic statistics printing
  612. * was specified via the stat_interval module parameter.
  613. *
  614. * No need to worry about fullstop here, since this one doesn't reference
  615. * volatile state or register callbacks.
  616. */
  617. static int lock_torture_stats(void *arg)
  618. {
  619. VERBOSE_TOROUT_STRING("lock_torture_stats task started");
  620. do {
  621. schedule_timeout_interruptible(stat_interval * HZ);
  622. lock_torture_stats_print();
  623. torture_shutdown_absorb("lock_torture_stats");
  624. } while (!torture_must_stop());
  625. torture_kthread_stopping("lock_torture_stats");
  626. return 0;
  627. }
  628. static inline void
  629. lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
  630. const char *tag)
  631. {
  632. pr_alert("%s" TORTURE_FLAG
  633. "--- %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",
  634. torture_type, tag, cxt.debug_lock ? " [debug]": "",
  635. cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval,
  636. verbose, shuffle_interval, stutter, shutdown_secs,
  637. onoff_interval, onoff_holdoff);
  638. }
  639. static void lock_torture_cleanup(void)
  640. {
  641. int i;
  642. if (torture_cleanup_begin())
  643. return;
  644. /*
  645. * Indicates early cleanup, meaning that the test has not run,
  646. * such as when passing bogus args when loading the module. As
  647. * such, only perform the underlying torture-specific cleanups,
  648. * and avoid anything related to locktorture.
  649. */
  650. if (!cxt.lwsa && !cxt.lrsa)
  651. goto end;
  652. if (writer_tasks) {
  653. for (i = 0; i < cxt.nrealwriters_stress; i++)
  654. torture_stop_kthread(lock_torture_writer,
  655. writer_tasks[i]);
  656. kfree(writer_tasks);
  657. writer_tasks = NULL;
  658. }
  659. if (reader_tasks) {
  660. for (i = 0; i < cxt.nrealreaders_stress; i++)
  661. torture_stop_kthread(lock_torture_reader,
  662. reader_tasks[i]);
  663. kfree(reader_tasks);
  664. reader_tasks = NULL;
  665. }
  666. torture_stop_kthread(lock_torture_stats, stats_task);
  667. lock_torture_stats_print(); /* -After- the stats thread is stopped! */
  668. if (atomic_read(&cxt.n_lock_torture_errors))
  669. lock_torture_print_module_parms(cxt.cur_ops,
  670. "End of test: FAILURE");
  671. else if (torture_onoff_failures())
  672. lock_torture_print_module_parms(cxt.cur_ops,
  673. "End of test: LOCK_HOTPLUG");
  674. else
  675. lock_torture_print_module_parms(cxt.cur_ops,
  676. "End of test: SUCCESS");
  677. kfree(cxt.lwsa);
  678. kfree(cxt.lrsa);
  679. end:
  680. torture_cleanup_end();
  681. }
  682. static int __init lock_torture_init(void)
  683. {
  684. int i, j;
  685. int firsterr = 0;
  686. static struct lock_torture_ops *torture_ops[] = {
  687. &lock_busted_ops,
  688. &spin_lock_ops, &spin_lock_irq_ops,
  689. &rw_lock_ops, &rw_lock_irq_ops,
  690. &mutex_lock_ops,
  691. #ifdef CONFIG_RT_MUTEXES
  692. &rtmutex_lock_ops,
  693. #endif
  694. &rwsem_lock_ops,
  695. &percpu_rwsem_lock_ops,
  696. };
  697. if (!torture_init_begin(torture_type, verbose, &torture_runnable))
  698. return -EBUSY;
  699. /* Process args and tell the world that the torturer is on the job. */
  700. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  701. cxt.cur_ops = torture_ops[i];
  702. if (strcmp(torture_type, cxt.cur_ops->name) == 0)
  703. break;
  704. }
  705. if (i == ARRAY_SIZE(torture_ops)) {
  706. pr_alert("lock-torture: invalid torture type: \"%s\"\n",
  707. torture_type);
  708. pr_alert("lock-torture types:");
  709. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  710. pr_alert(" %s", torture_ops[i]->name);
  711. pr_alert("\n");
  712. firsterr = -EINVAL;
  713. goto unwind;
  714. }
  715. if (nwriters_stress == 0 && nreaders_stress == 0) {
  716. pr_alert("lock-torture: must run at least one locking thread\n");
  717. firsterr = -EINVAL;
  718. goto unwind;
  719. }
  720. if (cxt.cur_ops->init)
  721. cxt.cur_ops->init();
  722. if (nwriters_stress >= 0)
  723. cxt.nrealwriters_stress = nwriters_stress;
  724. else
  725. cxt.nrealwriters_stress = 2 * num_online_cpus();
  726. #ifdef CONFIG_DEBUG_MUTEXES
  727. if (strncmp(torture_type, "mutex", 5) == 0)
  728. cxt.debug_lock = true;
  729. #endif
  730. #ifdef CONFIG_DEBUG_RT_MUTEXES
  731. if (strncmp(torture_type, "rtmutex", 7) == 0)
  732. cxt.debug_lock = true;
  733. #endif
  734. #ifdef CONFIG_DEBUG_SPINLOCK
  735. if ((strncmp(torture_type, "spin", 4) == 0) ||
  736. (strncmp(torture_type, "rw_lock", 7) == 0))
  737. cxt.debug_lock = true;
  738. #endif
  739. /* Initialize the statistics so that each run gets its own numbers. */
  740. if (nwriters_stress) {
  741. lock_is_write_held = 0;
  742. cxt.lwsa = kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KERNEL);
  743. if (cxt.lwsa == NULL) {
  744. VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
  745. firsterr = -ENOMEM;
  746. goto unwind;
  747. }
  748. for (i = 0; i < cxt.nrealwriters_stress; i++) {
  749. cxt.lwsa[i].n_lock_fail = 0;
  750. cxt.lwsa[i].n_lock_acquired = 0;
  751. }
  752. }
  753. if (cxt.cur_ops->readlock) {
  754. if (nreaders_stress >= 0)
  755. cxt.nrealreaders_stress = nreaders_stress;
  756. else {
  757. /*
  758. * By default distribute evenly the number of
  759. * readers and writers. We still run the same number
  760. * of threads as the writer-only locks default.
  761. */
  762. if (nwriters_stress < 0) /* user doesn't care */
  763. cxt.nrealwriters_stress = num_online_cpus();
  764. cxt.nrealreaders_stress = cxt.nrealwriters_stress;
  765. }
  766. if (nreaders_stress) {
  767. lock_is_read_held = 0;
  768. cxt.lrsa = kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_KERNEL);
  769. if (cxt.lrsa == NULL) {
  770. VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
  771. firsterr = -ENOMEM;
  772. kfree(cxt.lwsa);
  773. cxt.lwsa = NULL;
  774. goto unwind;
  775. }
  776. for (i = 0; i < cxt.nrealreaders_stress; i++) {
  777. cxt.lrsa[i].n_lock_fail = 0;
  778. cxt.lrsa[i].n_lock_acquired = 0;
  779. }
  780. }
  781. }
  782. lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
  783. /* Prepare torture context. */
  784. if (onoff_interval > 0) {
  785. firsterr = torture_onoff_init(onoff_holdoff * HZ,
  786. onoff_interval * HZ);
  787. if (firsterr)
  788. goto unwind;
  789. }
  790. if (shuffle_interval > 0) {
  791. firsterr = torture_shuffle_init(shuffle_interval);
  792. if (firsterr)
  793. goto unwind;
  794. }
  795. if (shutdown_secs > 0) {
  796. firsterr = torture_shutdown_init(shutdown_secs,
  797. lock_torture_cleanup);
  798. if (firsterr)
  799. goto unwind;
  800. }
  801. if (stutter > 0) {
  802. firsterr = torture_stutter_init(stutter);
  803. if (firsterr)
  804. goto unwind;
  805. }
  806. if (nwriters_stress) {
  807. writer_tasks = kzalloc(cxt.nrealwriters_stress * sizeof(writer_tasks[0]),
  808. GFP_KERNEL);
  809. if (writer_tasks == NULL) {
  810. VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
  811. firsterr = -ENOMEM;
  812. goto unwind;
  813. }
  814. }
  815. if (cxt.cur_ops->readlock) {
  816. reader_tasks = kzalloc(cxt.nrealreaders_stress * sizeof(reader_tasks[0]),
  817. GFP_KERNEL);
  818. if (reader_tasks == NULL) {
  819. VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
  820. kfree(writer_tasks);
  821. writer_tasks = NULL;
  822. firsterr = -ENOMEM;
  823. goto unwind;
  824. }
  825. }
  826. /*
  827. * Create the kthreads and start torturing (oh, those poor little locks).
  828. *
  829. * TODO: Note that we interleave writers with readers, giving writers a
  830. * slight advantage, by creating its kthread first. This can be modified
  831. * for very specific needs, or even let the user choose the policy, if
  832. * ever wanted.
  833. */
  834. for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
  835. j < cxt.nrealreaders_stress; i++, j++) {
  836. if (i >= cxt.nrealwriters_stress)
  837. goto create_reader;
  838. /* Create writer. */
  839. firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i],
  840. writer_tasks[i]);
  841. if (firsterr)
  842. goto unwind;
  843. create_reader:
  844. if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
  845. continue;
  846. /* Create reader. */
  847. firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
  848. reader_tasks[j]);
  849. if (firsterr)
  850. goto unwind;
  851. }
  852. if (stat_interval > 0) {
  853. firsterr = torture_create_kthread(lock_torture_stats, NULL,
  854. stats_task);
  855. if (firsterr)
  856. goto unwind;
  857. }
  858. torture_init_end();
  859. return 0;
  860. unwind:
  861. torture_init_end();
  862. lock_torture_cleanup();
  863. return firsterr;
  864. }
  865. module_init(lock_torture_init);
  866. module_exit(lock_torture_cleanup);