locktorture.c 26 KB

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