rcutorture.c 54 KB

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