rcutorture.c 39 KB

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