torture.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Common functions for in-kernel torture tests.
  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. static char *torture_type;
  51. static bool verbose;
  52. /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
  53. #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
  54. #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
  55. #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
  56. static int fullstop = FULLSTOP_RMMOD;
  57. static DEFINE_MUTEX(fullstop_mutex);
  58. static int *torture_runnable;
  59. #ifdef CONFIG_HOTPLUG_CPU
  60. /*
  61. * Variables for online-offline handling. Only present if CPU hotplug
  62. * is enabled, otherwise does nothing.
  63. */
  64. static struct task_struct *onoff_task;
  65. static long onoff_holdoff;
  66. static long onoff_interval;
  67. static long n_offline_attempts;
  68. static long n_offline_successes;
  69. static unsigned long sum_offline;
  70. static int min_offline = -1;
  71. static int max_offline;
  72. static long n_online_attempts;
  73. static long n_online_successes;
  74. static unsigned long sum_online;
  75. static int min_online = -1;
  76. static int max_online;
  77. /*
  78. * Attempt to take a CPU offline. Return false if the CPU is already
  79. * offline or if it is not subject to CPU-hotplug operations. The
  80. * caller can detect other failures by looking at the statistics.
  81. */
  82. bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
  83. unsigned long *sum_offl, int *min_offl, int *max_offl)
  84. {
  85. unsigned long delta;
  86. int ret;
  87. unsigned long starttime;
  88. if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
  89. return false;
  90. if (verbose)
  91. pr_alert("%s" TORTURE_FLAG
  92. "torture_onoff task: offlining %d\n",
  93. torture_type, cpu);
  94. starttime = jiffies;
  95. (*n_offl_attempts)++;
  96. ret = cpu_down(cpu);
  97. if (ret) {
  98. if (verbose)
  99. pr_alert("%s" TORTURE_FLAG
  100. "torture_onoff task: offline %d failed: errno %d\n",
  101. torture_type, cpu, ret);
  102. } else {
  103. if (verbose)
  104. pr_alert("%s" TORTURE_FLAG
  105. "torture_onoff task: offlined %d\n",
  106. torture_type, cpu);
  107. (*n_offl_successes)++;
  108. delta = jiffies - starttime;
  109. sum_offl += delta;
  110. if (*min_offl < 0) {
  111. *min_offl = delta;
  112. *max_offl = delta;
  113. }
  114. if (*min_offl > delta)
  115. *min_offl = delta;
  116. if (*max_offl < delta)
  117. *max_offl = delta;
  118. }
  119. return true;
  120. }
  121. EXPORT_SYMBOL_GPL(torture_offline);
  122. /*
  123. * Attempt to bring a CPU online. Return false if the CPU is already
  124. * online or if it is not subject to CPU-hotplug operations. The
  125. * caller can detect other failures by looking at the statistics.
  126. */
  127. bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
  128. unsigned long *sum_onl, int *min_onl, int *max_onl)
  129. {
  130. unsigned long delta;
  131. int ret;
  132. unsigned long starttime;
  133. if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
  134. return false;
  135. if (verbose)
  136. pr_alert("%s" TORTURE_FLAG
  137. "torture_onoff task: onlining %d\n",
  138. torture_type, cpu);
  139. starttime = jiffies;
  140. (*n_onl_attempts)++;
  141. ret = cpu_up(cpu);
  142. if (ret) {
  143. if (verbose)
  144. pr_alert("%s" TORTURE_FLAG
  145. "torture_onoff task: online %d failed: errno %d\n",
  146. torture_type, cpu, ret);
  147. } else {
  148. if (verbose)
  149. pr_alert("%s" TORTURE_FLAG
  150. "torture_onoff task: onlined %d\n",
  151. torture_type, cpu);
  152. (*n_onl_successes)++;
  153. delta = jiffies - starttime;
  154. *sum_onl += delta;
  155. if (*min_onl < 0) {
  156. *min_onl = delta;
  157. *max_onl = delta;
  158. }
  159. if (*min_onl > delta)
  160. *min_onl = delta;
  161. if (*max_onl < delta)
  162. *max_onl = delta;
  163. }
  164. return true;
  165. }
  166. EXPORT_SYMBOL_GPL(torture_online);
  167. /*
  168. * Execute random CPU-hotplug operations at the interval specified
  169. * by the onoff_interval.
  170. */
  171. static int
  172. torture_onoff(void *arg)
  173. {
  174. int cpu;
  175. int maxcpu = -1;
  176. DEFINE_TORTURE_RANDOM(rand);
  177. VERBOSE_TOROUT_STRING("torture_onoff task started");
  178. for_each_online_cpu(cpu)
  179. maxcpu = cpu;
  180. WARN_ON(maxcpu < 0);
  181. if (maxcpu == 0) {
  182. VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
  183. goto stop;
  184. }
  185. if (onoff_holdoff > 0) {
  186. VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
  187. schedule_timeout_interruptible(onoff_holdoff);
  188. VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
  189. }
  190. while (!torture_must_stop()) {
  191. cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
  192. if (!torture_offline(cpu,
  193. &n_offline_attempts, &n_offline_successes,
  194. &sum_offline, &min_offline, &max_offline))
  195. torture_online(cpu,
  196. &n_online_attempts, &n_online_successes,
  197. &sum_online, &min_online, &max_online);
  198. schedule_timeout_interruptible(onoff_interval);
  199. }
  200. stop:
  201. torture_kthread_stopping("torture_onoff");
  202. return 0;
  203. }
  204. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  205. /*
  206. * Initiate online-offline handling.
  207. */
  208. int torture_onoff_init(long ooholdoff, long oointerval)
  209. {
  210. int ret = 0;
  211. #ifdef CONFIG_HOTPLUG_CPU
  212. onoff_holdoff = ooholdoff;
  213. onoff_interval = oointerval;
  214. if (onoff_interval <= 0)
  215. return 0;
  216. ret = torture_create_kthread(torture_onoff, NULL, onoff_task);
  217. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  218. return ret;
  219. }
  220. EXPORT_SYMBOL_GPL(torture_onoff_init);
  221. /*
  222. * Clean up after online/offline testing.
  223. */
  224. static void torture_onoff_cleanup(void)
  225. {
  226. #ifdef CONFIG_HOTPLUG_CPU
  227. if (onoff_task == NULL)
  228. return;
  229. VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
  230. kthread_stop(onoff_task);
  231. onoff_task = NULL;
  232. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  233. }
  234. EXPORT_SYMBOL_GPL(torture_onoff_cleanup);
  235. /*
  236. * Print online/offline testing statistics.
  237. */
  238. void torture_onoff_stats(void)
  239. {
  240. #ifdef CONFIG_HOTPLUG_CPU
  241. pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
  242. n_online_successes, n_online_attempts,
  243. n_offline_successes, n_offline_attempts,
  244. min_online, max_online,
  245. min_offline, max_offline,
  246. sum_online, sum_offline, HZ);
  247. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  248. }
  249. EXPORT_SYMBOL_GPL(torture_onoff_stats);
  250. /*
  251. * Were all the online/offline operations successful?
  252. */
  253. bool torture_onoff_failures(void)
  254. {
  255. #ifdef CONFIG_HOTPLUG_CPU
  256. return n_online_successes != n_online_attempts ||
  257. n_offline_successes != n_offline_attempts;
  258. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  259. return false;
  260. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  261. }
  262. EXPORT_SYMBOL_GPL(torture_onoff_failures);
  263. #define TORTURE_RANDOM_MULT 39916801 /* prime */
  264. #define TORTURE_RANDOM_ADD 479001701 /* prime */
  265. #define TORTURE_RANDOM_REFRESH 10000
  266. /*
  267. * Crude but fast random-number generator. Uses a linear congruential
  268. * generator, with occasional help from cpu_clock().
  269. */
  270. unsigned long
  271. torture_random(struct torture_random_state *trsp)
  272. {
  273. if (--trsp->trs_count < 0) {
  274. trsp->trs_state += (unsigned long)local_clock();
  275. trsp->trs_count = TORTURE_RANDOM_REFRESH;
  276. }
  277. trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
  278. TORTURE_RANDOM_ADD;
  279. return swahw32(trsp->trs_state);
  280. }
  281. EXPORT_SYMBOL_GPL(torture_random);
  282. /*
  283. * Variables for shuffling. The idea is to ensure that each CPU stays
  284. * idle for an extended period to test interactions with dyntick idle,
  285. * as well as interactions with any per-CPU varibles.
  286. */
  287. struct shuffle_task {
  288. struct list_head st_l;
  289. struct task_struct *st_t;
  290. };
  291. static long shuffle_interval; /* In jiffies. */
  292. static struct task_struct *shuffler_task;
  293. static cpumask_var_t shuffle_tmp_mask;
  294. static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
  295. static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
  296. static DEFINE_MUTEX(shuffle_task_mutex);
  297. /*
  298. * Register a task to be shuffled. If there is no memory, just splat
  299. * and don't bother registering.
  300. */
  301. void torture_shuffle_task_register(struct task_struct *tp)
  302. {
  303. struct shuffle_task *stp;
  304. if (WARN_ON_ONCE(tp == NULL))
  305. return;
  306. stp = kmalloc(sizeof(*stp), GFP_KERNEL);
  307. if (WARN_ON_ONCE(stp == NULL))
  308. return;
  309. stp->st_t = tp;
  310. mutex_lock(&shuffle_task_mutex);
  311. list_add(&stp->st_l, &shuffle_task_list);
  312. mutex_unlock(&shuffle_task_mutex);
  313. }
  314. EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
  315. /*
  316. * Unregister all tasks, for example, at the end of the torture run.
  317. */
  318. static void torture_shuffle_task_unregister_all(void)
  319. {
  320. struct shuffle_task *stp;
  321. struct shuffle_task *p;
  322. mutex_lock(&shuffle_task_mutex);
  323. list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
  324. list_del(&stp->st_l);
  325. kfree(stp);
  326. }
  327. mutex_unlock(&shuffle_task_mutex);
  328. }
  329. /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
  330. * A special case is when shuffle_idle_cpu = -1, in which case we allow
  331. * the tasks to run on all CPUs.
  332. */
  333. static void torture_shuffle_tasks(void)
  334. {
  335. struct shuffle_task *stp;
  336. cpumask_setall(shuffle_tmp_mask);
  337. get_online_cpus();
  338. /* No point in shuffling if there is only one online CPU (ex: UP) */
  339. if (num_online_cpus() == 1) {
  340. put_online_cpus();
  341. return;
  342. }
  343. /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
  344. shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
  345. if (shuffle_idle_cpu >= nr_cpu_ids)
  346. shuffle_idle_cpu = -1;
  347. else
  348. cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
  349. mutex_lock(&shuffle_task_mutex);
  350. list_for_each_entry(stp, &shuffle_task_list, st_l)
  351. set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
  352. mutex_unlock(&shuffle_task_mutex);
  353. put_online_cpus();
  354. }
  355. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  356. * system to become idle at a time and cut off its timer ticks. This is meant
  357. * to test the support for such tickless idle CPU in RCU.
  358. */
  359. static int torture_shuffle(void *arg)
  360. {
  361. VERBOSE_TOROUT_STRING("torture_shuffle task started");
  362. do {
  363. schedule_timeout_interruptible(shuffle_interval);
  364. torture_shuffle_tasks();
  365. torture_shutdown_absorb("torture_shuffle");
  366. } while (!torture_must_stop());
  367. torture_kthread_stopping("torture_shuffle");
  368. return 0;
  369. }
  370. /*
  371. * Start the shuffler, with shuffint in jiffies.
  372. */
  373. int torture_shuffle_init(long shuffint)
  374. {
  375. shuffle_interval = shuffint;
  376. shuffle_idle_cpu = -1;
  377. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  378. VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
  379. return -ENOMEM;
  380. }
  381. /* Create the shuffler thread */
  382. return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
  383. }
  384. EXPORT_SYMBOL_GPL(torture_shuffle_init);
  385. /*
  386. * Stop the shuffling.
  387. */
  388. static void torture_shuffle_cleanup(void)
  389. {
  390. torture_shuffle_task_unregister_all();
  391. if (shuffler_task) {
  392. VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
  393. kthread_stop(shuffler_task);
  394. free_cpumask_var(shuffle_tmp_mask);
  395. }
  396. shuffler_task = NULL;
  397. }
  398. EXPORT_SYMBOL_GPL(torture_shuffle_cleanup);
  399. /*
  400. * Variables for auto-shutdown. This allows "lights out" torture runs
  401. * to be fully scripted.
  402. */
  403. static int shutdown_secs; /* desired test duration in seconds. */
  404. static struct task_struct *shutdown_task;
  405. static unsigned long shutdown_time; /* jiffies to system shutdown. */
  406. static void (*torture_shutdown_hook)(void);
  407. /*
  408. * Absorb kthreads into a kernel function that won't return, so that
  409. * they won't ever access module text or data again.
  410. */
  411. void torture_shutdown_absorb(const char *title)
  412. {
  413. while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  414. pr_notice("torture thread %s parking due to system shutdown\n",
  415. title);
  416. schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  417. }
  418. }
  419. EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
  420. /*
  421. * Cause the torture test to shutdown the system after the test has
  422. * run for the time specified by the shutdown_secs parameter.
  423. */
  424. static int torture_shutdown(void *arg)
  425. {
  426. long delta;
  427. unsigned long jiffies_snap;
  428. VERBOSE_TOROUT_STRING("torture_shutdown task started");
  429. jiffies_snap = jiffies;
  430. while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
  431. !torture_must_stop()) {
  432. delta = shutdown_time - jiffies_snap;
  433. if (verbose)
  434. pr_alert("%s" TORTURE_FLAG
  435. "torture_shutdown task: %lu jiffies remaining\n",
  436. torture_type, delta);
  437. schedule_timeout_interruptible(delta);
  438. jiffies_snap = jiffies;
  439. }
  440. if (torture_must_stop()) {
  441. torture_kthread_stopping("torture_shutdown");
  442. return 0;
  443. }
  444. /* OK, shut down the system. */
  445. VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
  446. shutdown_task = NULL; /* Avoid self-kill deadlock. */
  447. if (torture_shutdown_hook)
  448. torture_shutdown_hook();
  449. else
  450. VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
  451. ftrace_dump(DUMP_ALL);
  452. kernel_power_off(); /* Shut down the system. */
  453. return 0;
  454. }
  455. /*
  456. * Start up the shutdown task.
  457. */
  458. int torture_shutdown_init(int ssecs, void (*cleanup)(void))
  459. {
  460. int ret = 0;
  461. shutdown_secs = ssecs;
  462. torture_shutdown_hook = cleanup;
  463. if (shutdown_secs > 0) {
  464. shutdown_time = jiffies + shutdown_secs * HZ;
  465. ret = torture_create_kthread(torture_shutdown, NULL,
  466. shutdown_task);
  467. }
  468. return ret;
  469. }
  470. EXPORT_SYMBOL_GPL(torture_shutdown_init);
  471. /*
  472. * Detect and respond to a system shutdown.
  473. */
  474. static int torture_shutdown_notify(struct notifier_block *unused1,
  475. unsigned long unused2, void *unused3)
  476. {
  477. mutex_lock(&fullstop_mutex);
  478. if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
  479. VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
  480. WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
  481. } else {
  482. pr_warn("Concurrent rmmod and shutdown illegal!\n");
  483. }
  484. mutex_unlock(&fullstop_mutex);
  485. return NOTIFY_DONE;
  486. }
  487. static struct notifier_block torture_shutdown_nb = {
  488. .notifier_call = torture_shutdown_notify,
  489. };
  490. /*
  491. * Shut down the shutdown task. Say what??? Heh! This can happen if
  492. * the torture module gets an rmmod before the shutdown time arrives. ;-)
  493. */
  494. static void torture_shutdown_cleanup(void)
  495. {
  496. unregister_reboot_notifier(&torture_shutdown_nb);
  497. if (shutdown_task != NULL) {
  498. VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
  499. kthread_stop(shutdown_task);
  500. }
  501. shutdown_task = NULL;
  502. }
  503. /*
  504. * Variables for stuttering, which means to periodically pause and
  505. * restart testing in order to catch bugs that appear when load is
  506. * suddenly applied to or removed from the system.
  507. */
  508. static struct task_struct *stutter_task;
  509. static int stutter_pause_test;
  510. static int stutter;
  511. /*
  512. * Block until the stutter interval ends. This must be called periodically
  513. * by all running kthreads that need to be subject to stuttering.
  514. */
  515. void stutter_wait(const char *title)
  516. {
  517. cond_resched_rcu_qs();
  518. while (READ_ONCE(stutter_pause_test) ||
  519. (torture_runnable && !READ_ONCE(*torture_runnable))) {
  520. if (stutter_pause_test)
  521. if (READ_ONCE(stutter_pause_test) == 1)
  522. schedule_timeout_interruptible(1);
  523. else
  524. while (READ_ONCE(stutter_pause_test))
  525. cond_resched();
  526. else
  527. schedule_timeout_interruptible(round_jiffies_relative(HZ));
  528. torture_shutdown_absorb(title);
  529. }
  530. }
  531. EXPORT_SYMBOL_GPL(stutter_wait);
  532. /*
  533. * Cause the torture test to "stutter", starting and stopping all
  534. * threads periodically.
  535. */
  536. static int torture_stutter(void *arg)
  537. {
  538. VERBOSE_TOROUT_STRING("torture_stutter task started");
  539. do {
  540. if (!torture_must_stop()) {
  541. if (stutter > 1) {
  542. schedule_timeout_interruptible(stutter - 1);
  543. WRITE_ONCE(stutter_pause_test, 2);
  544. }
  545. schedule_timeout_interruptible(1);
  546. WRITE_ONCE(stutter_pause_test, 1);
  547. }
  548. if (!torture_must_stop())
  549. schedule_timeout_interruptible(stutter);
  550. WRITE_ONCE(stutter_pause_test, 0);
  551. torture_shutdown_absorb("torture_stutter");
  552. } while (!torture_must_stop());
  553. torture_kthread_stopping("torture_stutter");
  554. return 0;
  555. }
  556. /*
  557. * Initialize and kick off the torture_stutter kthread.
  558. */
  559. int torture_stutter_init(int s)
  560. {
  561. int ret;
  562. stutter = s;
  563. ret = torture_create_kthread(torture_stutter, NULL, stutter_task);
  564. return ret;
  565. }
  566. EXPORT_SYMBOL_GPL(torture_stutter_init);
  567. /*
  568. * Cleanup after the torture_stutter kthread.
  569. */
  570. static void torture_stutter_cleanup(void)
  571. {
  572. if (!stutter_task)
  573. return;
  574. VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
  575. kthread_stop(stutter_task);
  576. stutter_task = NULL;
  577. }
  578. /*
  579. * Initialize torture module. Please note that this is -not- invoked via
  580. * the usual module_init() mechanism, but rather by an explicit call from
  581. * the client torture module. This call must be paired with a later
  582. * torture_init_end().
  583. *
  584. * The runnable parameter points to a flag that controls whether or not
  585. * the test is currently runnable. If there is no such flag, pass in NULL.
  586. */
  587. bool torture_init_begin(char *ttype, bool v, int *runnable)
  588. {
  589. mutex_lock(&fullstop_mutex);
  590. if (torture_type != NULL) {
  591. pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
  592. ttype, torture_type);
  593. pr_alert("torture_init_begin: One torture test at a time!\n");
  594. mutex_unlock(&fullstop_mutex);
  595. return false;
  596. }
  597. torture_type = ttype;
  598. verbose = v;
  599. torture_runnable = runnable;
  600. fullstop = FULLSTOP_DONTSTOP;
  601. return true;
  602. }
  603. EXPORT_SYMBOL_GPL(torture_init_begin);
  604. /*
  605. * Tell the torture module that initialization is complete.
  606. */
  607. void torture_init_end(void)
  608. {
  609. mutex_unlock(&fullstop_mutex);
  610. register_reboot_notifier(&torture_shutdown_nb);
  611. }
  612. EXPORT_SYMBOL_GPL(torture_init_end);
  613. /*
  614. * Clean up torture module. Please note that this is -not- invoked via
  615. * the usual module_exit() mechanism, but rather by an explicit call from
  616. * the client torture module. Returns true if a race with system shutdown
  617. * is detected, otherwise, all kthreads started by functions in this file
  618. * will be shut down.
  619. *
  620. * This must be called before the caller starts shutting down its own
  621. * kthreads.
  622. *
  623. * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
  624. * in order to correctly perform the cleanup. They are separated because
  625. * threads can still need to reference the torture_type type, thus nullify
  626. * only after completing all other relevant calls.
  627. */
  628. bool torture_cleanup_begin(void)
  629. {
  630. mutex_lock(&fullstop_mutex);
  631. if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  632. pr_warn("Concurrent rmmod and shutdown illegal!\n");
  633. mutex_unlock(&fullstop_mutex);
  634. schedule_timeout_uninterruptible(10);
  635. return true;
  636. }
  637. WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
  638. mutex_unlock(&fullstop_mutex);
  639. torture_shutdown_cleanup();
  640. torture_shuffle_cleanup();
  641. torture_stutter_cleanup();
  642. torture_onoff_cleanup();
  643. return false;
  644. }
  645. EXPORT_SYMBOL_GPL(torture_cleanup_begin);
  646. void torture_cleanup_end(void)
  647. {
  648. mutex_lock(&fullstop_mutex);
  649. torture_type = NULL;
  650. mutex_unlock(&fullstop_mutex);
  651. }
  652. EXPORT_SYMBOL_GPL(torture_cleanup_end);
  653. /*
  654. * Is it time for the current torture test to stop?
  655. */
  656. bool torture_must_stop(void)
  657. {
  658. return torture_must_stop_irq() || kthread_should_stop();
  659. }
  660. EXPORT_SYMBOL_GPL(torture_must_stop);
  661. /*
  662. * Is it time for the current torture test to stop? This is the irq-safe
  663. * version, hence no check for kthread_should_stop().
  664. */
  665. bool torture_must_stop_irq(void)
  666. {
  667. return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
  668. }
  669. EXPORT_SYMBOL_GPL(torture_must_stop_irq);
  670. /*
  671. * Each kthread must wait for kthread_should_stop() before returning from
  672. * its top-level function, otherwise segfaults ensue. This function
  673. * prints a "stopping" message and waits for kthread_should_stop(), and
  674. * should be called from all torture kthreads immediately prior to
  675. * returning.
  676. */
  677. void torture_kthread_stopping(char *title)
  678. {
  679. char buf[128];
  680. snprintf(buf, sizeof(buf), "Stopping %s", title);
  681. VERBOSE_TOROUT_STRING(buf);
  682. while (!kthread_should_stop()) {
  683. torture_shutdown_absorb(title);
  684. schedule_timeout_uninterruptible(1);
  685. }
  686. }
  687. EXPORT_SYMBOL_GPL(torture_kthread_stopping);
  688. /*
  689. * Create a generic torture kthread that is immediately runnable. If you
  690. * need the kthread to be stopped so that you can do something to it before
  691. * it starts, you will need to open-code your own.
  692. */
  693. int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
  694. char *f, struct task_struct **tp)
  695. {
  696. int ret = 0;
  697. VERBOSE_TOROUT_STRING(m);
  698. *tp = kthread_run(fn, arg, "%s", s);
  699. if (IS_ERR(*tp)) {
  700. ret = PTR_ERR(*tp);
  701. VERBOSE_TOROUT_ERRSTRING(f);
  702. *tp = NULL;
  703. }
  704. torture_shuffle_task_register(*tp);
  705. return ret;
  706. }
  707. EXPORT_SYMBOL_GPL(_torture_create_kthread);
  708. /*
  709. * Stop a generic kthread, emitting a message.
  710. */
  711. void _torture_stop_kthread(char *m, struct task_struct **tp)
  712. {
  713. if (*tp == NULL)
  714. return;
  715. VERBOSE_TOROUT_STRING(m);
  716. kthread_stop(*tp);
  717. *tp = NULL;
  718. }
  719. EXPORT_SYMBOL_GPL(_torture_stop_kthread);