rcutorture.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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. #include <linux/sched.h>
  51. MODULE_LICENSE("GPL");
  52. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
  53. "Josh Triplett <josh@freedesktop.org>");
  54. static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
  55. static int nfakewriters = 4; /* # fake writer threads */
  56. static int stat_interval; /* Interval between stats, in seconds. */
  57. /* Defaults to "only at end of test". */
  58. static int verbose; /* Print more debug info. */
  59. static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
  60. static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
  61. static int stutter = 5; /* Start/stop testing interval (in sec) */
  62. static int irqreader = 1; /* RCU readers from irq (timers). */
  63. static int fqs_duration = 0; /* Duration of bursts (us), 0 to disable. */
  64. static int fqs_holdoff = 0; /* Hold time within burst (us). */
  65. static int fqs_stutter = 3; /* Wait time between bursts (s). */
  66. static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
  67. static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
  68. static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
  69. static char *torture_type = "rcu"; /* What RCU implementation to torture. */
  70. module_param(nreaders, int, 0444);
  71. MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  72. module_param(nfakewriters, int, 0444);
  73. MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
  74. module_param(stat_interval, int, 0444);
  75. MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  76. module_param(verbose, bool, 0444);
  77. MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  78. module_param(test_no_idle_hz, bool, 0444);
  79. MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
  80. module_param(shuffle_interval, int, 0444);
  81. MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
  82. module_param(stutter, int, 0444);
  83. MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
  84. module_param(irqreader, int, 0444);
  85. MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
  86. module_param(fqs_duration, int, 0444);
  87. MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
  88. module_param(fqs_holdoff, int, 0444);
  89. MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
  90. module_param(fqs_stutter, int, 0444);
  91. MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
  92. module_param(test_boost, int, 0444);
  93. MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
  94. module_param(test_boost_interval, int, 0444);
  95. MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
  96. module_param(test_boost_duration, int, 0444);
  97. MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
  98. module_param(torture_type, charp, 0444);
  99. MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
  100. #define TORTURE_FLAG "-torture:"
  101. #define PRINTK_STRING(s) \
  102. do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  103. #define VERBOSE_PRINTK_STRING(s) \
  104. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  105. #define VERBOSE_PRINTK_ERRSTRING(s) \
  106. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
  107. static char printk_buf[4096];
  108. static int nrealreaders;
  109. static struct task_struct *writer_task;
  110. static struct task_struct **fakewriter_tasks;
  111. static struct task_struct **reader_tasks;
  112. static struct task_struct *stats_task;
  113. static struct task_struct *shuffler_task;
  114. static struct task_struct *stutter_task;
  115. static struct task_struct *fqs_task;
  116. static struct task_struct *boost_tasks[NR_CPUS];
  117. #define RCU_TORTURE_PIPE_LEN 10
  118. struct rcu_torture {
  119. struct rcu_head rtort_rcu;
  120. int rtort_pipe_count;
  121. struct list_head rtort_free;
  122. int rtort_mbtest;
  123. };
  124. static LIST_HEAD(rcu_torture_freelist);
  125. static struct rcu_torture __rcu *rcu_torture_current;
  126. static long rcu_torture_current_version;
  127. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  128. static DEFINE_SPINLOCK(rcu_torture_lock);
  129. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
  130. { 0 };
  131. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
  132. { 0 };
  133. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  134. static atomic_t n_rcu_torture_alloc;
  135. static atomic_t n_rcu_torture_alloc_fail;
  136. static atomic_t n_rcu_torture_free;
  137. static atomic_t n_rcu_torture_mberror;
  138. static atomic_t n_rcu_torture_error;
  139. static long n_rcu_torture_boost_ktrerror;
  140. static long n_rcu_torture_boost_rterror;
  141. static long n_rcu_torture_boost_allocerror;
  142. static long n_rcu_torture_boost_afferror;
  143. static long n_rcu_torture_boost_failure;
  144. static long n_rcu_torture_boosts;
  145. static long n_rcu_torture_timers;
  146. static struct list_head rcu_torture_removed;
  147. static cpumask_var_t shuffle_tmp_mask;
  148. static int stutter_pause_test;
  149. #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
  150. #define RCUTORTURE_RUNNABLE_INIT 1
  151. #else
  152. #define RCUTORTURE_RUNNABLE_INIT 0
  153. #endif
  154. int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
  155. #ifdef CONFIG_RCU_BOOST
  156. #define rcu_can_boost() 1
  157. #else /* #ifdef CONFIG_RCU_BOOST */
  158. #define rcu_can_boost() 0
  159. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  160. static unsigned long boost_starttime; /* jiffies of next boost test start. */
  161. DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
  162. /* and boost task create/destroy. */
  163. /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
  164. #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
  165. #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
  166. #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
  167. static int fullstop = FULLSTOP_RMMOD;
  168. /*
  169. * Protect fullstop transitions and spawning of kthreads.
  170. */
  171. static DEFINE_MUTEX(fullstop_mutex);
  172. /*
  173. * Detect and respond to a system shutdown.
  174. */
  175. static int
  176. rcutorture_shutdown_notify(struct notifier_block *unused1,
  177. unsigned long unused2, void *unused3)
  178. {
  179. mutex_lock(&fullstop_mutex);
  180. if (fullstop == FULLSTOP_DONTSTOP)
  181. fullstop = FULLSTOP_SHUTDOWN;
  182. else
  183. printk(KERN_WARNING /* but going down anyway, so... */
  184. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  185. mutex_unlock(&fullstop_mutex);
  186. return NOTIFY_DONE;
  187. }
  188. /*
  189. * Absorb kthreads into a kernel function that won't return, so that
  190. * they won't ever access module text or data again.
  191. */
  192. static void rcutorture_shutdown_absorb(char *title)
  193. {
  194. if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  195. printk(KERN_NOTICE
  196. "rcutorture thread %s parking due to system shutdown\n",
  197. title);
  198. schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  199. }
  200. }
  201. /*
  202. * Allocate an element from the rcu_tortures pool.
  203. */
  204. static struct rcu_torture *
  205. rcu_torture_alloc(void)
  206. {
  207. struct list_head *p;
  208. spin_lock_bh(&rcu_torture_lock);
  209. if (list_empty(&rcu_torture_freelist)) {
  210. atomic_inc(&n_rcu_torture_alloc_fail);
  211. spin_unlock_bh(&rcu_torture_lock);
  212. return NULL;
  213. }
  214. atomic_inc(&n_rcu_torture_alloc);
  215. p = rcu_torture_freelist.next;
  216. list_del_init(p);
  217. spin_unlock_bh(&rcu_torture_lock);
  218. return container_of(p, struct rcu_torture, rtort_free);
  219. }
  220. /*
  221. * Free an element to the rcu_tortures pool.
  222. */
  223. static void
  224. rcu_torture_free(struct rcu_torture *p)
  225. {
  226. atomic_inc(&n_rcu_torture_free);
  227. spin_lock_bh(&rcu_torture_lock);
  228. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  229. spin_unlock_bh(&rcu_torture_lock);
  230. }
  231. struct rcu_random_state {
  232. unsigned long rrs_state;
  233. long rrs_count;
  234. };
  235. #define RCU_RANDOM_MULT 39916801 /* prime */
  236. #define RCU_RANDOM_ADD 479001701 /* prime */
  237. #define RCU_RANDOM_REFRESH 10000
  238. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  239. /*
  240. * Crude but fast random-number generator. Uses a linear congruential
  241. * generator, with occasional help from cpu_clock().
  242. */
  243. static unsigned long
  244. rcu_random(struct rcu_random_state *rrsp)
  245. {
  246. if (--rrsp->rrs_count < 0) {
  247. rrsp->rrs_state += (unsigned long)local_clock();
  248. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  249. }
  250. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  251. return swahw32(rrsp->rrs_state);
  252. }
  253. static void
  254. rcu_stutter_wait(char *title)
  255. {
  256. while (stutter_pause_test || !rcutorture_runnable) {
  257. if (rcutorture_runnable)
  258. schedule_timeout_interruptible(1);
  259. else
  260. schedule_timeout_interruptible(round_jiffies_relative(HZ));
  261. rcutorture_shutdown_absorb(title);
  262. }
  263. }
  264. /*
  265. * Operations vector for selecting different types of tests.
  266. */
  267. struct rcu_torture_ops {
  268. void (*init)(void);
  269. void (*cleanup)(void);
  270. int (*readlock)(void);
  271. void (*read_delay)(struct rcu_random_state *rrsp);
  272. void (*readunlock)(int idx);
  273. int (*completed)(void);
  274. void (*deferred_free)(struct rcu_torture *p);
  275. void (*sync)(void);
  276. void (*cb_barrier)(void);
  277. void (*fqs)(void);
  278. int (*stats)(char *page);
  279. int irq_capable;
  280. int can_boost;
  281. char *name;
  282. };
  283. static struct rcu_torture_ops *cur_ops;
  284. /*
  285. * Definitions for rcu torture testing.
  286. */
  287. static int rcu_torture_read_lock(void) __acquires(RCU)
  288. {
  289. rcu_read_lock();
  290. return 0;
  291. }
  292. static void rcu_read_delay(struct rcu_random_state *rrsp)
  293. {
  294. const unsigned long shortdelay_us = 200;
  295. const unsigned long longdelay_ms = 50;
  296. /* We want a short delay sometimes to make a reader delay the grace
  297. * period, and we want a long delay occasionally to trigger
  298. * force_quiescent_state. */
  299. if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
  300. mdelay(longdelay_ms);
  301. if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
  302. udelay(shortdelay_us);
  303. #ifdef CONFIG_PREEMPT
  304. if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
  305. preempt_schedule(); /* No QS if preempt_disable() in effect */
  306. #endif
  307. }
  308. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  309. {
  310. rcu_read_unlock();
  311. }
  312. static int rcu_torture_completed(void)
  313. {
  314. return rcu_batches_completed();
  315. }
  316. static void
  317. rcu_torture_cb(struct rcu_head *p)
  318. {
  319. int i;
  320. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  321. if (fullstop != FULLSTOP_DONTSTOP) {
  322. /* Test is ending, just drop callbacks on the floor. */
  323. /* The next initialization will pick up the pieces. */
  324. return;
  325. }
  326. i = rp->rtort_pipe_count;
  327. if (i > RCU_TORTURE_PIPE_LEN)
  328. i = RCU_TORTURE_PIPE_LEN;
  329. atomic_inc(&rcu_torture_wcount[i]);
  330. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  331. rp->rtort_mbtest = 0;
  332. rcu_torture_free(rp);
  333. } else
  334. cur_ops->deferred_free(rp);
  335. }
  336. static int rcu_no_completed(void)
  337. {
  338. return 0;
  339. }
  340. static void rcu_torture_deferred_free(struct rcu_torture *p)
  341. {
  342. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  343. }
  344. static struct rcu_torture_ops rcu_ops = {
  345. .init = NULL,
  346. .cleanup = NULL,
  347. .readlock = rcu_torture_read_lock,
  348. .read_delay = rcu_read_delay,
  349. .readunlock = rcu_torture_read_unlock,
  350. .completed = rcu_torture_completed,
  351. .deferred_free = rcu_torture_deferred_free,
  352. .sync = synchronize_rcu,
  353. .cb_barrier = rcu_barrier,
  354. .fqs = rcu_force_quiescent_state,
  355. .stats = NULL,
  356. .irq_capable = 1,
  357. .can_boost = rcu_can_boost(),
  358. .name = "rcu"
  359. };
  360. static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
  361. {
  362. int i;
  363. struct rcu_torture *rp;
  364. struct rcu_torture *rp1;
  365. cur_ops->sync();
  366. list_add(&p->rtort_free, &rcu_torture_removed);
  367. list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
  368. i = rp->rtort_pipe_count;
  369. if (i > RCU_TORTURE_PIPE_LEN)
  370. i = RCU_TORTURE_PIPE_LEN;
  371. atomic_inc(&rcu_torture_wcount[i]);
  372. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  373. rp->rtort_mbtest = 0;
  374. list_del(&rp->rtort_free);
  375. rcu_torture_free(rp);
  376. }
  377. }
  378. }
  379. static void rcu_sync_torture_init(void)
  380. {
  381. INIT_LIST_HEAD(&rcu_torture_removed);
  382. }
  383. static struct rcu_torture_ops rcu_sync_ops = {
  384. .init = rcu_sync_torture_init,
  385. .cleanup = NULL,
  386. .readlock = rcu_torture_read_lock,
  387. .read_delay = rcu_read_delay,
  388. .readunlock = rcu_torture_read_unlock,
  389. .completed = rcu_torture_completed,
  390. .deferred_free = rcu_sync_torture_deferred_free,
  391. .sync = synchronize_rcu,
  392. .cb_barrier = NULL,
  393. .fqs = rcu_force_quiescent_state,
  394. .stats = NULL,
  395. .irq_capable = 1,
  396. .can_boost = rcu_can_boost(),
  397. .name = "rcu_sync"
  398. };
  399. static struct rcu_torture_ops rcu_expedited_ops = {
  400. .init = rcu_sync_torture_init,
  401. .cleanup = NULL,
  402. .readlock = rcu_torture_read_lock,
  403. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  404. .readunlock = rcu_torture_read_unlock,
  405. .completed = rcu_no_completed,
  406. .deferred_free = rcu_sync_torture_deferred_free,
  407. .sync = synchronize_rcu_expedited,
  408. .cb_barrier = NULL,
  409. .fqs = rcu_force_quiescent_state,
  410. .stats = NULL,
  411. .irq_capable = 1,
  412. .can_boost = rcu_can_boost(),
  413. .name = "rcu_expedited"
  414. };
  415. /*
  416. * Definitions for rcu_bh torture testing.
  417. */
  418. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  419. {
  420. rcu_read_lock_bh();
  421. return 0;
  422. }
  423. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  424. {
  425. rcu_read_unlock_bh();
  426. }
  427. static int rcu_bh_torture_completed(void)
  428. {
  429. return rcu_batches_completed_bh();
  430. }
  431. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  432. {
  433. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  434. }
  435. struct rcu_bh_torture_synchronize {
  436. struct rcu_head head;
  437. struct completion completion;
  438. };
  439. static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
  440. {
  441. struct rcu_bh_torture_synchronize *rcu;
  442. rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
  443. complete(&rcu->completion);
  444. }
  445. static void rcu_bh_torture_synchronize(void)
  446. {
  447. struct rcu_bh_torture_synchronize rcu;
  448. init_rcu_head_on_stack(&rcu.head);
  449. init_completion(&rcu.completion);
  450. call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
  451. wait_for_completion(&rcu.completion);
  452. destroy_rcu_head_on_stack(&rcu.head);
  453. }
  454. static struct rcu_torture_ops rcu_bh_ops = {
  455. .init = NULL,
  456. .cleanup = NULL,
  457. .readlock = rcu_bh_torture_read_lock,
  458. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  459. .readunlock = rcu_bh_torture_read_unlock,
  460. .completed = rcu_bh_torture_completed,
  461. .deferred_free = rcu_bh_torture_deferred_free,
  462. .sync = rcu_bh_torture_synchronize,
  463. .cb_barrier = rcu_barrier_bh,
  464. .fqs = rcu_bh_force_quiescent_state,
  465. .stats = NULL,
  466. .irq_capable = 1,
  467. .name = "rcu_bh"
  468. };
  469. static struct rcu_torture_ops rcu_bh_sync_ops = {
  470. .init = rcu_sync_torture_init,
  471. .cleanup = NULL,
  472. .readlock = rcu_bh_torture_read_lock,
  473. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  474. .readunlock = rcu_bh_torture_read_unlock,
  475. .completed = rcu_bh_torture_completed,
  476. .deferred_free = rcu_sync_torture_deferred_free,
  477. .sync = rcu_bh_torture_synchronize,
  478. .cb_barrier = NULL,
  479. .fqs = rcu_bh_force_quiescent_state,
  480. .stats = NULL,
  481. .irq_capable = 1,
  482. .name = "rcu_bh_sync"
  483. };
  484. /*
  485. * Definitions for srcu torture testing.
  486. */
  487. static struct srcu_struct srcu_ctl;
  488. static void srcu_torture_init(void)
  489. {
  490. init_srcu_struct(&srcu_ctl);
  491. rcu_sync_torture_init();
  492. }
  493. static void srcu_torture_cleanup(void)
  494. {
  495. synchronize_srcu(&srcu_ctl);
  496. cleanup_srcu_struct(&srcu_ctl);
  497. }
  498. static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
  499. {
  500. return srcu_read_lock(&srcu_ctl);
  501. }
  502. static void srcu_read_delay(struct rcu_random_state *rrsp)
  503. {
  504. long delay;
  505. const long uspertick = 1000000 / HZ;
  506. const long longdelay = 10;
  507. /* We want there to be long-running readers, but not all the time. */
  508. delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
  509. if (!delay)
  510. schedule_timeout_interruptible(longdelay);
  511. else
  512. rcu_read_delay(rrsp);
  513. }
  514. static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
  515. {
  516. srcu_read_unlock(&srcu_ctl, idx);
  517. }
  518. static int srcu_torture_completed(void)
  519. {
  520. return srcu_batches_completed(&srcu_ctl);
  521. }
  522. static void srcu_torture_synchronize(void)
  523. {
  524. synchronize_srcu(&srcu_ctl);
  525. }
  526. static int srcu_torture_stats(char *page)
  527. {
  528. int cnt = 0;
  529. int cpu;
  530. int idx = srcu_ctl.completed & 0x1;
  531. cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
  532. torture_type, TORTURE_FLAG, idx);
  533. for_each_possible_cpu(cpu) {
  534. cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
  535. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
  536. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
  537. }
  538. cnt += sprintf(&page[cnt], "\n");
  539. return cnt;
  540. }
  541. static struct rcu_torture_ops srcu_ops = {
  542. .init = srcu_torture_init,
  543. .cleanup = srcu_torture_cleanup,
  544. .readlock = srcu_torture_read_lock,
  545. .read_delay = srcu_read_delay,
  546. .readunlock = srcu_torture_read_unlock,
  547. .completed = srcu_torture_completed,
  548. .deferred_free = rcu_sync_torture_deferred_free,
  549. .sync = srcu_torture_synchronize,
  550. .cb_barrier = NULL,
  551. .stats = srcu_torture_stats,
  552. .name = "srcu"
  553. };
  554. static void srcu_torture_synchronize_expedited(void)
  555. {
  556. synchronize_srcu_expedited(&srcu_ctl);
  557. }
  558. static struct rcu_torture_ops srcu_expedited_ops = {
  559. .init = srcu_torture_init,
  560. .cleanup = srcu_torture_cleanup,
  561. .readlock = srcu_torture_read_lock,
  562. .read_delay = srcu_read_delay,
  563. .readunlock = srcu_torture_read_unlock,
  564. .completed = srcu_torture_completed,
  565. .deferred_free = rcu_sync_torture_deferred_free,
  566. .sync = srcu_torture_synchronize_expedited,
  567. .cb_barrier = NULL,
  568. .stats = srcu_torture_stats,
  569. .name = "srcu_expedited"
  570. };
  571. /*
  572. * Definitions for sched torture testing.
  573. */
  574. static int sched_torture_read_lock(void)
  575. {
  576. preempt_disable();
  577. return 0;
  578. }
  579. static void sched_torture_read_unlock(int idx)
  580. {
  581. preempt_enable();
  582. }
  583. static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
  584. {
  585. call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
  586. }
  587. static void sched_torture_synchronize(void)
  588. {
  589. synchronize_sched();
  590. }
  591. static struct rcu_torture_ops sched_ops = {
  592. .init = rcu_sync_torture_init,
  593. .cleanup = NULL,
  594. .readlock = sched_torture_read_lock,
  595. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  596. .readunlock = sched_torture_read_unlock,
  597. .completed = rcu_no_completed,
  598. .deferred_free = rcu_sched_torture_deferred_free,
  599. .sync = sched_torture_synchronize,
  600. .cb_barrier = rcu_barrier_sched,
  601. .fqs = rcu_sched_force_quiescent_state,
  602. .stats = NULL,
  603. .irq_capable = 1,
  604. .name = "sched"
  605. };
  606. static struct rcu_torture_ops sched_sync_ops = {
  607. .init = rcu_sync_torture_init,
  608. .cleanup = NULL,
  609. .readlock = sched_torture_read_lock,
  610. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  611. .readunlock = sched_torture_read_unlock,
  612. .completed = rcu_no_completed,
  613. .deferred_free = rcu_sync_torture_deferred_free,
  614. .sync = sched_torture_synchronize,
  615. .cb_barrier = NULL,
  616. .fqs = rcu_sched_force_quiescent_state,
  617. .stats = NULL,
  618. .name = "sched_sync"
  619. };
  620. static struct rcu_torture_ops sched_expedited_ops = {
  621. .init = rcu_sync_torture_init,
  622. .cleanup = NULL,
  623. .readlock = sched_torture_read_lock,
  624. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  625. .readunlock = sched_torture_read_unlock,
  626. .completed = rcu_no_completed,
  627. .deferred_free = rcu_sync_torture_deferred_free,
  628. .sync = synchronize_sched_expedited,
  629. .cb_barrier = NULL,
  630. .fqs = rcu_sched_force_quiescent_state,
  631. .stats = NULL,
  632. .irq_capable = 1,
  633. .name = "sched_expedited"
  634. };
  635. /*
  636. * RCU torture priority-boost testing. Runs one real-time thread per
  637. * CPU for moderate bursts, repeatedly registering RCU callbacks and
  638. * spinning waiting for them to be invoked. If a given callback takes
  639. * too long to be invoked, we assume that priority inversion has occurred.
  640. */
  641. struct rcu_boost_inflight {
  642. struct rcu_head rcu;
  643. int inflight;
  644. };
  645. static void rcu_torture_boost_cb(struct rcu_head *head)
  646. {
  647. struct rcu_boost_inflight *rbip =
  648. container_of(head, struct rcu_boost_inflight, rcu);
  649. smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
  650. rbip->inflight = 0;
  651. }
  652. static int rcu_torture_boost(void *arg)
  653. {
  654. unsigned long call_rcu_time;
  655. unsigned long endtime;
  656. unsigned long oldstarttime;
  657. struct rcu_boost_inflight rbi = { .inflight = 0 };
  658. struct sched_param sp;
  659. VERBOSE_PRINTK_STRING("rcu_torture_boost started");
  660. /* Set real-time priority. */
  661. sp.sched_priority = 1;
  662. if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
  663. VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
  664. n_rcu_torture_boost_rterror++;
  665. }
  666. /* Each pass through the following loop does one boost-test cycle. */
  667. do {
  668. /* Wait for the next test interval. */
  669. oldstarttime = boost_starttime;
  670. while (jiffies - oldstarttime > ULONG_MAX / 2) {
  671. schedule_timeout_uninterruptible(1);
  672. rcu_stutter_wait("rcu_torture_boost");
  673. if (kthread_should_stop() ||
  674. fullstop != FULLSTOP_DONTSTOP)
  675. goto checkwait;
  676. }
  677. /* Do one boost-test interval. */
  678. endtime = oldstarttime + test_boost_duration * HZ;
  679. call_rcu_time = jiffies;
  680. while (jiffies - endtime > ULONG_MAX / 2) {
  681. /* If we don't have a callback in flight, post one. */
  682. if (!rbi.inflight) {
  683. smp_mb(); /* RCU core before ->inflight = 1. */
  684. rbi.inflight = 1;
  685. call_rcu(&rbi.rcu, rcu_torture_boost_cb);
  686. if (jiffies - call_rcu_time >
  687. test_boost_duration * HZ - HZ / 2) {
  688. VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
  689. n_rcu_torture_boost_failure++;
  690. }
  691. call_rcu_time = jiffies;
  692. }
  693. cond_resched();
  694. rcu_stutter_wait("rcu_torture_boost");
  695. if (kthread_should_stop() ||
  696. fullstop != FULLSTOP_DONTSTOP)
  697. goto checkwait;
  698. }
  699. /*
  700. * Set the start time of the next test interval.
  701. * Yes, this is vulnerable to long delays, but such
  702. * delays simply cause a false negative for the next
  703. * interval. Besides, we are running at RT priority,
  704. * so delays should be relatively rare.
  705. */
  706. while (oldstarttime == boost_starttime) {
  707. if (mutex_trylock(&boost_mutex)) {
  708. boost_starttime = jiffies +
  709. test_boost_interval * HZ;
  710. n_rcu_torture_boosts++;
  711. mutex_unlock(&boost_mutex);
  712. break;
  713. }
  714. schedule_timeout_uninterruptible(1);
  715. }
  716. /* Go do the stutter. */
  717. checkwait: rcu_stutter_wait("rcu_torture_boost");
  718. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  719. /* Clean up and exit. */
  720. VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
  721. rcutorture_shutdown_absorb("rcu_torture_boost");
  722. while (!kthread_should_stop() || rbi.inflight)
  723. schedule_timeout_uninterruptible(1);
  724. smp_mb(); /* order accesses to ->inflight before stack-frame death. */
  725. return 0;
  726. }
  727. /*
  728. * RCU torture force-quiescent-state kthread. Repeatedly induces
  729. * bursts of calls to force_quiescent_state(), increasing the probability
  730. * of occurrence of some important types of race conditions.
  731. */
  732. static int
  733. rcu_torture_fqs(void *arg)
  734. {
  735. unsigned long fqs_resume_time;
  736. int fqs_burst_remaining;
  737. VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
  738. do {
  739. fqs_resume_time = jiffies + fqs_stutter * HZ;
  740. while (jiffies - fqs_resume_time > LONG_MAX) {
  741. schedule_timeout_interruptible(1);
  742. }
  743. fqs_burst_remaining = fqs_duration;
  744. while (fqs_burst_remaining > 0) {
  745. cur_ops->fqs();
  746. udelay(fqs_holdoff);
  747. fqs_burst_remaining -= fqs_holdoff;
  748. }
  749. rcu_stutter_wait("rcu_torture_fqs");
  750. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  751. VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
  752. rcutorture_shutdown_absorb("rcu_torture_fqs");
  753. while (!kthread_should_stop())
  754. schedule_timeout_uninterruptible(1);
  755. return 0;
  756. }
  757. /*
  758. * RCU torture writer kthread. Repeatedly substitutes a new structure
  759. * for that pointed to by rcu_torture_current, freeing the old structure
  760. * after a series of grace periods (the "pipeline").
  761. */
  762. static int
  763. rcu_torture_writer(void *arg)
  764. {
  765. int i;
  766. long oldbatch = rcu_batches_completed();
  767. struct rcu_torture *rp;
  768. struct rcu_torture *old_rp;
  769. static DEFINE_RCU_RANDOM(rand);
  770. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  771. set_user_nice(current, 19);
  772. do {
  773. schedule_timeout_uninterruptible(1);
  774. rp = rcu_torture_alloc();
  775. if (rp == NULL)
  776. continue;
  777. rp->rtort_pipe_count = 0;
  778. udelay(rcu_random(&rand) & 0x3ff);
  779. old_rp = rcu_dereference_check(rcu_torture_current,
  780. current == writer_task);
  781. rp->rtort_mbtest = 1;
  782. rcu_assign_pointer(rcu_torture_current, rp);
  783. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  784. if (old_rp) {
  785. i = old_rp->rtort_pipe_count;
  786. if (i > RCU_TORTURE_PIPE_LEN)
  787. i = RCU_TORTURE_PIPE_LEN;
  788. atomic_inc(&rcu_torture_wcount[i]);
  789. old_rp->rtort_pipe_count++;
  790. cur_ops->deferred_free(old_rp);
  791. }
  792. rcu_torture_current_version++;
  793. oldbatch = cur_ops->completed();
  794. rcu_stutter_wait("rcu_torture_writer");
  795. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  796. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  797. rcutorture_shutdown_absorb("rcu_torture_writer");
  798. while (!kthread_should_stop())
  799. schedule_timeout_uninterruptible(1);
  800. return 0;
  801. }
  802. /*
  803. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  804. * delay between calls.
  805. */
  806. static int
  807. rcu_torture_fakewriter(void *arg)
  808. {
  809. DEFINE_RCU_RANDOM(rand);
  810. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
  811. set_user_nice(current, 19);
  812. do {
  813. schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
  814. udelay(rcu_random(&rand) & 0x3ff);
  815. cur_ops->sync();
  816. rcu_stutter_wait("rcu_torture_fakewriter");
  817. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  818. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
  819. rcutorture_shutdown_absorb("rcu_torture_fakewriter");
  820. while (!kthread_should_stop())
  821. schedule_timeout_uninterruptible(1);
  822. return 0;
  823. }
  824. /*
  825. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  826. * incrementing the corresponding element of the pipeline array. The
  827. * counter in the element should never be greater than 1, otherwise, the
  828. * RCU implementation is broken.
  829. */
  830. static void rcu_torture_timer(unsigned long unused)
  831. {
  832. int idx;
  833. int completed;
  834. static DEFINE_RCU_RANDOM(rand);
  835. static DEFINE_SPINLOCK(rand_lock);
  836. struct rcu_torture *p;
  837. int pipe_count;
  838. idx = cur_ops->readlock();
  839. completed = cur_ops->completed();
  840. p = rcu_dereference_check(rcu_torture_current,
  841. rcu_read_lock_held() ||
  842. rcu_read_lock_bh_held() ||
  843. rcu_read_lock_sched_held() ||
  844. srcu_read_lock_held(&srcu_ctl));
  845. if (p == NULL) {
  846. /* Leave because rcu_torture_writer is not yet underway */
  847. cur_ops->readunlock(idx);
  848. return;
  849. }
  850. if (p->rtort_mbtest == 0)
  851. atomic_inc(&n_rcu_torture_mberror);
  852. spin_lock(&rand_lock);
  853. cur_ops->read_delay(&rand);
  854. n_rcu_torture_timers++;
  855. spin_unlock(&rand_lock);
  856. preempt_disable();
  857. pipe_count = p->rtort_pipe_count;
  858. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  859. /* Should not happen, but... */
  860. pipe_count = RCU_TORTURE_PIPE_LEN;
  861. }
  862. __this_cpu_inc(rcu_torture_count[pipe_count]);
  863. completed = cur_ops->completed() - completed;
  864. if (completed > RCU_TORTURE_PIPE_LEN) {
  865. /* Should not happen, but... */
  866. completed = RCU_TORTURE_PIPE_LEN;
  867. }
  868. __this_cpu_inc(rcu_torture_batch[completed]);
  869. preempt_enable();
  870. cur_ops->readunlock(idx);
  871. }
  872. /*
  873. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  874. * incrementing the corresponding element of the pipeline array. The
  875. * counter in the element should never be greater than 1, otherwise, the
  876. * RCU implementation is broken.
  877. */
  878. static int
  879. rcu_torture_reader(void *arg)
  880. {
  881. int completed;
  882. int idx;
  883. DEFINE_RCU_RANDOM(rand);
  884. struct rcu_torture *p;
  885. int pipe_count;
  886. struct timer_list t;
  887. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  888. set_user_nice(current, 19);
  889. if (irqreader && cur_ops->irq_capable)
  890. setup_timer_on_stack(&t, rcu_torture_timer, 0);
  891. do {
  892. if (irqreader && cur_ops->irq_capable) {
  893. if (!timer_pending(&t))
  894. mod_timer(&t, jiffies + 1);
  895. }
  896. idx = cur_ops->readlock();
  897. completed = cur_ops->completed();
  898. p = rcu_dereference_check(rcu_torture_current,
  899. rcu_read_lock_held() ||
  900. rcu_read_lock_bh_held() ||
  901. rcu_read_lock_sched_held() ||
  902. srcu_read_lock_held(&srcu_ctl));
  903. if (p == NULL) {
  904. /* Wait for rcu_torture_writer to get underway */
  905. cur_ops->readunlock(idx);
  906. schedule_timeout_interruptible(HZ);
  907. continue;
  908. }
  909. if (p->rtort_mbtest == 0)
  910. atomic_inc(&n_rcu_torture_mberror);
  911. cur_ops->read_delay(&rand);
  912. preempt_disable();
  913. pipe_count = p->rtort_pipe_count;
  914. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  915. /* Should not happen, but... */
  916. pipe_count = RCU_TORTURE_PIPE_LEN;
  917. }
  918. __this_cpu_inc(rcu_torture_count[pipe_count]);
  919. completed = cur_ops->completed() - completed;
  920. if (completed > RCU_TORTURE_PIPE_LEN) {
  921. /* Should not happen, but... */
  922. completed = RCU_TORTURE_PIPE_LEN;
  923. }
  924. __this_cpu_inc(rcu_torture_batch[completed]);
  925. preempt_enable();
  926. cur_ops->readunlock(idx);
  927. schedule();
  928. rcu_stutter_wait("rcu_torture_reader");
  929. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  930. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  931. rcutorture_shutdown_absorb("rcu_torture_reader");
  932. if (irqreader && cur_ops->irq_capable)
  933. del_timer_sync(&t);
  934. while (!kthread_should_stop())
  935. schedule_timeout_uninterruptible(1);
  936. return 0;
  937. }
  938. /*
  939. * Create an RCU-torture statistics message in the specified buffer.
  940. */
  941. static int
  942. rcu_torture_printk(char *page)
  943. {
  944. int cnt = 0;
  945. int cpu;
  946. int i;
  947. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  948. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  949. for_each_possible_cpu(cpu) {
  950. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  951. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  952. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  953. }
  954. }
  955. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  956. if (pipesummary[i] != 0)
  957. break;
  958. }
  959. cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
  960. cnt += sprintf(&page[cnt],
  961. "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
  962. "rtmbe: %d rtbke: %ld rtbre: %ld rtbae: %ld rtbafe: %ld "
  963. "rtbf: %ld rtb: %ld nt: %ld",
  964. rcu_torture_current,
  965. rcu_torture_current_version,
  966. list_empty(&rcu_torture_freelist),
  967. atomic_read(&n_rcu_torture_alloc),
  968. atomic_read(&n_rcu_torture_alloc_fail),
  969. atomic_read(&n_rcu_torture_free),
  970. atomic_read(&n_rcu_torture_mberror),
  971. n_rcu_torture_boost_ktrerror,
  972. n_rcu_torture_boost_rterror,
  973. n_rcu_torture_boost_allocerror,
  974. n_rcu_torture_boost_afferror,
  975. n_rcu_torture_boost_failure,
  976. n_rcu_torture_boosts,
  977. n_rcu_torture_timers);
  978. if (atomic_read(&n_rcu_torture_mberror) != 0 ||
  979. n_rcu_torture_boost_ktrerror != 0 ||
  980. n_rcu_torture_boost_rterror != 0 ||
  981. n_rcu_torture_boost_allocerror != 0 ||
  982. n_rcu_torture_boost_afferror != 0 ||
  983. n_rcu_torture_boost_failure != 0)
  984. cnt += sprintf(&page[cnt], " !!!");
  985. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  986. if (i > 1) {
  987. cnt += sprintf(&page[cnt], "!!! ");
  988. atomic_inc(&n_rcu_torture_error);
  989. WARN_ON_ONCE(1);
  990. }
  991. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  992. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  993. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  994. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  995. cnt += sprintf(&page[cnt], "Reader Batch: ");
  996. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  997. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  998. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  999. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  1000. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1001. cnt += sprintf(&page[cnt], " %d",
  1002. atomic_read(&rcu_torture_wcount[i]));
  1003. }
  1004. cnt += sprintf(&page[cnt], "\n");
  1005. if (cur_ops->stats)
  1006. cnt += cur_ops->stats(&page[cnt]);
  1007. return cnt;
  1008. }
  1009. /*
  1010. * Print torture statistics. Caller must ensure that there is only
  1011. * one call to this function at a given time!!! This is normally
  1012. * accomplished by relying on the module system to only have one copy
  1013. * of the module loaded, and then by giving the rcu_torture_stats
  1014. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  1015. * thread is not running).
  1016. */
  1017. static void
  1018. rcu_torture_stats_print(void)
  1019. {
  1020. int cnt;
  1021. cnt = rcu_torture_printk(printk_buf);
  1022. printk(KERN_ALERT "%s", printk_buf);
  1023. }
  1024. /*
  1025. * Periodically prints torture statistics, if periodic statistics printing
  1026. * was specified via the stat_interval module parameter.
  1027. *
  1028. * No need to worry about fullstop here, since this one doesn't reference
  1029. * volatile state or register callbacks.
  1030. */
  1031. static int
  1032. rcu_torture_stats(void *arg)
  1033. {
  1034. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  1035. do {
  1036. schedule_timeout_interruptible(stat_interval * HZ);
  1037. rcu_torture_stats_print();
  1038. rcutorture_shutdown_absorb("rcu_torture_stats");
  1039. } while (!kthread_should_stop());
  1040. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  1041. return 0;
  1042. }
  1043. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  1044. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  1045. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  1046. */
  1047. static void rcu_torture_shuffle_tasks(void)
  1048. {
  1049. int i;
  1050. cpumask_setall(shuffle_tmp_mask);
  1051. get_online_cpus();
  1052. /* No point in shuffling if there is only one online CPU (ex: UP) */
  1053. if (num_online_cpus() == 1) {
  1054. put_online_cpus();
  1055. return;
  1056. }
  1057. if (rcu_idle_cpu != -1)
  1058. cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
  1059. set_cpus_allowed_ptr(current, shuffle_tmp_mask);
  1060. if (reader_tasks) {
  1061. for (i = 0; i < nrealreaders; i++)
  1062. if (reader_tasks[i])
  1063. set_cpus_allowed_ptr(reader_tasks[i],
  1064. shuffle_tmp_mask);
  1065. }
  1066. if (fakewriter_tasks) {
  1067. for (i = 0; i < nfakewriters; i++)
  1068. if (fakewriter_tasks[i])
  1069. set_cpus_allowed_ptr(fakewriter_tasks[i],
  1070. shuffle_tmp_mask);
  1071. }
  1072. if (writer_task)
  1073. set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
  1074. if (stats_task)
  1075. set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
  1076. if (rcu_idle_cpu == -1)
  1077. rcu_idle_cpu = num_online_cpus() - 1;
  1078. else
  1079. rcu_idle_cpu--;
  1080. put_online_cpus();
  1081. }
  1082. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  1083. * system to become idle at a time and cut off its timer ticks. This is meant
  1084. * to test the support for such tickless idle CPU in RCU.
  1085. */
  1086. static int
  1087. rcu_torture_shuffle(void *arg)
  1088. {
  1089. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  1090. do {
  1091. schedule_timeout_interruptible(shuffle_interval * HZ);
  1092. rcu_torture_shuffle_tasks();
  1093. rcutorture_shutdown_absorb("rcu_torture_shuffle");
  1094. } while (!kthread_should_stop());
  1095. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  1096. return 0;
  1097. }
  1098. /* Cause the rcutorture test to "stutter", starting and stopping all
  1099. * threads periodically.
  1100. */
  1101. static int
  1102. rcu_torture_stutter(void *arg)
  1103. {
  1104. VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
  1105. do {
  1106. schedule_timeout_interruptible(stutter * HZ);
  1107. stutter_pause_test = 1;
  1108. if (!kthread_should_stop())
  1109. schedule_timeout_interruptible(stutter * HZ);
  1110. stutter_pause_test = 0;
  1111. rcutorture_shutdown_absorb("rcu_torture_stutter");
  1112. } while (!kthread_should_stop());
  1113. VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
  1114. return 0;
  1115. }
  1116. static inline void
  1117. rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
  1118. {
  1119. printk(KERN_ALERT "%s" TORTURE_FLAG
  1120. "--- %s: nreaders=%d nfakewriters=%d "
  1121. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  1122. "shuffle_interval=%d stutter=%d irqreader=%d "
  1123. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
  1124. "test_boost=%d/%d test_boost_interval=%d "
  1125. "test_boost_duration=%d\n",
  1126. torture_type, tag, nrealreaders, nfakewriters,
  1127. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  1128. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
  1129. test_boost, cur_ops->can_boost,
  1130. test_boost_interval, test_boost_duration);
  1131. }
  1132. static struct notifier_block rcutorture_shutdown_nb = {
  1133. .notifier_call = rcutorture_shutdown_notify,
  1134. };
  1135. static void rcutorture_booster_cleanup(int cpu)
  1136. {
  1137. struct task_struct *t;
  1138. if (boost_tasks[cpu] == NULL)
  1139. return;
  1140. mutex_lock(&boost_mutex);
  1141. VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
  1142. t = boost_tasks[cpu];
  1143. boost_tasks[cpu] = NULL;
  1144. mutex_unlock(&boost_mutex);
  1145. /* This must be outside of the mutex, otherwise deadlock! */
  1146. kthread_stop(t);
  1147. }
  1148. static int rcutorture_booster_init(int cpu)
  1149. {
  1150. int retval;
  1151. if (boost_tasks[cpu] != NULL)
  1152. return 0; /* Already created, nothing more to do. */
  1153. /* Don't allow time recalculation while creating a new task. */
  1154. mutex_lock(&boost_mutex);
  1155. VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
  1156. boost_tasks[cpu] = kthread_create(rcu_torture_boost, NULL,
  1157. "rcu_torture_boost");
  1158. if (IS_ERR(boost_tasks[cpu])) {
  1159. retval = PTR_ERR(boost_tasks[cpu]);
  1160. VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
  1161. n_rcu_torture_boost_ktrerror++;
  1162. boost_tasks[cpu] = NULL;
  1163. mutex_unlock(&boost_mutex);
  1164. return retval;
  1165. }
  1166. kthread_bind(boost_tasks[cpu], cpu);
  1167. wake_up_process(boost_tasks[cpu]);
  1168. mutex_unlock(&boost_mutex);
  1169. return 0;
  1170. }
  1171. static int rcutorture_cpu_notify(struct notifier_block *self,
  1172. unsigned long action, void *hcpu)
  1173. {
  1174. long cpu = (long)hcpu;
  1175. switch (action) {
  1176. case CPU_ONLINE:
  1177. case CPU_DOWN_FAILED:
  1178. (void)rcutorture_booster_init(cpu);
  1179. break;
  1180. case CPU_DOWN_PREPARE:
  1181. rcutorture_booster_cleanup(cpu);
  1182. break;
  1183. default:
  1184. break;
  1185. }
  1186. return NOTIFY_OK;
  1187. }
  1188. static struct notifier_block rcutorture_cpu_nb = {
  1189. .notifier_call = rcutorture_cpu_notify,
  1190. };
  1191. static void
  1192. rcu_torture_cleanup(void)
  1193. {
  1194. int i;
  1195. mutex_lock(&fullstop_mutex);
  1196. if (fullstop == FULLSTOP_SHUTDOWN) {
  1197. printk(KERN_WARNING /* but going down anyway, so... */
  1198. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  1199. mutex_unlock(&fullstop_mutex);
  1200. schedule_timeout_uninterruptible(10);
  1201. if (cur_ops->cb_barrier != NULL)
  1202. cur_ops->cb_barrier();
  1203. return;
  1204. }
  1205. fullstop = FULLSTOP_RMMOD;
  1206. mutex_unlock(&fullstop_mutex);
  1207. unregister_reboot_notifier(&rcutorture_shutdown_nb);
  1208. if (stutter_task) {
  1209. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
  1210. kthread_stop(stutter_task);
  1211. }
  1212. stutter_task = NULL;
  1213. if (shuffler_task) {
  1214. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  1215. kthread_stop(shuffler_task);
  1216. free_cpumask_var(shuffle_tmp_mask);
  1217. }
  1218. shuffler_task = NULL;
  1219. if (writer_task) {
  1220. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  1221. kthread_stop(writer_task);
  1222. }
  1223. writer_task = NULL;
  1224. if (reader_tasks) {
  1225. for (i = 0; i < nrealreaders; i++) {
  1226. if (reader_tasks[i]) {
  1227. VERBOSE_PRINTK_STRING(
  1228. "Stopping rcu_torture_reader task");
  1229. kthread_stop(reader_tasks[i]);
  1230. }
  1231. reader_tasks[i] = NULL;
  1232. }
  1233. kfree(reader_tasks);
  1234. reader_tasks = NULL;
  1235. }
  1236. rcu_torture_current = NULL;
  1237. if (fakewriter_tasks) {
  1238. for (i = 0; i < nfakewriters; i++) {
  1239. if (fakewriter_tasks[i]) {
  1240. VERBOSE_PRINTK_STRING(
  1241. "Stopping rcu_torture_fakewriter task");
  1242. kthread_stop(fakewriter_tasks[i]);
  1243. }
  1244. fakewriter_tasks[i] = NULL;
  1245. }
  1246. kfree(fakewriter_tasks);
  1247. fakewriter_tasks = NULL;
  1248. }
  1249. if (stats_task) {
  1250. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  1251. kthread_stop(stats_task);
  1252. }
  1253. stats_task = NULL;
  1254. if (fqs_task) {
  1255. VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
  1256. kthread_stop(fqs_task);
  1257. }
  1258. fqs_task = NULL;
  1259. if ((test_boost == 1 && cur_ops->can_boost) ||
  1260. test_boost == 2) {
  1261. unregister_cpu_notifier(&rcutorture_cpu_nb);
  1262. for_each_possible_cpu(i)
  1263. rcutorture_booster_cleanup(i);
  1264. }
  1265. /* Wait for all RCU callbacks to fire. */
  1266. if (cur_ops->cb_barrier != NULL)
  1267. cur_ops->cb_barrier();
  1268. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  1269. if (cur_ops->cleanup)
  1270. cur_ops->cleanup();
  1271. if (atomic_read(&n_rcu_torture_error))
  1272. rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
  1273. else
  1274. rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
  1275. }
  1276. static int __init
  1277. rcu_torture_init(void)
  1278. {
  1279. int i;
  1280. int cpu;
  1281. int firsterr = 0;
  1282. static struct rcu_torture_ops *torture_ops[] =
  1283. { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
  1284. &rcu_bh_ops, &rcu_bh_sync_ops,
  1285. &srcu_ops, &srcu_expedited_ops,
  1286. &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
  1287. mutex_lock(&fullstop_mutex);
  1288. /* Process args and tell the world that the torturer is on the job. */
  1289. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1290. cur_ops = torture_ops[i];
  1291. if (strcmp(torture_type, cur_ops->name) == 0)
  1292. break;
  1293. }
  1294. if (i == ARRAY_SIZE(torture_ops)) {
  1295. printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
  1296. torture_type);
  1297. printk(KERN_ALERT "rcu-torture types:");
  1298. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1299. printk(KERN_ALERT " %s", torture_ops[i]->name);
  1300. printk(KERN_ALERT "\n");
  1301. mutex_unlock(&fullstop_mutex);
  1302. return -EINVAL;
  1303. }
  1304. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  1305. printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
  1306. "fqs_duration, fqs disabled.\n");
  1307. fqs_duration = 0;
  1308. }
  1309. if (cur_ops->init)
  1310. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  1311. if (nreaders >= 0)
  1312. nrealreaders = nreaders;
  1313. else
  1314. nrealreaders = 2 * num_online_cpus();
  1315. rcu_torture_print_module_parms(cur_ops, "Start of test");
  1316. fullstop = FULLSTOP_DONTSTOP;
  1317. /* Set up the freelist. */
  1318. INIT_LIST_HEAD(&rcu_torture_freelist);
  1319. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  1320. rcu_tortures[i].rtort_mbtest = 0;
  1321. list_add_tail(&rcu_tortures[i].rtort_free,
  1322. &rcu_torture_freelist);
  1323. }
  1324. /* Initialize the statistics so that each run gets its own numbers. */
  1325. rcu_torture_current = NULL;
  1326. rcu_torture_current_version = 0;
  1327. atomic_set(&n_rcu_torture_alloc, 0);
  1328. atomic_set(&n_rcu_torture_alloc_fail, 0);
  1329. atomic_set(&n_rcu_torture_free, 0);
  1330. atomic_set(&n_rcu_torture_mberror, 0);
  1331. atomic_set(&n_rcu_torture_error, 0);
  1332. n_rcu_torture_boost_ktrerror = 0;
  1333. n_rcu_torture_boost_rterror = 0;
  1334. n_rcu_torture_boost_allocerror = 0;
  1335. n_rcu_torture_boost_afferror = 0;
  1336. n_rcu_torture_boost_failure = 0;
  1337. n_rcu_torture_boosts = 0;
  1338. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1339. atomic_set(&rcu_torture_wcount[i], 0);
  1340. for_each_possible_cpu(cpu) {
  1341. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1342. per_cpu(rcu_torture_count, cpu)[i] = 0;
  1343. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  1344. }
  1345. }
  1346. /* Start up the kthreads. */
  1347. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  1348. writer_task = kthread_run(rcu_torture_writer, NULL,
  1349. "rcu_torture_writer");
  1350. if (IS_ERR(writer_task)) {
  1351. firsterr = PTR_ERR(writer_task);
  1352. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  1353. writer_task = NULL;
  1354. goto unwind;
  1355. }
  1356. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  1357. GFP_KERNEL);
  1358. if (fakewriter_tasks == NULL) {
  1359. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1360. firsterr = -ENOMEM;
  1361. goto unwind;
  1362. }
  1363. for (i = 0; i < nfakewriters; i++) {
  1364. VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
  1365. fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
  1366. "rcu_torture_fakewriter");
  1367. if (IS_ERR(fakewriter_tasks[i])) {
  1368. firsterr = PTR_ERR(fakewriter_tasks[i]);
  1369. VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
  1370. fakewriter_tasks[i] = NULL;
  1371. goto unwind;
  1372. }
  1373. }
  1374. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  1375. GFP_KERNEL);
  1376. if (reader_tasks == NULL) {
  1377. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1378. firsterr = -ENOMEM;
  1379. goto unwind;
  1380. }
  1381. for (i = 0; i < nrealreaders; i++) {
  1382. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  1383. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  1384. "rcu_torture_reader");
  1385. if (IS_ERR(reader_tasks[i])) {
  1386. firsterr = PTR_ERR(reader_tasks[i]);
  1387. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  1388. reader_tasks[i] = NULL;
  1389. goto unwind;
  1390. }
  1391. }
  1392. if (stat_interval > 0) {
  1393. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  1394. stats_task = kthread_run(rcu_torture_stats, NULL,
  1395. "rcu_torture_stats");
  1396. if (IS_ERR(stats_task)) {
  1397. firsterr = PTR_ERR(stats_task);
  1398. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  1399. stats_task = NULL;
  1400. goto unwind;
  1401. }
  1402. }
  1403. if (test_no_idle_hz) {
  1404. rcu_idle_cpu = num_online_cpus() - 1;
  1405. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  1406. firsterr = -ENOMEM;
  1407. VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
  1408. goto unwind;
  1409. }
  1410. /* Create the shuffler thread */
  1411. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  1412. "rcu_torture_shuffle");
  1413. if (IS_ERR(shuffler_task)) {
  1414. free_cpumask_var(shuffle_tmp_mask);
  1415. firsterr = PTR_ERR(shuffler_task);
  1416. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  1417. shuffler_task = NULL;
  1418. goto unwind;
  1419. }
  1420. }
  1421. if (stutter < 0)
  1422. stutter = 0;
  1423. if (stutter) {
  1424. /* Create the stutter thread */
  1425. stutter_task = kthread_run(rcu_torture_stutter, NULL,
  1426. "rcu_torture_stutter");
  1427. if (IS_ERR(stutter_task)) {
  1428. firsterr = PTR_ERR(stutter_task);
  1429. VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
  1430. stutter_task = NULL;
  1431. goto unwind;
  1432. }
  1433. }
  1434. if (fqs_duration < 0)
  1435. fqs_duration = 0;
  1436. if (fqs_duration) {
  1437. /* Create the stutter thread */
  1438. fqs_task = kthread_run(rcu_torture_fqs, NULL,
  1439. "rcu_torture_fqs");
  1440. if (IS_ERR(fqs_task)) {
  1441. firsterr = PTR_ERR(fqs_task);
  1442. VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
  1443. fqs_task = NULL;
  1444. goto unwind;
  1445. }
  1446. }
  1447. if (test_boost_interval < 1)
  1448. test_boost_interval = 1;
  1449. if (test_boost_duration < 2)
  1450. test_boost_duration = 2;
  1451. if ((test_boost == 1 && cur_ops->can_boost) ||
  1452. test_boost == 2) {
  1453. int retval;
  1454. boost_starttime = jiffies + test_boost_interval * HZ;
  1455. register_cpu_notifier(&rcutorture_cpu_nb);
  1456. for_each_possible_cpu(i) {
  1457. if (cpu_is_offline(i))
  1458. continue; /* Heuristic: CPU can go offline. */
  1459. retval = rcutorture_booster_init(i);
  1460. if (retval < 0) {
  1461. firsterr = retval;
  1462. goto unwind;
  1463. }
  1464. }
  1465. }
  1466. register_reboot_notifier(&rcutorture_shutdown_nb);
  1467. mutex_unlock(&fullstop_mutex);
  1468. return 0;
  1469. unwind:
  1470. mutex_unlock(&fullstop_mutex);
  1471. rcu_torture_cleanup();
  1472. return firsterr;
  1473. }
  1474. module_init(rcu_torture_init);
  1475. module_exit(rcu_torture_cleanup);