rcutorture.c 27 KB

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