rcutorture.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /*
  2. * Read-Copy Update module-based torture test facility
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2005, 2006
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. * Josh Triplett <josh@freedesktop.org>
  22. *
  23. * See also: Documentation/RCU/torture.txt
  24. */
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/kthread.h>
  30. #include <linux/err.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/smp.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/sched.h>
  36. #include <asm/atomic.h>
  37. #include <linux/bitops.h>
  38. #include <linux/completion.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/percpu.h>
  41. #include <linux/notifier.h>
  42. #include <linux/reboot.h>
  43. #include <linux/freezer.h>
  44. #include <linux/cpu.h>
  45. #include <linux/delay.h>
  46. #include <linux/stat.h>
  47. #include <linux/srcu.h>
  48. #include <linux/slab.h>
  49. #include <asm/byteorder.h>
  50. MODULE_LICENSE("GPL");
  51. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
  52. "Josh Triplett <josh@freedesktop.org>");
  53. static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
  54. static int nfakewriters = 4; /* # fake writer threads */
  55. static int stat_interval; /* Interval between stats, in seconds. */
  56. /* Defaults to "only at end of test". */
  57. static int verbose; /* Print more debug info. */
  58. static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
  59. static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
  60. static int stutter = 5; /* Start/stop testing interval (in sec) */
  61. static int irqreader = 1; /* RCU readers from irq (timers). */
  62. static int fqs_duration = 0; /* Duration of bursts (us), 0 to disable. */
  63. static int fqs_holdoff = 0; /* Hold time within burst (us). */
  64. static int fqs_stutter = 3; /* Wait time between bursts (s). */
  65. static char *torture_type = "rcu"; /* What RCU implementation to torture. */
  66. module_param(nreaders, int, 0444);
  67. MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  68. module_param(nfakewriters, int, 0444);
  69. MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
  70. module_param(stat_interval, int, 0444);
  71. MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  72. module_param(verbose, bool, 0444);
  73. MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  74. module_param(test_no_idle_hz, bool, 0444);
  75. MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
  76. module_param(shuffle_interval, int, 0444);
  77. MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
  78. module_param(stutter, int, 0444);
  79. MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
  80. module_param(irqreader, int, 0444);
  81. MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
  82. module_param(fqs_duration, int, 0444);
  83. MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
  84. module_param(fqs_holdoff, int, 0444);
  85. MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
  86. module_param(fqs_stutter, int, 0444);
  87. MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
  88. module_param(torture_type, charp, 0444);
  89. MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
  90. #define TORTURE_FLAG "-torture:"
  91. #define PRINTK_STRING(s) \
  92. do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  93. #define VERBOSE_PRINTK_STRING(s) \
  94. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  95. #define VERBOSE_PRINTK_ERRSTRING(s) \
  96. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
  97. static char printk_buf[4096];
  98. static int nrealreaders;
  99. static struct task_struct *writer_task;
  100. static struct task_struct **fakewriter_tasks;
  101. static struct task_struct **reader_tasks;
  102. static struct task_struct *stats_task;
  103. static struct task_struct *shuffler_task;
  104. static struct task_struct *stutter_task;
  105. static struct task_struct *fqs_task;
  106. #define RCU_TORTURE_PIPE_LEN 10
  107. struct rcu_torture {
  108. struct rcu_head rtort_rcu;
  109. int rtort_pipe_count;
  110. struct list_head rtort_free;
  111. int rtort_mbtest;
  112. };
  113. static LIST_HEAD(rcu_torture_freelist);
  114. static struct rcu_torture *rcu_torture_current;
  115. static long rcu_torture_current_version;
  116. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  117. static DEFINE_SPINLOCK(rcu_torture_lock);
  118. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
  119. { 0 };
  120. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
  121. { 0 };
  122. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  123. static atomic_t n_rcu_torture_alloc;
  124. static atomic_t n_rcu_torture_alloc_fail;
  125. static atomic_t n_rcu_torture_free;
  126. static atomic_t n_rcu_torture_mberror;
  127. static atomic_t n_rcu_torture_error;
  128. static long n_rcu_torture_timers;
  129. static struct list_head rcu_torture_removed;
  130. static cpumask_var_t shuffle_tmp_mask;
  131. static int stutter_pause_test;
  132. #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
  133. #define RCUTORTURE_RUNNABLE_INIT 1
  134. #else
  135. #define RCUTORTURE_RUNNABLE_INIT 0
  136. #endif
  137. int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
  138. /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
  139. #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
  140. #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
  141. #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
  142. static int fullstop = FULLSTOP_RMMOD;
  143. DEFINE_MUTEX(fullstop_mutex); /* Protect fullstop transitions and spawning */
  144. /* of kthreads. */
  145. /*
  146. * Detect and respond to a system shutdown.
  147. */
  148. static int
  149. rcutorture_shutdown_notify(struct notifier_block *unused1,
  150. unsigned long unused2, void *unused3)
  151. {
  152. mutex_lock(&fullstop_mutex);
  153. if (fullstop == FULLSTOP_DONTSTOP)
  154. fullstop = FULLSTOP_SHUTDOWN;
  155. else
  156. printk(KERN_WARNING /* but going down anyway, so... */
  157. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  158. mutex_unlock(&fullstop_mutex);
  159. return NOTIFY_DONE;
  160. }
  161. /*
  162. * Absorb kthreads into a kernel function that won't return, so that
  163. * they won't ever access module text or data again.
  164. */
  165. static void rcutorture_shutdown_absorb(char *title)
  166. {
  167. if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  168. printk(KERN_NOTICE
  169. "rcutorture thread %s parking due to system shutdown\n",
  170. title);
  171. schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  172. }
  173. }
  174. /*
  175. * Allocate an element from the rcu_tortures pool.
  176. */
  177. static struct rcu_torture *
  178. rcu_torture_alloc(void)
  179. {
  180. struct list_head *p;
  181. spin_lock_bh(&rcu_torture_lock);
  182. if (list_empty(&rcu_torture_freelist)) {
  183. atomic_inc(&n_rcu_torture_alloc_fail);
  184. spin_unlock_bh(&rcu_torture_lock);
  185. return NULL;
  186. }
  187. atomic_inc(&n_rcu_torture_alloc);
  188. p = rcu_torture_freelist.next;
  189. list_del_init(p);
  190. spin_unlock_bh(&rcu_torture_lock);
  191. return container_of(p, struct rcu_torture, rtort_free);
  192. }
  193. /*
  194. * Free an element to the rcu_tortures pool.
  195. */
  196. static void
  197. rcu_torture_free(struct rcu_torture *p)
  198. {
  199. atomic_inc(&n_rcu_torture_free);
  200. spin_lock_bh(&rcu_torture_lock);
  201. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  202. spin_unlock_bh(&rcu_torture_lock);
  203. }
  204. struct rcu_random_state {
  205. unsigned long rrs_state;
  206. long rrs_count;
  207. };
  208. #define RCU_RANDOM_MULT 39916801 /* prime */
  209. #define RCU_RANDOM_ADD 479001701 /* prime */
  210. #define RCU_RANDOM_REFRESH 10000
  211. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  212. /*
  213. * Crude but fast random-number generator. Uses a linear congruential
  214. * generator, with occasional help from cpu_clock().
  215. */
  216. static unsigned long
  217. rcu_random(struct rcu_random_state *rrsp)
  218. {
  219. if (--rrsp->rrs_count < 0) {
  220. rrsp->rrs_state += (unsigned long)local_clock();
  221. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  222. }
  223. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  224. return swahw32(rrsp->rrs_state);
  225. }
  226. static void
  227. rcu_stutter_wait(char *title)
  228. {
  229. while (stutter_pause_test || !rcutorture_runnable) {
  230. if (rcutorture_runnable)
  231. schedule_timeout_interruptible(1);
  232. else
  233. schedule_timeout_interruptible(round_jiffies_relative(HZ));
  234. rcutorture_shutdown_absorb(title);
  235. }
  236. }
  237. /*
  238. * Operations vector for selecting different types of tests.
  239. */
  240. struct rcu_torture_ops {
  241. void (*init)(void);
  242. void (*cleanup)(void);
  243. int (*readlock)(void);
  244. void (*read_delay)(struct rcu_random_state *rrsp);
  245. void (*readunlock)(int idx);
  246. int (*completed)(void);
  247. void (*deferred_free)(struct rcu_torture *p);
  248. void (*sync)(void);
  249. void (*cb_barrier)(void);
  250. void (*fqs)(void);
  251. int (*stats)(char *page);
  252. int irq_capable;
  253. char *name;
  254. };
  255. static struct rcu_torture_ops *cur_ops;
  256. /*
  257. * Definitions for rcu torture testing.
  258. */
  259. static int rcu_torture_read_lock(void) __acquires(RCU)
  260. {
  261. rcu_read_lock();
  262. return 0;
  263. }
  264. static void rcu_read_delay(struct rcu_random_state *rrsp)
  265. {
  266. const unsigned long shortdelay_us = 200;
  267. const unsigned long longdelay_ms = 50;
  268. /* We want a short delay sometimes to make a reader delay the grace
  269. * period, and we want a long delay occasionally to trigger
  270. * force_quiescent_state. */
  271. if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
  272. mdelay(longdelay_ms);
  273. if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
  274. udelay(shortdelay_us);
  275. }
  276. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  277. {
  278. rcu_read_unlock();
  279. }
  280. static int rcu_torture_completed(void)
  281. {
  282. return rcu_batches_completed();
  283. }
  284. static void
  285. rcu_torture_cb(struct rcu_head *p)
  286. {
  287. int i;
  288. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  289. if (fullstop != FULLSTOP_DONTSTOP) {
  290. /* Test is ending, just drop callbacks on the floor. */
  291. /* The next initialization will pick up the pieces. */
  292. return;
  293. }
  294. i = rp->rtort_pipe_count;
  295. if (i > RCU_TORTURE_PIPE_LEN)
  296. i = RCU_TORTURE_PIPE_LEN;
  297. atomic_inc(&rcu_torture_wcount[i]);
  298. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  299. rp->rtort_mbtest = 0;
  300. rcu_torture_free(rp);
  301. } else
  302. cur_ops->deferred_free(rp);
  303. }
  304. static int rcu_no_completed(void)
  305. {
  306. return 0;
  307. }
  308. static void rcu_torture_deferred_free(struct rcu_torture *p)
  309. {
  310. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  311. }
  312. static struct rcu_torture_ops rcu_ops = {
  313. .init = NULL,
  314. .cleanup = NULL,
  315. .readlock = rcu_torture_read_lock,
  316. .read_delay = rcu_read_delay,
  317. .readunlock = rcu_torture_read_unlock,
  318. .completed = rcu_torture_completed,
  319. .deferred_free = rcu_torture_deferred_free,
  320. .sync = synchronize_rcu,
  321. .cb_barrier = rcu_barrier,
  322. .fqs = rcu_force_quiescent_state,
  323. .stats = NULL,
  324. .irq_capable = 1,
  325. .name = "rcu"
  326. };
  327. static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
  328. {
  329. int i;
  330. struct rcu_torture *rp;
  331. struct rcu_torture *rp1;
  332. cur_ops->sync();
  333. list_add(&p->rtort_free, &rcu_torture_removed);
  334. list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
  335. i = rp->rtort_pipe_count;
  336. if (i > RCU_TORTURE_PIPE_LEN)
  337. i = RCU_TORTURE_PIPE_LEN;
  338. atomic_inc(&rcu_torture_wcount[i]);
  339. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  340. rp->rtort_mbtest = 0;
  341. list_del(&rp->rtort_free);
  342. rcu_torture_free(rp);
  343. }
  344. }
  345. }
  346. static void rcu_sync_torture_init(void)
  347. {
  348. INIT_LIST_HEAD(&rcu_torture_removed);
  349. }
  350. static struct rcu_torture_ops rcu_sync_ops = {
  351. .init = rcu_sync_torture_init,
  352. .cleanup = NULL,
  353. .readlock = rcu_torture_read_lock,
  354. .read_delay = rcu_read_delay,
  355. .readunlock = rcu_torture_read_unlock,
  356. .completed = rcu_torture_completed,
  357. .deferred_free = rcu_sync_torture_deferred_free,
  358. .sync = synchronize_rcu,
  359. .cb_barrier = NULL,
  360. .fqs = rcu_force_quiescent_state,
  361. .stats = NULL,
  362. .irq_capable = 1,
  363. .name = "rcu_sync"
  364. };
  365. static struct rcu_torture_ops rcu_expedited_ops = {
  366. .init = rcu_sync_torture_init,
  367. .cleanup = NULL,
  368. .readlock = rcu_torture_read_lock,
  369. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  370. .readunlock = rcu_torture_read_unlock,
  371. .completed = rcu_no_completed,
  372. .deferred_free = rcu_sync_torture_deferred_free,
  373. .sync = synchronize_rcu_expedited,
  374. .cb_barrier = NULL,
  375. .fqs = rcu_force_quiescent_state,
  376. .stats = NULL,
  377. .irq_capable = 1,
  378. .name = "rcu_expedited"
  379. };
  380. /*
  381. * Definitions for rcu_bh torture testing.
  382. */
  383. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  384. {
  385. rcu_read_lock_bh();
  386. return 0;
  387. }
  388. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  389. {
  390. rcu_read_unlock_bh();
  391. }
  392. static int rcu_bh_torture_completed(void)
  393. {
  394. return rcu_batches_completed_bh();
  395. }
  396. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  397. {
  398. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  399. }
  400. struct rcu_bh_torture_synchronize {
  401. struct rcu_head head;
  402. struct completion completion;
  403. };
  404. static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
  405. {
  406. struct rcu_bh_torture_synchronize *rcu;
  407. rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
  408. complete(&rcu->completion);
  409. }
  410. static void rcu_bh_torture_synchronize(void)
  411. {
  412. struct rcu_bh_torture_synchronize rcu;
  413. init_rcu_head_on_stack(&rcu.head);
  414. init_completion(&rcu.completion);
  415. call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
  416. wait_for_completion(&rcu.completion);
  417. destroy_rcu_head_on_stack(&rcu.head);
  418. }
  419. static struct rcu_torture_ops rcu_bh_ops = {
  420. .init = NULL,
  421. .cleanup = NULL,
  422. .readlock = rcu_bh_torture_read_lock,
  423. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  424. .readunlock = rcu_bh_torture_read_unlock,
  425. .completed = rcu_bh_torture_completed,
  426. .deferred_free = rcu_bh_torture_deferred_free,
  427. .sync = rcu_bh_torture_synchronize,
  428. .cb_barrier = rcu_barrier_bh,
  429. .fqs = rcu_bh_force_quiescent_state,
  430. .stats = NULL,
  431. .irq_capable = 1,
  432. .name = "rcu_bh"
  433. };
  434. static struct rcu_torture_ops rcu_bh_sync_ops = {
  435. .init = rcu_sync_torture_init,
  436. .cleanup = NULL,
  437. .readlock = rcu_bh_torture_read_lock,
  438. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  439. .readunlock = rcu_bh_torture_read_unlock,
  440. .completed = rcu_bh_torture_completed,
  441. .deferred_free = rcu_sync_torture_deferred_free,
  442. .sync = rcu_bh_torture_synchronize,
  443. .cb_barrier = NULL,
  444. .fqs = rcu_bh_force_quiescent_state,
  445. .stats = NULL,
  446. .irq_capable = 1,
  447. .name = "rcu_bh_sync"
  448. };
  449. /*
  450. * Definitions for srcu torture testing.
  451. */
  452. static struct srcu_struct srcu_ctl;
  453. static void srcu_torture_init(void)
  454. {
  455. init_srcu_struct(&srcu_ctl);
  456. rcu_sync_torture_init();
  457. }
  458. static void srcu_torture_cleanup(void)
  459. {
  460. synchronize_srcu(&srcu_ctl);
  461. cleanup_srcu_struct(&srcu_ctl);
  462. }
  463. static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
  464. {
  465. return srcu_read_lock(&srcu_ctl);
  466. }
  467. static void srcu_read_delay(struct rcu_random_state *rrsp)
  468. {
  469. long delay;
  470. const long uspertick = 1000000 / HZ;
  471. const long longdelay = 10;
  472. /* We want there to be long-running readers, but not all the time. */
  473. delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
  474. if (!delay)
  475. schedule_timeout_interruptible(longdelay);
  476. }
  477. static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
  478. {
  479. srcu_read_unlock(&srcu_ctl, idx);
  480. }
  481. static int srcu_torture_completed(void)
  482. {
  483. return srcu_batches_completed(&srcu_ctl);
  484. }
  485. static void srcu_torture_synchronize(void)
  486. {
  487. synchronize_srcu(&srcu_ctl);
  488. }
  489. static int srcu_torture_stats(char *page)
  490. {
  491. int cnt = 0;
  492. int cpu;
  493. int idx = srcu_ctl.completed & 0x1;
  494. cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
  495. torture_type, TORTURE_FLAG, idx);
  496. for_each_possible_cpu(cpu) {
  497. cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
  498. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
  499. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
  500. }
  501. cnt += sprintf(&page[cnt], "\n");
  502. return cnt;
  503. }
  504. static struct rcu_torture_ops srcu_ops = {
  505. .init = srcu_torture_init,
  506. .cleanup = srcu_torture_cleanup,
  507. .readlock = srcu_torture_read_lock,
  508. .read_delay = srcu_read_delay,
  509. .readunlock = srcu_torture_read_unlock,
  510. .completed = srcu_torture_completed,
  511. .deferred_free = rcu_sync_torture_deferred_free,
  512. .sync = srcu_torture_synchronize,
  513. .cb_barrier = NULL,
  514. .stats = srcu_torture_stats,
  515. .name = "srcu"
  516. };
  517. static void srcu_torture_synchronize_expedited(void)
  518. {
  519. synchronize_srcu_expedited(&srcu_ctl);
  520. }
  521. static struct rcu_torture_ops srcu_expedited_ops = {
  522. .init = srcu_torture_init,
  523. .cleanup = srcu_torture_cleanup,
  524. .readlock = srcu_torture_read_lock,
  525. .read_delay = srcu_read_delay,
  526. .readunlock = srcu_torture_read_unlock,
  527. .completed = srcu_torture_completed,
  528. .deferred_free = rcu_sync_torture_deferred_free,
  529. .sync = srcu_torture_synchronize_expedited,
  530. .cb_barrier = NULL,
  531. .stats = srcu_torture_stats,
  532. .name = "srcu_expedited"
  533. };
  534. /*
  535. * Definitions for sched torture testing.
  536. */
  537. static int sched_torture_read_lock(void)
  538. {
  539. preempt_disable();
  540. return 0;
  541. }
  542. static void sched_torture_read_unlock(int idx)
  543. {
  544. preempt_enable();
  545. }
  546. static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
  547. {
  548. call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
  549. }
  550. static void sched_torture_synchronize(void)
  551. {
  552. synchronize_sched();
  553. }
  554. static struct rcu_torture_ops sched_ops = {
  555. .init = rcu_sync_torture_init,
  556. .cleanup = NULL,
  557. .readlock = sched_torture_read_lock,
  558. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  559. .readunlock = sched_torture_read_unlock,
  560. .completed = rcu_no_completed,
  561. .deferred_free = rcu_sched_torture_deferred_free,
  562. .sync = sched_torture_synchronize,
  563. .cb_barrier = rcu_barrier_sched,
  564. .fqs = rcu_sched_force_quiescent_state,
  565. .stats = NULL,
  566. .irq_capable = 1,
  567. .name = "sched"
  568. };
  569. static struct rcu_torture_ops sched_sync_ops = {
  570. .init = rcu_sync_torture_init,
  571. .cleanup = NULL,
  572. .readlock = sched_torture_read_lock,
  573. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  574. .readunlock = sched_torture_read_unlock,
  575. .completed = rcu_no_completed,
  576. .deferred_free = rcu_sync_torture_deferred_free,
  577. .sync = sched_torture_synchronize,
  578. .cb_barrier = NULL,
  579. .fqs = rcu_sched_force_quiescent_state,
  580. .stats = NULL,
  581. .name = "sched_sync"
  582. };
  583. static struct rcu_torture_ops sched_expedited_ops = {
  584. .init = rcu_sync_torture_init,
  585. .cleanup = NULL,
  586. .readlock = sched_torture_read_lock,
  587. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  588. .readunlock = sched_torture_read_unlock,
  589. .completed = rcu_no_completed,
  590. .deferred_free = rcu_sync_torture_deferred_free,
  591. .sync = synchronize_sched_expedited,
  592. .cb_barrier = NULL,
  593. .fqs = rcu_sched_force_quiescent_state,
  594. .stats = NULL,
  595. .irq_capable = 1,
  596. .name = "sched_expedited"
  597. };
  598. /*
  599. * RCU torture force-quiescent-state kthread. Repeatedly induces
  600. * bursts of calls to force_quiescent_state(), increasing the probability
  601. * of occurrence of some important types of race conditions.
  602. */
  603. static int
  604. rcu_torture_fqs(void *arg)
  605. {
  606. unsigned long fqs_resume_time;
  607. int fqs_burst_remaining;
  608. VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
  609. do {
  610. fqs_resume_time = jiffies + fqs_stutter * HZ;
  611. while (jiffies - fqs_resume_time > LONG_MAX) {
  612. schedule_timeout_interruptible(1);
  613. }
  614. fqs_burst_remaining = fqs_duration;
  615. while (fqs_burst_remaining > 0) {
  616. cur_ops->fqs();
  617. udelay(fqs_holdoff);
  618. fqs_burst_remaining -= fqs_holdoff;
  619. }
  620. rcu_stutter_wait("rcu_torture_fqs");
  621. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  622. VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
  623. rcutorture_shutdown_absorb("rcu_torture_fqs");
  624. while (!kthread_should_stop())
  625. schedule_timeout_uninterruptible(1);
  626. return 0;
  627. }
  628. /*
  629. * RCU torture writer kthread. Repeatedly substitutes a new structure
  630. * for that pointed to by rcu_torture_current, freeing the old structure
  631. * after a series of grace periods (the "pipeline").
  632. */
  633. static int
  634. rcu_torture_writer(void *arg)
  635. {
  636. int i;
  637. long oldbatch = rcu_batches_completed();
  638. struct rcu_torture *rp;
  639. struct rcu_torture *old_rp;
  640. static DEFINE_RCU_RANDOM(rand);
  641. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  642. set_user_nice(current, 19);
  643. do {
  644. schedule_timeout_uninterruptible(1);
  645. rp = rcu_torture_alloc();
  646. if (rp == NULL)
  647. continue;
  648. rp->rtort_pipe_count = 0;
  649. udelay(rcu_random(&rand) & 0x3ff);
  650. old_rp = rcu_torture_current;
  651. rp->rtort_mbtest = 1;
  652. rcu_assign_pointer(rcu_torture_current, rp);
  653. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  654. if (old_rp) {
  655. i = old_rp->rtort_pipe_count;
  656. if (i > RCU_TORTURE_PIPE_LEN)
  657. i = RCU_TORTURE_PIPE_LEN;
  658. atomic_inc(&rcu_torture_wcount[i]);
  659. old_rp->rtort_pipe_count++;
  660. cur_ops->deferred_free(old_rp);
  661. }
  662. rcu_torture_current_version++;
  663. oldbatch = cur_ops->completed();
  664. rcu_stutter_wait("rcu_torture_writer");
  665. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  666. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  667. rcutorture_shutdown_absorb("rcu_torture_writer");
  668. while (!kthread_should_stop())
  669. schedule_timeout_uninterruptible(1);
  670. return 0;
  671. }
  672. /*
  673. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  674. * delay between calls.
  675. */
  676. static int
  677. rcu_torture_fakewriter(void *arg)
  678. {
  679. DEFINE_RCU_RANDOM(rand);
  680. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
  681. set_user_nice(current, 19);
  682. do {
  683. schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
  684. udelay(rcu_random(&rand) & 0x3ff);
  685. cur_ops->sync();
  686. rcu_stutter_wait("rcu_torture_fakewriter");
  687. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  688. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
  689. rcutorture_shutdown_absorb("rcu_torture_fakewriter");
  690. while (!kthread_should_stop())
  691. schedule_timeout_uninterruptible(1);
  692. return 0;
  693. }
  694. /*
  695. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  696. * incrementing the corresponding element of the pipeline array. The
  697. * counter in the element should never be greater than 1, otherwise, the
  698. * RCU implementation is broken.
  699. */
  700. static void rcu_torture_timer(unsigned long unused)
  701. {
  702. int idx;
  703. int completed;
  704. static DEFINE_RCU_RANDOM(rand);
  705. static DEFINE_SPINLOCK(rand_lock);
  706. struct rcu_torture *p;
  707. int pipe_count;
  708. idx = cur_ops->readlock();
  709. completed = cur_ops->completed();
  710. p = rcu_dereference_check(rcu_torture_current,
  711. rcu_read_lock_held() ||
  712. rcu_read_lock_bh_held() ||
  713. rcu_read_lock_sched_held() ||
  714. srcu_read_lock_held(&srcu_ctl));
  715. if (p == NULL) {
  716. /* Leave because rcu_torture_writer is not yet underway */
  717. cur_ops->readunlock(idx);
  718. return;
  719. }
  720. if (p->rtort_mbtest == 0)
  721. atomic_inc(&n_rcu_torture_mberror);
  722. spin_lock(&rand_lock);
  723. cur_ops->read_delay(&rand);
  724. n_rcu_torture_timers++;
  725. spin_unlock(&rand_lock);
  726. preempt_disable();
  727. pipe_count = p->rtort_pipe_count;
  728. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  729. /* Should not happen, but... */
  730. pipe_count = RCU_TORTURE_PIPE_LEN;
  731. }
  732. __this_cpu_inc(rcu_torture_count[pipe_count]);
  733. completed = cur_ops->completed() - completed;
  734. if (completed > RCU_TORTURE_PIPE_LEN) {
  735. /* Should not happen, but... */
  736. completed = RCU_TORTURE_PIPE_LEN;
  737. }
  738. __this_cpu_inc(rcu_torture_batch[completed]);
  739. preempt_enable();
  740. cur_ops->readunlock(idx);
  741. }
  742. /*
  743. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  744. * incrementing the corresponding element of the pipeline array. The
  745. * counter in the element should never be greater than 1, otherwise, the
  746. * RCU implementation is broken.
  747. */
  748. static int
  749. rcu_torture_reader(void *arg)
  750. {
  751. int completed;
  752. int idx;
  753. DEFINE_RCU_RANDOM(rand);
  754. struct rcu_torture *p;
  755. int pipe_count;
  756. struct timer_list t;
  757. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  758. set_user_nice(current, 19);
  759. if (irqreader && cur_ops->irq_capable)
  760. setup_timer_on_stack(&t, rcu_torture_timer, 0);
  761. do {
  762. if (irqreader && cur_ops->irq_capable) {
  763. if (!timer_pending(&t))
  764. mod_timer(&t, jiffies + 1);
  765. }
  766. idx = cur_ops->readlock();
  767. completed = cur_ops->completed();
  768. p = rcu_dereference_check(rcu_torture_current,
  769. rcu_read_lock_held() ||
  770. rcu_read_lock_bh_held() ||
  771. rcu_read_lock_sched_held() ||
  772. srcu_read_lock_held(&srcu_ctl));
  773. if (p == NULL) {
  774. /* Wait for rcu_torture_writer to get underway */
  775. cur_ops->readunlock(idx);
  776. schedule_timeout_interruptible(HZ);
  777. continue;
  778. }
  779. if (p->rtort_mbtest == 0)
  780. atomic_inc(&n_rcu_torture_mberror);
  781. cur_ops->read_delay(&rand);
  782. preempt_disable();
  783. pipe_count = p->rtort_pipe_count;
  784. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  785. /* Should not happen, but... */
  786. pipe_count = RCU_TORTURE_PIPE_LEN;
  787. }
  788. __this_cpu_inc(rcu_torture_count[pipe_count]);
  789. completed = cur_ops->completed() - completed;
  790. if (completed > RCU_TORTURE_PIPE_LEN) {
  791. /* Should not happen, but... */
  792. completed = RCU_TORTURE_PIPE_LEN;
  793. }
  794. __this_cpu_inc(rcu_torture_batch[completed]);
  795. preempt_enable();
  796. cur_ops->readunlock(idx);
  797. schedule();
  798. rcu_stutter_wait("rcu_torture_reader");
  799. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  800. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  801. rcutorture_shutdown_absorb("rcu_torture_reader");
  802. if (irqreader && cur_ops->irq_capable)
  803. del_timer_sync(&t);
  804. while (!kthread_should_stop())
  805. schedule_timeout_uninterruptible(1);
  806. return 0;
  807. }
  808. /*
  809. * Create an RCU-torture statistics message in the specified buffer.
  810. */
  811. static int
  812. rcu_torture_printk(char *page)
  813. {
  814. int cnt = 0;
  815. int cpu;
  816. int i;
  817. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  818. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  819. for_each_possible_cpu(cpu) {
  820. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  821. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  822. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  823. }
  824. }
  825. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  826. if (pipesummary[i] != 0)
  827. break;
  828. }
  829. cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
  830. cnt += sprintf(&page[cnt],
  831. "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
  832. "rtmbe: %d nt: %ld",
  833. rcu_torture_current,
  834. rcu_torture_current_version,
  835. list_empty(&rcu_torture_freelist),
  836. atomic_read(&n_rcu_torture_alloc),
  837. atomic_read(&n_rcu_torture_alloc_fail),
  838. atomic_read(&n_rcu_torture_free),
  839. atomic_read(&n_rcu_torture_mberror),
  840. n_rcu_torture_timers);
  841. if (atomic_read(&n_rcu_torture_mberror) != 0)
  842. cnt += sprintf(&page[cnt], " !!!");
  843. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  844. if (i > 1) {
  845. cnt += sprintf(&page[cnt], "!!! ");
  846. atomic_inc(&n_rcu_torture_error);
  847. WARN_ON_ONCE(1);
  848. }
  849. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  850. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  851. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  852. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  853. cnt += sprintf(&page[cnt], "Reader Batch: ");
  854. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  855. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  856. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  857. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  858. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  859. cnt += sprintf(&page[cnt], " %d",
  860. atomic_read(&rcu_torture_wcount[i]));
  861. }
  862. cnt += sprintf(&page[cnt], "\n");
  863. if (cur_ops->stats)
  864. cnt += cur_ops->stats(&page[cnt]);
  865. return cnt;
  866. }
  867. /*
  868. * Print torture statistics. Caller must ensure that there is only
  869. * one call to this function at a given time!!! This is normally
  870. * accomplished by relying on the module system to only have one copy
  871. * of the module loaded, and then by giving the rcu_torture_stats
  872. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  873. * thread is not running).
  874. */
  875. static void
  876. rcu_torture_stats_print(void)
  877. {
  878. int cnt;
  879. cnt = rcu_torture_printk(printk_buf);
  880. printk(KERN_ALERT "%s", printk_buf);
  881. }
  882. /*
  883. * Periodically prints torture statistics, if periodic statistics printing
  884. * was specified via the stat_interval module parameter.
  885. *
  886. * No need to worry about fullstop here, since this one doesn't reference
  887. * volatile state or register callbacks.
  888. */
  889. static int
  890. rcu_torture_stats(void *arg)
  891. {
  892. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  893. do {
  894. schedule_timeout_interruptible(stat_interval * HZ);
  895. rcu_torture_stats_print();
  896. rcutorture_shutdown_absorb("rcu_torture_stats");
  897. } while (!kthread_should_stop());
  898. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  899. return 0;
  900. }
  901. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  902. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  903. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  904. */
  905. static void rcu_torture_shuffle_tasks(void)
  906. {
  907. int i;
  908. cpumask_setall(shuffle_tmp_mask);
  909. get_online_cpus();
  910. /* No point in shuffling if there is only one online CPU (ex: UP) */
  911. if (num_online_cpus() == 1) {
  912. put_online_cpus();
  913. return;
  914. }
  915. if (rcu_idle_cpu != -1)
  916. cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
  917. set_cpus_allowed_ptr(current, shuffle_tmp_mask);
  918. if (reader_tasks) {
  919. for (i = 0; i < nrealreaders; i++)
  920. if (reader_tasks[i])
  921. set_cpus_allowed_ptr(reader_tasks[i],
  922. shuffle_tmp_mask);
  923. }
  924. if (fakewriter_tasks) {
  925. for (i = 0; i < nfakewriters; i++)
  926. if (fakewriter_tasks[i])
  927. set_cpus_allowed_ptr(fakewriter_tasks[i],
  928. shuffle_tmp_mask);
  929. }
  930. if (writer_task)
  931. set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
  932. if (stats_task)
  933. set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
  934. if (rcu_idle_cpu == -1)
  935. rcu_idle_cpu = num_online_cpus() - 1;
  936. else
  937. rcu_idle_cpu--;
  938. put_online_cpus();
  939. }
  940. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  941. * system to become idle at a time and cut off its timer ticks. This is meant
  942. * to test the support for such tickless idle CPU in RCU.
  943. */
  944. static int
  945. rcu_torture_shuffle(void *arg)
  946. {
  947. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  948. do {
  949. schedule_timeout_interruptible(shuffle_interval * HZ);
  950. rcu_torture_shuffle_tasks();
  951. rcutorture_shutdown_absorb("rcu_torture_shuffle");
  952. } while (!kthread_should_stop());
  953. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  954. return 0;
  955. }
  956. /* Cause the rcutorture test to "stutter", starting and stopping all
  957. * threads periodically.
  958. */
  959. static int
  960. rcu_torture_stutter(void *arg)
  961. {
  962. VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
  963. do {
  964. schedule_timeout_interruptible(stutter * HZ);
  965. stutter_pause_test = 1;
  966. if (!kthread_should_stop())
  967. schedule_timeout_interruptible(stutter * HZ);
  968. stutter_pause_test = 0;
  969. rcutorture_shutdown_absorb("rcu_torture_stutter");
  970. } while (!kthread_should_stop());
  971. VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
  972. return 0;
  973. }
  974. static inline void
  975. rcu_torture_print_module_parms(char *tag)
  976. {
  977. printk(KERN_ALERT "%s" TORTURE_FLAG
  978. "--- %s: nreaders=%d nfakewriters=%d "
  979. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  980. "shuffle_interval=%d stutter=%d irqreader=%d "
  981. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d\n",
  982. torture_type, tag, nrealreaders, nfakewriters,
  983. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  984. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter);
  985. }
  986. static struct notifier_block rcutorture_nb = {
  987. .notifier_call = rcutorture_shutdown_notify,
  988. };
  989. static void
  990. rcu_torture_cleanup(void)
  991. {
  992. int i;
  993. mutex_lock(&fullstop_mutex);
  994. if (fullstop == FULLSTOP_SHUTDOWN) {
  995. printk(KERN_WARNING /* but going down anyway, so... */
  996. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  997. mutex_unlock(&fullstop_mutex);
  998. schedule_timeout_uninterruptible(10);
  999. if (cur_ops->cb_barrier != NULL)
  1000. cur_ops->cb_barrier();
  1001. return;
  1002. }
  1003. fullstop = FULLSTOP_RMMOD;
  1004. mutex_unlock(&fullstop_mutex);
  1005. unregister_reboot_notifier(&rcutorture_nb);
  1006. if (stutter_task) {
  1007. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
  1008. kthread_stop(stutter_task);
  1009. }
  1010. stutter_task = NULL;
  1011. if (shuffler_task) {
  1012. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  1013. kthread_stop(shuffler_task);
  1014. free_cpumask_var(shuffle_tmp_mask);
  1015. }
  1016. shuffler_task = NULL;
  1017. if (writer_task) {
  1018. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  1019. kthread_stop(writer_task);
  1020. }
  1021. writer_task = NULL;
  1022. if (reader_tasks) {
  1023. for (i = 0; i < nrealreaders; i++) {
  1024. if (reader_tasks[i]) {
  1025. VERBOSE_PRINTK_STRING(
  1026. "Stopping rcu_torture_reader task");
  1027. kthread_stop(reader_tasks[i]);
  1028. }
  1029. reader_tasks[i] = NULL;
  1030. }
  1031. kfree(reader_tasks);
  1032. reader_tasks = NULL;
  1033. }
  1034. rcu_torture_current = NULL;
  1035. if (fakewriter_tasks) {
  1036. for (i = 0; i < nfakewriters; i++) {
  1037. if (fakewriter_tasks[i]) {
  1038. VERBOSE_PRINTK_STRING(
  1039. "Stopping rcu_torture_fakewriter task");
  1040. kthread_stop(fakewriter_tasks[i]);
  1041. }
  1042. fakewriter_tasks[i] = NULL;
  1043. }
  1044. kfree(fakewriter_tasks);
  1045. fakewriter_tasks = NULL;
  1046. }
  1047. if (stats_task) {
  1048. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  1049. kthread_stop(stats_task);
  1050. }
  1051. stats_task = NULL;
  1052. if (fqs_task) {
  1053. VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
  1054. kthread_stop(fqs_task);
  1055. }
  1056. fqs_task = NULL;
  1057. /* Wait for all RCU callbacks to fire. */
  1058. if (cur_ops->cb_barrier != NULL)
  1059. cur_ops->cb_barrier();
  1060. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  1061. if (cur_ops->cleanup)
  1062. cur_ops->cleanup();
  1063. if (atomic_read(&n_rcu_torture_error))
  1064. rcu_torture_print_module_parms("End of test: FAILURE");
  1065. else
  1066. rcu_torture_print_module_parms("End of test: SUCCESS");
  1067. }
  1068. static int __init
  1069. rcu_torture_init(void)
  1070. {
  1071. int i;
  1072. int cpu;
  1073. int firsterr = 0;
  1074. static struct rcu_torture_ops *torture_ops[] =
  1075. { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
  1076. &rcu_bh_ops, &rcu_bh_sync_ops,
  1077. &srcu_ops, &srcu_expedited_ops,
  1078. &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
  1079. mutex_lock(&fullstop_mutex);
  1080. /* Process args and tell the world that the torturer is on the job. */
  1081. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1082. cur_ops = torture_ops[i];
  1083. if (strcmp(torture_type, cur_ops->name) == 0)
  1084. break;
  1085. }
  1086. if (i == ARRAY_SIZE(torture_ops)) {
  1087. printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
  1088. torture_type);
  1089. printk(KERN_ALERT "rcu-torture types:");
  1090. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1091. printk(KERN_ALERT " %s", torture_ops[i]->name);
  1092. printk(KERN_ALERT "\n");
  1093. mutex_unlock(&fullstop_mutex);
  1094. return -EINVAL;
  1095. }
  1096. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  1097. printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
  1098. "fqs_duration, fqs disabled.\n");
  1099. fqs_duration = 0;
  1100. }
  1101. if (cur_ops->init)
  1102. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  1103. if (nreaders >= 0)
  1104. nrealreaders = nreaders;
  1105. else
  1106. nrealreaders = 2 * num_online_cpus();
  1107. rcu_torture_print_module_parms("Start of test");
  1108. fullstop = FULLSTOP_DONTSTOP;
  1109. /* Set up the freelist. */
  1110. INIT_LIST_HEAD(&rcu_torture_freelist);
  1111. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  1112. rcu_tortures[i].rtort_mbtest = 0;
  1113. list_add_tail(&rcu_tortures[i].rtort_free,
  1114. &rcu_torture_freelist);
  1115. }
  1116. /* Initialize the statistics so that each run gets its own numbers. */
  1117. rcu_torture_current = NULL;
  1118. rcu_torture_current_version = 0;
  1119. atomic_set(&n_rcu_torture_alloc, 0);
  1120. atomic_set(&n_rcu_torture_alloc_fail, 0);
  1121. atomic_set(&n_rcu_torture_free, 0);
  1122. atomic_set(&n_rcu_torture_mberror, 0);
  1123. atomic_set(&n_rcu_torture_error, 0);
  1124. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1125. atomic_set(&rcu_torture_wcount[i], 0);
  1126. for_each_possible_cpu(cpu) {
  1127. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1128. per_cpu(rcu_torture_count, cpu)[i] = 0;
  1129. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  1130. }
  1131. }
  1132. /* Start up the kthreads. */
  1133. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  1134. writer_task = kthread_run(rcu_torture_writer, NULL,
  1135. "rcu_torture_writer");
  1136. if (IS_ERR(writer_task)) {
  1137. firsterr = PTR_ERR(writer_task);
  1138. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  1139. writer_task = NULL;
  1140. goto unwind;
  1141. }
  1142. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  1143. GFP_KERNEL);
  1144. if (fakewriter_tasks == NULL) {
  1145. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1146. firsterr = -ENOMEM;
  1147. goto unwind;
  1148. }
  1149. for (i = 0; i < nfakewriters; i++) {
  1150. VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
  1151. fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
  1152. "rcu_torture_fakewriter");
  1153. if (IS_ERR(fakewriter_tasks[i])) {
  1154. firsterr = PTR_ERR(fakewriter_tasks[i]);
  1155. VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
  1156. fakewriter_tasks[i] = NULL;
  1157. goto unwind;
  1158. }
  1159. }
  1160. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  1161. GFP_KERNEL);
  1162. if (reader_tasks == NULL) {
  1163. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1164. firsterr = -ENOMEM;
  1165. goto unwind;
  1166. }
  1167. for (i = 0; i < nrealreaders; i++) {
  1168. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  1169. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  1170. "rcu_torture_reader");
  1171. if (IS_ERR(reader_tasks[i])) {
  1172. firsterr = PTR_ERR(reader_tasks[i]);
  1173. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  1174. reader_tasks[i] = NULL;
  1175. goto unwind;
  1176. }
  1177. }
  1178. if (stat_interval > 0) {
  1179. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  1180. stats_task = kthread_run(rcu_torture_stats, NULL,
  1181. "rcu_torture_stats");
  1182. if (IS_ERR(stats_task)) {
  1183. firsterr = PTR_ERR(stats_task);
  1184. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  1185. stats_task = NULL;
  1186. goto unwind;
  1187. }
  1188. }
  1189. if (test_no_idle_hz) {
  1190. rcu_idle_cpu = num_online_cpus() - 1;
  1191. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  1192. firsterr = -ENOMEM;
  1193. VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
  1194. goto unwind;
  1195. }
  1196. /* Create the shuffler thread */
  1197. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  1198. "rcu_torture_shuffle");
  1199. if (IS_ERR(shuffler_task)) {
  1200. free_cpumask_var(shuffle_tmp_mask);
  1201. firsterr = PTR_ERR(shuffler_task);
  1202. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  1203. shuffler_task = NULL;
  1204. goto unwind;
  1205. }
  1206. }
  1207. if (stutter < 0)
  1208. stutter = 0;
  1209. if (stutter) {
  1210. /* Create the stutter thread */
  1211. stutter_task = kthread_run(rcu_torture_stutter, NULL,
  1212. "rcu_torture_stutter");
  1213. if (IS_ERR(stutter_task)) {
  1214. firsterr = PTR_ERR(stutter_task);
  1215. VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
  1216. stutter_task = NULL;
  1217. goto unwind;
  1218. }
  1219. }
  1220. if (fqs_duration < 0)
  1221. fqs_duration = 0;
  1222. if (fqs_duration) {
  1223. /* Create the stutter thread */
  1224. fqs_task = kthread_run(rcu_torture_fqs, NULL,
  1225. "rcu_torture_fqs");
  1226. if (IS_ERR(fqs_task)) {
  1227. firsterr = PTR_ERR(fqs_task);
  1228. VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
  1229. fqs_task = NULL;
  1230. goto unwind;
  1231. }
  1232. }
  1233. register_reboot_notifier(&rcutorture_nb);
  1234. mutex_unlock(&fullstop_mutex);
  1235. return 0;
  1236. unwind:
  1237. mutex_unlock(&fullstop_mutex);
  1238. rcu_torture_cleanup();
  1239. return firsterr;
  1240. }
  1241. module_init(rcu_torture_init);
  1242. module_exit(rcu_torture_cleanup);