rcutorture.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  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. long oldbatch = rcu_batches_completed();
  717. struct rcu_torture *rp;
  718. struct rcu_torture *rp1;
  719. struct rcu_torture *old_rp;
  720. static DEFINE_RCU_RANDOM(rand);
  721. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  722. set_user_nice(current, 19);
  723. do {
  724. schedule_timeout_uninterruptible(1);
  725. rp = rcu_torture_alloc();
  726. if (rp == NULL)
  727. continue;
  728. rp->rtort_pipe_count = 0;
  729. udelay(rcu_random(&rand) & 0x3ff);
  730. old_rp = rcu_dereference_check(rcu_torture_current,
  731. current == writer_task);
  732. rp->rtort_mbtest = 1;
  733. rcu_assign_pointer(rcu_torture_current, rp);
  734. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  735. if (old_rp) {
  736. i = old_rp->rtort_pipe_count;
  737. if (i > RCU_TORTURE_PIPE_LEN)
  738. i = RCU_TORTURE_PIPE_LEN;
  739. atomic_inc(&rcu_torture_wcount[i]);
  740. old_rp->rtort_pipe_count++;
  741. if (gp_normal == gp_exp)
  742. exp = !!(rcu_random(&rand) & 0x80);
  743. else
  744. exp = gp_exp;
  745. if (!exp) {
  746. cur_ops->deferred_free(old_rp);
  747. } else {
  748. cur_ops->exp_sync();
  749. list_add(&old_rp->rtort_free,
  750. &rcu_torture_removed);
  751. list_for_each_entry_safe(rp, rp1,
  752. &rcu_torture_removed,
  753. rtort_free) {
  754. i = rp->rtort_pipe_count;
  755. if (i > RCU_TORTURE_PIPE_LEN)
  756. i = RCU_TORTURE_PIPE_LEN;
  757. atomic_inc(&rcu_torture_wcount[i]);
  758. if (++rp->rtort_pipe_count >=
  759. RCU_TORTURE_PIPE_LEN) {
  760. rp->rtort_mbtest = 0;
  761. list_del(&rp->rtort_free);
  762. rcu_torture_free(rp);
  763. }
  764. }
  765. }
  766. }
  767. rcutorture_record_progress(++rcu_torture_current_version);
  768. oldbatch = cur_ops->completed();
  769. rcu_stutter_wait("rcu_torture_writer");
  770. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  771. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  772. rcutorture_shutdown_absorb("rcu_torture_writer");
  773. while (!kthread_should_stop())
  774. schedule_timeout_uninterruptible(1);
  775. return 0;
  776. }
  777. /*
  778. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  779. * delay between calls.
  780. */
  781. static int
  782. rcu_torture_fakewriter(void *arg)
  783. {
  784. DEFINE_RCU_RANDOM(rand);
  785. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
  786. set_user_nice(current, 19);
  787. do {
  788. schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
  789. udelay(rcu_random(&rand) & 0x3ff);
  790. if (cur_ops->cb_barrier != NULL &&
  791. rcu_random(&rand) % (nfakewriters * 8) == 0) {
  792. cur_ops->cb_barrier();
  793. } else if (gp_normal == gp_exp) {
  794. if (rcu_random(&rand) & 0x80)
  795. cur_ops->sync();
  796. else
  797. cur_ops->exp_sync();
  798. } else if (gp_normal) {
  799. cur_ops->sync();
  800. } else {
  801. cur_ops->exp_sync();
  802. }
  803. rcu_stutter_wait("rcu_torture_fakewriter");
  804. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  805. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
  806. rcutorture_shutdown_absorb("rcu_torture_fakewriter");
  807. while (!kthread_should_stop())
  808. schedule_timeout_uninterruptible(1);
  809. return 0;
  810. }
  811. void rcutorture_trace_dump(void)
  812. {
  813. static atomic_t beenhere = ATOMIC_INIT(0);
  814. if (atomic_read(&beenhere))
  815. return;
  816. if (atomic_xchg(&beenhere, 1) != 0)
  817. return;
  818. ftrace_dump(DUMP_ALL);
  819. }
  820. /*
  821. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  822. * incrementing the corresponding element of the pipeline array. The
  823. * counter in the element should never be greater than 1, otherwise, the
  824. * RCU implementation is broken.
  825. */
  826. static void rcu_torture_timer(unsigned long unused)
  827. {
  828. int idx;
  829. int completed;
  830. int completed_end;
  831. static DEFINE_RCU_RANDOM(rand);
  832. static DEFINE_SPINLOCK(rand_lock);
  833. struct rcu_torture *p;
  834. int pipe_count;
  835. unsigned long long ts;
  836. idx = cur_ops->readlock();
  837. completed = cur_ops->completed();
  838. ts = rcu_trace_clock_local();
  839. p = rcu_dereference_check(rcu_torture_current,
  840. rcu_read_lock_bh_held() ||
  841. rcu_read_lock_sched_held() ||
  842. srcu_read_lock_held(&srcu_ctl));
  843. if (p == NULL) {
  844. /* Leave because rcu_torture_writer is not yet underway */
  845. cur_ops->readunlock(idx);
  846. return;
  847. }
  848. if (p->rtort_mbtest == 0)
  849. atomic_inc(&n_rcu_torture_mberror);
  850. spin_lock(&rand_lock);
  851. cur_ops->read_delay(&rand);
  852. n_rcu_torture_timers++;
  853. spin_unlock(&rand_lock);
  854. preempt_disable();
  855. pipe_count = p->rtort_pipe_count;
  856. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  857. /* Should not happen, but... */
  858. pipe_count = RCU_TORTURE_PIPE_LEN;
  859. }
  860. completed_end = cur_ops->completed();
  861. if (pipe_count > 1) {
  862. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
  863. completed, completed_end);
  864. rcutorture_trace_dump();
  865. }
  866. __this_cpu_inc(rcu_torture_count[pipe_count]);
  867. completed = completed_end - completed;
  868. if (completed > RCU_TORTURE_PIPE_LEN) {
  869. /* Should not happen, but... */
  870. completed = RCU_TORTURE_PIPE_LEN;
  871. }
  872. __this_cpu_inc(rcu_torture_batch[completed]);
  873. preempt_enable();
  874. cur_ops->readunlock(idx);
  875. }
  876. /*
  877. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  878. * incrementing the corresponding element of the pipeline array. The
  879. * counter in the element should never be greater than 1, otherwise, the
  880. * RCU implementation is broken.
  881. */
  882. static int
  883. rcu_torture_reader(void *arg)
  884. {
  885. int completed;
  886. int completed_end;
  887. int idx;
  888. DEFINE_RCU_RANDOM(rand);
  889. struct rcu_torture *p;
  890. int pipe_count;
  891. struct timer_list t;
  892. unsigned long long ts;
  893. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  894. set_user_nice(current, 19);
  895. if (irqreader && cur_ops->irq_capable)
  896. setup_timer_on_stack(&t, rcu_torture_timer, 0);
  897. do {
  898. if (irqreader && cur_ops->irq_capable) {
  899. if (!timer_pending(&t))
  900. mod_timer(&t, jiffies + 1);
  901. }
  902. idx = cur_ops->readlock();
  903. completed = cur_ops->completed();
  904. ts = rcu_trace_clock_local();
  905. p = rcu_dereference_check(rcu_torture_current,
  906. rcu_read_lock_bh_held() ||
  907. rcu_read_lock_sched_held() ||
  908. srcu_read_lock_held(&srcu_ctl));
  909. if (p == NULL) {
  910. /* Wait for rcu_torture_writer to get underway */
  911. cur_ops->readunlock(idx);
  912. schedule_timeout_interruptible(HZ);
  913. continue;
  914. }
  915. if (p->rtort_mbtest == 0)
  916. atomic_inc(&n_rcu_torture_mberror);
  917. cur_ops->read_delay(&rand);
  918. preempt_disable();
  919. pipe_count = p->rtort_pipe_count;
  920. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  921. /* Should not happen, but... */
  922. pipe_count = RCU_TORTURE_PIPE_LEN;
  923. }
  924. completed_end = cur_ops->completed();
  925. if (pipe_count > 1) {
  926. do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
  927. ts, completed, completed_end);
  928. rcutorture_trace_dump();
  929. }
  930. __this_cpu_inc(rcu_torture_count[pipe_count]);
  931. completed = completed_end - completed;
  932. if (completed > RCU_TORTURE_PIPE_LEN) {
  933. /* Should not happen, but... */
  934. completed = RCU_TORTURE_PIPE_LEN;
  935. }
  936. __this_cpu_inc(rcu_torture_batch[completed]);
  937. preempt_enable();
  938. cur_ops->readunlock(idx);
  939. schedule();
  940. rcu_stutter_wait("rcu_torture_reader");
  941. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  942. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  943. rcutorture_shutdown_absorb("rcu_torture_reader");
  944. if (irqreader && cur_ops->irq_capable)
  945. del_timer_sync(&t);
  946. while (!kthread_should_stop())
  947. schedule_timeout_uninterruptible(1);
  948. return 0;
  949. }
  950. /*
  951. * Create an RCU-torture statistics message in the specified buffer.
  952. */
  953. static int
  954. rcu_torture_printk(char *page)
  955. {
  956. int cnt = 0;
  957. int cpu;
  958. int i;
  959. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  960. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  961. for_each_possible_cpu(cpu) {
  962. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  963. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  964. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  965. }
  966. }
  967. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  968. if (pipesummary[i] != 0)
  969. break;
  970. }
  971. cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
  972. cnt += sprintf(&page[cnt],
  973. "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
  974. rcu_torture_current,
  975. rcu_torture_current_version,
  976. list_empty(&rcu_torture_freelist),
  977. atomic_read(&n_rcu_torture_alloc),
  978. atomic_read(&n_rcu_torture_alloc_fail),
  979. atomic_read(&n_rcu_torture_free));
  980. cnt += sprintf(&page[cnt], "rtmbe: %d rtbke: %ld rtbre: %ld ",
  981. atomic_read(&n_rcu_torture_mberror),
  982. n_rcu_torture_boost_ktrerror,
  983. n_rcu_torture_boost_rterror);
  984. cnt += sprintf(&page[cnt], "rtbf: %ld rtb: %ld nt: %ld ",
  985. n_rcu_torture_boost_failure,
  986. n_rcu_torture_boosts,
  987. n_rcu_torture_timers);
  988. cnt += sprintf(&page[cnt],
  989. "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
  990. n_online_successes, n_online_attempts,
  991. n_offline_successes, n_offline_attempts,
  992. min_online, max_online,
  993. min_offline, max_offline,
  994. sum_online, sum_offline, HZ);
  995. cnt += sprintf(&page[cnt], "barrier: %ld/%ld:%ld",
  996. n_barrier_successes,
  997. n_barrier_attempts,
  998. n_rcu_torture_barrier_error);
  999. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1000. if (atomic_read(&n_rcu_torture_mberror) != 0 ||
  1001. n_rcu_torture_barrier_error != 0 ||
  1002. n_rcu_torture_boost_ktrerror != 0 ||
  1003. n_rcu_torture_boost_rterror != 0 ||
  1004. n_rcu_torture_boost_failure != 0 ||
  1005. i > 1) {
  1006. cnt += sprintf(&page[cnt], "!!! ");
  1007. atomic_inc(&n_rcu_torture_error);
  1008. WARN_ON_ONCE(1);
  1009. }
  1010. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  1011. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1012. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  1013. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1014. cnt += sprintf(&page[cnt], "Reader Batch: ");
  1015. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1016. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  1017. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  1018. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  1019. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1020. cnt += sprintf(&page[cnt], " %d",
  1021. atomic_read(&rcu_torture_wcount[i]));
  1022. }
  1023. cnt += sprintf(&page[cnt], "\n");
  1024. if (cur_ops->stats)
  1025. cnt += cur_ops->stats(&page[cnt]);
  1026. return cnt;
  1027. }
  1028. /*
  1029. * Print torture statistics. Caller must ensure that there is only
  1030. * one call to this function at a given time!!! This is normally
  1031. * accomplished by relying on the module system to only have one copy
  1032. * of the module loaded, and then by giving the rcu_torture_stats
  1033. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  1034. * thread is not running).
  1035. */
  1036. static void
  1037. rcu_torture_stats_print(void)
  1038. {
  1039. int cnt;
  1040. cnt = rcu_torture_printk(printk_buf);
  1041. pr_alert("%s", printk_buf);
  1042. }
  1043. /*
  1044. * Periodically prints torture statistics, if periodic statistics printing
  1045. * was specified via the stat_interval module parameter.
  1046. *
  1047. * No need to worry about fullstop here, since this one doesn't reference
  1048. * volatile state or register callbacks.
  1049. */
  1050. static int
  1051. rcu_torture_stats(void *arg)
  1052. {
  1053. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  1054. do {
  1055. schedule_timeout_interruptible(stat_interval * HZ);
  1056. rcu_torture_stats_print();
  1057. rcutorture_shutdown_absorb("rcu_torture_stats");
  1058. } while (!kthread_should_stop());
  1059. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  1060. return 0;
  1061. }
  1062. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  1063. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  1064. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  1065. */
  1066. static void rcu_torture_shuffle_tasks(void)
  1067. {
  1068. int i;
  1069. cpumask_setall(shuffle_tmp_mask);
  1070. get_online_cpus();
  1071. /* No point in shuffling if there is only one online CPU (ex: UP) */
  1072. if (num_online_cpus() == 1) {
  1073. put_online_cpus();
  1074. return;
  1075. }
  1076. if (rcu_idle_cpu != -1)
  1077. cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
  1078. set_cpus_allowed_ptr(current, shuffle_tmp_mask);
  1079. if (reader_tasks) {
  1080. for (i = 0; i < nrealreaders; i++)
  1081. if (reader_tasks[i])
  1082. set_cpus_allowed_ptr(reader_tasks[i],
  1083. shuffle_tmp_mask);
  1084. }
  1085. if (fakewriter_tasks) {
  1086. for (i = 0; i < nfakewriters; i++)
  1087. if (fakewriter_tasks[i])
  1088. set_cpus_allowed_ptr(fakewriter_tasks[i],
  1089. shuffle_tmp_mask);
  1090. }
  1091. if (writer_task)
  1092. set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
  1093. if (stats_task)
  1094. set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
  1095. if (stutter_task)
  1096. set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
  1097. if (fqs_task)
  1098. set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
  1099. if (shutdown_task)
  1100. set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
  1101. #ifdef CONFIG_HOTPLUG_CPU
  1102. if (onoff_task)
  1103. set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
  1104. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  1105. if (stall_task)
  1106. set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
  1107. if (barrier_cbs_tasks)
  1108. for (i = 0; i < n_barrier_cbs; i++)
  1109. if (barrier_cbs_tasks[i])
  1110. set_cpus_allowed_ptr(barrier_cbs_tasks[i],
  1111. shuffle_tmp_mask);
  1112. if (barrier_task)
  1113. set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
  1114. if (rcu_idle_cpu == -1)
  1115. rcu_idle_cpu = num_online_cpus() - 1;
  1116. else
  1117. rcu_idle_cpu--;
  1118. put_online_cpus();
  1119. }
  1120. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  1121. * system to become idle at a time and cut off its timer ticks. This is meant
  1122. * to test the support for such tickless idle CPU in RCU.
  1123. */
  1124. static int
  1125. rcu_torture_shuffle(void *arg)
  1126. {
  1127. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  1128. do {
  1129. schedule_timeout_interruptible(shuffle_interval * HZ);
  1130. rcu_torture_shuffle_tasks();
  1131. rcutorture_shutdown_absorb("rcu_torture_shuffle");
  1132. } while (!kthread_should_stop());
  1133. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  1134. return 0;
  1135. }
  1136. /* Cause the rcutorture test to "stutter", starting and stopping all
  1137. * threads periodically.
  1138. */
  1139. static int
  1140. rcu_torture_stutter(void *arg)
  1141. {
  1142. VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
  1143. do {
  1144. schedule_timeout_interruptible(stutter * HZ);
  1145. stutter_pause_test = 1;
  1146. if (!kthread_should_stop())
  1147. schedule_timeout_interruptible(stutter * HZ);
  1148. stutter_pause_test = 0;
  1149. rcutorture_shutdown_absorb("rcu_torture_stutter");
  1150. } while (!kthread_should_stop());
  1151. VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
  1152. return 0;
  1153. }
  1154. static inline void
  1155. rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
  1156. {
  1157. pr_alert("%s" TORTURE_FLAG
  1158. "--- %s: nreaders=%d nfakewriters=%d "
  1159. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  1160. "shuffle_interval=%d stutter=%d irqreader=%d "
  1161. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
  1162. "test_boost=%d/%d test_boost_interval=%d "
  1163. "test_boost_duration=%d shutdown_secs=%d "
  1164. "stall_cpu=%d stall_cpu_holdoff=%d "
  1165. "n_barrier_cbs=%d "
  1166. "onoff_interval=%d onoff_holdoff=%d\n",
  1167. torture_type, tag, nrealreaders, nfakewriters,
  1168. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  1169. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
  1170. test_boost, cur_ops->can_boost,
  1171. test_boost_interval, test_boost_duration, shutdown_secs,
  1172. stall_cpu, stall_cpu_holdoff,
  1173. n_barrier_cbs,
  1174. onoff_interval, onoff_holdoff);
  1175. }
  1176. static struct notifier_block rcutorture_shutdown_nb = {
  1177. .notifier_call = rcutorture_shutdown_notify,
  1178. };
  1179. static void rcutorture_booster_cleanup(int cpu)
  1180. {
  1181. struct task_struct *t;
  1182. if (boost_tasks[cpu] == NULL)
  1183. return;
  1184. mutex_lock(&boost_mutex);
  1185. VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
  1186. t = boost_tasks[cpu];
  1187. boost_tasks[cpu] = NULL;
  1188. mutex_unlock(&boost_mutex);
  1189. /* This must be outside of the mutex, otherwise deadlock! */
  1190. kthread_stop(t);
  1191. boost_tasks[cpu] = NULL;
  1192. }
  1193. static int rcutorture_booster_init(int cpu)
  1194. {
  1195. int retval;
  1196. if (boost_tasks[cpu] != NULL)
  1197. return 0; /* Already created, nothing more to do. */
  1198. /* Don't allow time recalculation while creating a new task. */
  1199. mutex_lock(&boost_mutex);
  1200. VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
  1201. boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
  1202. cpu_to_node(cpu),
  1203. "rcu_torture_boost");
  1204. if (IS_ERR(boost_tasks[cpu])) {
  1205. retval = PTR_ERR(boost_tasks[cpu]);
  1206. VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
  1207. n_rcu_torture_boost_ktrerror++;
  1208. boost_tasks[cpu] = NULL;
  1209. mutex_unlock(&boost_mutex);
  1210. return retval;
  1211. }
  1212. kthread_bind(boost_tasks[cpu], cpu);
  1213. wake_up_process(boost_tasks[cpu]);
  1214. mutex_unlock(&boost_mutex);
  1215. return 0;
  1216. }
  1217. /*
  1218. * Cause the rcutorture test to shutdown the system after the test has
  1219. * run for the time specified by the shutdown_secs module parameter.
  1220. */
  1221. static int
  1222. rcu_torture_shutdown(void *arg)
  1223. {
  1224. long delta;
  1225. unsigned long jiffies_snap;
  1226. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
  1227. jiffies_snap = ACCESS_ONCE(jiffies);
  1228. while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
  1229. !kthread_should_stop()) {
  1230. delta = shutdown_time - jiffies_snap;
  1231. if (verbose)
  1232. pr_alert("%s" TORTURE_FLAG
  1233. "rcu_torture_shutdown task: %lu jiffies remaining\n",
  1234. torture_type, delta);
  1235. schedule_timeout_interruptible(delta);
  1236. jiffies_snap = ACCESS_ONCE(jiffies);
  1237. }
  1238. if (kthread_should_stop()) {
  1239. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
  1240. return 0;
  1241. }
  1242. /* OK, shut down the system. */
  1243. VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
  1244. shutdown_task = NULL; /* Avoid self-kill deadlock. */
  1245. rcu_torture_cleanup(); /* Get the success/failure message. */
  1246. kernel_power_off(); /* Shut down the system. */
  1247. return 0;
  1248. }
  1249. #ifdef CONFIG_HOTPLUG_CPU
  1250. /*
  1251. * Execute random CPU-hotplug operations at the interval specified
  1252. * by the onoff_interval.
  1253. */
  1254. static int
  1255. rcu_torture_onoff(void *arg)
  1256. {
  1257. int cpu;
  1258. unsigned long delta;
  1259. int maxcpu = -1;
  1260. DEFINE_RCU_RANDOM(rand);
  1261. int ret;
  1262. unsigned long starttime;
  1263. VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
  1264. for_each_online_cpu(cpu)
  1265. maxcpu = cpu;
  1266. WARN_ON(maxcpu < 0);
  1267. if (onoff_holdoff > 0) {
  1268. VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
  1269. schedule_timeout_interruptible(onoff_holdoff * HZ);
  1270. VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
  1271. }
  1272. while (!kthread_should_stop()) {
  1273. cpu = (rcu_random(&rand) >> 4) % (maxcpu + 1);
  1274. if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
  1275. if (verbose)
  1276. pr_alert("%s" TORTURE_FLAG
  1277. "rcu_torture_onoff task: offlining %d\n",
  1278. torture_type, cpu);
  1279. starttime = jiffies;
  1280. n_offline_attempts++;
  1281. ret = cpu_down(cpu);
  1282. if (ret) {
  1283. if (verbose)
  1284. pr_alert("%s" TORTURE_FLAG
  1285. "rcu_torture_onoff task: offline %d failed: errno %d\n",
  1286. torture_type, cpu, ret);
  1287. } else {
  1288. if (verbose)
  1289. pr_alert("%s" TORTURE_FLAG
  1290. "rcu_torture_onoff task: offlined %d\n",
  1291. torture_type, cpu);
  1292. n_offline_successes++;
  1293. delta = jiffies - starttime;
  1294. sum_offline += delta;
  1295. if (min_offline < 0) {
  1296. min_offline = delta;
  1297. max_offline = delta;
  1298. }
  1299. if (min_offline > delta)
  1300. min_offline = delta;
  1301. if (max_offline < delta)
  1302. max_offline = delta;
  1303. }
  1304. } else if (cpu_is_hotpluggable(cpu)) {
  1305. if (verbose)
  1306. pr_alert("%s" TORTURE_FLAG
  1307. "rcu_torture_onoff task: onlining %d\n",
  1308. torture_type, cpu);
  1309. starttime = jiffies;
  1310. n_online_attempts++;
  1311. if (cpu_up(cpu) == 0) {
  1312. if (verbose)
  1313. pr_alert("%s" TORTURE_FLAG
  1314. "rcu_torture_onoff task: onlined %d\n",
  1315. torture_type, cpu);
  1316. n_online_successes++;
  1317. delta = jiffies - starttime;
  1318. sum_online += delta;
  1319. if (min_online < 0) {
  1320. min_online = delta;
  1321. max_online = delta;
  1322. }
  1323. if (min_online > delta)
  1324. min_online = delta;
  1325. if (max_online < delta)
  1326. max_online = delta;
  1327. }
  1328. }
  1329. schedule_timeout_interruptible(onoff_interval * HZ);
  1330. }
  1331. VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
  1332. return 0;
  1333. }
  1334. static int
  1335. rcu_torture_onoff_init(void)
  1336. {
  1337. int ret;
  1338. if (onoff_interval <= 0)
  1339. return 0;
  1340. onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
  1341. if (IS_ERR(onoff_task)) {
  1342. ret = PTR_ERR(onoff_task);
  1343. onoff_task = NULL;
  1344. return ret;
  1345. }
  1346. return 0;
  1347. }
  1348. static void rcu_torture_onoff_cleanup(void)
  1349. {
  1350. if (onoff_task == NULL)
  1351. return;
  1352. VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
  1353. kthread_stop(onoff_task);
  1354. onoff_task = NULL;
  1355. }
  1356. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  1357. static int
  1358. rcu_torture_onoff_init(void)
  1359. {
  1360. return 0;
  1361. }
  1362. static void rcu_torture_onoff_cleanup(void)
  1363. {
  1364. }
  1365. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  1366. /*
  1367. * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
  1368. * induces a CPU stall for the time specified by stall_cpu.
  1369. */
  1370. static int rcu_torture_stall(void *args)
  1371. {
  1372. unsigned long stop_at;
  1373. VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
  1374. if (stall_cpu_holdoff > 0) {
  1375. VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
  1376. schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
  1377. VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
  1378. }
  1379. if (!kthread_should_stop()) {
  1380. stop_at = get_seconds() + stall_cpu;
  1381. /* RCU CPU stall is expected behavior in following code. */
  1382. pr_alert("rcu_torture_stall start.\n");
  1383. rcu_read_lock();
  1384. preempt_disable();
  1385. while (ULONG_CMP_LT(get_seconds(), stop_at))
  1386. continue; /* Induce RCU CPU stall warning. */
  1387. preempt_enable();
  1388. rcu_read_unlock();
  1389. pr_alert("rcu_torture_stall end.\n");
  1390. }
  1391. rcutorture_shutdown_absorb("rcu_torture_stall");
  1392. while (!kthread_should_stop())
  1393. schedule_timeout_interruptible(10 * HZ);
  1394. return 0;
  1395. }
  1396. /* Spawn CPU-stall kthread, if stall_cpu specified. */
  1397. static int __init rcu_torture_stall_init(void)
  1398. {
  1399. int ret;
  1400. if (stall_cpu <= 0)
  1401. return 0;
  1402. stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
  1403. if (IS_ERR(stall_task)) {
  1404. ret = PTR_ERR(stall_task);
  1405. stall_task = NULL;
  1406. return ret;
  1407. }
  1408. return 0;
  1409. }
  1410. /* Clean up after the CPU-stall kthread, if one was spawned. */
  1411. static void rcu_torture_stall_cleanup(void)
  1412. {
  1413. if (stall_task == NULL)
  1414. return;
  1415. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
  1416. kthread_stop(stall_task);
  1417. stall_task = NULL;
  1418. }
  1419. /* Callback function for RCU barrier testing. */
  1420. void rcu_torture_barrier_cbf(struct rcu_head *rcu)
  1421. {
  1422. atomic_inc(&barrier_cbs_invoked);
  1423. }
  1424. /* kthread function to register callbacks used to test RCU barriers. */
  1425. static int rcu_torture_barrier_cbs(void *arg)
  1426. {
  1427. long myid = (long)arg;
  1428. bool lastphase = 0;
  1429. struct rcu_head rcu;
  1430. init_rcu_head_on_stack(&rcu);
  1431. VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
  1432. set_user_nice(current, 19);
  1433. do {
  1434. wait_event(barrier_cbs_wq[myid],
  1435. barrier_phase != lastphase ||
  1436. kthread_should_stop() ||
  1437. fullstop != FULLSTOP_DONTSTOP);
  1438. lastphase = barrier_phase;
  1439. smp_mb(); /* ensure barrier_phase load before ->call(). */
  1440. if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
  1441. break;
  1442. cur_ops->call(&rcu, rcu_torture_barrier_cbf);
  1443. if (atomic_dec_and_test(&barrier_cbs_count))
  1444. wake_up(&barrier_wq);
  1445. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  1446. VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
  1447. rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
  1448. while (!kthread_should_stop())
  1449. schedule_timeout_interruptible(1);
  1450. cur_ops->cb_barrier();
  1451. destroy_rcu_head_on_stack(&rcu);
  1452. return 0;
  1453. }
  1454. /* kthread function to drive and coordinate RCU barrier testing. */
  1455. static int rcu_torture_barrier(void *arg)
  1456. {
  1457. int i;
  1458. VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
  1459. do {
  1460. atomic_set(&barrier_cbs_invoked, 0);
  1461. atomic_set(&barrier_cbs_count, n_barrier_cbs);
  1462. smp_mb(); /* Ensure barrier_phase after prior assignments. */
  1463. barrier_phase = !barrier_phase;
  1464. for (i = 0; i < n_barrier_cbs; i++)
  1465. wake_up(&barrier_cbs_wq[i]);
  1466. wait_event(barrier_wq,
  1467. atomic_read(&barrier_cbs_count) == 0 ||
  1468. kthread_should_stop() ||
  1469. fullstop != FULLSTOP_DONTSTOP);
  1470. if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
  1471. break;
  1472. n_barrier_attempts++;
  1473. cur_ops->cb_barrier();
  1474. if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
  1475. n_rcu_torture_barrier_error++;
  1476. WARN_ON_ONCE(1);
  1477. }
  1478. n_barrier_successes++;
  1479. schedule_timeout_interruptible(HZ / 10);
  1480. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  1481. VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
  1482. rcutorture_shutdown_absorb("rcu_torture_barrier");
  1483. while (!kthread_should_stop())
  1484. schedule_timeout_interruptible(1);
  1485. return 0;
  1486. }
  1487. /* Initialize RCU barrier testing. */
  1488. static int rcu_torture_barrier_init(void)
  1489. {
  1490. int i;
  1491. int ret;
  1492. if (n_barrier_cbs == 0)
  1493. return 0;
  1494. if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
  1495. pr_alert("%s" TORTURE_FLAG
  1496. " Call or barrier ops missing for %s,\n",
  1497. torture_type, cur_ops->name);
  1498. pr_alert("%s" TORTURE_FLAG
  1499. " RCU barrier testing omitted from run.\n",
  1500. torture_type);
  1501. return 0;
  1502. }
  1503. atomic_set(&barrier_cbs_count, 0);
  1504. atomic_set(&barrier_cbs_invoked, 0);
  1505. barrier_cbs_tasks =
  1506. kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
  1507. GFP_KERNEL);
  1508. barrier_cbs_wq =
  1509. kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
  1510. GFP_KERNEL);
  1511. if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
  1512. return -ENOMEM;
  1513. for (i = 0; i < n_barrier_cbs; i++) {
  1514. init_waitqueue_head(&barrier_cbs_wq[i]);
  1515. barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
  1516. (void *)(long)i,
  1517. "rcu_torture_barrier_cbs");
  1518. if (IS_ERR(barrier_cbs_tasks[i])) {
  1519. ret = PTR_ERR(barrier_cbs_tasks[i]);
  1520. VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
  1521. barrier_cbs_tasks[i] = NULL;
  1522. return ret;
  1523. }
  1524. }
  1525. barrier_task = kthread_run(rcu_torture_barrier, NULL,
  1526. "rcu_torture_barrier");
  1527. if (IS_ERR(barrier_task)) {
  1528. ret = PTR_ERR(barrier_task);
  1529. VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
  1530. barrier_task = NULL;
  1531. }
  1532. return 0;
  1533. }
  1534. /* Clean up after RCU barrier testing. */
  1535. static void rcu_torture_barrier_cleanup(void)
  1536. {
  1537. int i;
  1538. if (barrier_task != NULL) {
  1539. VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
  1540. kthread_stop(barrier_task);
  1541. barrier_task = NULL;
  1542. }
  1543. if (barrier_cbs_tasks != NULL) {
  1544. for (i = 0; i < n_barrier_cbs; i++) {
  1545. if (barrier_cbs_tasks[i] != NULL) {
  1546. VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
  1547. kthread_stop(barrier_cbs_tasks[i]);
  1548. barrier_cbs_tasks[i] = NULL;
  1549. }
  1550. }
  1551. kfree(barrier_cbs_tasks);
  1552. barrier_cbs_tasks = NULL;
  1553. }
  1554. if (barrier_cbs_wq != NULL) {
  1555. kfree(barrier_cbs_wq);
  1556. barrier_cbs_wq = NULL;
  1557. }
  1558. }
  1559. static int rcutorture_cpu_notify(struct notifier_block *self,
  1560. unsigned long action, void *hcpu)
  1561. {
  1562. long cpu = (long)hcpu;
  1563. switch (action) {
  1564. case CPU_ONLINE:
  1565. case CPU_DOWN_FAILED:
  1566. (void)rcutorture_booster_init(cpu);
  1567. break;
  1568. case CPU_DOWN_PREPARE:
  1569. rcutorture_booster_cleanup(cpu);
  1570. break;
  1571. default:
  1572. break;
  1573. }
  1574. return NOTIFY_OK;
  1575. }
  1576. static struct notifier_block rcutorture_cpu_nb = {
  1577. .notifier_call = rcutorture_cpu_notify,
  1578. };
  1579. static void
  1580. rcu_torture_cleanup(void)
  1581. {
  1582. int i;
  1583. mutex_lock(&fullstop_mutex);
  1584. rcutorture_record_test_transition();
  1585. if (fullstop == FULLSTOP_SHUTDOWN) {
  1586. pr_warn(/* but going down anyway, so... */
  1587. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  1588. mutex_unlock(&fullstop_mutex);
  1589. schedule_timeout_uninterruptible(10);
  1590. if (cur_ops->cb_barrier != NULL)
  1591. cur_ops->cb_barrier();
  1592. return;
  1593. }
  1594. fullstop = FULLSTOP_RMMOD;
  1595. mutex_unlock(&fullstop_mutex);
  1596. unregister_reboot_notifier(&rcutorture_shutdown_nb);
  1597. rcu_torture_barrier_cleanup();
  1598. rcu_torture_stall_cleanup();
  1599. if (stutter_task) {
  1600. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
  1601. kthread_stop(stutter_task);
  1602. }
  1603. stutter_task = NULL;
  1604. if (shuffler_task) {
  1605. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  1606. kthread_stop(shuffler_task);
  1607. free_cpumask_var(shuffle_tmp_mask);
  1608. }
  1609. shuffler_task = NULL;
  1610. if (writer_task) {
  1611. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  1612. kthread_stop(writer_task);
  1613. }
  1614. writer_task = NULL;
  1615. if (reader_tasks) {
  1616. for (i = 0; i < nrealreaders; i++) {
  1617. if (reader_tasks[i]) {
  1618. VERBOSE_PRINTK_STRING(
  1619. "Stopping rcu_torture_reader task");
  1620. kthread_stop(reader_tasks[i]);
  1621. }
  1622. reader_tasks[i] = NULL;
  1623. }
  1624. kfree(reader_tasks);
  1625. reader_tasks = NULL;
  1626. }
  1627. rcu_torture_current = NULL;
  1628. if (fakewriter_tasks) {
  1629. for (i = 0; i < nfakewriters; i++) {
  1630. if (fakewriter_tasks[i]) {
  1631. VERBOSE_PRINTK_STRING(
  1632. "Stopping rcu_torture_fakewriter task");
  1633. kthread_stop(fakewriter_tasks[i]);
  1634. }
  1635. fakewriter_tasks[i] = NULL;
  1636. }
  1637. kfree(fakewriter_tasks);
  1638. fakewriter_tasks = NULL;
  1639. }
  1640. if (stats_task) {
  1641. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  1642. kthread_stop(stats_task);
  1643. }
  1644. stats_task = NULL;
  1645. if (fqs_task) {
  1646. VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
  1647. kthread_stop(fqs_task);
  1648. }
  1649. fqs_task = NULL;
  1650. if ((test_boost == 1 && cur_ops->can_boost) ||
  1651. test_boost == 2) {
  1652. unregister_cpu_notifier(&rcutorture_cpu_nb);
  1653. for_each_possible_cpu(i)
  1654. rcutorture_booster_cleanup(i);
  1655. }
  1656. if (shutdown_task != NULL) {
  1657. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
  1658. kthread_stop(shutdown_task);
  1659. }
  1660. shutdown_task = NULL;
  1661. rcu_torture_onoff_cleanup();
  1662. /* Wait for all RCU callbacks to fire. */
  1663. if (cur_ops->cb_barrier != NULL)
  1664. cur_ops->cb_barrier();
  1665. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  1666. if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
  1667. rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
  1668. else if (n_online_successes != n_online_attempts ||
  1669. n_offline_successes != n_offline_attempts)
  1670. rcu_torture_print_module_parms(cur_ops,
  1671. "End of test: RCU_HOTPLUG");
  1672. else
  1673. rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
  1674. }
  1675. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  1676. static void rcu_torture_leak_cb(struct rcu_head *rhp)
  1677. {
  1678. }
  1679. static void rcu_torture_err_cb(struct rcu_head *rhp)
  1680. {
  1681. /*
  1682. * This -might- happen due to race conditions, but is unlikely.
  1683. * The scenario that leads to this happening is that the
  1684. * first of the pair of duplicate callbacks is queued,
  1685. * someone else starts a grace period that includes that
  1686. * callback, then the second of the pair must wait for the
  1687. * next grace period. Unlikely, but can happen. If it
  1688. * does happen, the debug-objects subsystem won't have splatted.
  1689. */
  1690. pr_alert("rcutorture: duplicated callback was invoked.\n");
  1691. }
  1692. #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1693. /*
  1694. * Verify that double-free causes debug-objects to complain, but only
  1695. * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
  1696. * cannot be carried out.
  1697. */
  1698. static void rcu_test_debug_objects(void)
  1699. {
  1700. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  1701. struct rcu_head rh1;
  1702. struct rcu_head rh2;
  1703. init_rcu_head_on_stack(&rh1);
  1704. init_rcu_head_on_stack(&rh2);
  1705. pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
  1706. /* Try to queue the rh2 pair of callbacks for the same grace period. */
  1707. preempt_disable(); /* Prevent preemption from interrupting test. */
  1708. rcu_read_lock(); /* Make it impossible to finish a grace period. */
  1709. call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
  1710. local_irq_disable(); /* Make it harder to start a new grace period. */
  1711. call_rcu(&rh2, rcu_torture_leak_cb);
  1712. call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
  1713. local_irq_enable();
  1714. rcu_read_unlock();
  1715. preempt_enable();
  1716. /* Wait for them all to get done so we can safely return. */
  1717. rcu_barrier();
  1718. pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
  1719. destroy_rcu_head_on_stack(&rh1);
  1720. destroy_rcu_head_on_stack(&rh2);
  1721. #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1722. pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
  1723. #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  1724. }
  1725. static int __init
  1726. rcu_torture_init(void)
  1727. {
  1728. int i;
  1729. int cpu;
  1730. int firsterr = 0;
  1731. int retval;
  1732. static struct rcu_torture_ops *torture_ops[] = {
  1733. &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
  1734. };
  1735. mutex_lock(&fullstop_mutex);
  1736. /* Process args and tell the world that the torturer is on the job. */
  1737. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1738. cur_ops = torture_ops[i];
  1739. if (strcmp(torture_type, cur_ops->name) == 0)
  1740. break;
  1741. }
  1742. if (i == ARRAY_SIZE(torture_ops)) {
  1743. pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
  1744. torture_type);
  1745. pr_alert("rcu-torture types:");
  1746. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1747. pr_alert(" %s", torture_ops[i]->name);
  1748. pr_alert("\n");
  1749. mutex_unlock(&fullstop_mutex);
  1750. return -EINVAL;
  1751. }
  1752. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  1753. pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
  1754. fqs_duration = 0;
  1755. }
  1756. if (cur_ops->init)
  1757. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  1758. if (nreaders >= 0)
  1759. nrealreaders = nreaders;
  1760. else
  1761. nrealreaders = 2 * num_online_cpus();
  1762. rcu_torture_print_module_parms(cur_ops, "Start of test");
  1763. fullstop = FULLSTOP_DONTSTOP;
  1764. /* Set up the freelist. */
  1765. INIT_LIST_HEAD(&rcu_torture_freelist);
  1766. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  1767. rcu_tortures[i].rtort_mbtest = 0;
  1768. list_add_tail(&rcu_tortures[i].rtort_free,
  1769. &rcu_torture_freelist);
  1770. }
  1771. /* Initialize the statistics so that each run gets its own numbers. */
  1772. rcu_torture_current = NULL;
  1773. rcu_torture_current_version = 0;
  1774. atomic_set(&n_rcu_torture_alloc, 0);
  1775. atomic_set(&n_rcu_torture_alloc_fail, 0);
  1776. atomic_set(&n_rcu_torture_free, 0);
  1777. atomic_set(&n_rcu_torture_mberror, 0);
  1778. atomic_set(&n_rcu_torture_error, 0);
  1779. n_rcu_torture_barrier_error = 0;
  1780. n_rcu_torture_boost_ktrerror = 0;
  1781. n_rcu_torture_boost_rterror = 0;
  1782. n_rcu_torture_boost_failure = 0;
  1783. n_rcu_torture_boosts = 0;
  1784. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1785. atomic_set(&rcu_torture_wcount[i], 0);
  1786. for_each_possible_cpu(cpu) {
  1787. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1788. per_cpu(rcu_torture_count, cpu)[i] = 0;
  1789. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  1790. }
  1791. }
  1792. /* Start up the kthreads. */
  1793. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  1794. writer_task = kthread_create(rcu_torture_writer, NULL,
  1795. "rcu_torture_writer");
  1796. if (IS_ERR(writer_task)) {
  1797. firsterr = PTR_ERR(writer_task);
  1798. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  1799. writer_task = NULL;
  1800. goto unwind;
  1801. }
  1802. wake_up_process(writer_task);
  1803. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  1804. GFP_KERNEL);
  1805. if (fakewriter_tasks == NULL) {
  1806. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1807. firsterr = -ENOMEM;
  1808. goto unwind;
  1809. }
  1810. for (i = 0; i < nfakewriters; i++) {
  1811. VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
  1812. fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
  1813. "rcu_torture_fakewriter");
  1814. if (IS_ERR(fakewriter_tasks[i])) {
  1815. firsterr = PTR_ERR(fakewriter_tasks[i]);
  1816. VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
  1817. fakewriter_tasks[i] = NULL;
  1818. goto unwind;
  1819. }
  1820. }
  1821. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  1822. GFP_KERNEL);
  1823. if (reader_tasks == NULL) {
  1824. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1825. firsterr = -ENOMEM;
  1826. goto unwind;
  1827. }
  1828. for (i = 0; i < nrealreaders; i++) {
  1829. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  1830. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  1831. "rcu_torture_reader");
  1832. if (IS_ERR(reader_tasks[i])) {
  1833. firsterr = PTR_ERR(reader_tasks[i]);
  1834. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  1835. reader_tasks[i] = NULL;
  1836. goto unwind;
  1837. }
  1838. }
  1839. if (stat_interval > 0) {
  1840. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  1841. stats_task = kthread_run(rcu_torture_stats, NULL,
  1842. "rcu_torture_stats");
  1843. if (IS_ERR(stats_task)) {
  1844. firsterr = PTR_ERR(stats_task);
  1845. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  1846. stats_task = NULL;
  1847. goto unwind;
  1848. }
  1849. }
  1850. if (test_no_idle_hz) {
  1851. rcu_idle_cpu = num_online_cpus() - 1;
  1852. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  1853. firsterr = -ENOMEM;
  1854. VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
  1855. goto unwind;
  1856. }
  1857. /* Create the shuffler thread */
  1858. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  1859. "rcu_torture_shuffle");
  1860. if (IS_ERR(shuffler_task)) {
  1861. free_cpumask_var(shuffle_tmp_mask);
  1862. firsterr = PTR_ERR(shuffler_task);
  1863. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  1864. shuffler_task = NULL;
  1865. goto unwind;
  1866. }
  1867. }
  1868. if (stutter < 0)
  1869. stutter = 0;
  1870. if (stutter) {
  1871. /* Create the stutter thread */
  1872. stutter_task = kthread_run(rcu_torture_stutter, NULL,
  1873. "rcu_torture_stutter");
  1874. if (IS_ERR(stutter_task)) {
  1875. firsterr = PTR_ERR(stutter_task);
  1876. VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
  1877. stutter_task = NULL;
  1878. goto unwind;
  1879. }
  1880. }
  1881. if (fqs_duration < 0)
  1882. fqs_duration = 0;
  1883. if (fqs_duration) {
  1884. /* Create the stutter thread */
  1885. fqs_task = kthread_run(rcu_torture_fqs, NULL,
  1886. "rcu_torture_fqs");
  1887. if (IS_ERR(fqs_task)) {
  1888. firsterr = PTR_ERR(fqs_task);
  1889. VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
  1890. fqs_task = NULL;
  1891. goto unwind;
  1892. }
  1893. }
  1894. if (test_boost_interval < 1)
  1895. test_boost_interval = 1;
  1896. if (test_boost_duration < 2)
  1897. test_boost_duration = 2;
  1898. if ((test_boost == 1 && cur_ops->can_boost) ||
  1899. test_boost == 2) {
  1900. boost_starttime = jiffies + test_boost_interval * HZ;
  1901. register_cpu_notifier(&rcutorture_cpu_nb);
  1902. for_each_possible_cpu(i) {
  1903. if (cpu_is_offline(i))
  1904. continue; /* Heuristic: CPU can go offline. */
  1905. retval = rcutorture_booster_init(i);
  1906. if (retval < 0) {
  1907. firsterr = retval;
  1908. goto unwind;
  1909. }
  1910. }
  1911. }
  1912. if (shutdown_secs > 0) {
  1913. shutdown_time = jiffies + shutdown_secs * HZ;
  1914. shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
  1915. "rcu_torture_shutdown");
  1916. if (IS_ERR(shutdown_task)) {
  1917. firsterr = PTR_ERR(shutdown_task);
  1918. VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
  1919. shutdown_task = NULL;
  1920. goto unwind;
  1921. }
  1922. wake_up_process(shutdown_task);
  1923. }
  1924. i = rcu_torture_onoff_init();
  1925. if (i != 0) {
  1926. firsterr = i;
  1927. goto unwind;
  1928. }
  1929. register_reboot_notifier(&rcutorture_shutdown_nb);
  1930. i = rcu_torture_stall_init();
  1931. if (i != 0) {
  1932. firsterr = i;
  1933. goto unwind;
  1934. }
  1935. retval = rcu_torture_barrier_init();
  1936. if (retval != 0) {
  1937. firsterr = retval;
  1938. goto unwind;
  1939. }
  1940. if (object_debug)
  1941. rcu_test_debug_objects();
  1942. rcutorture_record_test_transition();
  1943. mutex_unlock(&fullstop_mutex);
  1944. return 0;
  1945. unwind:
  1946. mutex_unlock(&fullstop_mutex);
  1947. rcu_torture_cleanup();
  1948. return firsterr;
  1949. }
  1950. module_init(rcu_torture_init);
  1951. module_exit(rcu_torture_cleanup);