torture.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. #define pr_fmt(fmt) fmt
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/kthread.h>
  29. #include <linux/err.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/smp.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/sched.h>
  34. #include <linux/sched/clock.h>
  35. #include <linux/atomic.h>
  36. #include <linux/bitops.h>
  37. #include <linux/completion.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/percpu.h>
  40. #include <linux/notifier.h>
  41. #include <linux/reboot.h>
  42. #include <linux/freezer.h>
  43. #include <linux/cpu.h>
  44. #include <linux/delay.h>
  45. #include <linux/stat.h>
  46. #include <linux/slab.h>
  47. #include <linux/trace_clock.h>
  48. #include <linux/ktime.h>
  49. #include <asm/byteorder.h>
  50. #include <linux/torture.h>
  51. #include "rcu/rcu.h"
  52. MODULE_LICENSE("GPL");
  53. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
  54. static char *torture_type;
  55. static int verbose;
  56. /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
  57. #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
  58. #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
  59. #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
  60. static int fullstop = FULLSTOP_RMMOD;
  61. static DEFINE_MUTEX(fullstop_mutex);
  62. #ifdef CONFIG_HOTPLUG_CPU
  63. /*
  64. * Variables for online-offline handling. Only present if CPU hotplug
  65. * is enabled, otherwise does nothing.
  66. */
  67. static struct task_struct *onoff_task;
  68. static long onoff_holdoff;
  69. static long onoff_interval;
  70. static long n_offline_attempts;
  71. static long n_offline_successes;
  72. static unsigned long sum_offline;
  73. static int min_offline = -1;
  74. static int max_offline;
  75. static long n_online_attempts;
  76. static long n_online_successes;
  77. static unsigned long sum_online;
  78. static int min_online = -1;
  79. static int max_online;
  80. /*
  81. * Attempt to take a CPU offline. Return false if the CPU is already
  82. * offline or if it is not subject to CPU-hotplug operations. The
  83. * caller can detect other failures by looking at the statistics.
  84. */
  85. bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
  86. unsigned long *sum_offl, int *min_offl, int *max_offl)
  87. {
  88. unsigned long delta;
  89. int ret;
  90. unsigned long starttime;
  91. if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
  92. return false;
  93. if (verbose > 1)
  94. pr_alert("%s" TORTURE_FLAG
  95. "torture_onoff task: offlining %d\n",
  96. torture_type, cpu);
  97. starttime = jiffies;
  98. (*n_offl_attempts)++;
  99. ret = cpu_down(cpu);
  100. if (ret) {
  101. if (verbose)
  102. pr_alert("%s" TORTURE_FLAG
  103. "torture_onoff task: offline %d failed: errno %d\n",
  104. torture_type, cpu, ret);
  105. } else {
  106. if (verbose > 1)
  107. pr_alert("%s" TORTURE_FLAG
  108. "torture_onoff task: offlined %d\n",
  109. torture_type, cpu);
  110. (*n_offl_successes)++;
  111. delta = jiffies - starttime;
  112. *sum_offl += delta;
  113. if (*min_offl < 0) {
  114. *min_offl = delta;
  115. *max_offl = delta;
  116. }
  117. if (*min_offl > delta)
  118. *min_offl = delta;
  119. if (*max_offl < delta)
  120. *max_offl = delta;
  121. }
  122. return true;
  123. }
  124. EXPORT_SYMBOL_GPL(torture_offline);
  125. /*
  126. * Attempt to bring a CPU online. Return false if the CPU is already
  127. * online or if it is not subject to CPU-hotplug operations. The
  128. * caller can detect other failures by looking at the statistics.
  129. */
  130. bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
  131. unsigned long *sum_onl, int *min_onl, int *max_onl)
  132. {
  133. unsigned long delta;
  134. int ret;
  135. unsigned long starttime;
  136. if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
  137. return false;
  138. if (verbose > 1)
  139. pr_alert("%s" TORTURE_FLAG
  140. "torture_onoff task: onlining %d\n",
  141. torture_type, cpu);
  142. starttime = jiffies;
  143. (*n_onl_attempts)++;
  144. ret = cpu_up(cpu);
  145. if (ret) {
  146. if (verbose)
  147. pr_alert("%s" TORTURE_FLAG
  148. "torture_onoff task: online %d failed: errno %d\n",
  149. torture_type, cpu, ret);
  150. } else {
  151. if (verbose > 1)
  152. pr_alert("%s" TORTURE_FLAG
  153. "torture_onoff task: onlined %d\n",
  154. torture_type, cpu);
  155. (*n_onl_successes)++;
  156. delta = jiffies - starttime;
  157. *sum_onl += delta;
  158. if (*min_onl < 0) {
  159. *min_onl = delta;
  160. *max_onl = delta;
  161. }
  162. if (*min_onl > delta)
  163. *min_onl = delta;
  164. if (*max_onl < delta)
  165. *max_onl = delta;
  166. }
  167. return true;
  168. }
  169. EXPORT_SYMBOL_GPL(torture_online);
  170. /*
  171. * Execute random CPU-hotplug operations at the interval specified
  172. * by the onoff_interval.
  173. */
  174. static int
  175. torture_onoff(void *arg)
  176. {
  177. int cpu;
  178. int maxcpu = -1;
  179. DEFINE_TORTURE_RANDOM(rand);
  180. VERBOSE_TOROUT_STRING("torture_onoff task started");
  181. for_each_online_cpu(cpu)
  182. maxcpu = cpu;
  183. WARN_ON(maxcpu < 0);
  184. if (maxcpu == 0) {
  185. VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
  186. goto stop;
  187. }
  188. if (onoff_holdoff > 0) {
  189. VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
  190. schedule_timeout_interruptible(onoff_holdoff);
  191. VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
  192. }
  193. while (!torture_must_stop()) {
  194. cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
  195. if (!torture_offline(cpu,
  196. &n_offline_attempts, &n_offline_successes,
  197. &sum_offline, &min_offline, &max_offline))
  198. torture_online(cpu,
  199. &n_online_attempts, &n_online_successes,
  200. &sum_online, &min_online, &max_online);
  201. schedule_timeout_interruptible(onoff_interval);
  202. }
  203. stop:
  204. torture_kthread_stopping("torture_onoff");
  205. return 0;
  206. }
  207. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  208. /*
  209. * Initiate online-offline handling.
  210. */
  211. int torture_onoff_init(long ooholdoff, long oointerval)
  212. {
  213. int ret = 0;
  214. #ifdef CONFIG_HOTPLUG_CPU
  215. onoff_holdoff = ooholdoff;
  216. onoff_interval = oointerval;
  217. if (onoff_interval <= 0)
  218. return 0;
  219. ret = torture_create_kthread(torture_onoff, NULL, onoff_task);
  220. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  221. return ret;
  222. }
  223. EXPORT_SYMBOL_GPL(torture_onoff_init);
  224. /*
  225. * Clean up after online/offline testing.
  226. */
  227. static void torture_onoff_cleanup(void)
  228. {
  229. #ifdef CONFIG_HOTPLUG_CPU
  230. if (onoff_task == NULL)
  231. return;
  232. VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
  233. kthread_stop(onoff_task);
  234. onoff_task = NULL;
  235. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  236. }
  237. EXPORT_SYMBOL_GPL(torture_onoff_cleanup);
  238. /*
  239. * Print online/offline testing statistics.
  240. */
  241. void torture_onoff_stats(void)
  242. {
  243. #ifdef CONFIG_HOTPLUG_CPU
  244. pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
  245. n_online_successes, n_online_attempts,
  246. n_offline_successes, n_offline_attempts,
  247. min_online, max_online,
  248. min_offline, max_offline,
  249. sum_online, sum_offline, HZ);
  250. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  251. }
  252. EXPORT_SYMBOL_GPL(torture_onoff_stats);
  253. /*
  254. * Were all the online/offline operations successful?
  255. */
  256. bool torture_onoff_failures(void)
  257. {
  258. #ifdef CONFIG_HOTPLUG_CPU
  259. return n_online_successes != n_online_attempts ||
  260. n_offline_successes != n_offline_attempts;
  261. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  262. return false;
  263. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  264. }
  265. EXPORT_SYMBOL_GPL(torture_onoff_failures);
  266. #define TORTURE_RANDOM_MULT 39916801 /* prime */
  267. #define TORTURE_RANDOM_ADD 479001701 /* prime */
  268. #define TORTURE_RANDOM_REFRESH 10000
  269. /*
  270. * Crude but fast random-number generator. Uses a linear congruential
  271. * generator, with occasional help from cpu_clock().
  272. */
  273. unsigned long
  274. torture_random(struct torture_random_state *trsp)
  275. {
  276. if (--trsp->trs_count < 0) {
  277. trsp->trs_state += (unsigned long)local_clock();
  278. trsp->trs_count = TORTURE_RANDOM_REFRESH;
  279. }
  280. trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
  281. TORTURE_RANDOM_ADD;
  282. return swahw32(trsp->trs_state);
  283. }
  284. EXPORT_SYMBOL_GPL(torture_random);
  285. /*
  286. * Variables for shuffling. The idea is to ensure that each CPU stays
  287. * idle for an extended period to test interactions with dyntick idle,
  288. * as well as interactions with any per-CPU variables.
  289. */
  290. struct shuffle_task {
  291. struct list_head st_l;
  292. struct task_struct *st_t;
  293. };
  294. static long shuffle_interval; /* In jiffies. */
  295. static struct task_struct *shuffler_task;
  296. static cpumask_var_t shuffle_tmp_mask;
  297. static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
  298. static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
  299. static DEFINE_MUTEX(shuffle_task_mutex);
  300. /*
  301. * Register a task to be shuffled. If there is no memory, just splat
  302. * and don't bother registering.
  303. */
  304. void torture_shuffle_task_register(struct task_struct *tp)
  305. {
  306. struct shuffle_task *stp;
  307. if (WARN_ON_ONCE(tp == NULL))
  308. return;
  309. stp = kmalloc(sizeof(*stp), GFP_KERNEL);
  310. if (WARN_ON_ONCE(stp == NULL))
  311. return;
  312. stp->st_t = tp;
  313. mutex_lock(&shuffle_task_mutex);
  314. list_add(&stp->st_l, &shuffle_task_list);
  315. mutex_unlock(&shuffle_task_mutex);
  316. }
  317. EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
  318. /*
  319. * Unregister all tasks, for example, at the end of the torture run.
  320. */
  321. static void torture_shuffle_task_unregister_all(void)
  322. {
  323. struct shuffle_task *stp;
  324. struct shuffle_task *p;
  325. mutex_lock(&shuffle_task_mutex);
  326. list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
  327. list_del(&stp->st_l);
  328. kfree(stp);
  329. }
  330. mutex_unlock(&shuffle_task_mutex);
  331. }
  332. /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
  333. * A special case is when shuffle_idle_cpu = -1, in which case we allow
  334. * the tasks to run on all CPUs.
  335. */
  336. static void torture_shuffle_tasks(void)
  337. {
  338. struct shuffle_task *stp;
  339. cpumask_setall(shuffle_tmp_mask);
  340. get_online_cpus();
  341. /* No point in shuffling if there is only one online CPU (ex: UP) */
  342. if (num_online_cpus() == 1) {
  343. put_online_cpus();
  344. return;
  345. }
  346. /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
  347. shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
  348. if (shuffle_idle_cpu >= nr_cpu_ids)
  349. shuffle_idle_cpu = -1;
  350. else
  351. cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
  352. mutex_lock(&shuffle_task_mutex);
  353. list_for_each_entry(stp, &shuffle_task_list, st_l)
  354. set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
  355. mutex_unlock(&shuffle_task_mutex);
  356. put_online_cpus();
  357. }
  358. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  359. * system to become idle at a time and cut off its timer ticks. This is meant
  360. * to test the support for such tickless idle CPU in RCU.
  361. */
  362. static int torture_shuffle(void *arg)
  363. {
  364. VERBOSE_TOROUT_STRING("torture_shuffle task started");
  365. do {
  366. schedule_timeout_interruptible(shuffle_interval);
  367. torture_shuffle_tasks();
  368. torture_shutdown_absorb("torture_shuffle");
  369. } while (!torture_must_stop());
  370. torture_kthread_stopping("torture_shuffle");
  371. return 0;
  372. }
  373. /*
  374. * Start the shuffler, with shuffint in jiffies.
  375. */
  376. int torture_shuffle_init(long shuffint)
  377. {
  378. shuffle_interval = shuffint;
  379. shuffle_idle_cpu = -1;
  380. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  381. VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
  382. return -ENOMEM;
  383. }
  384. /* Create the shuffler thread */
  385. return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
  386. }
  387. EXPORT_SYMBOL_GPL(torture_shuffle_init);
  388. /*
  389. * Stop the shuffling.
  390. */
  391. static void torture_shuffle_cleanup(void)
  392. {
  393. torture_shuffle_task_unregister_all();
  394. if (shuffler_task) {
  395. VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
  396. kthread_stop(shuffler_task);
  397. free_cpumask_var(shuffle_tmp_mask);
  398. }
  399. shuffler_task = NULL;
  400. }
  401. EXPORT_SYMBOL_GPL(torture_shuffle_cleanup);
  402. /*
  403. * Variables for auto-shutdown. This allows "lights out" torture runs
  404. * to be fully scripted.
  405. */
  406. static struct task_struct *shutdown_task;
  407. static ktime_t shutdown_time; /* time to system shutdown. */
  408. static void (*torture_shutdown_hook)(void);
  409. /*
  410. * Absorb kthreads into a kernel function that won't return, so that
  411. * they won't ever access module text or data again.
  412. */
  413. void torture_shutdown_absorb(const char *title)
  414. {
  415. while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  416. pr_notice("torture thread %s parking due to system shutdown\n",
  417. title);
  418. schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  419. }
  420. }
  421. EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
  422. /*
  423. * Cause the torture test to shutdown the system after the test has
  424. * run for the time specified by the shutdown_secs parameter.
  425. */
  426. static int torture_shutdown(void *arg)
  427. {
  428. ktime_t ktime_snap;
  429. VERBOSE_TOROUT_STRING("torture_shutdown task started");
  430. ktime_snap = ktime_get();
  431. while (ktime_before(ktime_snap, shutdown_time) &&
  432. !torture_must_stop()) {
  433. if (verbose)
  434. pr_alert("%s" TORTURE_FLAG
  435. "torture_shutdown task: %llu ms remaining\n",
  436. torture_type,
  437. ktime_ms_delta(shutdown_time, ktime_snap));
  438. set_current_state(TASK_INTERRUPTIBLE);
  439. schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
  440. ktime_snap = ktime_get();
  441. }
  442. if (torture_must_stop()) {
  443. torture_kthread_stopping("torture_shutdown");
  444. return 0;
  445. }
  446. /* OK, shut down the system. */
  447. VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
  448. shutdown_task = NULL; /* Avoid self-kill deadlock. */
  449. if (torture_shutdown_hook)
  450. torture_shutdown_hook();
  451. else
  452. VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
  453. rcu_ftrace_dump(DUMP_ALL);
  454. kernel_power_off(); /* Shut down the system. */
  455. return 0;
  456. }
  457. /*
  458. * Start up the shutdown task.
  459. */
  460. int torture_shutdown_init(int ssecs, void (*cleanup)(void))
  461. {
  462. int ret = 0;
  463. torture_shutdown_hook = cleanup;
  464. if (ssecs > 0) {
  465. shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
  466. ret = torture_create_kthread(torture_shutdown, NULL,
  467. shutdown_task);
  468. }
  469. return ret;
  470. }
  471. EXPORT_SYMBOL_GPL(torture_shutdown_init);
  472. /*
  473. * Detect and respond to a system shutdown.
  474. */
  475. static int torture_shutdown_notify(struct notifier_block *unused1,
  476. unsigned long unused2, void *unused3)
  477. {
  478. mutex_lock(&fullstop_mutex);
  479. if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
  480. VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
  481. WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
  482. } else {
  483. pr_warn("Concurrent rmmod and shutdown illegal!\n");
  484. }
  485. mutex_unlock(&fullstop_mutex);
  486. return NOTIFY_DONE;
  487. }
  488. static struct notifier_block torture_shutdown_nb = {
  489. .notifier_call = torture_shutdown_notify,
  490. };
  491. /*
  492. * Shut down the shutdown task. Say what??? Heh! This can happen if
  493. * the torture module gets an rmmod before the shutdown time arrives. ;-)
  494. */
  495. static void torture_shutdown_cleanup(void)
  496. {
  497. unregister_reboot_notifier(&torture_shutdown_nb);
  498. if (shutdown_task != NULL) {
  499. VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
  500. kthread_stop(shutdown_task);
  501. }
  502. shutdown_task = NULL;
  503. }
  504. /*
  505. * Variables for stuttering, which means to periodically pause and
  506. * restart testing in order to catch bugs that appear when load is
  507. * suddenly applied to or removed from the system.
  508. */
  509. static struct task_struct *stutter_task;
  510. static int stutter_pause_test;
  511. static int stutter;
  512. /*
  513. * Block until the stutter interval ends. This must be called periodically
  514. * by all running kthreads that need to be subject to stuttering.
  515. */
  516. void stutter_wait(const char *title)
  517. {
  518. int spt;
  519. cond_resched_tasks_rcu_qs();
  520. spt = READ_ONCE(stutter_pause_test);
  521. for (; spt; spt = READ_ONCE(stutter_pause_test)) {
  522. if (spt == 1) {
  523. schedule_timeout_interruptible(1);
  524. } else if (spt == 2) {
  525. while (READ_ONCE(stutter_pause_test))
  526. cond_resched();
  527. } else {
  528. schedule_timeout_interruptible(round_jiffies_relative(HZ));
  529. }
  530. torture_shutdown_absorb(title);
  531. }
  532. }
  533. EXPORT_SYMBOL_GPL(stutter_wait);
  534. /*
  535. * Cause the torture test to "stutter", starting and stopping all
  536. * threads periodically.
  537. */
  538. static int torture_stutter(void *arg)
  539. {
  540. VERBOSE_TOROUT_STRING("torture_stutter task started");
  541. do {
  542. if (!torture_must_stop() && stutter > 1) {
  543. WRITE_ONCE(stutter_pause_test, 1);
  544. schedule_timeout_interruptible(stutter - 1);
  545. WRITE_ONCE(stutter_pause_test, 2);
  546. schedule_timeout_interruptible(1);
  547. }
  548. WRITE_ONCE(stutter_pause_test, 0);
  549. if (!torture_must_stop())
  550. schedule_timeout_interruptible(stutter);
  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, int v)
  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. 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);