rcutorture.c 54 KB

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