rcutorture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Read-Copy Update /proc-based torture test facility
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2005
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. *
  22. * See also: Documentation/RCU/torture.txt
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/kthread.h>
  29. #include <linux/err.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/smp.h>
  32. #include <linux/rcupdate.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/sched.h>
  35. #include <asm/atomic.h>
  36. #include <linux/bitops.h>
  37. #include <linux/module.h>
  38. #include <linux/completion.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/percpu.h>
  41. #include <linux/notifier.h>
  42. #include <linux/rcuref.h>
  43. #include <linux/cpu.h>
  44. #include <linux/random.h>
  45. #include <linux/delay.h>
  46. #include <linux/byteorder/swabb.h>
  47. #include <linux/stat.h>
  48. MODULE_LICENSE("GPL");
  49. static int nreaders = -1; /* # reader threads, defaults to 4*ncpus */
  50. static int stat_interval = 0; /* Interval between stats, in seconds. */
  51. /* Defaults to "only at end of test". */
  52. static int verbose = 0; /* Print more debug info. */
  53. MODULE_PARM(nreaders, "i");
  54. MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  55. MODULE_PARM(stat_interval, "i");
  56. MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  57. MODULE_PARM(verbose, "i");
  58. MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  59. #define TORTURE_FLAG "rcutorture: "
  60. #define PRINTK_STRING(s) \
  61. do { printk(KERN_ALERT TORTURE_FLAG s "\n"); } while (0)
  62. #define VERBOSE_PRINTK_STRING(s) \
  63. do { if (verbose) printk(KERN_ALERT TORTURE_FLAG s "\n"); } while (0)
  64. #define VERBOSE_PRINTK_ERRSTRING(s) \
  65. do { if (verbose) printk(KERN_ALERT TORTURE_FLAG "!!! " s "\n"); } while (0)
  66. static char printk_buf[4096];
  67. static int nrealreaders;
  68. static struct task_struct *writer_task;
  69. static struct task_struct **reader_tasks;
  70. static struct task_struct *stats_task;
  71. #define RCU_TORTURE_PIPE_LEN 10
  72. struct rcu_torture {
  73. struct rcu_head rtort_rcu;
  74. int rtort_pipe_count;
  75. struct list_head rtort_free;
  76. };
  77. static int fullstop = 0; /* stop generating callbacks at test end. */
  78. static LIST_HEAD(rcu_torture_freelist);
  79. static struct rcu_torture *rcu_torture_current = NULL;
  80. static long rcu_torture_current_version = 0;
  81. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  82. static DEFINE_SPINLOCK(rcu_torture_lock);
  83. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
  84. { 0 };
  85. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
  86. { 0 };
  87. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  88. atomic_t n_rcu_torture_alloc;
  89. atomic_t n_rcu_torture_alloc_fail;
  90. atomic_t n_rcu_torture_free;
  91. /*
  92. * Allocate an element from the rcu_tortures pool.
  93. */
  94. struct rcu_torture *
  95. rcu_torture_alloc(void)
  96. {
  97. struct list_head *p;
  98. spin_lock(&rcu_torture_lock);
  99. if (list_empty(&rcu_torture_freelist)) {
  100. atomic_inc(&n_rcu_torture_alloc_fail);
  101. spin_unlock(&rcu_torture_lock);
  102. return NULL;
  103. }
  104. atomic_inc(&n_rcu_torture_alloc);
  105. p = rcu_torture_freelist.next;
  106. list_del_init(p);
  107. spin_unlock(&rcu_torture_lock);
  108. return container_of(p, struct rcu_torture, rtort_free);
  109. }
  110. /*
  111. * Free an element to the rcu_tortures pool.
  112. */
  113. static void
  114. rcu_torture_free(struct rcu_torture *p)
  115. {
  116. atomic_inc(&n_rcu_torture_free);
  117. spin_lock(&rcu_torture_lock);
  118. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  119. spin_unlock(&rcu_torture_lock);
  120. }
  121. static void
  122. rcu_torture_cb(struct rcu_head *p)
  123. {
  124. int i;
  125. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  126. if (fullstop) {
  127. /* Test is ending, just drop callbacks on the floor. */
  128. /* The next initialization will pick up the pieces. */
  129. return;
  130. }
  131. i = rp->rtort_pipe_count;
  132. if (i > RCU_TORTURE_PIPE_LEN)
  133. i = RCU_TORTURE_PIPE_LEN;
  134. atomic_inc(&rcu_torture_wcount[i]);
  135. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN)
  136. rcu_torture_free(rp);
  137. else
  138. call_rcu(p, rcu_torture_cb);
  139. }
  140. struct rcu_random_state {
  141. unsigned long rrs_state;
  142. unsigned long rrs_count;
  143. };
  144. #define RCU_RANDOM_MULT 39916801 /* prime */
  145. #define RCU_RANDOM_ADD 479001701 /* prime */
  146. #define RCU_RANDOM_REFRESH 10000
  147. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  148. /*
  149. * Crude but fast random-number generator. Uses a linear congruential
  150. * generator, with occasional help from get_random_bytes().
  151. */
  152. static long
  153. rcu_random(struct rcu_random_state *rrsp)
  154. {
  155. long refresh;
  156. if (--rrsp->rrs_count < 0) {
  157. get_random_bytes(&refresh, sizeof(refresh));
  158. rrsp->rrs_state += refresh;
  159. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  160. }
  161. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  162. return swahw32(rrsp->rrs_state);
  163. }
  164. /*
  165. * RCU torture writer kthread. Repeatedly substitutes a new structure
  166. * for that pointed to by rcu_torture_current, freeing the old structure
  167. * after a series of grace periods (the "pipeline").
  168. */
  169. static int
  170. rcu_torture_writer(void *arg)
  171. {
  172. int i;
  173. long oldbatch = rcu_batches_completed();
  174. struct rcu_torture *rp;
  175. struct rcu_torture *old_rp;
  176. static DEFINE_RCU_RANDOM(rand);
  177. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  178. do {
  179. schedule_timeout_uninterruptible(1);
  180. if (rcu_batches_completed() == oldbatch)
  181. continue;
  182. if ((rp = rcu_torture_alloc()) == NULL)
  183. continue;
  184. rp->rtort_pipe_count = 0;
  185. udelay(rcu_random(&rand) & 0x3ff);
  186. old_rp = rcu_torture_current;
  187. rcu_assign_pointer(rcu_torture_current, rp);
  188. smp_wmb();
  189. if (old_rp != NULL) {
  190. i = old_rp->rtort_pipe_count;
  191. if (i > RCU_TORTURE_PIPE_LEN)
  192. i = RCU_TORTURE_PIPE_LEN;
  193. atomic_inc(&rcu_torture_wcount[i]);
  194. old_rp->rtort_pipe_count++;
  195. call_rcu(&old_rp->rtort_rcu, rcu_torture_cb);
  196. }
  197. rcu_torture_current_version++;
  198. oldbatch = rcu_batches_completed();
  199. } while (!kthread_should_stop() && !fullstop);
  200. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  201. while (!kthread_should_stop())
  202. schedule_timeout_uninterruptible(1);
  203. return 0;
  204. }
  205. /*
  206. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  207. * incrementing the corresponding element of the pipeline array. The
  208. * counter in the element should never be greater than 1, otherwise, the
  209. * RCU implementation is broken.
  210. */
  211. static int
  212. rcu_torture_reader(void *arg)
  213. {
  214. int completed;
  215. DEFINE_RCU_RANDOM(rand);
  216. struct rcu_torture *p;
  217. int pipe_count;
  218. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  219. do {
  220. rcu_read_lock();
  221. completed = rcu_batches_completed();
  222. p = rcu_dereference(rcu_torture_current);
  223. if (p == NULL) {
  224. /* Wait for rcu_torture_writer to get underway */
  225. rcu_read_unlock();
  226. schedule_timeout_interruptible(HZ);
  227. continue;
  228. }
  229. udelay(rcu_random(&rand) & 0x7f);
  230. preempt_disable();
  231. pipe_count = p->rtort_pipe_count;
  232. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  233. /* Should not happen, but... */
  234. pipe_count = RCU_TORTURE_PIPE_LEN;
  235. }
  236. ++__get_cpu_var(rcu_torture_count)[pipe_count];
  237. completed = rcu_batches_completed() - completed;
  238. if (completed > RCU_TORTURE_PIPE_LEN) {
  239. /* Should not happen, but... */
  240. completed = RCU_TORTURE_PIPE_LEN;
  241. }
  242. ++__get_cpu_var(rcu_torture_batch)[completed];
  243. preempt_enable();
  244. rcu_read_unlock();
  245. schedule();
  246. } while (!kthread_should_stop() && !fullstop);
  247. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  248. while (!kthread_should_stop())
  249. schedule_timeout_uninterruptible(1);
  250. return 0;
  251. }
  252. /*
  253. * Create an RCU-torture statistics message in the specified buffer.
  254. */
  255. static int
  256. rcu_torture_printk(char *page)
  257. {
  258. int cnt = 0;
  259. int cpu;
  260. int i;
  261. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  262. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  263. for_each_cpu(cpu) {
  264. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  265. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  266. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  267. }
  268. }
  269. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  270. if (pipesummary[i] != 0)
  271. break;
  272. }
  273. cnt += sprintf(&page[cnt], "rcutorture: ");
  274. cnt += sprintf(&page[cnt],
  275. "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d",
  276. rcu_torture_current,
  277. rcu_torture_current_version,
  278. list_empty(&rcu_torture_freelist),
  279. atomic_read(&n_rcu_torture_alloc),
  280. atomic_read(&n_rcu_torture_alloc_fail),
  281. atomic_read(&n_rcu_torture_free));
  282. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  283. if (i > 1)
  284. cnt += sprintf(&page[cnt], "!!! ");
  285. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  286. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  287. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  288. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  289. cnt += sprintf(&page[cnt], "Reader Batch: ");
  290. for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
  291. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  292. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  293. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  294. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  295. cnt += sprintf(&page[cnt], " %d",
  296. atomic_read(&rcu_torture_wcount[i]));
  297. }
  298. cnt += sprintf(&page[cnt], "\n");
  299. return cnt;
  300. }
  301. /*
  302. * Print torture statistics. Caller must ensure that there is only
  303. * one call to this function at a given time!!! This is normally
  304. * accomplished by relying on the module system to only have one copy
  305. * of the module loaded, and then by giving the rcu_torture_stats
  306. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  307. * thread is not running).
  308. */
  309. static void
  310. rcu_torture_stats_print(void)
  311. {
  312. int cnt;
  313. cnt = rcu_torture_printk(printk_buf);
  314. printk(KERN_ALERT "%s", printk_buf);
  315. }
  316. /*
  317. * Periodically prints torture statistics, if periodic statistics printing
  318. * was specified via the stat_interval module parameter.
  319. *
  320. * No need to worry about fullstop here, since this one doesn't reference
  321. * volatile state or register callbacks.
  322. */
  323. static int
  324. rcu_torture_stats(void *arg)
  325. {
  326. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  327. do {
  328. schedule_timeout_interruptible(stat_interval * HZ);
  329. rcu_torture_stats_print();
  330. } while (!kthread_should_stop());
  331. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  332. return 0;
  333. }
  334. static void
  335. rcu_torture_cleanup(void)
  336. {
  337. int i;
  338. fullstop = 1;
  339. if (writer_task != NULL) {
  340. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  341. kthread_stop(writer_task);
  342. }
  343. writer_task = NULL;
  344. if (reader_tasks != NULL) {
  345. for (i = 0; i < nrealreaders; i++) {
  346. if (reader_tasks[i] != NULL) {
  347. VERBOSE_PRINTK_STRING(
  348. "Stopping rcu_torture_reader task");
  349. kthread_stop(reader_tasks[i]);
  350. }
  351. reader_tasks[i] = NULL;
  352. }
  353. kfree(reader_tasks);
  354. reader_tasks = NULL;
  355. }
  356. rcu_torture_current = NULL;
  357. if (stats_task != NULL) {
  358. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  359. kthread_stop(stats_task);
  360. }
  361. stats_task = NULL;
  362. /* Wait for all RCU callbacks to fire. */
  363. for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
  364. synchronize_rcu();
  365. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  366. PRINTK_STRING("--- End of test");
  367. }
  368. static int
  369. rcu_torture_init(void)
  370. {
  371. int i;
  372. int cpu;
  373. int firsterr = 0;
  374. /* Process args and tell the world that the torturer is on the job. */
  375. if (nreaders >= 0)
  376. nrealreaders = nreaders;
  377. else
  378. nrealreaders = 2 * num_online_cpus();
  379. printk(KERN_ALERT TORTURE_FLAG
  380. "--- Start of test: nreaders=%d stat_interval=%d verbose=%d\n",
  381. nrealreaders, stat_interval, verbose);
  382. fullstop = 0;
  383. /* Set up the freelist. */
  384. INIT_LIST_HEAD(&rcu_torture_freelist);
  385. for (i = 0; i < sizeof(rcu_tortures) / sizeof(rcu_tortures[0]); i++) {
  386. list_add_tail(&rcu_tortures[i].rtort_free,
  387. &rcu_torture_freelist);
  388. }
  389. /* Initialize the statistics so that each run gets its own numbers. */
  390. rcu_torture_current = NULL;
  391. rcu_torture_current_version = 0;
  392. atomic_set(&n_rcu_torture_alloc, 0);
  393. atomic_set(&n_rcu_torture_alloc_fail, 0);
  394. atomic_set(&n_rcu_torture_free, 0);
  395. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  396. atomic_set(&rcu_torture_wcount[i], 0);
  397. for_each_cpu(cpu) {
  398. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  399. per_cpu(rcu_torture_count, cpu)[i] = 0;
  400. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  401. }
  402. }
  403. /* Start up the kthreads. */
  404. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  405. writer_task = kthread_run(rcu_torture_writer, NULL,
  406. "rcu_torture_writer");
  407. if (IS_ERR(writer_task)) {
  408. firsterr = PTR_ERR(writer_task);
  409. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  410. writer_task = NULL;
  411. goto unwind;
  412. }
  413. reader_tasks = kmalloc(nrealreaders * sizeof(reader_tasks[0]),
  414. GFP_KERNEL);
  415. if (reader_tasks == NULL) {
  416. VERBOSE_PRINTK_ERRSTRING("out of memory");
  417. firsterr = -ENOMEM;
  418. goto unwind;
  419. }
  420. for (i = 0; i < nrealreaders; i++) {
  421. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  422. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  423. "rcu_torture_reader");
  424. if (IS_ERR(reader_tasks[i])) {
  425. firsterr = PTR_ERR(reader_tasks[i]);
  426. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  427. reader_tasks[i] = NULL;
  428. goto unwind;
  429. }
  430. }
  431. if (stat_interval > 0) {
  432. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  433. stats_task = kthread_run(rcu_torture_stats, NULL,
  434. "rcu_torture_stats");
  435. if (IS_ERR(stats_task)) {
  436. firsterr = PTR_ERR(stats_task);
  437. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  438. stats_task = NULL;
  439. goto unwind;
  440. }
  441. }
  442. return 0;
  443. unwind:
  444. rcu_torture_cleanup();
  445. return firsterr;
  446. }
  447. module_init(rcu_torture_init);
  448. module_exit(rcu_torture_cleanup);