rcutorture.c 52 KB

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