torture.c 21 KB

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