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. 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. #ifdef CONFIG_TASKS_RCU
  279. /*
  280. * Definitions for RCU-tasks perf testing.
  281. */
  282. static int tasks_perf_read_lock(void)
  283. {
  284. return 0;
  285. }
  286. static void tasks_perf_read_unlock(int idx)
  287. {
  288. }
  289. static struct rcu_perf_ops tasks_ops = {
  290. .ptype = RCU_TASKS_FLAVOR,
  291. .init = rcu_sync_perf_init,
  292. .readlock = tasks_perf_read_lock,
  293. .readunlock = tasks_perf_read_unlock,
  294. .started = rcu_no_completed,
  295. .completed = rcu_no_completed,
  296. .async = call_rcu_tasks,
  297. .gp_barrier = rcu_barrier_tasks,
  298. .sync = synchronize_rcu_tasks,
  299. .exp_sync = synchronize_rcu_tasks,
  300. .name = "tasks"
  301. };
  302. #define RCUPERF_TASKS_OPS &tasks_ops,
  303. static bool __maybe_unused torturing_tasks(void)
  304. {
  305. return cur_ops == &tasks_ops;
  306. }
  307. #else /* #ifdef CONFIG_TASKS_RCU */
  308. #define RCUPERF_TASKS_OPS
  309. static bool __maybe_unused torturing_tasks(void)
  310. {
  311. return false;
  312. }
  313. #endif /* #else #ifdef CONFIG_TASKS_RCU */
  314. /*
  315. * If performance tests complete, wait for shutdown to commence.
  316. */
  317. static void rcu_perf_wait_shutdown(void)
  318. {
  319. cond_resched_rcu_qs();
  320. if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
  321. return;
  322. while (!torture_must_stop())
  323. schedule_timeout_uninterruptible(1);
  324. }
  325. /*
  326. * RCU perf reader kthread. Repeatedly does empty RCU read-side
  327. * critical section, minimizing update-side interference.
  328. */
  329. static int
  330. rcu_perf_reader(void *arg)
  331. {
  332. unsigned long flags;
  333. int idx;
  334. long me = (long)arg;
  335. VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
  336. set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
  337. set_user_nice(current, MAX_NICE);
  338. atomic_inc(&n_rcu_perf_reader_started);
  339. do {
  340. local_irq_save(flags);
  341. idx = cur_ops->readlock();
  342. cur_ops->readunlock(idx);
  343. local_irq_restore(flags);
  344. rcu_perf_wait_shutdown();
  345. } while (!torture_must_stop());
  346. torture_kthread_stopping("rcu_perf_reader");
  347. return 0;
  348. }
  349. /*
  350. * Callback function for asynchronous grace periods from rcu_perf_writer().
  351. */
  352. static void rcu_perf_async_cb(struct rcu_head *rhp)
  353. {
  354. atomic_dec(this_cpu_ptr(&n_async_inflight));
  355. kfree(rhp);
  356. }
  357. /*
  358. * RCU perf writer kthread. Repeatedly does a grace period.
  359. */
  360. static int
  361. rcu_perf_writer(void *arg)
  362. {
  363. int i = 0;
  364. int i_max;
  365. long me = (long)arg;
  366. struct rcu_head *rhp = NULL;
  367. struct sched_param sp;
  368. bool started = false, done = false, alldone = false;
  369. u64 t;
  370. u64 *wdp;
  371. u64 *wdpp = writer_durations[me];
  372. VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
  373. WARN_ON(!wdpp);
  374. set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
  375. sp.sched_priority = 1;
  376. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  377. if (holdoff)
  378. schedule_timeout_uninterruptible(holdoff * HZ);
  379. t = ktime_get_mono_fast_ns();
  380. if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
  381. t_rcu_perf_writer_started = t;
  382. if (gp_exp) {
  383. b_rcu_perf_writer_started =
  384. cur_ops->exp_completed() / 2;
  385. } else {
  386. b_rcu_perf_writer_started =
  387. cur_ops->completed();
  388. }
  389. }
  390. do {
  391. if (writer_holdoff)
  392. udelay(writer_holdoff);
  393. wdp = &wdpp[i];
  394. *wdp = ktime_get_mono_fast_ns();
  395. if (gp_async) {
  396. retry:
  397. if (!rhp)
  398. rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
  399. if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
  400. rcu_perf_writer_state = RTWS_ASYNC;
  401. atomic_inc(this_cpu_ptr(&n_async_inflight));
  402. cur_ops->async(rhp, rcu_perf_async_cb);
  403. rhp = NULL;
  404. } else if (!kthread_should_stop()) {
  405. rcu_perf_writer_state = RTWS_BARRIER;
  406. cur_ops->gp_barrier();
  407. goto retry;
  408. } else {
  409. kfree(rhp); /* Because we are stopping. */
  410. }
  411. } else if (gp_exp) {
  412. rcu_perf_writer_state = RTWS_EXP_SYNC;
  413. cur_ops->exp_sync();
  414. } else {
  415. rcu_perf_writer_state = RTWS_SYNC;
  416. cur_ops->sync();
  417. }
  418. rcu_perf_writer_state = RTWS_IDLE;
  419. t = ktime_get_mono_fast_ns();
  420. *wdp = t - *wdp;
  421. i_max = i;
  422. if (!started &&
  423. atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
  424. started = true;
  425. if (!done && i >= MIN_MEAS) {
  426. done = true;
  427. sp.sched_priority = 0;
  428. sched_setscheduler_nocheck(current,
  429. SCHED_NORMAL, &sp);
  430. pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
  431. perf_type, PERF_FLAG, me, MIN_MEAS);
  432. if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
  433. nrealwriters) {
  434. schedule_timeout_interruptible(10);
  435. rcu_ftrace_dump(DUMP_ALL);
  436. PERFOUT_STRING("Test complete");
  437. t_rcu_perf_writer_finished = t;
  438. if (gp_exp) {
  439. b_rcu_perf_writer_finished =
  440. cur_ops->exp_completed() / 2;
  441. } else {
  442. b_rcu_perf_writer_finished =
  443. cur_ops->completed();
  444. }
  445. if (shutdown) {
  446. smp_mb(); /* Assign before wake. */
  447. wake_up(&shutdown_wq);
  448. }
  449. }
  450. }
  451. if (done && !alldone &&
  452. atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
  453. alldone = true;
  454. if (started && !alldone && i < MAX_MEAS - 1)
  455. i++;
  456. rcu_perf_wait_shutdown();
  457. } while (!torture_must_stop());
  458. if (gp_async) {
  459. rcu_perf_writer_state = RTWS_BARRIER;
  460. cur_ops->gp_barrier();
  461. }
  462. rcu_perf_writer_state = RTWS_STOPPING;
  463. writer_n_durations[me] = i_max;
  464. torture_kthread_stopping("rcu_perf_writer");
  465. return 0;
  466. }
  467. static inline void
  468. rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
  469. {
  470. pr_alert("%s" PERF_FLAG
  471. "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
  472. perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
  473. }
  474. static void
  475. rcu_perf_cleanup(void)
  476. {
  477. int i;
  478. int j;
  479. int ngps = 0;
  480. u64 *wdp;
  481. u64 *wdpp;
  482. /*
  483. * Would like warning at start, but everything is expedited
  484. * during the mid-boot phase, so have to wait till the end.
  485. */
  486. if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
  487. VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
  488. if (rcu_gp_is_normal() && gp_exp)
  489. VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
  490. if (gp_exp && gp_async)
  491. VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
  492. if (torture_cleanup_begin())
  493. return;
  494. if (reader_tasks) {
  495. for (i = 0; i < nrealreaders; i++)
  496. torture_stop_kthread(rcu_perf_reader,
  497. reader_tasks[i]);
  498. kfree(reader_tasks);
  499. }
  500. if (writer_tasks) {
  501. for (i = 0; i < nrealwriters; i++) {
  502. torture_stop_kthread(rcu_perf_writer,
  503. writer_tasks[i]);
  504. if (!writer_n_durations)
  505. continue;
  506. j = writer_n_durations[i];
  507. pr_alert("%s%s writer %d gps: %d\n",
  508. perf_type, PERF_FLAG, i, j);
  509. ngps += j;
  510. }
  511. pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
  512. perf_type, PERF_FLAG,
  513. t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
  514. t_rcu_perf_writer_finished -
  515. t_rcu_perf_writer_started,
  516. ngps,
  517. b_rcu_perf_writer_finished -
  518. b_rcu_perf_writer_started);
  519. for (i = 0; i < nrealwriters; i++) {
  520. if (!writer_durations)
  521. break;
  522. if (!writer_n_durations)
  523. continue;
  524. wdpp = writer_durations[i];
  525. if (!wdpp)
  526. continue;
  527. for (j = 0; j <= writer_n_durations[i]; j++) {
  528. wdp = &wdpp[j];
  529. pr_alert("%s%s %4d writer-duration: %5d %llu\n",
  530. perf_type, PERF_FLAG,
  531. i, j, *wdp);
  532. if (j % 100 == 0)
  533. schedule_timeout_uninterruptible(1);
  534. }
  535. kfree(writer_durations[i]);
  536. }
  537. kfree(writer_tasks);
  538. kfree(writer_durations);
  539. kfree(writer_n_durations);
  540. }
  541. /* Do flavor-specific cleanup operations. */
  542. if (cur_ops->cleanup != NULL)
  543. cur_ops->cleanup();
  544. torture_cleanup_end();
  545. }
  546. /*
  547. * Return the number if non-negative. If -1, the number of CPUs.
  548. * If less than -1, that much less than the number of CPUs, but
  549. * at least one.
  550. */
  551. static int compute_real(int n)
  552. {
  553. int nr;
  554. if (n >= 0) {
  555. nr = n;
  556. } else {
  557. nr = num_online_cpus() + 1 + n;
  558. if (nr <= 0)
  559. nr = 1;
  560. }
  561. return nr;
  562. }
  563. /*
  564. * RCU perf shutdown kthread. Just waits to be awakened, then shuts
  565. * down system.
  566. */
  567. static int
  568. rcu_perf_shutdown(void *arg)
  569. {
  570. do {
  571. wait_event(shutdown_wq,
  572. atomic_read(&n_rcu_perf_writer_finished) >=
  573. nrealwriters);
  574. } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
  575. smp_mb(); /* Wake before output. */
  576. rcu_perf_cleanup();
  577. kernel_power_off();
  578. return -EINVAL;
  579. }
  580. static int __init
  581. rcu_perf_init(void)
  582. {
  583. long i;
  584. int firsterr = 0;
  585. static struct rcu_perf_ops *perf_ops[] = {
  586. &rcu_ops, &rcu_bh_ops, &srcu_ops, &srcud_ops, &sched_ops,
  587. RCUPERF_TASKS_OPS
  588. };
  589. if (!torture_init_begin(perf_type, verbose, &perf_runnable))
  590. return -EBUSY;
  591. /* Process args and tell the world that the perf'er is on the job. */
  592. for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
  593. cur_ops = perf_ops[i];
  594. if (strcmp(perf_type, cur_ops->name) == 0)
  595. break;
  596. }
  597. if (i == ARRAY_SIZE(perf_ops)) {
  598. pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
  599. perf_type);
  600. pr_alert("rcu-perf types:");
  601. for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
  602. pr_alert(" %s", perf_ops[i]->name);
  603. pr_alert("\n");
  604. firsterr = -EINVAL;
  605. goto unwind;
  606. }
  607. if (cur_ops->init)
  608. cur_ops->init();
  609. nrealwriters = compute_real(nwriters);
  610. nrealreaders = compute_real(nreaders);
  611. atomic_set(&n_rcu_perf_reader_started, 0);
  612. atomic_set(&n_rcu_perf_writer_started, 0);
  613. atomic_set(&n_rcu_perf_writer_finished, 0);
  614. rcu_perf_print_module_parms(cur_ops, "Start of test");
  615. /* Start up the kthreads. */
  616. if (shutdown) {
  617. init_waitqueue_head(&shutdown_wq);
  618. firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
  619. shutdown_task);
  620. if (firsterr)
  621. goto unwind;
  622. schedule_timeout_uninterruptible(1);
  623. }
  624. reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
  625. GFP_KERNEL);
  626. if (reader_tasks == NULL) {
  627. VERBOSE_PERFOUT_ERRSTRING("out of memory");
  628. firsterr = -ENOMEM;
  629. goto unwind;
  630. }
  631. for (i = 0; i < nrealreaders; i++) {
  632. firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
  633. reader_tasks[i]);
  634. if (firsterr)
  635. goto unwind;
  636. }
  637. while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
  638. schedule_timeout_uninterruptible(1);
  639. writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
  640. GFP_KERNEL);
  641. writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
  642. GFP_KERNEL);
  643. writer_n_durations =
  644. kcalloc(nrealwriters, sizeof(*writer_n_durations),
  645. GFP_KERNEL);
  646. if (!writer_tasks || !writer_durations || !writer_n_durations) {
  647. VERBOSE_PERFOUT_ERRSTRING("out of memory");
  648. firsterr = -ENOMEM;
  649. goto unwind;
  650. }
  651. for (i = 0; i < nrealwriters; i++) {
  652. writer_durations[i] =
  653. kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
  654. GFP_KERNEL);
  655. if (!writer_durations[i]) {
  656. firsterr = -ENOMEM;
  657. goto unwind;
  658. }
  659. firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
  660. writer_tasks[i]);
  661. if (firsterr)
  662. goto unwind;
  663. }
  664. torture_init_end();
  665. return 0;
  666. unwind:
  667. torture_init_end();
  668. rcu_perf_cleanup();
  669. return firsterr;
  670. }
  671. module_init(rcu_perf_init);
  672. module_exit(rcu_perf_cleanup);