rcutorture.c 55 KB

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