rcutorture.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  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 <asm/byteorder.h>
  50. MODULE_LICENSE("GPL");
  51. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
  52. "Josh Triplett <josh@freedesktop.org>");
  53. static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
  54. static int nfakewriters = 4; /* # fake writer threads */
  55. static int stat_interval; /* Interval between stats, in seconds. */
  56. /* Defaults to "only at end of test". */
  57. static bool verbose; /* Print more debug info. */
  58. static bool test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
  59. static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
  60. static int stutter = 5; /* Start/stop testing interval (in sec) */
  61. static int irqreader = 1; /* RCU readers from irq (timers). */
  62. static int fqs_duration; /* Duration of bursts (us), 0 to disable. */
  63. static int fqs_holdoff; /* Hold time within burst (us). */
  64. static int fqs_stutter = 3; /* Wait time between bursts (s). */
  65. static int onoff_interval; /* Wait time between CPU hotplugs, 0=disable. */
  66. static int onoff_holdoff; /* Seconds after boot before CPU hotplugs. */
  67. static int shutdown_secs; /* Shutdown time (s). <=0 for no shutdown. */
  68. static int stall_cpu; /* CPU-stall duration (s). 0 for no stall. */
  69. static int stall_cpu_holdoff = 10; /* Time to wait until stall (s). */
  70. static int test_boost = 1; /* Test RCU prio boost: 0=no, 1=maybe, 2=yes. */
  71. static int test_boost_interval = 7; /* Interval between boost tests, seconds. */
  72. static int test_boost_duration = 4; /* Duration of each boost test, seconds. */
  73. static char *torture_type = "rcu"; /* What RCU implementation to torture. */
  74. module_param(nreaders, int, 0444);
  75. MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  76. module_param(nfakewriters, int, 0444);
  77. MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
  78. module_param(stat_interval, int, 0644);
  79. MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  80. module_param(verbose, bool, 0444);
  81. MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  82. module_param(test_no_idle_hz, bool, 0444);
  83. MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
  84. module_param(shuffle_interval, int, 0444);
  85. MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
  86. module_param(stutter, int, 0444);
  87. MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
  88. module_param(irqreader, int, 0444);
  89. MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
  90. module_param(fqs_duration, int, 0444);
  91. MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
  92. module_param(fqs_holdoff, int, 0444);
  93. MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
  94. module_param(fqs_stutter, int, 0444);
  95. MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
  96. module_param(onoff_interval, int, 0444);
  97. MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
  98. module_param(onoff_holdoff, int, 0444);
  99. MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
  100. module_param(shutdown_secs, int, 0444);
  101. MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), zero to disable.");
  102. module_param(stall_cpu, int, 0444);
  103. MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
  104. module_param(stall_cpu_holdoff, int, 0444);
  105. MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
  106. module_param(test_boost, int, 0444);
  107. MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
  108. module_param(test_boost_interval, int, 0444);
  109. MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
  110. module_param(test_boost_duration, int, 0444);
  111. MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
  112. module_param(torture_type, charp, 0444);
  113. MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
  114. #define TORTURE_FLAG "-torture:"
  115. #define PRINTK_STRING(s) \
  116. do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  117. #define VERBOSE_PRINTK_STRING(s) \
  118. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  119. #define VERBOSE_PRINTK_ERRSTRING(s) \
  120. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
  121. static char printk_buf[4096];
  122. static int nrealreaders;
  123. static struct task_struct *writer_task;
  124. static struct task_struct **fakewriter_tasks;
  125. static struct task_struct **reader_tasks;
  126. static struct task_struct *stats_task;
  127. static struct task_struct *shuffler_task;
  128. static struct task_struct *stutter_task;
  129. static struct task_struct *fqs_task;
  130. static struct task_struct *boost_tasks[NR_CPUS];
  131. static struct task_struct *shutdown_task;
  132. #ifdef CONFIG_HOTPLUG_CPU
  133. static struct task_struct *onoff_task;
  134. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  135. static struct task_struct *stall_task;
  136. #define RCU_TORTURE_PIPE_LEN 10
  137. struct rcu_torture {
  138. struct rcu_head rtort_rcu;
  139. int rtort_pipe_count;
  140. struct list_head rtort_free;
  141. int rtort_mbtest;
  142. };
  143. static LIST_HEAD(rcu_torture_freelist);
  144. static struct rcu_torture __rcu *rcu_torture_current;
  145. static unsigned long rcu_torture_current_version;
  146. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  147. static DEFINE_SPINLOCK(rcu_torture_lock);
  148. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
  149. { 0 };
  150. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
  151. { 0 };
  152. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  153. static atomic_t n_rcu_torture_alloc;
  154. static atomic_t n_rcu_torture_alloc_fail;
  155. static atomic_t n_rcu_torture_free;
  156. static atomic_t n_rcu_torture_mberror;
  157. static atomic_t n_rcu_torture_error;
  158. static long n_rcu_torture_boost_ktrerror;
  159. static long n_rcu_torture_boost_rterror;
  160. static long n_rcu_torture_boost_failure;
  161. static long n_rcu_torture_boosts;
  162. static long n_rcu_torture_timers;
  163. static long n_offline_attempts;
  164. static long n_offline_successes;
  165. static long n_online_attempts;
  166. static long n_online_successes;
  167. static struct list_head rcu_torture_removed;
  168. static cpumask_var_t shuffle_tmp_mask;
  169. static int stutter_pause_test;
  170. #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
  171. #define RCUTORTURE_RUNNABLE_INIT 1
  172. #else
  173. #define RCUTORTURE_RUNNABLE_INIT 0
  174. #endif
  175. int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
  176. module_param(rcutorture_runnable, int, 0444);
  177. MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
  178. #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
  179. #define rcu_can_boost() 1
  180. #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
  181. #define rcu_can_boost() 0
  182. #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
  183. static unsigned long shutdown_time; /* jiffies to system shutdown. */
  184. static unsigned long boost_starttime; /* jiffies of next boost test start. */
  185. DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
  186. /* and boost task create/destroy. */
  187. /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
  188. #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
  189. #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
  190. #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
  191. static int fullstop = FULLSTOP_RMMOD;
  192. /*
  193. * Protect fullstop transitions and spawning of kthreads.
  194. */
  195. static DEFINE_MUTEX(fullstop_mutex);
  196. /* Forward reference. */
  197. static void rcu_torture_cleanup(void);
  198. /*
  199. * Detect and respond to a system shutdown.
  200. */
  201. static int
  202. rcutorture_shutdown_notify(struct notifier_block *unused1,
  203. unsigned long unused2, void *unused3)
  204. {
  205. mutex_lock(&fullstop_mutex);
  206. if (fullstop == FULLSTOP_DONTSTOP)
  207. fullstop = FULLSTOP_SHUTDOWN;
  208. else
  209. printk(KERN_WARNING /* but going down anyway, so... */
  210. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  211. mutex_unlock(&fullstop_mutex);
  212. return NOTIFY_DONE;
  213. }
  214. /*
  215. * Absorb kthreads into a kernel function that won't return, so that
  216. * they won't ever access module text or data again.
  217. */
  218. static void rcutorture_shutdown_absorb(char *title)
  219. {
  220. if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  221. printk(KERN_NOTICE
  222. "rcutorture thread %s parking due to system shutdown\n",
  223. title);
  224. schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  225. }
  226. }
  227. /*
  228. * Allocate an element from the rcu_tortures pool.
  229. */
  230. static struct rcu_torture *
  231. rcu_torture_alloc(void)
  232. {
  233. struct list_head *p;
  234. spin_lock_bh(&rcu_torture_lock);
  235. if (list_empty(&rcu_torture_freelist)) {
  236. atomic_inc(&n_rcu_torture_alloc_fail);
  237. spin_unlock_bh(&rcu_torture_lock);
  238. return NULL;
  239. }
  240. atomic_inc(&n_rcu_torture_alloc);
  241. p = rcu_torture_freelist.next;
  242. list_del_init(p);
  243. spin_unlock_bh(&rcu_torture_lock);
  244. return container_of(p, struct rcu_torture, rtort_free);
  245. }
  246. /*
  247. * Free an element to the rcu_tortures pool.
  248. */
  249. static void
  250. rcu_torture_free(struct rcu_torture *p)
  251. {
  252. atomic_inc(&n_rcu_torture_free);
  253. spin_lock_bh(&rcu_torture_lock);
  254. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  255. spin_unlock_bh(&rcu_torture_lock);
  256. }
  257. struct rcu_random_state {
  258. unsigned long rrs_state;
  259. long rrs_count;
  260. };
  261. #define RCU_RANDOM_MULT 39916801 /* prime */
  262. #define RCU_RANDOM_ADD 479001701 /* prime */
  263. #define RCU_RANDOM_REFRESH 10000
  264. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  265. /*
  266. * Crude but fast random-number generator. Uses a linear congruential
  267. * generator, with occasional help from cpu_clock().
  268. */
  269. static unsigned long
  270. rcu_random(struct rcu_random_state *rrsp)
  271. {
  272. if (--rrsp->rrs_count < 0) {
  273. rrsp->rrs_state += (unsigned long)local_clock();
  274. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  275. }
  276. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  277. return swahw32(rrsp->rrs_state);
  278. }
  279. static void
  280. rcu_stutter_wait(char *title)
  281. {
  282. while (stutter_pause_test || !rcutorture_runnable) {
  283. if (rcutorture_runnable)
  284. schedule_timeout_interruptible(1);
  285. else
  286. schedule_timeout_interruptible(round_jiffies_relative(HZ));
  287. rcutorture_shutdown_absorb(title);
  288. }
  289. }
  290. /*
  291. * Operations vector for selecting different types of tests.
  292. */
  293. struct rcu_torture_ops {
  294. void (*init)(void);
  295. void (*cleanup)(void);
  296. int (*readlock)(void);
  297. void (*read_delay)(struct rcu_random_state *rrsp);
  298. void (*readunlock)(int idx);
  299. int (*completed)(void);
  300. void (*deferred_free)(struct rcu_torture *p);
  301. void (*sync)(void);
  302. void (*cb_barrier)(void);
  303. void (*fqs)(void);
  304. int (*stats)(char *page);
  305. int irq_capable;
  306. int can_boost;
  307. char *name;
  308. };
  309. static struct rcu_torture_ops *cur_ops;
  310. /*
  311. * Definitions for rcu torture testing.
  312. */
  313. static int rcu_torture_read_lock(void) __acquires(RCU)
  314. {
  315. rcu_read_lock();
  316. return 0;
  317. }
  318. static void rcu_read_delay(struct rcu_random_state *rrsp)
  319. {
  320. const unsigned long shortdelay_us = 200;
  321. const unsigned long longdelay_ms = 50;
  322. /* We want a short delay sometimes to make a reader delay the grace
  323. * period, and we want a long delay occasionally to trigger
  324. * force_quiescent_state. */
  325. if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
  326. mdelay(longdelay_ms);
  327. if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
  328. udelay(shortdelay_us);
  329. #ifdef CONFIG_PREEMPT
  330. if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
  331. preempt_schedule(); /* No QS if preempt_disable() in effect */
  332. #endif
  333. }
  334. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  335. {
  336. rcu_read_unlock();
  337. }
  338. static int rcu_torture_completed(void)
  339. {
  340. return rcu_batches_completed();
  341. }
  342. static void
  343. rcu_torture_cb(struct rcu_head *p)
  344. {
  345. int i;
  346. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  347. if (fullstop != FULLSTOP_DONTSTOP) {
  348. /* Test is ending, just drop callbacks on the floor. */
  349. /* The next initialization will pick up the pieces. */
  350. return;
  351. }
  352. i = rp->rtort_pipe_count;
  353. if (i > RCU_TORTURE_PIPE_LEN)
  354. i = RCU_TORTURE_PIPE_LEN;
  355. atomic_inc(&rcu_torture_wcount[i]);
  356. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  357. rp->rtort_mbtest = 0;
  358. rcu_torture_free(rp);
  359. } else
  360. cur_ops->deferred_free(rp);
  361. }
  362. static int rcu_no_completed(void)
  363. {
  364. return 0;
  365. }
  366. static void rcu_torture_deferred_free(struct rcu_torture *p)
  367. {
  368. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  369. }
  370. static struct rcu_torture_ops rcu_ops = {
  371. .init = NULL,
  372. .cleanup = NULL,
  373. .readlock = rcu_torture_read_lock,
  374. .read_delay = rcu_read_delay,
  375. .readunlock = rcu_torture_read_unlock,
  376. .completed = rcu_torture_completed,
  377. .deferred_free = rcu_torture_deferred_free,
  378. .sync = synchronize_rcu,
  379. .cb_barrier = rcu_barrier,
  380. .fqs = rcu_force_quiescent_state,
  381. .stats = NULL,
  382. .irq_capable = 1,
  383. .can_boost = rcu_can_boost(),
  384. .name = "rcu"
  385. };
  386. static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
  387. {
  388. int i;
  389. struct rcu_torture *rp;
  390. struct rcu_torture *rp1;
  391. cur_ops->sync();
  392. list_add(&p->rtort_free, &rcu_torture_removed);
  393. list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
  394. i = rp->rtort_pipe_count;
  395. if (i > RCU_TORTURE_PIPE_LEN)
  396. i = RCU_TORTURE_PIPE_LEN;
  397. atomic_inc(&rcu_torture_wcount[i]);
  398. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  399. rp->rtort_mbtest = 0;
  400. list_del(&rp->rtort_free);
  401. rcu_torture_free(rp);
  402. }
  403. }
  404. }
  405. static void rcu_sync_torture_init(void)
  406. {
  407. INIT_LIST_HEAD(&rcu_torture_removed);
  408. }
  409. static struct rcu_torture_ops rcu_sync_ops = {
  410. .init = rcu_sync_torture_init,
  411. .cleanup = NULL,
  412. .readlock = rcu_torture_read_lock,
  413. .read_delay = rcu_read_delay,
  414. .readunlock = rcu_torture_read_unlock,
  415. .completed = rcu_torture_completed,
  416. .deferred_free = rcu_sync_torture_deferred_free,
  417. .sync = synchronize_rcu,
  418. .cb_barrier = NULL,
  419. .fqs = rcu_force_quiescent_state,
  420. .stats = NULL,
  421. .irq_capable = 1,
  422. .can_boost = rcu_can_boost(),
  423. .name = "rcu_sync"
  424. };
  425. static struct rcu_torture_ops rcu_expedited_ops = {
  426. .init = rcu_sync_torture_init,
  427. .cleanup = NULL,
  428. .readlock = rcu_torture_read_lock,
  429. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  430. .readunlock = rcu_torture_read_unlock,
  431. .completed = rcu_no_completed,
  432. .deferred_free = rcu_sync_torture_deferred_free,
  433. .sync = synchronize_rcu_expedited,
  434. .cb_barrier = NULL,
  435. .fqs = rcu_force_quiescent_state,
  436. .stats = NULL,
  437. .irq_capable = 1,
  438. .can_boost = rcu_can_boost(),
  439. .name = "rcu_expedited"
  440. };
  441. /*
  442. * Definitions for rcu_bh torture testing.
  443. */
  444. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  445. {
  446. rcu_read_lock_bh();
  447. return 0;
  448. }
  449. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  450. {
  451. rcu_read_unlock_bh();
  452. }
  453. static int rcu_bh_torture_completed(void)
  454. {
  455. return rcu_batches_completed_bh();
  456. }
  457. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  458. {
  459. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  460. }
  461. static struct rcu_torture_ops rcu_bh_ops = {
  462. .init = NULL,
  463. .cleanup = NULL,
  464. .readlock = rcu_bh_torture_read_lock,
  465. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  466. .readunlock = rcu_bh_torture_read_unlock,
  467. .completed = rcu_bh_torture_completed,
  468. .deferred_free = rcu_bh_torture_deferred_free,
  469. .sync = synchronize_rcu_bh,
  470. .cb_barrier = rcu_barrier_bh,
  471. .fqs = rcu_bh_force_quiescent_state,
  472. .stats = NULL,
  473. .irq_capable = 1,
  474. .name = "rcu_bh"
  475. };
  476. static struct rcu_torture_ops rcu_bh_sync_ops = {
  477. .init = rcu_sync_torture_init,
  478. .cleanup = NULL,
  479. .readlock = rcu_bh_torture_read_lock,
  480. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  481. .readunlock = rcu_bh_torture_read_unlock,
  482. .completed = rcu_bh_torture_completed,
  483. .deferred_free = rcu_sync_torture_deferred_free,
  484. .sync = synchronize_rcu_bh,
  485. .cb_barrier = NULL,
  486. .fqs = rcu_bh_force_quiescent_state,
  487. .stats = NULL,
  488. .irq_capable = 1,
  489. .name = "rcu_bh_sync"
  490. };
  491. static struct rcu_torture_ops rcu_bh_expedited_ops = {
  492. .init = rcu_sync_torture_init,
  493. .cleanup = NULL,
  494. .readlock = rcu_bh_torture_read_lock,
  495. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  496. .readunlock = rcu_bh_torture_read_unlock,
  497. .completed = rcu_bh_torture_completed,
  498. .deferred_free = rcu_sync_torture_deferred_free,
  499. .sync = synchronize_rcu_bh_expedited,
  500. .cb_barrier = NULL,
  501. .fqs = rcu_bh_force_quiescent_state,
  502. .stats = NULL,
  503. .irq_capable = 1,
  504. .name = "rcu_bh_expedited"
  505. };
  506. /*
  507. * Definitions for srcu torture testing.
  508. */
  509. static struct srcu_struct srcu_ctl;
  510. static void srcu_torture_init(void)
  511. {
  512. init_srcu_struct(&srcu_ctl);
  513. rcu_sync_torture_init();
  514. }
  515. static void srcu_torture_cleanup(void)
  516. {
  517. synchronize_srcu(&srcu_ctl);
  518. cleanup_srcu_struct(&srcu_ctl);
  519. }
  520. static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
  521. {
  522. return srcu_read_lock(&srcu_ctl);
  523. }
  524. static void srcu_read_delay(struct rcu_random_state *rrsp)
  525. {
  526. long delay;
  527. const long uspertick = 1000000 / HZ;
  528. const long longdelay = 10;
  529. /* We want there to be long-running readers, but not all the time. */
  530. delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
  531. if (!delay)
  532. schedule_timeout_interruptible(longdelay);
  533. else
  534. rcu_read_delay(rrsp);
  535. }
  536. static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
  537. {
  538. srcu_read_unlock(&srcu_ctl, idx);
  539. }
  540. static int srcu_torture_completed(void)
  541. {
  542. return srcu_batches_completed(&srcu_ctl);
  543. }
  544. static void srcu_torture_synchronize(void)
  545. {
  546. synchronize_srcu(&srcu_ctl);
  547. }
  548. static int srcu_torture_stats(char *page)
  549. {
  550. int cnt = 0;
  551. int cpu;
  552. int idx = srcu_ctl.completed & 0x1;
  553. cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
  554. torture_type, TORTURE_FLAG, idx);
  555. for_each_possible_cpu(cpu) {
  556. cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
  557. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
  558. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
  559. }
  560. cnt += sprintf(&page[cnt], "\n");
  561. return cnt;
  562. }
  563. static struct rcu_torture_ops srcu_ops = {
  564. .init = srcu_torture_init,
  565. .cleanup = srcu_torture_cleanup,
  566. .readlock = srcu_torture_read_lock,
  567. .read_delay = srcu_read_delay,
  568. .readunlock = srcu_torture_read_unlock,
  569. .completed = srcu_torture_completed,
  570. .deferred_free = rcu_sync_torture_deferred_free,
  571. .sync = srcu_torture_synchronize,
  572. .cb_barrier = NULL,
  573. .stats = srcu_torture_stats,
  574. .name = "srcu"
  575. };
  576. static int srcu_torture_read_lock_raw(void) __acquires(&srcu_ctl)
  577. {
  578. return srcu_read_lock_raw(&srcu_ctl);
  579. }
  580. static void srcu_torture_read_unlock_raw(int idx) __releases(&srcu_ctl)
  581. {
  582. srcu_read_unlock_raw(&srcu_ctl, idx);
  583. }
  584. static struct rcu_torture_ops srcu_raw_ops = {
  585. .init = srcu_torture_init,
  586. .cleanup = srcu_torture_cleanup,
  587. .readlock = srcu_torture_read_lock_raw,
  588. .read_delay = srcu_read_delay,
  589. .readunlock = srcu_torture_read_unlock_raw,
  590. .completed = srcu_torture_completed,
  591. .deferred_free = rcu_sync_torture_deferred_free,
  592. .sync = srcu_torture_synchronize,
  593. .cb_barrier = NULL,
  594. .stats = srcu_torture_stats,
  595. .name = "srcu_raw"
  596. };
  597. static void srcu_torture_synchronize_expedited(void)
  598. {
  599. synchronize_srcu_expedited(&srcu_ctl);
  600. }
  601. static struct rcu_torture_ops srcu_expedited_ops = {
  602. .init = srcu_torture_init,
  603. .cleanup = srcu_torture_cleanup,
  604. .readlock = srcu_torture_read_lock,
  605. .read_delay = srcu_read_delay,
  606. .readunlock = srcu_torture_read_unlock,
  607. .completed = srcu_torture_completed,
  608. .deferred_free = rcu_sync_torture_deferred_free,
  609. .sync = srcu_torture_synchronize_expedited,
  610. .cb_barrier = NULL,
  611. .stats = srcu_torture_stats,
  612. .name = "srcu_expedited"
  613. };
  614. /*
  615. * Definitions for sched torture testing.
  616. */
  617. static int sched_torture_read_lock(void)
  618. {
  619. preempt_disable();
  620. return 0;
  621. }
  622. static void sched_torture_read_unlock(int idx)
  623. {
  624. preempt_enable();
  625. }
  626. static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
  627. {
  628. call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
  629. }
  630. static struct rcu_torture_ops sched_ops = {
  631. .init = rcu_sync_torture_init,
  632. .cleanup = NULL,
  633. .readlock = sched_torture_read_lock,
  634. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  635. .readunlock = sched_torture_read_unlock,
  636. .completed = rcu_no_completed,
  637. .deferred_free = rcu_sched_torture_deferred_free,
  638. .sync = synchronize_sched,
  639. .cb_barrier = rcu_barrier_sched,
  640. .fqs = rcu_sched_force_quiescent_state,
  641. .stats = NULL,
  642. .irq_capable = 1,
  643. .name = "sched"
  644. };
  645. static struct rcu_torture_ops sched_sync_ops = {
  646. .init = rcu_sync_torture_init,
  647. .cleanup = NULL,
  648. .readlock = sched_torture_read_lock,
  649. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  650. .readunlock = sched_torture_read_unlock,
  651. .completed = rcu_no_completed,
  652. .deferred_free = rcu_sync_torture_deferred_free,
  653. .sync = synchronize_sched,
  654. .cb_barrier = NULL,
  655. .fqs = rcu_sched_force_quiescent_state,
  656. .stats = NULL,
  657. .name = "sched_sync"
  658. };
  659. static struct rcu_torture_ops sched_expedited_ops = {
  660. .init = rcu_sync_torture_init,
  661. .cleanup = NULL,
  662. .readlock = sched_torture_read_lock,
  663. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  664. .readunlock = sched_torture_read_unlock,
  665. .completed = rcu_no_completed,
  666. .deferred_free = rcu_sync_torture_deferred_free,
  667. .sync = synchronize_sched_expedited,
  668. .cb_barrier = NULL,
  669. .fqs = rcu_sched_force_quiescent_state,
  670. .stats = NULL,
  671. .irq_capable = 1,
  672. .name = "sched_expedited"
  673. };
  674. /*
  675. * RCU torture priority-boost testing. Runs one real-time thread per
  676. * CPU for moderate bursts, repeatedly registering RCU callbacks and
  677. * spinning waiting for them to be invoked. If a given callback takes
  678. * too long to be invoked, we assume that priority inversion has occurred.
  679. */
  680. struct rcu_boost_inflight {
  681. struct rcu_head rcu;
  682. int inflight;
  683. };
  684. static void rcu_torture_boost_cb(struct rcu_head *head)
  685. {
  686. struct rcu_boost_inflight *rbip =
  687. container_of(head, struct rcu_boost_inflight, rcu);
  688. smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
  689. rbip->inflight = 0;
  690. }
  691. static int rcu_torture_boost(void *arg)
  692. {
  693. unsigned long call_rcu_time;
  694. unsigned long endtime;
  695. unsigned long oldstarttime;
  696. struct rcu_boost_inflight rbi = { .inflight = 0 };
  697. struct sched_param sp;
  698. VERBOSE_PRINTK_STRING("rcu_torture_boost started");
  699. /* Set real-time priority. */
  700. sp.sched_priority = 1;
  701. if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
  702. VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
  703. n_rcu_torture_boost_rterror++;
  704. }
  705. init_rcu_head_on_stack(&rbi.rcu);
  706. /* Each pass through the following loop does one boost-test cycle. */
  707. do {
  708. /* Wait for the next test interval. */
  709. oldstarttime = boost_starttime;
  710. while (ULONG_CMP_LT(jiffies, oldstarttime)) {
  711. schedule_timeout_uninterruptible(1);
  712. rcu_stutter_wait("rcu_torture_boost");
  713. if (kthread_should_stop() ||
  714. fullstop != FULLSTOP_DONTSTOP)
  715. goto checkwait;
  716. }
  717. /* Do one boost-test interval. */
  718. endtime = oldstarttime + test_boost_duration * HZ;
  719. call_rcu_time = jiffies;
  720. while (ULONG_CMP_LT(jiffies, endtime)) {
  721. /* If we don't have a callback in flight, post one. */
  722. if (!rbi.inflight) {
  723. smp_mb(); /* RCU core before ->inflight = 1. */
  724. rbi.inflight = 1;
  725. call_rcu(&rbi.rcu, rcu_torture_boost_cb);
  726. if (jiffies - call_rcu_time >
  727. test_boost_duration * HZ - HZ / 2) {
  728. VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
  729. n_rcu_torture_boost_failure++;
  730. }
  731. call_rcu_time = jiffies;
  732. }
  733. cond_resched();
  734. rcu_stutter_wait("rcu_torture_boost");
  735. if (kthread_should_stop() ||
  736. fullstop != FULLSTOP_DONTSTOP)
  737. goto checkwait;
  738. }
  739. /*
  740. * Set the start time of the next test interval.
  741. * Yes, this is vulnerable to long delays, but such
  742. * delays simply cause a false negative for the next
  743. * interval. Besides, we are running at RT priority,
  744. * so delays should be relatively rare.
  745. */
  746. while (oldstarttime == boost_starttime &&
  747. !kthread_should_stop()) {
  748. if (mutex_trylock(&boost_mutex)) {
  749. boost_starttime = jiffies +
  750. test_boost_interval * HZ;
  751. n_rcu_torture_boosts++;
  752. mutex_unlock(&boost_mutex);
  753. break;
  754. }
  755. schedule_timeout_uninterruptible(1);
  756. }
  757. /* Go do the stutter. */
  758. checkwait: rcu_stutter_wait("rcu_torture_boost");
  759. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  760. /* Clean up and exit. */
  761. VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
  762. rcutorture_shutdown_absorb("rcu_torture_boost");
  763. while (!kthread_should_stop() || rbi.inflight)
  764. schedule_timeout_uninterruptible(1);
  765. smp_mb(); /* order accesses to ->inflight before stack-frame death. */
  766. destroy_rcu_head_on_stack(&rbi.rcu);
  767. return 0;
  768. }
  769. /*
  770. * RCU torture force-quiescent-state kthread. Repeatedly induces
  771. * bursts of calls to force_quiescent_state(), increasing the probability
  772. * of occurrence of some important types of race conditions.
  773. */
  774. static int
  775. rcu_torture_fqs(void *arg)
  776. {
  777. unsigned long fqs_resume_time;
  778. int fqs_burst_remaining;
  779. VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
  780. do {
  781. fqs_resume_time = jiffies + fqs_stutter * HZ;
  782. while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
  783. !kthread_should_stop()) {
  784. schedule_timeout_interruptible(1);
  785. }
  786. fqs_burst_remaining = fqs_duration;
  787. while (fqs_burst_remaining > 0 &&
  788. !kthread_should_stop()) {
  789. cur_ops->fqs();
  790. udelay(fqs_holdoff);
  791. fqs_burst_remaining -= fqs_holdoff;
  792. }
  793. rcu_stutter_wait("rcu_torture_fqs");
  794. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  795. VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
  796. rcutorture_shutdown_absorb("rcu_torture_fqs");
  797. while (!kthread_should_stop())
  798. schedule_timeout_uninterruptible(1);
  799. return 0;
  800. }
  801. /*
  802. * RCU torture writer kthread. Repeatedly substitutes a new structure
  803. * for that pointed to by rcu_torture_current, freeing the old structure
  804. * after a series of grace periods (the "pipeline").
  805. */
  806. static int
  807. rcu_torture_writer(void *arg)
  808. {
  809. int i;
  810. long oldbatch = rcu_batches_completed();
  811. struct rcu_torture *rp;
  812. struct rcu_torture *old_rp;
  813. static DEFINE_RCU_RANDOM(rand);
  814. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  815. set_user_nice(current, 19);
  816. do {
  817. schedule_timeout_uninterruptible(1);
  818. rp = rcu_torture_alloc();
  819. if (rp == NULL)
  820. continue;
  821. rp->rtort_pipe_count = 0;
  822. udelay(rcu_random(&rand) & 0x3ff);
  823. old_rp = rcu_dereference_check(rcu_torture_current,
  824. current == writer_task);
  825. rp->rtort_mbtest = 1;
  826. rcu_assign_pointer(rcu_torture_current, rp);
  827. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  828. if (old_rp) {
  829. i = old_rp->rtort_pipe_count;
  830. if (i > RCU_TORTURE_PIPE_LEN)
  831. i = RCU_TORTURE_PIPE_LEN;
  832. atomic_inc(&rcu_torture_wcount[i]);
  833. old_rp->rtort_pipe_count++;
  834. cur_ops->deferred_free(old_rp);
  835. }
  836. rcutorture_record_progress(++rcu_torture_current_version);
  837. oldbatch = cur_ops->completed();
  838. rcu_stutter_wait("rcu_torture_writer");
  839. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  840. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  841. rcutorture_shutdown_absorb("rcu_torture_writer");
  842. while (!kthread_should_stop())
  843. schedule_timeout_uninterruptible(1);
  844. return 0;
  845. }
  846. /*
  847. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  848. * delay between calls.
  849. */
  850. static int
  851. rcu_torture_fakewriter(void *arg)
  852. {
  853. DEFINE_RCU_RANDOM(rand);
  854. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
  855. set_user_nice(current, 19);
  856. do {
  857. schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
  858. udelay(rcu_random(&rand) & 0x3ff);
  859. cur_ops->sync();
  860. rcu_stutter_wait("rcu_torture_fakewriter");
  861. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  862. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
  863. rcutorture_shutdown_absorb("rcu_torture_fakewriter");
  864. while (!kthread_should_stop())
  865. schedule_timeout_uninterruptible(1);
  866. return 0;
  867. }
  868. void rcutorture_trace_dump(void)
  869. {
  870. static atomic_t beenhere = ATOMIC_INIT(0);
  871. if (atomic_read(&beenhere))
  872. return;
  873. if (atomic_xchg(&beenhere, 1) != 0)
  874. return;
  875. do_trace_rcu_torture_read(cur_ops->name, (struct rcu_head *)~0UL);
  876. ftrace_dump(DUMP_ALL);
  877. }
  878. /*
  879. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  880. * incrementing the corresponding element of the pipeline array. The
  881. * counter in the element should never be greater than 1, otherwise, the
  882. * RCU implementation is broken.
  883. */
  884. static void rcu_torture_timer(unsigned long unused)
  885. {
  886. int idx;
  887. int completed;
  888. static DEFINE_RCU_RANDOM(rand);
  889. static DEFINE_SPINLOCK(rand_lock);
  890. struct rcu_torture *p;
  891. int pipe_count;
  892. idx = cur_ops->readlock();
  893. completed = cur_ops->completed();
  894. p = rcu_dereference_check(rcu_torture_current,
  895. rcu_read_lock_bh_held() ||
  896. rcu_read_lock_sched_held() ||
  897. srcu_read_lock_held(&srcu_ctl));
  898. if (p == NULL) {
  899. /* Leave because rcu_torture_writer is not yet underway */
  900. cur_ops->readunlock(idx);
  901. return;
  902. }
  903. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
  904. if (p->rtort_mbtest == 0)
  905. atomic_inc(&n_rcu_torture_mberror);
  906. spin_lock(&rand_lock);
  907. cur_ops->read_delay(&rand);
  908. n_rcu_torture_timers++;
  909. spin_unlock(&rand_lock);
  910. preempt_disable();
  911. pipe_count = p->rtort_pipe_count;
  912. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  913. /* Should not happen, but... */
  914. pipe_count = RCU_TORTURE_PIPE_LEN;
  915. }
  916. if (pipe_count > 1)
  917. rcutorture_trace_dump();
  918. __this_cpu_inc(rcu_torture_count[pipe_count]);
  919. completed = cur_ops->completed() - completed;
  920. if (completed > RCU_TORTURE_PIPE_LEN) {
  921. /* Should not happen, but... */
  922. completed = RCU_TORTURE_PIPE_LEN;
  923. }
  924. __this_cpu_inc(rcu_torture_batch[completed]);
  925. preempt_enable();
  926. cur_ops->readunlock(idx);
  927. }
  928. /*
  929. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  930. * incrementing the corresponding element of the pipeline array. The
  931. * counter in the element should never be greater than 1, otherwise, the
  932. * RCU implementation is broken.
  933. */
  934. static int
  935. rcu_torture_reader(void *arg)
  936. {
  937. int completed;
  938. int idx;
  939. DEFINE_RCU_RANDOM(rand);
  940. struct rcu_torture *p;
  941. int pipe_count;
  942. struct timer_list t;
  943. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  944. set_user_nice(current, 19);
  945. if (irqreader && cur_ops->irq_capable)
  946. setup_timer_on_stack(&t, rcu_torture_timer, 0);
  947. do {
  948. if (irqreader && cur_ops->irq_capable) {
  949. if (!timer_pending(&t))
  950. mod_timer(&t, jiffies + 1);
  951. }
  952. idx = cur_ops->readlock();
  953. completed = cur_ops->completed();
  954. p = rcu_dereference_check(rcu_torture_current,
  955. rcu_read_lock_bh_held() ||
  956. rcu_read_lock_sched_held() ||
  957. srcu_read_lock_held(&srcu_ctl));
  958. if (p == NULL) {
  959. /* Wait for rcu_torture_writer to get underway */
  960. cur_ops->readunlock(idx);
  961. schedule_timeout_interruptible(HZ);
  962. continue;
  963. }
  964. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu);
  965. if (p->rtort_mbtest == 0)
  966. atomic_inc(&n_rcu_torture_mberror);
  967. cur_ops->read_delay(&rand);
  968. preempt_disable();
  969. pipe_count = p->rtort_pipe_count;
  970. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  971. /* Should not happen, but... */
  972. pipe_count = RCU_TORTURE_PIPE_LEN;
  973. }
  974. if (pipe_count > 1)
  975. rcutorture_trace_dump();
  976. __this_cpu_inc(rcu_torture_count[pipe_count]);
  977. completed = cur_ops->completed() - completed;
  978. if (completed > RCU_TORTURE_PIPE_LEN) {
  979. /* Should not happen, but... */
  980. completed = RCU_TORTURE_PIPE_LEN;
  981. }
  982. __this_cpu_inc(rcu_torture_batch[completed]);
  983. preempt_enable();
  984. cur_ops->readunlock(idx);
  985. schedule();
  986. rcu_stutter_wait("rcu_torture_reader");
  987. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  988. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  989. rcutorture_shutdown_absorb("rcu_torture_reader");
  990. if (irqreader && cur_ops->irq_capable)
  991. del_timer_sync(&t);
  992. while (!kthread_should_stop())
  993. schedule_timeout_uninterruptible(1);
  994. return 0;
  995. }
  996. /*
  997. * Create an RCU-torture statistics message in the specified buffer.
  998. */
  999. static int
  1000. rcu_torture_printk(char *page)
  1001. {
  1002. int cnt = 0;
  1003. int cpu;
  1004. int i;
  1005. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  1006. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  1007. for_each_possible_cpu(cpu) {
  1008. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1009. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  1010. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  1011. }
  1012. }
  1013. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  1014. if (pipesummary[i] != 0)
  1015. break;
  1016. }
  1017. cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
  1018. cnt += sprintf(&page[cnt],
  1019. "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d "
  1020. "rtmbe: %d rtbke: %ld rtbre: %ld "
  1021. "rtbf: %ld rtb: %ld nt: %ld "
  1022. "onoff: %ld/%ld:%ld/%ld",
  1023. rcu_torture_current,
  1024. rcu_torture_current_version,
  1025. list_empty(&rcu_torture_freelist),
  1026. atomic_read(&n_rcu_torture_alloc),
  1027. atomic_read(&n_rcu_torture_alloc_fail),
  1028. atomic_read(&n_rcu_torture_free),
  1029. atomic_read(&n_rcu_torture_mberror),
  1030. n_rcu_torture_boost_ktrerror,
  1031. n_rcu_torture_boost_rterror,
  1032. n_rcu_torture_boost_failure,
  1033. n_rcu_torture_boosts,
  1034. n_rcu_torture_timers,
  1035. n_online_successes,
  1036. n_online_attempts,
  1037. n_offline_successes,
  1038. n_offline_attempts);
  1039. if (atomic_read(&n_rcu_torture_mberror) != 0 ||
  1040. n_rcu_torture_boost_ktrerror != 0 ||
  1041. n_rcu_torture_boost_rterror != 0 ||
  1042. n_rcu_torture_boost_failure != 0)
  1043. cnt += sprintf(&page[cnt], " !!!");
  1044. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1045. if (i > 1) {
  1046. cnt += sprintf(&page[cnt], "!!! ");
  1047. atomic_inc(&n_rcu_torture_error);
  1048. WARN_ON_ONCE(1);
  1049. }
  1050. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  1051. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1052. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  1053. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1054. cnt += sprintf(&page[cnt], "Reader Batch: ");
  1055. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1056. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  1057. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1058. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  1059. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1060. cnt += sprintf(&page[cnt], " %d",
  1061. atomic_read(&rcu_torture_wcount[i]));
  1062. }
  1063. cnt += sprintf(&page[cnt], "\n");
  1064. if (cur_ops->stats)
  1065. cnt += cur_ops->stats(&page[cnt]);
  1066. return cnt;
  1067. }
  1068. /*
  1069. * Print torture statistics. Caller must ensure that there is only
  1070. * one call to this function at a given time!!! This is normally
  1071. * accomplished by relying on the module system to only have one copy
  1072. * of the module loaded, and then by giving the rcu_torture_stats
  1073. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  1074. * thread is not running).
  1075. */
  1076. static void
  1077. rcu_torture_stats_print(void)
  1078. {
  1079. int cnt;
  1080. cnt = rcu_torture_printk(printk_buf);
  1081. printk(KERN_ALERT "%s", printk_buf);
  1082. }
  1083. /*
  1084. * Periodically prints torture statistics, if periodic statistics printing
  1085. * was specified via the stat_interval module parameter.
  1086. *
  1087. * No need to worry about fullstop here, since this one doesn't reference
  1088. * volatile state or register callbacks.
  1089. */
  1090. static int
  1091. rcu_torture_stats(void *arg)
  1092. {
  1093. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  1094. do {
  1095. schedule_timeout_interruptible(stat_interval * HZ);
  1096. rcu_torture_stats_print();
  1097. rcutorture_shutdown_absorb("rcu_torture_stats");
  1098. } while (!kthread_should_stop());
  1099. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  1100. return 0;
  1101. }
  1102. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  1103. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  1104. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  1105. */
  1106. static void rcu_torture_shuffle_tasks(void)
  1107. {
  1108. int i;
  1109. cpumask_setall(shuffle_tmp_mask);
  1110. get_online_cpus();
  1111. /* No point in shuffling if there is only one online CPU (ex: UP) */
  1112. if (num_online_cpus() == 1) {
  1113. put_online_cpus();
  1114. return;
  1115. }
  1116. if (rcu_idle_cpu != -1)
  1117. cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
  1118. set_cpus_allowed_ptr(current, shuffle_tmp_mask);
  1119. if (reader_tasks) {
  1120. for (i = 0; i < nrealreaders; i++)
  1121. if (reader_tasks[i])
  1122. set_cpus_allowed_ptr(reader_tasks[i],
  1123. shuffle_tmp_mask);
  1124. }
  1125. if (fakewriter_tasks) {
  1126. for (i = 0; i < nfakewriters; i++)
  1127. if (fakewriter_tasks[i])
  1128. set_cpus_allowed_ptr(fakewriter_tasks[i],
  1129. shuffle_tmp_mask);
  1130. }
  1131. if (writer_task)
  1132. set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
  1133. if (stats_task)
  1134. set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
  1135. if (rcu_idle_cpu == -1)
  1136. rcu_idle_cpu = num_online_cpus() - 1;
  1137. else
  1138. rcu_idle_cpu--;
  1139. put_online_cpus();
  1140. }
  1141. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  1142. * system to become idle at a time and cut off its timer ticks. This is meant
  1143. * to test the support for such tickless idle CPU in RCU.
  1144. */
  1145. static int
  1146. rcu_torture_shuffle(void *arg)
  1147. {
  1148. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  1149. do {
  1150. schedule_timeout_interruptible(shuffle_interval * HZ);
  1151. rcu_torture_shuffle_tasks();
  1152. rcutorture_shutdown_absorb("rcu_torture_shuffle");
  1153. } while (!kthread_should_stop());
  1154. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  1155. return 0;
  1156. }
  1157. /* Cause the rcutorture test to "stutter", starting and stopping all
  1158. * threads periodically.
  1159. */
  1160. static int
  1161. rcu_torture_stutter(void *arg)
  1162. {
  1163. VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
  1164. do {
  1165. schedule_timeout_interruptible(stutter * HZ);
  1166. stutter_pause_test = 1;
  1167. if (!kthread_should_stop())
  1168. schedule_timeout_interruptible(stutter * HZ);
  1169. stutter_pause_test = 0;
  1170. rcutorture_shutdown_absorb("rcu_torture_stutter");
  1171. } while (!kthread_should_stop());
  1172. VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
  1173. return 0;
  1174. }
  1175. static inline void
  1176. rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
  1177. {
  1178. printk(KERN_ALERT "%s" TORTURE_FLAG
  1179. "--- %s: nreaders=%d nfakewriters=%d "
  1180. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  1181. "shuffle_interval=%d stutter=%d irqreader=%d "
  1182. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
  1183. "test_boost=%d/%d test_boost_interval=%d "
  1184. "test_boost_duration=%d shutdown_secs=%d "
  1185. "onoff_interval=%d onoff_holdoff=%d\n",
  1186. torture_type, tag, nrealreaders, nfakewriters,
  1187. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  1188. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
  1189. test_boost, cur_ops->can_boost,
  1190. test_boost_interval, test_boost_duration, shutdown_secs,
  1191. onoff_interval, onoff_holdoff);
  1192. }
  1193. static struct notifier_block rcutorture_shutdown_nb = {
  1194. .notifier_call = rcutorture_shutdown_notify,
  1195. };
  1196. static void rcutorture_booster_cleanup(int cpu)
  1197. {
  1198. struct task_struct *t;
  1199. if (boost_tasks[cpu] == NULL)
  1200. return;
  1201. mutex_lock(&boost_mutex);
  1202. VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
  1203. t = boost_tasks[cpu];
  1204. boost_tasks[cpu] = NULL;
  1205. mutex_unlock(&boost_mutex);
  1206. /* This must be outside of the mutex, otherwise deadlock! */
  1207. kthread_stop(t);
  1208. }
  1209. static int rcutorture_booster_init(int cpu)
  1210. {
  1211. int retval;
  1212. if (boost_tasks[cpu] != NULL)
  1213. return 0; /* Already created, nothing more to do. */
  1214. /* Don't allow time recalculation while creating a new task. */
  1215. mutex_lock(&boost_mutex);
  1216. VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
  1217. boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
  1218. cpu_to_node(cpu),
  1219. "rcu_torture_boost");
  1220. if (IS_ERR(boost_tasks[cpu])) {
  1221. retval = PTR_ERR(boost_tasks[cpu]);
  1222. VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
  1223. n_rcu_torture_boost_ktrerror++;
  1224. boost_tasks[cpu] = NULL;
  1225. mutex_unlock(&boost_mutex);
  1226. return retval;
  1227. }
  1228. kthread_bind(boost_tasks[cpu], cpu);
  1229. wake_up_process(boost_tasks[cpu]);
  1230. mutex_unlock(&boost_mutex);
  1231. return 0;
  1232. }
  1233. /*
  1234. * Cause the rcutorture test to shutdown the system after the test has
  1235. * run for the time specified by the shutdown_secs module parameter.
  1236. */
  1237. static int
  1238. rcu_torture_shutdown(void *arg)
  1239. {
  1240. long delta;
  1241. unsigned long jiffies_snap;
  1242. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
  1243. jiffies_snap = ACCESS_ONCE(jiffies);
  1244. while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
  1245. !kthread_should_stop()) {
  1246. delta = shutdown_time - jiffies_snap;
  1247. if (verbose)
  1248. printk(KERN_ALERT "%s" TORTURE_FLAG
  1249. "rcu_torture_shutdown task: %lu "
  1250. "jiffies remaining\n",
  1251. torture_type, delta);
  1252. schedule_timeout_interruptible(delta);
  1253. jiffies_snap = ACCESS_ONCE(jiffies);
  1254. }
  1255. if (kthread_should_stop()) {
  1256. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
  1257. return 0;
  1258. }
  1259. /* OK, shut down the system. */
  1260. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
  1261. shutdown_task = NULL; /* Avoid self-kill deadlock. */
  1262. rcu_torture_cleanup(); /* Get the success/failure message. */
  1263. kernel_power_off(); /* Shut down the system. */
  1264. return 0;
  1265. }
  1266. #ifdef CONFIG_HOTPLUG_CPU
  1267. /*
  1268. * Execute random CPU-hotplug operations at the interval specified
  1269. * by the onoff_interval.
  1270. */
  1271. static int __cpuinit
  1272. rcu_torture_onoff(void *arg)
  1273. {
  1274. int cpu;
  1275. int maxcpu = -1;
  1276. DEFINE_RCU_RANDOM(rand);
  1277. VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
  1278. for_each_online_cpu(cpu)
  1279. maxcpu = cpu;
  1280. WARN_ON(maxcpu < 0);
  1281. if (onoff_holdoff > 0) {
  1282. VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
  1283. schedule_timeout_interruptible(onoff_holdoff * HZ);
  1284. VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
  1285. }
  1286. while (!kthread_should_stop()) {
  1287. cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
  1288. if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
  1289. if (verbose)
  1290. printk(KERN_ALERT "%s" TORTURE_FLAG
  1291. "rcu_torture_onoff task: offlining %d\n",
  1292. torture_type, cpu);
  1293. n_offline_attempts++;
  1294. if (cpu_down(cpu) == 0) {
  1295. if (verbose)
  1296. printk(KERN_ALERT "%s" TORTURE_FLAG
  1297. "rcu_torture_onoff task: "
  1298. "offlined %d\n",
  1299. torture_type, cpu);
  1300. n_offline_successes++;
  1301. }
  1302. } else if (cpu_is_hotpluggable(cpu)) {
  1303. if (verbose)
  1304. printk(KERN_ALERT "%s" TORTURE_FLAG
  1305. "rcu_torture_onoff task: onlining %d\n",
  1306. torture_type, cpu);
  1307. n_online_attempts++;
  1308. if (cpu_up(cpu) == 0) {
  1309. if (verbose)
  1310. printk(KERN_ALERT "%s" TORTURE_FLAG
  1311. "rcu_torture_onoff task: "
  1312. "onlined %d\n",
  1313. torture_type, cpu);
  1314. n_online_successes++;
  1315. }
  1316. }
  1317. schedule_timeout_interruptible(onoff_interval * HZ);
  1318. }
  1319. VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
  1320. return 0;
  1321. }
  1322. static int __cpuinit
  1323. rcu_torture_onoff_init(void)
  1324. {
  1325. int ret;
  1326. if (onoff_interval <= 0)
  1327. return 0;
  1328. onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
  1329. if (IS_ERR(onoff_task)) {
  1330. ret = PTR_ERR(onoff_task);
  1331. onoff_task = NULL;
  1332. return ret;
  1333. }
  1334. return 0;
  1335. }
  1336. static void rcu_torture_onoff_cleanup(void)
  1337. {
  1338. if (onoff_task == NULL)
  1339. return;
  1340. VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
  1341. kthread_stop(onoff_task);
  1342. }
  1343. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  1344. static void
  1345. rcu_torture_onoff_init(void)
  1346. {
  1347. }
  1348. static void rcu_torture_onoff_cleanup(void)
  1349. {
  1350. }
  1351. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  1352. /*
  1353. * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
  1354. * induces a CPU stall for the time specified by stall_cpu.
  1355. */
  1356. static int __cpuinit rcu_torture_stall(void *args)
  1357. {
  1358. unsigned long stop_at;
  1359. VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
  1360. if (stall_cpu_holdoff > 0) {
  1361. VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
  1362. schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
  1363. VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
  1364. }
  1365. if (!kthread_should_stop()) {
  1366. stop_at = get_seconds() + stall_cpu;
  1367. /* RCU CPU stall is expected behavior in following code. */
  1368. printk(KERN_ALERT "rcu_torture_stall start.\n");
  1369. rcu_read_lock();
  1370. preempt_disable();
  1371. while (ULONG_CMP_LT(get_seconds(), stop_at))
  1372. continue; /* Induce RCU CPU stall warning. */
  1373. preempt_enable();
  1374. rcu_read_unlock();
  1375. printk(KERN_ALERT "rcu_torture_stall end.\n");
  1376. }
  1377. rcutorture_shutdown_absorb("rcu_torture_stall");
  1378. while (!kthread_should_stop())
  1379. schedule_timeout_interruptible(10 * HZ);
  1380. return 0;
  1381. }
  1382. /* Spawn CPU-stall kthread, if stall_cpu specified. */
  1383. static int __init rcu_torture_stall_init(void)
  1384. {
  1385. int ret;
  1386. if (stall_cpu <= 0)
  1387. return 0;
  1388. stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
  1389. if (IS_ERR(stall_task)) {
  1390. ret = PTR_ERR(stall_task);
  1391. stall_task = NULL;
  1392. return ret;
  1393. }
  1394. return 0;
  1395. }
  1396. /* Clean up after the CPU-stall kthread, if one was spawned. */
  1397. static void rcu_torture_stall_cleanup(void)
  1398. {
  1399. if (stall_task == NULL)
  1400. return;
  1401. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
  1402. kthread_stop(stall_task);
  1403. }
  1404. static int rcutorture_cpu_notify(struct notifier_block *self,
  1405. unsigned long action, void *hcpu)
  1406. {
  1407. long cpu = (long)hcpu;
  1408. switch (action) {
  1409. case CPU_ONLINE:
  1410. case CPU_DOWN_FAILED:
  1411. (void)rcutorture_booster_init(cpu);
  1412. break;
  1413. case CPU_DOWN_PREPARE:
  1414. rcutorture_booster_cleanup(cpu);
  1415. break;
  1416. default:
  1417. break;
  1418. }
  1419. return NOTIFY_OK;
  1420. }
  1421. static struct notifier_block rcutorture_cpu_nb = {
  1422. .notifier_call = rcutorture_cpu_notify,
  1423. };
  1424. static void
  1425. rcu_torture_cleanup(void)
  1426. {
  1427. int i;
  1428. mutex_lock(&fullstop_mutex);
  1429. rcutorture_record_test_transition();
  1430. if (fullstop == FULLSTOP_SHUTDOWN) {
  1431. printk(KERN_WARNING /* but going down anyway, so... */
  1432. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  1433. mutex_unlock(&fullstop_mutex);
  1434. schedule_timeout_uninterruptible(10);
  1435. if (cur_ops->cb_barrier != NULL)
  1436. cur_ops->cb_barrier();
  1437. return;
  1438. }
  1439. fullstop = FULLSTOP_RMMOD;
  1440. mutex_unlock(&fullstop_mutex);
  1441. unregister_reboot_notifier(&rcutorture_shutdown_nb);
  1442. rcu_torture_stall_cleanup();
  1443. if (stutter_task) {
  1444. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
  1445. kthread_stop(stutter_task);
  1446. }
  1447. stutter_task = NULL;
  1448. if (shuffler_task) {
  1449. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  1450. kthread_stop(shuffler_task);
  1451. free_cpumask_var(shuffle_tmp_mask);
  1452. }
  1453. shuffler_task = NULL;
  1454. if (writer_task) {
  1455. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  1456. kthread_stop(writer_task);
  1457. }
  1458. writer_task = NULL;
  1459. if (reader_tasks) {
  1460. for (i = 0; i < nrealreaders; i++) {
  1461. if (reader_tasks[i]) {
  1462. VERBOSE_PRINTK_STRING(
  1463. "Stopping rcu_torture_reader task");
  1464. kthread_stop(reader_tasks[i]);
  1465. }
  1466. reader_tasks[i] = NULL;
  1467. }
  1468. kfree(reader_tasks);
  1469. reader_tasks = NULL;
  1470. }
  1471. rcu_torture_current = NULL;
  1472. if (fakewriter_tasks) {
  1473. for (i = 0; i < nfakewriters; i++) {
  1474. if (fakewriter_tasks[i]) {
  1475. VERBOSE_PRINTK_STRING(
  1476. "Stopping rcu_torture_fakewriter task");
  1477. kthread_stop(fakewriter_tasks[i]);
  1478. }
  1479. fakewriter_tasks[i] = NULL;
  1480. }
  1481. kfree(fakewriter_tasks);
  1482. fakewriter_tasks = NULL;
  1483. }
  1484. if (stats_task) {
  1485. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  1486. kthread_stop(stats_task);
  1487. }
  1488. stats_task = NULL;
  1489. if (fqs_task) {
  1490. VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
  1491. kthread_stop(fqs_task);
  1492. }
  1493. fqs_task = NULL;
  1494. if ((test_boost == 1 && cur_ops->can_boost) ||
  1495. test_boost == 2) {
  1496. unregister_cpu_notifier(&rcutorture_cpu_nb);
  1497. for_each_possible_cpu(i)
  1498. rcutorture_booster_cleanup(i);
  1499. }
  1500. if (shutdown_task != NULL) {
  1501. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
  1502. kthread_stop(shutdown_task);
  1503. }
  1504. rcu_torture_onoff_cleanup();
  1505. /* Wait for all RCU callbacks to fire. */
  1506. if (cur_ops->cb_barrier != NULL)
  1507. cur_ops->cb_barrier();
  1508. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  1509. if (cur_ops->cleanup)
  1510. cur_ops->cleanup();
  1511. if (atomic_read(&n_rcu_torture_error))
  1512. rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
  1513. else if (n_online_successes != n_online_attempts ||
  1514. n_offline_successes != n_offline_attempts)
  1515. rcu_torture_print_module_parms(cur_ops,
  1516. "End of test: RCU_HOTPLUG");
  1517. else
  1518. rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
  1519. }
  1520. static int __init
  1521. rcu_torture_init(void)
  1522. {
  1523. int i;
  1524. int cpu;
  1525. int firsterr = 0;
  1526. static struct rcu_torture_ops *torture_ops[] =
  1527. { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
  1528. &rcu_bh_ops, &rcu_bh_sync_ops, &rcu_bh_expedited_ops,
  1529. &srcu_ops, &srcu_raw_ops, &srcu_expedited_ops,
  1530. &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
  1531. mutex_lock(&fullstop_mutex);
  1532. /* Process args and tell the world that the torturer is on the job. */
  1533. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1534. cur_ops = torture_ops[i];
  1535. if (strcmp(torture_type, cur_ops->name) == 0)
  1536. break;
  1537. }
  1538. if (i == ARRAY_SIZE(torture_ops)) {
  1539. printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
  1540. torture_type);
  1541. printk(KERN_ALERT "rcu-torture types:");
  1542. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1543. printk(KERN_ALERT " %s", torture_ops[i]->name);
  1544. printk(KERN_ALERT "\n");
  1545. mutex_unlock(&fullstop_mutex);
  1546. return -EINVAL;
  1547. }
  1548. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  1549. printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
  1550. "fqs_duration, fqs disabled.\n");
  1551. fqs_duration = 0;
  1552. }
  1553. if (cur_ops->init)
  1554. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  1555. if (nreaders >= 0)
  1556. nrealreaders = nreaders;
  1557. else
  1558. nrealreaders = 2 * num_online_cpus();
  1559. rcu_torture_print_module_parms(cur_ops, "Start of test");
  1560. fullstop = FULLSTOP_DONTSTOP;
  1561. /* Set up the freelist. */
  1562. INIT_LIST_HEAD(&rcu_torture_freelist);
  1563. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  1564. rcu_tortures[i].rtort_mbtest = 0;
  1565. list_add_tail(&rcu_tortures[i].rtort_free,
  1566. &rcu_torture_freelist);
  1567. }
  1568. /* Initialize the statistics so that each run gets its own numbers. */
  1569. rcu_torture_current = NULL;
  1570. rcu_torture_current_version = 0;
  1571. atomic_set(&n_rcu_torture_alloc, 0);
  1572. atomic_set(&n_rcu_torture_alloc_fail, 0);
  1573. atomic_set(&n_rcu_torture_free, 0);
  1574. atomic_set(&n_rcu_torture_mberror, 0);
  1575. atomic_set(&n_rcu_torture_error, 0);
  1576. n_rcu_torture_boost_ktrerror = 0;
  1577. n_rcu_torture_boost_rterror = 0;
  1578. n_rcu_torture_boost_failure = 0;
  1579. n_rcu_torture_boosts = 0;
  1580. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1581. atomic_set(&rcu_torture_wcount[i], 0);
  1582. for_each_possible_cpu(cpu) {
  1583. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1584. per_cpu(rcu_torture_count, cpu)[i] = 0;
  1585. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  1586. }
  1587. }
  1588. /* Start up the kthreads. */
  1589. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  1590. writer_task = kthread_run(rcu_torture_writer, NULL,
  1591. "rcu_torture_writer");
  1592. if (IS_ERR(writer_task)) {
  1593. firsterr = PTR_ERR(writer_task);
  1594. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  1595. writer_task = NULL;
  1596. goto unwind;
  1597. }
  1598. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  1599. GFP_KERNEL);
  1600. if (fakewriter_tasks == NULL) {
  1601. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1602. firsterr = -ENOMEM;
  1603. goto unwind;
  1604. }
  1605. for (i = 0; i < nfakewriters; i++) {
  1606. VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
  1607. fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
  1608. "rcu_torture_fakewriter");
  1609. if (IS_ERR(fakewriter_tasks[i])) {
  1610. firsterr = PTR_ERR(fakewriter_tasks[i]);
  1611. VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
  1612. fakewriter_tasks[i] = NULL;
  1613. goto unwind;
  1614. }
  1615. }
  1616. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  1617. GFP_KERNEL);
  1618. if (reader_tasks == NULL) {
  1619. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1620. firsterr = -ENOMEM;
  1621. goto unwind;
  1622. }
  1623. for (i = 0; i < nrealreaders; i++) {
  1624. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  1625. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  1626. "rcu_torture_reader");
  1627. if (IS_ERR(reader_tasks[i])) {
  1628. firsterr = PTR_ERR(reader_tasks[i]);
  1629. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  1630. reader_tasks[i] = NULL;
  1631. goto unwind;
  1632. }
  1633. }
  1634. if (stat_interval > 0) {
  1635. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  1636. stats_task = kthread_run(rcu_torture_stats, NULL,
  1637. "rcu_torture_stats");
  1638. if (IS_ERR(stats_task)) {
  1639. firsterr = PTR_ERR(stats_task);
  1640. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  1641. stats_task = NULL;
  1642. goto unwind;
  1643. }
  1644. }
  1645. if (test_no_idle_hz) {
  1646. rcu_idle_cpu = num_online_cpus() - 1;
  1647. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  1648. firsterr = -ENOMEM;
  1649. VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
  1650. goto unwind;
  1651. }
  1652. /* Create the shuffler thread */
  1653. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  1654. "rcu_torture_shuffle");
  1655. if (IS_ERR(shuffler_task)) {
  1656. free_cpumask_var(shuffle_tmp_mask);
  1657. firsterr = PTR_ERR(shuffler_task);
  1658. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  1659. shuffler_task = NULL;
  1660. goto unwind;
  1661. }
  1662. }
  1663. if (stutter < 0)
  1664. stutter = 0;
  1665. if (stutter) {
  1666. /* Create the stutter thread */
  1667. stutter_task = kthread_run(rcu_torture_stutter, NULL,
  1668. "rcu_torture_stutter");
  1669. if (IS_ERR(stutter_task)) {
  1670. firsterr = PTR_ERR(stutter_task);
  1671. VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
  1672. stutter_task = NULL;
  1673. goto unwind;
  1674. }
  1675. }
  1676. if (fqs_duration < 0)
  1677. fqs_duration = 0;
  1678. if (fqs_duration) {
  1679. /* Create the stutter thread */
  1680. fqs_task = kthread_run(rcu_torture_fqs, NULL,
  1681. "rcu_torture_fqs");
  1682. if (IS_ERR(fqs_task)) {
  1683. firsterr = PTR_ERR(fqs_task);
  1684. VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
  1685. fqs_task = NULL;
  1686. goto unwind;
  1687. }
  1688. }
  1689. if (test_boost_interval < 1)
  1690. test_boost_interval = 1;
  1691. if (test_boost_duration < 2)
  1692. test_boost_duration = 2;
  1693. if ((test_boost == 1 && cur_ops->can_boost) ||
  1694. test_boost == 2) {
  1695. int retval;
  1696. boost_starttime = jiffies + test_boost_interval * HZ;
  1697. register_cpu_notifier(&rcutorture_cpu_nb);
  1698. for_each_possible_cpu(i) {
  1699. if (cpu_is_offline(i))
  1700. continue; /* Heuristic: CPU can go offline. */
  1701. retval = rcutorture_booster_init(i);
  1702. if (retval < 0) {
  1703. firsterr = retval;
  1704. goto unwind;
  1705. }
  1706. }
  1707. }
  1708. if (shutdown_secs > 0) {
  1709. shutdown_time = jiffies + shutdown_secs * HZ;
  1710. shutdown_task = kthread_run(rcu_torture_shutdown, NULL,
  1711. "rcu_torture_shutdown");
  1712. if (IS_ERR(shutdown_task)) {
  1713. firsterr = PTR_ERR(shutdown_task);
  1714. VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
  1715. shutdown_task = NULL;
  1716. goto unwind;
  1717. }
  1718. }
  1719. rcu_torture_onoff_init();
  1720. register_reboot_notifier(&rcutorture_shutdown_nb);
  1721. rcu_torture_stall_init();
  1722. rcutorture_record_test_transition();
  1723. mutex_unlock(&fullstop_mutex);
  1724. return 0;
  1725. unwind:
  1726. mutex_unlock(&fullstop_mutex);
  1727. rcu_torture_cleanup();
  1728. return firsterr;
  1729. }
  1730. module_init(rcu_torture_init);
  1731. module_exit(rcu_torture_cleanup);