rcutorture.c 31 KB

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