rcutorture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. set_user_nice(current, 19);
  179. do {
  180. schedule_timeout_uninterruptible(1);
  181. if (rcu_batches_completed() == oldbatch)
  182. continue;
  183. if ((rp = rcu_torture_alloc()) == NULL)
  184. continue;
  185. rp->rtort_pipe_count = 0;
  186. udelay(rcu_random(&rand) & 0x3ff);
  187. old_rp = rcu_torture_current;
  188. rcu_assign_pointer(rcu_torture_current, rp);
  189. smp_wmb();
  190. if (old_rp != NULL) {
  191. i = old_rp->rtort_pipe_count;
  192. if (i > RCU_TORTURE_PIPE_LEN)
  193. i = RCU_TORTURE_PIPE_LEN;
  194. atomic_inc(&rcu_torture_wcount[i]);
  195. old_rp->rtort_pipe_count++;
  196. call_rcu(&old_rp->rtort_rcu, rcu_torture_cb);
  197. }
  198. rcu_torture_current_version++;
  199. oldbatch = rcu_batches_completed();
  200. } while (!kthread_should_stop() && !fullstop);
  201. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  202. while (!kthread_should_stop())
  203. schedule_timeout_uninterruptible(1);
  204. return 0;
  205. }
  206. /*
  207. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  208. * incrementing the corresponding element of the pipeline array. The
  209. * counter in the element should never be greater than 1, otherwise, the
  210. * RCU implementation is broken.
  211. */
  212. static int
  213. rcu_torture_reader(void *arg)
  214. {
  215. int completed;
  216. DEFINE_RCU_RANDOM(rand);
  217. struct rcu_torture *p;
  218. int pipe_count;
  219. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  220. set_user_nice(current, 19);
  221. do {
  222. rcu_read_lock();
  223. completed = rcu_batches_completed();
  224. p = rcu_dereference(rcu_torture_current);
  225. if (p == NULL) {
  226. /* Wait for rcu_torture_writer to get underway */
  227. rcu_read_unlock();
  228. schedule_timeout_interruptible(HZ);
  229. continue;
  230. }
  231. udelay(rcu_random(&rand) & 0x7f);
  232. preempt_disable();
  233. pipe_count = p->rtort_pipe_count;
  234. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  235. /* Should not happen, but... */
  236. pipe_count = RCU_TORTURE_PIPE_LEN;
  237. }
  238. ++__get_cpu_var(rcu_torture_count)[pipe_count];
  239. completed = rcu_batches_completed() - completed;
  240. if (completed > RCU_TORTURE_PIPE_LEN) {
  241. /* Should not happen, but... */
  242. completed = RCU_TORTURE_PIPE_LEN;
  243. }
  244. ++__get_cpu_var(rcu_torture_batch)[completed];
  245. preempt_enable();
  246. rcu_read_unlock();
  247. schedule();
  248. } while (!kthread_should_stop() && !fullstop);
  249. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  250. while (!kthread_should_stop())
  251. schedule_timeout_uninterruptible(1);
  252. return 0;
  253. }
  254. /*
  255. * Create an RCU-torture statistics message in the specified buffer.
  256. */
  257. static int
  258. rcu_torture_printk(char *page)
  259. {
  260. int cnt = 0;
  261. int cpu;
  262. int i;
  263. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  264. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  265. for_each_cpu(cpu) {
  266. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  267. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  268. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  269. }
  270. }
  271. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  272. if (pipesummary[i] != 0)
  273. break;
  274. }
  275. cnt += sprintf(&page[cnt], "rcutorture: ");
  276. cnt += sprintf(&page[cnt],
  277. "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d",
  278. rcu_torture_current,
  279. rcu_torture_current_version,
  280. list_empty(&rcu_torture_freelist),
  281. atomic_read(&n_rcu_torture_alloc),
  282. atomic_read(&n_rcu_torture_alloc_fail),
  283. atomic_read(&n_rcu_torture_free));
  284. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  285. if (i > 1)
  286. cnt += sprintf(&page[cnt], "!!! ");
  287. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  288. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  289. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  290. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  291. cnt += sprintf(&page[cnt], "Reader Batch: ");
  292. for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
  293. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  294. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  295. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  296. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  297. cnt += sprintf(&page[cnt], " %d",
  298. atomic_read(&rcu_torture_wcount[i]));
  299. }
  300. cnt += sprintf(&page[cnt], "\n");
  301. return cnt;
  302. }
  303. /*
  304. * Print torture statistics. Caller must ensure that there is only
  305. * one call to this function at a given time!!! This is normally
  306. * accomplished by relying on the module system to only have one copy
  307. * of the module loaded, and then by giving the rcu_torture_stats
  308. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  309. * thread is not running).
  310. */
  311. static void
  312. rcu_torture_stats_print(void)
  313. {
  314. int cnt;
  315. cnt = rcu_torture_printk(printk_buf);
  316. printk(KERN_ALERT "%s", printk_buf);
  317. }
  318. /*
  319. * Periodically prints torture statistics, if periodic statistics printing
  320. * was specified via the stat_interval module parameter.
  321. *
  322. * No need to worry about fullstop here, since this one doesn't reference
  323. * volatile state or register callbacks.
  324. */
  325. static int
  326. rcu_torture_stats(void *arg)
  327. {
  328. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  329. do {
  330. schedule_timeout_interruptible(stat_interval * HZ);
  331. rcu_torture_stats_print();
  332. } while (!kthread_should_stop());
  333. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  334. return 0;
  335. }
  336. static void
  337. rcu_torture_cleanup(void)
  338. {
  339. int i;
  340. fullstop = 1;
  341. if (writer_task != NULL) {
  342. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  343. kthread_stop(writer_task);
  344. }
  345. writer_task = NULL;
  346. if (reader_tasks != NULL) {
  347. for (i = 0; i < nrealreaders; i++) {
  348. if (reader_tasks[i] != NULL) {
  349. VERBOSE_PRINTK_STRING(
  350. "Stopping rcu_torture_reader task");
  351. kthread_stop(reader_tasks[i]);
  352. }
  353. reader_tasks[i] = NULL;
  354. }
  355. kfree(reader_tasks);
  356. reader_tasks = NULL;
  357. }
  358. rcu_torture_current = NULL;
  359. if (stats_task != NULL) {
  360. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  361. kthread_stop(stats_task);
  362. }
  363. stats_task = NULL;
  364. /* Wait for all RCU callbacks to fire. */
  365. for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
  366. synchronize_rcu();
  367. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  368. PRINTK_STRING("--- End of test");
  369. }
  370. static int
  371. rcu_torture_init(void)
  372. {
  373. int i;
  374. int cpu;
  375. int firsterr = 0;
  376. /* Process args and tell the world that the torturer is on the job. */
  377. if (nreaders >= 0)
  378. nrealreaders = nreaders;
  379. else
  380. nrealreaders = 2 * num_online_cpus();
  381. printk(KERN_ALERT TORTURE_FLAG
  382. "--- Start of test: nreaders=%d stat_interval=%d verbose=%d\n",
  383. nrealreaders, stat_interval, verbose);
  384. fullstop = 0;
  385. /* Set up the freelist. */
  386. INIT_LIST_HEAD(&rcu_torture_freelist);
  387. for (i = 0; i < sizeof(rcu_tortures) / sizeof(rcu_tortures[0]); i++) {
  388. list_add_tail(&rcu_tortures[i].rtort_free,
  389. &rcu_torture_freelist);
  390. }
  391. /* Initialize the statistics so that each run gets its own numbers. */
  392. rcu_torture_current = NULL;
  393. rcu_torture_current_version = 0;
  394. atomic_set(&n_rcu_torture_alloc, 0);
  395. atomic_set(&n_rcu_torture_alloc_fail, 0);
  396. atomic_set(&n_rcu_torture_free, 0);
  397. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  398. atomic_set(&rcu_torture_wcount[i], 0);
  399. for_each_cpu(cpu) {
  400. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  401. per_cpu(rcu_torture_count, cpu)[i] = 0;
  402. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  403. }
  404. }
  405. /* Start up the kthreads. */
  406. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  407. writer_task = kthread_run(rcu_torture_writer, NULL,
  408. "rcu_torture_writer");
  409. if (IS_ERR(writer_task)) {
  410. firsterr = PTR_ERR(writer_task);
  411. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  412. writer_task = NULL;
  413. goto unwind;
  414. }
  415. reader_tasks = kmalloc(nrealreaders * sizeof(reader_tasks[0]),
  416. GFP_KERNEL);
  417. if (reader_tasks == NULL) {
  418. VERBOSE_PRINTK_ERRSTRING("out of memory");
  419. firsterr = -ENOMEM;
  420. goto unwind;
  421. }
  422. for (i = 0; i < nrealreaders; i++) {
  423. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  424. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  425. "rcu_torture_reader");
  426. if (IS_ERR(reader_tasks[i])) {
  427. firsterr = PTR_ERR(reader_tasks[i]);
  428. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  429. reader_tasks[i] = NULL;
  430. goto unwind;
  431. }
  432. }
  433. if (stat_interval > 0) {
  434. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  435. stats_task = kthread_run(rcu_torture_stats, NULL,
  436. "rcu_torture_stats");
  437. if (IS_ERR(stats_task)) {
  438. firsterr = PTR_ERR(stats_task);
  439. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  440. stats_task = NULL;
  441. goto unwind;
  442. }
  443. }
  444. return 0;
  445. unwind:
  446. rcu_torture_cleanup();
  447. return firsterr;
  448. }
  449. module_init(rcu_torture_init);
  450. module_exit(rcu_torture_cleanup);