rcutorture.c 29 KB

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