rcutorture.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. WARN_ON_ONCE(1);
  612. }
  613. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  614. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  615. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  616. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  617. cnt += sprintf(&page[cnt], "Reader Batch: ");
  618. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  619. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  620. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  621. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  622. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  623. cnt += sprintf(&page[cnt], " %d",
  624. atomic_read(&rcu_torture_wcount[i]));
  625. }
  626. cnt += sprintf(&page[cnt], "\n");
  627. if (cur_ops->stats)
  628. cnt += cur_ops->stats(&page[cnt]);
  629. return cnt;
  630. }
  631. /*
  632. * Print torture statistics. Caller must ensure that there is only
  633. * one call to this function at a given time!!! This is normally
  634. * accomplished by relying on the module system to only have one copy
  635. * of the module loaded, and then by giving the rcu_torture_stats
  636. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  637. * thread is not running).
  638. */
  639. static void
  640. rcu_torture_stats_print(void)
  641. {
  642. int cnt;
  643. cnt = rcu_torture_printk(printk_buf);
  644. printk(KERN_ALERT "%s", printk_buf);
  645. }
  646. /*
  647. * Periodically prints torture statistics, if periodic statistics printing
  648. * was specified via the stat_interval module parameter.
  649. *
  650. * No need to worry about fullstop here, since this one doesn't reference
  651. * volatile state or register callbacks.
  652. */
  653. static int
  654. rcu_torture_stats(void *arg)
  655. {
  656. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  657. do {
  658. schedule_timeout_interruptible(stat_interval * HZ);
  659. rcu_torture_stats_print();
  660. } while (!kthread_should_stop());
  661. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  662. return 0;
  663. }
  664. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  665. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  666. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  667. */
  668. static void rcu_torture_shuffle_tasks(void)
  669. {
  670. cpumask_t tmp_mask;
  671. int i;
  672. cpus_setall(tmp_mask);
  673. get_online_cpus();
  674. /* No point in shuffling if there is only one online CPU (ex: UP) */
  675. if (num_online_cpus() == 1) {
  676. put_online_cpus();
  677. return;
  678. }
  679. if (rcu_idle_cpu != -1)
  680. cpu_clear(rcu_idle_cpu, tmp_mask);
  681. set_cpus_allowed_ptr(current, &tmp_mask);
  682. if (reader_tasks) {
  683. for (i = 0; i < nrealreaders; i++)
  684. if (reader_tasks[i])
  685. set_cpus_allowed_ptr(reader_tasks[i],
  686. &tmp_mask);
  687. }
  688. if (fakewriter_tasks) {
  689. for (i = 0; i < nfakewriters; i++)
  690. if (fakewriter_tasks[i])
  691. set_cpus_allowed_ptr(fakewriter_tasks[i],
  692. &tmp_mask);
  693. }
  694. if (writer_task)
  695. set_cpus_allowed_ptr(writer_task, &tmp_mask);
  696. if (stats_task)
  697. set_cpus_allowed_ptr(stats_task, &tmp_mask);
  698. if (rcu_idle_cpu == -1)
  699. rcu_idle_cpu = num_online_cpus() - 1;
  700. else
  701. rcu_idle_cpu--;
  702. put_online_cpus();
  703. }
  704. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  705. * system to become idle at a time and cut off its timer ticks. This is meant
  706. * to test the support for such tickless idle CPU in RCU.
  707. */
  708. static int
  709. rcu_torture_shuffle(void *arg)
  710. {
  711. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  712. do {
  713. schedule_timeout_interruptible(shuffle_interval * HZ);
  714. rcu_torture_shuffle_tasks();
  715. } while (!kthread_should_stop());
  716. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  717. return 0;
  718. }
  719. static inline void
  720. rcu_torture_print_module_parms(char *tag)
  721. {
  722. printk(KERN_ALERT "%s" TORTURE_FLAG
  723. "--- %s: nreaders=%d nfakewriters=%d "
  724. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  725. "shuffle_interval = %d\n",
  726. torture_type, tag, nrealreaders, nfakewriters,
  727. stat_interval, verbose, test_no_idle_hz, shuffle_interval);
  728. }
  729. static void
  730. rcu_torture_cleanup(void)
  731. {
  732. int i;
  733. fullstop = 1;
  734. if (shuffler_task) {
  735. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  736. kthread_stop(shuffler_task);
  737. }
  738. shuffler_task = NULL;
  739. if (writer_task) {
  740. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  741. kthread_stop(writer_task);
  742. }
  743. writer_task = NULL;
  744. if (reader_tasks) {
  745. for (i = 0; i < nrealreaders; i++) {
  746. if (reader_tasks[i]) {
  747. VERBOSE_PRINTK_STRING(
  748. "Stopping rcu_torture_reader task");
  749. kthread_stop(reader_tasks[i]);
  750. }
  751. reader_tasks[i] = NULL;
  752. }
  753. kfree(reader_tasks);
  754. reader_tasks = NULL;
  755. }
  756. rcu_torture_current = NULL;
  757. if (fakewriter_tasks) {
  758. for (i = 0; i < nfakewriters; i++) {
  759. if (fakewriter_tasks[i]) {
  760. VERBOSE_PRINTK_STRING(
  761. "Stopping rcu_torture_fakewriter task");
  762. kthread_stop(fakewriter_tasks[i]);
  763. }
  764. fakewriter_tasks[i] = NULL;
  765. }
  766. kfree(fakewriter_tasks);
  767. fakewriter_tasks = NULL;
  768. }
  769. if (stats_task) {
  770. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  771. kthread_stop(stats_task);
  772. }
  773. stats_task = NULL;
  774. /* Wait for all RCU callbacks to fire. */
  775. if (cur_ops->cb_barrier != NULL)
  776. cur_ops->cb_barrier();
  777. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  778. if (cur_ops->cleanup)
  779. cur_ops->cleanup();
  780. if (atomic_read(&n_rcu_torture_error))
  781. rcu_torture_print_module_parms("End of test: FAILURE");
  782. else
  783. rcu_torture_print_module_parms("End of test: SUCCESS");
  784. }
  785. static int __init
  786. rcu_torture_init(void)
  787. {
  788. int i;
  789. int cpu;
  790. int firsterr = 0;
  791. static struct rcu_torture_ops *torture_ops[] =
  792. { &rcu_ops, &rcu_sync_ops, &rcu_bh_ops, &rcu_bh_sync_ops,
  793. &srcu_ops, &sched_ops, &sched_ops_sync, };
  794. /* Process args and tell the world that the torturer is on the job. */
  795. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  796. cur_ops = torture_ops[i];
  797. if (strcmp(torture_type, cur_ops->name) == 0)
  798. break;
  799. }
  800. if (i == ARRAY_SIZE(torture_ops)) {
  801. printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
  802. torture_type);
  803. return (-EINVAL);
  804. }
  805. if (cur_ops->init)
  806. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  807. if (nreaders >= 0)
  808. nrealreaders = nreaders;
  809. else
  810. nrealreaders = 2 * num_online_cpus();
  811. rcu_torture_print_module_parms("Start of test");
  812. fullstop = 0;
  813. /* Set up the freelist. */
  814. INIT_LIST_HEAD(&rcu_torture_freelist);
  815. for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
  816. rcu_tortures[i].rtort_mbtest = 0;
  817. list_add_tail(&rcu_tortures[i].rtort_free,
  818. &rcu_torture_freelist);
  819. }
  820. /* Initialize the statistics so that each run gets its own numbers. */
  821. rcu_torture_current = NULL;
  822. rcu_torture_current_version = 0;
  823. atomic_set(&n_rcu_torture_alloc, 0);
  824. atomic_set(&n_rcu_torture_alloc_fail, 0);
  825. atomic_set(&n_rcu_torture_free, 0);
  826. atomic_set(&n_rcu_torture_mberror, 0);
  827. atomic_set(&n_rcu_torture_error, 0);
  828. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  829. atomic_set(&rcu_torture_wcount[i], 0);
  830. for_each_possible_cpu(cpu) {
  831. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  832. per_cpu(rcu_torture_count, cpu)[i] = 0;
  833. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  834. }
  835. }
  836. /* Start up the kthreads. */
  837. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  838. writer_task = kthread_run(rcu_torture_writer, NULL,
  839. "rcu_torture_writer");
  840. if (IS_ERR(writer_task)) {
  841. firsterr = PTR_ERR(writer_task);
  842. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  843. writer_task = NULL;
  844. goto unwind;
  845. }
  846. fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
  847. GFP_KERNEL);
  848. if (fakewriter_tasks == NULL) {
  849. VERBOSE_PRINTK_ERRSTRING("out of memory");
  850. firsterr = -ENOMEM;
  851. goto unwind;
  852. }
  853. for (i = 0; i < nfakewriters; i++) {
  854. VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
  855. fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
  856. "rcu_torture_fakewriter");
  857. if (IS_ERR(fakewriter_tasks[i])) {
  858. firsterr = PTR_ERR(fakewriter_tasks[i]);
  859. VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
  860. fakewriter_tasks[i] = NULL;
  861. goto unwind;
  862. }
  863. }
  864. reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
  865. GFP_KERNEL);
  866. if (reader_tasks == NULL) {
  867. VERBOSE_PRINTK_ERRSTRING("out of memory");
  868. firsterr = -ENOMEM;
  869. goto unwind;
  870. }
  871. for (i = 0; i < nrealreaders; i++) {
  872. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  873. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  874. "rcu_torture_reader");
  875. if (IS_ERR(reader_tasks[i])) {
  876. firsterr = PTR_ERR(reader_tasks[i]);
  877. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  878. reader_tasks[i] = NULL;
  879. goto unwind;
  880. }
  881. }
  882. if (stat_interval > 0) {
  883. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  884. stats_task = kthread_run(rcu_torture_stats, NULL,
  885. "rcu_torture_stats");
  886. if (IS_ERR(stats_task)) {
  887. firsterr = PTR_ERR(stats_task);
  888. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  889. stats_task = NULL;
  890. goto unwind;
  891. }
  892. }
  893. if (test_no_idle_hz) {
  894. rcu_idle_cpu = num_online_cpus() - 1;
  895. /* Create the shuffler thread */
  896. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  897. "rcu_torture_shuffle");
  898. if (IS_ERR(shuffler_task)) {
  899. firsterr = PTR_ERR(shuffler_task);
  900. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  901. shuffler_task = NULL;
  902. goto unwind;
  903. }
  904. }
  905. return 0;
  906. unwind:
  907. rcu_torture_cleanup();
  908. return firsterr;
  909. }
  910. module_init(rcu_torture_init);
  911. module_exit(rcu_torture_cleanup);