rcutorture.c 51 KB

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