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