rcutorture.c 55 KB

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