blob: 83c411cb09bd34d553eabc6e49fdc44c169ee040 [file] [log] [blame]
Paul E. McKenney8bf05ed2019-01-17 10:09:19 -08001// SPDX-License-Identifier: GPL-2.0+
Paul E. McKenney8704baa2015-12-31 18:33:22 -08002/*
3 * Read-Copy Update module-based performance-test facility
4 *
Paul E. McKenney8704baa2015-12-31 18:33:22 -08005 * Copyright (C) IBM Corporation, 2015
6 *
Paul E. McKenney8bf05ed2019-01-17 10:09:19 -08007 * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
Paul E. McKenney8704baa2015-12-31 18:33:22 -08008 */
Paul E. McKenney60500032018-05-15 12:25:05 -07009
10#define pr_fmt(fmt) fmt
11
Paul E. McKenney8704baa2015-12-31 18:33:22 -080012#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/kthread.h>
17#include <linux/err.h>
18#include <linux/spinlock.h>
19#include <linux/smp.h>
20#include <linux/rcupdate.h>
21#include <linux/interrupt.h>
22#include <linux/sched.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010023#include <uapi/linux/sched/types.h>
Paul E. McKenney8704baa2015-12-31 18:33:22 -080024#include <linux/atomic.h>
25#include <linux/bitops.h>
26#include <linux/completion.h>
27#include <linux/moduleparam.h>
28#include <linux/percpu.h>
29#include <linux/notifier.h>
30#include <linux/reboot.h>
31#include <linux/freezer.h>
32#include <linux/cpu.h>
33#include <linux/delay.h>
34#include <linux/stat.h>
35#include <linux/srcu.h>
36#include <linux/slab.h>
37#include <asm/byteorder.h>
38#include <linux/torture.h>
39#include <linux/vmalloc.h>
40
Paul E. McKenney25c36322017-05-03 09:51:55 -070041#include "rcu.h"
42
Paul E. McKenney8704baa2015-12-31 18:33:22 -080043MODULE_LICENSE("GPL");
Paul E. McKenney8bf05ed2019-01-17 10:09:19 -080044MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080045
46#define PERF_FLAG "-perf:"
47#define PERFOUT_STRING(s) \
SeongJae Parka56fefa2016-08-21 16:54:39 +090048 pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
Paul E. McKenney8704baa2015-12-31 18:33:22 -080049#define VERBOSE_PERFOUT_STRING(s) \
50 do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
51#define VERBOSE_PERFOUT_ERRSTRING(s) \
52 do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
53
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080054/*
55 * The intended use cases for the nreaders and nwriters module parameters
56 * are as follows:
57 *
58 * 1. Specify only the nr_cpus kernel boot parameter. This will
59 * set both nreaders and nwriters to the value specified by
60 * nr_cpus for a mixed reader/writer test.
61 *
62 * 2. Specify the nr_cpus kernel boot parameter, but set
63 * rcuperf.nreaders to zero. This will set nwriters to the
64 * value specified by nr_cpus for an update-only test.
65 *
66 * 3. Specify the nr_cpus kernel boot parameter, but set
67 * rcuperf.nwriters to zero. This will set nreaders to the
68 * value specified by nr_cpus for a read-only test.
69 *
70 * Various other use cases may of course be specified.
71 */
72
Paul E. McKenney881ed592017-04-17 12:47:10 -070073torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
74torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
Boqun Fengaf06d4f2016-05-25 09:25:33 +080075torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
Paul E. McKenneydf37e662016-01-30 20:56:38 -080076torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080077torture_param(int, nreaders, -1, "Number of RCU reader threads");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080078torture_param(int, nwriters, -1, "Number of RCU updater threads");
Paul E. McKenney492b95e2017-04-21 16:09:15 -070079torture_param(bool, shutdown, !IS_ENABLED(MODULE),
80 "Shutdown at end of performance tests.");
Paul E. McKenney90127d62018-05-09 10:29:18 -070081torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
Paul E. McKenney820687a2017-04-25 15:12:56 -070082torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080083
84static char *perf_type = "rcu";
85module_param(perf_type, charp, 0444);
86MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
87
88static int nrealreaders;
89static int nrealwriters;
90static struct task_struct **writer_tasks;
91static struct task_struct **reader_tasks;
92static struct task_struct *shutdown_task;
93
94static u64 **writer_durations;
95static int *writer_n_durations;
96static atomic_t n_rcu_perf_reader_started;
97static atomic_t n_rcu_perf_writer_started;
98static atomic_t n_rcu_perf_writer_finished;
99static wait_queue_head_t shutdown_wq;
100static u64 t_rcu_perf_writer_started;
101static u64 t_rcu_perf_writer_finished;
102static unsigned long b_rcu_perf_writer_started;
103static unsigned long b_rcu_perf_writer_finished;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700104static DEFINE_PER_CPU(atomic_t, n_async_inflight);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800105
106static int rcu_perf_writer_state;
107#define RTWS_INIT 0
Paul E. McKenney881ed592017-04-17 12:47:10 -0700108#define RTWS_ASYNC 1
109#define RTWS_BARRIER 2
110#define RTWS_EXP_SYNC 3
111#define RTWS_SYNC 4
112#define RTWS_IDLE 5
113#define RTWS_STOPPING 6
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800114
115#define MAX_MEAS 10000
116#define MIN_MEAS 100
117
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800118/*
119 * Operations vector for selecting different types of tests.
120 */
121
122struct rcu_perf_ops {
123 int ptype;
124 void (*init)(void);
125 void (*cleanup)(void);
126 int (*readlock)(void);
127 void (*readunlock)(int idx);
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700128 unsigned long (*get_gp_seq)(void);
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700129 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800130 unsigned long (*exp_completed)(void);
Paul E. McKenney881ed592017-04-17 12:47:10 -0700131 void (*async)(struct rcu_head *head, rcu_callback_t func);
132 void (*gp_barrier)(void);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800133 void (*sync)(void);
134 void (*exp_sync)(void);
135 const char *name;
136};
137
138static struct rcu_perf_ops *cur_ops;
139
140/*
141 * Definitions for rcu perf testing.
142 */
143
144static int rcu_perf_read_lock(void) __acquires(RCU)
145{
146 rcu_read_lock();
147 return 0;
148}
149
150static void rcu_perf_read_unlock(int idx) __releases(RCU)
151{
152 rcu_read_unlock();
153}
154
155static unsigned long __maybe_unused rcu_no_completed(void)
156{
157 return 0;
158}
159
160static void rcu_sync_perf_init(void)
161{
162}
163
164static struct rcu_perf_ops rcu_ops = {
165 .ptype = RCU_FLAVOR,
166 .init = rcu_sync_perf_init,
167 .readlock = rcu_perf_read_lock,
168 .readunlock = rcu_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700169 .get_gp_seq = rcu_get_gp_seq,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700170 .gp_diff = rcu_seq_diff,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800171 .exp_completed = rcu_exp_batches_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700172 .async = call_rcu,
173 .gp_barrier = rcu_barrier,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800174 .sync = synchronize_rcu,
175 .exp_sync = synchronize_rcu_expedited,
176 .name = "rcu"
177};
178
179/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800180 * Definitions for srcu perf testing.
181 */
182
183DEFINE_STATIC_SRCU(srcu_ctl_perf);
184static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
185
186static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
187{
188 return srcu_read_lock(srcu_ctlp);
189}
190
191static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
192{
193 srcu_read_unlock(srcu_ctlp, idx);
194}
195
196static unsigned long srcu_perf_completed(void)
197{
198 return srcu_batches_completed(srcu_ctlp);
199}
200
Paul E. McKenney881ed592017-04-17 12:47:10 -0700201static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
202{
203 call_srcu(srcu_ctlp, head, func);
204}
205
206static void srcu_rcu_barrier(void)
207{
208 srcu_barrier(srcu_ctlp);
209}
210
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800211static void srcu_perf_synchronize(void)
212{
213 synchronize_srcu(srcu_ctlp);
214}
215
216static void srcu_perf_synchronize_expedited(void)
217{
218 synchronize_srcu_expedited(srcu_ctlp);
219}
220
221static struct rcu_perf_ops srcu_ops = {
222 .ptype = SRCU_FLAVOR,
223 .init = rcu_sync_perf_init,
224 .readlock = srcu_perf_read_lock,
225 .readunlock = srcu_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700226 .get_gp_seq = srcu_perf_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700227 .gp_diff = rcu_seq_diff,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800228 .exp_completed = srcu_perf_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700229 .async = srcu_call_rcu,
230 .gp_barrier = srcu_rcu_barrier,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800231 .sync = srcu_perf_synchronize,
232 .exp_sync = srcu_perf_synchronize_expedited,
233 .name = "srcu"
234};
235
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700236static struct srcu_struct srcud;
237
238static void srcu_sync_perf_init(void)
239{
240 srcu_ctlp = &srcud;
241 init_srcu_struct(srcu_ctlp);
242}
243
244static void srcu_sync_perf_cleanup(void)
245{
246 cleanup_srcu_struct(srcu_ctlp);
247}
248
249static struct rcu_perf_ops srcud_ops = {
250 .ptype = SRCU_FLAVOR,
251 .init = srcu_sync_perf_init,
252 .cleanup = srcu_sync_perf_cleanup,
253 .readlock = srcu_perf_read_lock,
254 .readunlock = srcu_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700255 .get_gp_seq = srcu_perf_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700256 .gp_diff = rcu_seq_diff,
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700257 .exp_completed = srcu_perf_completed,
258 .async = srcu_call_rcu,
259 .gp_barrier = srcu_rcu_barrier,
260 .sync = srcu_perf_synchronize,
261 .exp_sync = srcu_perf_synchronize_expedited,
262 .name = "srcud"
263};
264
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800265/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800266 * Definitions for RCU-tasks perf testing.
267 */
268
269static int tasks_perf_read_lock(void)
270{
271 return 0;
272}
273
274static void tasks_perf_read_unlock(int idx)
275{
276}
277
278static struct rcu_perf_ops tasks_ops = {
279 .ptype = RCU_TASKS_FLAVOR,
280 .init = rcu_sync_perf_init,
281 .readlock = tasks_perf_read_lock,
282 .readunlock = tasks_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700283 .get_gp_seq = rcu_no_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700284 .gp_diff = rcu_seq_diff,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700285 .async = call_rcu_tasks,
286 .gp_barrier = rcu_barrier_tasks,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800287 .sync = synchronize_rcu_tasks,
288 .exp_sync = synchronize_rcu_tasks,
289 .name = "tasks"
290};
291
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700292static unsigned long rcuperf_seq_diff(unsigned long new, unsigned long old)
293{
294 if (!cur_ops->gp_diff)
295 return new - old;
296 return cur_ops->gp_diff(new, old);
297}
298
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800299/*
300 * If performance tests complete, wait for shutdown to commence.
301 */
302static void rcu_perf_wait_shutdown(void)
303{
Paul E. McKenneycee43932018-03-02 16:35:27 -0800304 cond_resched_tasks_rcu_qs();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800305 if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
306 return;
307 while (!torture_must_stop())
308 schedule_timeout_uninterruptible(1);
309}
310
311/*
312 * RCU perf reader kthread. Repeatedly does empty RCU read-side
313 * critical section, minimizing update-side interference.
314 */
315static int
316rcu_perf_reader(void *arg)
317{
318 unsigned long flags;
319 int idx;
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800320 long me = (long)arg;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800321
322 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800323 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800324 set_user_nice(current, MAX_NICE);
325 atomic_inc(&n_rcu_perf_reader_started);
326
327 do {
328 local_irq_save(flags);
329 idx = cur_ops->readlock();
330 cur_ops->readunlock(idx);
331 local_irq_restore(flags);
332 rcu_perf_wait_shutdown();
333 } while (!torture_must_stop());
334 torture_kthread_stopping("rcu_perf_reader");
335 return 0;
336}
337
338/*
Paul E. McKenney881ed592017-04-17 12:47:10 -0700339 * Callback function for asynchronous grace periods from rcu_perf_writer().
340 */
341static void rcu_perf_async_cb(struct rcu_head *rhp)
342{
343 atomic_dec(this_cpu_ptr(&n_async_inflight));
344 kfree(rhp);
345}
346
347/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800348 * RCU perf writer kthread. Repeatedly does a grace period.
349 */
350static int
351rcu_perf_writer(void *arg)
352{
353 int i = 0;
354 int i_max;
355 long me = (long)arg;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700356 struct rcu_head *rhp = NULL;
Paul E. McKenney2094c992016-01-12 15:17:21 -0800357 struct sched_param sp;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800358 bool started = false, done = false, alldone = false;
359 u64 t;
360 u64 *wdp;
361 u64 *wdpp = writer_durations[me];
362
363 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800364 WARN_ON(!wdpp);
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800365 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney2094c992016-01-12 15:17:21 -0800366 sp.sched_priority = 1;
367 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
Paul E. McKenneydf37e662016-01-30 20:56:38 -0800368
369 if (holdoff)
370 schedule_timeout_uninterruptible(holdoff * HZ);
371
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800372 t = ktime_get_mono_fast_ns();
373 if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
374 t_rcu_perf_writer_started = t;
375 if (gp_exp) {
376 b_rcu_perf_writer_started =
377 cur_ops->exp_completed() / 2;
378 } else {
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700379 b_rcu_perf_writer_started = cur_ops->get_gp_seq();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800380 }
381 }
382
383 do {
Paul E. McKenney820687a2017-04-25 15:12:56 -0700384 if (writer_holdoff)
385 udelay(writer_holdoff);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800386 wdp = &wdpp[i];
387 *wdp = ktime_get_mono_fast_ns();
Paul E. McKenney881ed592017-04-17 12:47:10 -0700388 if (gp_async) {
389retry:
390 if (!rhp)
391 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
392 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
393 rcu_perf_writer_state = RTWS_ASYNC;
394 atomic_inc(this_cpu_ptr(&n_async_inflight));
395 cur_ops->async(rhp, rcu_perf_async_cb);
396 rhp = NULL;
397 } else if (!kthread_should_stop()) {
398 rcu_perf_writer_state = RTWS_BARRIER;
399 cur_ops->gp_barrier();
400 goto retry;
401 } else {
402 kfree(rhp); /* Because we are stopping. */
403 }
404 } else if (gp_exp) {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800405 rcu_perf_writer_state = RTWS_EXP_SYNC;
406 cur_ops->exp_sync();
407 } else {
408 rcu_perf_writer_state = RTWS_SYNC;
409 cur_ops->sync();
410 }
411 rcu_perf_writer_state = RTWS_IDLE;
412 t = ktime_get_mono_fast_ns();
413 *wdp = t - *wdp;
414 i_max = i;
415 if (!started &&
416 atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
417 started = true;
418 if (!done && i >= MIN_MEAS) {
419 done = true;
Paul E. McKenney620316e2016-01-30 21:32:09 -0800420 sp.sched_priority = 0;
421 sched_setscheduler_nocheck(current,
422 SCHED_NORMAL, &sp);
SeongJae Parka56fefa2016-08-21 16:54:39 +0900423 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
424 perf_type, PERF_FLAG, me, MIN_MEAS);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800425 if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
426 nrealwriters) {
Paul E. McKenney620316e2016-01-30 21:32:09 -0800427 schedule_timeout_interruptible(10);
Paul E. McKenneyac2bb272016-01-29 14:58:17 -0800428 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800429 PERFOUT_STRING("Test complete");
430 t_rcu_perf_writer_finished = t;
431 if (gp_exp) {
432 b_rcu_perf_writer_finished =
433 cur_ops->exp_completed() / 2;
434 } else {
435 b_rcu_perf_writer_finished =
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700436 cur_ops->get_gp_seq();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800437 }
Artem Savkove6fb1fc2016-02-07 13:31:39 +0100438 if (shutdown) {
439 smp_mb(); /* Assign before wake. */
440 wake_up(&shutdown_wq);
441 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800442 }
443 }
444 if (done && !alldone &&
445 atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
446 alldone = true;
447 if (started && !alldone && i < MAX_MEAS - 1)
448 i++;
449 rcu_perf_wait_shutdown();
450 } while (!torture_must_stop());
Paul E. McKenney881ed592017-04-17 12:47:10 -0700451 if (gp_async) {
452 rcu_perf_writer_state = RTWS_BARRIER;
453 cur_ops->gp_barrier();
454 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800455 rcu_perf_writer_state = RTWS_STOPPING;
456 writer_n_durations[me] = i_max;
457 torture_kthread_stopping("rcu_perf_writer");
458 return 0;
459}
460
Paul E. McKenney96221792018-05-17 11:33:17 -0700461static void
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800462rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
463{
464 pr_alert("%s" PERF_FLAG
465 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
466 perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
467}
468
469static void
470rcu_perf_cleanup(void)
471{
472 int i;
473 int j;
474 int ngps = 0;
475 u64 *wdp;
476 u64 *wdpp;
477
Paul E. McKenney96839372017-04-14 16:12:52 -0700478 /*
479 * Would like warning at start, but everything is expedited
480 * during the mid-boot phase, so have to wait till the end.
481 */
482 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
483 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
484 if (rcu_gp_is_normal() && gp_exp)
485 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
Paul E. McKenney881ed592017-04-17 12:47:10 -0700486 if (gp_exp && gp_async)
487 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
Paul E. McKenney96839372017-04-14 16:12:52 -0700488
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800489 if (torture_cleanup_begin())
490 return;
491
492 if (reader_tasks) {
493 for (i = 0; i < nrealreaders; i++)
494 torture_stop_kthread(rcu_perf_reader,
495 reader_tasks[i]);
496 kfree(reader_tasks);
497 }
498
499 if (writer_tasks) {
500 for (i = 0; i < nrealwriters; i++) {
501 torture_stop_kthread(rcu_perf_writer,
502 writer_tasks[i]);
503 if (!writer_n_durations)
504 continue;
505 j = writer_n_durations[i];
506 pr_alert("%s%s writer %d gps: %d\n",
507 perf_type, PERF_FLAG, i, j);
508 ngps += j;
509 }
510 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
511 perf_type, PERF_FLAG,
512 t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
513 t_rcu_perf_writer_finished -
514 t_rcu_perf_writer_started,
515 ngps,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700516 rcuperf_seq_diff(b_rcu_perf_writer_finished,
517 b_rcu_perf_writer_started));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800518 for (i = 0; i < nrealwriters; i++) {
519 if (!writer_durations)
520 break;
521 if (!writer_n_durations)
522 continue;
523 wdpp = writer_durations[i];
524 if (!wdpp)
525 continue;
526 for (j = 0; j <= writer_n_durations[i]; j++) {
527 wdp = &wdpp[j];
528 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
529 perf_type, PERF_FLAG,
530 i, j, *wdp);
531 if (j % 100 == 0)
532 schedule_timeout_uninterruptible(1);
533 }
534 kfree(writer_durations[i]);
535 }
536 kfree(writer_tasks);
537 kfree(writer_durations);
538 kfree(writer_n_durations);
539 }
540
Paul E. McKenney620d2462018-07-07 18:25:10 -0700541 /* Do torture-type-specific cleanup operations. */
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800542 if (cur_ops->cleanup != NULL)
543 cur_ops->cleanup();
544
545 torture_cleanup_end();
546}
547
548/*
549 * Return the number if non-negative. If -1, the number of CPUs.
550 * If less than -1, that much less than the number of CPUs, but
551 * at least one.
552 */
553static int compute_real(int n)
554{
555 int nr;
556
557 if (n >= 0) {
558 nr = n;
559 } else {
560 nr = num_online_cpus() + 1 + n;
561 if (nr <= 0)
562 nr = 1;
563 }
564 return nr;
565}
566
567/*
568 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
569 * down system.
570 */
571static int
572rcu_perf_shutdown(void *arg)
573{
574 do {
575 wait_event(shutdown_wq,
576 atomic_read(&n_rcu_perf_writer_finished) >=
577 nrealwriters);
578 } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
579 smp_mb(); /* Wake before output. */
580 rcu_perf_cleanup();
581 kernel_power_off();
582 return -EINVAL;
583}
584
585static int __init
586rcu_perf_init(void)
587{
588 long i;
589 int firsterr = 0;
590 static struct rcu_perf_ops *perf_ops[] = {
Paul E. McKenney620d2462018-07-07 18:25:10 -0700591 &rcu_ops, &srcu_ops, &srcud_ops, &tasks_ops,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800592 };
593
Paul E. McKenneya2f25772017-11-21 20:19:17 -0800594 if (!torture_init_begin(perf_type, verbose))
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800595 return -EBUSY;
596
597 /* Process args and tell the world that the perf'er is on the job. */
598 for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
599 cur_ops = perf_ops[i];
600 if (strcmp(perf_type, cur_ops->name) == 0)
601 break;
602 }
603 if (i == ARRAY_SIZE(perf_ops)) {
Joe Perchesa7538352018-05-14 13:27:33 -0700604 pr_alert("rcu-perf: invalid perf type: \"%s\"\n", perf_type);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800605 pr_alert("rcu-perf types:");
606 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
Joe Perchesa7538352018-05-14 13:27:33 -0700607 pr_cont(" %s", perf_ops[i]->name);
608 pr_cont("\n");
Paul E. McKenneyf0288062018-07-07 18:26:50 -0700609 WARN_ON(!IS_MODULE(CONFIG_RCU_PERF_TEST));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800610 firsterr = -EINVAL;
611 goto unwind;
612 }
613 if (cur_ops->init)
614 cur_ops->init();
615
616 nrealwriters = compute_real(nwriters);
617 nrealreaders = compute_real(nreaders);
618 atomic_set(&n_rcu_perf_reader_started, 0);
619 atomic_set(&n_rcu_perf_writer_started, 0);
620 atomic_set(&n_rcu_perf_writer_finished, 0);
621 rcu_perf_print_module_parms(cur_ops, "Start of test");
622
623 /* Start up the kthreads. */
624
625 if (shutdown) {
626 init_waitqueue_head(&shutdown_wq);
627 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
628 shutdown_task);
629 if (firsterr)
630 goto unwind;
631 schedule_timeout_uninterruptible(1);
632 }
633 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
634 GFP_KERNEL);
635 if (reader_tasks == NULL) {
636 VERBOSE_PERFOUT_ERRSTRING("out of memory");
637 firsterr = -ENOMEM;
638 goto unwind;
639 }
640 for (i = 0; i < nrealreaders; i++) {
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800641 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800642 reader_tasks[i]);
643 if (firsterr)
644 goto unwind;
645 }
646 while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
647 schedule_timeout_uninterruptible(1);
648 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
649 GFP_KERNEL);
650 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
651 GFP_KERNEL);
652 writer_n_durations =
653 kcalloc(nrealwriters, sizeof(*writer_n_durations),
654 GFP_KERNEL);
655 if (!writer_tasks || !writer_durations || !writer_n_durations) {
656 VERBOSE_PERFOUT_ERRSTRING("out of memory");
657 firsterr = -ENOMEM;
658 goto unwind;
659 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800660 for (i = 0; i < nrealwriters; i++) {
661 writer_durations[i] =
662 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
663 GFP_KERNEL);
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000664 if (!writer_durations[i]) {
665 firsterr = -ENOMEM;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800666 goto unwind;
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000667 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800668 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
669 writer_tasks[i]);
670 if (firsterr)
671 goto unwind;
672 }
673 torture_init_end();
674 return 0;
675
676unwind:
677 torture_init_end();
678 rcu_perf_cleanup();
679 return firsterr;
680}
681
682module_init(rcu_perf_init);
683module_exit(rcu_perf_cleanup);