rcutorture.c 22 KB

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