rcutorture.c 54 KB

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