rcutorture.c 39 KB

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