rcutorture.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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 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_allocerror;
  141. static long n_rcu_torture_boost_afferror;
  142. static long n_rcu_torture_boost_failure;
  143. static long n_rcu_torture_boosts;
  144. static long n_rcu_torture_timers;
  145. static struct list_head rcu_torture_removed;
  146. static cpumask_var_t shuffle_tmp_mask;
  147. static int stutter_pause_test;
  148. #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
  149. #define RCUTORTURE_RUNNABLE_INIT 1
  150. #else
  151. #define RCUTORTURE_RUNNABLE_INIT 0
  152. #endif
  153. int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
  154. #ifdef CONFIG_RCU_BOOST
  155. #define rcu_can_boost() 1
  156. #else /* #ifdef CONFIG_RCU_BOOST */
  157. #define rcu_can_boost() 0
  158. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  159. static unsigned long boost_starttime; /* jiffies of next boost test start. */
  160. DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
  161. /* and boost task create/destroy. */
  162. /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
  163. #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
  164. #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
  165. #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
  166. static int fullstop = FULLSTOP_RMMOD;
  167. /*
  168. * Protect fullstop transitions and spawning of kthreads.
  169. */
  170. static DEFINE_MUTEX(fullstop_mutex);
  171. /*
  172. * Detect and respond to a system shutdown.
  173. */
  174. static int
  175. rcutorture_shutdown_notify(struct notifier_block *unused1,
  176. unsigned long unused2, void *unused3)
  177. {
  178. mutex_lock(&fullstop_mutex);
  179. if (fullstop == FULLSTOP_DONTSTOP)
  180. fullstop = FULLSTOP_SHUTDOWN;
  181. else
  182. printk(KERN_WARNING /* but going down anyway, so... */
  183. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  184. mutex_unlock(&fullstop_mutex);
  185. return NOTIFY_DONE;
  186. }
  187. /*
  188. * Absorb kthreads into a kernel function that won't return, so that
  189. * they won't ever access module text or data again.
  190. */
  191. static void rcutorture_shutdown_absorb(char *title)
  192. {
  193. if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  194. printk(KERN_NOTICE
  195. "rcutorture thread %s parking due to system shutdown\n",
  196. title);
  197. schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  198. }
  199. }
  200. /*
  201. * Allocate an element from the rcu_tortures pool.
  202. */
  203. static struct rcu_torture *
  204. rcu_torture_alloc(void)
  205. {
  206. struct list_head *p;
  207. spin_lock_bh(&rcu_torture_lock);
  208. if (list_empty(&rcu_torture_freelist)) {
  209. atomic_inc(&n_rcu_torture_alloc_fail);
  210. spin_unlock_bh(&rcu_torture_lock);
  211. return NULL;
  212. }
  213. atomic_inc(&n_rcu_torture_alloc);
  214. p = rcu_torture_freelist.next;
  215. list_del_init(p);
  216. spin_unlock_bh(&rcu_torture_lock);
  217. return container_of(p, struct rcu_torture, rtort_free);
  218. }
  219. /*
  220. * Free an element to the rcu_tortures pool.
  221. */
  222. static void
  223. rcu_torture_free(struct rcu_torture *p)
  224. {
  225. atomic_inc(&n_rcu_torture_free);
  226. spin_lock_bh(&rcu_torture_lock);
  227. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  228. spin_unlock_bh(&rcu_torture_lock);
  229. }
  230. struct rcu_random_state {
  231. unsigned long rrs_state;
  232. long rrs_count;
  233. };
  234. #define RCU_RANDOM_MULT 39916801 /* prime */
  235. #define RCU_RANDOM_ADD 479001701 /* prime */
  236. #define RCU_RANDOM_REFRESH 10000
  237. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  238. /*
  239. * Crude but fast random-number generator. Uses a linear congruential
  240. * generator, with occasional help from cpu_clock().
  241. */
  242. static unsigned long
  243. rcu_random(struct rcu_random_state *rrsp)
  244. {
  245. if (--rrsp->rrs_count < 0) {
  246. rrsp->rrs_state += (unsigned long)local_clock();
  247. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  248. }
  249. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  250. return swahw32(rrsp->rrs_state);
  251. }
  252. static void
  253. rcu_stutter_wait(char *title)
  254. {
  255. while (stutter_pause_test || !rcutorture_runnable) {
  256. if (rcutorture_runnable)
  257. schedule_timeout_interruptible(1);
  258. else
  259. schedule_timeout_interruptible(round_jiffies_relative(HZ));
  260. rcutorture_shutdown_absorb(title);
  261. }
  262. }
  263. /*
  264. * Operations vector for selecting different types of tests.
  265. */
  266. struct rcu_torture_ops {
  267. void (*init)(void);
  268. void (*cleanup)(void);
  269. int (*readlock)(void);
  270. void (*read_delay)(struct rcu_random_state *rrsp);
  271. void (*readunlock)(int idx);
  272. int (*completed)(void);
  273. void (*deferred_free)(struct rcu_torture *p);
  274. void (*sync)(void);
  275. void (*cb_barrier)(void);
  276. void (*fqs)(void);
  277. int (*stats)(char *page);
  278. int irq_capable;
  279. int can_boost;
  280. char *name;
  281. };
  282. static struct rcu_torture_ops *cur_ops;
  283. /*
  284. * Definitions for rcu torture testing.
  285. */
  286. static int rcu_torture_read_lock(void) __acquires(RCU)
  287. {
  288. rcu_read_lock();
  289. return 0;
  290. }
  291. static void rcu_read_delay(struct rcu_random_state *rrsp)
  292. {
  293. const unsigned long shortdelay_us = 200;
  294. const unsigned long longdelay_ms = 50;
  295. /* We want a short delay sometimes to make a reader delay the grace
  296. * period, and we want a long delay occasionally to trigger
  297. * force_quiescent_state. */
  298. if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
  299. mdelay(longdelay_ms);
  300. if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
  301. udelay(shortdelay_us);
  302. #ifdef CONFIG_PREEMPT
  303. if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
  304. preempt_schedule(); /* No QS if preempt_disable() in effect */
  305. #endif
  306. }
  307. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  308. {
  309. rcu_read_unlock();
  310. }
  311. static int rcu_torture_completed(void)
  312. {
  313. return rcu_batches_completed();
  314. }
  315. static void
  316. rcu_torture_cb(struct rcu_head *p)
  317. {
  318. int i;
  319. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  320. if (fullstop != FULLSTOP_DONTSTOP) {
  321. /* Test is ending, just drop callbacks on the floor. */
  322. /* The next initialization will pick up the pieces. */
  323. return;
  324. }
  325. i = rp->rtort_pipe_count;
  326. if (i > RCU_TORTURE_PIPE_LEN)
  327. i = RCU_TORTURE_PIPE_LEN;
  328. atomic_inc(&rcu_torture_wcount[i]);
  329. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  330. rp->rtort_mbtest = 0;
  331. rcu_torture_free(rp);
  332. } else
  333. cur_ops->deferred_free(rp);
  334. }
  335. static int rcu_no_completed(void)
  336. {
  337. return 0;
  338. }
  339. static void rcu_torture_deferred_free(struct rcu_torture *p)
  340. {
  341. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  342. }
  343. static struct rcu_torture_ops rcu_ops = {
  344. .init = NULL,
  345. .cleanup = NULL,
  346. .readlock = rcu_torture_read_lock,
  347. .read_delay = rcu_read_delay,
  348. .readunlock = rcu_torture_read_unlock,
  349. .completed = rcu_torture_completed,
  350. .deferred_free = rcu_torture_deferred_free,
  351. .sync = synchronize_rcu,
  352. .cb_barrier = rcu_barrier,
  353. .fqs = rcu_force_quiescent_state,
  354. .stats = NULL,
  355. .irq_capable = 1,
  356. .can_boost = rcu_can_boost(),
  357. .name = "rcu"
  358. };
  359. static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
  360. {
  361. int i;
  362. struct rcu_torture *rp;
  363. struct rcu_torture *rp1;
  364. cur_ops->sync();
  365. list_add(&p->rtort_free, &rcu_torture_removed);
  366. list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
  367. i = rp->rtort_pipe_count;
  368. if (i > RCU_TORTURE_PIPE_LEN)
  369. i = RCU_TORTURE_PIPE_LEN;
  370. atomic_inc(&rcu_torture_wcount[i]);
  371. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  372. rp->rtort_mbtest = 0;
  373. list_del(&rp->rtort_free);
  374. rcu_torture_free(rp);
  375. }
  376. }
  377. }
  378. static void rcu_sync_torture_init(void)
  379. {
  380. INIT_LIST_HEAD(&rcu_torture_removed);
  381. }
  382. static struct rcu_torture_ops rcu_sync_ops = {
  383. .init = rcu_sync_torture_init,
  384. .cleanup = NULL,
  385. .readlock = rcu_torture_read_lock,
  386. .read_delay = rcu_read_delay,
  387. .readunlock = rcu_torture_read_unlock,
  388. .completed = rcu_torture_completed,
  389. .deferred_free = rcu_sync_torture_deferred_free,
  390. .sync = synchronize_rcu,
  391. .cb_barrier = NULL,
  392. .fqs = rcu_force_quiescent_state,
  393. .stats = NULL,
  394. .irq_capable = 1,
  395. .can_boost = rcu_can_boost(),
  396. .name = "rcu_sync"
  397. };
  398. static struct rcu_torture_ops rcu_expedited_ops = {
  399. .init = rcu_sync_torture_init,
  400. .cleanup = NULL,
  401. .readlock = rcu_torture_read_lock,
  402. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  403. .readunlock = rcu_torture_read_unlock,
  404. .completed = rcu_no_completed,
  405. .deferred_free = rcu_sync_torture_deferred_free,
  406. .sync = synchronize_rcu_expedited,
  407. .cb_barrier = NULL,
  408. .fqs = rcu_force_quiescent_state,
  409. .stats = NULL,
  410. .irq_capable = 1,
  411. .can_boost = rcu_can_boost(),
  412. .name = "rcu_expedited"
  413. };
  414. /*
  415. * Definitions for rcu_bh torture testing.
  416. */
  417. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  418. {
  419. rcu_read_lock_bh();
  420. return 0;
  421. }
  422. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  423. {
  424. rcu_read_unlock_bh();
  425. }
  426. static int rcu_bh_torture_completed(void)
  427. {
  428. return rcu_batches_completed_bh();
  429. }
  430. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  431. {
  432. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  433. }
  434. struct rcu_bh_torture_synchronize {
  435. struct rcu_head head;
  436. struct completion completion;
  437. };
  438. static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
  439. {
  440. struct rcu_bh_torture_synchronize *rcu;
  441. rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
  442. complete(&rcu->completion);
  443. }
  444. static void rcu_bh_torture_synchronize(void)
  445. {
  446. struct rcu_bh_torture_synchronize rcu;
  447. init_rcu_head_on_stack(&rcu.head);
  448. init_completion(&rcu.completion);
  449. call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
  450. wait_for_completion(&rcu.completion);
  451. destroy_rcu_head_on_stack(&rcu.head);
  452. }
  453. static struct rcu_torture_ops rcu_bh_ops = {
  454. .init = NULL,
  455. .cleanup = NULL,
  456. .readlock = rcu_bh_torture_read_lock,
  457. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  458. .readunlock = rcu_bh_torture_read_unlock,
  459. .completed = rcu_bh_torture_completed,
  460. .deferred_free = rcu_bh_torture_deferred_free,
  461. .sync = rcu_bh_torture_synchronize,
  462. .cb_barrier = rcu_barrier_bh,
  463. .fqs = rcu_bh_force_quiescent_state,
  464. .stats = NULL,
  465. .irq_capable = 1,
  466. .name = "rcu_bh"
  467. };
  468. static struct rcu_torture_ops rcu_bh_sync_ops = {
  469. .init = rcu_sync_torture_init,
  470. .cleanup = NULL,
  471. .readlock = rcu_bh_torture_read_lock,
  472. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  473. .readunlock = rcu_bh_torture_read_unlock,
  474. .completed = rcu_bh_torture_completed,
  475. .deferred_free = rcu_sync_torture_deferred_free,
  476. .sync = rcu_bh_torture_synchronize,
  477. .cb_barrier = NULL,
  478. .fqs = rcu_bh_force_quiescent_state,
  479. .stats = NULL,
  480. .irq_capable = 1,
  481. .name = "rcu_bh_sync"
  482. };
  483. /*
  484. * Definitions for srcu torture testing.
  485. */
  486. static struct srcu_struct srcu_ctl;
  487. static void srcu_torture_init(void)
  488. {
  489. init_srcu_struct(&srcu_ctl);
  490. rcu_sync_torture_init();
  491. }
  492. static void srcu_torture_cleanup(void)
  493. {
  494. synchronize_srcu(&srcu_ctl);
  495. cleanup_srcu_struct(&srcu_ctl);
  496. }
  497. static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
  498. {
  499. return srcu_read_lock(&srcu_ctl);
  500. }
  501. static void srcu_read_delay(struct rcu_random_state *rrsp)
  502. {
  503. long delay;
  504. const long uspertick = 1000000 / HZ;
  505. const long longdelay = 10;
  506. /* We want there to be long-running readers, but not all the time. */
  507. delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
  508. if (!delay)
  509. schedule_timeout_interruptible(longdelay);
  510. else
  511. rcu_read_delay(rrsp);
  512. }
  513. static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
  514. {
  515. srcu_read_unlock(&srcu_ctl, idx);
  516. }
  517. static int srcu_torture_completed(void)
  518. {
  519. return srcu_batches_completed(&srcu_ctl);
  520. }
  521. static void srcu_torture_synchronize(void)
  522. {
  523. synchronize_srcu(&srcu_ctl);
  524. }
  525. static int srcu_torture_stats(char *page)
  526. {
  527. int cnt = 0;
  528. int cpu;
  529. int idx = srcu_ctl.completed & 0x1;
  530. cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
  531. torture_type, TORTURE_FLAG, idx);
  532. for_each_possible_cpu(cpu) {
  533. cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
  534. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
  535. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
  536. }
  537. cnt += sprintf(&page[cnt], "\n");
  538. return cnt;
  539. }
  540. static struct rcu_torture_ops srcu_ops = {
  541. .init = srcu_torture_init,
  542. .cleanup = srcu_torture_cleanup,
  543. .readlock = srcu_torture_read_lock,
  544. .read_delay = srcu_read_delay,
  545. .readunlock = srcu_torture_read_unlock,
  546. .completed = srcu_torture_completed,
  547. .deferred_free = rcu_sync_torture_deferred_free,
  548. .sync = srcu_torture_synchronize,
  549. .cb_barrier = NULL,
  550. .stats = srcu_torture_stats,
  551. .name = "srcu"
  552. };
  553. static void srcu_torture_synchronize_expedited(void)
  554. {
  555. synchronize_srcu_expedited(&srcu_ctl);
  556. }
  557. static struct rcu_torture_ops srcu_expedited_ops = {
  558. .init = srcu_torture_init,
  559. .cleanup = srcu_torture_cleanup,
  560. .readlock = srcu_torture_read_lock,
  561. .read_delay = srcu_read_delay,
  562. .readunlock = srcu_torture_read_unlock,
  563. .completed = srcu_torture_completed,
  564. .deferred_free = rcu_sync_torture_deferred_free,
  565. .sync = srcu_torture_synchronize_expedited,
  566. .cb_barrier = NULL,
  567. .stats = srcu_torture_stats,
  568. .name = "srcu_expedited"
  569. };
  570. /*
  571. * Definitions for sched torture testing.
  572. */
  573. static int sched_torture_read_lock(void)
  574. {
  575. preempt_disable();
  576. return 0;
  577. }
  578. static void sched_torture_read_unlock(int idx)
  579. {
  580. preempt_enable();
  581. }
  582. static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
  583. {
  584. call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
  585. }
  586. static void sched_torture_synchronize(void)
  587. {
  588. synchronize_sched();
  589. }
  590. static struct rcu_torture_ops sched_ops = {
  591. .init = rcu_sync_torture_init,
  592. .cleanup = NULL,
  593. .readlock = sched_torture_read_lock,
  594. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  595. .readunlock = sched_torture_read_unlock,
  596. .completed = rcu_no_completed,
  597. .deferred_free = rcu_sched_torture_deferred_free,
  598. .sync = sched_torture_synchronize,
  599. .cb_barrier = rcu_barrier_sched,
  600. .fqs = rcu_sched_force_quiescent_state,
  601. .stats = NULL,
  602. .irq_capable = 1,
  603. .name = "sched"
  604. };
  605. static struct rcu_torture_ops sched_sync_ops = {
  606. .init = rcu_sync_torture_init,
  607. .cleanup = NULL,
  608. .readlock = sched_torture_read_lock,
  609. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  610. .readunlock = sched_torture_read_unlock,
  611. .completed = rcu_no_completed,
  612. .deferred_free = rcu_sync_torture_deferred_free,
  613. .sync = sched_torture_synchronize,
  614. .cb_barrier = NULL,
  615. .fqs = rcu_sched_force_quiescent_state,
  616. .stats = NULL,
  617. .name = "sched_sync"
  618. };
  619. static struct rcu_torture_ops sched_expedited_ops = {
  620. .init = rcu_sync_torture_init,
  621. .cleanup = NULL,
  622. .readlock = sched_torture_read_lock,
  623. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  624. .readunlock = sched_torture_read_unlock,
  625. .completed = rcu_no_completed,
  626. .deferred_free = rcu_sync_torture_deferred_free,
  627. .sync = synchronize_sched_expedited,
  628. .cb_barrier = NULL,
  629. .fqs = rcu_sched_force_quiescent_state,
  630. .stats = NULL,
  631. .irq_capable = 1,
  632. .name = "sched_expedited"
  633. };
  634. /*
  635. * RCU torture priority-boost testing. Runs one real-time thread per
  636. * CPU for moderate bursts, repeatedly registering RCU callbacks and
  637. * spinning waiting for them to be invoked. If a given callback takes
  638. * too long to be invoked, we assume that priority inversion has occurred.
  639. */
  640. struct rcu_boost_inflight {
  641. struct rcu_head rcu;
  642. int inflight;
  643. };
  644. static void rcu_torture_boost_cb(struct rcu_head *head)
  645. {
  646. struct rcu_boost_inflight *rbip =
  647. container_of(head, struct rcu_boost_inflight, rcu);
  648. smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
  649. rbip->inflight = 0;
  650. }
  651. static int rcu_torture_boost(void *arg)
  652. {
  653. unsigned long call_rcu_time;
  654. unsigned long endtime;
  655. unsigned long oldstarttime;
  656. struct rcu_boost_inflight rbi = { .inflight = 0 };
  657. struct sched_param sp;
  658. VERBOSE_PRINTK_STRING("rcu_torture_boost started");
  659. /* Set real-time priority. */
  660. sp.sched_priority = 1;
  661. if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
  662. VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
  663. n_rcu_torture_boost_rterror++;
  664. }
  665. /* Each pass through the following loop does one boost-test cycle. */
  666. do {
  667. /* Wait for the next test interval. */
  668. oldstarttime = boost_starttime;
  669. while (jiffies - oldstarttime > ULONG_MAX / 2) {
  670. schedule_timeout_uninterruptible(1);
  671. rcu_stutter_wait("rcu_torture_boost");
  672. if (kthread_should_stop() ||
  673. fullstop != FULLSTOP_DONTSTOP)
  674. goto checkwait;
  675. }
  676. /* Do one boost-test interval. */
  677. endtime = oldstarttime + test_boost_duration * HZ;
  678. call_rcu_time = jiffies;
  679. while (jiffies - endtime > ULONG_MAX / 2) {
  680. /* If we don't have a callback in flight, post one. */
  681. if (!rbi.inflight) {
  682. smp_mb(); /* RCU core before ->inflight = 1. */
  683. rbi.inflight = 1;
  684. call_rcu(&rbi.rcu, rcu_torture_boost_cb);
  685. if (jiffies - call_rcu_time >
  686. test_boost_duration * HZ - HZ / 2) {
  687. VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
  688. n_rcu_torture_boost_failure++;
  689. }
  690. call_rcu_time = jiffies;
  691. }
  692. cond_resched();
  693. rcu_stutter_wait("rcu_torture_boost");
  694. if (kthread_should_stop() ||
  695. fullstop != FULLSTOP_DONTSTOP)
  696. goto checkwait;
  697. }
  698. /*
  699. * Set the start time of the next test interval.
  700. * Yes, this is vulnerable to long delays, but such
  701. * delays simply cause a false negative for the next
  702. * interval. Besides, we are running at RT priority,
  703. * so delays should be relatively rare.
  704. */
  705. while (oldstarttime == boost_starttime) {
  706. if (mutex_trylock(&boost_mutex)) {
  707. boost_starttime = jiffies +
  708. test_boost_interval * HZ;
  709. n_rcu_torture_boosts++;
  710. mutex_unlock(&boost_mutex);
  711. break;
  712. }
  713. schedule_timeout_uninterruptible(1);
  714. }
  715. /* Go do the stutter. */
  716. checkwait: rcu_stutter_wait("rcu_torture_boost");
  717. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  718. /* Clean up and exit. */
  719. VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
  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. 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: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
  961. "rtmbe: %d rtbke: %ld rtbre: %ld rtbae: %ld rtbafe: %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_allocerror,
  973. n_rcu_torture_boost_afferror,
  974. n_rcu_torture_boost_failure,
  975. n_rcu_torture_boosts,
  976. n_rcu_torture_timers);
  977. if (atomic_read(&n_rcu_torture_mberror) != 0 ||
  978. n_rcu_torture_boost_ktrerror != 0 ||
  979. n_rcu_torture_boost_rterror != 0 ||
  980. n_rcu_torture_boost_allocerror != 0 ||
  981. n_rcu_torture_boost_afferror != 0 ||
  982. n_rcu_torture_boost_failure != 0)
  983. cnt += sprintf(&page[cnt], " !!!");
  984. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  985. if (i > 1) {
  986. cnt += sprintf(&page[cnt], "!!! ");
  987. atomic_inc(&n_rcu_torture_error);
  988. WARN_ON_ONCE(1);
  989. }
  990. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  991. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  992. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  993. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  994. cnt += sprintf(&page[cnt], "Reader Batch: ");
  995. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  996. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  997. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  998. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  999. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1000. cnt += sprintf(&page[cnt], " %d",
  1001. atomic_read(&rcu_torture_wcount[i]));
  1002. }
  1003. cnt += sprintf(&page[cnt], "\n");
  1004. if (cur_ops->stats)
  1005. cnt += cur_ops->stats(&page[cnt]);
  1006. return cnt;
  1007. }
  1008. /*
  1009. * Print torture statistics. Caller must ensure that there is only
  1010. * one call to this function at a given time!!! This is normally
  1011. * accomplished by relying on the module system to only have one copy
  1012. * of the module loaded, and then by giving the rcu_torture_stats
  1013. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  1014. * thread is not running).
  1015. */
  1016. static void
  1017. rcu_torture_stats_print(void)
  1018. {
  1019. int cnt;
  1020. cnt = rcu_torture_printk(printk_buf);
  1021. printk(KERN_ALERT "%s", printk_buf);
  1022. }
  1023. /*
  1024. * Periodically prints torture statistics, if periodic statistics printing
  1025. * was specified via the stat_interval module parameter.
  1026. *
  1027. * No need to worry about fullstop here, since this one doesn't reference
  1028. * volatile state or register callbacks.
  1029. */
  1030. static int
  1031. rcu_torture_stats(void *arg)
  1032. {
  1033. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  1034. do {
  1035. schedule_timeout_interruptible(stat_interval * HZ);
  1036. rcu_torture_stats_print();
  1037. rcutorture_shutdown_absorb("rcu_torture_stats");
  1038. } while (!kthread_should_stop());
  1039. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  1040. return 0;
  1041. }
  1042. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  1043. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  1044. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  1045. */
  1046. static void rcu_torture_shuffle_tasks(void)
  1047. {
  1048. int i;
  1049. cpumask_setall(shuffle_tmp_mask);
  1050. get_online_cpus();
  1051. /* No point in shuffling if there is only one online CPU (ex: UP) */
  1052. if (num_online_cpus() == 1) {
  1053. put_online_cpus();
  1054. return;
  1055. }
  1056. if (rcu_idle_cpu != -1)
  1057. cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
  1058. set_cpus_allowed_ptr(current, shuffle_tmp_mask);
  1059. if (reader_tasks) {
  1060. for (i = 0; i < nrealreaders; i++)
  1061. if (reader_tasks[i])
  1062. set_cpus_allowed_ptr(reader_tasks[i],
  1063. shuffle_tmp_mask);
  1064. }
  1065. if (fakewriter_tasks) {
  1066. for (i = 0; i < nfakewriters; i++)
  1067. if (fakewriter_tasks[i])
  1068. set_cpus_allowed_ptr(fakewriter_tasks[i],
  1069. shuffle_tmp_mask);
  1070. }
  1071. if (writer_task)
  1072. set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
  1073. if (stats_task)
  1074. set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
  1075. if (rcu_idle_cpu == -1)
  1076. rcu_idle_cpu = num_online_cpus() - 1;
  1077. else
  1078. rcu_idle_cpu--;
  1079. put_online_cpus();
  1080. }
  1081. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  1082. * system to become idle at a time and cut off its timer ticks. This is meant
  1083. * to test the support for such tickless idle CPU in RCU.
  1084. */
  1085. static int
  1086. rcu_torture_shuffle(void *arg)
  1087. {
  1088. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  1089. do {
  1090. schedule_timeout_interruptible(shuffle_interval * HZ);
  1091. rcu_torture_shuffle_tasks();
  1092. rcutorture_shutdown_absorb("rcu_torture_shuffle");
  1093. } while (!kthread_should_stop());
  1094. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  1095. return 0;
  1096. }
  1097. /* Cause the rcutorture test to "stutter", starting and stopping all
  1098. * threads periodically.
  1099. */
  1100. static int
  1101. rcu_torture_stutter(void *arg)
  1102. {
  1103. VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
  1104. do {
  1105. schedule_timeout_interruptible(stutter * HZ);
  1106. stutter_pause_test = 1;
  1107. if (!kthread_should_stop())
  1108. schedule_timeout_interruptible(stutter * HZ);
  1109. stutter_pause_test = 0;
  1110. rcutorture_shutdown_absorb("rcu_torture_stutter");
  1111. } while (!kthread_should_stop());
  1112. VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
  1113. return 0;
  1114. }
  1115. static inline void
  1116. rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, char *tag)
  1117. {
  1118. printk(KERN_ALERT "%s" TORTURE_FLAG
  1119. "--- %s: nreaders=%d nfakewriters=%d "
  1120. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  1121. "shuffle_interval=%d stutter=%d irqreader=%d "
  1122. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
  1123. "test_boost=%d/%d test_boost_interval=%d "
  1124. "test_boost_duration=%d\n",
  1125. torture_type, tag, nrealreaders, nfakewriters,
  1126. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  1127. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
  1128. test_boost, cur_ops->can_boost,
  1129. test_boost_interval, test_boost_duration);
  1130. }
  1131. static struct notifier_block rcutorture_shutdown_nb = {
  1132. .notifier_call = rcutorture_shutdown_notify,
  1133. };
  1134. static void rcutorture_booster_cleanup(int cpu)
  1135. {
  1136. struct task_struct *t;
  1137. if (boost_tasks[cpu] == NULL)
  1138. return;
  1139. mutex_lock(&boost_mutex);
  1140. VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
  1141. t = boost_tasks[cpu];
  1142. boost_tasks[cpu] = NULL;
  1143. mutex_unlock(&boost_mutex);
  1144. /* This must be outside of the mutex, otherwise deadlock! */
  1145. kthread_stop(t);
  1146. }
  1147. static int rcutorture_booster_init(int cpu)
  1148. {
  1149. int retval;
  1150. if (boost_tasks[cpu] != NULL)
  1151. return 0; /* Already created, nothing more to do. */
  1152. /* Don't allow time recalculation while creating a new task. */
  1153. mutex_lock(&boost_mutex);
  1154. VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
  1155. boost_tasks[cpu] = kthread_create(rcu_torture_boost, NULL,
  1156. "rcu_torture_boost");
  1157. if (IS_ERR(boost_tasks[cpu])) {
  1158. retval = PTR_ERR(boost_tasks[cpu]);
  1159. VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
  1160. n_rcu_torture_boost_ktrerror++;
  1161. boost_tasks[cpu] = NULL;
  1162. mutex_unlock(&boost_mutex);
  1163. return retval;
  1164. }
  1165. kthread_bind(boost_tasks[cpu], cpu);
  1166. wake_up_process(boost_tasks[cpu]);
  1167. mutex_unlock(&boost_mutex);
  1168. return 0;
  1169. }
  1170. static int rcutorture_cpu_notify(struct notifier_block *self,
  1171. unsigned long action, void *hcpu)
  1172. {
  1173. long cpu = (long)hcpu;
  1174. switch (action) {
  1175. case CPU_ONLINE:
  1176. case CPU_DOWN_FAILED:
  1177. (void)rcutorture_booster_init(cpu);
  1178. break;
  1179. case CPU_DOWN_PREPARE:
  1180. rcutorture_booster_cleanup(cpu);
  1181. break;
  1182. default:
  1183. break;
  1184. }
  1185. return NOTIFY_OK;
  1186. }
  1187. static struct notifier_block rcutorture_cpu_nb = {
  1188. .notifier_call = rcutorture_cpu_notify,
  1189. };
  1190. static void
  1191. rcu_torture_cleanup(void)
  1192. {
  1193. int i;
  1194. mutex_lock(&fullstop_mutex);
  1195. if (fullstop == FULLSTOP_SHUTDOWN) {
  1196. printk(KERN_WARNING /* but going down anyway, so... */
  1197. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  1198. mutex_unlock(&fullstop_mutex);
  1199. schedule_timeout_uninterruptible(10);
  1200. if (cur_ops->cb_barrier != NULL)
  1201. cur_ops->cb_barrier();
  1202. return;
  1203. }
  1204. fullstop = FULLSTOP_RMMOD;
  1205. mutex_unlock(&fullstop_mutex);
  1206. unregister_reboot_notifier(&rcutorture_shutdown_nb);
  1207. if (stutter_task) {
  1208. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
  1209. kthread_stop(stutter_task);
  1210. }
  1211. stutter_task = NULL;
  1212. if (shuffler_task) {
  1213. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  1214. kthread_stop(shuffler_task);
  1215. free_cpumask_var(shuffle_tmp_mask);
  1216. }
  1217. shuffler_task = NULL;
  1218. if (writer_task) {
  1219. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  1220. kthread_stop(writer_task);
  1221. }
  1222. writer_task = NULL;
  1223. if (reader_tasks) {
  1224. for (i = 0; i < nrealreaders; i++) {
  1225. if (reader_tasks[i]) {
  1226. VERBOSE_PRINTK_STRING(
  1227. "Stopping rcu_torture_reader task");
  1228. kthread_stop(reader_tasks[i]);
  1229. }
  1230. reader_tasks[i] = NULL;
  1231. }
  1232. kfree(reader_tasks);
  1233. reader_tasks = NULL;
  1234. }
  1235. rcu_torture_current = NULL;
  1236. if (fakewriter_tasks) {
  1237. for (i = 0; i < nfakewriters; i++) {
  1238. if (fakewriter_tasks[i]) {
  1239. VERBOSE_PRINTK_STRING(
  1240. "Stopping rcu_torture_fakewriter task");
  1241. kthread_stop(fakewriter_tasks[i]);
  1242. }
  1243. fakewriter_tasks[i] = NULL;
  1244. }
  1245. kfree(fakewriter_tasks);
  1246. fakewriter_tasks = NULL;
  1247. }
  1248. if (stats_task) {
  1249. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  1250. kthread_stop(stats_task);
  1251. }
  1252. stats_task = NULL;
  1253. if (fqs_task) {
  1254. VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
  1255. kthread_stop(fqs_task);
  1256. }
  1257. fqs_task = NULL;
  1258. if ((test_boost == 1 && cur_ops->can_boost) ||
  1259. test_boost == 2) {
  1260. unregister_cpu_notifier(&rcutorture_cpu_nb);
  1261. for_each_possible_cpu(i)
  1262. rcutorture_booster_cleanup(i);
  1263. }
  1264. /* Wait for all RCU callbacks to fire. */
  1265. if (cur_ops->cb_barrier != NULL)
  1266. cur_ops->cb_barrier();
  1267. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  1268. if (cur_ops->cleanup)
  1269. cur_ops->cleanup();
  1270. if (atomic_read(&n_rcu_torture_error))
  1271. rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
  1272. else
  1273. rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
  1274. }
  1275. static int __init
  1276. rcu_torture_init(void)
  1277. {
  1278. int i;
  1279. int cpu;
  1280. int firsterr = 0;
  1281. static struct rcu_torture_ops *torture_ops[] =
  1282. { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
  1283. &rcu_bh_ops, &rcu_bh_sync_ops,
  1284. &srcu_ops, &srcu_expedited_ops,
  1285. &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
  1286. mutex_lock(&fullstop_mutex);
  1287. /* Process args and tell the world that the torturer is on the job. */
  1288. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1289. cur_ops = torture_ops[i];
  1290. if (strcmp(torture_type, cur_ops->name) == 0)
  1291. break;
  1292. }
  1293. if (i == ARRAY_SIZE(torture_ops)) {
  1294. printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
  1295. torture_type);
  1296. printk(KERN_ALERT "rcu-torture types:");
  1297. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1298. printk(KERN_ALERT " %s", torture_ops[i]->name);
  1299. printk(KERN_ALERT "\n");
  1300. mutex_unlock(&fullstop_mutex);
  1301. return -EINVAL;
  1302. }
  1303. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  1304. printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
  1305. "fqs_duration, fqs disabled.\n");
  1306. fqs_duration = 0;
  1307. }
  1308. if (cur_ops->init)
  1309. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  1310. if (nreaders >= 0)
  1311. nrealreaders = nreaders;
  1312. else
  1313. nrealreaders = 2 * num_online_cpus();
  1314. rcu_torture_print_module_parms(cur_ops, "Start of test");
  1315. fullstop = FULLSTOP_DONTSTOP;
  1316. /* Set up the freelist. */
  1317. INIT_LIST_HEAD(&rcu_torture_freelist);
  1318. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  1319. rcu_tortures[i].rtort_mbtest = 0;
  1320. list_add_tail(&rcu_tortures[i].rtort_free,
  1321. &rcu_torture_freelist);
  1322. }
  1323. /* Initialize the statistics so that each run gets its own numbers. */
  1324. rcu_torture_current = NULL;
  1325. rcu_torture_current_version = 0;
  1326. atomic_set(&n_rcu_torture_alloc, 0);
  1327. atomic_set(&n_rcu_torture_alloc_fail, 0);
  1328. atomic_set(&n_rcu_torture_free, 0);
  1329. atomic_set(&n_rcu_torture_mberror, 0);
  1330. atomic_set(&n_rcu_torture_error, 0);
  1331. n_rcu_torture_boost_ktrerror = 0;
  1332. n_rcu_torture_boost_rterror = 0;
  1333. n_rcu_torture_boost_allocerror = 0;
  1334. n_rcu_torture_boost_afferror = 0;
  1335. n_rcu_torture_boost_failure = 0;
  1336. n_rcu_torture_boosts = 0;
  1337. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1338. atomic_set(&rcu_torture_wcount[i], 0);
  1339. for_each_possible_cpu(cpu) {
  1340. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1341. per_cpu(rcu_torture_count, cpu)[i] = 0;
  1342. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  1343. }
  1344. }
  1345. /* Start up the kthreads. */
  1346. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  1347. writer_task = kthread_run(rcu_torture_writer, NULL,
  1348. "rcu_torture_writer");
  1349. if (IS_ERR(writer_task)) {
  1350. firsterr = PTR_ERR(writer_task);
  1351. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  1352. writer_task = NULL;
  1353. goto unwind;
  1354. }
  1355. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  1356. GFP_KERNEL);
  1357. if (fakewriter_tasks == NULL) {
  1358. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1359. firsterr = -ENOMEM;
  1360. goto unwind;
  1361. }
  1362. for (i = 0; i < nfakewriters; i++) {
  1363. VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
  1364. fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
  1365. "rcu_torture_fakewriter");
  1366. if (IS_ERR(fakewriter_tasks[i])) {
  1367. firsterr = PTR_ERR(fakewriter_tasks[i]);
  1368. VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
  1369. fakewriter_tasks[i] = NULL;
  1370. goto unwind;
  1371. }
  1372. }
  1373. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  1374. GFP_KERNEL);
  1375. if (reader_tasks == NULL) {
  1376. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1377. firsterr = -ENOMEM;
  1378. goto unwind;
  1379. }
  1380. for (i = 0; i < nrealreaders; i++) {
  1381. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  1382. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  1383. "rcu_torture_reader");
  1384. if (IS_ERR(reader_tasks[i])) {
  1385. firsterr = PTR_ERR(reader_tasks[i]);
  1386. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  1387. reader_tasks[i] = NULL;
  1388. goto unwind;
  1389. }
  1390. }
  1391. if (stat_interval > 0) {
  1392. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  1393. stats_task = kthread_run(rcu_torture_stats, NULL,
  1394. "rcu_torture_stats");
  1395. if (IS_ERR(stats_task)) {
  1396. firsterr = PTR_ERR(stats_task);
  1397. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  1398. stats_task = NULL;
  1399. goto unwind;
  1400. }
  1401. }
  1402. if (test_no_idle_hz) {
  1403. rcu_idle_cpu = num_online_cpus() - 1;
  1404. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  1405. firsterr = -ENOMEM;
  1406. VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
  1407. goto unwind;
  1408. }
  1409. /* Create the shuffler thread */
  1410. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  1411. "rcu_torture_shuffle");
  1412. if (IS_ERR(shuffler_task)) {
  1413. free_cpumask_var(shuffle_tmp_mask);
  1414. firsterr = PTR_ERR(shuffler_task);
  1415. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  1416. shuffler_task = NULL;
  1417. goto unwind;
  1418. }
  1419. }
  1420. if (stutter < 0)
  1421. stutter = 0;
  1422. if (stutter) {
  1423. /* Create the stutter thread */
  1424. stutter_task = kthread_run(rcu_torture_stutter, NULL,
  1425. "rcu_torture_stutter");
  1426. if (IS_ERR(stutter_task)) {
  1427. firsterr = PTR_ERR(stutter_task);
  1428. VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
  1429. stutter_task = NULL;
  1430. goto unwind;
  1431. }
  1432. }
  1433. if (fqs_duration < 0)
  1434. fqs_duration = 0;
  1435. if (fqs_duration) {
  1436. /* Create the stutter thread */
  1437. fqs_task = kthread_run(rcu_torture_fqs, NULL,
  1438. "rcu_torture_fqs");
  1439. if (IS_ERR(fqs_task)) {
  1440. firsterr = PTR_ERR(fqs_task);
  1441. VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
  1442. fqs_task = NULL;
  1443. goto unwind;
  1444. }
  1445. }
  1446. if (test_boost_interval < 1)
  1447. test_boost_interval = 1;
  1448. if (test_boost_duration < 2)
  1449. test_boost_duration = 2;
  1450. if ((test_boost == 1 && cur_ops->can_boost) ||
  1451. test_boost == 2) {
  1452. int retval;
  1453. boost_starttime = jiffies + test_boost_interval * HZ;
  1454. register_cpu_notifier(&rcutorture_cpu_nb);
  1455. for_each_possible_cpu(i) {
  1456. if (cpu_is_offline(i))
  1457. continue; /* Heuristic: CPU can go offline. */
  1458. retval = rcutorture_booster_init(i);
  1459. if (retval < 0) {
  1460. firsterr = retval;
  1461. goto unwind;
  1462. }
  1463. }
  1464. }
  1465. register_reboot_notifier(&rcutorture_shutdown_nb);
  1466. mutex_unlock(&fullstop_mutex);
  1467. return 0;
  1468. unwind:
  1469. mutex_unlock(&fullstop_mutex);
  1470. rcu_torture_cleanup();
  1471. return firsterr;
  1472. }
  1473. module_init(rcu_torture_init);
  1474. module_exit(rcu_torture_cleanup);