rcutorture.c 61 KB

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