locktorture.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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)
  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 module init");
  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, MAX_NICE);
  191. do {
  192. if ((torture_random(&rand) & 0xfffff) == 0)
  193. schedule_timeout_uninterruptible(1);
  194. cur_ops->writelock();
  195. if (WARN_ON_ONCE(lock_is_write_held))
  196. lwsp->n_write_lock_fail++;
  197. lock_is_write_held = 1;
  198. lwsp->n_write_lock_acquired++;
  199. cur_ops->write_delay(&rand);
  200. lock_is_write_held = 0;
  201. cur_ops->writeunlock();
  202. stutter_wait("lock_torture_writer");
  203. } while (!torture_must_stop());
  204. torture_kthread_stopping("lock_torture_writer");
  205. return 0;
  206. }
  207. /*
  208. * Create an lock-torture-statistics message in the specified buffer.
  209. */
  210. static void lock_torture_printk(char *page)
  211. {
  212. bool fail = 0;
  213. int i;
  214. long max = 0;
  215. long min = lwsa[0].n_write_lock_acquired;
  216. long long sum = 0;
  217. for (i = 0; i < nrealwriters_stress; i++) {
  218. if (lwsa[i].n_write_lock_fail)
  219. fail = true;
  220. sum += lwsa[i].n_write_lock_acquired;
  221. if (max < lwsa[i].n_write_lock_fail)
  222. max = lwsa[i].n_write_lock_fail;
  223. if (min > lwsa[i].n_write_lock_fail)
  224. min = lwsa[i].n_write_lock_fail;
  225. }
  226. page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
  227. page += sprintf(page,
  228. "Writes: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
  229. sum, max, min, max / 2 > min ? "???" : "",
  230. fail, fail ? "!!!" : "");
  231. if (fail)
  232. atomic_inc(&n_lock_torture_errors);
  233. }
  234. /*
  235. * Print torture statistics. Caller must ensure that there is only one
  236. * call to this function at a given time!!! This is normally accomplished
  237. * by relying on the module system to only have one copy of the module
  238. * loaded, and then by giving the lock_torture_stats kthread full control
  239. * (or the init/cleanup functions when lock_torture_stats thread is not
  240. * running).
  241. */
  242. static void lock_torture_stats_print(void)
  243. {
  244. int size = nrealwriters_stress * 200 + 8192;
  245. char *buf;
  246. buf = kmalloc(size, GFP_KERNEL);
  247. if (!buf) {
  248. pr_err("lock_torture_stats_print: Out of memory, need: %d",
  249. size);
  250. return;
  251. }
  252. lock_torture_printk(buf);
  253. pr_alert("%s", buf);
  254. kfree(buf);
  255. }
  256. /*
  257. * Periodically prints torture statistics, if periodic statistics printing
  258. * was specified via the stat_interval module parameter.
  259. *
  260. * No need to worry about fullstop here, since this one doesn't reference
  261. * volatile state or register callbacks.
  262. */
  263. static int lock_torture_stats(void *arg)
  264. {
  265. VERBOSE_TOROUT_STRING("lock_torture_stats task started");
  266. do {
  267. schedule_timeout_interruptible(stat_interval * HZ);
  268. lock_torture_stats_print();
  269. torture_shutdown_absorb("lock_torture_stats");
  270. } while (!torture_must_stop());
  271. torture_kthread_stopping("lock_torture_stats");
  272. return 0;
  273. }
  274. static inline void
  275. lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
  276. const char *tag)
  277. {
  278. pr_alert("%s" TORTURE_FLAG
  279. "--- %s: nwriters_stress=%d stat_interval=%d verbose=%d shuffle_interval=%d stutter=%d shutdown_secs=%d onoff_interval=%d onoff_holdoff=%d\n",
  280. torture_type, tag, nrealwriters_stress, stat_interval, verbose,
  281. shuffle_interval, stutter, shutdown_secs,
  282. onoff_interval, onoff_holdoff);
  283. }
  284. static void lock_torture_cleanup(void)
  285. {
  286. int i;
  287. if (torture_cleanup())
  288. return;
  289. if (writer_tasks) {
  290. for (i = 0; i < nrealwriters_stress; i++)
  291. torture_stop_kthread(lock_torture_writer,
  292. writer_tasks[i]);
  293. kfree(writer_tasks);
  294. writer_tasks = NULL;
  295. }
  296. torture_stop_kthread(lock_torture_stats, stats_task);
  297. lock_torture_stats_print(); /* -After- the stats thread is stopped! */
  298. if (atomic_read(&n_lock_torture_errors))
  299. lock_torture_print_module_parms(cur_ops,
  300. "End of test: FAILURE");
  301. else if (torture_onoff_failures())
  302. lock_torture_print_module_parms(cur_ops,
  303. "End of test: LOCK_HOTPLUG");
  304. else
  305. lock_torture_print_module_parms(cur_ops,
  306. "End of test: SUCCESS");
  307. }
  308. static int __init lock_torture_init(void)
  309. {
  310. int i;
  311. int firsterr = 0;
  312. static struct lock_torture_ops *torture_ops[] = {
  313. &lock_busted_ops, &spin_lock_ops, &spin_lock_irq_ops,
  314. };
  315. if (!torture_init_begin(torture_type, verbose, &locktorture_runnable))
  316. return -EBUSY;
  317. /* Process args and tell the world that the torturer is on the job. */
  318. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  319. cur_ops = torture_ops[i];
  320. if (strcmp(torture_type, cur_ops->name) == 0)
  321. break;
  322. }
  323. if (i == ARRAY_SIZE(torture_ops)) {
  324. pr_alert("lock-torture: invalid torture type: \"%s\"\n",
  325. torture_type);
  326. pr_alert("lock-torture types:");
  327. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  328. pr_alert(" %s", torture_ops[i]->name);
  329. pr_alert("\n");
  330. torture_init_end();
  331. return -EINVAL;
  332. }
  333. if (cur_ops->init)
  334. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  335. if (nwriters_stress >= 0)
  336. nrealwriters_stress = nwriters_stress;
  337. else
  338. nrealwriters_stress = 2 * num_online_cpus();
  339. lock_torture_print_module_parms(cur_ops, "Start of test");
  340. /* Initialize the statistics so that each run gets its own numbers. */
  341. lock_is_write_held = 0;
  342. lwsa = kmalloc(sizeof(*lwsa) * nrealwriters_stress, GFP_KERNEL);
  343. if (lwsa == NULL) {
  344. VERBOSE_TOROUT_STRING("lwsa: Out of memory");
  345. firsterr = -ENOMEM;
  346. goto unwind;
  347. }
  348. for (i = 0; i < nrealwriters_stress; i++) {
  349. lwsa[i].n_write_lock_fail = 0;
  350. lwsa[i].n_write_lock_acquired = 0;
  351. }
  352. /* Start up the kthreads. */
  353. if (onoff_interval > 0) {
  354. firsterr = torture_onoff_init(onoff_holdoff * HZ,
  355. onoff_interval * HZ);
  356. if (firsterr)
  357. goto unwind;
  358. }
  359. if (shuffle_interval > 0) {
  360. firsterr = torture_shuffle_init(shuffle_interval);
  361. if (firsterr)
  362. goto unwind;
  363. }
  364. if (shutdown_secs > 0) {
  365. firsterr = torture_shutdown_init(shutdown_secs,
  366. lock_torture_cleanup);
  367. if (firsterr)
  368. goto unwind;
  369. }
  370. if (stutter > 0) {
  371. firsterr = torture_stutter_init(stutter);
  372. if (firsterr)
  373. goto unwind;
  374. }
  375. writer_tasks = kzalloc(nrealwriters_stress * sizeof(writer_tasks[0]),
  376. GFP_KERNEL);
  377. if (writer_tasks == NULL) {
  378. VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
  379. firsterr = -ENOMEM;
  380. goto unwind;
  381. }
  382. for (i = 0; i < nrealwriters_stress; i++) {
  383. firsterr = torture_create_kthread(lock_torture_writer, &lwsa[i],
  384. writer_tasks[i]);
  385. if (firsterr)
  386. goto unwind;
  387. }
  388. if (stat_interval > 0) {
  389. firsterr = torture_create_kthread(lock_torture_stats, NULL,
  390. stats_task);
  391. if (firsterr)
  392. goto unwind;
  393. }
  394. torture_init_end();
  395. return 0;
  396. unwind:
  397. torture_init_end();
  398. lock_torture_cleanup();
  399. return firsterr;
  400. }
  401. module_init(lock_torture_init);
  402. module_exit(lock_torture_cleanup);