blob: df29119b2013f1c3c7f716cc35ee7116ace5c0ca [file] [log] [blame]
Paul E. McKenney8704baa2015-12-31 18:33:22 -08001/*
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 */
Paul E. McKenney60500032018-05-15 12:25:05 -070022
23#define pr_fmt(fmt) fmt
24
Paul E. McKenney8704baa2015-12-31 18:33:22 -080025#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/kthread.h>
30#include <linux/err.h>
31#include <linux/spinlock.h>
32#include <linux/smp.h>
33#include <linux/rcupdate.h>
34#include <linux/interrupt.h>
35#include <linux/sched.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010036#include <uapi/linux/sched/types.h>
Paul E. McKenney8704baa2015-12-31 18:33:22 -080037#include <linux/atomic.h>
38#include <linux/bitops.h>
39#include <linux/completion.h>
40#include <linux/moduleparam.h>
41#include <linux/percpu.h>
42#include <linux/notifier.h>
43#include <linux/reboot.h>
44#include <linux/freezer.h>
45#include <linux/cpu.h>
46#include <linux/delay.h>
47#include <linux/stat.h>
48#include <linux/srcu.h>
49#include <linux/slab.h>
50#include <asm/byteorder.h>
51#include <linux/torture.h>
52#include <linux/vmalloc.h>
53
Paul E. McKenney25c36322017-05-03 09:51:55 -070054#include "rcu.h"
55
Paul E. McKenney8704baa2015-12-31 18:33:22 -080056MODULE_LICENSE("GPL");
57MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
58
59#define PERF_FLAG "-perf:"
60#define PERFOUT_STRING(s) \
SeongJae Parka56fefa2016-08-21 16:54:39 +090061 pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
Paul E. McKenney8704baa2015-12-31 18:33:22 -080062#define VERBOSE_PERFOUT_STRING(s) \
63 do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
64#define VERBOSE_PERFOUT_ERRSTRING(s) \
65 do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
66
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080067/*
68 * The intended use cases for the nreaders and nwriters module parameters
69 * are as follows:
70 *
71 * 1. Specify only the nr_cpus kernel boot parameter. This will
72 * set both nreaders and nwriters to the value specified by
73 * nr_cpus for a mixed reader/writer test.
74 *
75 * 2. Specify the nr_cpus kernel boot parameter, but set
76 * rcuperf.nreaders to zero. This will set nwriters to the
77 * value specified by nr_cpus for an update-only test.
78 *
79 * 3. Specify the nr_cpus kernel boot parameter, but set
80 * rcuperf.nwriters to zero. This will set nreaders to the
81 * value specified by nr_cpus for a read-only test.
82 *
83 * Various other use cases may of course be specified.
84 */
85
Paul E. McKenney881ed592017-04-17 12:47:10 -070086torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
87torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
Boqun Fengaf06d4f2016-05-25 09:25:33 +080088torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
Paul E. McKenneydf37e662016-01-30 20:56:38 -080089torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080090torture_param(int, nreaders, -1, "Number of RCU reader threads");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080091torture_param(int, nwriters, -1, "Number of RCU updater threads");
Paul E. McKenney492b95e2017-04-21 16:09:15 -070092torture_param(bool, shutdown, !IS_ENABLED(MODULE),
93 "Shutdown at end of performance tests.");
Paul E. McKenney90127d62018-05-09 10:29:18 -070094torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
Paul E. McKenney820687a2017-04-25 15:12:56 -070095torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080096
97static char *perf_type = "rcu";
98module_param(perf_type, charp, 0444);
99MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
100
101static int nrealreaders;
102static int nrealwriters;
103static struct task_struct **writer_tasks;
104static struct task_struct **reader_tasks;
105static struct task_struct *shutdown_task;
106
107static u64 **writer_durations;
108static int *writer_n_durations;
109static atomic_t n_rcu_perf_reader_started;
110static atomic_t n_rcu_perf_writer_started;
111static atomic_t n_rcu_perf_writer_finished;
112static wait_queue_head_t shutdown_wq;
113static u64 t_rcu_perf_writer_started;
114static u64 t_rcu_perf_writer_finished;
115static unsigned long b_rcu_perf_writer_started;
116static unsigned long b_rcu_perf_writer_finished;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700117static DEFINE_PER_CPU(atomic_t, n_async_inflight);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800118
119static int rcu_perf_writer_state;
120#define RTWS_INIT 0
Paul E. McKenney881ed592017-04-17 12:47:10 -0700121#define RTWS_ASYNC 1
122#define RTWS_BARRIER 2
123#define RTWS_EXP_SYNC 3
124#define RTWS_SYNC 4
125#define RTWS_IDLE 5
126#define RTWS_STOPPING 6
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800127
128#define MAX_MEAS 10000
129#define MIN_MEAS 100
130
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800131/*
132 * Operations vector for selecting different types of tests.
133 */
134
135struct rcu_perf_ops {
136 int ptype;
137 void (*init)(void);
138 void (*cleanup)(void);
139 int (*readlock)(void);
140 void (*readunlock)(int idx);
141 unsigned long (*started)(void);
142 unsigned long (*completed)(void);
143 unsigned long (*exp_completed)(void);
Paul E. McKenney881ed592017-04-17 12:47:10 -0700144 void (*async)(struct rcu_head *head, rcu_callback_t func);
145 void (*gp_barrier)(void);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800146 void (*sync)(void);
147 void (*exp_sync)(void);
148 const char *name;
149};
150
151static struct rcu_perf_ops *cur_ops;
152
153/*
154 * Definitions for rcu perf testing.
155 */
156
157static int rcu_perf_read_lock(void) __acquires(RCU)
158{
159 rcu_read_lock();
160 return 0;
161}
162
163static void rcu_perf_read_unlock(int idx) __releases(RCU)
164{
165 rcu_read_unlock();
166}
167
168static unsigned long __maybe_unused rcu_no_completed(void)
169{
170 return 0;
171}
172
173static void rcu_sync_perf_init(void)
174{
175}
176
177static struct rcu_perf_ops rcu_ops = {
178 .ptype = RCU_FLAVOR,
179 .init = rcu_sync_perf_init,
180 .readlock = rcu_perf_read_lock,
181 .readunlock = rcu_perf_read_unlock,
182 .started = rcu_batches_started,
183 .completed = rcu_batches_completed,
184 .exp_completed = rcu_exp_batches_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700185 .async = call_rcu,
186 .gp_barrier = rcu_barrier,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800187 .sync = synchronize_rcu,
188 .exp_sync = synchronize_rcu_expedited,
189 .name = "rcu"
190};
191
192/*
193 * Definitions for rcu_bh perf testing.
194 */
195
196static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH)
197{
198 rcu_read_lock_bh();
199 return 0;
200}
201
202static void rcu_bh_perf_read_unlock(int idx) __releases(RCU_BH)
203{
204 rcu_read_unlock_bh();
205}
206
207static struct rcu_perf_ops rcu_bh_ops = {
208 .ptype = RCU_BH_FLAVOR,
209 .init = rcu_sync_perf_init,
210 .readlock = rcu_bh_perf_read_lock,
211 .readunlock = rcu_bh_perf_read_unlock,
212 .started = rcu_batches_started_bh,
213 .completed = rcu_batches_completed_bh,
214 .exp_completed = rcu_exp_batches_completed_sched,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700215 .async = call_rcu_bh,
216 .gp_barrier = rcu_barrier_bh,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800217 .sync = synchronize_rcu_bh,
218 .exp_sync = synchronize_rcu_bh_expedited,
219 .name = "rcu_bh"
220};
221
222/*
223 * Definitions for srcu perf testing.
224 */
225
226DEFINE_STATIC_SRCU(srcu_ctl_perf);
227static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
228
229static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
230{
231 return srcu_read_lock(srcu_ctlp);
232}
233
234static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
235{
236 srcu_read_unlock(srcu_ctlp, idx);
237}
238
239static unsigned long srcu_perf_completed(void)
240{
241 return srcu_batches_completed(srcu_ctlp);
242}
243
Paul E. McKenney881ed592017-04-17 12:47:10 -0700244static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
245{
246 call_srcu(srcu_ctlp, head, func);
247}
248
249static void srcu_rcu_barrier(void)
250{
251 srcu_barrier(srcu_ctlp);
252}
253
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800254static void srcu_perf_synchronize(void)
255{
256 synchronize_srcu(srcu_ctlp);
257}
258
259static void srcu_perf_synchronize_expedited(void)
260{
261 synchronize_srcu_expedited(srcu_ctlp);
262}
263
264static struct rcu_perf_ops srcu_ops = {
265 .ptype = SRCU_FLAVOR,
266 .init = rcu_sync_perf_init,
267 .readlock = srcu_perf_read_lock,
268 .readunlock = srcu_perf_read_unlock,
269 .started = NULL,
270 .completed = srcu_perf_completed,
271 .exp_completed = srcu_perf_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700272 .async = srcu_call_rcu,
273 .gp_barrier = srcu_rcu_barrier,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800274 .sync = srcu_perf_synchronize,
275 .exp_sync = srcu_perf_synchronize_expedited,
276 .name = "srcu"
277};
278
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700279static struct srcu_struct srcud;
280
281static void srcu_sync_perf_init(void)
282{
283 srcu_ctlp = &srcud;
284 init_srcu_struct(srcu_ctlp);
285}
286
287static void srcu_sync_perf_cleanup(void)
288{
289 cleanup_srcu_struct(srcu_ctlp);
290}
291
292static struct rcu_perf_ops srcud_ops = {
293 .ptype = SRCU_FLAVOR,
294 .init = srcu_sync_perf_init,
295 .cleanup = srcu_sync_perf_cleanup,
296 .readlock = srcu_perf_read_lock,
297 .readunlock = srcu_perf_read_unlock,
298 .started = NULL,
299 .completed = srcu_perf_completed,
300 .exp_completed = srcu_perf_completed,
301 .async = srcu_call_rcu,
302 .gp_barrier = srcu_rcu_barrier,
303 .sync = srcu_perf_synchronize,
304 .exp_sync = srcu_perf_synchronize_expedited,
305 .name = "srcud"
306};
307
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800308/*
309 * Definitions for sched perf testing.
310 */
311
312static int sched_perf_read_lock(void)
313{
314 preempt_disable();
315 return 0;
316}
317
318static void sched_perf_read_unlock(int idx)
319{
320 preempt_enable();
321}
322
323static struct rcu_perf_ops sched_ops = {
324 .ptype = RCU_SCHED_FLAVOR,
325 .init = rcu_sync_perf_init,
326 .readlock = sched_perf_read_lock,
327 .readunlock = sched_perf_read_unlock,
328 .started = rcu_batches_started_sched,
329 .completed = rcu_batches_completed_sched,
330 .exp_completed = rcu_exp_batches_completed_sched,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700331 .async = call_rcu_sched,
332 .gp_barrier = rcu_barrier_sched,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800333 .sync = synchronize_sched,
334 .exp_sync = synchronize_sched_expedited,
335 .name = "sched"
336};
337
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800338/*
339 * Definitions for RCU-tasks perf testing.
340 */
341
342static int tasks_perf_read_lock(void)
343{
344 return 0;
345}
346
347static void tasks_perf_read_unlock(int idx)
348{
349}
350
351static struct rcu_perf_ops tasks_ops = {
352 .ptype = RCU_TASKS_FLAVOR,
353 .init = rcu_sync_perf_init,
354 .readlock = tasks_perf_read_lock,
355 .readunlock = tasks_perf_read_unlock,
356 .started = rcu_no_completed,
357 .completed = rcu_no_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700358 .async = call_rcu_tasks,
359 .gp_barrier = rcu_barrier_tasks,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800360 .sync = synchronize_rcu_tasks,
361 .exp_sync = synchronize_rcu_tasks,
362 .name = "tasks"
363};
364
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800365static bool __maybe_unused torturing_tasks(void)
366{
367 return cur_ops == &tasks_ops;
368}
369
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800370/*
371 * If performance tests complete, wait for shutdown to commence.
372 */
373static void rcu_perf_wait_shutdown(void)
374{
Paul E. McKenneycee43932018-03-02 16:35:27 -0800375 cond_resched_tasks_rcu_qs();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800376 if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
377 return;
378 while (!torture_must_stop())
379 schedule_timeout_uninterruptible(1);
380}
381
382/*
383 * RCU perf reader kthread. Repeatedly does empty RCU read-side
384 * critical section, minimizing update-side interference.
385 */
386static int
387rcu_perf_reader(void *arg)
388{
389 unsigned long flags;
390 int idx;
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800391 long me = (long)arg;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800392
393 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800394 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800395 set_user_nice(current, MAX_NICE);
396 atomic_inc(&n_rcu_perf_reader_started);
397
398 do {
399 local_irq_save(flags);
400 idx = cur_ops->readlock();
401 cur_ops->readunlock(idx);
402 local_irq_restore(flags);
403 rcu_perf_wait_shutdown();
404 } while (!torture_must_stop());
405 torture_kthread_stopping("rcu_perf_reader");
406 return 0;
407}
408
409/*
Paul E. McKenney881ed592017-04-17 12:47:10 -0700410 * Callback function for asynchronous grace periods from rcu_perf_writer().
411 */
412static void rcu_perf_async_cb(struct rcu_head *rhp)
413{
414 atomic_dec(this_cpu_ptr(&n_async_inflight));
415 kfree(rhp);
416}
417
418/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800419 * RCU perf writer kthread. Repeatedly does a grace period.
420 */
421static int
422rcu_perf_writer(void *arg)
423{
424 int i = 0;
425 int i_max;
426 long me = (long)arg;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700427 struct rcu_head *rhp = NULL;
Paul E. McKenney2094c992016-01-12 15:17:21 -0800428 struct sched_param sp;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800429 bool started = false, done = false, alldone = false;
430 u64 t;
431 u64 *wdp;
432 u64 *wdpp = writer_durations[me];
433
434 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800435 WARN_ON(!wdpp);
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800436 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney2094c992016-01-12 15:17:21 -0800437 sp.sched_priority = 1;
438 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
Paul E. McKenneydf37e662016-01-30 20:56:38 -0800439
440 if (holdoff)
441 schedule_timeout_uninterruptible(holdoff * HZ);
442
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800443 t = ktime_get_mono_fast_ns();
444 if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
445 t_rcu_perf_writer_started = t;
446 if (gp_exp) {
447 b_rcu_perf_writer_started =
448 cur_ops->exp_completed() / 2;
449 } else {
450 b_rcu_perf_writer_started =
451 cur_ops->completed();
452 }
453 }
454
455 do {
Paul E. McKenney820687a2017-04-25 15:12:56 -0700456 if (writer_holdoff)
457 udelay(writer_holdoff);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800458 wdp = &wdpp[i];
459 *wdp = ktime_get_mono_fast_ns();
Paul E. McKenney881ed592017-04-17 12:47:10 -0700460 if (gp_async) {
461retry:
462 if (!rhp)
463 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
464 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
465 rcu_perf_writer_state = RTWS_ASYNC;
466 atomic_inc(this_cpu_ptr(&n_async_inflight));
467 cur_ops->async(rhp, rcu_perf_async_cb);
468 rhp = NULL;
469 } else if (!kthread_should_stop()) {
470 rcu_perf_writer_state = RTWS_BARRIER;
471 cur_ops->gp_barrier();
472 goto retry;
473 } else {
474 kfree(rhp); /* Because we are stopping. */
475 }
476 } else if (gp_exp) {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800477 rcu_perf_writer_state = RTWS_EXP_SYNC;
478 cur_ops->exp_sync();
479 } else {
480 rcu_perf_writer_state = RTWS_SYNC;
481 cur_ops->sync();
482 }
483 rcu_perf_writer_state = RTWS_IDLE;
484 t = ktime_get_mono_fast_ns();
485 *wdp = t - *wdp;
486 i_max = i;
487 if (!started &&
488 atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
489 started = true;
490 if (!done && i >= MIN_MEAS) {
491 done = true;
Paul E. McKenney620316e2016-01-30 21:32:09 -0800492 sp.sched_priority = 0;
493 sched_setscheduler_nocheck(current,
494 SCHED_NORMAL, &sp);
SeongJae Parka56fefa2016-08-21 16:54:39 +0900495 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
496 perf_type, PERF_FLAG, me, MIN_MEAS);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800497 if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
498 nrealwriters) {
Paul E. McKenney620316e2016-01-30 21:32:09 -0800499 schedule_timeout_interruptible(10);
Paul E. McKenneyac2bb272016-01-29 14:58:17 -0800500 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800501 PERFOUT_STRING("Test complete");
502 t_rcu_perf_writer_finished = t;
503 if (gp_exp) {
504 b_rcu_perf_writer_finished =
505 cur_ops->exp_completed() / 2;
506 } else {
507 b_rcu_perf_writer_finished =
508 cur_ops->completed();
509 }
Artem Savkove6fb1fc2016-02-07 13:31:39 +0100510 if (shutdown) {
511 smp_mb(); /* Assign before wake. */
512 wake_up(&shutdown_wq);
513 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800514 }
515 }
516 if (done && !alldone &&
517 atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
518 alldone = true;
519 if (started && !alldone && i < MAX_MEAS - 1)
520 i++;
521 rcu_perf_wait_shutdown();
522 } while (!torture_must_stop());
Paul E. McKenney881ed592017-04-17 12:47:10 -0700523 if (gp_async) {
524 rcu_perf_writer_state = RTWS_BARRIER;
525 cur_ops->gp_barrier();
526 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800527 rcu_perf_writer_state = RTWS_STOPPING;
528 writer_n_durations[me] = i_max;
529 torture_kthread_stopping("rcu_perf_writer");
530 return 0;
531}
532
533static inline void
534rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
535{
536 pr_alert("%s" PERF_FLAG
537 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
538 perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
539}
540
541static void
542rcu_perf_cleanup(void)
543{
544 int i;
545 int j;
546 int ngps = 0;
547 u64 *wdp;
548 u64 *wdpp;
549
Paul E. McKenney96839372017-04-14 16:12:52 -0700550 /*
551 * Would like warning at start, but everything is expedited
552 * during the mid-boot phase, so have to wait till the end.
553 */
554 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
555 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
556 if (rcu_gp_is_normal() && gp_exp)
557 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
Paul E. McKenney881ed592017-04-17 12:47:10 -0700558 if (gp_exp && gp_async)
559 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
Paul E. McKenney96839372017-04-14 16:12:52 -0700560
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800561 if (torture_cleanup_begin())
562 return;
563
564 if (reader_tasks) {
565 for (i = 0; i < nrealreaders; i++)
566 torture_stop_kthread(rcu_perf_reader,
567 reader_tasks[i]);
568 kfree(reader_tasks);
569 }
570
571 if (writer_tasks) {
572 for (i = 0; i < nrealwriters; i++) {
573 torture_stop_kthread(rcu_perf_writer,
574 writer_tasks[i]);
575 if (!writer_n_durations)
576 continue;
577 j = writer_n_durations[i];
578 pr_alert("%s%s writer %d gps: %d\n",
579 perf_type, PERF_FLAG, i, j);
580 ngps += j;
581 }
582 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
583 perf_type, PERF_FLAG,
584 t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
585 t_rcu_perf_writer_finished -
586 t_rcu_perf_writer_started,
587 ngps,
588 b_rcu_perf_writer_finished -
589 b_rcu_perf_writer_started);
590 for (i = 0; i < nrealwriters; i++) {
591 if (!writer_durations)
592 break;
593 if (!writer_n_durations)
594 continue;
595 wdpp = writer_durations[i];
596 if (!wdpp)
597 continue;
598 for (j = 0; j <= writer_n_durations[i]; j++) {
599 wdp = &wdpp[j];
600 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
601 perf_type, PERF_FLAG,
602 i, j, *wdp);
603 if (j % 100 == 0)
604 schedule_timeout_uninterruptible(1);
605 }
606 kfree(writer_durations[i]);
607 }
608 kfree(writer_tasks);
609 kfree(writer_durations);
610 kfree(writer_n_durations);
611 }
612
613 /* Do flavor-specific cleanup operations. */
614 if (cur_ops->cleanup != NULL)
615 cur_ops->cleanup();
616
617 torture_cleanup_end();
618}
619
620/*
621 * Return the number if non-negative. If -1, the number of CPUs.
622 * If less than -1, that much less than the number of CPUs, but
623 * at least one.
624 */
625static int compute_real(int n)
626{
627 int nr;
628
629 if (n >= 0) {
630 nr = n;
631 } else {
632 nr = num_online_cpus() + 1 + n;
633 if (nr <= 0)
634 nr = 1;
635 }
636 return nr;
637}
638
639/*
640 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
641 * down system.
642 */
643static int
644rcu_perf_shutdown(void *arg)
645{
646 do {
647 wait_event(shutdown_wq,
648 atomic_read(&n_rcu_perf_writer_finished) >=
649 nrealwriters);
650 } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
651 smp_mb(); /* Wake before output. */
652 rcu_perf_cleanup();
653 kernel_power_off();
654 return -EINVAL;
655}
656
657static int __init
658rcu_perf_init(void)
659{
660 long i;
661 int firsterr = 0;
662 static struct rcu_perf_ops *perf_ops[] = {
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700663 &rcu_ops, &rcu_bh_ops, &srcu_ops, &srcud_ops, &sched_ops,
Paul E. McKenneyf1dbc542017-05-25 08:23:06 -0700664 &tasks_ops,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800665 };
666
Paul E. McKenneya2f25772017-11-21 20:19:17 -0800667 if (!torture_init_begin(perf_type, verbose))
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800668 return -EBUSY;
669
670 /* Process args and tell the world that the perf'er is on the job. */
671 for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
672 cur_ops = perf_ops[i];
673 if (strcmp(perf_type, cur_ops->name) == 0)
674 break;
675 }
676 if (i == ARRAY_SIZE(perf_ops)) {
677 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
678 perf_type);
679 pr_alert("rcu-perf types:");
680 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
681 pr_alert(" %s", perf_ops[i]->name);
682 pr_alert("\n");
683 firsterr = -EINVAL;
684 goto unwind;
685 }
686 if (cur_ops->init)
687 cur_ops->init();
688
689 nrealwriters = compute_real(nwriters);
690 nrealreaders = compute_real(nreaders);
691 atomic_set(&n_rcu_perf_reader_started, 0);
692 atomic_set(&n_rcu_perf_writer_started, 0);
693 atomic_set(&n_rcu_perf_writer_finished, 0);
694 rcu_perf_print_module_parms(cur_ops, "Start of test");
695
696 /* Start up the kthreads. */
697
698 if (shutdown) {
699 init_waitqueue_head(&shutdown_wq);
700 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
701 shutdown_task);
702 if (firsterr)
703 goto unwind;
704 schedule_timeout_uninterruptible(1);
705 }
706 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
707 GFP_KERNEL);
708 if (reader_tasks == NULL) {
709 VERBOSE_PERFOUT_ERRSTRING("out of memory");
710 firsterr = -ENOMEM;
711 goto unwind;
712 }
713 for (i = 0; i < nrealreaders; i++) {
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800714 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800715 reader_tasks[i]);
716 if (firsterr)
717 goto unwind;
718 }
719 while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
720 schedule_timeout_uninterruptible(1);
721 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
722 GFP_KERNEL);
723 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
724 GFP_KERNEL);
725 writer_n_durations =
726 kcalloc(nrealwriters, sizeof(*writer_n_durations),
727 GFP_KERNEL);
728 if (!writer_tasks || !writer_durations || !writer_n_durations) {
729 VERBOSE_PERFOUT_ERRSTRING("out of memory");
730 firsterr = -ENOMEM;
731 goto unwind;
732 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800733 for (i = 0; i < nrealwriters; i++) {
734 writer_durations[i] =
735 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
736 GFP_KERNEL);
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000737 if (!writer_durations[i]) {
738 firsterr = -ENOMEM;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800739 goto unwind;
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000740 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800741 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
742 writer_tasks[i]);
743 if (firsterr)
744 goto unwind;
745 }
746 torture_init_end();
747 return 0;
748
749unwind:
750 torture_init_end();
751 rcu_perf_cleanup();
752 return firsterr;
753}
754
755module_init(rcu_perf_init);
756module_exit(rcu_perf_cleanup);