rcutorture.c 61 KB

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