rcutorture.c 38 KB

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