blob: b459da70b4fc3f19b583a802ff7960e149767ef0 [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);
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700141 unsigned long (*get_gp_seq)(void);
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700142 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800143 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,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700182 .get_gp_seq = rcu_get_gp_seq,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700183 .gp_diff = rcu_seq_diff,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800184 .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/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800193 * Definitions for srcu perf testing.
194 */
195
196DEFINE_STATIC_SRCU(srcu_ctl_perf);
197static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
198
199static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
200{
201 return srcu_read_lock(srcu_ctlp);
202}
203
204static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
205{
206 srcu_read_unlock(srcu_ctlp, idx);
207}
208
209static unsigned long srcu_perf_completed(void)
210{
211 return srcu_batches_completed(srcu_ctlp);
212}
213
Paul E. McKenney881ed592017-04-17 12:47:10 -0700214static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
215{
216 call_srcu(srcu_ctlp, head, func);
217}
218
219static void srcu_rcu_barrier(void)
220{
221 srcu_barrier(srcu_ctlp);
222}
223
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800224static void srcu_perf_synchronize(void)
225{
226 synchronize_srcu(srcu_ctlp);
227}
228
229static void srcu_perf_synchronize_expedited(void)
230{
231 synchronize_srcu_expedited(srcu_ctlp);
232}
233
234static struct rcu_perf_ops srcu_ops = {
235 .ptype = SRCU_FLAVOR,
236 .init = rcu_sync_perf_init,
237 .readlock = srcu_perf_read_lock,
238 .readunlock = srcu_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700239 .get_gp_seq = srcu_perf_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700240 .gp_diff = rcu_seq_diff,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800241 .exp_completed = srcu_perf_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700242 .async = srcu_call_rcu,
243 .gp_barrier = srcu_rcu_barrier,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800244 .sync = srcu_perf_synchronize,
245 .exp_sync = srcu_perf_synchronize_expedited,
246 .name = "srcu"
247};
248
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700249static struct srcu_struct srcud;
250
251static void srcu_sync_perf_init(void)
252{
253 srcu_ctlp = &srcud;
254 init_srcu_struct(srcu_ctlp);
255}
256
257static void srcu_sync_perf_cleanup(void)
258{
259 cleanup_srcu_struct(srcu_ctlp);
260}
261
262static struct rcu_perf_ops srcud_ops = {
263 .ptype = SRCU_FLAVOR,
264 .init = srcu_sync_perf_init,
265 .cleanup = srcu_sync_perf_cleanup,
266 .readlock = srcu_perf_read_lock,
267 .readunlock = srcu_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700268 .get_gp_seq = srcu_perf_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700269 .gp_diff = rcu_seq_diff,
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700270 .exp_completed = srcu_perf_completed,
271 .async = srcu_call_rcu,
272 .gp_barrier = srcu_rcu_barrier,
273 .sync = srcu_perf_synchronize,
274 .exp_sync = srcu_perf_synchronize_expedited,
275 .name = "srcud"
276};
277
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800278/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800279 * Definitions for RCU-tasks perf testing.
280 */
281
282static int tasks_perf_read_lock(void)
283{
284 return 0;
285}
286
287static void tasks_perf_read_unlock(int idx)
288{
289}
290
291static struct rcu_perf_ops tasks_ops = {
292 .ptype = RCU_TASKS_FLAVOR,
293 .init = rcu_sync_perf_init,
294 .readlock = tasks_perf_read_lock,
295 .readunlock = tasks_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700296 .get_gp_seq = rcu_no_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700297 .gp_diff = rcu_seq_diff,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700298 .async = call_rcu_tasks,
299 .gp_barrier = rcu_barrier_tasks,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800300 .sync = synchronize_rcu_tasks,
301 .exp_sync = synchronize_rcu_tasks,
302 .name = "tasks"
303};
304
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700305static unsigned long rcuperf_seq_diff(unsigned long new, unsigned long old)
306{
307 if (!cur_ops->gp_diff)
308 return new - old;
309 return cur_ops->gp_diff(new, old);
310}
311
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800312/*
313 * If performance tests complete, wait for shutdown to commence.
314 */
315static void rcu_perf_wait_shutdown(void)
316{
Paul E. McKenneycee43932018-03-02 16:35:27 -0800317 cond_resched_tasks_rcu_qs();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800318 if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
319 return;
320 while (!torture_must_stop())
321 schedule_timeout_uninterruptible(1);
322}
323
324/*
325 * RCU perf reader kthread. Repeatedly does empty RCU read-side
326 * critical section, minimizing update-side interference.
327 */
328static int
329rcu_perf_reader(void *arg)
330{
331 unsigned long flags;
332 int idx;
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800333 long me = (long)arg;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800334
335 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800336 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800337 set_user_nice(current, MAX_NICE);
338 atomic_inc(&n_rcu_perf_reader_started);
339
340 do {
341 local_irq_save(flags);
342 idx = cur_ops->readlock();
343 cur_ops->readunlock(idx);
344 local_irq_restore(flags);
345 rcu_perf_wait_shutdown();
346 } while (!torture_must_stop());
347 torture_kthread_stopping("rcu_perf_reader");
348 return 0;
349}
350
351/*
Paul E. McKenney881ed592017-04-17 12:47:10 -0700352 * Callback function for asynchronous grace periods from rcu_perf_writer().
353 */
354static void rcu_perf_async_cb(struct rcu_head *rhp)
355{
356 atomic_dec(this_cpu_ptr(&n_async_inflight));
357 kfree(rhp);
358}
359
360/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800361 * RCU perf writer kthread. Repeatedly does a grace period.
362 */
363static int
364rcu_perf_writer(void *arg)
365{
366 int i = 0;
367 int i_max;
368 long me = (long)arg;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700369 struct rcu_head *rhp = NULL;
Paul E. McKenney2094c992016-01-12 15:17:21 -0800370 struct sched_param sp;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800371 bool started = false, done = false, alldone = false;
372 u64 t;
373 u64 *wdp;
374 u64 *wdpp = writer_durations[me];
375
376 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800377 WARN_ON(!wdpp);
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800378 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney2094c992016-01-12 15:17:21 -0800379 sp.sched_priority = 1;
380 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
Paul E. McKenneydf37e662016-01-30 20:56:38 -0800381
382 if (holdoff)
383 schedule_timeout_uninterruptible(holdoff * HZ);
384
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800385 t = ktime_get_mono_fast_ns();
386 if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
387 t_rcu_perf_writer_started = t;
388 if (gp_exp) {
389 b_rcu_perf_writer_started =
390 cur_ops->exp_completed() / 2;
391 } else {
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700392 b_rcu_perf_writer_started = cur_ops->get_gp_seq();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800393 }
394 }
395
396 do {
Paul E. McKenney820687a2017-04-25 15:12:56 -0700397 if (writer_holdoff)
398 udelay(writer_holdoff);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800399 wdp = &wdpp[i];
400 *wdp = ktime_get_mono_fast_ns();
Paul E. McKenney881ed592017-04-17 12:47:10 -0700401 if (gp_async) {
402retry:
403 if (!rhp)
404 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
405 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
406 rcu_perf_writer_state = RTWS_ASYNC;
407 atomic_inc(this_cpu_ptr(&n_async_inflight));
408 cur_ops->async(rhp, rcu_perf_async_cb);
409 rhp = NULL;
410 } else if (!kthread_should_stop()) {
411 rcu_perf_writer_state = RTWS_BARRIER;
412 cur_ops->gp_barrier();
413 goto retry;
414 } else {
415 kfree(rhp); /* Because we are stopping. */
416 }
417 } else if (gp_exp) {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800418 rcu_perf_writer_state = RTWS_EXP_SYNC;
419 cur_ops->exp_sync();
420 } else {
421 rcu_perf_writer_state = RTWS_SYNC;
422 cur_ops->sync();
423 }
424 rcu_perf_writer_state = RTWS_IDLE;
425 t = ktime_get_mono_fast_ns();
426 *wdp = t - *wdp;
427 i_max = i;
428 if (!started &&
429 atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
430 started = true;
431 if (!done && i >= MIN_MEAS) {
432 done = true;
Paul E. McKenney620316e2016-01-30 21:32:09 -0800433 sp.sched_priority = 0;
434 sched_setscheduler_nocheck(current,
435 SCHED_NORMAL, &sp);
SeongJae Parka56fefa2016-08-21 16:54:39 +0900436 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
437 perf_type, PERF_FLAG, me, MIN_MEAS);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800438 if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
439 nrealwriters) {
Paul E. McKenney620316e2016-01-30 21:32:09 -0800440 schedule_timeout_interruptible(10);
Paul E. McKenneyac2bb272016-01-29 14:58:17 -0800441 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800442 PERFOUT_STRING("Test complete");
443 t_rcu_perf_writer_finished = t;
444 if (gp_exp) {
445 b_rcu_perf_writer_finished =
446 cur_ops->exp_completed() / 2;
447 } else {
448 b_rcu_perf_writer_finished =
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700449 cur_ops->get_gp_seq();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800450 }
Artem Savkove6fb1fc2016-02-07 13:31:39 +0100451 if (shutdown) {
452 smp_mb(); /* Assign before wake. */
453 wake_up(&shutdown_wq);
454 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800455 }
456 }
457 if (done && !alldone &&
458 atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
459 alldone = true;
460 if (started && !alldone && i < MAX_MEAS - 1)
461 i++;
462 rcu_perf_wait_shutdown();
463 } while (!torture_must_stop());
Paul E. McKenney881ed592017-04-17 12:47:10 -0700464 if (gp_async) {
465 rcu_perf_writer_state = RTWS_BARRIER;
466 cur_ops->gp_barrier();
467 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800468 rcu_perf_writer_state = RTWS_STOPPING;
469 writer_n_durations[me] = i_max;
470 torture_kthread_stopping("rcu_perf_writer");
471 return 0;
472}
473
Paul E. McKenney96221792018-05-17 11:33:17 -0700474static void
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800475rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
476{
477 pr_alert("%s" PERF_FLAG
478 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
479 perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
480}
481
482static void
483rcu_perf_cleanup(void)
484{
485 int i;
486 int j;
487 int ngps = 0;
488 u64 *wdp;
489 u64 *wdpp;
490
Paul E. McKenney96839372017-04-14 16:12:52 -0700491 /*
492 * Would like warning at start, but everything is expedited
493 * during the mid-boot phase, so have to wait till the end.
494 */
495 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
496 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
497 if (rcu_gp_is_normal() && gp_exp)
498 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
Paul E. McKenney881ed592017-04-17 12:47:10 -0700499 if (gp_exp && gp_async)
500 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
Paul E. McKenney96839372017-04-14 16:12:52 -0700501
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800502 if (torture_cleanup_begin())
503 return;
504
505 if (reader_tasks) {
506 for (i = 0; i < nrealreaders; i++)
507 torture_stop_kthread(rcu_perf_reader,
508 reader_tasks[i]);
509 kfree(reader_tasks);
510 }
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,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700529 rcuperf_seq_diff(b_rcu_perf_writer_finished,
530 b_rcu_perf_writer_started));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800531 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
Paul E. McKenney620d2462018-07-07 18:25:10 -0700554 /* Do torture-type-specific cleanup operations. */
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800555 if (cur_ops->cleanup != NULL)
556 cur_ops->cleanup();
557
558 torture_cleanup_end();
559}
560
561/*
562 * Return the number if non-negative. If -1, the number of CPUs.
563 * If less than -1, that much less than the number of CPUs, but
564 * at least one.
565 */
566static int compute_real(int n)
567{
568 int nr;
569
570 if (n >= 0) {
571 nr = n;
572 } else {
573 nr = num_online_cpus() + 1 + n;
574 if (nr <= 0)
575 nr = 1;
576 }
577 return nr;
578}
579
580/*
581 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
582 * down system.
583 */
584static int
585rcu_perf_shutdown(void *arg)
586{
587 do {
588 wait_event(shutdown_wq,
589 atomic_read(&n_rcu_perf_writer_finished) >=
590 nrealwriters);
591 } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
592 smp_mb(); /* Wake before output. */
593 rcu_perf_cleanup();
594 kernel_power_off();
595 return -EINVAL;
596}
597
598static int __init
599rcu_perf_init(void)
600{
601 long i;
602 int firsterr = 0;
603 static struct rcu_perf_ops *perf_ops[] = {
Paul E. McKenney620d2462018-07-07 18:25:10 -0700604 &rcu_ops, &srcu_ops, &srcud_ops, &tasks_ops,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800605 };
606
Paul E. McKenneya2f25772017-11-21 20:19:17 -0800607 if (!torture_init_begin(perf_type, verbose))
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800608 return -EBUSY;
609
610 /* Process args and tell the world that the perf'er is on the job. */
611 for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
612 cur_ops = perf_ops[i];
613 if (strcmp(perf_type, cur_ops->name) == 0)
614 break;
615 }
616 if (i == ARRAY_SIZE(perf_ops)) {
Joe Perchesa7538352018-05-14 13:27:33 -0700617 pr_alert("rcu-perf: invalid perf type: \"%s\"\n", perf_type);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800618 pr_alert("rcu-perf types:");
619 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
Joe Perchesa7538352018-05-14 13:27:33 -0700620 pr_cont(" %s", perf_ops[i]->name);
621 pr_cont("\n");
Paul E. McKenneyf0288062018-07-07 18:26:50 -0700622 WARN_ON(!IS_MODULE(CONFIG_RCU_PERF_TEST));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800623 firsterr = -EINVAL;
624 goto unwind;
625 }
626 if (cur_ops->init)
627 cur_ops->init();
628
629 nrealwriters = compute_real(nwriters);
630 nrealreaders = compute_real(nreaders);
631 atomic_set(&n_rcu_perf_reader_started, 0);
632 atomic_set(&n_rcu_perf_writer_started, 0);
633 atomic_set(&n_rcu_perf_writer_finished, 0);
634 rcu_perf_print_module_parms(cur_ops, "Start of test");
635
636 /* Start up the kthreads. */
637
638 if (shutdown) {
639 init_waitqueue_head(&shutdown_wq);
640 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
641 shutdown_task);
642 if (firsterr)
643 goto unwind;
644 schedule_timeout_uninterruptible(1);
645 }
646 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
647 GFP_KERNEL);
648 if (reader_tasks == NULL) {
649 VERBOSE_PERFOUT_ERRSTRING("out of memory");
650 firsterr = -ENOMEM;
651 goto unwind;
652 }
653 for (i = 0; i < nrealreaders; i++) {
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800654 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800655 reader_tasks[i]);
656 if (firsterr)
657 goto unwind;
658 }
659 while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
660 schedule_timeout_uninterruptible(1);
661 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
662 GFP_KERNEL);
663 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
664 GFP_KERNEL);
665 writer_n_durations =
666 kcalloc(nrealwriters, sizeof(*writer_n_durations),
667 GFP_KERNEL);
668 if (!writer_tasks || !writer_durations || !writer_n_durations) {
669 VERBOSE_PERFOUT_ERRSTRING("out of memory");
670 firsterr = -ENOMEM;
671 goto unwind;
672 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800673 for (i = 0; i < nrealwriters; i++) {
674 writer_durations[i] =
675 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
676 GFP_KERNEL);
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000677 if (!writer_durations[i]) {
678 firsterr = -ENOMEM;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800679 goto unwind;
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000680 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800681 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
682 writer_tasks[i]);
683 if (firsterr)
684 goto unwind;
685 }
686 torture_init_end();
687 return 0;
688
689unwind:
690 torture_init_end();
691 rcu_perf_cleanup();
692 return firsterr;
693}
694
695module_init(rcu_perf_init);
696module_exit(rcu_perf_cleanup);