torture.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2005, 2006
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. * Josh Triplett <josh@freedesktop.org>
  22. *
  23. * See also: Documentation/RCU/torture.txt
  24. */
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/kthread.h>
  30. #include <linux/err.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/smp.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/sched.h>
  36. #include <linux/atomic.h>
  37. #include <linux/bitops.h>
  38. #include <linux/completion.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/percpu.h>
  41. #include <linux/notifier.h>
  42. #include <linux/reboot.h>
  43. #include <linux/freezer.h>
  44. #include <linux/cpu.h>
  45. #include <linux/delay.h>
  46. #include <linux/stat.h>
  47. #include <linux/srcu.h>
  48. #include <linux/slab.h>
  49. #include <linux/trace_clock.h>
  50. #include <asm/byteorder.h>
  51. MODULE_LICENSE("GPL");
  52. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@freedesktop.org>");
  53. MODULE_ALIAS("rcutorture");
  54. #ifdef MODULE_PARAM_PREFIX
  55. #undef MODULE_PARAM_PREFIX
  56. #endif
  57. #define MODULE_PARAM_PREFIX "rcutorture."
  58. static int fqs_duration;
  59. module_param(fqs_duration, int, 0444);
  60. MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us), 0 to disable");
  61. static int fqs_holdoff;
  62. module_param(fqs_holdoff, int, 0444);
  63. MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
  64. static int fqs_stutter = 3;
  65. module_param(fqs_stutter, int, 0444);
  66. MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
  67. static bool gp_exp;
  68. module_param(gp_exp, bool, 0444);
  69. MODULE_PARM_DESC(gp_exp, "Use expedited GP wait primitives");
  70. static bool gp_normal;
  71. module_param(gp_normal, bool, 0444);
  72. MODULE_PARM_DESC(gp_normal, "Use normal (non-expedited) GP wait primitives");
  73. static int irqreader = 1;
  74. module_param(irqreader, int, 0444);
  75. MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
  76. static int n_barrier_cbs;
  77. module_param(n_barrier_cbs, int, 0444);
  78. MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
  79. static int nfakewriters = 4;
  80. module_param(nfakewriters, int, 0444);
  81. MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
  82. static int nreaders = -1;
  83. module_param(nreaders, int, 0444);
  84. MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  85. static int object_debug;
  86. module_param(object_debug, int, 0444);
  87. MODULE_PARM_DESC(object_debug, "Enable debug-object double call_rcu() testing");
  88. static int onoff_holdoff;
  89. module_param(onoff_holdoff, int, 0444);
  90. MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
  91. static int onoff_interval;
  92. module_param(onoff_interval, int, 0444);
  93. MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
  94. static int shuffle_interval = 3;
  95. module_param(shuffle_interval, int, 0444);
  96. MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
  97. static int shutdown_secs;
  98. module_param(shutdown_secs, int, 0444);
  99. MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), <= zero to disable.");
  100. static int stall_cpu;
  101. module_param(stall_cpu, int, 0444);
  102. MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
  103. static int stall_cpu_holdoff = 10;
  104. module_param(stall_cpu_holdoff, int, 0444);
  105. MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
  106. static int stat_interval = 60;
  107. module_param(stat_interval, int, 0644);
  108. MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  109. static int stutter = 5;
  110. module_param(stutter, int, 0444);
  111. MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
  112. static int test_boost = 1;
  113. module_param(test_boost, int, 0444);
  114. MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
  115. static int test_boost_duration = 4;
  116. module_param(test_boost_duration, int, 0444);
  117. MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
  118. static int test_boost_interval = 7;
  119. module_param(test_boost_interval, int, 0444);
  120. MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
  121. static bool test_no_idle_hz = true;
  122. module_param(test_no_idle_hz, bool, 0444);
  123. MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
  124. static char *torture_type = "rcu";
  125. module_param(torture_type, charp, 0444);
  126. MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
  127. static bool verbose;
  128. module_param(verbose, bool, 0444);
  129. MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  130. #define TORTURE_FLAG "-torture:"
  131. #define PRINTK_STRING(s) \
  132. do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  133. #define VERBOSE_PRINTK_STRING(s) \
  134. do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  135. #define VERBOSE_PRINTK_ERRSTRING(s) \
  136. do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
  137. static char printk_buf[4096];
  138. static int nrealreaders;
  139. static struct task_struct *writer_task;
  140. static struct task_struct **fakewriter_tasks;
  141. static struct task_struct **reader_tasks;
  142. static struct task_struct *stats_task;
  143. static struct task_struct *shuffler_task;
  144. static struct task_struct *stutter_task;
  145. static struct task_struct *fqs_task;
  146. static struct task_struct *boost_tasks[NR_CPUS];
  147. static struct task_struct *shutdown_task;
  148. #ifdef CONFIG_HOTPLUG_CPU
  149. static struct task_struct *onoff_task;
  150. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  151. static struct task_struct *stall_task;
  152. static struct task_struct **barrier_cbs_tasks;
  153. static struct task_struct *barrier_task;
  154. #define RCU_TORTURE_PIPE_LEN 10
  155. struct rcu_torture {
  156. struct rcu_head rtort_rcu;
  157. int rtort_pipe_count;
  158. struct list_head rtort_free;
  159. int rtort_mbtest;
  160. };
  161. static LIST_HEAD(rcu_torture_freelist);
  162. static struct rcu_torture __rcu *rcu_torture_current;
  163. static unsigned long rcu_torture_current_version;
  164. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  165. static DEFINE_SPINLOCK(rcu_torture_lock);
  166. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
  167. { 0 };
  168. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
  169. { 0 };
  170. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  171. static atomic_t n_rcu_torture_alloc;
  172. static atomic_t n_rcu_torture_alloc_fail;
  173. static atomic_t n_rcu_torture_free;
  174. static atomic_t n_rcu_torture_mberror;
  175. static atomic_t n_rcu_torture_error;
  176. static long n_rcu_torture_barrier_error;
  177. static long n_rcu_torture_boost_ktrerror;
  178. static long n_rcu_torture_boost_rterror;
  179. static long n_rcu_torture_boost_failure;
  180. static long n_rcu_torture_boosts;
  181. static long n_rcu_torture_timers;
  182. static long n_offline_attempts;
  183. static long n_offline_successes;
  184. static unsigned long sum_offline;
  185. static int min_offline = -1;
  186. static int max_offline;
  187. static long n_online_attempts;
  188. static long n_online_successes;
  189. static unsigned long sum_online;
  190. static int min_online = -1;
  191. static int max_online;
  192. static long n_barrier_attempts;
  193. static long n_barrier_successes;
  194. static struct list_head rcu_torture_removed;
  195. static cpumask_var_t shuffle_tmp_mask;
  196. static int stutter_pause_test;
  197. #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
  198. #define RCUTORTURE_RUNNABLE_INIT 1
  199. #else
  200. #define RCUTORTURE_RUNNABLE_INIT 0
  201. #endif
  202. int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
  203. module_param(rcutorture_runnable, int, 0444);
  204. MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
  205. #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
  206. #define rcu_can_boost() 1
  207. #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
  208. #define rcu_can_boost() 0
  209. #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
  210. #ifdef CONFIG_RCU_TRACE
  211. static u64 notrace rcu_trace_clock_local(void)
  212. {
  213. u64 ts = trace_clock_local();
  214. unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
  215. return ts;
  216. }
  217. #else /* #ifdef CONFIG_RCU_TRACE */
  218. static u64 notrace rcu_trace_clock_local(void)
  219. {
  220. return 0ULL;
  221. }
  222. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  223. static unsigned long shutdown_time; /* jiffies to system shutdown. */
  224. static unsigned long boost_starttime; /* jiffies of next boost test start. */
  225. DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
  226. /* and boost task create/destroy. */
  227. static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
  228. static bool barrier_phase; /* Test phase. */
  229. static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
  230. static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
  231. static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
  232. /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
  233. #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
  234. #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
  235. #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
  236. static int fullstop = FULLSTOP_RMMOD;
  237. /*
  238. * Protect fullstop transitions and spawning of kthreads.
  239. */
  240. static DEFINE_MUTEX(fullstop_mutex);
  241. /* Forward reference. */
  242. static void rcu_torture_cleanup(void);
  243. /*
  244. * Detect and respond to a system shutdown.
  245. */
  246. static int
  247. rcutorture_shutdown_notify(struct notifier_block *unused1,
  248. unsigned long unused2, void *unused3)
  249. {
  250. mutex_lock(&fullstop_mutex);
  251. if (fullstop == FULLSTOP_DONTSTOP)
  252. fullstop = FULLSTOP_SHUTDOWN;
  253. else
  254. pr_warn(/* but going down anyway, so... */
  255. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  256. mutex_unlock(&fullstop_mutex);
  257. return NOTIFY_DONE;
  258. }
  259. /*
  260. * Absorb kthreads into a kernel function that won't return, so that
  261. * they won't ever access module text or data again.
  262. */
  263. static void rcutorture_shutdown_absorb(const char *title)
  264. {
  265. if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  266. pr_notice(
  267. "rcutorture thread %s parking due to system shutdown\n",
  268. title);
  269. schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  270. }
  271. }
  272. /*
  273. * Allocate an element from the rcu_tortures pool.
  274. */
  275. static struct rcu_torture *
  276. rcu_torture_alloc(void)
  277. {
  278. struct list_head *p;
  279. spin_lock_bh(&rcu_torture_lock);
  280. if (list_empty(&rcu_torture_freelist)) {
  281. atomic_inc(&n_rcu_torture_alloc_fail);
  282. spin_unlock_bh(&rcu_torture_lock);
  283. return NULL;
  284. }
  285. atomic_inc(&n_rcu_torture_alloc);
  286. p = rcu_torture_freelist.next;
  287. list_del_init(p);
  288. spin_unlock_bh(&rcu_torture_lock);
  289. return container_of(p, struct rcu_torture, rtort_free);
  290. }
  291. /*
  292. * Free an element to the rcu_tortures pool.
  293. */
  294. static void
  295. rcu_torture_free(struct rcu_torture *p)
  296. {
  297. atomic_inc(&n_rcu_torture_free);
  298. spin_lock_bh(&rcu_torture_lock);
  299. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  300. spin_unlock_bh(&rcu_torture_lock);
  301. }
  302. struct rcu_random_state {
  303. unsigned long rrs_state;
  304. long rrs_count;
  305. };
  306. #define RCU_RANDOM_MULT 39916801 /* prime */
  307. #define RCU_RANDOM_ADD 479001701 /* prime */
  308. #define RCU_RANDOM_REFRESH 10000
  309. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  310. /*
  311. * Crude but fast random-number generator. Uses a linear congruential
  312. * generator, with occasional help from cpu_clock().
  313. */
  314. static unsigned long
  315. rcu_random(struct rcu_random_state *rrsp)
  316. {
  317. if (--rrsp->rrs_count < 0) {
  318. rrsp->rrs_state += (unsigned long)local_clock();
  319. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  320. }
  321. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  322. return swahw32(rrsp->rrs_state);
  323. }
  324. static void
  325. rcu_stutter_wait(const char *title)
  326. {
  327. while (stutter_pause_test || !rcutorture_runnable) {
  328. if (rcutorture_runnable)
  329. schedule_timeout_interruptible(1);
  330. else
  331. schedule_timeout_interruptible(round_jiffies_relative(HZ));
  332. rcutorture_shutdown_absorb(title);
  333. }
  334. }
  335. /*
  336. * Operations vector for selecting different types of tests.
  337. */
  338. struct rcu_torture_ops {
  339. void (*init)(void);
  340. int (*readlock)(void);
  341. void (*read_delay)(struct rcu_random_state *rrsp);
  342. void (*readunlock)(int idx);
  343. int (*completed)(void);
  344. void (*deferred_free)(struct rcu_torture *p);
  345. void (*sync)(void);
  346. void (*exp_sync)(void);
  347. void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
  348. void (*cb_barrier)(void);
  349. void (*fqs)(void);
  350. int (*stats)(char *page);
  351. int irq_capable;
  352. int can_boost;
  353. const char *name;
  354. };
  355. static struct rcu_torture_ops *cur_ops;
  356. /*
  357. * Definitions for rcu torture testing.
  358. */
  359. static int rcu_torture_read_lock(void) __acquires(RCU)
  360. {
  361. rcu_read_lock();
  362. return 0;
  363. }
  364. static void rcu_read_delay(struct rcu_random_state *rrsp)
  365. {
  366. const unsigned long shortdelay_us = 200;
  367. const unsigned long longdelay_ms = 50;
  368. /* We want a short delay sometimes to make a reader delay the grace
  369. * period, and we want a long delay occasionally to trigger
  370. * force_quiescent_state. */
  371. if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
  372. mdelay(longdelay_ms);
  373. if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
  374. udelay(shortdelay_us);
  375. #ifdef CONFIG_PREEMPT
  376. if (!preempt_count() && !(rcu_random(rrsp) % (nrealreaders * 20000)))
  377. preempt_schedule(); /* No QS if preempt_disable() in effect */
  378. #endif
  379. }
  380. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  381. {
  382. rcu_read_unlock();
  383. }
  384. static int rcu_torture_completed(void)
  385. {
  386. return rcu_batches_completed();
  387. }
  388. static void
  389. rcu_torture_cb(struct rcu_head *p)
  390. {
  391. int i;
  392. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  393. if (fullstop != FULLSTOP_DONTSTOP) {
  394. /* Test is ending, just drop callbacks on the floor. */
  395. /* The next initialization will pick up the pieces. */
  396. return;
  397. }
  398. i = rp->rtort_pipe_count;
  399. if (i > RCU_TORTURE_PIPE_LEN)
  400. i = RCU_TORTURE_PIPE_LEN;
  401. atomic_inc(&rcu_torture_wcount[i]);
  402. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  403. rp->rtort_mbtest = 0;
  404. rcu_torture_free(rp);
  405. } else {
  406. cur_ops->deferred_free(rp);
  407. }
  408. }
  409. static int rcu_no_completed(void)
  410. {
  411. return 0;
  412. }
  413. static void rcu_torture_deferred_free(struct rcu_torture *p)
  414. {
  415. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  416. }
  417. static void rcu_sync_torture_init(void)
  418. {
  419. INIT_LIST_HEAD(&rcu_torture_removed);
  420. }
  421. static struct rcu_torture_ops rcu_ops = {
  422. .init = rcu_sync_torture_init,
  423. .readlock = rcu_torture_read_lock,
  424. .read_delay = rcu_read_delay,
  425. .readunlock = rcu_torture_read_unlock,
  426. .completed = rcu_torture_completed,
  427. .deferred_free = rcu_torture_deferred_free,
  428. .sync = synchronize_rcu,
  429. .exp_sync = synchronize_rcu_expedited,
  430. .call = call_rcu,
  431. .cb_barrier = rcu_barrier,
  432. .fqs = rcu_force_quiescent_state,
  433. .stats = NULL,
  434. .irq_capable = 1,
  435. .can_boost = rcu_can_boost(),
  436. .name = "rcu"
  437. };
  438. /*
  439. * Definitions for rcu_bh torture testing.
  440. */
  441. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  442. {
  443. rcu_read_lock_bh();
  444. return 0;
  445. }
  446. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  447. {
  448. rcu_read_unlock_bh();
  449. }
  450. static int rcu_bh_torture_completed(void)
  451. {
  452. return rcu_batches_completed_bh();
  453. }
  454. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  455. {
  456. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  457. }
  458. static struct rcu_torture_ops rcu_bh_ops = {
  459. .init = rcu_sync_torture_init,
  460. .readlock = rcu_bh_torture_read_lock,
  461. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  462. .readunlock = rcu_bh_torture_read_unlock,
  463. .completed = rcu_bh_torture_completed,
  464. .deferred_free = rcu_bh_torture_deferred_free,
  465. .sync = synchronize_rcu_bh,
  466. .exp_sync = synchronize_rcu_bh_expedited,
  467. .call = call_rcu_bh,
  468. .cb_barrier = rcu_barrier_bh,
  469. .fqs = rcu_bh_force_quiescent_state,
  470. .stats = NULL,
  471. .irq_capable = 1,
  472. .name = "rcu_bh"
  473. };
  474. /*
  475. * Definitions for srcu torture testing.
  476. */
  477. DEFINE_STATIC_SRCU(srcu_ctl);
  478. static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
  479. {
  480. return srcu_read_lock(&srcu_ctl);
  481. }
  482. static void srcu_read_delay(struct rcu_random_state *rrsp)
  483. {
  484. long delay;
  485. const long uspertick = 1000000 / HZ;
  486. const long longdelay = 10;
  487. /* We want there to be long-running readers, but not all the time. */
  488. delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
  489. if (!delay)
  490. schedule_timeout_interruptible(longdelay);
  491. else
  492. rcu_read_delay(rrsp);
  493. }
  494. static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
  495. {
  496. srcu_read_unlock(&srcu_ctl, idx);
  497. }
  498. static int srcu_torture_completed(void)
  499. {
  500. return srcu_batches_completed(&srcu_ctl);
  501. }
  502. static void srcu_torture_deferred_free(struct rcu_torture *rp)
  503. {
  504. call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
  505. }
  506. static void srcu_torture_synchronize(void)
  507. {
  508. synchronize_srcu(&srcu_ctl);
  509. }
  510. static void srcu_torture_call(struct rcu_head *head,
  511. void (*func)(struct rcu_head *head))
  512. {
  513. call_srcu(&srcu_ctl, head, func);
  514. }
  515. static void srcu_torture_barrier(void)
  516. {
  517. srcu_barrier(&srcu_ctl);
  518. }
  519. static int srcu_torture_stats(char *page)
  520. {
  521. int cnt = 0;
  522. int cpu;
  523. int idx = srcu_ctl.completed & 0x1;
  524. cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
  525. torture_type, TORTURE_FLAG, idx);
  526. for_each_possible_cpu(cpu) {
  527. cnt += sprintf(&page[cnt], " %d(%lu,%lu)", cpu,
  528. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
  529. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
  530. }
  531. cnt += sprintf(&page[cnt], "\n");
  532. return cnt;
  533. }
  534. static void srcu_torture_synchronize_expedited(void)
  535. {
  536. synchronize_srcu_expedited(&srcu_ctl);
  537. }
  538. static struct rcu_torture_ops srcu_ops = {
  539. .init = rcu_sync_torture_init,
  540. .readlock = srcu_torture_read_lock,
  541. .read_delay = srcu_read_delay,
  542. .readunlock = srcu_torture_read_unlock,
  543. .completed = srcu_torture_completed,
  544. .deferred_free = srcu_torture_deferred_free,
  545. .sync = srcu_torture_synchronize,
  546. .exp_sync = srcu_torture_synchronize_expedited,
  547. .call = srcu_torture_call,
  548. .cb_barrier = srcu_torture_barrier,
  549. .stats = srcu_torture_stats,
  550. .name = "srcu"
  551. };
  552. /*
  553. * Definitions for sched torture testing.
  554. */
  555. static int sched_torture_read_lock(void)
  556. {
  557. preempt_disable();
  558. return 0;
  559. }
  560. static void sched_torture_read_unlock(int idx)
  561. {
  562. preempt_enable();
  563. }
  564. static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
  565. {
  566. call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
  567. }
  568. static struct rcu_torture_ops sched_ops = {
  569. .init = rcu_sync_torture_init,
  570. .readlock = sched_torture_read_lock,
  571. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  572. .readunlock = sched_torture_read_unlock,
  573. .completed = rcu_no_completed,
  574. .deferred_free = rcu_sched_torture_deferred_free,
  575. .sync = synchronize_sched,
  576. .exp_sync = synchronize_sched_expedited,
  577. .call = call_rcu_sched,
  578. .cb_barrier = rcu_barrier_sched,
  579. .fqs = rcu_sched_force_quiescent_state,
  580. .stats = NULL,
  581. .irq_capable = 1,
  582. .name = "sched"
  583. };
  584. /*
  585. * RCU torture priority-boost testing. Runs one real-time thread per
  586. * CPU for moderate bursts, repeatedly registering RCU callbacks and
  587. * spinning waiting for them to be invoked. If a given callback takes
  588. * too long to be invoked, we assume that priority inversion has occurred.
  589. */
  590. struct rcu_boost_inflight {
  591. struct rcu_head rcu;
  592. int inflight;
  593. };
  594. static void rcu_torture_boost_cb(struct rcu_head *head)
  595. {
  596. struct rcu_boost_inflight *rbip =
  597. container_of(head, struct rcu_boost_inflight, rcu);
  598. smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
  599. rbip->inflight = 0;
  600. }
  601. static int rcu_torture_boost(void *arg)
  602. {
  603. unsigned long call_rcu_time;
  604. unsigned long endtime;
  605. unsigned long oldstarttime;
  606. struct rcu_boost_inflight rbi = { .inflight = 0 };
  607. struct sched_param sp;
  608. VERBOSE_PRINTK_STRING("rcu_torture_boost started");
  609. /* Set real-time priority. */
  610. sp.sched_priority = 1;
  611. if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
  612. VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
  613. n_rcu_torture_boost_rterror++;
  614. }
  615. init_rcu_head_on_stack(&rbi.rcu);
  616. /* Each pass through the following loop does one boost-test cycle. */
  617. do {
  618. /* Wait for the next test interval. */
  619. oldstarttime = boost_starttime;
  620. while (ULONG_CMP_LT(jiffies, oldstarttime)) {
  621. schedule_timeout_interruptible(oldstarttime - jiffies);
  622. rcu_stutter_wait("rcu_torture_boost");
  623. if (kthread_should_stop() ||
  624. fullstop != FULLSTOP_DONTSTOP)
  625. goto checkwait;
  626. }
  627. /* Do one boost-test interval. */
  628. endtime = oldstarttime + test_boost_duration * HZ;
  629. call_rcu_time = jiffies;
  630. while (ULONG_CMP_LT(jiffies, endtime)) {
  631. /* If we don't have a callback in flight, post one. */
  632. if (!rbi.inflight) {
  633. smp_mb(); /* RCU core before ->inflight = 1. */
  634. rbi.inflight = 1;
  635. call_rcu(&rbi.rcu, rcu_torture_boost_cb);
  636. if (jiffies - call_rcu_time >
  637. test_boost_duration * HZ - HZ / 2) {
  638. VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
  639. n_rcu_torture_boost_failure++;
  640. }
  641. call_rcu_time = jiffies;
  642. }
  643. cond_resched();
  644. rcu_stutter_wait("rcu_torture_boost");
  645. if (kthread_should_stop() ||
  646. fullstop != FULLSTOP_DONTSTOP)
  647. goto checkwait;
  648. }
  649. /*
  650. * Set the start time of the next test interval.
  651. * Yes, this is vulnerable to long delays, but such
  652. * delays simply cause a false negative for the next
  653. * interval. Besides, we are running at RT priority,
  654. * so delays should be relatively rare.
  655. */
  656. while (oldstarttime == boost_starttime &&
  657. !kthread_should_stop()) {
  658. if (mutex_trylock(&boost_mutex)) {
  659. boost_starttime = jiffies +
  660. test_boost_interval * HZ;
  661. n_rcu_torture_boosts++;
  662. mutex_unlock(&boost_mutex);
  663. break;
  664. }
  665. schedule_timeout_uninterruptible(1);
  666. }
  667. /* Go do the stutter. */
  668. checkwait: rcu_stutter_wait("rcu_torture_boost");
  669. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  670. /* Clean up and exit. */
  671. VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
  672. rcutorture_shutdown_absorb("rcu_torture_boost");
  673. while (!kthread_should_stop() || rbi.inflight)
  674. schedule_timeout_uninterruptible(1);
  675. smp_mb(); /* order accesses to ->inflight before stack-frame death. */
  676. destroy_rcu_head_on_stack(&rbi.rcu);
  677. return 0;
  678. }
  679. /*
  680. * RCU torture force-quiescent-state kthread. Repeatedly induces
  681. * bursts of calls to force_quiescent_state(), increasing the probability
  682. * of occurrence of some important types of race conditions.
  683. */
  684. static int
  685. rcu_torture_fqs(void *arg)
  686. {
  687. unsigned long fqs_resume_time;
  688. int fqs_burst_remaining;
  689. VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
  690. do {
  691. fqs_resume_time = jiffies + fqs_stutter * HZ;
  692. while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
  693. !kthread_should_stop()) {
  694. schedule_timeout_interruptible(1);
  695. }
  696. fqs_burst_remaining = fqs_duration;
  697. while (fqs_burst_remaining > 0 &&
  698. !kthread_should_stop()) {
  699. cur_ops->fqs();
  700. udelay(fqs_holdoff);
  701. fqs_burst_remaining -= fqs_holdoff;
  702. }
  703. rcu_stutter_wait("rcu_torture_fqs");
  704. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  705. VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
  706. rcutorture_shutdown_absorb("rcu_torture_fqs");
  707. while (!kthread_should_stop())
  708. schedule_timeout_uninterruptible(1);
  709. return 0;
  710. }
  711. /*
  712. * RCU torture writer kthread. Repeatedly substitutes a new structure
  713. * for that pointed to by rcu_torture_current, freeing the old structure
  714. * after a series of grace periods (the "pipeline").
  715. */
  716. static int
  717. rcu_torture_writer(void *arg)
  718. {
  719. bool exp;
  720. int i;
  721. struct rcu_torture *rp;
  722. struct rcu_torture *rp1;
  723. struct rcu_torture *old_rp;
  724. static DEFINE_RCU_RANDOM(rand);
  725. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  726. set_user_nice(current, 19);
  727. do {
  728. schedule_timeout_uninterruptible(1);
  729. rp = rcu_torture_alloc();
  730. if (rp == NULL)
  731. continue;
  732. rp->rtort_pipe_count = 0;
  733. udelay(rcu_random(&rand) & 0x3ff);
  734. old_rp = rcu_dereference_check(rcu_torture_current,
  735. current == writer_task);
  736. rp->rtort_mbtest = 1;
  737. rcu_assign_pointer(rcu_torture_current, rp);
  738. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  739. if (old_rp) {
  740. i = old_rp->rtort_pipe_count;
  741. if (i > RCU_TORTURE_PIPE_LEN)
  742. i = RCU_TORTURE_PIPE_LEN;
  743. atomic_inc(&rcu_torture_wcount[i]);
  744. old_rp->rtort_pipe_count++;
  745. if (gp_normal == gp_exp)
  746. exp = !!(rcu_random(&rand) & 0x80);
  747. else
  748. exp = gp_exp;
  749. if (!exp) {
  750. cur_ops->deferred_free(old_rp);
  751. } else {
  752. cur_ops->exp_sync();
  753. list_add(&old_rp->rtort_free,
  754. &rcu_torture_removed);
  755. list_for_each_entry_safe(rp, rp1,
  756. &rcu_torture_removed,
  757. rtort_free) {
  758. i = rp->rtort_pipe_count;
  759. if (i > RCU_TORTURE_PIPE_LEN)
  760. i = RCU_TORTURE_PIPE_LEN;
  761. atomic_inc(&rcu_torture_wcount[i]);
  762. if (++rp->rtort_pipe_count >=
  763. RCU_TORTURE_PIPE_LEN) {
  764. rp->rtort_mbtest = 0;
  765. list_del(&rp->rtort_free);
  766. rcu_torture_free(rp);
  767. }
  768. }
  769. }
  770. }
  771. rcutorture_record_progress(++rcu_torture_current_version);
  772. rcu_stutter_wait("rcu_torture_writer");
  773. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  774. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  775. rcutorture_shutdown_absorb("rcu_torture_writer");
  776. while (!kthread_should_stop())
  777. schedule_timeout_uninterruptible(1);
  778. return 0;
  779. }
  780. /*
  781. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  782. * delay between calls.
  783. */
  784. static int
  785. rcu_torture_fakewriter(void *arg)
  786. {
  787. DEFINE_RCU_RANDOM(rand);
  788. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
  789. set_user_nice(current, 19);
  790. do {
  791. schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
  792. udelay(rcu_random(&rand) & 0x3ff);
  793. if (cur_ops->cb_barrier != NULL &&
  794. rcu_random(&rand) % (nfakewriters * 8) == 0) {
  795. cur_ops->cb_barrier();
  796. } else if (gp_normal == gp_exp) {
  797. if (rcu_random(&rand) & 0x80)
  798. cur_ops->sync();
  799. else
  800. cur_ops->exp_sync();
  801. } else if (gp_normal) {
  802. cur_ops->sync();
  803. } else {
  804. cur_ops->exp_sync();
  805. }
  806. rcu_stutter_wait("rcu_torture_fakewriter");
  807. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  808. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
  809. rcutorture_shutdown_absorb("rcu_torture_fakewriter");
  810. while (!kthread_should_stop())
  811. schedule_timeout_uninterruptible(1);
  812. return 0;
  813. }
  814. void rcutorture_trace_dump(void)
  815. {
  816. static atomic_t beenhere = ATOMIC_INIT(0);
  817. if (atomic_read(&beenhere))
  818. return;
  819. if (atomic_xchg(&beenhere, 1) != 0)
  820. return;
  821. ftrace_dump(DUMP_ALL);
  822. }
  823. /*
  824. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  825. * incrementing the corresponding element of the pipeline array. The
  826. * counter in the element should never be greater than 1, otherwise, the
  827. * RCU implementation is broken.
  828. */
  829. static void rcu_torture_timer(unsigned long unused)
  830. {
  831. int idx;
  832. int completed;
  833. int completed_end;
  834. static DEFINE_RCU_RANDOM(rand);
  835. static DEFINE_SPINLOCK(rand_lock);
  836. struct rcu_torture *p;
  837. int pipe_count;
  838. unsigned long long ts;
  839. idx = cur_ops->readlock();
  840. completed = cur_ops->completed();
  841. ts = rcu_trace_clock_local();
  842. p = rcu_dereference_check(rcu_torture_current,
  843. rcu_read_lock_bh_held() ||
  844. rcu_read_lock_sched_held() ||
  845. srcu_read_lock_held(&srcu_ctl));
  846. if (p == NULL) {
  847. /* Leave because rcu_torture_writer is not yet underway */
  848. cur_ops->readunlock(idx);
  849. return;
  850. }
  851. if (p->rtort_mbtest == 0)
  852. atomic_inc(&n_rcu_torture_mberror);
  853. spin_lock(&rand_lock);
  854. cur_ops->read_delay(&rand);
  855. n_rcu_torture_timers++;
  856. spin_unlock(&rand_lock);
  857. preempt_disable();
  858. pipe_count = p->rtort_pipe_count;
  859. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  860. /* Should not happen, but... */
  861. pipe_count = RCU_TORTURE_PIPE_LEN;
  862. }
  863. completed_end = cur_ops->completed();
  864. if (pipe_count > 1) {
  865. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
  866. completed, completed_end);
  867. rcutorture_trace_dump();
  868. }
  869. __this_cpu_inc(rcu_torture_count[pipe_count]);
  870. completed = completed_end - completed;
  871. if (completed > RCU_TORTURE_PIPE_LEN) {
  872. /* Should not happen, but... */
  873. completed = RCU_TORTURE_PIPE_LEN;
  874. }
  875. __this_cpu_inc(rcu_torture_batch[completed]);
  876. preempt_enable();
  877. cur_ops->readunlock(idx);
  878. }
  879. /*
  880. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  881. * incrementing the corresponding element of the pipeline array. The
  882. * counter in the element should never be greater than 1, otherwise, the
  883. * RCU implementation is broken.
  884. */
  885. static int
  886. rcu_torture_reader(void *arg)
  887. {
  888. int completed;
  889. int completed_end;
  890. int idx;
  891. DEFINE_RCU_RANDOM(rand);
  892. struct rcu_torture *p;
  893. int pipe_count;
  894. struct timer_list t;
  895. unsigned long long ts;
  896. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  897. set_user_nice(current, 19);
  898. if (irqreader && cur_ops->irq_capable)
  899. setup_timer_on_stack(&t, rcu_torture_timer, 0);
  900. do {
  901. if (irqreader && cur_ops->irq_capable) {
  902. if (!timer_pending(&t))
  903. mod_timer(&t, jiffies + 1);
  904. }
  905. idx = cur_ops->readlock();
  906. completed = cur_ops->completed();
  907. ts = rcu_trace_clock_local();
  908. p = rcu_dereference_check(rcu_torture_current,
  909. rcu_read_lock_bh_held() ||
  910. rcu_read_lock_sched_held() ||
  911. srcu_read_lock_held(&srcu_ctl));
  912. if (p == NULL) {
  913. /* Wait for rcu_torture_writer to get underway */
  914. cur_ops->readunlock(idx);
  915. schedule_timeout_interruptible(HZ);
  916. continue;
  917. }
  918. if (p->rtort_mbtest == 0)
  919. atomic_inc(&n_rcu_torture_mberror);
  920. cur_ops->read_delay(&rand);
  921. preempt_disable();
  922. pipe_count = p->rtort_pipe_count;
  923. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  924. /* Should not happen, but... */
  925. pipe_count = RCU_TORTURE_PIPE_LEN;
  926. }
  927. completed_end = cur_ops->completed();
  928. if (pipe_count > 1) {
  929. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
  930. ts, completed, completed_end);
  931. rcutorture_trace_dump();
  932. }
  933. __this_cpu_inc(rcu_torture_count[pipe_count]);
  934. completed = completed_end - completed;
  935. if (completed > RCU_TORTURE_PIPE_LEN) {
  936. /* Should not happen, but... */
  937. completed = RCU_TORTURE_PIPE_LEN;
  938. }
  939. __this_cpu_inc(rcu_torture_batch[completed]);
  940. preempt_enable();
  941. cur_ops->readunlock(idx);
  942. schedule();
  943. rcu_stutter_wait("rcu_torture_reader");
  944. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  945. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  946. rcutorture_shutdown_absorb("rcu_torture_reader");
  947. if (irqreader && cur_ops->irq_capable)
  948. del_timer_sync(&t);
  949. while (!kthread_should_stop())
  950. schedule_timeout_uninterruptible(1);
  951. return 0;
  952. }
  953. /*
  954. * Create an RCU-torture statistics message in the specified buffer.
  955. */
  956. static int
  957. rcu_torture_printk(char *page)
  958. {
  959. int cnt = 0;
  960. int cpu;
  961. int i;
  962. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  963. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  964. for_each_possible_cpu(cpu) {
  965. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  966. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  967. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  968. }
  969. }
  970. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  971. if (pipesummary[i] != 0)
  972. break;
  973. }
  974. cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
  975. cnt += sprintf(&page[cnt],
  976. "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
  977. rcu_torture_current,
  978. rcu_torture_current_version,
  979. list_empty(&rcu_torture_freelist),
  980. atomic_read(&n_rcu_torture_alloc),
  981. atomic_read(&n_rcu_torture_alloc_fail),
  982. atomic_read(&n_rcu_torture_free));
  983. cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
  984. atomic_read(&n_rcu_torture_mberror),
  985. n_rcu_torture_boost_ktrerror,
  986. n_rcu_torture_boost_rterror);
  987. cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
  988. n_rcu_torture_boost_failure,
  989. n_rcu_torture_boosts,
  990. n_rcu_torture_timers);
  991. cnt += sprintf(&page[cnt],
  992. "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
  993. n_online_successes, n_online_attempts,
  994. n_offline_successes, n_offline_attempts,
  995. min_online, max_online,
  996. min_offline, max_offline,
  997. sum_online, sum_offline, HZ);
  998. cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
  999. n_barrier_successes,
  1000. n_barrier_attempts,
  1001. n_rcu_torture_barrier_error);
  1002. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1003. if (atomic_read(&n_rcu_torture_mberror) != 0 ||
  1004. n_rcu_torture_barrier_error != 0 ||
  1005. n_rcu_torture_boost_ktrerror != 0 ||
  1006. n_rcu_torture_boost_rterror != 0 ||
  1007. n_rcu_torture_boost_failure != 0 ||
  1008. i > 1) {
  1009. cnt += sprintf(&page[cnt], "!!! ");
  1010. atomic_inc(&n_rcu_torture_error);
  1011. WARN_ON_ONCE(1);
  1012. }
  1013. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  1014. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1015. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  1016. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1017. cnt += sprintf(&page[cnt], "Reader Batch: ");
  1018. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1019. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  1020. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1021. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  1022. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1023. cnt += sprintf(&page[cnt], " %d",
  1024. atomic_read(&rcu_torture_wcount[i]));
  1025. }
  1026. cnt += sprintf(&page[cnt], "\n");
  1027. if (cur_ops->stats)
  1028. cnt += cur_ops->stats(&page[cnt]);
  1029. return cnt;
  1030. }
  1031. /*
  1032. * Print torture statistics. Caller must ensure that there is only
  1033. * one call to this function at a given time!!! This is normally
  1034. * accomplished by relying on the module system to only have one copy
  1035. * of the module loaded, and then by giving the rcu_torture_stats
  1036. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  1037. * thread is not running).
  1038. */
  1039. static void
  1040. rcu_torture_stats_print(void)
  1041. {
  1042. int cnt;
  1043. cnt = rcu_torture_printk(printk_buf);
  1044. pr_alert("%s", printk_buf);
  1045. }
  1046. /*
  1047. * Periodically prints torture statistics, if periodic statistics printing
  1048. * was specified via the stat_interval module parameter.
  1049. *
  1050. * No need to worry about fullstop here, since this one doesn't reference
  1051. * volatile state or register callbacks.
  1052. */
  1053. static int
  1054. rcu_torture_stats(void *arg)
  1055. {
  1056. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  1057. do {
  1058. schedule_timeout_interruptible(stat_interval * HZ);
  1059. rcu_torture_stats_print();
  1060. rcutorture_shutdown_absorb("rcu_torture_stats");
  1061. } while (!kthread_should_stop());
  1062. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  1063. return 0;
  1064. }
  1065. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  1066. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  1067. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  1068. */
  1069. static void rcu_torture_shuffle_tasks(void)
  1070. {
  1071. int i;
  1072. cpumask_setall(shuffle_tmp_mask);
  1073. get_online_cpus();
  1074. /* No point in shuffling if there is only one online CPU (ex: UP) */
  1075. if (num_online_cpus() == 1) {
  1076. put_online_cpus();
  1077. return;
  1078. }
  1079. if (rcu_idle_cpu != -1)
  1080. cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
  1081. set_cpus_allowed_ptr(current, shuffle_tmp_mask);
  1082. if (reader_tasks) {
  1083. for (i = 0; i < nrealreaders; i++)
  1084. if (reader_tasks[i])
  1085. set_cpus_allowed_ptr(reader_tasks[i],
  1086. shuffle_tmp_mask);
  1087. }
  1088. if (fakewriter_tasks) {
  1089. for (i = 0; i < nfakewriters; i++)
  1090. if (fakewriter_tasks[i])
  1091. set_cpus_allowed_ptr(fakewriter_tasks[i],
  1092. shuffle_tmp_mask);
  1093. }
  1094. if (writer_task)
  1095. set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
  1096. if (stats_task)
  1097. set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
  1098. if (stutter_task)
  1099. set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
  1100. if (fqs_task)
  1101. set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
  1102. if (shutdown_task)
  1103. set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
  1104. #ifdef CONFIG_HOTPLUG_CPU
  1105. if (onoff_task)
  1106. set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
  1107. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  1108. if (stall_task)
  1109. set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
  1110. if (barrier_cbs_tasks)
  1111. for (i = 0; i < n_barrier_cbs; i++)
  1112. if (barrier_cbs_tasks[i])
  1113. set_cpus_allowed_ptr(barrier_cbs_tasks[i],
  1114. shuffle_tmp_mask);
  1115. if (barrier_task)
  1116. set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
  1117. if (rcu_idle_cpu == -1)
  1118. rcu_idle_cpu = num_online_cpus() - 1;
  1119. else
  1120. rcu_idle_cpu--;
  1121. put_online_cpus();
  1122. }
  1123. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  1124. * system to become idle at a time and cut off its timer ticks. This is meant
  1125. * to test the support for such tickless idle CPU in RCU.
  1126. */
  1127. static int
  1128. rcu_torture_shuffle(void *arg)
  1129. {
  1130. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  1131. do {
  1132. schedule_timeout_interruptible(shuffle_interval * HZ);
  1133. rcu_torture_shuffle_tasks();
  1134. rcutorture_shutdown_absorb("rcu_torture_shuffle");
  1135. } while (!kthread_should_stop());
  1136. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  1137. return 0;
  1138. }
  1139. /* Cause the rcutorture test to "stutter", starting and stopping all
  1140. * threads periodically.
  1141. */
  1142. static int
  1143. rcu_torture_stutter(void *arg)
  1144. {
  1145. VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
  1146. do {
  1147. schedule_timeout_interruptible(stutter * HZ);
  1148. stutter_pause_test = 1;
  1149. if (!kthread_should_stop())
  1150. schedule_timeout_interruptible(stutter * HZ);
  1151. stutter_pause_test = 0;
  1152. rcutorture_shutdown_absorb("rcu_torture_stutter");
  1153. } while (!kthread_should_stop());
  1154. VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
  1155. return 0;
  1156. }
  1157. static inline void
  1158. rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
  1159. {
  1160. pr_alert("%s" TORTURE_FLAG
  1161. "--- %s: nreaders=%d nfakewriters=%d "
  1162. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  1163. "shuffle_interval=%d stutter=%d irqreader=%d "
  1164. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
  1165. "test_boost=%d/%d test_boost_interval=%d "
  1166. "test_boost_duration=%d shutdown_secs=%d "
  1167. "stall_cpu=%d stall_cpu_holdoff=%d "
  1168. "n_barrier_cbs=%d "
  1169. "onoff_interval=%d onoff_holdoff=%d\n",
  1170. torture_type, tag, nrealreaders, nfakewriters,
  1171. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  1172. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
  1173. test_boost, cur_ops->can_boost,
  1174. test_boost_interval, test_boost_duration, shutdown_secs,
  1175. stall_cpu, stall_cpu_holdoff,
  1176. n_barrier_cbs,
  1177. onoff_interval, onoff_holdoff);
  1178. }
  1179. static struct notifier_block rcutorture_shutdown_nb = {
  1180. .notifier_call = rcutorture_shutdown_notify,
  1181. };
  1182. static void rcutorture_booster_cleanup(int cpu)
  1183. {
  1184. struct task_struct *t;
  1185. if (boost_tasks[cpu] == NULL)
  1186. return;
  1187. mutex_lock(&boost_mutex);
  1188. VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
  1189. t = boost_tasks[cpu];
  1190. boost_tasks[cpu] = NULL;
  1191. mutex_unlock(&boost_mutex);
  1192. /* This must be outside of the mutex, otherwise deadlock! */
  1193. kthread_stop(t);
  1194. boost_tasks[cpu] = NULL;
  1195. }
  1196. static int rcutorture_booster_init(int cpu)
  1197. {
  1198. int retval;
  1199. if (boost_tasks[cpu] != NULL)
  1200. return 0; /* Already created, nothing more to do. */
  1201. /* Don't allow time recalculation while creating a new task. */
  1202. mutex_lock(&boost_mutex);
  1203. VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
  1204. boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
  1205. cpu_to_node(cpu),
  1206. "rcu_torture_boost");
  1207. if (IS_ERR(boost_tasks[cpu])) {
  1208. retval = PTR_ERR(boost_tasks[cpu]);
  1209. VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
  1210. n_rcu_torture_boost_ktrerror++;
  1211. boost_tasks[cpu] = NULL;
  1212. mutex_unlock(&boost_mutex);
  1213. return retval;
  1214. }
  1215. kthread_bind(boost_tasks[cpu], cpu);
  1216. wake_up_process(boost_tasks[cpu]);
  1217. mutex_unlock(&boost_mutex);
  1218. return 0;
  1219. }
  1220. /*
  1221. * Cause the rcutorture test to shutdown the system after the test has
  1222. * run for the time specified by the shutdown_secs module parameter.
  1223. */
  1224. static int
  1225. rcu_torture_shutdown(void *arg)
  1226. {
  1227. long delta;
  1228. unsigned long jiffies_snap;
  1229. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
  1230. jiffies_snap = ACCESS_ONCE(jiffies);
  1231. while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
  1232. !kthread_should_stop()) {
  1233. delta = shutdown_time - jiffies_snap;
  1234. if (verbose)
  1235. pr_alert("%s" TORTURE_FLAG
  1236. "rcu_torture_shutdown task: %lu jiffies remaining\n",
  1237. torture_type, delta);
  1238. schedule_timeout_interruptible(delta);
  1239. jiffies_snap = ACCESS_ONCE(jiffies);
  1240. }
  1241. if (kthread_should_stop()) {
  1242. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
  1243. return 0;
  1244. }
  1245. /* OK, shut down the system. */
  1246. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
  1247. shutdown_task = NULL; /* Avoid self-kill deadlock. */
  1248. rcu_torture_cleanup(); /* Get the success/failure message. */
  1249. kernel_power_off(); /* Shut down the system. */
  1250. return 0;
  1251. }
  1252. #ifdef CONFIG_HOTPLUG_CPU
  1253. /*
  1254. * Execute random CPU-hotplug operations at the interval specified
  1255. * by the onoff_interval.
  1256. */
  1257. static int
  1258. rcu_torture_onoff(void *arg)
  1259. {
  1260. int cpu;
  1261. unsigned long delta;
  1262. int maxcpu = -1;
  1263. DEFINE_RCU_RANDOM(rand);
  1264. int ret;
  1265. unsigned long starttime;
  1266. VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
  1267. for_each_online_cpu(cpu)
  1268. maxcpu = cpu;
  1269. WARN_ON(maxcpu < 0);
  1270. if (onoff_holdoff > 0) {
  1271. VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
  1272. schedule_timeout_interruptible(onoff_holdoff * HZ);
  1273. VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
  1274. }
  1275. while (!kthread_should_stop()) {
  1276. cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
  1277. if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
  1278. if (verbose)
  1279. pr_alert("%s" TORTURE_FLAG
  1280. "rcu_torture_onoff task: offlining %d\n",
  1281. torture_type, cpu);
  1282. starttime = jiffies;
  1283. n_offline_attempts++;
  1284. ret = cpu_down(cpu);
  1285. if (ret) {
  1286. if (verbose)
  1287. pr_alert("%s" TORTURE_FLAG
  1288. "rcu_torture_onoff task: offline %d failed: errno %d\n",
  1289. torture_type, cpu, ret);
  1290. } else {
  1291. if (verbose)
  1292. pr_alert("%s" TORTURE_FLAG
  1293. "rcu_torture_onoff task: offlined %d\n",
  1294. torture_type, cpu);
  1295. n_offline_successes++;
  1296. delta = jiffies - starttime;
  1297. sum_offline += delta;
  1298. if (min_offline < 0) {
  1299. min_offline = delta;
  1300. max_offline = delta;
  1301. }
  1302. if (min_offline > delta)
  1303. min_offline = delta;
  1304. if (max_offline < delta)
  1305. max_offline = delta;
  1306. }
  1307. } else if (cpu_is_hotpluggable(cpu)) {
  1308. if (verbose)
  1309. pr_alert("%s" TORTURE_FLAG
  1310. "rcu_torture_onoff task: onlining %d\n",
  1311. torture_type, cpu);
  1312. starttime = jiffies;
  1313. n_online_attempts++;
  1314. ret = cpu_up(cpu);
  1315. if (ret) {
  1316. if (verbose)
  1317. pr_alert("%s" TORTURE_FLAG
  1318. "rcu_torture_onoff task: online %d failed: errno %d\n",
  1319. torture_type, cpu, ret);
  1320. } else {
  1321. if (verbose)
  1322. pr_alert("%s" TORTURE_FLAG
  1323. "rcu_torture_onoff task: onlined %d\n",
  1324. torture_type, cpu);
  1325. n_online_successes++;
  1326. delta = jiffies - starttime;
  1327. sum_online += delta;
  1328. if (min_online < 0) {
  1329. min_online = delta;
  1330. max_online = delta;
  1331. }
  1332. if (min_online > delta)
  1333. min_online = delta;
  1334. if (max_online < delta)
  1335. max_online = delta;
  1336. }
  1337. }
  1338. schedule_timeout_interruptible(onoff_interval * HZ);
  1339. }
  1340. VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
  1341. return 0;
  1342. }
  1343. static int
  1344. rcu_torture_onoff_init(void)
  1345. {
  1346. int ret;
  1347. if (onoff_interval <= 0)
  1348. return 0;
  1349. onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
  1350. if (IS_ERR(onoff_task)) {
  1351. ret = PTR_ERR(onoff_task);
  1352. onoff_task = NULL;
  1353. return ret;
  1354. }
  1355. return 0;
  1356. }
  1357. static void rcu_torture_onoff_cleanup(void)
  1358. {
  1359. if (onoff_task == NULL)
  1360. return;
  1361. VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
  1362. kthread_stop(onoff_task);
  1363. onoff_task = NULL;
  1364. }
  1365. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  1366. static int
  1367. rcu_torture_onoff_init(void)
  1368. {
  1369. return 0;
  1370. }
  1371. static void rcu_torture_onoff_cleanup(void)
  1372. {
  1373. }
  1374. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  1375. /*
  1376. * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
  1377. * induces a CPU stall for the time specified by stall_cpu.
  1378. */
  1379. static int rcu_torture_stall(void *args)
  1380. {
  1381. unsigned long stop_at;
  1382. VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
  1383. if (stall_cpu_holdoff > 0) {
  1384. VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
  1385. schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
  1386. VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
  1387. }
  1388. if (!kthread_should_stop()) {
  1389. stop_at = get_seconds() + stall_cpu;
  1390. /* RCU CPU stall is expected behavior in following code. */
  1391. pr_alert("rcu_torture_stall start.\n");
  1392. rcu_read_lock();
  1393. preempt_disable();
  1394. while (ULONG_CMP_LT(get_seconds(), stop_at))
  1395. continue; /* Induce RCU CPU stall warning. */
  1396. preempt_enable();
  1397. rcu_read_unlock();
  1398. pr_alert("rcu_torture_stall end.\n");
  1399. }
  1400. rcutorture_shutdown_absorb("rcu_torture_stall");
  1401. while (!kthread_should_stop())
  1402. schedule_timeout_interruptible(10 * HZ);
  1403. return 0;
  1404. }
  1405. /* Spawn CPU-stall kthread, if stall_cpu specified. */
  1406. static int __init rcu_torture_stall_init(void)
  1407. {
  1408. int ret;
  1409. if (stall_cpu <= 0)
  1410. return 0;
  1411. stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
  1412. if (IS_ERR(stall_task)) {
  1413. ret = PTR_ERR(stall_task);
  1414. stall_task = NULL;
  1415. return ret;
  1416. }
  1417. return 0;
  1418. }
  1419. /* Clean up after the CPU-stall kthread, if one was spawned. */
  1420. static void rcu_torture_stall_cleanup(void)
  1421. {
  1422. if (stall_task == NULL)
  1423. return;
  1424. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
  1425. kthread_stop(stall_task);
  1426. stall_task = NULL;
  1427. }
  1428. /* Callback function for RCU barrier testing. */
  1429. void rcu_torture_barrier_cbf(struct rcu_head *rcu)
  1430. {
  1431. atomic_inc(&barrier_cbs_invoked);
  1432. }
  1433. /* kthread function to register callbacks used to test RCU barriers. */
  1434. static int rcu_torture_barrier_cbs(void *arg)
  1435. {
  1436. long myid = (long)arg;
  1437. bool lastphase = 0;
  1438. struct rcu_head rcu;
  1439. init_rcu_head_on_stack(&rcu);
  1440. VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
  1441. set_user_nice(current, 19);
  1442. do {
  1443. wait_event(barrier_cbs_wq[myid],
  1444. barrier_phase != lastphase ||
  1445. kthread_should_stop() ||
  1446. fullstop != FULLSTOP_DONTSTOP);
  1447. lastphase = barrier_phase;
  1448. smp_mb(); /* ensure barrier_phase load before ->call(). */
  1449. if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
  1450. break;
  1451. cur_ops->call(&rcu, rcu_torture_barrier_cbf);
  1452. if (atomic_dec_and_test(&barrier_cbs_count))
  1453. wake_up(&barrier_wq);
  1454. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  1455. VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
  1456. rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
  1457. while (!kthread_should_stop())
  1458. schedule_timeout_interruptible(1);
  1459. cur_ops->cb_barrier();
  1460. destroy_rcu_head_on_stack(&rcu);
  1461. return 0;
  1462. }
  1463. /* kthread function to drive and coordinate RCU barrier testing. */
  1464. static int rcu_torture_barrier(void *arg)
  1465. {
  1466. int i;
  1467. VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
  1468. do {
  1469. atomic_set(&barrier_cbs_invoked, 0);
  1470. atomic_set(&barrier_cbs_count, n_barrier_cbs);
  1471. smp_mb(); /* Ensure barrier_phase after prior assignments. */
  1472. barrier_phase = !barrier_phase;
  1473. for (i = 0; i < n_barrier_cbs; i++)
  1474. wake_up(&barrier_cbs_wq[i]);
  1475. wait_event(barrier_wq,
  1476. atomic_read(&barrier_cbs_count) == 0 ||
  1477. kthread_should_stop() ||
  1478. fullstop != FULLSTOP_DONTSTOP);
  1479. if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
  1480. break;
  1481. n_barrier_attempts++;
  1482. cur_ops->cb_barrier();
  1483. if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
  1484. n_rcu_torture_barrier_error++;
  1485. WARN_ON_ONCE(1);
  1486. }
  1487. n_barrier_successes++;
  1488. schedule_timeout_interruptible(HZ / 10);
  1489. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  1490. VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
  1491. rcutorture_shutdown_absorb("rcu_torture_barrier");
  1492. while (!kthread_should_stop())
  1493. schedule_timeout_interruptible(1);
  1494. return 0;
  1495. }
  1496. /* Initialize RCU barrier testing. */
  1497. static int rcu_torture_barrier_init(void)
  1498. {
  1499. int i;
  1500. int ret;
  1501. if (n_barrier_cbs == 0)
  1502. return 0;
  1503. if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
  1504. pr_alert("%s" TORTURE_FLAG
  1505. " Call or barrier ops missing for %s,\n",
  1506. torture_type, cur_ops->name);
  1507. pr_alert("%s" TORTURE_FLAG
  1508. " RCU barrier testing omitted from run.\n",
  1509. torture_type);
  1510. return 0;
  1511. }
  1512. atomic_set(&barrier_cbs_count, 0);
  1513. atomic_set(&barrier_cbs_invoked, 0);
  1514. barrier_cbs_tasks =
  1515. kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
  1516. GFP_KERNEL);
  1517. barrier_cbs_wq =
  1518. kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
  1519. GFP_KERNEL);
  1520. if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
  1521. return -ENOMEM;
  1522. for (i = 0; i < n_barrier_cbs; i++) {
  1523. init_waitqueue_head(&barrier_cbs_wq[i]);
  1524. barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
  1525. (void *)(long)i,
  1526. "rcu_torture_barrier_cbs");
  1527. if (IS_ERR(barrier_cbs_tasks[i])) {
  1528. ret = PTR_ERR(barrier_cbs_tasks[i]);
  1529. VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
  1530. barrier_cbs_tasks[i] = NULL;
  1531. return ret;
  1532. }
  1533. }
  1534. barrier_task = kthread_run(rcu_torture_barrier, NULL,
  1535. "rcu_torture_barrier");
  1536. if (IS_ERR(barrier_task)) {
  1537. ret = PTR_ERR(barrier_task);
  1538. VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
  1539. barrier_task = NULL;
  1540. }
  1541. return 0;
  1542. }
  1543. /* Clean up after RCU barrier testing. */
  1544. static void rcu_torture_barrier_cleanup(void)
  1545. {
  1546. int i;
  1547. if (barrier_task != NULL) {
  1548. VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
  1549. kthread_stop(barrier_task);
  1550. barrier_task = NULL;
  1551. }
  1552. if (barrier_cbs_tasks != NULL) {
  1553. for (i = 0; i < n_barrier_cbs; i++) {
  1554. if (barrier_cbs_tasks[i] != NULL) {
  1555. VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
  1556. kthread_stop(barrier_cbs_tasks[i]);
  1557. barrier_cbs_tasks[i] = NULL;
  1558. }
  1559. }
  1560. kfree(barrier_cbs_tasks);
  1561. barrier_cbs_tasks = NULL;
  1562. }
  1563. if (barrier_cbs_wq != NULL) {
  1564. kfree(barrier_cbs_wq);
  1565. barrier_cbs_wq = NULL;
  1566. }
  1567. }
  1568. static int rcutorture_cpu_notify(struct notifier_block *self,
  1569. unsigned long action, void *hcpu)
  1570. {
  1571. long cpu = (long)hcpu;
  1572. switch (action) {
  1573. case CPU_ONLINE:
  1574. case CPU_DOWN_FAILED:
  1575. (void)rcutorture_booster_init(cpu);
  1576. break;
  1577. case CPU_DOWN_PREPARE:
  1578. rcutorture_booster_cleanup(cpu);
  1579. break;
  1580. default:
  1581. break;
  1582. }
  1583. return NOTIFY_OK;
  1584. }
  1585. static struct notifier_block rcutorture_cpu_nb = {
  1586. .notifier_call = rcutorture_cpu_notify,
  1587. };
  1588. static void
  1589. rcu_torture_cleanup(void)
  1590. {
  1591. int i;
  1592. mutex_lock(&fullstop_mutex);
  1593. rcutorture_record_test_transition();
  1594. if (fullstop == FULLSTOP_SHUTDOWN) {
  1595. pr_warn(/* but going down anyway, so... */
  1596. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  1597. mutex_unlock(&fullstop_mutex);
  1598. schedule_timeout_uninterruptible(10);
  1599. if (cur_ops->cb_barrier != NULL)
  1600. cur_ops->cb_barrier();
  1601. return;
  1602. }
  1603. fullstop = FULLSTOP_RMMOD;
  1604. mutex_unlock(&fullstop_mutex);
  1605. unregister_reboot_notifier(&rcutorture_shutdown_nb);
  1606. rcu_torture_barrier_cleanup();
  1607. rcu_torture_stall_cleanup();
  1608. if (stutter_task) {
  1609. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
  1610. kthread_stop(stutter_task);
  1611. }
  1612. stutter_task = NULL;
  1613. if (shuffler_task) {
  1614. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  1615. kthread_stop(shuffler_task);
  1616. free_cpumask_var(shuffle_tmp_mask);
  1617. }
  1618. shuffler_task = NULL;
  1619. if (writer_task) {
  1620. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  1621. kthread_stop(writer_task);
  1622. }
  1623. writer_task = NULL;
  1624. if (reader_tasks) {
  1625. for (i = 0; i < nrealreaders; i++) {
  1626. if (reader_tasks[i]) {
  1627. VERBOSE_PRINTK_STRING(
  1628. "Stopping rcu_torture_reader task");
  1629. kthread_stop(reader_tasks[i]);
  1630. }
  1631. reader_tasks[i] = NULL;
  1632. }
  1633. kfree(reader_tasks);
  1634. reader_tasks = NULL;
  1635. }
  1636. rcu_torture_current = NULL;
  1637. if (fakewriter_tasks) {
  1638. for (i = 0; i < nfakewriters; i++) {
  1639. if (fakewriter_tasks[i]) {
  1640. VERBOSE_PRINTK_STRING(
  1641. "Stopping rcu_torture_fakewriter task");
  1642. kthread_stop(fakewriter_tasks[i]);
  1643. }
  1644. fakewriter_tasks[i] = NULL;
  1645. }
  1646. kfree(fakewriter_tasks);
  1647. fakewriter_tasks = NULL;
  1648. }
  1649. if (stats_task) {
  1650. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  1651. kthread_stop(stats_task);
  1652. }
  1653. stats_task = NULL;
  1654. if (fqs_task) {
  1655. VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
  1656. kthread_stop(fqs_task);
  1657. }
  1658. fqs_task = NULL;
  1659. if ((test_boost == 1 && cur_ops->can_boost) ||
  1660. test_boost == 2) {
  1661. unregister_cpu_notifier(&rcutorture_cpu_nb);
  1662. for_each_possible_cpu(i)
  1663. rcutorture_booster_cleanup(i);
  1664. }
  1665. if (shutdown_task != NULL) {
  1666. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
  1667. kthread_stop(shutdown_task);
  1668. }
  1669. shutdown_task = NULL;
  1670. rcu_torture_onoff_cleanup();
  1671. /* Wait for all RCU callbacks to fire. */
  1672. if (cur_ops->cb_barrier != NULL)
  1673. cur_ops->cb_barrier();
  1674. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  1675. if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
  1676. rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
  1677. else if (n_online_successes != n_online_attempts ||
  1678. n_offline_successes != n_offline_attempts)
  1679. rcu_torture_print_module_parms(cur_ops,
  1680. "End of test: RCU_HOTPLUG");
  1681. else
  1682. rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
  1683. }
  1684. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  1685. static void rcu_torture_leak_cb(struct rcu_head *rhp)
  1686. {
  1687. }
  1688. static void rcu_torture_err_cb(struct rcu_head *rhp)
  1689. {
  1690. /*
  1691. * This -might- happen due to race conditions, but is unlikely.
  1692. * The scenario that leads to this happening is that the
  1693. * first of the pair of duplicate callbacks is queued,
  1694. * someone else starts a grace period that includes that
  1695. * callback, then the second of the pair must wait for the
  1696. * next grace period. Unlikely, but can happen. If it
  1697. * does happen, the debug-objects subsystem won't have splatted.
  1698. */
  1699. pr_alert("rcutorture: duplicated callback was invoked.\n");
  1700. }
  1701. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1702. /*
  1703. * Verify that double-free causes debug-objects to complain, but only
  1704. * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
  1705. * cannot be carried out.
  1706. */
  1707. static void rcu_test_debug_objects(void)
  1708. {
  1709. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  1710. struct rcu_head rh1;
  1711. struct rcu_head rh2;
  1712. init_rcu_head_on_stack(&rh1);
  1713. init_rcu_head_on_stack(&rh2);
  1714. pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
  1715. /* Try to queue the rh2 pair of callbacks for the same grace period. */
  1716. preempt_disable(); /* Prevent preemption from interrupting test. */
  1717. rcu_read_lock(); /* Make it impossible to finish a grace period. */
  1718. call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
  1719. local_irq_disable(); /* Make it harder to start a new grace period. */
  1720. call_rcu(&rh2, rcu_torture_leak_cb);
  1721. call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
  1722. local_irq_enable();
  1723. rcu_read_unlock();
  1724. preempt_enable();
  1725. /* Wait for them all to get done so we can safely return. */
  1726. rcu_barrier();
  1727. pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
  1728. destroy_rcu_head_on_stack(&rh1);
  1729. destroy_rcu_head_on_stack(&rh2);
  1730. #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1731. pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
  1732. #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1733. }
  1734. static int __init
  1735. rcu_torture_init(void)
  1736. {
  1737. int i;
  1738. int cpu;
  1739. int firsterr = 0;
  1740. int retval;
  1741. static struct rcu_torture_ops *torture_ops[] = {
  1742. &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
  1743. };
  1744. mutex_lock(&fullstop_mutex);
  1745. /* Process args and tell the world that the torturer is on the job. */
  1746. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1747. cur_ops = torture_ops[i];
  1748. if (strcmp(torture_type, cur_ops->name) == 0)
  1749. break;
  1750. }
  1751. if (i == ARRAY_SIZE(torture_ops)) {
  1752. pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
  1753. torture_type);
  1754. pr_alert("rcu-torture types:");
  1755. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1756. pr_alert(" %s", torture_ops[i]->name);
  1757. pr_alert("\n");
  1758. mutex_unlock(&fullstop_mutex);
  1759. return -EINVAL;
  1760. }
  1761. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  1762. pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
  1763. fqs_duration = 0;
  1764. }
  1765. if (cur_ops->init)
  1766. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  1767. if (nreaders >= 0)
  1768. nrealreaders = nreaders;
  1769. else
  1770. nrealreaders = 2 * num_online_cpus();
  1771. rcu_torture_print_module_parms(cur_ops, "Start of test");
  1772. fullstop = FULLSTOP_DONTSTOP;
  1773. /* Set up the freelist. */
  1774. INIT_LIST_HEAD(&rcu_torture_freelist);
  1775. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  1776. rcu_tortures[i].rtort_mbtest = 0;
  1777. list_add_tail(&rcu_tortures[i].rtort_free,
  1778. &rcu_torture_freelist);
  1779. }
  1780. /* Initialize the statistics so that each run gets its own numbers. */
  1781. rcu_torture_current = NULL;
  1782. rcu_torture_current_version = 0;
  1783. atomic_set(&n_rcu_torture_alloc, 0);
  1784. atomic_set(&n_rcu_torture_alloc_fail, 0);
  1785. atomic_set(&n_rcu_torture_free, 0);
  1786. atomic_set(&n_rcu_torture_mberror, 0);
  1787. atomic_set(&n_rcu_torture_error, 0);
  1788. n_rcu_torture_barrier_error = 0;
  1789. n_rcu_torture_boost_ktrerror = 0;
  1790. n_rcu_torture_boost_rterror = 0;
  1791. n_rcu_torture_boost_failure = 0;
  1792. n_rcu_torture_boosts = 0;
  1793. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1794. atomic_set(&rcu_torture_wcount[i], 0);
  1795. for_each_possible_cpu(cpu) {
  1796. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1797. per_cpu(rcu_torture_count, cpu)[i] = 0;
  1798. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  1799. }
  1800. }
  1801. /* Start up the kthreads. */
  1802. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  1803. writer_task = kthread_create(rcu_torture_writer, NULL,
  1804. "rcu_torture_writer");
  1805. if (IS_ERR(writer_task)) {
  1806. firsterr = PTR_ERR(writer_task);
  1807. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  1808. writer_task = NULL;
  1809. goto unwind;
  1810. }
  1811. wake_up_process(writer_task);
  1812. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  1813. GFP_KERNEL);
  1814. if (fakewriter_tasks == NULL) {
  1815. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1816. firsterr = -ENOMEM;
  1817. goto unwind;
  1818. }
  1819. for (i = 0; i < nfakewriters; i++) {
  1820. VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
  1821. fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
  1822. "rcu_torture_fakewriter");
  1823. if (IS_ERR(fakewriter_tasks[i])) {
  1824. firsterr = PTR_ERR(fakewriter_tasks[i]);
  1825. VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
  1826. fakewriter_tasks[i] = NULL;
  1827. goto unwind;
  1828. }
  1829. }
  1830. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  1831. GFP_KERNEL);
  1832. if (reader_tasks == NULL) {
  1833. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1834. firsterr = -ENOMEM;
  1835. goto unwind;
  1836. }
  1837. for (i = 0; i < nrealreaders; i++) {
  1838. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  1839. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  1840. "rcu_torture_reader");
  1841. if (IS_ERR(reader_tasks[i])) {
  1842. firsterr = PTR_ERR(reader_tasks[i]);
  1843. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  1844. reader_tasks[i] = NULL;
  1845. goto unwind;
  1846. }
  1847. }
  1848. if (stat_interval > 0) {
  1849. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  1850. stats_task = kthread_run(rcu_torture_stats, NULL,
  1851. "rcu_torture_stats");
  1852. if (IS_ERR(stats_task)) {
  1853. firsterr = PTR_ERR(stats_task);
  1854. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  1855. stats_task = NULL;
  1856. goto unwind;
  1857. }
  1858. }
  1859. if (test_no_idle_hz) {
  1860. rcu_idle_cpu = num_online_cpus() - 1;
  1861. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  1862. firsterr = -ENOMEM;
  1863. VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
  1864. goto unwind;
  1865. }
  1866. /* Create the shuffler thread */
  1867. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  1868. "rcu_torture_shuffle");
  1869. if (IS_ERR(shuffler_task)) {
  1870. free_cpumask_var(shuffle_tmp_mask);
  1871. firsterr = PTR_ERR(shuffler_task);
  1872. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  1873. shuffler_task = NULL;
  1874. goto unwind;
  1875. }
  1876. }
  1877. if (stutter < 0)
  1878. stutter = 0;
  1879. if (stutter) {
  1880. /* Create the stutter thread */
  1881. stutter_task = kthread_run(rcu_torture_stutter, NULL,
  1882. "rcu_torture_stutter");
  1883. if (IS_ERR(stutter_task)) {
  1884. firsterr = PTR_ERR(stutter_task);
  1885. VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
  1886. stutter_task = NULL;
  1887. goto unwind;
  1888. }
  1889. }
  1890. if (fqs_duration < 0)
  1891. fqs_duration = 0;
  1892. if (fqs_duration) {
  1893. /* Create the stutter thread */
  1894. fqs_task = kthread_run(rcu_torture_fqs, NULL,
  1895. "rcu_torture_fqs");
  1896. if (IS_ERR(fqs_task)) {
  1897. firsterr = PTR_ERR(fqs_task);
  1898. VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
  1899. fqs_task = NULL;
  1900. goto unwind;
  1901. }
  1902. }
  1903. if (test_boost_interval < 1)
  1904. test_boost_interval = 1;
  1905. if (test_boost_duration < 2)
  1906. test_boost_duration = 2;
  1907. if ((test_boost == 1 && cur_ops->can_boost) ||
  1908. test_boost == 2) {
  1909. boost_starttime = jiffies + test_boost_interval * HZ;
  1910. register_cpu_notifier(&rcutorture_cpu_nb);
  1911. for_each_possible_cpu(i) {
  1912. if (cpu_is_offline(i))
  1913. continue; /* Heuristic: CPU can go offline. */
  1914. retval = rcutorture_booster_init(i);
  1915. if (retval < 0) {
  1916. firsterr = retval;
  1917. goto unwind;
  1918. }
  1919. }
  1920. }
  1921. if (shutdown_secs > 0) {
  1922. shutdown_time = jiffies + shutdown_secs * HZ;
  1923. shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
  1924. "rcu_torture_shutdown");
  1925. if (IS_ERR(shutdown_task)) {
  1926. firsterr = PTR_ERR(shutdown_task);
  1927. VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
  1928. shutdown_task = NULL;
  1929. goto unwind;
  1930. }
  1931. wake_up_process(shutdown_task);
  1932. }
  1933. i = rcu_torture_onoff_init();
  1934. if (i != 0) {
  1935. firsterr = i;
  1936. goto unwind;
  1937. }
  1938. register_reboot_notifier(&rcutorture_shutdown_nb);
  1939. i = rcu_torture_stall_init();
  1940. if (i != 0) {
  1941. firsterr = i;
  1942. goto unwind;
  1943. }
  1944. retval = rcu_torture_barrier_init();
  1945. if (retval != 0) {
  1946. firsterr = retval;
  1947. goto unwind;
  1948. }
  1949. if (object_debug)
  1950. rcu_test_debug_objects();
  1951. rcutorture_record_test_transition();
  1952. mutex_unlock(&fullstop_mutex);
  1953. return 0;
  1954. unwind:
  1955. mutex_unlock(&fullstop_mutex);
  1956. rcu_torture_cleanup();
  1957. return firsterr;
  1958. }
  1959. module_init(rcu_torture_init);
  1960. module_exit(rcu_torture_cleanup);