rcutorture.c 46 KB

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