rcutorture.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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
  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/cpu.h>
  43. #include <linux/random.h>
  44. #include <linux/delay.h>
  45. #include <linux/byteorder/swabb.h>
  46. #include <linux/stat.h>
  47. MODULE_LICENSE("GPL");
  48. static int nreaders = -1; /* # reader threads, defaults to 4*ncpus */
  49. static int stat_interval; /* Interval between stats, in seconds. */
  50. /* Defaults to "only at end of test". */
  51. static int verbose; /* Print more debug info. */
  52. static int test_no_idle_hz; /* Test RCU's support for tickless idle CPUs. */
  53. static int shuffle_interval = 5; /* Interval between shuffles (in sec)*/
  54. static char *torture_type = "rcu"; /* What to torture. */
  55. module_param(nreaders, int, 0);
  56. MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
  57. module_param(stat_interval, int, 0);
  58. MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
  59. module_param(verbose, bool, 0);
  60. MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
  61. module_param(test_no_idle_hz, bool, 0);
  62. MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
  63. module_param(shuffle_interval, int, 0);
  64. MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
  65. module_param(torture_type, charp, 0);
  66. MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh)");
  67. #define TORTURE_FLAG "-torture:"
  68. #define PRINTK_STRING(s) \
  69. do { printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  70. #define VERBOSE_PRINTK_STRING(s) \
  71. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG s "\n", torture_type); } while (0)
  72. #define VERBOSE_PRINTK_ERRSTRING(s) \
  73. do { if (verbose) printk(KERN_ALERT "%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
  74. static char printk_buf[4096];
  75. static int nrealreaders;
  76. static struct task_struct *writer_task;
  77. static struct task_struct **reader_tasks;
  78. static struct task_struct *stats_task;
  79. static struct task_struct *shuffler_task;
  80. #define RCU_TORTURE_PIPE_LEN 10
  81. struct rcu_torture {
  82. struct rcu_head rtort_rcu;
  83. int rtort_pipe_count;
  84. struct list_head rtort_free;
  85. int rtort_mbtest;
  86. };
  87. static int fullstop = 0; /* stop generating callbacks at test end. */
  88. static LIST_HEAD(rcu_torture_freelist);
  89. static struct rcu_torture *rcu_torture_current = NULL;
  90. static long rcu_torture_current_version = 0;
  91. static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
  92. static DEFINE_SPINLOCK(rcu_torture_lock);
  93. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count) =
  94. { 0 };
  95. static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch) =
  96. { 0 };
  97. static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
  98. atomic_t n_rcu_torture_alloc;
  99. atomic_t n_rcu_torture_alloc_fail;
  100. atomic_t n_rcu_torture_free;
  101. atomic_t n_rcu_torture_mberror;
  102. atomic_t n_rcu_torture_error;
  103. /*
  104. * Allocate an element from the rcu_tortures pool.
  105. */
  106. static struct rcu_torture *
  107. rcu_torture_alloc(void)
  108. {
  109. struct list_head *p;
  110. spin_lock_bh(&rcu_torture_lock);
  111. if (list_empty(&rcu_torture_freelist)) {
  112. atomic_inc(&n_rcu_torture_alloc_fail);
  113. spin_unlock_bh(&rcu_torture_lock);
  114. return NULL;
  115. }
  116. atomic_inc(&n_rcu_torture_alloc);
  117. p = rcu_torture_freelist.next;
  118. list_del_init(p);
  119. spin_unlock_bh(&rcu_torture_lock);
  120. return container_of(p, struct rcu_torture, rtort_free);
  121. }
  122. /*
  123. * Free an element to the rcu_tortures pool.
  124. */
  125. static void
  126. rcu_torture_free(struct rcu_torture *p)
  127. {
  128. atomic_inc(&n_rcu_torture_free);
  129. spin_lock_bh(&rcu_torture_lock);
  130. list_add_tail(&p->rtort_free, &rcu_torture_freelist);
  131. spin_unlock_bh(&rcu_torture_lock);
  132. }
  133. struct rcu_random_state {
  134. unsigned long rrs_state;
  135. unsigned long rrs_count;
  136. };
  137. #define RCU_RANDOM_MULT 39916801 /* prime */
  138. #define RCU_RANDOM_ADD 479001701 /* prime */
  139. #define RCU_RANDOM_REFRESH 10000
  140. #define DEFINE_RCU_RANDOM(name) struct rcu_random_state name = { 0, 0 }
  141. /*
  142. * Crude but fast random-number generator. Uses a linear congruential
  143. * generator, with occasional help from get_random_bytes().
  144. */
  145. static long
  146. rcu_random(struct rcu_random_state *rrsp)
  147. {
  148. long refresh;
  149. if (--rrsp->rrs_count < 0) {
  150. get_random_bytes(&refresh, sizeof(refresh));
  151. rrsp->rrs_state += refresh;
  152. rrsp->rrs_count = RCU_RANDOM_REFRESH;
  153. }
  154. rrsp->rrs_state = rrsp->rrs_state * RCU_RANDOM_MULT + RCU_RANDOM_ADD;
  155. return swahw32(rrsp->rrs_state);
  156. }
  157. /*
  158. * Operations vector for selecting different types of tests.
  159. */
  160. struct rcu_torture_ops {
  161. void (*init)(void);
  162. void (*cleanup)(void);
  163. int (*readlock)(void);
  164. void (*readunlock)(int idx);
  165. int (*completed)(void);
  166. void (*deferredfree)(struct rcu_torture *p);
  167. int (*stats)(char *page);
  168. char *name;
  169. };
  170. static struct rcu_torture_ops *cur_ops = NULL;
  171. /*
  172. * Definitions for rcu torture testing.
  173. */
  174. static int rcu_torture_read_lock(void) __acquires(RCU)
  175. {
  176. rcu_read_lock();
  177. return 0;
  178. }
  179. static void rcu_torture_read_unlock(int idx) __releases(RCU)
  180. {
  181. rcu_read_unlock();
  182. }
  183. static int rcu_torture_completed(void)
  184. {
  185. return rcu_batches_completed();
  186. }
  187. static void
  188. rcu_torture_cb(struct rcu_head *p)
  189. {
  190. int i;
  191. struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
  192. if (fullstop) {
  193. /* Test is ending, just drop callbacks on the floor. */
  194. /* The next initialization will pick up the pieces. */
  195. return;
  196. }
  197. i = rp->rtort_pipe_count;
  198. if (i > RCU_TORTURE_PIPE_LEN)
  199. i = RCU_TORTURE_PIPE_LEN;
  200. atomic_inc(&rcu_torture_wcount[i]);
  201. if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
  202. rp->rtort_mbtest = 0;
  203. rcu_torture_free(rp);
  204. } else
  205. cur_ops->deferredfree(rp);
  206. }
  207. static void rcu_torture_deferred_free(struct rcu_torture *p)
  208. {
  209. call_rcu(&p->rtort_rcu, rcu_torture_cb);
  210. }
  211. static struct rcu_torture_ops rcu_ops = {
  212. .init = NULL,
  213. .cleanup = NULL,
  214. .readlock = rcu_torture_read_lock,
  215. .readunlock = rcu_torture_read_unlock,
  216. .completed = rcu_torture_completed,
  217. .deferredfree = rcu_torture_deferred_free,
  218. .stats = NULL,
  219. .name = "rcu"
  220. };
  221. /*
  222. * Definitions for rcu_bh torture testing.
  223. */
  224. static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
  225. {
  226. rcu_read_lock_bh();
  227. return 0;
  228. }
  229. static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
  230. {
  231. rcu_read_unlock_bh();
  232. }
  233. static int rcu_bh_torture_completed(void)
  234. {
  235. return rcu_batches_completed_bh();
  236. }
  237. static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
  238. {
  239. call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
  240. }
  241. static struct rcu_torture_ops rcu_bh_ops = {
  242. .init = NULL,
  243. .cleanup = NULL,
  244. .readlock = rcu_bh_torture_read_lock,
  245. .readunlock = rcu_bh_torture_read_unlock,
  246. .completed = rcu_bh_torture_completed,
  247. .deferredfree = rcu_bh_torture_deferred_free,
  248. .stats = NULL,
  249. .name = "rcu_bh"
  250. };
  251. static struct rcu_torture_ops *torture_ops[] =
  252. { &rcu_ops, &rcu_bh_ops, NULL };
  253. /*
  254. * RCU torture writer kthread. Repeatedly substitutes a new structure
  255. * for that pointed to by rcu_torture_current, freeing the old structure
  256. * after a series of grace periods (the "pipeline").
  257. */
  258. static int
  259. rcu_torture_writer(void *arg)
  260. {
  261. int i;
  262. long oldbatch = rcu_batches_completed();
  263. struct rcu_torture *rp;
  264. struct rcu_torture *old_rp;
  265. static DEFINE_RCU_RANDOM(rand);
  266. VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
  267. set_user_nice(current, 19);
  268. do {
  269. schedule_timeout_uninterruptible(1);
  270. if ((rp = rcu_torture_alloc()) == NULL)
  271. continue;
  272. rp->rtort_pipe_count = 0;
  273. udelay(rcu_random(&rand) & 0x3ff);
  274. old_rp = rcu_torture_current;
  275. rp->rtort_mbtest = 1;
  276. rcu_assign_pointer(rcu_torture_current, rp);
  277. smp_wmb();
  278. if (old_rp != NULL) {
  279. i = old_rp->rtort_pipe_count;
  280. if (i > RCU_TORTURE_PIPE_LEN)
  281. i = RCU_TORTURE_PIPE_LEN;
  282. atomic_inc(&rcu_torture_wcount[i]);
  283. old_rp->rtort_pipe_count++;
  284. cur_ops->deferredfree(old_rp);
  285. }
  286. rcu_torture_current_version++;
  287. oldbatch = cur_ops->completed();
  288. } while (!kthread_should_stop() && !fullstop);
  289. VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
  290. while (!kthread_should_stop())
  291. schedule_timeout_uninterruptible(1);
  292. return 0;
  293. }
  294. /*
  295. * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
  296. * incrementing the corresponding element of the pipeline array. The
  297. * counter in the element should never be greater than 1, otherwise, the
  298. * RCU implementation is broken.
  299. */
  300. static int
  301. rcu_torture_reader(void *arg)
  302. {
  303. int completed;
  304. int idx;
  305. DEFINE_RCU_RANDOM(rand);
  306. struct rcu_torture *p;
  307. int pipe_count;
  308. VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
  309. set_user_nice(current, 19);
  310. do {
  311. idx = cur_ops->readlock();
  312. completed = cur_ops->completed();
  313. p = rcu_dereference(rcu_torture_current);
  314. if (p == NULL) {
  315. /* Wait for rcu_torture_writer to get underway */
  316. cur_ops->readunlock(idx);
  317. schedule_timeout_interruptible(HZ);
  318. continue;
  319. }
  320. if (p->rtort_mbtest == 0)
  321. atomic_inc(&n_rcu_torture_mberror);
  322. udelay(rcu_random(&rand) & 0x7f);
  323. preempt_disable();
  324. pipe_count = p->rtort_pipe_count;
  325. if (pipe_count > RCU_TORTURE_PIPE_LEN) {
  326. /* Should not happen, but... */
  327. pipe_count = RCU_TORTURE_PIPE_LEN;
  328. }
  329. ++__get_cpu_var(rcu_torture_count)[pipe_count];
  330. completed = cur_ops->completed() - completed;
  331. if (completed > RCU_TORTURE_PIPE_LEN) {
  332. /* Should not happen, but... */
  333. completed = RCU_TORTURE_PIPE_LEN;
  334. }
  335. ++__get_cpu_var(rcu_torture_batch)[completed];
  336. preempt_enable();
  337. cur_ops->readunlock(idx);
  338. schedule();
  339. } while (!kthread_should_stop() && !fullstop);
  340. VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
  341. while (!kthread_should_stop())
  342. schedule_timeout_uninterruptible(1);
  343. return 0;
  344. }
  345. /*
  346. * Create an RCU-torture statistics message in the specified buffer.
  347. */
  348. static int
  349. rcu_torture_printk(char *page)
  350. {
  351. int cnt = 0;
  352. int cpu;
  353. int i;
  354. long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  355. long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
  356. for_each_possible_cpu(cpu) {
  357. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  358. pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
  359. batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
  360. }
  361. }
  362. for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
  363. if (pipesummary[i] != 0)
  364. break;
  365. }
  366. cnt += sprintf(&page[cnt], "%s%s ", torture_type, TORTURE_FLAG);
  367. cnt += sprintf(&page[cnt],
  368. "rtc: %p ver: %ld tfle: %d rta: %d rtaf: %d rtf: %d "
  369. "rtmbe: %d",
  370. rcu_torture_current,
  371. rcu_torture_current_version,
  372. list_empty(&rcu_torture_freelist),
  373. atomic_read(&n_rcu_torture_alloc),
  374. atomic_read(&n_rcu_torture_alloc_fail),
  375. atomic_read(&n_rcu_torture_free),
  376. atomic_read(&n_rcu_torture_mberror));
  377. if (atomic_read(&n_rcu_torture_mberror) != 0)
  378. cnt += sprintf(&page[cnt], " !!!");
  379. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  380. if (i > 1) {
  381. cnt += sprintf(&page[cnt], "!!! ");
  382. atomic_inc(&n_rcu_torture_error);
  383. }
  384. cnt += sprintf(&page[cnt], "Reader Pipe: ");
  385. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  386. cnt += sprintf(&page[cnt], " %ld", pipesummary[i]);
  387. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  388. cnt += sprintf(&page[cnt], "Reader Batch: ");
  389. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  390. cnt += sprintf(&page[cnt], " %ld", batchsummary[i]);
  391. cnt += sprintf(&page[cnt], "\n%s%s ", torture_type, TORTURE_FLAG);
  392. cnt += sprintf(&page[cnt], "Free-Block Circulation: ");
  393. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  394. cnt += sprintf(&page[cnt], " %d",
  395. atomic_read(&rcu_torture_wcount[i]));
  396. }
  397. cnt += sprintf(&page[cnt], "\n");
  398. if (cur_ops->stats != NULL)
  399. cnt += cur_ops->stats(&page[cnt]);
  400. return cnt;
  401. }
  402. /*
  403. * Print torture statistics. Caller must ensure that there is only
  404. * one call to this function at a given time!!! This is normally
  405. * accomplished by relying on the module system to only have one copy
  406. * of the module loaded, and then by giving the rcu_torture_stats
  407. * kthread full control (or the init/cleanup functions when rcu_torture_stats
  408. * thread is not running).
  409. */
  410. static void
  411. rcu_torture_stats_print(void)
  412. {
  413. int cnt;
  414. cnt = rcu_torture_printk(printk_buf);
  415. printk(KERN_ALERT "%s", printk_buf);
  416. }
  417. /*
  418. * Periodically prints torture statistics, if periodic statistics printing
  419. * was specified via the stat_interval module parameter.
  420. *
  421. * No need to worry about fullstop here, since this one doesn't reference
  422. * volatile state or register callbacks.
  423. */
  424. static int
  425. rcu_torture_stats(void *arg)
  426. {
  427. VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
  428. do {
  429. schedule_timeout_interruptible(stat_interval * HZ);
  430. rcu_torture_stats_print();
  431. } while (!kthread_should_stop());
  432. VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
  433. return 0;
  434. }
  435. static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
  436. /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
  437. * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
  438. */
  439. void rcu_torture_shuffle_tasks(void)
  440. {
  441. cpumask_t tmp_mask = CPU_MASK_ALL;
  442. int i;
  443. lock_cpu_hotplug();
  444. /* No point in shuffling if there is only one online CPU (ex: UP) */
  445. if (num_online_cpus() == 1) {
  446. unlock_cpu_hotplug();
  447. return;
  448. }
  449. if (rcu_idle_cpu != -1)
  450. cpu_clear(rcu_idle_cpu, tmp_mask);
  451. set_cpus_allowed(current, tmp_mask);
  452. if (reader_tasks != NULL) {
  453. for (i = 0; i < nrealreaders; i++)
  454. if (reader_tasks[i])
  455. set_cpus_allowed(reader_tasks[i], tmp_mask);
  456. }
  457. if (writer_task)
  458. set_cpus_allowed(writer_task, tmp_mask);
  459. if (stats_task)
  460. set_cpus_allowed(stats_task, tmp_mask);
  461. if (rcu_idle_cpu == -1)
  462. rcu_idle_cpu = num_online_cpus() - 1;
  463. else
  464. rcu_idle_cpu--;
  465. unlock_cpu_hotplug();
  466. }
  467. /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
  468. * system to become idle at a time and cut off its timer ticks. This is meant
  469. * to test the support for such tickless idle CPU in RCU.
  470. */
  471. static int
  472. rcu_torture_shuffle(void *arg)
  473. {
  474. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
  475. do {
  476. schedule_timeout_interruptible(shuffle_interval * HZ);
  477. rcu_torture_shuffle_tasks();
  478. } while (!kthread_should_stop());
  479. VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
  480. return 0;
  481. }
  482. static inline void
  483. rcu_torture_print_module_parms(char *tag)
  484. {
  485. printk(KERN_ALERT "%s" TORTURE_FLAG "--- %s: nreaders=%d "
  486. "stat_interval=%d verbose=%d test_no_idle_hz=%d "
  487. "shuffle_interval = %d\n",
  488. torture_type, tag, nrealreaders, stat_interval, verbose,
  489. test_no_idle_hz, shuffle_interval);
  490. }
  491. static void
  492. rcu_torture_cleanup(void)
  493. {
  494. int i;
  495. fullstop = 1;
  496. if (shuffler_task != NULL) {
  497. VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
  498. kthread_stop(shuffler_task);
  499. }
  500. shuffler_task = NULL;
  501. if (writer_task != NULL) {
  502. VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
  503. kthread_stop(writer_task);
  504. }
  505. writer_task = NULL;
  506. if (reader_tasks != NULL) {
  507. for (i = 0; i < nrealreaders; i++) {
  508. if (reader_tasks[i] != NULL) {
  509. VERBOSE_PRINTK_STRING(
  510. "Stopping rcu_torture_reader task");
  511. kthread_stop(reader_tasks[i]);
  512. }
  513. reader_tasks[i] = NULL;
  514. }
  515. kfree(reader_tasks);
  516. reader_tasks = NULL;
  517. }
  518. rcu_torture_current = NULL;
  519. if (stats_task != NULL) {
  520. VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
  521. kthread_stop(stats_task);
  522. }
  523. stats_task = NULL;
  524. /* Wait for all RCU callbacks to fire. */
  525. rcu_barrier();
  526. rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
  527. if (cur_ops->cleanup != NULL)
  528. cur_ops->cleanup();
  529. if (atomic_read(&n_rcu_torture_error))
  530. rcu_torture_print_module_parms("End of test: FAILURE");
  531. else
  532. rcu_torture_print_module_parms("End of test: SUCCESS");
  533. }
  534. static int
  535. rcu_torture_init(void)
  536. {
  537. int i;
  538. int cpu;
  539. int firsterr = 0;
  540. /* Process args and tell the world that the torturer is on the job. */
  541. for (i = 0; cur_ops = torture_ops[i], cur_ops != NULL; i++) {
  542. cur_ops = torture_ops[i];
  543. if (strcmp(torture_type, cur_ops->name) == 0) {
  544. break;
  545. }
  546. }
  547. if (cur_ops == NULL) {
  548. printk(KERN_ALERT "rcutorture: invalid torture type: \"%s\"\n",
  549. torture_type);
  550. return (-EINVAL);
  551. }
  552. if (cur_ops->init != NULL)
  553. cur_ops->init(); /* no "goto unwind" prior to this point!!! */
  554. if (nreaders >= 0)
  555. nrealreaders = nreaders;
  556. else
  557. nrealreaders = 2 * num_online_cpus();
  558. rcu_torture_print_module_parms("Start of test");
  559. fullstop = 0;
  560. /* Set up the freelist. */
  561. INIT_LIST_HEAD(&rcu_torture_freelist);
  562. for (i = 0; i < sizeof(rcu_tortures) / sizeof(rcu_tortures[0]); i++) {
  563. rcu_tortures[i].rtort_mbtest = 0;
  564. list_add_tail(&rcu_tortures[i].rtort_free,
  565. &rcu_torture_freelist);
  566. }
  567. /* Initialize the statistics so that each run gets its own numbers. */
  568. rcu_torture_current = NULL;
  569. rcu_torture_current_version = 0;
  570. atomic_set(&n_rcu_torture_alloc, 0);
  571. atomic_set(&n_rcu_torture_alloc_fail, 0);
  572. atomic_set(&n_rcu_torture_free, 0);
  573. atomic_set(&n_rcu_torture_mberror, 0);
  574. atomic_set(&n_rcu_torture_error, 0);
  575. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
  576. atomic_set(&rcu_torture_wcount[i], 0);
  577. for_each_possible_cpu(cpu) {
  578. for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
  579. per_cpu(rcu_torture_count, cpu)[i] = 0;
  580. per_cpu(rcu_torture_batch, cpu)[i] = 0;
  581. }
  582. }
  583. /* Start up the kthreads. */
  584. VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
  585. writer_task = kthread_run(rcu_torture_writer, NULL,
  586. "rcu_torture_writer");
  587. if (IS_ERR(writer_task)) {
  588. firsterr = PTR_ERR(writer_task);
  589. VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
  590. writer_task = NULL;
  591. goto unwind;
  592. }
  593. reader_tasks = kmalloc(nrealreaders * sizeof(reader_tasks[0]),
  594. GFP_KERNEL);
  595. if (reader_tasks == NULL) {
  596. VERBOSE_PRINTK_ERRSTRING("out of memory");
  597. firsterr = -ENOMEM;
  598. goto unwind;
  599. }
  600. for (i = 0; i < nrealreaders; i++) {
  601. VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
  602. reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
  603. "rcu_torture_reader");
  604. if (IS_ERR(reader_tasks[i])) {
  605. firsterr = PTR_ERR(reader_tasks[i]);
  606. VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
  607. reader_tasks[i] = NULL;
  608. goto unwind;
  609. }
  610. }
  611. if (stat_interval > 0) {
  612. VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
  613. stats_task = kthread_run(rcu_torture_stats, NULL,
  614. "rcu_torture_stats");
  615. if (IS_ERR(stats_task)) {
  616. firsterr = PTR_ERR(stats_task);
  617. VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
  618. stats_task = NULL;
  619. goto unwind;
  620. }
  621. }
  622. if (test_no_idle_hz) {
  623. rcu_idle_cpu = num_online_cpus() - 1;
  624. /* Create the shuffler thread */
  625. shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
  626. "rcu_torture_shuffle");
  627. if (IS_ERR(shuffler_task)) {
  628. firsterr = PTR_ERR(shuffler_task);
  629. VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
  630. shuffler_task = NULL;
  631. goto unwind;
  632. }
  633. }
  634. return 0;
  635. unwind:
  636. rcu_torture_cleanup();
  637. return firsterr;
  638. }
  639. module_init(rcu_torture_init);
  640. module_exit(rcu_torture_cleanup);