rcutorture.c 44 KB

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