rcutorture.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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 <asm/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 <asm/byteorder.h>
  50. MODULE_LICENSE("GPL");
  51. MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and "
  52. "Josh Triplett <josh@freedesktop.org>");
  53. static int nreaders = -1; /* # reader threads, defaults to 2*ncpus */
  54. static int nfakewriters = 4; /* # fake writer threads */
  55. static int stat_interval; /* Interval between stats, in seconds. */
  56. /* Defaults to "only at end of test". */
  57. static int verbose; /* Print more debug info. */
  58. static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
  59. static int shuffle_interval = 3; /* Interval between shuffles (in sec)*/
  60. static int stutter = 5; /* Start/stop testing interval (in sec) */
  61. static int irqreader = 1; /* RCU readers from irq (timers). */
  62. static int fqs_duration = 0; /* Duration of bursts (us), 0 to disable. */
  63. static int fqs_holdoff = 0; /* Hold time within burst (us). */
  64. static int fqs_stutter = 3; /* Wait time between bursts (s). */
  65. static char *torture_type = "rcu"; /* What RCU implementation to torture. */
  66. module_param(nreaders, int, 0444);
  67. MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  68. module_param(nfakewriters, int, 0444);
  69. MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
  70. module_param(stat_interval, int, 0444);
  71. MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  72. module_param(verbose, bool, 0444);
  73. MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  74. module_param(test_no_idle_hz, bool, 0444);
  75. MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
  76. module_param(shuffle_interval, int, 0444);
  77. MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
  78. module_param(stutter, int, 0444);
  79. MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
  80. module_param(irqreader, int, 0444);
  81. MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
  82. module_param(fqs_duration, int, 0444);
  83. MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us)");
  84. module_param(fqs_holdoff, int, 0444);
  85. MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
  86. module_param(fqs_stutter, int, 0444);
  87. MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
  88. module_param(torture_type, charp, 0444);
  89. MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, srcu)");
  90. #define TORTURE_FLAG "-torture:"
  91. #define PRINTK_STRING(s) \
  92. do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  93. #define VERBOSE_PRINTK_STRING(s) \
  94. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  95. #define VERBOSE_PRINTK_ERRSTRING(s) \
  96. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
  97. static char printk_buf[4096];
  98. static int nrealreaders;
  99. static struct task_struct *writer_task;
  100. static struct task_struct **fakewriter_tasks;
  101. static struct task_struct **reader_tasks;
  102. static struct task_struct *stats_task;
  103. static struct task_struct *shuffler_task;
  104. static struct task_struct *stutter_task;
  105. static struct task_struct *fqs_task;
  106. #define RCU_TORTURE_PIPE_LEN 10
  107. struct rcu_torture {
  108. struct rcu_head rtort_rcu;
  109. int rtort_pipe_count;
  110. struct list_head rtort_free;
  111. int rtort_mbtest;
  112. };
  113. static LIST_HEAD(rcu_torture_freelist);
  114. static struct rcu_torture *rcu_torture_current;
  115. static long rcu_torture_current_version;
  116. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  117. static DEFINE_SPINLOCK(rcu_torture_lock);
  118. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
  119. { 0 };
  120. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
  121. { 0 };
  122. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  123. static atomic_t n_rcu_torture_alloc;
  124. static atomic_t n_rcu_torture_alloc_fail;
  125. static atomic_t n_rcu_torture_free;
  126. static atomic_t n_rcu_torture_mberror;
  127. static atomic_t n_rcu_torture_error;
  128. static long n_rcu_torture_timers;
  129. static struct list_head rcu_torture_removed;
  130. static cpumask_var_t shuffle_tmp_mask;
  131. static int stutter_pause_test;
  132. #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
  133. #define RCUTORTURE_RUNNABLE_INIT 1
  134. #else
  135. #define RCUTORTURE_RUNNABLE_INIT 0
  136. #endif
  137. int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
  138. /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
  139. #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
  140. #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
  141. #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
  142. static int fullstop = FULLSTOP_RMMOD;
  143. DEFINE_MUTEX(fullstop_mutex); /* Protect fullstop transitions and spawning */
  144. /* of kthreads. */
  145. /*
  146. * Detect and respond to a system shutdown.
  147. */
  148. static int
  149. rcutorture_shutdown_notify(struct notifier_block *unused1,
  150. unsigned long unused2, void *unused3)
  151. {
  152. mutex_lock(&fullstop_mutex);
  153. if (fullstop == FULLSTOP_DONTSTOP)
  154. fullstop = FULLSTOP_SHUTDOWN;
  155. else
  156. printk(KERN_WARNING /* but going down anyway, so... */
  157. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  158. mutex_unlock(&fullstop_mutex);
  159. return NOTIFY_DONE;
  160. }
  161. /*
  162. * Absorb kthreads into a kernel function that won't return, so that
  163. * they won't ever access module text or data again.
  164. */
  165. static void rcutorture_shutdown_absorb(char *title)
  166. {
  167. if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
  168. printk(KERN_NOTICE
  169. "rcutorture thread %s parking due to system shutdown\n",
  170. title);
  171. schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  172. }
  173. }
  174. /*
  175. * Allocate an element from the rcu_tortures pool.
  176. */
  177. static struct rcu_torture *
  178. rcu_torture_alloc(void)
  179. {
  180. struct list_head *p;
  181. spin_lock_bh(&rcu_torture_lock);
  182. if (list_empty(&rcu_torture_freelist)) {
  183. atomic_inc(&n_rcu_torture_alloc_fail);
  184. spin_unlock_bh(&rcu_torture_lock);
  185. return NULL;
  186. }
  187. atomic_inc(&n_rcu_torture_alloc);
  188. p = rcu_torture_freelist.next;
  189. list_del_init(p);
  190. spin_unlock_bh(&rcu_torture_lock);
  191. return container_of(p, struct rcu_torture, rtort_free);
  192. }
  193. /*
  194. * Free an element to the rcu_tortures pool.
  195. */
  196. static void
  197. rcu_torture_free(struct rcu_torture *p)
  198. {
  199. atomic_inc(&n_rcu_torture_free);
  200. spin_lock_bh(&rcu_torture_lock);
  201. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  202. spin_unlock_bh(&rcu_torture_lock);
  203. }
  204. struct rcu_random_state {
  205. unsigned long rrs_state;
  206. long rrs_count;
  207. };
  208. #define RCU_RANDOM_MULT 39916801 /* prime */
  209. #define RCU_RANDOM_ADD 479001701 /* prime */
  210. #define RCU_RANDOM_REFRESH 10000
  211. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  212. /*
  213. * Crude but fast random-number generator. Uses a linear congruential
  214. * generator, with occasional help from cpu_clock().
  215. */
  216. static unsigned long
  217. rcu_random(struct rcu_random_state *rrsp)
  218. {
  219. if (--rrsp->rrs_count < 0) {
  220. rrsp->rrs_state +=
  221. (unsigned long)cpu_clock(raw_smp_processor_id());
  222. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  223. }
  224. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  225. return swahw32(rrsp->rrs_state);
  226. }
  227. static void
  228. rcu_stutter_wait(char *title)
  229. {
  230. while (stutter_pause_test || !rcutorture_runnable) {
  231. if (rcutorture_runnable)
  232. schedule_timeout_interruptible(1);
  233. else
  234. schedule_timeout_interruptible(round_jiffies_relative(HZ));
  235. rcutorture_shutdown_absorb(title);
  236. }
  237. }
  238. /*
  239. * Operations vector for selecting different types of tests.
  240. */
  241. struct rcu_torture_ops {
  242. void (*init)(void);
  243. void (*cleanup)(void);
  244. int (*readlock)(void);
  245. void (*read_delay)(struct rcu_random_state *rrsp);
  246. void (*readunlock)(int idx);
  247. int (*completed)(void);
  248. void (*deferred_free)(struct rcu_torture *p);
  249. void (*sync)(void);
  250. void (*cb_barrier)(void);
  251. void (*fqs)(void);
  252. int (*stats)(char *page);
  253. int irq_capable;
  254. char *name;
  255. };
  256. static struct rcu_torture_ops *cur_ops;
  257. /*
  258. * Definitions for rcu torture testing.
  259. */
  260. static int rcu_torture_read_lock(void) __acquires(RCU)
  261. {
  262. rcu_read_lock();
  263. return 0;
  264. }
  265. static void rcu_read_delay(struct rcu_random_state *rrsp)
  266. {
  267. const unsigned long shortdelay_us = 200;
  268. const unsigned long longdelay_ms = 50;
  269. /* We want a short delay sometimes to make a reader delay the grace
  270. * period, and we want a long delay occasionally to trigger
  271. * force_quiescent_state. */
  272. if (!(rcu_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
  273. mdelay(longdelay_ms);
  274. if (!(rcu_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
  275. udelay(shortdelay_us);
  276. }
  277. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  278. {
  279. rcu_read_unlock();
  280. }
  281. static int rcu_torture_completed(void)
  282. {
  283. return rcu_batches_completed();
  284. }
  285. static void
  286. rcu_torture_cb(struct rcu_head *p)
  287. {
  288. int i;
  289. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  290. if (fullstop != FULLSTOP_DONTSTOP) {
  291. /* Test is ending, just drop callbacks on the floor. */
  292. /* The next initialization will pick up the pieces. */
  293. return;
  294. }
  295. i = rp->rtort_pipe_count;
  296. if (i > RCU_TORTURE_PIPE_LEN)
  297. i = RCU_TORTURE_PIPE_LEN;
  298. atomic_inc(&rcu_torture_wcount[i]);
  299. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  300. rp->rtort_mbtest = 0;
  301. rcu_torture_free(rp);
  302. } else
  303. cur_ops->deferred_free(rp);
  304. }
  305. static int rcu_no_completed(void)
  306. {
  307. return 0;
  308. }
  309. static void rcu_torture_deferred_free(struct rcu_torture *p)
  310. {
  311. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  312. }
  313. static struct rcu_torture_ops rcu_ops = {
  314. .init = NULL,
  315. .cleanup = NULL,
  316. .readlock = rcu_torture_read_lock,
  317. .read_delay = rcu_read_delay,
  318. .readunlock = rcu_torture_read_unlock,
  319. .completed = rcu_torture_completed,
  320. .deferred_free = rcu_torture_deferred_free,
  321. .sync = synchronize_rcu,
  322. .cb_barrier = rcu_barrier,
  323. .fqs = rcu_force_quiescent_state,
  324. .stats = NULL,
  325. .irq_capable = 1,
  326. .name = "rcu"
  327. };
  328. static void rcu_sync_torture_deferred_free(struct rcu_torture *p)
  329. {
  330. int i;
  331. struct rcu_torture *rp;
  332. struct rcu_torture *rp1;
  333. cur_ops->sync();
  334. list_add(&p->rtort_free, &rcu_torture_removed);
  335. list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
  336. i = rp->rtort_pipe_count;
  337. if (i > RCU_TORTURE_PIPE_LEN)
  338. i = RCU_TORTURE_PIPE_LEN;
  339. atomic_inc(&rcu_torture_wcount[i]);
  340. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  341. rp->rtort_mbtest = 0;
  342. list_del(&rp->rtort_free);
  343. rcu_torture_free(rp);
  344. }
  345. }
  346. }
  347. static void rcu_sync_torture_init(void)
  348. {
  349. INIT_LIST_HEAD(&rcu_torture_removed);
  350. }
  351. static struct rcu_torture_ops rcu_sync_ops = {
  352. .init = rcu_sync_torture_init,
  353. .cleanup = NULL,
  354. .readlock = rcu_torture_read_lock,
  355. .read_delay = rcu_read_delay,
  356. .readunlock = rcu_torture_read_unlock,
  357. .completed = rcu_torture_completed,
  358. .deferred_free = rcu_sync_torture_deferred_free,
  359. .sync = synchronize_rcu,
  360. .cb_barrier = NULL,
  361. .fqs = rcu_force_quiescent_state,
  362. .stats = NULL,
  363. .irq_capable = 1,
  364. .name = "rcu_sync"
  365. };
  366. static struct rcu_torture_ops rcu_expedited_ops = {
  367. .init = rcu_sync_torture_init,
  368. .cleanup = NULL,
  369. .readlock = rcu_torture_read_lock,
  370. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  371. .readunlock = rcu_torture_read_unlock,
  372. .completed = rcu_no_completed,
  373. .deferred_free = rcu_sync_torture_deferred_free,
  374. .sync = synchronize_rcu_expedited,
  375. .cb_barrier = NULL,
  376. .fqs = rcu_force_quiescent_state,
  377. .stats = NULL,
  378. .irq_capable = 1,
  379. .name = "rcu_expedited"
  380. };
  381. /*
  382. * Definitions for rcu_bh torture testing.
  383. */
  384. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  385. {
  386. rcu_read_lock_bh();
  387. return 0;
  388. }
  389. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  390. {
  391. rcu_read_unlock_bh();
  392. }
  393. static int rcu_bh_torture_completed(void)
  394. {
  395. return rcu_batches_completed_bh();
  396. }
  397. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  398. {
  399. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  400. }
  401. struct rcu_bh_torture_synchronize {
  402. struct rcu_head head;
  403. struct completion completion;
  404. };
  405. static void rcu_bh_torture_wakeme_after_cb(struct rcu_head *head)
  406. {
  407. struct rcu_bh_torture_synchronize *rcu;
  408. rcu = container_of(head, struct rcu_bh_torture_synchronize, head);
  409. complete(&rcu->completion);
  410. }
  411. static void rcu_bh_torture_synchronize(void)
  412. {
  413. struct rcu_bh_torture_synchronize rcu;
  414. init_rcu_head_on_stack(&rcu.head);
  415. init_completion(&rcu.completion);
  416. call_rcu_bh(&rcu.head, rcu_bh_torture_wakeme_after_cb);
  417. wait_for_completion(&rcu.completion);
  418. destroy_rcu_head_on_stack(&rcu.head);
  419. }
  420. static struct rcu_torture_ops rcu_bh_ops = {
  421. .init = NULL,
  422. .cleanup = NULL,
  423. .readlock = rcu_bh_torture_read_lock,
  424. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  425. .readunlock = rcu_bh_torture_read_unlock,
  426. .completed = rcu_bh_torture_completed,
  427. .deferred_free = rcu_bh_torture_deferred_free,
  428. .sync = rcu_bh_torture_synchronize,
  429. .cb_barrier = rcu_barrier_bh,
  430. .fqs = rcu_bh_force_quiescent_state,
  431. .stats = NULL,
  432. .irq_capable = 1,
  433. .name = "rcu_bh"
  434. };
  435. static struct rcu_torture_ops rcu_bh_sync_ops = {
  436. .init = rcu_sync_torture_init,
  437. .cleanup = NULL,
  438. .readlock = rcu_bh_torture_read_lock,
  439. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  440. .readunlock = rcu_bh_torture_read_unlock,
  441. .completed = rcu_bh_torture_completed,
  442. .deferred_free = rcu_sync_torture_deferred_free,
  443. .sync = rcu_bh_torture_synchronize,
  444. .cb_barrier = NULL,
  445. .fqs = rcu_bh_force_quiescent_state,
  446. .stats = NULL,
  447. .irq_capable = 1,
  448. .name = "rcu_bh_sync"
  449. };
  450. /*
  451. * Definitions for srcu torture testing.
  452. */
  453. static struct srcu_struct srcu_ctl;
  454. static void srcu_torture_init(void)
  455. {
  456. init_srcu_struct(&srcu_ctl);
  457. rcu_sync_torture_init();
  458. }
  459. static void srcu_torture_cleanup(void)
  460. {
  461. synchronize_srcu(&srcu_ctl);
  462. cleanup_srcu_struct(&srcu_ctl);
  463. }
  464. static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
  465. {
  466. return srcu_read_lock(&srcu_ctl);
  467. }
  468. static void srcu_read_delay(struct rcu_random_state *rrsp)
  469. {
  470. long delay;
  471. const long uspertick = 1000000 / HZ;
  472. const long longdelay = 10;
  473. /* We want there to be long-running readers, but not all the time. */
  474. delay = rcu_random(rrsp) % (nrealreaders * 2 * longdelay * uspertick);
  475. if (!delay)
  476. schedule_timeout_interruptible(longdelay);
  477. }
  478. static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
  479. {
  480. srcu_read_unlock(&srcu_ctl, idx);
  481. }
  482. static int srcu_torture_completed(void)
  483. {
  484. return srcu_batches_completed(&srcu_ctl);
  485. }
  486. static void srcu_torture_synchronize(void)
  487. {
  488. synchronize_srcu(&srcu_ctl);
  489. }
  490. static int srcu_torture_stats(char *page)
  491. {
  492. int cnt = 0;
  493. int cpu;
  494. int idx = srcu_ctl.completed & 0x1;
  495. cnt += sprintf(&page[cnt], "%s%s per-CPU(idx=%d):",
  496. torture_type, TORTURE_FLAG, idx);
  497. for_each_possible_cpu(cpu) {
  498. cnt += sprintf(&page[cnt], " %d(%d,%d)", cpu,
  499. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
  500. per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
  501. }
  502. cnt += sprintf(&page[cnt], "\n");
  503. return cnt;
  504. }
  505. static struct rcu_torture_ops srcu_ops = {
  506. .init = srcu_torture_init,
  507. .cleanup = srcu_torture_cleanup,
  508. .readlock = srcu_torture_read_lock,
  509. .read_delay = srcu_read_delay,
  510. .readunlock = srcu_torture_read_unlock,
  511. .completed = srcu_torture_completed,
  512. .deferred_free = rcu_sync_torture_deferred_free,
  513. .sync = srcu_torture_synchronize,
  514. .cb_barrier = NULL,
  515. .stats = srcu_torture_stats,
  516. .name = "srcu"
  517. };
  518. static void srcu_torture_synchronize_expedited(void)
  519. {
  520. synchronize_srcu_expedited(&srcu_ctl);
  521. }
  522. static struct rcu_torture_ops srcu_expedited_ops = {
  523. .init = srcu_torture_init,
  524. .cleanup = srcu_torture_cleanup,
  525. .readlock = srcu_torture_read_lock,
  526. .read_delay = srcu_read_delay,
  527. .readunlock = srcu_torture_read_unlock,
  528. .completed = srcu_torture_completed,
  529. .deferred_free = rcu_sync_torture_deferred_free,
  530. .sync = srcu_torture_synchronize_expedited,
  531. .cb_barrier = NULL,
  532. .stats = srcu_torture_stats,
  533. .name = "srcu_expedited"
  534. };
  535. /*
  536. * Definitions for sched torture testing.
  537. */
  538. static int sched_torture_read_lock(void)
  539. {
  540. preempt_disable();
  541. return 0;
  542. }
  543. static void sched_torture_read_unlock(int idx)
  544. {
  545. preempt_enable();
  546. }
  547. static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
  548. {
  549. call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
  550. }
  551. static void sched_torture_synchronize(void)
  552. {
  553. synchronize_sched();
  554. }
  555. static struct rcu_torture_ops sched_ops = {
  556. .init = rcu_sync_torture_init,
  557. .cleanup = NULL,
  558. .readlock = sched_torture_read_lock,
  559. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  560. .readunlock = sched_torture_read_unlock,
  561. .completed = rcu_no_completed,
  562. .deferred_free = rcu_sched_torture_deferred_free,
  563. .sync = sched_torture_synchronize,
  564. .cb_barrier = rcu_barrier_sched,
  565. .fqs = rcu_sched_force_quiescent_state,
  566. .stats = NULL,
  567. .irq_capable = 1,
  568. .name = "sched"
  569. };
  570. static struct rcu_torture_ops sched_sync_ops = {
  571. .init = rcu_sync_torture_init,
  572. .cleanup = NULL,
  573. .readlock = sched_torture_read_lock,
  574. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  575. .readunlock = sched_torture_read_unlock,
  576. .completed = rcu_no_completed,
  577. .deferred_free = rcu_sync_torture_deferred_free,
  578. .sync = sched_torture_synchronize,
  579. .cb_barrier = NULL,
  580. .fqs = rcu_sched_force_quiescent_state,
  581. .stats = NULL,
  582. .name = "sched_sync"
  583. };
  584. static struct rcu_torture_ops sched_expedited_ops = {
  585. .init = rcu_sync_torture_init,
  586. .cleanup = NULL,
  587. .readlock = sched_torture_read_lock,
  588. .read_delay = rcu_read_delay, /* just reuse rcu's version. */
  589. .readunlock = sched_torture_read_unlock,
  590. .completed = rcu_no_completed,
  591. .deferred_free = rcu_sync_torture_deferred_free,
  592. .sync = synchronize_sched_expedited,
  593. .cb_barrier = NULL,
  594. .fqs = rcu_sched_force_quiescent_state,
  595. .stats = NULL,
  596. .irq_capable = 1,
  597. .name = "sched_expedited"
  598. };
  599. /*
  600. * RCU torture force-quiescent-state kthread. Repeatedly induces
  601. * bursts of calls to force_quiescent_state(), increasing the probability
  602. * of occurrence of some important types of race conditions.
  603. */
  604. static int
  605. rcu_torture_fqs(void *arg)
  606. {
  607. unsigned long fqs_resume_time;
  608. int fqs_burst_remaining;
  609. VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
  610. do {
  611. fqs_resume_time = jiffies + fqs_stutter * HZ;
  612. while (jiffies - fqs_resume_time > LONG_MAX) {
  613. schedule_timeout_interruptible(1);
  614. }
  615. fqs_burst_remaining = fqs_duration;
  616. while (fqs_burst_remaining > 0) {
  617. cur_ops->fqs();
  618. udelay(fqs_holdoff);
  619. fqs_burst_remaining -= fqs_holdoff;
  620. }
  621. rcu_stutter_wait("rcu_torture_fqs");
  622. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  623. VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
  624. rcutorture_shutdown_absorb("rcu_torture_fqs");
  625. while (!kthread_should_stop())
  626. schedule_timeout_uninterruptible(1);
  627. return 0;
  628. }
  629. /*
  630. * RCU torture writer kthread. Repeatedly substitutes a new structure
  631. * for that pointed to by rcu_torture_current, freeing the old structure
  632. * after a series of grace periods (the "pipeline").
  633. */
  634. static int
  635. rcu_torture_writer(void *arg)
  636. {
  637. int i;
  638. long oldbatch = rcu_batches_completed();
  639. struct rcu_torture *rp;
  640. struct rcu_torture *old_rp;
  641. static DEFINE_RCU_RANDOM(rand);
  642. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  643. set_user_nice(current, 19);
  644. do {
  645. schedule_timeout_uninterruptible(1);
  646. rp = rcu_torture_alloc();
  647. if (rp == NULL)
  648. continue;
  649. rp->rtort_pipe_count = 0;
  650. udelay(rcu_random(&rand) & 0x3ff);
  651. old_rp = rcu_torture_current;
  652. rp->rtort_mbtest = 1;
  653. rcu_assign_pointer(rcu_torture_current, rp);
  654. smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
  655. if (old_rp) {
  656. i = old_rp->rtort_pipe_count;
  657. if (i > RCU_TORTURE_PIPE_LEN)
  658. i = RCU_TORTURE_PIPE_LEN;
  659. atomic_inc(&rcu_torture_wcount[i]);
  660. old_rp->rtort_pipe_count++;
  661. cur_ops->deferred_free(old_rp);
  662. }
  663. rcu_torture_current_version++;
  664. oldbatch = cur_ops->completed();
  665. rcu_stutter_wait("rcu_torture_writer");
  666. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  667. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  668. rcutorture_shutdown_absorb("rcu_torture_writer");
  669. while (!kthread_should_stop())
  670. schedule_timeout_uninterruptible(1);
  671. return 0;
  672. }
  673. /*
  674. * RCU torture fake writer kthread. Repeatedly calls sync, with a random
  675. * delay between calls.
  676. */
  677. static int
  678. rcu_torture_fakewriter(void *arg)
  679. {
  680. DEFINE_RCU_RANDOM(rand);
  681. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
  682. set_user_nice(current, 19);
  683. do {
  684. schedule_timeout_uninterruptible(1 + rcu_random(&rand)%10);
  685. udelay(rcu_random(&rand) & 0x3ff);
  686. cur_ops->sync();
  687. rcu_stutter_wait("rcu_torture_fakewriter");
  688. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  689. VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
  690. rcutorture_shutdown_absorb("rcu_torture_fakewriter");
  691. while (!kthread_should_stop())
  692. schedule_timeout_uninterruptible(1);
  693. return 0;
  694. }
  695. /*
  696. * RCU torture reader from timer handler. Dereferences rcu_torture_current,
  697. * incrementing the corresponding element of the pipeline array. The
  698. * counter in the element should never be greater than 1, otherwise, the
  699. * RCU implementation is broken.
  700. */
  701. static void rcu_torture_timer(unsigned long unused)
  702. {
  703. int idx;
  704. int completed;
  705. static DEFINE_RCU_RANDOM(rand);
  706. static DEFINE_SPINLOCK(rand_lock);
  707. struct rcu_torture *p;
  708. int pipe_count;
  709. idx = cur_ops->readlock();
  710. completed = cur_ops->completed();
  711. p = rcu_dereference_check(rcu_torture_current,
  712. rcu_read_lock_held() ||
  713. rcu_read_lock_bh_held() ||
  714. rcu_read_lock_sched_held() ||
  715. srcu_read_lock_held(&srcu_ctl));
  716. if (p == NULL) {
  717. /* Leave because rcu_torture_writer is not yet underway */
  718. cur_ops->readunlock(idx);
  719. return;
  720. }
  721. if (p->rtort_mbtest == 0)
  722. atomic_inc(&n_rcu_torture_mberror);
  723. spin_lock(&rand_lock);
  724. cur_ops->read_delay(&rand);
  725. n_rcu_torture_timers++;
  726. spin_unlock(&rand_lock);
  727. preempt_disable();
  728. pipe_count = p->rtort_pipe_count;
  729. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  730. /* Should not happen, but... */
  731. pipe_count = RCU_TORTURE_PIPE_LEN;
  732. }
  733. __this_cpu_inc(rcu_torture_count[pipe_count]);
  734. completed = cur_ops->completed() - completed;
  735. if (completed > RCU_TORTURE_PIPE_LEN) {
  736. /* Should not happen, but... */
  737. completed = RCU_TORTURE_PIPE_LEN;
  738. }
  739. __this_cpu_inc(rcu_torture_batch[completed]);
  740. preempt_enable();
  741. cur_ops->readunlock(idx);
  742. }
  743. /*
  744. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  745. * incrementing the corresponding element of the pipeline array. The
  746. * counter in the element should never be greater than 1, otherwise, the
  747. * RCU implementation is broken.
  748. */
  749. static int
  750. rcu_torture_reader(void *arg)
  751. {
  752. int completed;
  753. int idx;
  754. DEFINE_RCU_RANDOM(rand);
  755. struct rcu_torture *p;
  756. int pipe_count;
  757. struct timer_list t;
  758. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  759. set_user_nice(current, 19);
  760. if (irqreader && cur_ops->irq_capable)
  761. setup_timer_on_stack(&t, rcu_torture_timer, 0);
  762. do {
  763. if (irqreader && cur_ops->irq_capable) {
  764. if (!timer_pending(&t))
  765. mod_timer(&t, jiffies + 1);
  766. }
  767. idx = cur_ops->readlock();
  768. completed = cur_ops->completed();
  769. p = rcu_dereference_check(rcu_torture_current,
  770. rcu_read_lock_held() ||
  771. rcu_read_lock_bh_held() ||
  772. rcu_read_lock_sched_held() ||
  773. srcu_read_lock_held(&srcu_ctl));
  774. if (p == NULL) {
  775. /* Wait for rcu_torture_writer to get underway */
  776. cur_ops->readunlock(idx);
  777. schedule_timeout_interruptible(HZ);
  778. continue;
  779. }
  780. if (p->rtort_mbtest == 0)
  781. atomic_inc(&n_rcu_torture_mberror);
  782. cur_ops->read_delay(&rand);
  783. preempt_disable();
  784. pipe_count = p->rtort_pipe_count;
  785. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  786. /* Should not happen, but... */
  787. pipe_count = RCU_TORTURE_PIPE_LEN;
  788. }
  789. __this_cpu_inc(rcu_torture_count[pipe_count]);
  790. completed = cur_ops->completed() - completed;
  791. if (completed > RCU_TORTURE_PIPE_LEN) {
  792. /* Should not happen, but... */
  793. completed = RCU_TORTURE_PIPE_LEN;
  794. }
  795. __this_cpu_inc(rcu_torture_batch[completed]);
  796. preempt_enable();
  797. cur_ops->readunlock(idx);
  798. schedule();
  799. rcu_stutter_wait("rcu_torture_reader");
  800. } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
  801. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  802. rcutorture_shutdown_absorb("rcu_torture_reader");
  803. if (irqreader && cur_ops->irq_capable)
  804. del_timer_sync(&t);
  805. while (!kthread_should_stop())
  806. schedule_timeout_uninterruptible(1);
  807. return 0;
  808. }
  809. /*
  810. * Create an RCU-torture statistics message in the specified buffer.
  811. */
  812. static int
  813. rcu_torture_printk(char *page)
  814. {
  815. int cnt = 0;
  816. int cpu;
  817. int i;
  818. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  819. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  820. for_each_possible_cpu(cpu) {
  821. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  822. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  823. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  824. }
  825. }
  826. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  827. if (pipesummary[i] != 0)
  828. break;
  829. }
  830. cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
  831. cnt += sprintf(&page[cnt],
  832. "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
  833. "rtmbe: %d nt: %ld",
  834. rcu_torture_current,
  835. rcu_torture_current_version,
  836. list_empty(&rcu_torture_freelist),
  837. atomic_read(&n_rcu_torture_alloc),
  838. atomic_read(&n_rcu_torture_alloc_fail),
  839. atomic_read(&n_rcu_torture_free),
  840. atomic_read(&n_rcu_torture_mberror),
  841. n_rcu_torture_timers);
  842. if (atomic_read(&n_rcu_torture_mberror) != 0)
  843. cnt += sprintf(&page[cnt], " !!!");
  844. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  845. if (i > 1) {
  846. cnt += sprintf(&page[cnt], "!!! ");
  847. atomic_inc(&n_rcu_torture_error);
  848. WARN_ON_ONCE(1);
  849. }
  850. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  851. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  852. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  853. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  854. cnt += sprintf(&page[cnt], "Reader Batch: ");
  855. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  856. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  857. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  858. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  859. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  860. cnt += sprintf(&page[cnt], " %d",
  861. atomic_read(&rcu_torture_wcount[i]));
  862. }
  863. cnt += sprintf(&page[cnt], "\n");
  864. if (cur_ops->stats)
  865. cnt += cur_ops->stats(&page[cnt]);
  866. return cnt;
  867. }
  868. /*
  869. * Print torture statistics. Caller must ensure that there is only
  870. * one call to this function at a given time!!! This is normally
  871. * accomplished by relying on the module system to only have one copy
  872. * of the module loaded, and then by giving the rcu_torture_stats
  873. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  874. * thread is not running).
  875. */
  876. static void
  877. rcu_torture_stats_print(void)
  878. {
  879. int cnt;
  880. cnt = rcu_torture_printk(printk_buf);
  881. printk(KERN_ALERT "%s", printk_buf);
  882. }
  883. /*
  884. * Periodically prints torture statistics, if periodic statistics printing
  885. * was specified via the stat_interval module parameter.
  886. *
  887. * No need to worry about fullstop here, since this one doesn't reference
  888. * volatile state or register callbacks.
  889. */
  890. static int
  891. rcu_torture_stats(void *arg)
  892. {
  893. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  894. do {
  895. schedule_timeout_interruptible(stat_interval * HZ);
  896. rcu_torture_stats_print();
  897. rcutorture_shutdown_absorb("rcu_torture_stats");
  898. } while (!kthread_should_stop());
  899. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  900. return 0;
  901. }
  902. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  903. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  904. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  905. */
  906. static void rcu_torture_shuffle_tasks(void)
  907. {
  908. int i;
  909. cpumask_setall(shuffle_tmp_mask);
  910. get_online_cpus();
  911. /* No point in shuffling if there is only one online CPU (ex: UP) */
  912. if (num_online_cpus() == 1) {
  913. put_online_cpus();
  914. return;
  915. }
  916. if (rcu_idle_cpu != -1)
  917. cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
  918. set_cpus_allowed_ptr(current, shuffle_tmp_mask);
  919. if (reader_tasks) {
  920. for (i = 0; i < nrealreaders; i++)
  921. if (reader_tasks[i])
  922. set_cpus_allowed_ptr(reader_tasks[i],
  923. shuffle_tmp_mask);
  924. }
  925. if (fakewriter_tasks) {
  926. for (i = 0; i < nfakewriters; i++)
  927. if (fakewriter_tasks[i])
  928. set_cpus_allowed_ptr(fakewriter_tasks[i],
  929. shuffle_tmp_mask);
  930. }
  931. if (writer_task)
  932. set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
  933. if (stats_task)
  934. set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
  935. if (rcu_idle_cpu == -1)
  936. rcu_idle_cpu = num_online_cpus() - 1;
  937. else
  938. rcu_idle_cpu--;
  939. put_online_cpus();
  940. }
  941. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  942. * system to become idle at a time and cut off its timer ticks. This is meant
  943. * to test the support for such tickless idle CPU in RCU.
  944. */
  945. static int
  946. rcu_torture_shuffle(void *arg)
  947. {
  948. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  949. do {
  950. schedule_timeout_interruptible(shuffle_interval * HZ);
  951. rcu_torture_shuffle_tasks();
  952. rcutorture_shutdown_absorb("rcu_torture_shuffle");
  953. } while (!kthread_should_stop());
  954. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  955. return 0;
  956. }
  957. /* Cause the rcutorture test to "stutter", starting and stopping all
  958. * threads periodically.
  959. */
  960. static int
  961. rcu_torture_stutter(void *arg)
  962. {
  963. VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
  964. do {
  965. schedule_timeout_interruptible(stutter * HZ);
  966. stutter_pause_test = 1;
  967. if (!kthread_should_stop())
  968. schedule_timeout_interruptible(stutter * HZ);
  969. stutter_pause_test = 0;
  970. rcutorture_shutdown_absorb("rcu_torture_stutter");
  971. } while (!kthread_should_stop());
  972. VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
  973. return 0;
  974. }
  975. static inline void
  976. rcu_torture_print_module_parms(char *tag)
  977. {
  978. printk(KERN_ALERT "%s" TORTURE_FLAG
  979. "--- %s: nreaders=%d nfakewriters=%d "
  980. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  981. "shuffle_interval=%d stutter=%d irqreader=%d "
  982. "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d\n",
  983. torture_type, tag, nrealreaders, nfakewriters,
  984. stat_interval, verbose, test_no_idle_hz, shuffle_interval,
  985. stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter);
  986. }
  987. static struct notifier_block rcutorture_nb = {
  988. .notifier_call = rcutorture_shutdown_notify,
  989. };
  990. static void
  991. rcu_torture_cleanup(void)
  992. {
  993. int i;
  994. mutex_lock(&fullstop_mutex);
  995. if (fullstop == FULLSTOP_SHUTDOWN) {
  996. printk(KERN_WARNING /* but going down anyway, so... */
  997. "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
  998. mutex_unlock(&fullstop_mutex);
  999. schedule_timeout_uninterruptible(10);
  1000. if (cur_ops->cb_barrier != NULL)
  1001. cur_ops->cb_barrier();
  1002. return;
  1003. }
  1004. fullstop = FULLSTOP_RMMOD;
  1005. mutex_unlock(&fullstop_mutex);
  1006. unregister_reboot_notifier(&rcutorture_nb);
  1007. if (stutter_task) {
  1008. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
  1009. kthread_stop(stutter_task);
  1010. }
  1011. stutter_task = NULL;
  1012. if (shuffler_task) {
  1013. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  1014. kthread_stop(shuffler_task);
  1015. free_cpumask_var(shuffle_tmp_mask);
  1016. }
  1017. shuffler_task = NULL;
  1018. if (writer_task) {
  1019. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  1020. kthread_stop(writer_task);
  1021. }
  1022. writer_task = NULL;
  1023. if (reader_tasks) {
  1024. for (i = 0; i < nrealreaders; i++) {
  1025. if (reader_tasks[i]) {
  1026. VERBOSE_PRINTK_STRING(
  1027. "Stopping rcu_torture_reader task");
  1028. kthread_stop(reader_tasks[i]);
  1029. }
  1030. reader_tasks[i] = NULL;
  1031. }
  1032. kfree(reader_tasks);
  1033. reader_tasks = NULL;
  1034. }
  1035. rcu_torture_current = NULL;
  1036. if (fakewriter_tasks) {
  1037. for (i = 0; i < nfakewriters; i++) {
  1038. if (fakewriter_tasks[i]) {
  1039. VERBOSE_PRINTK_STRING(
  1040. "Stopping rcu_torture_fakewriter task");
  1041. kthread_stop(fakewriter_tasks[i]);
  1042. }
  1043. fakewriter_tasks[i] = NULL;
  1044. }
  1045. kfree(fakewriter_tasks);
  1046. fakewriter_tasks = NULL;
  1047. }
  1048. if (stats_task) {
  1049. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  1050. kthread_stop(stats_task);
  1051. }
  1052. stats_task = NULL;
  1053. if (fqs_task) {
  1054. VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
  1055. kthread_stop(fqs_task);
  1056. }
  1057. fqs_task = NULL;
  1058. /* Wait for all RCU callbacks to fire. */
  1059. if (cur_ops->cb_barrier != NULL)
  1060. cur_ops->cb_barrier();
  1061. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  1062. if (cur_ops->cleanup)
  1063. cur_ops->cleanup();
  1064. if (atomic_read(&n_rcu_torture_error))
  1065. rcu_torture_print_module_parms("End of test: FAILURE");
  1066. else
  1067. rcu_torture_print_module_parms("End of test: SUCCESS");
  1068. }
  1069. static int __init
  1070. rcu_torture_init(void)
  1071. {
  1072. int i;
  1073. int cpu;
  1074. int firsterr = 0;
  1075. static struct rcu_torture_ops *torture_ops[] =
  1076. { &rcu_ops, &rcu_sync_ops, &rcu_expedited_ops,
  1077. &rcu_bh_ops, &rcu_bh_sync_ops,
  1078. &srcu_ops, &srcu_expedited_ops,
  1079. &sched_ops, &sched_sync_ops, &sched_expedited_ops, };
  1080. mutex_lock(&fullstop_mutex);
  1081. /* Process args and tell the world that the torturer is on the job. */
  1082. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1083. cur_ops = torture_ops[i];
  1084. if (strcmp(torture_type, cur_ops->name) == 0)
  1085. break;
  1086. }
  1087. if (i == ARRAY_SIZE(torture_ops)) {
  1088. printk(KERN_ALERT "rcu-torture: invalid torture type: \"%s\"\n",
  1089. torture_type);
  1090. printk(KERN_ALERT "rcu-torture types:");
  1091. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1092. printk(KERN_ALERT " %s", torture_ops[i]->name);
  1093. printk(KERN_ALERT "\n");
  1094. mutex_unlock(&fullstop_mutex);
  1095. return -EINVAL;
  1096. }
  1097. if (cur_ops->fqs == NULL && fqs_duration != 0) {
  1098. printk(KERN_ALERT "rcu-torture: ->fqs NULL and non-zero "
  1099. "fqs_duration, fqs disabled.\n");
  1100. fqs_duration = 0;
  1101. }
  1102. if (cur_ops->init)
  1103. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  1104. if (nreaders >= 0)
  1105. nrealreaders = nreaders;
  1106. else
  1107. nrealreaders = 2 * num_online_cpus();
  1108. rcu_torture_print_module_parms("Start of test");
  1109. fullstop = FULLSTOP_DONTSTOP;
  1110. /* Set up the freelist. */
  1111. INIT_LIST_HEAD(&rcu_torture_freelist);
  1112. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  1113. rcu_tortures[i].rtort_mbtest = 0;
  1114. list_add_tail(&rcu_tortures[i].rtort_free,
  1115. &rcu_torture_freelist);
  1116. }
  1117. /* Initialize the statistics so that each run gets its own numbers. */
  1118. rcu_torture_current = NULL;
  1119. rcu_torture_current_version = 0;
  1120. atomic_set(&n_rcu_torture_alloc, 0);
  1121. atomic_set(&n_rcu_torture_alloc_fail, 0);
  1122. atomic_set(&n_rcu_torture_free, 0);
  1123. atomic_set(&n_rcu_torture_mberror, 0);
  1124. atomic_set(&n_rcu_torture_error, 0);
  1125. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  1126. atomic_set(&rcu_torture_wcount[i], 0);
  1127. for_each_possible_cpu(cpu) {
  1128. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  1129. per_cpu(rcu_torture_count, cpu)[i] = 0;
  1130. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  1131. }
  1132. }
  1133. /* Start up the kthreads. */
  1134. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  1135. writer_task = kthread_run(rcu_torture_writer, NULL,
  1136. "rcu_torture_writer");
  1137. if (IS_ERR(writer_task)) {
  1138. firsterr = PTR_ERR(writer_task);
  1139. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  1140. writer_task = NULL;
  1141. goto unwind;
  1142. }
  1143. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  1144. GFP_KERNEL);
  1145. if (fakewriter_tasks == NULL) {
  1146. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1147. firsterr = -ENOMEM;
  1148. goto unwind;
  1149. }
  1150. for (i = 0; i < nfakewriters; i++) {
  1151. VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
  1152. fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
  1153. "rcu_torture_fakewriter");
  1154. if (IS_ERR(fakewriter_tasks[i])) {
  1155. firsterr = PTR_ERR(fakewriter_tasks[i]);
  1156. VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
  1157. fakewriter_tasks[i] = NULL;
  1158. goto unwind;
  1159. }
  1160. }
  1161. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  1162. GFP_KERNEL);
  1163. if (reader_tasks == NULL) {
  1164. VERBOSE_PRINTK_ERRSTRING("out of memory");
  1165. firsterr = -ENOMEM;
  1166. goto unwind;
  1167. }
  1168. for (i = 0; i < nrealreaders; i++) {
  1169. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  1170. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  1171. "rcu_torture_reader");
  1172. if (IS_ERR(reader_tasks[i])) {
  1173. firsterr = PTR_ERR(reader_tasks[i]);
  1174. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  1175. reader_tasks[i] = NULL;
  1176. goto unwind;
  1177. }
  1178. }
  1179. if (stat_interval > 0) {
  1180. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  1181. stats_task = kthread_run(rcu_torture_stats, NULL,
  1182. "rcu_torture_stats");
  1183. if (IS_ERR(stats_task)) {
  1184. firsterr = PTR_ERR(stats_task);
  1185. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  1186. stats_task = NULL;
  1187. goto unwind;
  1188. }
  1189. }
  1190. if (test_no_idle_hz) {
  1191. rcu_idle_cpu = num_online_cpus() - 1;
  1192. if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  1193. firsterr = -ENOMEM;
  1194. VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
  1195. goto unwind;
  1196. }
  1197. /* Create the shuffler thread */
  1198. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  1199. "rcu_torture_shuffle");
  1200. if (IS_ERR(shuffler_task)) {
  1201. free_cpumask_var(shuffle_tmp_mask);
  1202. firsterr = PTR_ERR(shuffler_task);
  1203. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  1204. shuffler_task = NULL;
  1205. goto unwind;
  1206. }
  1207. }
  1208. if (stutter < 0)
  1209. stutter = 0;
  1210. if (stutter) {
  1211. /* Create the stutter thread */
  1212. stutter_task = kthread_run(rcu_torture_stutter, NULL,
  1213. "rcu_torture_stutter");
  1214. if (IS_ERR(stutter_task)) {
  1215. firsterr = PTR_ERR(stutter_task);
  1216. VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
  1217. stutter_task = NULL;
  1218. goto unwind;
  1219. }
  1220. }
  1221. if (fqs_duration < 0)
  1222. fqs_duration = 0;
  1223. if (fqs_duration) {
  1224. /* Create the stutter thread */
  1225. fqs_task = kthread_run(rcu_torture_fqs, NULL,
  1226. "rcu_torture_fqs");
  1227. if (IS_ERR(fqs_task)) {
  1228. firsterr = PTR_ERR(fqs_task);
  1229. VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
  1230. fqs_task = NULL;
  1231. goto unwind;
  1232. }
  1233. }
  1234. register_reboot_notifier(&rcutorture_nb);
  1235. mutex_unlock(&fullstop_mutex);
  1236. return 0;
  1237. unwind:
  1238. mutex_unlock(&fullstop_mutex);
  1239. rcu_torture_cleanup();
  1240. return firsterr;
  1241. }
  1242. module_init(rcu_torture_init);
  1243. module_exit(rcu_torture_cleanup);