rcuperf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. * Read-Copy Update module-based performance-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, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright (C) IBM Corporation, 2015
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. */
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/kthread.h>
  27. #include <linux/err.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/smp.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/sched.h>
  33. #include <uapi/linux/sched/types.h>
  34. #include <linux/atomic.h>
  35. #include <linux/bitops.h>
  36. #include <linux/completion.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/percpu.h>
  39. #include <linux/notifier.h>
  40. #include <linux/reboot.h>
  41. #include <linux/freezer.h>
  42. #include <linux/cpu.h>
  43. #include <linux/delay.h>
  44. #include <linux/stat.h>
  45. #include <linux/srcu.h>
  46. #include <linux/slab.h>
  47. #include <asm/byteorder.h>
  48. #include <linux/torture.h>
  49. #include <linux/vmalloc.h>
  50. #include "rcu.h"
  51. MODULE_LICENSE("GPL");
  52. MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
  53. #define PERF_FLAG "-perf:"
  54. #define PERFOUT_STRING(s) \
  55. pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
  56. #define VERBOSE_PERFOUT_STRING(s) \
  57. do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
  58. #define VERBOSE_PERFOUT_ERRSTRING(s) \
  59. do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
  60. /*
  61. * The intended use cases for the nreaders and nwriters module parameters
  62. * are as follows:
  63. *
  64. * 1. Specify only the nr_cpus kernel boot parameter. This will
  65. * set both nreaders and nwriters to the value specified by
  66. * nr_cpus for a mixed reader/writer test.
  67. *
  68. * 2. Specify the nr_cpus kernel boot parameter, but set
  69. * rcuperf.nreaders to zero. This will set nwriters to the
  70. * value specified by nr_cpus for an update-only test.
  71. *
  72. * 3. Specify the nr_cpus kernel boot parameter, but set
  73. * rcuperf.nwriters to zero. This will set nreaders to the
  74. * value specified by nr_cpus for a read-only test.
  75. *
  76. * Various other use cases may of course be specified.
  77. */
  78. torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
  79. torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
  80. torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
  81. torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
  82. torture_param(int, nreaders, -1, "Number of RCU reader threads");
  83. torture_param(int, nwriters, -1, "Number of RCU updater threads");
  84. torture_param(bool, shutdown, !IS_ENABLED(MODULE),
  85. "Shutdown at end of performance tests.");
  86. torture_param(bool, verbose, true, "Enable verbose debugging printk()s");
  87. torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
  88. static char *perf_type = "rcu";
  89. module_param(perf_type, charp, 0444);
  90. MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
  91. static int nrealreaders;
  92. static int nrealwriters;
  93. static struct task_struct **writer_tasks;
  94. static struct task_struct **reader_tasks;
  95. static struct task_struct *shutdown_task;
  96. static u64 **writer_durations;
  97. static int *writer_n_durations;
  98. static atomic_t n_rcu_perf_reader_started;
  99. static atomic_t n_rcu_perf_writer_started;
  100. static atomic_t n_rcu_perf_writer_finished;
  101. static wait_queue_head_t shutdown_wq;
  102. static u64 t_rcu_perf_writer_started;
  103. static u64 t_rcu_perf_writer_finished;
  104. static unsigned long b_rcu_perf_writer_started;
  105. static unsigned long b_rcu_perf_writer_finished;
  106. static DEFINE_PER_CPU(atomic_t, n_async_inflight);
  107. static int rcu_perf_writer_state;
  108. #define RTWS_INIT 0
  109. #define RTWS_ASYNC 1
  110. #define RTWS_BARRIER 2
  111. #define RTWS_EXP_SYNC 3
  112. #define RTWS_SYNC 4
  113. #define RTWS_IDLE 5
  114. #define RTWS_STOPPING 6
  115. #define MAX_MEAS 10000
  116. #define MIN_MEAS 100
  117. /*
  118. * Operations vector for selecting different types of tests.
  119. */
  120. struct rcu_perf_ops {
  121. int ptype;
  122. void (*init)(void);
  123. void (*cleanup)(void);
  124. int (*readlock)(void);
  125. void (*readunlock)(int idx);
  126. unsigned long (*started)(void);
  127. unsigned long (*completed)(void);
  128. unsigned long (*exp_completed)(void);
  129. void (*async)(struct rcu_head *head, rcu_callback_t func);
  130. void (*gp_barrier)(void);
  131. void (*sync)(void);
  132. void (*exp_sync)(void);
  133. const char *name;
  134. };
  135. static struct rcu_perf_ops *cur_ops;
  136. /*
  137. * Definitions for rcu perf testing.
  138. */
  139. static int rcu_perf_read_lock(void) __acquires(RCU)
  140. {
  141. rcu_read_lock();
  142. return 0;
  143. }
  144. static void rcu_perf_read_unlock(int idx) __releases(RCU)
  145. {
  146. rcu_read_unlock();
  147. }
  148. static unsigned long __maybe_unused rcu_no_completed(void)
  149. {
  150. return 0;
  151. }
  152. static void rcu_sync_perf_init(void)
  153. {
  154. }
  155. static struct rcu_perf_ops rcu_ops = {
  156. .ptype = RCU_FLAVOR,
  157. .init = rcu_sync_perf_init,
  158. .readlock = rcu_perf_read_lock,
  159. .readunlock = rcu_perf_read_unlock,
  160. .started = rcu_batches_started,
  161. .completed = rcu_batches_completed,
  162. .exp_completed = rcu_exp_batches_completed,
  163. .async = call_rcu,
  164. .gp_barrier = rcu_barrier,
  165. .sync = synchronize_rcu,
  166. .exp_sync = synchronize_rcu_expedited,
  167. .name = "rcu"
  168. };
  169. /*
  170. * Definitions for rcu_bh perf testing.
  171. */
  172. static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH)
  173. {
  174. rcu_read_lock_bh();
  175. return 0;
  176. }
  177. static void rcu_bh_perf_read_unlock(int idx) __releases(RCU_BH)
  178. {
  179. rcu_read_unlock_bh();
  180. }
  181. static struct rcu_perf_ops rcu_bh_ops = {
  182. .ptype = RCU_BH_FLAVOR,
  183. .init = rcu_sync_perf_init,
  184. .readlock = rcu_bh_perf_read_lock,
  185. .readunlock = rcu_bh_perf_read_unlock,
  186. .started = rcu_batches_started_bh,
  187. .completed = rcu_batches_completed_bh,
  188. .exp_completed = rcu_exp_batches_completed_sched,
  189. .async = call_rcu_bh,
  190. .gp_barrier = rcu_barrier_bh,
  191. .sync = synchronize_rcu_bh,
  192. .exp_sync = synchronize_rcu_bh_expedited,
  193. .name = "rcu_bh"
  194. };
  195. /*
  196. * Definitions for srcu perf testing.
  197. */
  198. DEFINE_STATIC_SRCU(srcu_ctl_perf);
  199. static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
  200. static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
  201. {
  202. return srcu_read_lock(srcu_ctlp);
  203. }
  204. static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
  205. {
  206. srcu_read_unlock(srcu_ctlp, idx);
  207. }
  208. static unsigned long srcu_perf_completed(void)
  209. {
  210. return srcu_batches_completed(srcu_ctlp);
  211. }
  212. static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
  213. {
  214. call_srcu(srcu_ctlp, head, func);
  215. }
  216. static void srcu_rcu_barrier(void)
  217. {
  218. srcu_barrier(srcu_ctlp);
  219. }
  220. static void srcu_perf_synchronize(void)
  221. {
  222. synchronize_srcu(srcu_ctlp);
  223. }
  224. static void srcu_perf_synchronize_expedited(void)
  225. {
  226. synchronize_srcu_expedited(srcu_ctlp);
  227. }
  228. static struct rcu_perf_ops srcu_ops = {
  229. .ptype = SRCU_FLAVOR,
  230. .init = rcu_sync_perf_init,
  231. .readlock = srcu_perf_read_lock,
  232. .readunlock = srcu_perf_read_unlock,
  233. .started = NULL,
  234. .completed = srcu_perf_completed,
  235. .exp_completed = srcu_perf_completed,
  236. .async = srcu_call_rcu,
  237. .gp_barrier = srcu_rcu_barrier,
  238. .sync = srcu_perf_synchronize,
  239. .exp_sync = srcu_perf_synchronize_expedited,
  240. .name = "srcu"
  241. };
  242. static struct srcu_struct srcud;
  243. static void srcu_sync_perf_init(void)
  244. {
  245. srcu_ctlp = &srcud;
  246. init_srcu_struct(srcu_ctlp);
  247. }
  248. static void srcu_sync_perf_cleanup(void)
  249. {
  250. cleanup_srcu_struct(srcu_ctlp);
  251. }
  252. static struct rcu_perf_ops srcud_ops = {
  253. .ptype = SRCU_FLAVOR,
  254. .init = srcu_sync_perf_init,
  255. .cleanup = srcu_sync_perf_cleanup,
  256. .readlock = srcu_perf_read_lock,
  257. .readunlock = srcu_perf_read_unlock,
  258. .started = NULL,
  259. .completed = srcu_perf_completed,
  260. .exp_completed = srcu_perf_completed,
  261. .async = srcu_call_rcu,
  262. .gp_barrier = srcu_rcu_barrier,
  263. .sync = srcu_perf_synchronize,
  264. .exp_sync = srcu_perf_synchronize_expedited,
  265. .name = "srcud"
  266. };
  267. /*
  268. * Definitions for sched perf testing.
  269. */
  270. static int sched_perf_read_lock(void)
  271. {
  272. preempt_disable();
  273. return 0;
  274. }
  275. static void sched_perf_read_unlock(int idx)
  276. {
  277. preempt_enable();
  278. }
  279. static struct rcu_perf_ops sched_ops = {
  280. .ptype = RCU_SCHED_FLAVOR,
  281. .init = rcu_sync_perf_init,
  282. .readlock = sched_perf_read_lock,
  283. .readunlock = sched_perf_read_unlock,
  284. .started = rcu_batches_started_sched,
  285. .completed = rcu_batches_completed_sched,
  286. .exp_completed = rcu_exp_batches_completed_sched,
  287. .async = call_rcu_sched,
  288. .gp_barrier = rcu_barrier_sched,
  289. .sync = synchronize_sched,
  290. .exp_sync = synchronize_sched_expedited,
  291. .name = "sched"
  292. };
  293. /*
  294. * Definitions for RCU-tasks perf testing.
  295. */
  296. static int tasks_perf_read_lock(void)
  297. {
  298. return 0;
  299. }
  300. static void tasks_perf_read_unlock(int idx)
  301. {
  302. }
  303. static struct rcu_perf_ops tasks_ops = {
  304. .ptype = RCU_TASKS_FLAVOR,
  305. .init = rcu_sync_perf_init,
  306. .readlock = tasks_perf_read_lock,
  307. .readunlock = tasks_perf_read_unlock,
  308. .started = rcu_no_completed,
  309. .completed = rcu_no_completed,
  310. .async = call_rcu_tasks,
  311. .gp_barrier = rcu_barrier_tasks,
  312. .sync = synchronize_rcu_tasks,
  313. .exp_sync = synchronize_rcu_tasks,
  314. .name = "tasks"
  315. };
  316. static bool __maybe_unused torturing_tasks(void)
  317. {
  318. return cur_ops == &tasks_ops;
  319. }
  320. /*
  321. * If performance tests complete, wait for shutdown to commence.
  322. */
  323. static void rcu_perf_wait_shutdown(void)
  324. {
  325. cond_resched_tasks_rcu_qs();
  326. if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
  327. return;
  328. while (!torture_must_stop())
  329. schedule_timeout_uninterruptible(1);
  330. }
  331. /*
  332. * RCU perf reader kthread. Repeatedly does empty RCU read-side
  333. * critical section, minimizing update-side interference.
  334. */
  335. static int
  336. rcu_perf_reader(void *arg)
  337. {
  338. unsigned long flags;
  339. int idx;
  340. long me = (long)arg;
  341. VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
  342. set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
  343. set_user_nice(current, MAX_NICE);
  344. atomic_inc(&n_rcu_perf_reader_started);
  345. do {
  346. local_irq_save(flags);
  347. idx = cur_ops->readlock();
  348. cur_ops->readunlock(idx);
  349. local_irq_restore(flags);
  350. rcu_perf_wait_shutdown();
  351. } while (!torture_must_stop());
  352. torture_kthread_stopping("rcu_perf_reader");
  353. return 0;
  354. }
  355. /*
  356. * Callback function for asynchronous grace periods from rcu_perf_writer().
  357. */
  358. static void rcu_perf_async_cb(struct rcu_head *rhp)
  359. {
  360. atomic_dec(this_cpu_ptr(&n_async_inflight));
  361. kfree(rhp);
  362. }
  363. /*
  364. * RCU perf writer kthread. Repeatedly does a grace period.
  365. */
  366. static int
  367. rcu_perf_writer(void *arg)
  368. {
  369. int i = 0;
  370. int i_max;
  371. long me = (long)arg;
  372. struct rcu_head *rhp = NULL;
  373. struct sched_param sp;
  374. bool started = false, done = false, alldone = false;
  375. u64 t;
  376. u64 *wdp;
  377. u64 *wdpp = writer_durations[me];
  378. VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
  379. WARN_ON(!wdpp);
  380. set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
  381. sp.sched_priority = 1;
  382. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  383. if (holdoff)
  384. schedule_timeout_uninterruptible(holdoff * HZ);
  385. t = ktime_get_mono_fast_ns();
  386. if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
  387. t_rcu_perf_writer_started = t;
  388. if (gp_exp) {
  389. b_rcu_perf_writer_started =
  390. cur_ops->exp_completed() / 2;
  391. } else {
  392. b_rcu_perf_writer_started =
  393. cur_ops->completed();
  394. }
  395. }
  396. do {
  397. if (writer_holdoff)
  398. udelay(writer_holdoff);
  399. wdp = &wdpp[i];
  400. *wdp = ktime_get_mono_fast_ns();
  401. if (gp_async) {
  402. retry:
  403. if (!rhp)
  404. rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
  405. if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
  406. rcu_perf_writer_state = RTWS_ASYNC;
  407. atomic_inc(this_cpu_ptr(&n_async_inflight));
  408. cur_ops->async(rhp, rcu_perf_async_cb);
  409. rhp = NULL;
  410. } else if (!kthread_should_stop()) {
  411. rcu_perf_writer_state = RTWS_BARRIER;
  412. cur_ops->gp_barrier();
  413. goto retry;
  414. } else {
  415. kfree(rhp); /* Because we are stopping. */
  416. }
  417. } else if (gp_exp) {
  418. rcu_perf_writer_state = RTWS_EXP_SYNC;
  419. cur_ops->exp_sync();
  420. } else {
  421. rcu_perf_writer_state = RTWS_SYNC;
  422. cur_ops->sync();
  423. }
  424. rcu_perf_writer_state = RTWS_IDLE;
  425. t = ktime_get_mono_fast_ns();
  426. *wdp = t - *wdp;
  427. i_max = i;
  428. if (!started &&
  429. atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
  430. started = true;
  431. if (!done && i >= MIN_MEAS) {
  432. done = true;
  433. sp.sched_priority = 0;
  434. sched_setscheduler_nocheck(current,
  435. SCHED_NORMAL, &sp);
  436. pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
  437. perf_type, PERF_FLAG, me, MIN_MEAS);
  438. if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
  439. nrealwriters) {
  440. schedule_timeout_interruptible(10);
  441. rcu_ftrace_dump(DUMP_ALL);
  442. PERFOUT_STRING("Test complete");
  443. t_rcu_perf_writer_finished = t;
  444. if (gp_exp) {
  445. b_rcu_perf_writer_finished =
  446. cur_ops->exp_completed() / 2;
  447. } else {
  448. b_rcu_perf_writer_finished =
  449. cur_ops->completed();
  450. }
  451. if (shutdown) {
  452. smp_mb(); /* Assign before wake. */
  453. wake_up(&shutdown_wq);
  454. }
  455. }
  456. }
  457. if (done && !alldone &&
  458. atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
  459. alldone = true;
  460. if (started && !alldone && i < MAX_MEAS - 1)
  461. i++;
  462. rcu_perf_wait_shutdown();
  463. } while (!torture_must_stop());
  464. if (gp_async) {
  465. rcu_perf_writer_state = RTWS_BARRIER;
  466. cur_ops->gp_barrier();
  467. }
  468. rcu_perf_writer_state = RTWS_STOPPING;
  469. writer_n_durations[me] = i_max;
  470. torture_kthread_stopping("rcu_perf_writer");
  471. return 0;
  472. }
  473. static inline void
  474. rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
  475. {
  476. pr_alert("%s" PERF_FLAG
  477. "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
  478. perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
  479. }
  480. static void
  481. rcu_perf_cleanup(void)
  482. {
  483. int i;
  484. int j;
  485. int ngps = 0;
  486. u64 *wdp;
  487. u64 *wdpp;
  488. /*
  489. * Would like warning at start, but everything is expedited
  490. * during the mid-boot phase, so have to wait till the end.
  491. */
  492. if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
  493. VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
  494. if (rcu_gp_is_normal() && gp_exp)
  495. VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
  496. if (gp_exp && gp_async)
  497. VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
  498. if (torture_cleanup_begin())
  499. return;
  500. if (reader_tasks) {
  501. for (i = 0; i < nrealreaders; i++)
  502. torture_stop_kthread(rcu_perf_reader,
  503. reader_tasks[i]);
  504. kfree(reader_tasks);
  505. }
  506. if (writer_tasks) {
  507. for (i = 0; i < nrealwriters; i++) {
  508. torture_stop_kthread(rcu_perf_writer,
  509. writer_tasks[i]);
  510. if (!writer_n_durations)
  511. continue;
  512. j = writer_n_durations[i];
  513. pr_alert("%s%s writer %d gps: %d\n",
  514. perf_type, PERF_FLAG, i, j);
  515. ngps += j;
  516. }
  517. pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
  518. perf_type, PERF_FLAG,
  519. t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
  520. t_rcu_perf_writer_finished -
  521. t_rcu_perf_writer_started,
  522. ngps,
  523. b_rcu_perf_writer_finished -
  524. b_rcu_perf_writer_started);
  525. for (i = 0; i < nrealwriters; i++) {
  526. if (!writer_durations)
  527. break;
  528. if (!writer_n_durations)
  529. continue;
  530. wdpp = writer_durations[i];
  531. if (!wdpp)
  532. continue;
  533. for (j = 0; j <= writer_n_durations[i]; j++) {
  534. wdp = &wdpp[j];
  535. pr_alert("%s%s %4d writer-duration: %5d %llu\n",
  536. perf_type, PERF_FLAG,
  537. i, j, *wdp);
  538. if (j % 100 == 0)
  539. schedule_timeout_uninterruptible(1);
  540. }
  541. kfree(writer_durations[i]);
  542. }
  543. kfree(writer_tasks);
  544. kfree(writer_durations);
  545. kfree(writer_n_durations);
  546. }
  547. /* Do flavor-specific cleanup operations. */
  548. if (cur_ops->cleanup != NULL)
  549. cur_ops->cleanup();
  550. torture_cleanup_end();
  551. }
  552. /*
  553. * Return the number if non-negative. If -1, the number of CPUs.
  554. * If less than -1, that much less than the number of CPUs, but
  555. * at least one.
  556. */
  557. static int compute_real(int n)
  558. {
  559. int nr;
  560. if (n >= 0) {
  561. nr = n;
  562. } else {
  563. nr = num_online_cpus() + 1 + n;
  564. if (nr <= 0)
  565. nr = 1;
  566. }
  567. return nr;
  568. }
  569. /*
  570. * RCU perf shutdown kthread. Just waits to be awakened, then shuts
  571. * down system.
  572. */
  573. static int
  574. rcu_perf_shutdown(void *arg)
  575. {
  576. do {
  577. wait_event(shutdown_wq,
  578. atomic_read(&n_rcu_perf_writer_finished) >=
  579. nrealwriters);
  580. } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
  581. smp_mb(); /* Wake before output. */
  582. rcu_perf_cleanup();
  583. kernel_power_off();
  584. return -EINVAL;
  585. }
  586. static int __init
  587. rcu_perf_init(void)
  588. {
  589. long i;
  590. int firsterr = 0;
  591. static struct rcu_perf_ops *perf_ops[] = {
  592. &rcu_ops, &rcu_bh_ops, &srcu_ops, &srcud_ops, &sched_ops,
  593. &tasks_ops,
  594. };
  595. if (!torture_init_begin(perf_type, verbose))
  596. return -EBUSY;
  597. /* Process args and tell the world that the perf'er is on the job. */
  598. for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
  599. cur_ops = perf_ops[i];
  600. if (strcmp(perf_type, cur_ops->name) == 0)
  601. break;
  602. }
  603. if (i == ARRAY_SIZE(perf_ops)) {
  604. pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
  605. perf_type);
  606. pr_alert("rcu-perf types:");
  607. for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
  608. pr_alert(" %s", perf_ops[i]->name);
  609. pr_alert("\n");
  610. firsterr = -EINVAL;
  611. goto unwind;
  612. }
  613. if (cur_ops->init)
  614. cur_ops->init();
  615. nrealwriters = compute_real(nwriters);
  616. nrealreaders = compute_real(nreaders);
  617. atomic_set(&n_rcu_perf_reader_started, 0);
  618. atomic_set(&n_rcu_perf_writer_started, 0);
  619. atomic_set(&n_rcu_perf_writer_finished, 0);
  620. rcu_perf_print_module_parms(cur_ops, "Start of test");
  621. /* Start up the kthreads. */
  622. if (shutdown) {
  623. init_waitqueue_head(&shutdown_wq);
  624. firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
  625. shutdown_task);
  626. if (firsterr)
  627. goto unwind;
  628. schedule_timeout_uninterruptible(1);
  629. }
  630. reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
  631. GFP_KERNEL);
  632. if (reader_tasks == NULL) {
  633. VERBOSE_PERFOUT_ERRSTRING("out of memory");
  634. firsterr = -ENOMEM;
  635. goto unwind;
  636. }
  637. for (i = 0; i < nrealreaders; i++) {
  638. firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
  639. reader_tasks[i]);
  640. if (firsterr)
  641. goto unwind;
  642. }
  643. while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
  644. schedule_timeout_uninterruptible(1);
  645. writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
  646. GFP_KERNEL);
  647. writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
  648. GFP_KERNEL);
  649. writer_n_durations =
  650. kcalloc(nrealwriters, sizeof(*writer_n_durations),
  651. GFP_KERNEL);
  652. if (!writer_tasks || !writer_durations || !writer_n_durations) {
  653. VERBOSE_PERFOUT_ERRSTRING("out of memory");
  654. firsterr = -ENOMEM;
  655. goto unwind;
  656. }
  657. for (i = 0; i < nrealwriters; i++) {
  658. writer_durations[i] =
  659. kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
  660. GFP_KERNEL);
  661. if (!writer_durations[i]) {
  662. firsterr = -ENOMEM;
  663. goto unwind;
  664. }
  665. firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
  666. writer_tasks[i]);
  667. if (firsterr)
  668. goto unwind;
  669. }
  670. torture_init_end();
  671. return 0;
  672. unwind:
  673. torture_init_end();
  674. rcu_perf_cleanup();
  675. return firsterr;
  676. }
  677. module_init(rcu_perf_init);
  678. module_exit(rcu_perf_cleanup);