rcutorture.c 27 KB

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