rcutorture.c 62 KB

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