rcutorture.c 46 KB

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