torture.c 26 KB

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