rcuperf.c 18 KB

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