rcutorture.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright (C) IBM Corporation, 2005, 2006
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. * Josh Triplett <josh@joshtriplett.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 <linux/trace_clock.h>
  50. #include <asm/byteorder.h>
  51. #include <linux/torture.h>
  52. #include <linux/vmalloc.h>
  53. MODULE_LICENSE("GPL");
  54. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
  55. torture_param(int, cbflood_inter_holdoff, HZ,
  56. "Holdoff between floods (jiffies)");
  57. torture_param(int, cbflood_intra_holdoff, 1,
  58. "Holdoff between bursts (jiffies)");
  59. torture_param(int, cbflood_n_burst, 3, "# bursts in flood, zero to disable");
  60. torture_param(int, cbflood_n_per_burst, 20000,
  61. "# callbacks per burst in flood");
  62. torture_param(int, fqs_duration, 0,
  63. "Duration of fqs bursts (us), 0 to disable");
  64. torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
  65. torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
  66. torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
  67. torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
  68. torture_param(bool, gp_normal, false,
  69. "Use normal (non-expedited) GP wait primitives");
  70. torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
  71. torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
  72. torture_param(int, n_barrier_cbs, 0,
  73. "# of callbacks/kthreads for barrier testing");
  74. torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
  75. torture_param(int, nreaders, -1, "Number of RCU reader threads");
  76. torture_param(int, object_debug, 0,
  77. "Enable debug-object double call_rcu() testing");
  78. torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
  79. torture_param(int, onoff_interval, 0,
  80. "Time between CPU hotplugs (s), 0=disable");
  81. torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
  82. torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
  83. torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
  84. torture_param(int, stall_cpu_holdoff, 10,
  85. "Time to wait before starting stall (s).");
  86. torture_param(int, stat_interval, 60,
  87. "Number of seconds between stats printk()s");
  88. torture_param(int, stutter, 5, "Number of seconds to run/halt test");
  89. torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
  90. torture_param(int, test_boost_duration, 4,
  91. "Duration of each boost test, seconds.");
  92. torture_param(int, test_boost_interval, 7,
  93. "Interval between boost tests, seconds.");
  94. torture_param(bool, test_no_idle_hz, true,
  95. "Test support for tickless idle CPUs");
  96. torture_param(bool, verbose, true,
  97. "Enable verbose debugging printk()s");
  98. static char *torture_type = "rcu";
  99. module_param(torture_type, charp, 0444);
  100. MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
  101. static int nrealreaders;
  102. static int ncbflooders;
  103. static struct task_struct *writer_task;
  104. static struct task_struct **fakewriter_tasks;
  105. static struct task_struct **reader_tasks;
  106. static struct task_struct *stats_task;
  107. static struct task_struct **cbflood_task;
  108. static struct task_struct *fqs_task;
  109. static struct task_struct *boost_tasks[NR_CPUS];
  110. static struct task_struct *stall_task;
  111. static struct task_struct **barrier_cbs_tasks;
  112. static struct task_struct *barrier_task;
  113. #define RCU_TORTURE_PIPE_LEN 10
  114. struct rcu_torture {
  115. struct rcu_head rtort_rcu;
  116. int rtort_pipe_count;
  117. struct list_head rtort_free;
  118. int rtort_mbtest;
  119. };
  120. static LIST_HEAD(rcu_torture_freelist);
  121. static struct rcu_torture __rcu *rcu_torture_current;
  122. static unsigned long rcu_torture_current_version;
  123. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  124. static DEFINE_SPINLOCK(rcu_torture_lock);
  125. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1],
  126. rcu_torture_count) = { 0 };
  127. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1],
  128. rcu_torture_batch) = { 0 };
  129. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  130. static atomic_t n_rcu_torture_alloc;
  131. static atomic_t n_rcu_torture_alloc_fail;
  132. static atomic_t n_rcu_torture_free;
  133. static atomic_t n_rcu_torture_mberror;
  134. static atomic_t n_rcu_torture_error;
  135. static long n_rcu_torture_barrier_error;
  136. static long n_rcu_torture_boost_ktrerror;
  137. static long n_rcu_torture_boost_rterror;
  138. static long n_rcu_torture_boost_failure;
  139. static long n_rcu_torture_boosts;
  140. static long n_rcu_torture_timers;
  141. static long n_barrier_attempts;
  142. static long n_barrier_successes;
  143. static atomic_long_t n_cbfloods;
  144. static struct list_head rcu_torture_removed;
  145. static int rcu_torture_writer_state;
  146. #define RTWS_FIXED_DELAY 0
  147. #define RTWS_DELAY 1
  148. #define RTWS_REPLACE 2
  149. #define RTWS_DEF_FREE 3
  150. #define RTWS_EXP_SYNC 4
  151. #define RTWS_COND_GET 5
  152. #define RTWS_COND_SYNC 6
  153. #define RTWS_SYNC 7
  154. #define RTWS_STUTTER 8
  155. #define RTWS_STOPPING 9
  156. #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
  157. #define RCUTORTURE_RUNNABLE_INIT 1
  158. #else
  159. #define RCUTORTURE_RUNNABLE_INIT 0
  160. #endif
  161. static int torture_runnable = RCUTORTURE_RUNNABLE_INIT;
  162. module_param(torture_runnable, int, 0444);
  163. MODULE_PARM_DESC(torture_runnable, "Start rcutorture at boot");
  164. #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
  165. #define rcu_can_boost() 1
  166. #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
  167. #define rcu_can_boost() 0
  168. #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
  169. #ifdef CONFIG_RCU_TRACE
  170. static u64 notrace rcu_trace_clock_local(void)
  171. {
  172. u64 ts = trace_clock_local();
  173. unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
  174. return ts;
  175. }
  176. #else /* #ifdef CONFIG_RCU_TRACE */
  177. static u64 notrace rcu_trace_clock_local(void)
  178. {
  179. return 0ULL;
  180. }
  181. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  182. static unsigned long boost_starttime; /* jiffies of next boost test start. */
  183. static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
  184. /* and boost task create/destroy. */
  185. static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
  186. static bool barrier_phase; /* Test phase. */
  187. static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
  188. static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
  189. static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
  190. /*
  191. * Allocate an element from the rcu_tortures pool.
  192. */
  193. static struct rcu_torture *
  194. rcu_torture_alloc(void)
  195. {
  196. struct list_head *p;
  197. spin_lock_bh(&rcu_torture_lock);
  198. if (list_empty(&rcu_torture_freelist)) {
  199. atomic_inc(&n_rcu_torture_alloc_fail);
  200. spin_unlock_bh(&rcu_torture_lock);
  201. return NULL;
  202. }
  203. atomic_inc(&n_rcu_torture_alloc);
  204. p = rcu_torture_freelist.next;
  205. list_del_init(p);
  206. spin_unlock_bh(&rcu_torture_lock);
  207. return container_of(p, struct rcu_torture, rtort_free);
  208. }
  209. /*
  210. * Free an element to the rcu_tortures pool.
  211. */
  212. static void
  213. rcu_torture_free(struct rcu_torture *p)
  214. {
  215. atomic_inc(&n_rcu_torture_free);
  216. spin_lock_bh(&rcu_torture_lock);
  217. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  218. spin_unlock_bh(&rcu_torture_lock);
  219. }
  220. /*
  221. * Operations vector for selecting different types of tests.
  222. */
  223. struct rcu_torture_ops {
  224. int ttype;
  225. void (*init)(void);
  226. int (*readlock)(void);
  227. void (*read_delay)(struct torture_random_state *rrsp);
  228. void (*readunlock)(int idx);
  229. unsigned long (*started)(void);
  230. unsigned long (*completed)(void);
  231. void (*deferred_free)(struct rcu_torture *p);
  232. void (*sync)(void);
  233. void (*exp_sync)(void);
  234. unsigned long (*get_state)(void);
  235. void (*cond_sync)(unsigned long oldstate);
  236. void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
  237. void (*cb_barrier)(void);
  238. void (*fqs)(void);
  239. void (*stats)(void);
  240. int irq_capable;
  241. int can_boost;
  242. const char *name;
  243. };
  244. static struct rcu_torture_ops *cur_ops;
  245. /*
  246. * Definitions for rcu torture testing.
  247. */
  248. static int rcu_torture_read_lock(void) __acquires(RCU)
  249. {
  250. rcu_read_lock();
  251. return 0;
  252. }
  253. static void rcu_read_delay(struct torture_random_state *rrsp)
  254. {
  255. const unsigned long shortdelay_us = 200;
  256. const unsigned long longdelay_ms = 50;
  257. /* We want a short delay sometimes to make a reader delay the grace
  258. * period, and we want a long delay occasionally to trigger
  259. * force_quiescent_state. */
  260. if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
  261. mdelay(longdelay_ms);
  262. if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
  263. udelay(shortdelay_us);
  264. #ifdef CONFIG_PREEMPT
  265. if (!preempt_count() &&
  266. !(torture_random(rrsp) % (nrealreaders * 20000)))
  267. preempt_schedule(); /* No QS if preempt_disable() in effect */
  268. #endif
  269. }
  270. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  271. {
  272. rcu_read_unlock();
  273. }
  274. /*
  275. * Update callback in the pipe. This should be invoked after a grace period.
  276. */
  277. static bool
  278. rcu_torture_pipe_update_one(struct rcu_torture *rp)
  279. {
  280. int i;
  281. i = rp->rtort_pipe_count;
  282. if (i > RCU_TORTURE_PIPE_LEN)
  283. i = RCU_TORTURE_PIPE_LEN;
  284. atomic_inc(&rcu_torture_wcount[i]);
  285. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  286. rp->rtort_mbtest = 0;
  287. return true;
  288. }
  289. return false;
  290. }
  291. /*
  292. * Update all callbacks in the pipe. Suitable for synchronous grace-period
  293. * primitives.
  294. */
  295. static void
  296. rcu_torture_pipe_update(struct rcu_torture *old_rp)
  297. {
  298. struct rcu_torture *rp;
  299. struct rcu_torture *rp1;
  300. if (old_rp)
  301. list_add(&old_rp->rtort_free, &rcu_torture_removed);
  302. list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
  303. if (rcu_torture_pipe_update_one(rp)) {
  304. list_del(&rp->rtort_free);
  305. rcu_torture_free(rp);
  306. }
  307. }
  308. }
  309. static void
  310. rcu_torture_cb(struct rcu_head *p)
  311. {
  312. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  313. if (torture_must_stop_irq()) {
  314. /* Test is ending, just drop callbacks on the floor. */
  315. /* The next initialization will pick up the pieces. */
  316. return;
  317. }
  318. if (rcu_torture_pipe_update_one(rp))
  319. rcu_torture_free(rp);
  320. else
  321. cur_ops->deferred_free(rp);
  322. }
  323. static unsigned long rcu_no_completed(void)
  324. {
  325. return 0;
  326. }
  327. static void rcu_torture_deferred_free(struct rcu_torture *p)
  328. {
  329. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  330. }
  331. static void rcu_sync_torture_init(void)
  332. {
  333. INIT_LIST_HEAD(&rcu_torture_removed);
  334. }
  335. static struct rcu_torture_ops rcu_ops = {
  336. .ttype = RCU_FLAVOR,
  337. .init = rcu_sync_torture_init,
  338. .readlock = rcu_torture_read_lock,
  339. .read_delay = rcu_read_delay,
  340. .readunlock = rcu_torture_read_unlock,
  341. .started = rcu_batches_started,
  342. .completed = rcu_batches_completed,
  343. .deferred_free = rcu_torture_deferred_free,
  344. .sync = synchronize_rcu,
  345. .exp_sync = synchronize_rcu_expedited,
  346. .get_state = get_state_synchronize_rcu,
  347. .cond_sync = cond_synchronize_rcu,
  348. .call = call_rcu,
  349. .cb_barrier = rcu_barrier,
  350. .fqs = rcu_force_quiescent_state,
  351. .stats = NULL,
  352. .irq_capable = 1,
  353. .can_boost = rcu_can_boost(),
  354. .name = "rcu"
  355. };
  356. /*
  357. * Definitions for rcu_bh torture testing.
  358. */
  359. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  360. {
  361. rcu_read_lock_bh();
  362. return 0;
  363. }
  364. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  365. {
  366. rcu_read_unlock_bh();
  367. }
  368. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  369. {
  370. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  371. }
  372. static struct rcu_torture_ops rcu_bh_ops = {
  373. .ttype = RCU_BH_FLAVOR,
  374. .init = rcu_sync_torture_init,
  375. .readlock = rcu_bh_torture_read_lock,
  376. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  377. .readunlock = rcu_bh_torture_read_unlock,
  378. .started = rcu_batches_started_bh,
  379. .completed = rcu_batches_completed_bh,
  380. .deferred_free = rcu_bh_torture_deferred_free,
  381. .sync = synchronize_rcu_bh,
  382. .exp_sync = synchronize_rcu_bh_expedited,
  383. .call = call_rcu_bh,
  384. .cb_barrier = rcu_barrier_bh,
  385. .fqs = rcu_bh_force_quiescent_state,
  386. .stats = NULL,
  387. .irq_capable = 1,
  388. .name = "rcu_bh"
  389. };
  390. /*
  391. * Don't even think about trying any of these in real life!!!
  392. * The names includes "busted", and they really means it!
  393. * The only purpose of these functions is to provide a buggy RCU
  394. * implementation to make sure that rcutorture correctly emits
  395. * buggy-RCU error messages.
  396. */
  397. static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
  398. {
  399. /* This is a deliberate bug for testing purposes only! */
  400. rcu_torture_cb(&p->rtort_rcu);
  401. }
  402. static void synchronize_rcu_busted(void)
  403. {
  404. /* This is a deliberate bug for testing purposes only! */
  405. }
  406. static void
  407. call_rcu_busted(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  408. {
  409. /* This is a deliberate bug for testing purposes only! */
  410. func(head);
  411. }
  412. static struct rcu_torture_ops rcu_busted_ops = {
  413. .ttype = INVALID_RCU_FLAVOR,
  414. .init = rcu_sync_torture_init,
  415. .readlock = rcu_torture_read_lock,
  416. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  417. .readunlock = rcu_torture_read_unlock,
  418. .started = rcu_no_completed,
  419. .completed = rcu_no_completed,
  420. .deferred_free = rcu_busted_torture_deferred_free,
  421. .sync = synchronize_rcu_busted,
  422. .exp_sync = synchronize_rcu_busted,
  423. .call = call_rcu_busted,
  424. .cb_barrier = NULL,
  425. .fqs = NULL,
  426. .stats = NULL,
  427. .irq_capable = 1,
  428. .name = "rcu_busted"
  429. };
  430. /*
  431. * Definitions for srcu torture testing.
  432. */
  433. DEFINE_STATIC_SRCU(srcu_ctl);
  434. static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
  435. {
  436. return srcu_read_lock(&srcu_ctl);
  437. }
  438. static void srcu_read_delay(struct torture_random_state *rrsp)
  439. {
  440. long delay;
  441. const long uspertick = 1000000 / HZ;
  442. const long longdelay = 10;
  443. /* We want there to be long-running readers, but not all the time. */
  444. delay = torture_random(rrsp) %
  445. (nrealreaders * 2 * longdelay * uspertick);
  446. if (!delay)
  447. schedule_timeout_interruptible(longdelay);
  448. else
  449. rcu_read_delay(rrsp);
  450. }
  451. static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
  452. {
  453. srcu_read_unlock(&srcu_ctl, idx);
  454. }
  455. static unsigned long srcu_torture_completed(void)
  456. {
  457. return srcu_batches_completed(&srcu_ctl);
  458. }
  459. static void srcu_torture_deferred_free(struct rcu_torture *rp)
  460. {
  461. call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
  462. }
  463. static void srcu_torture_synchronize(void)
  464. {
  465. synchronize_srcu(&srcu_ctl);
  466. }
  467. static void srcu_torture_call(struct rcu_head *head,
  468. void (*func)(struct rcu_head *head))
  469. {
  470. call_srcu(&srcu_ctl, head, func);
  471. }
  472. static void srcu_torture_barrier(void)
  473. {
  474. srcu_barrier(&srcu_ctl);
  475. }
  476. static void srcu_torture_stats(void)
  477. {
  478. int cpu;
  479. int idx = srcu_ctl.completed & 0x1;
  480. pr_alert("%s%s per-CPU(idx=%d):",
  481. torture_type, TORTURE_FLAG, idx);
  482. for_each_possible_cpu(cpu) {
  483. long c0, c1;
  484. c0 = (long)per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx];
  485. c1 = (long)per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx];
  486. pr_cont(" %d(%ld,%ld)", cpu, c0, c1);
  487. }
  488. pr_cont("\n");
  489. }
  490. static void srcu_torture_synchronize_expedited(void)
  491. {
  492. synchronize_srcu_expedited(&srcu_ctl);
  493. }
  494. static struct rcu_torture_ops srcu_ops = {
  495. .ttype = SRCU_FLAVOR,
  496. .init = rcu_sync_torture_init,
  497. .readlock = srcu_torture_read_lock,
  498. .read_delay = srcu_read_delay,
  499. .readunlock = srcu_torture_read_unlock,
  500. .started = NULL,
  501. .completed = srcu_torture_completed,
  502. .deferred_free = srcu_torture_deferred_free,
  503. .sync = srcu_torture_synchronize,
  504. .exp_sync = srcu_torture_synchronize_expedited,
  505. .call = srcu_torture_call,
  506. .cb_barrier = srcu_torture_barrier,
  507. .stats = srcu_torture_stats,
  508. .name = "srcu"
  509. };
  510. /*
  511. * Definitions for sched torture testing.
  512. */
  513. static int sched_torture_read_lock(void)
  514. {
  515. preempt_disable();
  516. return 0;
  517. }
  518. static void sched_torture_read_unlock(int idx)
  519. {
  520. preempt_enable();
  521. }
  522. static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
  523. {
  524. call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
  525. }
  526. static struct rcu_torture_ops sched_ops = {
  527. .ttype = RCU_SCHED_FLAVOR,
  528. .init = rcu_sync_torture_init,
  529. .readlock = sched_torture_read_lock,
  530. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  531. .readunlock = sched_torture_read_unlock,
  532. .started = rcu_batches_started_sched,
  533. .completed = rcu_batches_completed_sched,
  534. .deferred_free = rcu_sched_torture_deferred_free,
  535. .sync = synchronize_sched,
  536. .exp_sync = synchronize_sched_expedited,
  537. .call = call_rcu_sched,
  538. .cb_barrier = rcu_barrier_sched,
  539. .fqs = rcu_sched_force_quiescent_state,
  540. .stats = NULL,
  541. .irq_capable = 1,
  542. .name = "sched"
  543. };
  544. #ifdef CONFIG_TASKS_RCU
  545. /*
  546. * Definitions for RCU-tasks torture testing.
  547. */
  548. static int tasks_torture_read_lock(void)
  549. {
  550. return 0;
  551. }
  552. static void tasks_torture_read_unlock(int idx)
  553. {
  554. }
  555. static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
  556. {
  557. call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
  558. }
  559. static struct rcu_torture_ops tasks_ops = {
  560. .ttype = RCU_TASKS_FLAVOR,
  561. .init = rcu_sync_torture_init,
  562. .readlock = tasks_torture_read_lock,
  563. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  564. .readunlock = tasks_torture_read_unlock,
  565. .started = rcu_no_completed,
  566. .completed = rcu_no_completed,
  567. .deferred_free = rcu_tasks_torture_deferred_free,
  568. .sync = synchronize_rcu_tasks,
  569. .exp_sync = synchronize_rcu_tasks,
  570. .call = call_rcu_tasks,
  571. .cb_barrier = rcu_barrier_tasks,
  572. .fqs = NULL,
  573. .stats = NULL,
  574. .irq_capable = 1,
  575. .name = "tasks"
  576. };
  577. #define RCUTORTURE_TASKS_OPS &tasks_ops,
  578. #else /* #ifdef CONFIG_TASKS_RCU */
  579. #define RCUTORTURE_TASKS_OPS
  580. #endif /* #else #ifdef CONFIG_TASKS_RCU */
  581. /*
  582. * RCU torture priority-boost testing. Runs one real-time thread per
  583. * CPU for moderate bursts, repeatedly registering RCU callbacks and
  584. * spinning waiting for them to be invoked. If a given callback takes
  585. * too long to be invoked, we assume that priority inversion has occurred.
  586. */
  587. struct rcu_boost_inflight {
  588. struct rcu_head rcu;
  589. int inflight;
  590. };
  591. static void rcu_torture_boost_cb(struct rcu_head *head)
  592. {
  593. struct rcu_boost_inflight *rbip =
  594. container_of(head, struct rcu_boost_inflight, rcu);
  595. smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
  596. rbip->inflight = 0;
  597. }
  598. static int rcu_torture_boost(void *arg)
  599. {
  600. unsigned long call_rcu_time;
  601. unsigned long endtime;
  602. unsigned long oldstarttime;
  603. struct rcu_boost_inflight rbi = { .inflight = 0 };
  604. struct sched_param sp;
  605. VERBOSE_TOROUT_STRING("rcu_torture_boost started");
  606. /* Set real-time priority. */
  607. sp.sched_priority = 1;
  608. if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
  609. VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
  610. n_rcu_torture_boost_rterror++;
  611. }
  612. init_rcu_head_on_stack(&rbi.rcu);
  613. /* Each pass through the following loop does one boost-test cycle. */
  614. do {
  615. /* Wait for the next test interval. */
  616. oldstarttime = boost_starttime;
  617. while (ULONG_CMP_LT(jiffies, oldstarttime)) {
  618. schedule_timeout_interruptible(oldstarttime - jiffies);
  619. stutter_wait("rcu_torture_boost");
  620. if (torture_must_stop())
  621. goto checkwait;
  622. }
  623. /* Do one boost-test interval. */
  624. endtime = oldstarttime + test_boost_duration * HZ;
  625. call_rcu_time = jiffies;
  626. while (ULONG_CMP_LT(jiffies, endtime)) {
  627. /* If we don't have a callback in flight, post one. */
  628. if (!rbi.inflight) {
  629. smp_mb(); /* RCU core before ->inflight = 1. */
  630. rbi.inflight = 1;
  631. call_rcu(&rbi.rcu, rcu_torture_boost_cb);
  632. if (jiffies - call_rcu_time >
  633. test_boost_duration * HZ - HZ / 2) {
  634. VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
  635. n_rcu_torture_boost_failure++;
  636. }
  637. call_rcu_time = jiffies;
  638. }
  639. cond_resched_rcu_qs();
  640. stutter_wait("rcu_torture_boost");
  641. if (torture_must_stop())
  642. goto checkwait;
  643. }
  644. /*
  645. * Set the start time of the next test interval.
  646. * Yes, this is vulnerable to long delays, but such
  647. * delays simply cause a false negative for the next
  648. * interval. Besides, we are running at RT priority,
  649. * so delays should be relatively rare.
  650. */
  651. while (oldstarttime == boost_starttime &&
  652. !kthread_should_stop()) {
  653. if (mutex_trylock(&boost_mutex)) {
  654. boost_starttime = jiffies +
  655. test_boost_interval * HZ;
  656. n_rcu_torture_boosts++;
  657. mutex_unlock(&boost_mutex);
  658. break;
  659. }
  660. schedule_timeout_uninterruptible(1);
  661. }
  662. /* Go do the stutter. */
  663. checkwait: stutter_wait("rcu_torture_boost");
  664. } while (!torture_must_stop());
  665. /* Clean up and exit. */
  666. while (!kthread_should_stop() || rbi.inflight) {
  667. torture_shutdown_absorb("rcu_torture_boost");
  668. schedule_timeout_uninterruptible(1);
  669. }
  670. smp_mb(); /* order accesses to ->inflight before stack-frame death. */
  671. destroy_rcu_head_on_stack(&rbi.rcu);
  672. torture_kthread_stopping("rcu_torture_boost");
  673. return 0;
  674. }
  675. static void rcu_torture_cbflood_cb(struct rcu_head *rhp)
  676. {
  677. }
  678. /*
  679. * RCU torture callback-flood kthread. Repeatedly induces bursts of calls
  680. * to call_rcu() or analogous, increasing the probability of occurrence
  681. * of callback-overflow corner cases.
  682. */
  683. static int
  684. rcu_torture_cbflood(void *arg)
  685. {
  686. int err = 1;
  687. int i;
  688. int j;
  689. struct rcu_head *rhp;
  690. if (cbflood_n_per_burst > 0 &&
  691. cbflood_inter_holdoff > 0 &&
  692. cbflood_intra_holdoff > 0 &&
  693. cur_ops->call &&
  694. cur_ops->cb_barrier) {
  695. rhp = vmalloc(sizeof(*rhp) *
  696. cbflood_n_burst * cbflood_n_per_burst);
  697. err = !rhp;
  698. }
  699. if (err) {
  700. VERBOSE_TOROUT_STRING("rcu_torture_cbflood disabled: Bad args or OOM");
  701. while (!torture_must_stop())
  702. schedule_timeout_interruptible(HZ);
  703. return 0;
  704. }
  705. VERBOSE_TOROUT_STRING("rcu_torture_cbflood task started");
  706. do {
  707. schedule_timeout_interruptible(cbflood_inter_holdoff);
  708. atomic_long_inc(&n_cbfloods);
  709. WARN_ON(signal_pending(current));
  710. for (i = 0; i < cbflood_n_burst; i++) {
  711. for (j = 0; j < cbflood_n_per_burst; j++) {
  712. cur_ops->call(&rhp[i * cbflood_n_per_burst + j],
  713. rcu_torture_cbflood_cb);
  714. }
  715. schedule_timeout_interruptible(cbflood_intra_holdoff);
  716. WARN_ON(signal_pending(current));
  717. }
  718. cur_ops->cb_barrier();
  719. stutter_wait("rcu_torture_cbflood");
  720. } while (!torture_must_stop());
  721. vfree(rhp);
  722. torture_kthread_stopping("rcu_torture_cbflood");
  723. return 0;
  724. }
  725. /*
  726. * RCU torture force-quiescent-state kthread. Repeatedly induces
  727. * bursts of calls to force_quiescent_state(), increasing the probability
  728. * of occurrence of some important types of race conditions.
  729. */
  730. static int
  731. rcu_torture_fqs(void *arg)
  732. {
  733. unsigned long fqs_resume_time;
  734. int fqs_burst_remaining;
  735. VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
  736. do {
  737. fqs_resume_time = jiffies + fqs_stutter * HZ;
  738. while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
  739. !kthread_should_stop()) {
  740. schedule_timeout_interruptible(1);
  741. }
  742. fqs_burst_remaining = fqs_duration;
  743. while (fqs_burst_remaining > 0 &&
  744. !kthread_should_stop()) {
  745. cur_ops->fqs();
  746. udelay(fqs_holdoff);
  747. fqs_burst_remaining -= fqs_holdoff;
  748. }
  749. stutter_wait("rcu_torture_fqs");
  750. } while (!torture_must_stop());
  751. torture_kthread_stopping("rcu_torture_fqs");
  752. return 0;
  753. }
  754. /*
  755. * RCU torture writer kthread. Repeatedly substitutes a new structure
  756. * for that pointed to by rcu_torture_current, freeing the old structure
  757. * after a series of grace periods (the "pipeline").
  758. */
  759. static int
  760. rcu_torture_writer(void *arg)
  761. {
  762. unsigned long gp_snap;
  763. bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
  764. bool gp_sync1 = gp_sync;
  765. int i;
  766. struct rcu_torture *rp;
  767. struct rcu_torture *old_rp;
  768. static DEFINE_TORTURE_RANDOM(rand);
  769. int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
  770. RTWS_COND_GET, RTWS_SYNC };
  771. int nsynctypes = 0;
  772. VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
  773. /* Initialize synctype[] array. If none set, take default. */
  774. if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync)
  775. gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
  776. if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync)
  777. synctype[nsynctypes++] = RTWS_COND_GET;
  778. else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync))
  779. pr_alert("rcu_torture_writer: gp_cond without primitives.\n");
  780. if (gp_exp1 && cur_ops->exp_sync)
  781. synctype[nsynctypes++] = RTWS_EXP_SYNC;
  782. else if (gp_exp && !cur_ops->exp_sync)
  783. pr_alert("rcu_torture_writer: gp_exp without primitives.\n");
  784. if (gp_normal1 && cur_ops->deferred_free)
  785. synctype[nsynctypes++] = RTWS_DEF_FREE;
  786. else if (gp_normal && !cur_ops->deferred_free)
  787. pr_alert("rcu_torture_writer: gp_normal without primitives.\n");
  788. if (gp_sync1 && cur_ops->sync)
  789. synctype[nsynctypes++] = RTWS_SYNC;
  790. else if (gp_sync && !cur_ops->sync)
  791. pr_alert("rcu_torture_writer: gp_sync without primitives.\n");
  792. if (WARN_ONCE(nsynctypes == 0,
  793. "rcu_torture_writer: No update-side primitives.\n")) {
  794. /*
  795. * No updates primitives, so don't try updating.
  796. * The resulting test won't be testing much, hence the
  797. * above WARN_ONCE().
  798. */
  799. rcu_torture_writer_state = RTWS_STOPPING;
  800. torture_kthread_stopping("rcu_torture_writer");
  801. }
  802. do {
  803. rcu_torture_writer_state = RTWS_FIXED_DELAY;
  804. schedule_timeout_uninterruptible(1);
  805. rp = rcu_torture_alloc();
  806. if (rp == NULL)
  807. continue;
  808. rp->rtort_pipe_count = 0;
  809. rcu_torture_writer_state = RTWS_DELAY;
  810. udelay(torture_random(&rand) & 0x3ff);
  811. rcu_torture_writer_state = RTWS_REPLACE;
  812. old_rp = rcu_dereference_check(rcu_torture_current,
  813. current == writer_task);
  814. rp->rtort_mbtest = 1;
  815. rcu_assign_pointer(rcu_torture_current, rp);
  816. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  817. if (old_rp) {
  818. i = old_rp->rtort_pipe_count;
  819. if (i > RCU_TORTURE_PIPE_LEN)
  820. i = RCU_TORTURE_PIPE_LEN;
  821. atomic_inc(&rcu_torture_wcount[i]);
  822. old_rp->rtort_pipe_count++;
  823. switch (synctype[torture_random(&rand) % nsynctypes]) {
  824. case RTWS_DEF_FREE:
  825. rcu_torture_writer_state = RTWS_DEF_FREE;
  826. cur_ops->deferred_free(old_rp);
  827. break;
  828. case RTWS_EXP_SYNC:
  829. rcu_torture_writer_state = RTWS_EXP_SYNC;
  830. cur_ops->exp_sync();
  831. rcu_torture_pipe_update(old_rp);
  832. break;
  833. case RTWS_COND_GET:
  834. rcu_torture_writer_state = RTWS_COND_GET;
  835. gp_snap = cur_ops->get_state();
  836. i = torture_random(&rand) % 16;
  837. if (i != 0)
  838. schedule_timeout_interruptible(i);
  839. udelay(torture_random(&rand) % 1000);
  840. rcu_torture_writer_state = RTWS_COND_SYNC;
  841. cur_ops->cond_sync(gp_snap);
  842. rcu_torture_pipe_update(old_rp);
  843. break;
  844. case RTWS_SYNC:
  845. rcu_torture_writer_state = RTWS_SYNC;
  846. cur_ops->sync();
  847. rcu_torture_pipe_update(old_rp);
  848. break;
  849. default:
  850. WARN_ON_ONCE(1);
  851. break;
  852. }
  853. }
  854. rcutorture_record_progress(++rcu_torture_current_version);
  855. rcu_torture_writer_state = RTWS_STUTTER;
  856. stutter_wait("rcu_torture_writer");
  857. } while (!torture_must_stop());
  858. rcu_torture_writer_state = RTWS_STOPPING;
  859. torture_kthread_stopping("rcu_torture_writer");
  860. return 0;
  861. }
  862. /*
  863. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  864. * delay between calls.
  865. */
  866. static int
  867. rcu_torture_fakewriter(void *arg)
  868. {
  869. DEFINE_TORTURE_RANDOM(rand);
  870. VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
  871. set_user_nice(current, MAX_NICE);
  872. do {
  873. schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
  874. udelay(torture_random(&rand) & 0x3ff);
  875. if (cur_ops->cb_barrier != NULL &&
  876. torture_random(&rand) % (nfakewriters * 8) == 0) {
  877. cur_ops->cb_barrier();
  878. } else if (gp_normal == gp_exp) {
  879. if (torture_random(&rand) & 0x80)
  880. cur_ops->sync();
  881. else
  882. cur_ops->exp_sync();
  883. } else if (gp_normal) {
  884. cur_ops->sync();
  885. } else {
  886. cur_ops->exp_sync();
  887. }
  888. stutter_wait("rcu_torture_fakewriter");
  889. } while (!torture_must_stop());
  890. torture_kthread_stopping("rcu_torture_fakewriter");
  891. return 0;
  892. }
  893. static void rcutorture_trace_dump(void)
  894. {
  895. static atomic_t beenhere = ATOMIC_INIT(0);
  896. if (atomic_read(&beenhere))
  897. return;
  898. if (atomic_xchg(&beenhere, 1) != 0)
  899. return;
  900. ftrace_dump(DUMP_ALL);
  901. }
  902. /*
  903. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  904. * incrementing the corresponding element of the pipeline array. The
  905. * counter in the element should never be greater than 1, otherwise, the
  906. * RCU implementation is broken.
  907. */
  908. static void rcu_torture_timer(unsigned long unused)
  909. {
  910. int idx;
  911. unsigned long started;
  912. unsigned long completed;
  913. static DEFINE_TORTURE_RANDOM(rand);
  914. static DEFINE_SPINLOCK(rand_lock);
  915. struct rcu_torture *p;
  916. int pipe_count;
  917. unsigned long long ts;
  918. idx = cur_ops->readlock();
  919. if (cur_ops->started)
  920. started = cur_ops->started();
  921. else
  922. started = cur_ops->completed();
  923. ts = rcu_trace_clock_local();
  924. p = rcu_dereference_check(rcu_torture_current,
  925. rcu_read_lock_bh_held() ||
  926. rcu_read_lock_sched_held() ||
  927. srcu_read_lock_held(&srcu_ctl));
  928. if (p == NULL) {
  929. /* Leave because rcu_torture_writer is not yet underway */
  930. cur_ops->readunlock(idx);
  931. return;
  932. }
  933. if (p->rtort_mbtest == 0)
  934. atomic_inc(&n_rcu_torture_mberror);
  935. spin_lock(&rand_lock);
  936. cur_ops->read_delay(&rand);
  937. n_rcu_torture_timers++;
  938. spin_unlock(&rand_lock);
  939. preempt_disable();
  940. pipe_count = p->rtort_pipe_count;
  941. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  942. /* Should not happen, but... */
  943. pipe_count = RCU_TORTURE_PIPE_LEN;
  944. }
  945. completed = cur_ops->completed();
  946. if (pipe_count > 1) {
  947. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
  948. started, completed);
  949. rcutorture_trace_dump();
  950. }
  951. __this_cpu_inc(rcu_torture_count[pipe_count]);
  952. completed = completed - started;
  953. if (cur_ops->started)
  954. completed++;
  955. if (completed > RCU_TORTURE_PIPE_LEN) {
  956. /* Should not happen, but... */
  957. completed = RCU_TORTURE_PIPE_LEN;
  958. }
  959. __this_cpu_inc(rcu_torture_batch[completed]);
  960. preempt_enable();
  961. cur_ops->readunlock(idx);
  962. }
  963. /*
  964. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  965. * incrementing the corresponding element of the pipeline array. The
  966. * counter in the element should never be greater than 1, otherwise, the
  967. * RCU implementation is broken.
  968. */
  969. static int
  970. rcu_torture_reader(void *arg)
  971. {
  972. unsigned long started;
  973. unsigned long completed;
  974. int idx;
  975. DEFINE_TORTURE_RANDOM(rand);
  976. struct rcu_torture *p;
  977. int pipe_count;
  978. struct timer_list t;
  979. unsigned long long ts;
  980. VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
  981. set_user_nice(current, MAX_NICE);
  982. if (irqreader && cur_ops->irq_capable)
  983. setup_timer_on_stack(&t, rcu_torture_timer, 0);
  984. do {
  985. if (irqreader && cur_ops->irq_capable) {
  986. if (!timer_pending(&t))
  987. mod_timer(&t, jiffies + 1);
  988. }
  989. idx = cur_ops->readlock();
  990. if (cur_ops->started)
  991. started = cur_ops->started();
  992. else
  993. started = cur_ops->completed();
  994. ts = rcu_trace_clock_local();
  995. p = rcu_dereference_check(rcu_torture_current,
  996. rcu_read_lock_bh_held() ||
  997. rcu_read_lock_sched_held() ||
  998. srcu_read_lock_held(&srcu_ctl));
  999. if (p == NULL) {
  1000. /* Wait for rcu_torture_writer to get underway */
  1001. cur_ops->readunlock(idx);
  1002. schedule_timeout_interruptible(HZ);
  1003. continue;
  1004. }
  1005. if (p->rtort_mbtest == 0)
  1006. atomic_inc(&n_rcu_torture_mberror);
  1007. cur_ops->read_delay(&rand);
  1008. preempt_disable();
  1009. pipe_count = p->rtort_pipe_count;
  1010. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  1011. /* Should not happen, but... */
  1012. pipe_count = RCU_TORTURE_PIPE_LEN;
  1013. }
  1014. completed = cur_ops->completed();
  1015. if (pipe_count > 1) {
  1016. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
  1017. ts, started, completed);
  1018. rcutorture_trace_dump();
  1019. }
  1020. __this_cpu_inc(rcu_torture_count[pipe_count]);
  1021. completed = completed - started;
  1022. if (cur_ops->started)
  1023. completed++;
  1024. if (completed > RCU_TORTURE_PIPE_LEN) {
  1025. /* Should not happen, but... */
  1026. completed = RCU_TORTURE_PIPE_LEN;
  1027. }
  1028. __this_cpu_inc(rcu_torture_batch[completed]);
  1029. preempt_enable();
  1030. cur_ops->readunlock(idx);
  1031. cond_resched_rcu_qs();
  1032. stutter_wait("rcu_torture_reader");
  1033. } while (!torture_must_stop());
  1034. if (irqreader && cur_ops->irq_capable) {
  1035. del_timer_sync(&t);
  1036. destroy_timer_on_stack(&t);
  1037. }
  1038. torture_kthread_stopping("rcu_torture_reader");
  1039. return 0;
  1040. }
  1041. /*
  1042. * Print torture statistics. Caller must ensure that there is only
  1043. * one call to this function at a given time!!! This is normally
  1044. * accomplished by relying on the module system to only have one copy
  1045. * of the module loaded, and then by giving the rcu_torture_stats
  1046. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  1047. * thread is not running).
  1048. */
  1049. static void
  1050. rcu_torture_stats_print(void)
  1051. {
  1052. int cpu;
  1053. int i;
  1054. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  1055. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  1056. static unsigned long rtcv_snap = ULONG_MAX;
  1057. for_each_possible_cpu(cpu) {
  1058. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1059. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  1060. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  1061. }
  1062. }
  1063. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  1064. if (pipesummary[i] != 0)
  1065. break;
  1066. }
  1067. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  1068. pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
  1069. rcu_torture_current,
  1070. rcu_torture_current_version,
  1071. list_empty(&rcu_torture_freelist),
  1072. atomic_read(&n_rcu_torture_alloc),
  1073. atomic_read(&n_rcu_torture_alloc_fail),
  1074. atomic_read(&n_rcu_torture_free));
  1075. pr_cont("rtmbe: %d rtbke: %ld rtbre: %ld ",
  1076. atomic_read(&n_rcu_torture_mberror),
  1077. n_rcu_torture_boost_ktrerror,
  1078. n_rcu_torture_boost_rterror);
  1079. pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
  1080. n_rcu_torture_boost_failure,
  1081. n_rcu_torture_boosts,
  1082. n_rcu_torture_timers);
  1083. torture_onoff_stats();
  1084. pr_cont("barrier: %ld/%ld:%ld ",
  1085. n_barrier_successes,
  1086. n_barrier_attempts,
  1087. n_rcu_torture_barrier_error);
  1088. pr_cont("cbflood: %ld\n", atomic_long_read(&n_cbfloods));
  1089. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  1090. if (atomic_read(&n_rcu_torture_mberror) != 0 ||
  1091. n_rcu_torture_barrier_error != 0 ||
  1092. n_rcu_torture_boost_ktrerror != 0 ||
  1093. n_rcu_torture_boost_rterror != 0 ||
  1094. n_rcu_torture_boost_failure != 0 ||
  1095. i > 1) {
  1096. pr_cont("%s", "!!! ");
  1097. atomic_inc(&n_rcu_torture_error);
  1098. WARN_ON_ONCE(1);
  1099. }
  1100. pr_cont("Reader Pipe: ");
  1101. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1102. pr_cont(" %ld", pipesummary[i]);
  1103. pr_cont("\n");
  1104. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  1105. pr_cont("Reader Batch: ");
  1106. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1107. pr_cont(" %ld", batchsummary[i]);
  1108. pr_cont("\n");
  1109. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  1110. pr_cont("Free-Block Circulation: ");
  1111. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1112. pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
  1113. }
  1114. pr_cont("\n");
  1115. if (cur_ops->stats)
  1116. cur_ops->stats();
  1117. if (rtcv_snap == rcu_torture_current_version &&
  1118. rcu_torture_current != NULL) {
  1119. int __maybe_unused flags;
  1120. unsigned long __maybe_unused gpnum;
  1121. unsigned long __maybe_unused completed;
  1122. rcutorture_get_gp_data(cur_ops->ttype,
  1123. &flags, &gpnum, &completed);
  1124. pr_alert("??? Writer stall state %d g%lu c%lu f%#x\n",
  1125. rcu_torture_writer_state,
  1126. gpnum, completed, flags);
  1127. show_rcu_gp_kthreads();
  1128. rcutorture_trace_dump();
  1129. }
  1130. rtcv_snap = rcu_torture_current_version;
  1131. }
  1132. /*
  1133. * Periodically prints torture statistics, if periodic statistics printing
  1134. * was specified via the stat_interval module parameter.
  1135. */
  1136. static int
  1137. rcu_torture_stats(void *arg)
  1138. {
  1139. VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
  1140. do {
  1141. schedule_timeout_interruptible(stat_interval * HZ);
  1142. rcu_torture_stats_print();
  1143. torture_shutdown_absorb("rcu_torture_stats");
  1144. } while (!torture_must_stop());
  1145. torture_kthread_stopping("rcu_torture_stats");
  1146. return 0;
  1147. }
  1148. static inline void
  1149. rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
  1150. {
  1151. pr_alert("%s" TORTURE_FLAG
  1152. "--- %s: nreaders=%d nfakewriters=%d "
  1153. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  1154. "shuffle_interval=%d stutter=%d irqreader=%d "
  1155. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
  1156. "test_boost=%d/%d test_boost_interval=%d "
  1157. "test_boost_duration=%d shutdown_secs=%d "
  1158. "stall_cpu=%d stall_cpu_holdoff=%d "
  1159. "n_barrier_cbs=%d "
  1160. "onoff_interval=%d onoff_holdoff=%d\n",
  1161. torture_type, tag, nrealreaders, nfakewriters,
  1162. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  1163. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
  1164. test_boost, cur_ops->can_boost,
  1165. test_boost_interval, test_boost_duration, shutdown_secs,
  1166. stall_cpu, stall_cpu_holdoff,
  1167. n_barrier_cbs,
  1168. onoff_interval, onoff_holdoff);
  1169. }
  1170. static void rcutorture_booster_cleanup(int cpu)
  1171. {
  1172. struct task_struct *t;
  1173. if (boost_tasks[cpu] == NULL)
  1174. return;
  1175. mutex_lock(&boost_mutex);
  1176. t = boost_tasks[cpu];
  1177. boost_tasks[cpu] = NULL;
  1178. mutex_unlock(&boost_mutex);
  1179. /* This must be outside of the mutex, otherwise deadlock! */
  1180. torture_stop_kthread(rcu_torture_boost, t);
  1181. }
  1182. static int rcutorture_booster_init(int cpu)
  1183. {
  1184. int retval;
  1185. if (boost_tasks[cpu] != NULL)
  1186. return 0; /* Already created, nothing more to do. */
  1187. /* Don't allow time recalculation while creating a new task. */
  1188. mutex_lock(&boost_mutex);
  1189. VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
  1190. boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
  1191. cpu_to_node(cpu),
  1192. "rcu_torture_boost");
  1193. if (IS_ERR(boost_tasks[cpu])) {
  1194. retval = PTR_ERR(boost_tasks[cpu]);
  1195. VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
  1196. n_rcu_torture_boost_ktrerror++;
  1197. boost_tasks[cpu] = NULL;
  1198. mutex_unlock(&boost_mutex);
  1199. return retval;
  1200. }
  1201. kthread_bind(boost_tasks[cpu], cpu);
  1202. wake_up_process(boost_tasks[cpu]);
  1203. mutex_unlock(&boost_mutex);
  1204. return 0;
  1205. }
  1206. /*
  1207. * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
  1208. * induces a CPU stall for the time specified by stall_cpu.
  1209. */
  1210. static int rcu_torture_stall(void *args)
  1211. {
  1212. unsigned long stop_at;
  1213. VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
  1214. if (stall_cpu_holdoff > 0) {
  1215. VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
  1216. schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
  1217. VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
  1218. }
  1219. if (!kthread_should_stop()) {
  1220. stop_at = get_seconds() + stall_cpu;
  1221. /* RCU CPU stall is expected behavior in following code. */
  1222. pr_alert("rcu_torture_stall start.\n");
  1223. rcu_read_lock();
  1224. preempt_disable();
  1225. while (ULONG_CMP_LT(get_seconds(), stop_at))
  1226. continue; /* Induce RCU CPU stall warning. */
  1227. preempt_enable();
  1228. rcu_read_unlock();
  1229. pr_alert("rcu_torture_stall end.\n");
  1230. }
  1231. torture_shutdown_absorb("rcu_torture_stall");
  1232. while (!kthread_should_stop())
  1233. schedule_timeout_interruptible(10 * HZ);
  1234. return 0;
  1235. }
  1236. /* Spawn CPU-stall kthread, if stall_cpu specified. */
  1237. static int __init rcu_torture_stall_init(void)
  1238. {
  1239. if (stall_cpu <= 0)
  1240. return 0;
  1241. return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
  1242. }
  1243. /* Callback function for RCU barrier testing. */
  1244. static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
  1245. {
  1246. atomic_inc(&barrier_cbs_invoked);
  1247. }
  1248. /* kthread function to register callbacks used to test RCU barriers. */
  1249. static int rcu_torture_barrier_cbs(void *arg)
  1250. {
  1251. long myid = (long)arg;
  1252. bool lastphase = 0;
  1253. bool newphase;
  1254. struct rcu_head rcu;
  1255. init_rcu_head_on_stack(&rcu);
  1256. VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
  1257. set_user_nice(current, MAX_NICE);
  1258. do {
  1259. wait_event(barrier_cbs_wq[myid],
  1260. (newphase =
  1261. ACCESS_ONCE(barrier_phase)) != lastphase ||
  1262. torture_must_stop());
  1263. lastphase = newphase;
  1264. smp_mb(); /* ensure barrier_phase load before ->call(). */
  1265. if (torture_must_stop())
  1266. break;
  1267. cur_ops->call(&rcu, rcu_torture_barrier_cbf);
  1268. if (atomic_dec_and_test(&barrier_cbs_count))
  1269. wake_up(&barrier_wq);
  1270. } while (!torture_must_stop());
  1271. if (cur_ops->cb_barrier != NULL)
  1272. cur_ops->cb_barrier();
  1273. destroy_rcu_head_on_stack(&rcu);
  1274. torture_kthread_stopping("rcu_torture_barrier_cbs");
  1275. return 0;
  1276. }
  1277. /* kthread function to drive and coordinate RCU barrier testing. */
  1278. static int rcu_torture_barrier(void *arg)
  1279. {
  1280. int i;
  1281. VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
  1282. do {
  1283. atomic_set(&barrier_cbs_invoked, 0);
  1284. atomic_set(&barrier_cbs_count, n_barrier_cbs);
  1285. smp_mb(); /* Ensure barrier_phase after prior assignments. */
  1286. barrier_phase = !barrier_phase;
  1287. for (i = 0; i < n_barrier_cbs; i++)
  1288. wake_up(&barrier_cbs_wq[i]);
  1289. wait_event(barrier_wq,
  1290. atomic_read(&barrier_cbs_count) == 0 ||
  1291. torture_must_stop());
  1292. if (torture_must_stop())
  1293. break;
  1294. n_barrier_attempts++;
  1295. cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
  1296. if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
  1297. n_rcu_torture_barrier_error++;
  1298. pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
  1299. atomic_read(&barrier_cbs_invoked),
  1300. n_barrier_cbs);
  1301. WARN_ON_ONCE(1);
  1302. }
  1303. n_barrier_successes++;
  1304. schedule_timeout_interruptible(HZ / 10);
  1305. } while (!torture_must_stop());
  1306. torture_kthread_stopping("rcu_torture_barrier");
  1307. return 0;
  1308. }
  1309. /* Initialize RCU barrier testing. */
  1310. static int rcu_torture_barrier_init(void)
  1311. {
  1312. int i;
  1313. int ret;
  1314. if (n_barrier_cbs == 0)
  1315. return 0;
  1316. if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
  1317. pr_alert("%s" TORTURE_FLAG
  1318. " Call or barrier ops missing for %s,\n",
  1319. torture_type, cur_ops->name);
  1320. pr_alert("%s" TORTURE_FLAG
  1321. " RCU barrier testing omitted from run.\n",
  1322. torture_type);
  1323. return 0;
  1324. }
  1325. atomic_set(&barrier_cbs_count, 0);
  1326. atomic_set(&barrier_cbs_invoked, 0);
  1327. barrier_cbs_tasks =
  1328. kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
  1329. GFP_KERNEL);
  1330. barrier_cbs_wq =
  1331. kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
  1332. GFP_KERNEL);
  1333. if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
  1334. return -ENOMEM;
  1335. for (i = 0; i < n_barrier_cbs; i++) {
  1336. init_waitqueue_head(&barrier_cbs_wq[i]);
  1337. ret = torture_create_kthread(rcu_torture_barrier_cbs,
  1338. (void *)(long)i,
  1339. barrier_cbs_tasks[i]);
  1340. if (ret)
  1341. return ret;
  1342. }
  1343. return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
  1344. }
  1345. /* Clean up after RCU barrier testing. */
  1346. static void rcu_torture_barrier_cleanup(void)
  1347. {
  1348. int i;
  1349. torture_stop_kthread(rcu_torture_barrier, barrier_task);
  1350. if (barrier_cbs_tasks != NULL) {
  1351. for (i = 0; i < n_barrier_cbs; i++)
  1352. torture_stop_kthread(rcu_torture_barrier_cbs,
  1353. barrier_cbs_tasks[i]);
  1354. kfree(barrier_cbs_tasks);
  1355. barrier_cbs_tasks = NULL;
  1356. }
  1357. if (barrier_cbs_wq != NULL) {
  1358. kfree(barrier_cbs_wq);
  1359. barrier_cbs_wq = NULL;
  1360. }
  1361. }
  1362. static int rcutorture_cpu_notify(struct notifier_block *self,
  1363. unsigned long action, void *hcpu)
  1364. {
  1365. long cpu = (long)hcpu;
  1366. switch (action) {
  1367. case CPU_ONLINE:
  1368. case CPU_DOWN_FAILED:
  1369. (void)rcutorture_booster_init(cpu);
  1370. break;
  1371. case CPU_DOWN_PREPARE:
  1372. rcutorture_booster_cleanup(cpu);
  1373. break;
  1374. default:
  1375. break;
  1376. }
  1377. return NOTIFY_OK;
  1378. }
  1379. static struct notifier_block rcutorture_cpu_nb = {
  1380. .notifier_call = rcutorture_cpu_notify,
  1381. };
  1382. static void
  1383. rcu_torture_cleanup(void)
  1384. {
  1385. int i;
  1386. rcutorture_record_test_transition();
  1387. if (torture_cleanup_begin()) {
  1388. if (cur_ops->cb_barrier != NULL)
  1389. cur_ops->cb_barrier();
  1390. return;
  1391. }
  1392. rcu_torture_barrier_cleanup();
  1393. torture_stop_kthread(rcu_torture_stall, stall_task);
  1394. torture_stop_kthread(rcu_torture_writer, writer_task);
  1395. if (reader_tasks) {
  1396. for (i = 0; i < nrealreaders; i++)
  1397. torture_stop_kthread(rcu_torture_reader,
  1398. reader_tasks[i]);
  1399. kfree(reader_tasks);
  1400. }
  1401. rcu_torture_current = NULL;
  1402. if (fakewriter_tasks) {
  1403. for (i = 0; i < nfakewriters; i++) {
  1404. torture_stop_kthread(rcu_torture_fakewriter,
  1405. fakewriter_tasks[i]);
  1406. }
  1407. kfree(fakewriter_tasks);
  1408. fakewriter_tasks = NULL;
  1409. }
  1410. torture_stop_kthread(rcu_torture_stats, stats_task);
  1411. torture_stop_kthread(rcu_torture_fqs, fqs_task);
  1412. for (i = 0; i < ncbflooders; i++)
  1413. torture_stop_kthread(rcu_torture_cbflood, cbflood_task[i]);
  1414. if ((test_boost == 1 && cur_ops->can_boost) ||
  1415. test_boost == 2) {
  1416. unregister_cpu_notifier(&rcutorture_cpu_nb);
  1417. for_each_possible_cpu(i)
  1418. rcutorture_booster_cleanup(i);
  1419. }
  1420. /* Wait for all RCU callbacks to fire. */
  1421. if (cur_ops->cb_barrier != NULL)
  1422. cur_ops->cb_barrier();
  1423. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  1424. if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
  1425. rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
  1426. else if (torture_onoff_failures())
  1427. rcu_torture_print_module_parms(cur_ops,
  1428. "End of test: RCU_HOTPLUG");
  1429. else
  1430. rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
  1431. torture_cleanup_end();
  1432. }
  1433. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  1434. static void rcu_torture_leak_cb(struct rcu_head *rhp)
  1435. {
  1436. }
  1437. static void rcu_torture_err_cb(struct rcu_head *rhp)
  1438. {
  1439. /*
  1440. * This -might- happen due to race conditions, but is unlikely.
  1441. * The scenario that leads to this happening is that the
  1442. * first of the pair of duplicate callbacks is queued,
  1443. * someone else starts a grace period that includes that
  1444. * callback, then the second of the pair must wait for the
  1445. * next grace period. Unlikely, but can happen. If it
  1446. * does happen, the debug-objects subsystem won't have splatted.
  1447. */
  1448. pr_alert("rcutorture: duplicated callback was invoked.\n");
  1449. }
  1450. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1451. /*
  1452. * Verify that double-free causes debug-objects to complain, but only
  1453. * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
  1454. * cannot be carried out.
  1455. */
  1456. static void rcu_test_debug_objects(void)
  1457. {
  1458. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  1459. struct rcu_head rh1;
  1460. struct rcu_head rh2;
  1461. init_rcu_head_on_stack(&rh1);
  1462. init_rcu_head_on_stack(&rh2);
  1463. pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
  1464. /* Try to queue the rh2 pair of callbacks for the same grace period. */
  1465. preempt_disable(); /* Prevent preemption from interrupting test. */
  1466. rcu_read_lock(); /* Make it impossible to finish a grace period. */
  1467. call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
  1468. local_irq_disable(); /* Make it harder to start a new grace period. */
  1469. call_rcu(&rh2, rcu_torture_leak_cb);
  1470. call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
  1471. local_irq_enable();
  1472. rcu_read_unlock();
  1473. preempt_enable();
  1474. /* Wait for them all to get done so we can safely return. */
  1475. rcu_barrier();
  1476. pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
  1477. destroy_rcu_head_on_stack(&rh1);
  1478. destroy_rcu_head_on_stack(&rh2);
  1479. #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1480. pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
  1481. #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1482. }
  1483. static int __init
  1484. rcu_torture_init(void)
  1485. {
  1486. int i;
  1487. int cpu;
  1488. int firsterr = 0;
  1489. static struct rcu_torture_ops *torture_ops[] = {
  1490. &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &sched_ops,
  1491. RCUTORTURE_TASKS_OPS
  1492. };
  1493. if (!torture_init_begin(torture_type, verbose, &torture_runnable))
  1494. return -EBUSY;
  1495. /* Process args and tell the world that the torturer is on the job. */
  1496. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1497. cur_ops = torture_ops[i];
  1498. if (strcmp(torture_type, cur_ops->name) == 0)
  1499. break;
  1500. }
  1501. if (i == ARRAY_SIZE(torture_ops)) {
  1502. pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
  1503. torture_type);
  1504. pr_alert("rcu-torture types:");
  1505. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1506. pr_alert(" %s", torture_ops[i]->name);
  1507. pr_alert("\n");
  1508. torture_init_end();
  1509. return -EINVAL;
  1510. }
  1511. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  1512. pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
  1513. fqs_duration = 0;
  1514. }
  1515. if (cur_ops->init)
  1516. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  1517. if (nreaders >= 0) {
  1518. nrealreaders = nreaders;
  1519. } else {
  1520. nrealreaders = num_online_cpus() - 1;
  1521. if (nrealreaders <= 0)
  1522. nrealreaders = 1;
  1523. }
  1524. rcu_torture_print_module_parms(cur_ops, "Start of test");
  1525. /* Set up the freelist. */
  1526. INIT_LIST_HEAD(&rcu_torture_freelist);
  1527. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  1528. rcu_tortures[i].rtort_mbtest = 0;
  1529. list_add_tail(&rcu_tortures[i].rtort_free,
  1530. &rcu_torture_freelist);
  1531. }
  1532. /* Initialize the statistics so that each run gets its own numbers. */
  1533. rcu_torture_current = NULL;
  1534. rcu_torture_current_version = 0;
  1535. atomic_set(&n_rcu_torture_alloc, 0);
  1536. atomic_set(&n_rcu_torture_alloc_fail, 0);
  1537. atomic_set(&n_rcu_torture_free, 0);
  1538. atomic_set(&n_rcu_torture_mberror, 0);
  1539. atomic_set(&n_rcu_torture_error, 0);
  1540. n_rcu_torture_barrier_error = 0;
  1541. n_rcu_torture_boost_ktrerror = 0;
  1542. n_rcu_torture_boost_rterror = 0;
  1543. n_rcu_torture_boost_failure = 0;
  1544. n_rcu_torture_boosts = 0;
  1545. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1546. atomic_set(&rcu_torture_wcount[i], 0);
  1547. for_each_possible_cpu(cpu) {
  1548. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1549. per_cpu(rcu_torture_count, cpu)[i] = 0;
  1550. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  1551. }
  1552. }
  1553. /* Start up the kthreads. */
  1554. firsterr = torture_create_kthread(rcu_torture_writer, NULL,
  1555. writer_task);
  1556. if (firsterr)
  1557. goto unwind;
  1558. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  1559. GFP_KERNEL);
  1560. if (fakewriter_tasks == NULL) {
  1561. VERBOSE_TOROUT_ERRSTRING("out of memory");
  1562. firsterr = -ENOMEM;
  1563. goto unwind;
  1564. }
  1565. for (i = 0; i < nfakewriters; i++) {
  1566. firsterr = torture_create_kthread(rcu_torture_fakewriter,
  1567. NULL, fakewriter_tasks[i]);
  1568. if (firsterr)
  1569. goto unwind;
  1570. }
  1571. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  1572. GFP_KERNEL);
  1573. if (reader_tasks == NULL) {
  1574. VERBOSE_TOROUT_ERRSTRING("out of memory");
  1575. firsterr = -ENOMEM;
  1576. goto unwind;
  1577. }
  1578. for (i = 0; i < nrealreaders; i++) {
  1579. firsterr = torture_create_kthread(rcu_torture_reader, NULL,
  1580. reader_tasks[i]);
  1581. if (firsterr)
  1582. goto unwind;
  1583. }
  1584. if (stat_interval > 0) {
  1585. firsterr = torture_create_kthread(rcu_torture_stats, NULL,
  1586. stats_task);
  1587. if (firsterr)
  1588. goto unwind;
  1589. }
  1590. if (test_no_idle_hz) {
  1591. firsterr = torture_shuffle_init(shuffle_interval * HZ);
  1592. if (firsterr)
  1593. goto unwind;
  1594. }
  1595. if (stutter < 0)
  1596. stutter = 0;
  1597. if (stutter) {
  1598. firsterr = torture_stutter_init(stutter * HZ);
  1599. if (firsterr)
  1600. goto unwind;
  1601. }
  1602. if (fqs_duration < 0)
  1603. fqs_duration = 0;
  1604. if (fqs_duration) {
  1605. /* Create the fqs thread */
  1606. firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
  1607. fqs_task);
  1608. if (firsterr)
  1609. goto unwind;
  1610. }
  1611. if (test_boost_interval < 1)
  1612. test_boost_interval = 1;
  1613. if (test_boost_duration < 2)
  1614. test_boost_duration = 2;
  1615. if ((test_boost == 1 && cur_ops->can_boost) ||
  1616. test_boost == 2) {
  1617. boost_starttime = jiffies + test_boost_interval * HZ;
  1618. register_cpu_notifier(&rcutorture_cpu_nb);
  1619. for_each_possible_cpu(i) {
  1620. if (cpu_is_offline(i))
  1621. continue; /* Heuristic: CPU can go offline. */
  1622. firsterr = rcutorture_booster_init(i);
  1623. if (firsterr)
  1624. goto unwind;
  1625. }
  1626. }
  1627. firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
  1628. if (firsterr)
  1629. goto unwind;
  1630. firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval * HZ);
  1631. if (firsterr)
  1632. goto unwind;
  1633. firsterr = rcu_torture_stall_init();
  1634. if (firsterr)
  1635. goto unwind;
  1636. firsterr = rcu_torture_barrier_init();
  1637. if (firsterr)
  1638. goto unwind;
  1639. if (object_debug)
  1640. rcu_test_debug_objects();
  1641. if (cbflood_n_burst > 0) {
  1642. /* Create the cbflood threads */
  1643. ncbflooders = (num_online_cpus() + 3) / 4;
  1644. cbflood_task = kcalloc(ncbflooders, sizeof(*cbflood_task),
  1645. GFP_KERNEL);
  1646. if (!cbflood_task) {
  1647. VERBOSE_TOROUT_ERRSTRING("out of memory");
  1648. firsterr = -ENOMEM;
  1649. goto unwind;
  1650. }
  1651. for (i = 0; i < ncbflooders; i++) {
  1652. firsterr = torture_create_kthread(rcu_torture_cbflood,
  1653. NULL,
  1654. cbflood_task[i]);
  1655. if (firsterr)
  1656. goto unwind;
  1657. }
  1658. }
  1659. rcutorture_record_test_transition();
  1660. torture_init_end();
  1661. return 0;
  1662. unwind:
  1663. torture_init_end();
  1664. rcu_torture_cleanup();
  1665. return firsterr;
  1666. }
  1667. module_init(rcu_torture_init);
  1668. module_exit(rcu_torture_cleanup);