torture.c 25 KB

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