rcutorture.c 54 KB

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