rcutorture.c 22 KB

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