rcutorture.c 55 KB

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