rcutorture.c 54 KB

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