locktorture.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. * Author: Paul E. McKenney <paulmck@us.ibm.com>
  21. * Based on kernel/rcu/torture.c.
  22. */
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/kthread.h>
  28. #include <linux/err.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/smp.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/sched.h>
  33. #include <linux/atomic.h>
  34. #include <linux/bitops.h>
  35. #include <linux/completion.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/percpu.h>
  38. #include <linux/notifier.h>
  39. #include <linux/reboot.h>
  40. #include <linux/freezer.h>
  41. #include <linux/cpu.h>
  42. #include <linux/delay.h>
  43. #include <linux/stat.h>
  44. #include <linux/slab.h>
  45. #include <linux/trace_clock.h>
  46. #include <asm/byteorder.h>
  47. #include <linux/torture.h>
  48. MODULE_LICENSE("GPL");
  49. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
  50. torture_param(int, nwriters_stress, -1,
  51. "Number of write-locking stress-test threads");
  52. torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
  53. torture_param(int, onoff_interval, 0,
  54. "Time between CPU hotplugs (s), 0=disable");
  55. torture_param(int, shuffle_interval, 3,
  56. "Number of jiffies between shuffles, 0=disable");
  57. torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
  58. torture_param(int, stat_interval, 60,
  59. "Number of seconds between stats printk()s");
  60. torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
  61. torture_param(bool, verbose, true,
  62. "Enable verbose debugging printk()s");
  63. static char *torture_type = "spin_lock";
  64. module_param(torture_type, charp, 0444);
  65. MODULE_PARM_DESC(torture_type,
  66. "Type of lock to torture (spin_lock, spin_lock_irq, ...)");
  67. static atomic_t n_lock_torture_errors;
  68. static struct task_struct *stats_task;
  69. static struct task_struct **writer_tasks;
  70. static int nrealwriters_stress;
  71. static bool lock_is_write_held;
  72. struct lock_writer_stress_stats {
  73. long n_write_lock_fail;
  74. long n_write_lock_acquired;
  75. };
  76. static struct lock_writer_stress_stats *lwsa;
  77. #if defined(MODULE) || defined(CONFIG_LOCK_TORTURE_TEST_RUNNABLE)
  78. #define LOCKTORTURE_RUNNABLE_INIT 1
  79. #else
  80. #define LOCKTORTURE_RUNNABLE_INIT 0
  81. #endif
  82. int locktorture_runnable = LOCKTORTURE_RUNNABLE_INIT;
  83. module_param(locktorture_runnable, int, 0444);
  84. MODULE_PARM_DESC(locktorture_runnable, "Start locktorture at boot");
  85. /* Forward reference. */
  86. static void lock_torture_cleanup(void);
  87. /*
  88. * Operations vector for selecting different types of tests.
  89. */
  90. struct lock_torture_ops {
  91. void (*init)(void);
  92. int (*writelock)(void);
  93. void (*write_delay)(struct torture_random_state *trsp);
  94. void (*writeunlock)(void);
  95. unsigned long flags;
  96. const char *name;
  97. };
  98. static struct lock_torture_ops *cur_ops;
  99. /*
  100. * Definitions for lock torture testing.
  101. */
  102. static int torture_lock_busted_write_lock(void)
  103. {
  104. return 0; /* BUGGY, do not use in real life!!! */
  105. }
  106. static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
  107. {
  108. const unsigned long longdelay_us = 100;
  109. /* We want a long delay occasionally to force massive contention. */
  110. if (!(torture_random(trsp) %
  111. (nrealwriters_stress * 2000 * longdelay_us)))
  112. mdelay(longdelay_us);
  113. #ifdef CONFIG_PREEMPT
  114. if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
  115. preempt_schedule(); /* Allow test to be preempted. */
  116. #endif
  117. }
  118. static void torture_lock_busted_write_unlock(void)
  119. {
  120. /* BUGGY, do not use in real life!!! */
  121. }
  122. static struct lock_torture_ops lock_busted_ops = {
  123. .writelock = torture_lock_busted_write_lock,
  124. .write_delay = torture_lock_busted_write_delay,
  125. .writeunlock = torture_lock_busted_write_unlock,
  126. .name = "lock_busted"
  127. };
  128. static DEFINE_SPINLOCK(torture_spinlock);
  129. static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
  130. {
  131. spin_lock(&torture_spinlock);
  132. return 0;
  133. }
  134. static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
  135. {
  136. const unsigned long shortdelay_us = 2;
  137. const unsigned long longdelay_us = 100;
  138. /* We want a short delay mostly to emulate likely code, and
  139. * we want a long delay occasionally to force massive contention.
  140. */
  141. if (!(torture_random(trsp) %
  142. (nrealwriters_stress * 2000 * longdelay_us)))
  143. mdelay(longdelay_us);
  144. if (!(torture_random(trsp) %
  145. (nrealwriters_stress * 2 * shortdelay_us)))
  146. udelay(shortdelay_us);
  147. #ifdef CONFIG_PREEMPT
  148. if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
  149. preempt_schedule(); /* Allow test to be preempted. */
  150. #endif
  151. }
  152. static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
  153. {
  154. spin_unlock(&torture_spinlock);
  155. }
  156. static struct lock_torture_ops spin_lock_ops = {
  157. .writelock = torture_spin_lock_write_lock,
  158. .write_delay = torture_spin_lock_write_delay,
  159. .writeunlock = torture_spin_lock_write_unlock,
  160. .name = "spin_lock"
  161. };
  162. static int torture_spin_lock_write_lock_irq(void)
  163. __acquires(torture_spinlock_irq)
  164. {
  165. unsigned long flags;
  166. spin_lock_irqsave(&torture_spinlock, flags);
  167. cur_ops->flags = flags;
  168. return 0;
  169. }
  170. static void torture_lock_spin_write_unlock_irq(void)
  171. __releases(torture_spinlock)
  172. {
  173. spin_unlock_irqrestore(&torture_spinlock, cur_ops->flags);
  174. }
  175. static struct lock_torture_ops spin_lock_irq_ops = {
  176. .writelock = torture_spin_lock_write_lock_irq,
  177. .write_delay = torture_spin_lock_write_delay,
  178. .writeunlock = torture_lock_spin_write_unlock_irq,
  179. .name = "spin_lock_irq"
  180. };
  181. /*
  182. * Lock torture writer kthread. Repeatedly acquires and releases
  183. * the lock, checking for duplicate acquisitions.
  184. */
  185. static int lock_torture_writer(void *arg)
  186. {
  187. struct lock_writer_stress_stats *lwsp = arg;
  188. static DEFINE_TORTURE_RANDOM(rand);
  189. VERBOSE_TOROUT_STRING("lock_torture_writer task started");
  190. set_user_nice(current, 19);
  191. do {
  192. schedule_timeout_uninterruptible(1);
  193. cur_ops->writelock();
  194. if (WARN_ON_ONCE(lock_is_write_held))
  195. lwsp->n_write_lock_fail++;
  196. lock_is_write_held = 1;
  197. lwsp->n_write_lock_acquired++;
  198. cur_ops->write_delay(&rand);
  199. lock_is_write_held = 0;
  200. cur_ops->writeunlock();
  201. stutter_wait("lock_torture_writer");
  202. } while (!torture_must_stop());
  203. torture_kthread_stopping("lock_torture_writer");
  204. return 0;
  205. }
  206. /*
  207. * Create an lock-torture-statistics message in the specified buffer.
  208. */
  209. static void lock_torture_printk(char *page)
  210. {
  211. bool fail = 0;
  212. int i;
  213. long max = 0;
  214. long min = lwsa[0].n_write_lock_acquired;
  215. long long sum = 0;
  216. for (i = 0; i < nrealwriters_stress; i++) {
  217. if (lwsa[i].n_write_lock_fail)
  218. fail = true;
  219. sum += lwsa[i].n_write_lock_acquired;
  220. if (max < lwsa[i].n_write_lock_fail)
  221. max = lwsa[i].n_write_lock_fail;
  222. if (min > lwsa[i].n_write_lock_fail)
  223. min = lwsa[i].n_write_lock_fail;
  224. }
  225. page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
  226. page += sprintf(page,
  227. "Writes: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
  228. sum, max, min, max / 2 > min ? "???" : "",
  229. fail, fail ? "!!!" : "");
  230. if (fail)
  231. atomic_inc(&n_lock_torture_errors);
  232. }
  233. /*
  234. * Print torture statistics. Caller must ensure that there is only one
  235. * call to this function at a given time!!! This is normally accomplished
  236. * by relying on the module system to only have one copy of the module
  237. * loaded, and then by giving the lock_torture_stats kthread full control
  238. * (or the init/cleanup functions when lock_torture_stats thread is not
  239. * running).
  240. */
  241. static void lock_torture_stats_print(void)
  242. {
  243. int size = nrealwriters_stress * 200 + 8192;
  244. char *buf;
  245. buf = kmalloc(size, GFP_KERNEL);
  246. if (!buf) {
  247. pr_err("lock_torture_stats_print: Out of memory, need: %d",
  248. size);
  249. return;
  250. }
  251. lock_torture_printk(buf);
  252. pr_alert("%s", buf);
  253. kfree(buf);
  254. }
  255. /*
  256. * Periodically prints torture statistics, if periodic statistics printing
  257. * was specified via the stat_interval module parameter.
  258. *
  259. * No need to worry about fullstop here, since this one doesn't reference
  260. * volatile state or register callbacks.
  261. */
  262. static int lock_torture_stats(void *arg)
  263. {
  264. VERBOSE_TOROUT_STRING("lock_torture_stats task started");
  265. do {
  266. schedule_timeout_interruptible(stat_interval * HZ);
  267. lock_torture_stats_print();
  268. torture_shutdown_absorb("lock_torture_stats");
  269. } while (!torture_must_stop());
  270. torture_kthread_stopping("lock_torture_stats");
  271. return 0;
  272. }
  273. static inline void
  274. lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
  275. const char *tag)
  276. {
  277. pr_alert("%s" TORTURE_FLAG
  278. "--- %s: nwriters_stress=%d stat_interval=%d verbose=%d shuffle_interval=%d stutter=%d shutdown_secs=%d onoff_interval=%d onoff_holdoff=%d\n",
  279. torture_type, tag, nrealwriters_stress, stat_interval, verbose,
  280. shuffle_interval, stutter, shutdown_secs,
  281. onoff_interval, onoff_holdoff);
  282. }
  283. static void lock_torture_cleanup(void)
  284. {
  285. int i;
  286. if (torture_cleanup())
  287. return;
  288. if (writer_tasks) {
  289. for (i = 0; i < nrealwriters_stress; i++)
  290. torture_stop_kthread(lock_torture_writer,
  291. writer_tasks[i]);
  292. kfree(writer_tasks);
  293. writer_tasks = NULL;
  294. }
  295. torture_stop_kthread(lock_torture_stats, stats_task);
  296. lock_torture_stats_print(); /* -After- the stats thread is stopped! */
  297. if (atomic_read(&n_lock_torture_errors))
  298. lock_torture_print_module_parms(cur_ops,
  299. "End of test: FAILURE");
  300. else if (torture_onoff_failures())
  301. lock_torture_print_module_parms(cur_ops,
  302. "End of test: LOCK_HOTPLUG");
  303. else
  304. lock_torture_print_module_parms(cur_ops,
  305. "End of test: SUCCESS");
  306. }
  307. static int __init lock_torture_init(void)
  308. {
  309. int i;
  310. int firsterr = 0;
  311. static struct lock_torture_ops *torture_ops[] = {
  312. &lock_busted_ops, &spin_lock_ops, &spin_lock_irq_ops,
  313. };
  314. torture_init_begin(torture_type, verbose, &locktorture_runnable);
  315. /* Process args and tell the world that the torturer is on the job. */
  316. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  317. cur_ops = torture_ops[i];
  318. if (strcmp(torture_type, cur_ops->name) == 0)
  319. break;
  320. }
  321. if (i == ARRAY_SIZE(torture_ops)) {
  322. pr_alert("lock-torture: invalid torture type: \"%s\"\n",
  323. torture_type);
  324. pr_alert("lock-torture types:");
  325. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  326. pr_alert(" %s", torture_ops[i]->name);
  327. pr_alert("\n");
  328. torture_init_end();
  329. return -EINVAL;
  330. }
  331. if (cur_ops->init)
  332. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  333. if (nwriters_stress >= 0)
  334. nrealwriters_stress = nwriters_stress;
  335. else
  336. nrealwriters_stress = 2 * num_online_cpus();
  337. lock_torture_print_module_parms(cur_ops, "Start of test");
  338. /* Initialize the statistics so that each run gets its own numbers. */
  339. lock_is_write_held = 0;
  340. lwsa = kmalloc(sizeof(*lwsa) * nrealwriters_stress, GFP_KERNEL);
  341. if (lwsa == NULL) {
  342. VERBOSE_TOROUT_STRING("lwsa: Out of memory");
  343. firsterr = -ENOMEM;
  344. goto unwind;
  345. }
  346. for (i = 0; i < nrealwriters_stress; i++) {
  347. lwsa[i].n_write_lock_fail = 0;
  348. lwsa[i].n_write_lock_acquired = 0;
  349. }
  350. /* Start up the kthreads. */
  351. if (onoff_interval > 0) {
  352. firsterr = torture_onoff_init(onoff_holdoff * HZ,
  353. onoff_interval * HZ);
  354. if (firsterr)
  355. goto unwind;
  356. }
  357. if (shuffle_interval > 0) {
  358. firsterr = torture_shuffle_init(shuffle_interval);
  359. if (firsterr)
  360. goto unwind;
  361. }
  362. if (shutdown_secs > 0) {
  363. firsterr = torture_shutdown_init(shutdown_secs,
  364. lock_torture_cleanup);
  365. if (firsterr)
  366. goto unwind;
  367. }
  368. if (stutter > 0) {
  369. firsterr = torture_stutter_init(stutter);
  370. if (firsterr)
  371. goto unwind;
  372. }
  373. writer_tasks = kzalloc(nrealwriters_stress * sizeof(writer_tasks[0]),
  374. GFP_KERNEL);
  375. if (writer_tasks == NULL) {
  376. VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
  377. firsterr = -ENOMEM;
  378. goto unwind;
  379. }
  380. for (i = 0; i < nrealwriters_stress; i++) {
  381. firsterr = torture_create_kthread(lock_torture_writer, &lwsa[i],
  382. writer_tasks[i]);
  383. if (firsterr)
  384. goto unwind;
  385. }
  386. if (stat_interval > 0) {
  387. firsterr = torture_create_kthread(lock_torture_stats, NULL,
  388. stats_task);
  389. if (firsterr)
  390. goto unwind;
  391. }
  392. torture_init_end();
  393. return 0;
  394. unwind:
  395. torture_init_end();
  396. lock_torture_cleanup();
  397. return firsterr;
  398. }
  399. module_init(lock_torture_init);
  400. module_exit(lock_torture_cleanup);