rcutorture.c 54 KB

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