rcuperf.c 19 KB

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