rcutorture.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. int rtort_mbtest;
  77. };
  78. static int fullstop = 0; /* stop generating callbacks at test end. */
  79. static LIST_HEAD(rcu_torture_freelist);
  80. static struct rcu_torture *rcu_torture_current = NULL;
  81. static long rcu_torture_current_version = 0;
  82. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  83. static DEFINE_SPINLOCK(rcu_torture_lock);
  84. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
  85. { 0 };
  86. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
  87. { 0 };
  88. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  89. atomic_t n_rcu_torture_alloc;
  90. atomic_t n_rcu_torture_alloc_fail;
  91. atomic_t n_rcu_torture_free;
  92. atomic_t n_rcu_torture_mberror;
  93. atomic_t n_rcu_torture_error;
  94. /*
  95. * Allocate an element from the rcu_tortures pool.
  96. */
  97. struct rcu_torture *
  98. rcu_torture_alloc(void)
  99. {
  100. struct list_head *p;
  101. spin_lock(&rcu_torture_lock);
  102. if (list_empty(&rcu_torture_freelist)) {
  103. atomic_inc(&n_rcu_torture_alloc_fail);
  104. spin_unlock(&rcu_torture_lock);
  105. return NULL;
  106. }
  107. atomic_inc(&n_rcu_torture_alloc);
  108. p = rcu_torture_freelist.next;
  109. list_del_init(p);
  110. spin_unlock(&rcu_torture_lock);
  111. return container_of(p, struct rcu_torture, rtort_free);
  112. }
  113. /*
  114. * Free an element to the rcu_tortures pool.
  115. */
  116. static void
  117. rcu_torture_free(struct rcu_torture *p)
  118. {
  119. atomic_inc(&n_rcu_torture_free);
  120. spin_lock(&rcu_torture_lock);
  121. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  122. spin_unlock(&rcu_torture_lock);
  123. }
  124. static void
  125. rcu_torture_cb(struct rcu_head *p)
  126. {
  127. int i;
  128. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  129. if (fullstop) {
  130. /* Test is ending, just drop callbacks on the floor. */
  131. /* The next initialization will pick up the pieces. */
  132. return;
  133. }
  134. i = rp->rtort_pipe_count;
  135. if (i > RCU_TORTURE_PIPE_LEN)
  136. i = RCU_TORTURE_PIPE_LEN;
  137. atomic_inc(&rcu_torture_wcount[i]);
  138. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  139. rp->rtort_mbtest = 0;
  140. rcu_torture_free(rp);
  141. } else
  142. call_rcu(p, rcu_torture_cb);
  143. }
  144. struct rcu_random_state {
  145. unsigned long rrs_state;
  146. unsigned long rrs_count;
  147. };
  148. #define RCU_RANDOM_MULT 39916801 /* prime */
  149. #define RCU_RANDOM_ADD 479001701 /* prime */
  150. #define RCU_RANDOM_REFRESH 10000
  151. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  152. /*
  153. * Crude but fast random-number generator. Uses a linear congruential
  154. * generator, with occasional help from get_random_bytes().
  155. */
  156. static long
  157. rcu_random(struct rcu_random_state *rrsp)
  158. {
  159. long refresh;
  160. if (--rrsp->rrs_count < 0) {
  161. get_random_bytes(&refresh, sizeof(refresh));
  162. rrsp->rrs_state += refresh;
  163. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  164. }
  165. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  166. return swahw32(rrsp->rrs_state);
  167. }
  168. /*
  169. * RCU torture writer kthread. Repeatedly substitutes a new structure
  170. * for that pointed to by rcu_torture_current, freeing the old structure
  171. * after a series of grace periods (the "pipeline").
  172. */
  173. static int
  174. rcu_torture_writer(void *arg)
  175. {
  176. int i;
  177. long oldbatch = rcu_batches_completed();
  178. struct rcu_torture *rp;
  179. struct rcu_torture *old_rp;
  180. static DEFINE_RCU_RANDOM(rand);
  181. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  182. set_user_nice(current, 19);
  183. do {
  184. schedule_timeout_uninterruptible(1);
  185. if (rcu_batches_completed() == oldbatch)
  186. continue;
  187. if ((rp = rcu_torture_alloc()) == NULL)
  188. continue;
  189. rp->rtort_pipe_count = 0;
  190. udelay(rcu_random(&rand) & 0x3ff);
  191. old_rp = rcu_torture_current;
  192. rp->rtort_mbtest = 1;
  193. rcu_assign_pointer(rcu_torture_current, rp);
  194. smp_wmb();
  195. if (old_rp != NULL) {
  196. i = old_rp->rtort_pipe_count;
  197. if (i > RCU_TORTURE_PIPE_LEN)
  198. i = RCU_TORTURE_PIPE_LEN;
  199. atomic_inc(&rcu_torture_wcount[i]);
  200. old_rp->rtort_pipe_count++;
  201. call_rcu(&old_rp->rtort_rcu, rcu_torture_cb);
  202. }
  203. rcu_torture_current_version++;
  204. oldbatch = rcu_batches_completed();
  205. } while (!kthread_should_stop() && !fullstop);
  206. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  207. while (!kthread_should_stop())
  208. schedule_timeout_uninterruptible(1);
  209. return 0;
  210. }
  211. /*
  212. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  213. * incrementing the corresponding element of the pipeline array. The
  214. * counter in the element should never be greater than 1, otherwise, the
  215. * RCU implementation is broken.
  216. */
  217. static int
  218. rcu_torture_reader(void *arg)
  219. {
  220. int completed;
  221. DEFINE_RCU_RANDOM(rand);
  222. struct rcu_torture *p;
  223. int pipe_count;
  224. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  225. set_user_nice(current, 19);
  226. do {
  227. rcu_read_lock();
  228. completed = rcu_batches_completed();
  229. p = rcu_dereference(rcu_torture_current);
  230. if (p == NULL) {
  231. /* Wait for rcu_torture_writer to get underway */
  232. rcu_read_unlock();
  233. schedule_timeout_interruptible(HZ);
  234. continue;
  235. }
  236. if (p->rtort_mbtest == 0)
  237. atomic_inc(&n_rcu_torture_mberror);
  238. udelay(rcu_random(&rand) & 0x7f);
  239. preempt_disable();
  240. pipe_count = p->rtort_pipe_count;
  241. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  242. /* Should not happen, but... */
  243. pipe_count = RCU_TORTURE_PIPE_LEN;
  244. }
  245. ++__get_cpu_var(rcu_torture_count)[pipe_count];
  246. completed = rcu_batches_completed() - completed;
  247. if (completed > RCU_TORTURE_PIPE_LEN) {
  248. /* Should not happen, but... */
  249. completed = RCU_TORTURE_PIPE_LEN;
  250. }
  251. ++__get_cpu_var(rcu_torture_batch)[completed];
  252. preempt_enable();
  253. rcu_read_unlock();
  254. schedule();
  255. } while (!kthread_should_stop() && !fullstop);
  256. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  257. while (!kthread_should_stop())
  258. schedule_timeout_uninterruptible(1);
  259. return 0;
  260. }
  261. /*
  262. * Create an RCU-torture statistics message in the specified buffer.
  263. */
  264. static int
  265. rcu_torture_printk(char *page)
  266. {
  267. int cnt = 0;
  268. int cpu;
  269. int i;
  270. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  271. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  272. for_each_cpu(cpu) {
  273. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  274. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  275. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  276. }
  277. }
  278. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  279. if (pipesummary[i] != 0)
  280. break;
  281. }
  282. cnt += sprintf(&page[cnt], "rcutorture: ");
  283. cnt += sprintf(&page[cnt],
  284. "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
  285. "rtmbe: %d",
  286. rcu_torture_current,
  287. rcu_torture_current_version,
  288. list_empty(&rcu_torture_freelist),
  289. atomic_read(&n_rcu_torture_alloc),
  290. atomic_read(&n_rcu_torture_alloc_fail),
  291. atomic_read(&n_rcu_torture_free),
  292. atomic_read(&n_rcu_torture_mberror));
  293. if (atomic_read(&n_rcu_torture_mberror) != 0)
  294. cnt += sprintf(&page[cnt], " !!!");
  295. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  296. if (i > 1) {
  297. cnt += sprintf(&page[cnt], "!!! ");
  298. atomic_inc(&n_rcu_torture_error);
  299. }
  300. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  301. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  302. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  303. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  304. cnt += sprintf(&page[cnt], "Reader Batch: ");
  305. for (i = 0; i < RCU_TORTURE_PIPE_LEN; i++)
  306. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  307. cnt += sprintf(&page[cnt], "\nrcutorture: ");
  308. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  309. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  310. cnt += sprintf(&page[cnt], " %d",
  311. atomic_read(&rcu_torture_wcount[i]));
  312. }
  313. cnt += sprintf(&page[cnt], "\n");
  314. return cnt;
  315. }
  316. /*
  317. * Print torture statistics. Caller must ensure that there is only
  318. * one call to this function at a given time!!! This is normally
  319. * accomplished by relying on the module system to only have one copy
  320. * of the module loaded, and then by giving the rcu_torture_stats
  321. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  322. * thread is not running).
  323. */
  324. static void
  325. rcu_torture_stats_print(void)
  326. {
  327. int cnt;
  328. cnt = rcu_torture_printk(printk_buf);
  329. printk(KERN_ALERT "%s", printk_buf);
  330. }
  331. /*
  332. * Periodically prints torture statistics, if periodic statistics printing
  333. * was specified via the stat_interval module parameter.
  334. *
  335. * No need to worry about fullstop here, since this one doesn't reference
  336. * volatile state or register callbacks.
  337. */
  338. static int
  339. rcu_torture_stats(void *arg)
  340. {
  341. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  342. do {
  343. schedule_timeout_interruptible(stat_interval * HZ);
  344. rcu_torture_stats_print();
  345. } while (!kthread_should_stop());
  346. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  347. return 0;
  348. }
  349. static void
  350. rcu_torture_cleanup(void)
  351. {
  352. int i;
  353. fullstop = 1;
  354. if (writer_task != NULL) {
  355. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  356. kthread_stop(writer_task);
  357. }
  358. writer_task = NULL;
  359. if (reader_tasks != NULL) {
  360. for (i = 0; i < nrealreaders; i++) {
  361. if (reader_tasks[i] != NULL) {
  362. VERBOSE_PRINTK_STRING(
  363. "Stopping rcu_torture_reader task");
  364. kthread_stop(reader_tasks[i]);
  365. }
  366. reader_tasks[i] = NULL;
  367. }
  368. kfree(reader_tasks);
  369. reader_tasks = NULL;
  370. }
  371. rcu_torture_current = NULL;
  372. if (stats_task != NULL) {
  373. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  374. kthread_stop(stats_task);
  375. }
  376. stats_task = NULL;
  377. /* Wait for all RCU callbacks to fire. */
  378. rcu_barrier();
  379. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  380. printk(KERN_ALERT TORTURE_FLAG
  381. "--- End of test: %s\n",
  382. atomic_read(&n_rcu_torture_error) == 0 ? "SUCCESS" : "FAILURE");
  383. }
  384. static int
  385. rcu_torture_init(void)
  386. {
  387. int i;
  388. int cpu;
  389. int firsterr = 0;
  390. /* Process args and tell the world that the torturer is on the job. */
  391. if (nreaders >= 0)
  392. nrealreaders = nreaders;
  393. else
  394. nrealreaders = 2 * num_online_cpus();
  395. printk(KERN_ALERT TORTURE_FLAG
  396. "--- Start of test: nreaders=%d stat_interval=%d verbose=%d\n",
  397. nrealreaders, stat_interval, verbose);
  398. fullstop = 0;
  399. /* Set up the freelist. */
  400. INIT_LIST_HEAD(&rcu_torture_freelist);
  401. for (i = 0; i < sizeof(rcu_tortures) / sizeof(rcu_tortures[0]); i++) {
  402. rcu_tortures[i].rtort_mbtest = 0;
  403. list_add_tail(&rcu_tortures[i].rtort_free,
  404. &rcu_torture_freelist);
  405. }
  406. /* Initialize the statistics so that each run gets its own numbers. */
  407. rcu_torture_current = NULL;
  408. rcu_torture_current_version = 0;
  409. atomic_set(&n_rcu_torture_alloc, 0);
  410. atomic_set(&n_rcu_torture_alloc_fail, 0);
  411. atomic_set(&n_rcu_torture_free, 0);
  412. atomic_set(&n_rcu_torture_mberror, 0);
  413. atomic_set(&n_rcu_torture_error, 0);
  414. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  415. atomic_set(&rcu_torture_wcount[i], 0);
  416. for_each_cpu(cpu) {
  417. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  418. per_cpu(rcu_torture_count, cpu)[i] = 0;
  419. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  420. }
  421. }
  422. /* Start up the kthreads. */
  423. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  424. writer_task = kthread_run(rcu_torture_writer, NULL,
  425. "rcu_torture_writer");
  426. if (IS_ERR(writer_task)) {
  427. firsterr = PTR_ERR(writer_task);
  428. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  429. writer_task = NULL;
  430. goto unwind;
  431. }
  432. reader_tasks = kmalloc(nrealreaders * sizeof(reader_tasks[0]),
  433. GFP_KERNEL);
  434. if (reader_tasks == NULL) {
  435. VERBOSE_PRINTK_ERRSTRING("out of memory");
  436. firsterr = -ENOMEM;
  437. goto unwind;
  438. }
  439. for (i = 0; i < nrealreaders; i++) {
  440. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  441. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  442. "rcu_torture_reader");
  443. if (IS_ERR(reader_tasks[i])) {
  444. firsterr = PTR_ERR(reader_tasks[i]);
  445. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  446. reader_tasks[i] = NULL;
  447. goto unwind;
  448. }
  449. }
  450. if (stat_interval > 0) {
  451. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  452. stats_task = kthread_run(rcu_torture_stats, NULL,
  453. "rcu_torture_stats");
  454. if (IS_ERR(stats_task)) {
  455. firsterr = PTR_ERR(stats_task);
  456. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  457. stats_task = NULL;
  458. goto unwind;
  459. }
  460. }
  461. return 0;
  462. unwind:
  463. rcu_torture_cleanup();
  464. return firsterr;
  465. }
  466. module_init(rcu_torture_init);
  467. module_exit(rcu_torture_cleanup);