rcutorture.c 27 KB

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