rcutorture.c 53 KB

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