rcutorture.c 54 KB

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