rcutorture.c 54 KB

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