rcutorture.c 55 KB

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