torture.c 22 KB

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