rcutorture.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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], rcu_torture_count);
  126. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
  127. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  128. static atomic_t n_rcu_torture_alloc;
  129. static atomic_t n_rcu_torture_alloc_fail;
  130. static atomic_t n_rcu_torture_free;
  131. static atomic_t n_rcu_torture_mberror;
  132. static atomic_t n_rcu_torture_error;
  133. static long n_rcu_torture_barrier_error;
  134. static long n_rcu_torture_boost_ktrerror;
  135. static long n_rcu_torture_boost_rterror;
  136. static long n_rcu_torture_boost_failure;
  137. static long n_rcu_torture_boosts;
  138. static long n_rcu_torture_timers;
  139. static long n_barrier_attempts;
  140. static long n_barrier_successes;
  141. static atomic_long_t n_cbfloods;
  142. static struct list_head rcu_torture_removed;
  143. static int rcu_torture_writer_state;
  144. #define RTWS_FIXED_DELAY 0
  145. #define RTWS_DELAY 1
  146. #define RTWS_REPLACE 2
  147. #define RTWS_DEF_FREE 3
  148. #define RTWS_EXP_SYNC 4
  149. #define RTWS_COND_GET 5
  150. #define RTWS_COND_SYNC 6
  151. #define RTWS_SYNC 7
  152. #define RTWS_STUTTER 8
  153. #define RTWS_STOPPING 9
  154. static const char * const rcu_torture_writer_state_names[] = {
  155. "RTWS_FIXED_DELAY",
  156. "RTWS_DELAY",
  157. "RTWS_REPLACE",
  158. "RTWS_DEF_FREE",
  159. "RTWS_EXP_SYNC",
  160. "RTWS_COND_GET",
  161. "RTWS_COND_SYNC",
  162. "RTWS_SYNC",
  163. "RTWS_STUTTER",
  164. "RTWS_STOPPING",
  165. };
  166. static const char *rcu_torture_writer_state_getname(void)
  167. {
  168. unsigned int i = READ_ONCE(rcu_torture_writer_state);
  169. if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
  170. return "???";
  171. return rcu_torture_writer_state_names[i];
  172. }
  173. static int torture_runnable = IS_ENABLED(MODULE);
  174. module_param(torture_runnable, int, 0444);
  175. MODULE_PARM_DESC(torture_runnable, "Start rcutorture at boot");
  176. #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
  177. #define rcu_can_boost() 1
  178. #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
  179. #define rcu_can_boost() 0
  180. #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
  181. #ifdef CONFIG_RCU_TRACE
  182. static u64 notrace rcu_trace_clock_local(void)
  183. {
  184. u64 ts = trace_clock_local();
  185. unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
  186. return ts;
  187. }
  188. #else /* #ifdef CONFIG_RCU_TRACE */
  189. static u64 notrace rcu_trace_clock_local(void)
  190. {
  191. return 0ULL;
  192. }
  193. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  194. static unsigned long boost_starttime; /* jiffies of next boost test start. */
  195. static DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
  196. /* and boost task create/destroy. */
  197. static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
  198. static bool barrier_phase; /* Test phase. */
  199. static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
  200. static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
  201. static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
  202. /*
  203. * Allocate an element from the rcu_tortures pool.
  204. */
  205. static struct rcu_torture *
  206. rcu_torture_alloc(void)
  207. {
  208. struct list_head *p;
  209. spin_lock_bh(&rcu_torture_lock);
  210. if (list_empty(&rcu_torture_freelist)) {
  211. atomic_inc(&n_rcu_torture_alloc_fail);
  212. spin_unlock_bh(&rcu_torture_lock);
  213. return NULL;
  214. }
  215. atomic_inc(&n_rcu_torture_alloc);
  216. p = rcu_torture_freelist.next;
  217. list_del_init(p);
  218. spin_unlock_bh(&rcu_torture_lock);
  219. return container_of(p, struct rcu_torture, rtort_free);
  220. }
  221. /*
  222. * Free an element to the rcu_tortures pool.
  223. */
  224. static void
  225. rcu_torture_free(struct rcu_torture *p)
  226. {
  227. atomic_inc(&n_rcu_torture_free);
  228. spin_lock_bh(&rcu_torture_lock);
  229. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  230. spin_unlock_bh(&rcu_torture_lock);
  231. }
  232. /*
  233. * Operations vector for selecting different types of tests.
  234. */
  235. struct rcu_torture_ops {
  236. int ttype;
  237. void (*init)(void);
  238. void (*cleanup)(void);
  239. int (*readlock)(void);
  240. void (*read_delay)(struct torture_random_state *rrsp);
  241. void (*readunlock)(int idx);
  242. unsigned long (*started)(void);
  243. unsigned long (*completed)(void);
  244. void (*deferred_free)(struct rcu_torture *p);
  245. void (*sync)(void);
  246. void (*exp_sync)(void);
  247. unsigned long (*get_state)(void);
  248. void (*cond_sync)(unsigned long oldstate);
  249. call_rcu_func_t call;
  250. void (*cb_barrier)(void);
  251. void (*fqs)(void);
  252. void (*stats)(void);
  253. int irq_capable;
  254. int can_boost;
  255. const char *name;
  256. };
  257. static struct rcu_torture_ops *cur_ops;
  258. /*
  259. * Definitions for rcu torture testing.
  260. */
  261. static int rcu_torture_read_lock(void) __acquires(RCU)
  262. {
  263. rcu_read_lock();
  264. return 0;
  265. }
  266. static void rcu_read_delay(struct torture_random_state *rrsp)
  267. {
  268. unsigned long started;
  269. unsigned long completed;
  270. const unsigned long shortdelay_us = 200;
  271. const unsigned long longdelay_ms = 50;
  272. unsigned long long ts;
  273. /* We want a short delay sometimes to make a reader delay the grace
  274. * period, and we want a long delay occasionally to trigger
  275. * force_quiescent_state. */
  276. if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
  277. started = cur_ops->completed();
  278. ts = rcu_trace_clock_local();
  279. mdelay(longdelay_ms);
  280. completed = cur_ops->completed();
  281. do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
  282. started, completed);
  283. }
  284. if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
  285. udelay(shortdelay_us);
  286. #ifdef CONFIG_PREEMPT
  287. if (!preempt_count() &&
  288. !(torture_random(rrsp) % (nrealreaders * 20000)))
  289. preempt_schedule(); /* No QS if preempt_disable() in effect */
  290. #endif
  291. }
  292. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  293. {
  294. rcu_read_unlock();
  295. }
  296. /*
  297. * Update callback in the pipe. This should be invoked after a grace period.
  298. */
  299. static bool
  300. rcu_torture_pipe_update_one(struct rcu_torture *rp)
  301. {
  302. int i;
  303. i = rp->rtort_pipe_count;
  304. if (i > RCU_TORTURE_PIPE_LEN)
  305. i = RCU_TORTURE_PIPE_LEN;
  306. atomic_inc(&rcu_torture_wcount[i]);
  307. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  308. rp->rtort_mbtest = 0;
  309. return true;
  310. }
  311. return false;
  312. }
  313. /*
  314. * Update all callbacks in the pipe. Suitable for synchronous grace-period
  315. * primitives.
  316. */
  317. static void
  318. rcu_torture_pipe_update(struct rcu_torture *old_rp)
  319. {
  320. struct rcu_torture *rp;
  321. struct rcu_torture *rp1;
  322. if (old_rp)
  323. list_add(&old_rp->rtort_free, &rcu_torture_removed);
  324. list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
  325. if (rcu_torture_pipe_update_one(rp)) {
  326. list_del(&rp->rtort_free);
  327. rcu_torture_free(rp);
  328. }
  329. }
  330. }
  331. static void
  332. rcu_torture_cb(struct rcu_head *p)
  333. {
  334. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  335. if (torture_must_stop_irq()) {
  336. /* Test is ending, just drop callbacks on the floor. */
  337. /* The next initialization will pick up the pieces. */
  338. return;
  339. }
  340. if (rcu_torture_pipe_update_one(rp))
  341. rcu_torture_free(rp);
  342. else
  343. cur_ops->deferred_free(rp);
  344. }
  345. static unsigned long rcu_no_completed(void)
  346. {
  347. return 0;
  348. }
  349. static void rcu_torture_deferred_free(struct rcu_torture *p)
  350. {
  351. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  352. }
  353. static void rcu_sync_torture_init(void)
  354. {
  355. INIT_LIST_HEAD(&rcu_torture_removed);
  356. }
  357. static struct rcu_torture_ops rcu_ops = {
  358. .ttype = RCU_FLAVOR,
  359. .init = rcu_sync_torture_init,
  360. .readlock = rcu_torture_read_lock,
  361. .read_delay = rcu_read_delay,
  362. .readunlock = rcu_torture_read_unlock,
  363. .started = rcu_batches_started,
  364. .completed = rcu_batches_completed,
  365. .deferred_free = rcu_torture_deferred_free,
  366. .sync = synchronize_rcu,
  367. .exp_sync = synchronize_rcu_expedited,
  368. .get_state = get_state_synchronize_rcu,
  369. .cond_sync = cond_synchronize_rcu,
  370. .call = call_rcu,
  371. .cb_barrier = rcu_barrier,
  372. .fqs = rcu_force_quiescent_state,
  373. .stats = NULL,
  374. .irq_capable = 1,
  375. .can_boost = rcu_can_boost(),
  376. .name = "rcu"
  377. };
  378. /*
  379. * Definitions for rcu_bh torture testing.
  380. */
  381. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  382. {
  383. rcu_read_lock_bh();
  384. return 0;
  385. }
  386. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  387. {
  388. rcu_read_unlock_bh();
  389. }
  390. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  391. {
  392. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  393. }
  394. static struct rcu_torture_ops rcu_bh_ops = {
  395. .ttype = RCU_BH_FLAVOR,
  396. .init = rcu_sync_torture_init,
  397. .readlock = rcu_bh_torture_read_lock,
  398. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  399. .readunlock = rcu_bh_torture_read_unlock,
  400. .started = rcu_batches_started_bh,
  401. .completed = rcu_batches_completed_bh,
  402. .deferred_free = rcu_bh_torture_deferred_free,
  403. .sync = synchronize_rcu_bh,
  404. .exp_sync = synchronize_rcu_bh_expedited,
  405. .call = call_rcu_bh,
  406. .cb_barrier = rcu_barrier_bh,
  407. .fqs = rcu_bh_force_quiescent_state,
  408. .stats = NULL,
  409. .irq_capable = 1,
  410. .name = "rcu_bh"
  411. };
  412. /*
  413. * Don't even think about trying any of these in real life!!!
  414. * The names includes "busted", and they really means it!
  415. * The only purpose of these functions is to provide a buggy RCU
  416. * implementation to make sure that rcutorture correctly emits
  417. * buggy-RCU error messages.
  418. */
  419. static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
  420. {
  421. /* This is a deliberate bug for testing purposes only! */
  422. rcu_torture_cb(&p->rtort_rcu);
  423. }
  424. static void synchronize_rcu_busted(void)
  425. {
  426. /* This is a deliberate bug for testing purposes only! */
  427. }
  428. static void
  429. call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
  430. {
  431. /* This is a deliberate bug for testing purposes only! */
  432. func(head);
  433. }
  434. static struct rcu_torture_ops rcu_busted_ops = {
  435. .ttype = INVALID_RCU_FLAVOR,
  436. .init = rcu_sync_torture_init,
  437. .readlock = rcu_torture_read_lock,
  438. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  439. .readunlock = rcu_torture_read_unlock,
  440. .started = rcu_no_completed,
  441. .completed = rcu_no_completed,
  442. .deferred_free = rcu_busted_torture_deferred_free,
  443. .sync = synchronize_rcu_busted,
  444. .exp_sync = synchronize_rcu_busted,
  445. .call = call_rcu_busted,
  446. .cb_barrier = NULL,
  447. .fqs = NULL,
  448. .stats = NULL,
  449. .irq_capable = 1,
  450. .name = "rcu_busted"
  451. };
  452. /*
  453. * Definitions for srcu torture testing.
  454. */
  455. DEFINE_STATIC_SRCU(srcu_ctl);
  456. static struct srcu_struct srcu_ctld;
  457. static struct srcu_struct *srcu_ctlp = &srcu_ctl;
  458. static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
  459. {
  460. return srcu_read_lock(srcu_ctlp);
  461. }
  462. static void srcu_read_delay(struct torture_random_state *rrsp)
  463. {
  464. long delay;
  465. const long uspertick = 1000000 / HZ;
  466. const long longdelay = 10;
  467. /* We want there to be long-running readers, but not all the time. */
  468. delay = torture_random(rrsp) %
  469. (nrealreaders * 2 * longdelay * uspertick);
  470. if (!delay)
  471. schedule_timeout_interruptible(longdelay);
  472. else
  473. rcu_read_delay(rrsp);
  474. }
  475. static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
  476. {
  477. srcu_read_unlock(srcu_ctlp, idx);
  478. }
  479. static unsigned long srcu_torture_completed(void)
  480. {
  481. return srcu_batches_completed(srcu_ctlp);
  482. }
  483. static void srcu_torture_deferred_free(struct rcu_torture *rp)
  484. {
  485. call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
  486. }
  487. static void srcu_torture_synchronize(void)
  488. {
  489. synchronize_srcu(srcu_ctlp);
  490. }
  491. static void srcu_torture_call(struct rcu_head *head,
  492. rcu_callback_t func)
  493. {
  494. call_srcu(srcu_ctlp, head, func);
  495. }
  496. static void srcu_torture_barrier(void)
  497. {
  498. srcu_barrier(srcu_ctlp);
  499. }
  500. static void srcu_torture_stats(void)
  501. {
  502. int cpu;
  503. int idx = srcu_ctlp->completed & 0x1;
  504. pr_alert("%s%s per-CPU(idx=%d):",
  505. torture_type, TORTURE_FLAG, idx);
  506. for_each_possible_cpu(cpu) {
  507. long c0, c1;
  508. c0 = (long)per_cpu_ptr(srcu_ctlp->per_cpu_ref, cpu)->c[!idx];
  509. c1 = (long)per_cpu_ptr(srcu_ctlp->per_cpu_ref, cpu)->c[idx];
  510. pr_cont(" %d(%ld,%ld)", cpu, c0, c1);
  511. }
  512. pr_cont("\n");
  513. }
  514. static void srcu_torture_synchronize_expedited(void)
  515. {
  516. synchronize_srcu_expedited(srcu_ctlp);
  517. }
  518. static struct rcu_torture_ops srcu_ops = {
  519. .ttype = SRCU_FLAVOR,
  520. .init = rcu_sync_torture_init,
  521. .readlock = srcu_torture_read_lock,
  522. .read_delay = srcu_read_delay,
  523. .readunlock = srcu_torture_read_unlock,
  524. .started = NULL,
  525. .completed = srcu_torture_completed,
  526. .deferred_free = srcu_torture_deferred_free,
  527. .sync = srcu_torture_synchronize,
  528. .exp_sync = srcu_torture_synchronize_expedited,
  529. .call = srcu_torture_call,
  530. .cb_barrier = srcu_torture_barrier,
  531. .stats = srcu_torture_stats,
  532. .name = "srcu"
  533. };
  534. static void srcu_torture_init(void)
  535. {
  536. rcu_sync_torture_init();
  537. WARN_ON(init_srcu_struct(&srcu_ctld));
  538. srcu_ctlp = &srcu_ctld;
  539. }
  540. static void srcu_torture_cleanup(void)
  541. {
  542. cleanup_srcu_struct(&srcu_ctld);
  543. srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
  544. }
  545. /* As above, but dynamically allocated. */
  546. static struct rcu_torture_ops srcud_ops = {
  547. .ttype = SRCU_FLAVOR,
  548. .init = srcu_torture_init,
  549. .cleanup = srcu_torture_cleanup,
  550. .readlock = srcu_torture_read_lock,
  551. .read_delay = srcu_read_delay,
  552. .readunlock = srcu_torture_read_unlock,
  553. .started = NULL,
  554. .completed = srcu_torture_completed,
  555. .deferred_free = srcu_torture_deferred_free,
  556. .sync = srcu_torture_synchronize,
  557. .exp_sync = srcu_torture_synchronize_expedited,
  558. .call = srcu_torture_call,
  559. .cb_barrier = srcu_torture_barrier,
  560. .stats = srcu_torture_stats,
  561. .name = "srcud"
  562. };
  563. /*
  564. * Definitions for sched torture testing.
  565. */
  566. static int sched_torture_read_lock(void)
  567. {
  568. preempt_disable();
  569. return 0;
  570. }
  571. static void sched_torture_read_unlock(int idx)
  572. {
  573. preempt_enable();
  574. }
  575. static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
  576. {
  577. call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
  578. }
  579. static struct rcu_torture_ops sched_ops = {
  580. .ttype = RCU_SCHED_FLAVOR,
  581. .init = rcu_sync_torture_init,
  582. .readlock = sched_torture_read_lock,
  583. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  584. .readunlock = sched_torture_read_unlock,
  585. .started = rcu_batches_started_sched,
  586. .completed = rcu_batches_completed_sched,
  587. .deferred_free = rcu_sched_torture_deferred_free,
  588. .sync = synchronize_sched,
  589. .exp_sync = synchronize_sched_expedited,
  590. .get_state = get_state_synchronize_sched,
  591. .cond_sync = cond_synchronize_sched,
  592. .call = call_rcu_sched,
  593. .cb_barrier = rcu_barrier_sched,
  594. .fqs = rcu_sched_force_quiescent_state,
  595. .stats = NULL,
  596. .irq_capable = 1,
  597. .name = "sched"
  598. };
  599. #ifdef CONFIG_TASKS_RCU
  600. /*
  601. * Definitions for RCU-tasks torture testing.
  602. */
  603. static int tasks_torture_read_lock(void)
  604. {
  605. return 0;
  606. }
  607. static void tasks_torture_read_unlock(int idx)
  608. {
  609. }
  610. static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
  611. {
  612. call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
  613. }
  614. static struct rcu_torture_ops tasks_ops = {
  615. .ttype = RCU_TASKS_FLAVOR,
  616. .init = rcu_sync_torture_init,
  617. .readlock = tasks_torture_read_lock,
  618. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  619. .readunlock = tasks_torture_read_unlock,
  620. .started = rcu_no_completed,
  621. .completed = rcu_no_completed,
  622. .deferred_free = rcu_tasks_torture_deferred_free,
  623. .sync = synchronize_rcu_tasks,
  624. .exp_sync = synchronize_rcu_tasks,
  625. .call = call_rcu_tasks,
  626. .cb_barrier = rcu_barrier_tasks,
  627. .fqs = NULL,
  628. .stats = NULL,
  629. .irq_capable = 1,
  630. .name = "tasks"
  631. };
  632. #define RCUTORTURE_TASKS_OPS &tasks_ops,
  633. static bool __maybe_unused torturing_tasks(void)
  634. {
  635. return cur_ops == &tasks_ops;
  636. }
  637. #else /* #ifdef CONFIG_TASKS_RCU */
  638. #define RCUTORTURE_TASKS_OPS
  639. static bool __maybe_unused torturing_tasks(void)
  640. {
  641. return false;
  642. }
  643. #endif /* #else #ifdef CONFIG_TASKS_RCU */
  644. /*
  645. * RCU torture priority-boost testing. Runs one real-time thread per
  646. * CPU for moderate bursts, repeatedly registering RCU callbacks and
  647. * spinning waiting for them to be invoked. If a given callback takes
  648. * too long to be invoked, we assume that priority inversion has occurred.
  649. */
  650. struct rcu_boost_inflight {
  651. struct rcu_head rcu;
  652. int inflight;
  653. };
  654. static void rcu_torture_boost_cb(struct rcu_head *head)
  655. {
  656. struct rcu_boost_inflight *rbip =
  657. container_of(head, struct rcu_boost_inflight, rcu);
  658. /* Ensure RCU-core accesses precede clearing ->inflight */
  659. smp_store_release(&rbip->inflight, 0);
  660. }
  661. static int rcu_torture_boost(void *arg)
  662. {
  663. unsigned long call_rcu_time;
  664. unsigned long endtime;
  665. unsigned long oldstarttime;
  666. struct rcu_boost_inflight rbi = { .inflight = 0 };
  667. struct sched_param sp;
  668. VERBOSE_TOROUT_STRING("rcu_torture_boost started");
  669. /* Set real-time priority. */
  670. sp.sched_priority = 1;
  671. if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
  672. VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
  673. n_rcu_torture_boost_rterror++;
  674. }
  675. init_rcu_head_on_stack(&rbi.rcu);
  676. /* Each pass through the following loop does one boost-test cycle. */
  677. do {
  678. /* Wait for the next test interval. */
  679. oldstarttime = boost_starttime;
  680. while (ULONG_CMP_LT(jiffies, oldstarttime)) {
  681. schedule_timeout_interruptible(oldstarttime - jiffies);
  682. stutter_wait("rcu_torture_boost");
  683. if (torture_must_stop())
  684. goto checkwait;
  685. }
  686. /* Do one boost-test interval. */
  687. endtime = oldstarttime + test_boost_duration * HZ;
  688. call_rcu_time = jiffies;
  689. while (ULONG_CMP_LT(jiffies, endtime)) {
  690. /* If we don't have a callback in flight, post one. */
  691. if (!smp_load_acquire(&rbi.inflight)) {
  692. /* RCU core before ->inflight = 1. */
  693. smp_store_release(&rbi.inflight, 1);
  694. call_rcu(&rbi.rcu, rcu_torture_boost_cb);
  695. if (jiffies - call_rcu_time >
  696. test_boost_duration * HZ - HZ / 2) {
  697. VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
  698. n_rcu_torture_boost_failure++;
  699. }
  700. call_rcu_time = jiffies;
  701. }
  702. stutter_wait("rcu_torture_boost");
  703. if (torture_must_stop())
  704. goto checkwait;
  705. }
  706. /*
  707. * Set the start time of the next test interval.
  708. * Yes, this is vulnerable to long delays, but such
  709. * delays simply cause a false negative for the next
  710. * interval. Besides, we are running at RT priority,
  711. * so delays should be relatively rare.
  712. */
  713. while (oldstarttime == boost_starttime &&
  714. !kthread_should_stop()) {
  715. if (mutex_trylock(&boost_mutex)) {
  716. boost_starttime = jiffies +
  717. test_boost_interval * HZ;
  718. n_rcu_torture_boosts++;
  719. mutex_unlock(&boost_mutex);
  720. break;
  721. }
  722. schedule_timeout_uninterruptible(1);
  723. }
  724. /* Go do the stutter. */
  725. checkwait: stutter_wait("rcu_torture_boost");
  726. } while (!torture_must_stop());
  727. /* Clean up and exit. */
  728. while (!kthread_should_stop() || smp_load_acquire(&rbi.inflight)) {
  729. torture_shutdown_absorb("rcu_torture_boost");
  730. schedule_timeout_uninterruptible(1);
  731. }
  732. destroy_rcu_head_on_stack(&rbi.rcu);
  733. torture_kthread_stopping("rcu_torture_boost");
  734. return 0;
  735. }
  736. static void rcu_torture_cbflood_cb(struct rcu_head *rhp)
  737. {
  738. }
  739. /*
  740. * RCU torture callback-flood kthread. Repeatedly induces bursts of calls
  741. * to call_rcu() or analogous, increasing the probability of occurrence
  742. * of callback-overflow corner cases.
  743. */
  744. static int
  745. rcu_torture_cbflood(void *arg)
  746. {
  747. int err = 1;
  748. int i;
  749. int j;
  750. struct rcu_head *rhp;
  751. if (cbflood_n_per_burst > 0 &&
  752. cbflood_inter_holdoff > 0 &&
  753. cbflood_intra_holdoff > 0 &&
  754. cur_ops->call &&
  755. cur_ops->cb_barrier) {
  756. rhp = vmalloc(sizeof(*rhp) *
  757. cbflood_n_burst * cbflood_n_per_burst);
  758. err = !rhp;
  759. }
  760. if (err) {
  761. VERBOSE_TOROUT_STRING("rcu_torture_cbflood disabled: Bad args or OOM");
  762. goto wait_for_stop;
  763. }
  764. VERBOSE_TOROUT_STRING("rcu_torture_cbflood task started");
  765. do {
  766. schedule_timeout_interruptible(cbflood_inter_holdoff);
  767. atomic_long_inc(&n_cbfloods);
  768. WARN_ON(signal_pending(current));
  769. for (i = 0; i < cbflood_n_burst; i++) {
  770. for (j = 0; j < cbflood_n_per_burst; j++) {
  771. cur_ops->call(&rhp[i * cbflood_n_per_burst + j],
  772. rcu_torture_cbflood_cb);
  773. }
  774. schedule_timeout_interruptible(cbflood_intra_holdoff);
  775. WARN_ON(signal_pending(current));
  776. }
  777. cur_ops->cb_barrier();
  778. stutter_wait("rcu_torture_cbflood");
  779. } while (!torture_must_stop());
  780. vfree(rhp);
  781. wait_for_stop:
  782. torture_kthread_stopping("rcu_torture_cbflood");
  783. return 0;
  784. }
  785. /*
  786. * RCU torture force-quiescent-state kthread. Repeatedly induces
  787. * bursts of calls to force_quiescent_state(), increasing the probability
  788. * of occurrence of some important types of race conditions.
  789. */
  790. static int
  791. rcu_torture_fqs(void *arg)
  792. {
  793. unsigned long fqs_resume_time;
  794. int fqs_burst_remaining;
  795. VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
  796. do {
  797. fqs_resume_time = jiffies + fqs_stutter * HZ;
  798. while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
  799. !kthread_should_stop()) {
  800. schedule_timeout_interruptible(1);
  801. }
  802. fqs_burst_remaining = fqs_duration;
  803. while (fqs_burst_remaining > 0 &&
  804. !kthread_should_stop()) {
  805. cur_ops->fqs();
  806. udelay(fqs_holdoff);
  807. fqs_burst_remaining -= fqs_holdoff;
  808. }
  809. stutter_wait("rcu_torture_fqs");
  810. } while (!torture_must_stop());
  811. torture_kthread_stopping("rcu_torture_fqs");
  812. return 0;
  813. }
  814. /*
  815. * RCU torture writer kthread. Repeatedly substitutes a new structure
  816. * for that pointed to by rcu_torture_current, freeing the old structure
  817. * after a series of grace periods (the "pipeline").
  818. */
  819. static int
  820. rcu_torture_writer(void *arg)
  821. {
  822. bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
  823. int expediting = 0;
  824. unsigned long gp_snap;
  825. bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
  826. bool gp_sync1 = gp_sync;
  827. int i;
  828. struct rcu_torture *rp;
  829. struct rcu_torture *old_rp;
  830. static DEFINE_TORTURE_RANDOM(rand);
  831. int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
  832. RTWS_COND_GET, RTWS_SYNC };
  833. int nsynctypes = 0;
  834. VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
  835. if (!can_expedite) {
  836. pr_alert("%s" TORTURE_FLAG
  837. " GP expediting controlled from boot/sysfs for %s,\n",
  838. torture_type, cur_ops->name);
  839. pr_alert("%s" TORTURE_FLAG
  840. " Disabled dynamic grace-period expediting.\n",
  841. torture_type);
  842. }
  843. /* Initialize synctype[] array. If none set, take default. */
  844. if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync1)
  845. gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
  846. if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync)
  847. synctype[nsynctypes++] = RTWS_COND_GET;
  848. else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync))
  849. pr_alert("rcu_torture_writer: gp_cond without primitives.\n");
  850. if (gp_exp1 && cur_ops->exp_sync)
  851. synctype[nsynctypes++] = RTWS_EXP_SYNC;
  852. else if (gp_exp && !cur_ops->exp_sync)
  853. pr_alert("rcu_torture_writer: gp_exp without primitives.\n");
  854. if (gp_normal1 && cur_ops->deferred_free)
  855. synctype[nsynctypes++] = RTWS_DEF_FREE;
  856. else if (gp_normal && !cur_ops->deferred_free)
  857. pr_alert("rcu_torture_writer: gp_normal without primitives.\n");
  858. if (gp_sync1 && cur_ops->sync)
  859. synctype[nsynctypes++] = RTWS_SYNC;
  860. else if (gp_sync && !cur_ops->sync)
  861. pr_alert("rcu_torture_writer: gp_sync without primitives.\n");
  862. if (WARN_ONCE(nsynctypes == 0,
  863. "rcu_torture_writer: No update-side primitives.\n")) {
  864. /*
  865. * No updates primitives, so don't try updating.
  866. * The resulting test won't be testing much, hence the
  867. * above WARN_ONCE().
  868. */
  869. rcu_torture_writer_state = RTWS_STOPPING;
  870. torture_kthread_stopping("rcu_torture_writer");
  871. }
  872. do {
  873. rcu_torture_writer_state = RTWS_FIXED_DELAY;
  874. schedule_timeout_uninterruptible(1);
  875. rp = rcu_torture_alloc();
  876. if (rp == NULL)
  877. continue;
  878. rp->rtort_pipe_count = 0;
  879. rcu_torture_writer_state = RTWS_DELAY;
  880. udelay(torture_random(&rand) & 0x3ff);
  881. rcu_torture_writer_state = RTWS_REPLACE;
  882. old_rp = rcu_dereference_check(rcu_torture_current,
  883. current == writer_task);
  884. rp->rtort_mbtest = 1;
  885. rcu_assign_pointer(rcu_torture_current, rp);
  886. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  887. if (old_rp) {
  888. i = old_rp->rtort_pipe_count;
  889. if (i > RCU_TORTURE_PIPE_LEN)
  890. i = RCU_TORTURE_PIPE_LEN;
  891. atomic_inc(&rcu_torture_wcount[i]);
  892. old_rp->rtort_pipe_count++;
  893. switch (synctype[torture_random(&rand) % nsynctypes]) {
  894. case RTWS_DEF_FREE:
  895. rcu_torture_writer_state = RTWS_DEF_FREE;
  896. cur_ops->deferred_free(old_rp);
  897. break;
  898. case RTWS_EXP_SYNC:
  899. rcu_torture_writer_state = RTWS_EXP_SYNC;
  900. cur_ops->exp_sync();
  901. rcu_torture_pipe_update(old_rp);
  902. break;
  903. case RTWS_COND_GET:
  904. rcu_torture_writer_state = RTWS_COND_GET;
  905. gp_snap = cur_ops->get_state();
  906. i = torture_random(&rand) % 16;
  907. if (i != 0)
  908. schedule_timeout_interruptible(i);
  909. udelay(torture_random(&rand) % 1000);
  910. rcu_torture_writer_state = RTWS_COND_SYNC;
  911. cur_ops->cond_sync(gp_snap);
  912. rcu_torture_pipe_update(old_rp);
  913. break;
  914. case RTWS_SYNC:
  915. rcu_torture_writer_state = RTWS_SYNC;
  916. cur_ops->sync();
  917. rcu_torture_pipe_update(old_rp);
  918. break;
  919. default:
  920. WARN_ON_ONCE(1);
  921. break;
  922. }
  923. }
  924. rcutorture_record_progress(++rcu_torture_current_version);
  925. /* Cycle through nesting levels of rcu_expedite_gp() calls. */
  926. if (can_expedite &&
  927. !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
  928. WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
  929. if (expediting >= 0)
  930. rcu_expedite_gp();
  931. else
  932. rcu_unexpedite_gp();
  933. if (++expediting > 3)
  934. expediting = -expediting;
  935. }
  936. rcu_torture_writer_state = RTWS_STUTTER;
  937. stutter_wait("rcu_torture_writer");
  938. } while (!torture_must_stop());
  939. /* Reset expediting back to unexpedited. */
  940. if (expediting > 0)
  941. expediting = -expediting;
  942. while (can_expedite && expediting++ < 0)
  943. rcu_unexpedite_gp();
  944. WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
  945. rcu_torture_writer_state = RTWS_STOPPING;
  946. torture_kthread_stopping("rcu_torture_writer");
  947. return 0;
  948. }
  949. /*
  950. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  951. * delay between calls.
  952. */
  953. static int
  954. rcu_torture_fakewriter(void *arg)
  955. {
  956. DEFINE_TORTURE_RANDOM(rand);
  957. VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
  958. set_user_nice(current, MAX_NICE);
  959. do {
  960. schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
  961. udelay(torture_random(&rand) & 0x3ff);
  962. if (cur_ops->cb_barrier != NULL &&
  963. torture_random(&rand) % (nfakewriters * 8) == 0) {
  964. cur_ops->cb_barrier();
  965. } else if (gp_normal == gp_exp) {
  966. if (torture_random(&rand) & 0x80)
  967. cur_ops->sync();
  968. else
  969. cur_ops->exp_sync();
  970. } else if (gp_normal) {
  971. cur_ops->sync();
  972. } else {
  973. cur_ops->exp_sync();
  974. }
  975. stutter_wait("rcu_torture_fakewriter");
  976. } while (!torture_must_stop());
  977. torture_kthread_stopping("rcu_torture_fakewriter");
  978. return 0;
  979. }
  980. /*
  981. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  982. * incrementing the corresponding element of the pipeline array. The
  983. * counter in the element should never be greater than 1, otherwise, the
  984. * RCU implementation is broken.
  985. */
  986. static void rcu_torture_timer(unsigned long unused)
  987. {
  988. int idx;
  989. unsigned long started;
  990. unsigned long completed;
  991. static DEFINE_TORTURE_RANDOM(rand);
  992. static DEFINE_SPINLOCK(rand_lock);
  993. struct rcu_torture *p;
  994. int pipe_count;
  995. unsigned long long ts;
  996. idx = cur_ops->readlock();
  997. if (cur_ops->started)
  998. started = cur_ops->started();
  999. else
  1000. started = cur_ops->completed();
  1001. ts = rcu_trace_clock_local();
  1002. p = rcu_dereference_check(rcu_torture_current,
  1003. rcu_read_lock_bh_held() ||
  1004. rcu_read_lock_sched_held() ||
  1005. srcu_read_lock_held(srcu_ctlp) ||
  1006. torturing_tasks());
  1007. if (p == NULL) {
  1008. /* Leave because rcu_torture_writer is not yet underway */
  1009. cur_ops->readunlock(idx);
  1010. return;
  1011. }
  1012. if (p->rtort_mbtest == 0)
  1013. atomic_inc(&n_rcu_torture_mberror);
  1014. spin_lock(&rand_lock);
  1015. cur_ops->read_delay(&rand);
  1016. n_rcu_torture_timers++;
  1017. spin_unlock(&rand_lock);
  1018. preempt_disable();
  1019. pipe_count = p->rtort_pipe_count;
  1020. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  1021. /* Should not happen, but... */
  1022. pipe_count = RCU_TORTURE_PIPE_LEN;
  1023. }
  1024. completed = cur_ops->completed();
  1025. if (pipe_count > 1) {
  1026. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
  1027. started, completed);
  1028. rcu_ftrace_dump(DUMP_ALL);
  1029. }
  1030. __this_cpu_inc(rcu_torture_count[pipe_count]);
  1031. completed = completed - started;
  1032. if (cur_ops->started)
  1033. completed++;
  1034. if (completed > RCU_TORTURE_PIPE_LEN) {
  1035. /* Should not happen, but... */
  1036. completed = RCU_TORTURE_PIPE_LEN;
  1037. }
  1038. __this_cpu_inc(rcu_torture_batch[completed]);
  1039. preempt_enable();
  1040. cur_ops->readunlock(idx);
  1041. }
  1042. /*
  1043. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  1044. * incrementing the corresponding element of the pipeline array. The
  1045. * counter in the element should never be greater than 1, otherwise, the
  1046. * RCU implementation is broken.
  1047. */
  1048. static int
  1049. rcu_torture_reader(void *arg)
  1050. {
  1051. unsigned long started;
  1052. unsigned long completed;
  1053. int idx;
  1054. DEFINE_TORTURE_RANDOM(rand);
  1055. struct rcu_torture *p;
  1056. int pipe_count;
  1057. struct timer_list t;
  1058. unsigned long long ts;
  1059. VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
  1060. set_user_nice(current, MAX_NICE);
  1061. if (irqreader && cur_ops->irq_capable)
  1062. setup_timer_on_stack(&t, rcu_torture_timer, 0);
  1063. do {
  1064. if (irqreader && cur_ops->irq_capable) {
  1065. if (!timer_pending(&t))
  1066. mod_timer(&t, jiffies + 1);
  1067. }
  1068. idx = cur_ops->readlock();
  1069. if (cur_ops->started)
  1070. started = cur_ops->started();
  1071. else
  1072. started = cur_ops->completed();
  1073. ts = rcu_trace_clock_local();
  1074. p = rcu_dereference_check(rcu_torture_current,
  1075. rcu_read_lock_bh_held() ||
  1076. rcu_read_lock_sched_held() ||
  1077. srcu_read_lock_held(srcu_ctlp) ||
  1078. torturing_tasks());
  1079. if (p == NULL) {
  1080. /* Wait for rcu_torture_writer to get underway */
  1081. cur_ops->readunlock(idx);
  1082. schedule_timeout_interruptible(HZ);
  1083. continue;
  1084. }
  1085. if (p->rtort_mbtest == 0)
  1086. atomic_inc(&n_rcu_torture_mberror);
  1087. cur_ops->read_delay(&rand);
  1088. preempt_disable();
  1089. pipe_count = p->rtort_pipe_count;
  1090. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  1091. /* Should not happen, but... */
  1092. pipe_count = RCU_TORTURE_PIPE_LEN;
  1093. }
  1094. completed = cur_ops->completed();
  1095. if (pipe_count > 1) {
  1096. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
  1097. ts, started, completed);
  1098. rcu_ftrace_dump(DUMP_ALL);
  1099. }
  1100. __this_cpu_inc(rcu_torture_count[pipe_count]);
  1101. completed = completed - started;
  1102. if (cur_ops->started)
  1103. completed++;
  1104. if (completed > RCU_TORTURE_PIPE_LEN) {
  1105. /* Should not happen, but... */
  1106. completed = RCU_TORTURE_PIPE_LEN;
  1107. }
  1108. __this_cpu_inc(rcu_torture_batch[completed]);
  1109. preempt_enable();
  1110. cur_ops->readunlock(idx);
  1111. stutter_wait("rcu_torture_reader");
  1112. } while (!torture_must_stop());
  1113. if (irqreader && cur_ops->irq_capable) {
  1114. del_timer_sync(&t);
  1115. destroy_timer_on_stack(&t);
  1116. }
  1117. torture_kthread_stopping("rcu_torture_reader");
  1118. return 0;
  1119. }
  1120. /*
  1121. * Print torture statistics. Caller must ensure that there is only
  1122. * one call to this function at a given time!!! This is normally
  1123. * accomplished by relying on the module system to only have one copy
  1124. * of the module loaded, and then by giving the rcu_torture_stats
  1125. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  1126. * thread is not running).
  1127. */
  1128. static void
  1129. rcu_torture_stats_print(void)
  1130. {
  1131. int cpu;
  1132. int i;
  1133. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  1134. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  1135. static unsigned long rtcv_snap = ULONG_MAX;
  1136. struct task_struct *wtp;
  1137. for_each_possible_cpu(cpu) {
  1138. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1139. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  1140. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  1141. }
  1142. }
  1143. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  1144. if (pipesummary[i] != 0)
  1145. break;
  1146. }
  1147. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  1148. pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
  1149. rcu_torture_current,
  1150. rcu_torture_current_version,
  1151. list_empty(&rcu_torture_freelist),
  1152. atomic_read(&n_rcu_torture_alloc),
  1153. atomic_read(&n_rcu_torture_alloc_fail),
  1154. atomic_read(&n_rcu_torture_free));
  1155. pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
  1156. atomic_read(&n_rcu_torture_mberror),
  1157. n_rcu_torture_barrier_error,
  1158. n_rcu_torture_boost_ktrerror,
  1159. n_rcu_torture_boost_rterror);
  1160. pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
  1161. n_rcu_torture_boost_failure,
  1162. n_rcu_torture_boosts,
  1163. n_rcu_torture_timers);
  1164. torture_onoff_stats();
  1165. pr_cont("barrier: %ld/%ld:%ld ",
  1166. n_barrier_successes,
  1167. n_barrier_attempts,
  1168. n_rcu_torture_barrier_error);
  1169. pr_cont("cbflood: %ld\n", atomic_long_read(&n_cbfloods));
  1170. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  1171. if (atomic_read(&n_rcu_torture_mberror) != 0 ||
  1172. n_rcu_torture_barrier_error != 0 ||
  1173. n_rcu_torture_boost_ktrerror != 0 ||
  1174. n_rcu_torture_boost_rterror != 0 ||
  1175. n_rcu_torture_boost_failure != 0 ||
  1176. i > 1) {
  1177. pr_cont("%s", "!!! ");
  1178. atomic_inc(&n_rcu_torture_error);
  1179. WARN_ON_ONCE(1);
  1180. }
  1181. pr_cont("Reader Pipe: ");
  1182. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1183. pr_cont(" %ld", pipesummary[i]);
  1184. pr_cont("\n");
  1185. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  1186. pr_cont("Reader Batch: ");
  1187. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1188. pr_cont(" %ld", batchsummary[i]);
  1189. pr_cont("\n");
  1190. pr_alert("%s%s ", torture_type, TORTURE_FLAG);
  1191. pr_cont("Free-Block Circulation: ");
  1192. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1193. pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
  1194. }
  1195. pr_cont("\n");
  1196. if (cur_ops->stats)
  1197. cur_ops->stats();
  1198. if (rtcv_snap == rcu_torture_current_version &&
  1199. rcu_torture_current != NULL) {
  1200. int __maybe_unused flags;
  1201. unsigned long __maybe_unused gpnum;
  1202. unsigned long __maybe_unused completed;
  1203. rcutorture_get_gp_data(cur_ops->ttype,
  1204. &flags, &gpnum, &completed);
  1205. wtp = READ_ONCE(writer_task);
  1206. pr_alert("??? Writer stall state %s(%d) g%lu c%lu f%#x ->state %#lx\n",
  1207. rcu_torture_writer_state_getname(),
  1208. rcu_torture_writer_state,
  1209. gpnum, completed, flags,
  1210. wtp == NULL ? ~0UL : wtp->state);
  1211. show_rcu_gp_kthreads();
  1212. rcu_ftrace_dump(DUMP_ALL);
  1213. }
  1214. rtcv_snap = rcu_torture_current_version;
  1215. }
  1216. /*
  1217. * Periodically prints torture statistics, if periodic statistics printing
  1218. * was specified via the stat_interval module parameter.
  1219. */
  1220. static int
  1221. rcu_torture_stats(void *arg)
  1222. {
  1223. VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
  1224. do {
  1225. schedule_timeout_interruptible(stat_interval * HZ);
  1226. rcu_torture_stats_print();
  1227. torture_shutdown_absorb("rcu_torture_stats");
  1228. } while (!torture_must_stop());
  1229. torture_kthread_stopping("rcu_torture_stats");
  1230. return 0;
  1231. }
  1232. static inline void
  1233. rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
  1234. {
  1235. pr_alert("%s" TORTURE_FLAG
  1236. "--- %s: nreaders=%d nfakewriters=%d "
  1237. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  1238. "shuffle_interval=%d stutter=%d irqreader=%d "
  1239. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
  1240. "test_boost=%d/%d test_boost_interval=%d "
  1241. "test_boost_duration=%d shutdown_secs=%d "
  1242. "stall_cpu=%d stall_cpu_holdoff=%d "
  1243. "n_barrier_cbs=%d "
  1244. "onoff_interval=%d onoff_holdoff=%d\n",
  1245. torture_type, tag, nrealreaders, nfakewriters,
  1246. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  1247. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
  1248. test_boost, cur_ops->can_boost,
  1249. test_boost_interval, test_boost_duration, shutdown_secs,
  1250. stall_cpu, stall_cpu_holdoff,
  1251. n_barrier_cbs,
  1252. onoff_interval, onoff_holdoff);
  1253. }
  1254. static int rcutorture_booster_cleanup(unsigned int cpu)
  1255. {
  1256. struct task_struct *t;
  1257. if (boost_tasks[cpu] == NULL)
  1258. return 0;
  1259. mutex_lock(&boost_mutex);
  1260. t = boost_tasks[cpu];
  1261. boost_tasks[cpu] = NULL;
  1262. mutex_unlock(&boost_mutex);
  1263. /* This must be outside of the mutex, otherwise deadlock! */
  1264. torture_stop_kthread(rcu_torture_boost, t);
  1265. return 0;
  1266. }
  1267. static int rcutorture_booster_init(unsigned int cpu)
  1268. {
  1269. int retval;
  1270. if (boost_tasks[cpu] != NULL)
  1271. return 0; /* Already created, nothing more to do. */
  1272. /* Don't allow time recalculation while creating a new task. */
  1273. mutex_lock(&boost_mutex);
  1274. VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
  1275. boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
  1276. cpu_to_node(cpu),
  1277. "rcu_torture_boost");
  1278. if (IS_ERR(boost_tasks[cpu])) {
  1279. retval = PTR_ERR(boost_tasks[cpu]);
  1280. VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
  1281. n_rcu_torture_boost_ktrerror++;
  1282. boost_tasks[cpu] = NULL;
  1283. mutex_unlock(&boost_mutex);
  1284. return retval;
  1285. }
  1286. kthread_bind(boost_tasks[cpu], cpu);
  1287. wake_up_process(boost_tasks[cpu]);
  1288. mutex_unlock(&boost_mutex);
  1289. return 0;
  1290. }
  1291. /*
  1292. * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
  1293. * induces a CPU stall for the time specified by stall_cpu.
  1294. */
  1295. static int rcu_torture_stall(void *args)
  1296. {
  1297. unsigned long stop_at;
  1298. VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
  1299. if (stall_cpu_holdoff > 0) {
  1300. VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
  1301. schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
  1302. VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
  1303. }
  1304. if (!kthread_should_stop()) {
  1305. stop_at = get_seconds() + stall_cpu;
  1306. /* RCU CPU stall is expected behavior in following code. */
  1307. pr_alert("rcu_torture_stall start.\n");
  1308. rcu_read_lock();
  1309. preempt_disable();
  1310. while (ULONG_CMP_LT(get_seconds(), stop_at))
  1311. continue; /* Induce RCU CPU stall warning. */
  1312. preempt_enable();
  1313. rcu_read_unlock();
  1314. pr_alert("rcu_torture_stall end.\n");
  1315. }
  1316. torture_shutdown_absorb("rcu_torture_stall");
  1317. while (!kthread_should_stop())
  1318. schedule_timeout_interruptible(10 * HZ);
  1319. return 0;
  1320. }
  1321. /* Spawn CPU-stall kthread, if stall_cpu specified. */
  1322. static int __init rcu_torture_stall_init(void)
  1323. {
  1324. if (stall_cpu <= 0)
  1325. return 0;
  1326. return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
  1327. }
  1328. /* Callback function for RCU barrier testing. */
  1329. static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
  1330. {
  1331. atomic_inc(&barrier_cbs_invoked);
  1332. }
  1333. /* kthread function to register callbacks used to test RCU barriers. */
  1334. static int rcu_torture_barrier_cbs(void *arg)
  1335. {
  1336. long myid = (long)arg;
  1337. bool lastphase = 0;
  1338. bool newphase;
  1339. struct rcu_head rcu;
  1340. init_rcu_head_on_stack(&rcu);
  1341. VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
  1342. set_user_nice(current, MAX_NICE);
  1343. do {
  1344. wait_event(barrier_cbs_wq[myid],
  1345. (newphase =
  1346. smp_load_acquire(&barrier_phase)) != lastphase ||
  1347. torture_must_stop());
  1348. lastphase = newphase;
  1349. if (torture_must_stop())
  1350. break;
  1351. /*
  1352. * The above smp_load_acquire() ensures barrier_phase load
  1353. * is ordered before the following ->call().
  1354. */
  1355. local_irq_disable(); /* Just to test no-irq call_rcu(). */
  1356. cur_ops->call(&rcu, rcu_torture_barrier_cbf);
  1357. local_irq_enable();
  1358. if (atomic_dec_and_test(&barrier_cbs_count))
  1359. wake_up(&barrier_wq);
  1360. } while (!torture_must_stop());
  1361. if (cur_ops->cb_barrier != NULL)
  1362. cur_ops->cb_barrier();
  1363. destroy_rcu_head_on_stack(&rcu);
  1364. torture_kthread_stopping("rcu_torture_barrier_cbs");
  1365. return 0;
  1366. }
  1367. /* kthread function to drive and coordinate RCU barrier testing. */
  1368. static int rcu_torture_barrier(void *arg)
  1369. {
  1370. int i;
  1371. VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
  1372. do {
  1373. atomic_set(&barrier_cbs_invoked, 0);
  1374. atomic_set(&barrier_cbs_count, n_barrier_cbs);
  1375. /* Ensure barrier_phase ordered after prior assignments. */
  1376. smp_store_release(&barrier_phase, !barrier_phase);
  1377. for (i = 0; i < n_barrier_cbs; i++)
  1378. wake_up(&barrier_cbs_wq[i]);
  1379. wait_event(barrier_wq,
  1380. atomic_read(&barrier_cbs_count) == 0 ||
  1381. torture_must_stop());
  1382. if (torture_must_stop())
  1383. break;
  1384. n_barrier_attempts++;
  1385. cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
  1386. if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
  1387. n_rcu_torture_barrier_error++;
  1388. pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
  1389. atomic_read(&barrier_cbs_invoked),
  1390. n_barrier_cbs);
  1391. WARN_ON_ONCE(1);
  1392. }
  1393. n_barrier_successes++;
  1394. schedule_timeout_interruptible(HZ / 10);
  1395. } while (!torture_must_stop());
  1396. torture_kthread_stopping("rcu_torture_barrier");
  1397. return 0;
  1398. }
  1399. /* Initialize RCU barrier testing. */
  1400. static int rcu_torture_barrier_init(void)
  1401. {
  1402. int i;
  1403. int ret;
  1404. if (n_barrier_cbs <= 0)
  1405. return 0;
  1406. if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
  1407. pr_alert("%s" TORTURE_FLAG
  1408. " Call or barrier ops missing for %s,\n",
  1409. torture_type, cur_ops->name);
  1410. pr_alert("%s" TORTURE_FLAG
  1411. " RCU barrier testing omitted from run.\n",
  1412. torture_type);
  1413. return 0;
  1414. }
  1415. atomic_set(&barrier_cbs_count, 0);
  1416. atomic_set(&barrier_cbs_invoked, 0);
  1417. barrier_cbs_tasks =
  1418. kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
  1419. GFP_KERNEL);
  1420. barrier_cbs_wq =
  1421. kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
  1422. GFP_KERNEL);
  1423. if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
  1424. return -ENOMEM;
  1425. for (i = 0; i < n_barrier_cbs; i++) {
  1426. init_waitqueue_head(&barrier_cbs_wq[i]);
  1427. ret = torture_create_kthread(rcu_torture_barrier_cbs,
  1428. (void *)(long)i,
  1429. barrier_cbs_tasks[i]);
  1430. if (ret)
  1431. return ret;
  1432. }
  1433. return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
  1434. }
  1435. /* Clean up after RCU barrier testing. */
  1436. static void rcu_torture_barrier_cleanup(void)
  1437. {
  1438. int i;
  1439. torture_stop_kthread(rcu_torture_barrier, barrier_task);
  1440. if (barrier_cbs_tasks != NULL) {
  1441. for (i = 0; i < n_barrier_cbs; i++)
  1442. torture_stop_kthread(rcu_torture_barrier_cbs,
  1443. barrier_cbs_tasks[i]);
  1444. kfree(barrier_cbs_tasks);
  1445. barrier_cbs_tasks = NULL;
  1446. }
  1447. if (barrier_cbs_wq != NULL) {
  1448. kfree(barrier_cbs_wq);
  1449. barrier_cbs_wq = NULL;
  1450. }
  1451. }
  1452. static enum cpuhp_state rcutor_hp;
  1453. static void
  1454. rcu_torture_cleanup(void)
  1455. {
  1456. int i;
  1457. rcutorture_record_test_transition();
  1458. if (torture_cleanup_begin()) {
  1459. if (cur_ops->cb_barrier != NULL)
  1460. cur_ops->cb_barrier();
  1461. return;
  1462. }
  1463. rcu_torture_barrier_cleanup();
  1464. torture_stop_kthread(rcu_torture_stall, stall_task);
  1465. torture_stop_kthread(rcu_torture_writer, writer_task);
  1466. if (reader_tasks) {
  1467. for (i = 0; i < nrealreaders; i++)
  1468. torture_stop_kthread(rcu_torture_reader,
  1469. reader_tasks[i]);
  1470. kfree(reader_tasks);
  1471. }
  1472. rcu_torture_current = NULL;
  1473. if (fakewriter_tasks) {
  1474. for (i = 0; i < nfakewriters; i++) {
  1475. torture_stop_kthread(rcu_torture_fakewriter,
  1476. fakewriter_tasks[i]);
  1477. }
  1478. kfree(fakewriter_tasks);
  1479. fakewriter_tasks = NULL;
  1480. }
  1481. torture_stop_kthread(rcu_torture_stats, stats_task);
  1482. torture_stop_kthread(rcu_torture_fqs, fqs_task);
  1483. for (i = 0; i < ncbflooders; i++)
  1484. torture_stop_kthread(rcu_torture_cbflood, cbflood_task[i]);
  1485. if ((test_boost == 1 && cur_ops->can_boost) ||
  1486. test_boost == 2)
  1487. cpuhp_remove_state(rcutor_hp);
  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. firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
  1695. rcutorture_booster_init,
  1696. rcutorture_booster_cleanup);
  1697. if (firsterr < 0)
  1698. goto unwind;
  1699. rcutor_hp = firsterr;
  1700. }
  1701. firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
  1702. if (firsterr)
  1703. goto unwind;
  1704. firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval * HZ);
  1705. if (firsterr)
  1706. goto unwind;
  1707. firsterr = rcu_torture_stall_init();
  1708. if (firsterr)
  1709. goto unwind;
  1710. firsterr = rcu_torture_barrier_init();
  1711. if (firsterr)
  1712. goto unwind;
  1713. if (object_debug)
  1714. rcu_test_debug_objects();
  1715. if (cbflood_n_burst > 0) {
  1716. /* Create the cbflood threads */
  1717. ncbflooders = (num_online_cpus() + 3) / 4;
  1718. cbflood_task = kcalloc(ncbflooders, sizeof(*cbflood_task),
  1719. GFP_KERNEL);
  1720. if (!cbflood_task) {
  1721. VERBOSE_TOROUT_ERRSTRING("out of memory");
  1722. firsterr = -ENOMEM;
  1723. goto unwind;
  1724. }
  1725. for (i = 0; i < ncbflooders; i++) {
  1726. firsterr = torture_create_kthread(rcu_torture_cbflood,
  1727. NULL,
  1728. cbflood_task[i]);
  1729. if (firsterr)
  1730. goto unwind;
  1731. }
  1732. }
  1733. rcutorture_record_test_transition();
  1734. torture_init_end();
  1735. return 0;
  1736. unwind:
  1737. torture_init_end();
  1738. rcu_torture_cleanup();
  1739. return firsterr;
  1740. }
  1741. module_init(rcu_torture_init);
  1742. module_exit(rcu_torture_cleanup);