blob: 5f884d560384f7a46014afa2e7f4ee32ff7580be [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. McKenneye838a7d2018-12-28 07:48:43 -080073#ifdef MODULE
74# define RCUPERF_SHUTDOWN 0
75#else
76# define RCUPERF_SHUTDOWN 1
77#endif
78
Paul E. McKenney881ed592017-04-17 12:47:10 -070079torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
80torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
Boqun Fengaf06d4f2016-05-25 09:25:33 +080081torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
Paul E. McKenneydf37e662016-01-30 20:56:38 -080082torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080083torture_param(int, nreaders, -1, "Number of RCU reader threads");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080084torture_param(int, nwriters, -1, "Number of RCU updater threads");
Paul E. McKenneye838a7d2018-12-28 07:48:43 -080085torture_param(bool, shutdown, RCUPERF_SHUTDOWN,
Paul E. McKenney492b95e2017-04-21 16:09:15 -070086 "Shutdown at end of performance tests.");
Paul E. McKenney90127d62018-05-09 10:29:18 -070087torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
Paul E. McKenney820687a2017-04-25 15:12:56 -070088torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080089
90static char *perf_type = "rcu";
91module_param(perf_type, charp, 0444);
Xiao Yangb3f38862019-05-31 23:15:45 +080092MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, srcu, ...)");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080093
94static int nrealreaders;
95static int nrealwriters;
96static struct task_struct **writer_tasks;
97static struct task_struct **reader_tasks;
98static struct task_struct *shutdown_task;
99
100static u64 **writer_durations;
101static int *writer_n_durations;
102static atomic_t n_rcu_perf_reader_started;
103static atomic_t n_rcu_perf_writer_started;
104static atomic_t n_rcu_perf_writer_finished;
105static wait_queue_head_t shutdown_wq;
106static u64 t_rcu_perf_writer_started;
107static u64 t_rcu_perf_writer_finished;
108static unsigned long b_rcu_perf_writer_started;
109static unsigned long b_rcu_perf_writer_finished;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700110static DEFINE_PER_CPU(atomic_t, n_async_inflight);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800111
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800112#define MAX_MEAS 10000
113#define MIN_MEAS 100
114
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800115/*
116 * Operations vector for selecting different types of tests.
117 */
118
119struct rcu_perf_ops {
120 int ptype;
121 void (*init)(void);
122 void (*cleanup)(void);
123 int (*readlock)(void);
124 void (*readunlock)(int idx);
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700125 unsigned long (*get_gp_seq)(void);
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700126 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800127 unsigned long (*exp_completed)(void);
Paul E. McKenney881ed592017-04-17 12:47:10 -0700128 void (*async)(struct rcu_head *head, rcu_callback_t func);
129 void (*gp_barrier)(void);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800130 void (*sync)(void);
131 void (*exp_sync)(void);
132 const char *name;
133};
134
135static struct rcu_perf_ops *cur_ops;
136
137/*
138 * Definitions for rcu perf testing.
139 */
140
141static int rcu_perf_read_lock(void) __acquires(RCU)
142{
143 rcu_read_lock();
144 return 0;
145}
146
147static void rcu_perf_read_unlock(int idx) __releases(RCU)
148{
149 rcu_read_unlock();
150}
151
152static unsigned long __maybe_unused rcu_no_completed(void)
153{
154 return 0;
155}
156
157static void rcu_sync_perf_init(void)
158{
159}
160
161static struct rcu_perf_ops rcu_ops = {
162 .ptype = RCU_FLAVOR,
163 .init = rcu_sync_perf_init,
164 .readlock = rcu_perf_read_lock,
165 .readunlock = rcu_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700166 .get_gp_seq = rcu_get_gp_seq,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700167 .gp_diff = rcu_seq_diff,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800168 .exp_completed = rcu_exp_batches_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700169 .async = call_rcu,
170 .gp_barrier = rcu_barrier,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800171 .sync = synchronize_rcu,
172 .exp_sync = synchronize_rcu_expedited,
173 .name = "rcu"
174};
175
176/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800177 * Definitions for srcu perf testing.
178 */
179
180DEFINE_STATIC_SRCU(srcu_ctl_perf);
181static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
182
183static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
184{
185 return srcu_read_lock(srcu_ctlp);
186}
187
188static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
189{
190 srcu_read_unlock(srcu_ctlp, idx);
191}
192
193static unsigned long srcu_perf_completed(void)
194{
195 return srcu_batches_completed(srcu_ctlp);
196}
197
Paul E. McKenney881ed592017-04-17 12:47:10 -0700198static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
199{
200 call_srcu(srcu_ctlp, head, func);
201}
202
203static void srcu_rcu_barrier(void)
204{
205 srcu_barrier(srcu_ctlp);
206}
207
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800208static void srcu_perf_synchronize(void)
209{
210 synchronize_srcu(srcu_ctlp);
211}
212
213static void srcu_perf_synchronize_expedited(void)
214{
215 synchronize_srcu_expedited(srcu_ctlp);
216}
217
218static struct rcu_perf_ops srcu_ops = {
219 .ptype = SRCU_FLAVOR,
220 .init = rcu_sync_perf_init,
221 .readlock = srcu_perf_read_lock,
222 .readunlock = srcu_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700223 .get_gp_seq = srcu_perf_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700224 .gp_diff = rcu_seq_diff,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800225 .exp_completed = srcu_perf_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700226 .async = srcu_call_rcu,
227 .gp_barrier = srcu_rcu_barrier,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800228 .sync = srcu_perf_synchronize,
229 .exp_sync = srcu_perf_synchronize_expedited,
230 .name = "srcu"
231};
232
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700233static struct srcu_struct srcud;
234
235static void srcu_sync_perf_init(void)
236{
237 srcu_ctlp = &srcud;
238 init_srcu_struct(srcu_ctlp);
239}
240
241static void srcu_sync_perf_cleanup(void)
242{
243 cleanup_srcu_struct(srcu_ctlp);
244}
245
246static struct rcu_perf_ops srcud_ops = {
247 .ptype = SRCU_FLAVOR,
248 .init = srcu_sync_perf_init,
249 .cleanup = srcu_sync_perf_cleanup,
250 .readlock = srcu_perf_read_lock,
251 .readunlock = srcu_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700252 .get_gp_seq = srcu_perf_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700253 .gp_diff = rcu_seq_diff,
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700254 .exp_completed = srcu_perf_completed,
255 .async = srcu_call_rcu,
256 .gp_barrier = srcu_rcu_barrier,
257 .sync = srcu_perf_synchronize,
258 .exp_sync = srcu_perf_synchronize_expedited,
259 .name = "srcud"
260};
261
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800262/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800263 * Definitions for RCU-tasks perf testing.
264 */
265
266static int tasks_perf_read_lock(void)
267{
268 return 0;
269}
270
271static void tasks_perf_read_unlock(int idx)
272{
273}
274
275static struct rcu_perf_ops tasks_ops = {
276 .ptype = RCU_TASKS_FLAVOR,
277 .init = rcu_sync_perf_init,
278 .readlock = tasks_perf_read_lock,
279 .readunlock = tasks_perf_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700280 .get_gp_seq = rcu_no_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700281 .gp_diff = rcu_seq_diff,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700282 .async = call_rcu_tasks,
283 .gp_barrier = rcu_barrier_tasks,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800284 .sync = synchronize_rcu_tasks,
285 .exp_sync = synchronize_rcu_tasks,
286 .name = "tasks"
287};
288
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700289static unsigned long rcuperf_seq_diff(unsigned long new, unsigned long old)
290{
291 if (!cur_ops->gp_diff)
292 return new - old;
293 return cur_ops->gp_diff(new, old);
294}
295
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800296/*
297 * If performance tests complete, wait for shutdown to commence.
298 */
299static void rcu_perf_wait_shutdown(void)
300{
Paul E. McKenneycee43932018-03-02 16:35:27 -0800301 cond_resched_tasks_rcu_qs();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800302 if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
303 return;
304 while (!torture_must_stop())
305 schedule_timeout_uninterruptible(1);
306}
307
308/*
309 * RCU perf reader kthread. Repeatedly does empty RCU read-side
310 * critical section, minimizing update-side interference.
311 */
312static int
313rcu_perf_reader(void *arg)
314{
315 unsigned long flags;
316 int idx;
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800317 long me = (long)arg;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800318
319 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800320 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800321 set_user_nice(current, MAX_NICE);
322 atomic_inc(&n_rcu_perf_reader_started);
323
324 do {
325 local_irq_save(flags);
326 idx = cur_ops->readlock();
327 cur_ops->readunlock(idx);
328 local_irq_restore(flags);
329 rcu_perf_wait_shutdown();
330 } while (!torture_must_stop());
331 torture_kthread_stopping("rcu_perf_reader");
332 return 0;
333}
334
335/*
Paul E. McKenney881ed592017-04-17 12:47:10 -0700336 * Callback function for asynchronous grace periods from rcu_perf_writer().
337 */
338static void rcu_perf_async_cb(struct rcu_head *rhp)
339{
340 atomic_dec(this_cpu_ptr(&n_async_inflight));
341 kfree(rhp);
342}
343
344/*
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800345 * RCU perf writer kthread. Repeatedly does a grace period.
346 */
347static int
348rcu_perf_writer(void *arg)
349{
350 int i = 0;
351 int i_max;
352 long me = (long)arg;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700353 struct rcu_head *rhp = NULL;
Paul E. McKenney2094c992016-01-12 15:17:21 -0800354 struct sched_param sp;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800355 bool started = false, done = false, alldone = false;
356 u64 t;
357 u64 *wdp;
358 u64 *wdpp = writer_durations[me];
359
360 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800361 WARN_ON(!wdpp);
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800362 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney2094c992016-01-12 15:17:21 -0800363 sp.sched_priority = 1;
364 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
Paul E. McKenneydf37e662016-01-30 20:56:38 -0800365
366 if (holdoff)
367 schedule_timeout_uninterruptible(holdoff * HZ);
368
Joel Fernandes (Google)77e97522019-07-04 00:34:30 -0400369 /*
370 * Wait until rcu_end_inkernel_boot() is called for normal GP tests
371 * so that RCU is not always expedited for normal GP tests.
372 * The system_state test is approximate, but works well in practice.
373 */
374 while (!gp_exp && system_state != SYSTEM_RUNNING)
375 schedule_timeout_uninterruptible(1);
376
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800377 t = ktime_get_mono_fast_ns();
378 if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
379 t_rcu_perf_writer_started = t;
380 if (gp_exp) {
381 b_rcu_perf_writer_started =
382 cur_ops->exp_completed() / 2;
383 } else {
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700384 b_rcu_perf_writer_started = cur_ops->get_gp_seq();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800385 }
386 }
387
388 do {
Paul E. McKenney820687a2017-04-25 15:12:56 -0700389 if (writer_holdoff)
390 udelay(writer_holdoff);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800391 wdp = &wdpp[i];
392 *wdp = ktime_get_mono_fast_ns();
Paul E. McKenney881ed592017-04-17 12:47:10 -0700393 if (gp_async) {
394retry:
395 if (!rhp)
396 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
397 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
Paul E. McKenney881ed592017-04-17 12:47:10 -0700398 atomic_inc(this_cpu_ptr(&n_async_inflight));
399 cur_ops->async(rhp, rcu_perf_async_cb);
400 rhp = NULL;
401 } else if (!kthread_should_stop()) {
Paul E. McKenney881ed592017-04-17 12:47:10 -0700402 cur_ops->gp_barrier();
403 goto retry;
404 } else {
405 kfree(rhp); /* Because we are stopping. */
406 }
407 } else if (gp_exp) {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800408 cur_ops->exp_sync();
409 } else {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800410 cur_ops->sync();
411 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800412 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) {
Paul E. McKenney881ed592017-04-17 12:47:10 -0700452 cur_ops->gp_barrier();
453 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800454 writer_n_durations[me] = i_max;
455 torture_kthread_stopping("rcu_perf_writer");
456 return 0;
457}
458
Paul E. McKenney96221792018-05-17 11:33:17 -0700459static void
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800460rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
461{
462 pr_alert("%s" PERF_FLAG
463 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
464 perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
465}
466
467static void
468rcu_perf_cleanup(void)
469{
470 int i;
471 int j;
472 int ngps = 0;
473 u64 *wdp;
474 u64 *wdpp;
475
Paul E. McKenney96839372017-04-14 16:12:52 -0700476 /*
477 * Would like warning at start, but everything is expedited
478 * during the mid-boot phase, so have to wait till the end.
479 */
480 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
481 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
482 if (rcu_gp_is_normal() && gp_exp)
483 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
Paul E. McKenney881ed592017-04-17 12:47:10 -0700484 if (gp_exp && gp_async)
485 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
Paul E. McKenney96839372017-04-14 16:12:52 -0700486
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800487 if (torture_cleanup_begin())
488 return;
Paul E. McKenneyad092c02019-03-21 10:26:41 -0700489 if (!cur_ops) {
490 torture_cleanup_end();
491 return;
492 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800493
494 if (reader_tasks) {
495 for (i = 0; i < nrealreaders; i++)
496 torture_stop_kthread(rcu_perf_reader,
497 reader_tasks[i]);
498 kfree(reader_tasks);
499 }
500
501 if (writer_tasks) {
502 for (i = 0; i < nrealwriters; i++) {
503 torture_stop_kthread(rcu_perf_writer,
504 writer_tasks[i]);
505 if (!writer_n_durations)
506 continue;
507 j = writer_n_durations[i];
508 pr_alert("%s%s writer %d gps: %d\n",
509 perf_type, PERF_FLAG, i, j);
510 ngps += j;
511 }
512 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
513 perf_type, PERF_FLAG,
514 t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
515 t_rcu_perf_writer_finished -
516 t_rcu_perf_writer_started,
517 ngps,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700518 rcuperf_seq_diff(b_rcu_perf_writer_finished,
519 b_rcu_perf_writer_started));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800520 for (i = 0; i < nrealwriters; i++) {
521 if (!writer_durations)
522 break;
523 if (!writer_n_durations)
524 continue;
525 wdpp = writer_durations[i];
526 if (!wdpp)
527 continue;
528 for (j = 0; j <= writer_n_durations[i]; j++) {
529 wdp = &wdpp[j];
530 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
531 perf_type, PERF_FLAG,
532 i, j, *wdp);
533 if (j % 100 == 0)
534 schedule_timeout_uninterruptible(1);
535 }
536 kfree(writer_durations[i]);
537 }
538 kfree(writer_tasks);
539 kfree(writer_durations);
540 kfree(writer_n_durations);
541 }
542
Paul E. McKenney620d2462018-07-07 18:25:10 -0700543 /* Do torture-type-specific cleanup operations. */
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800544 if (cur_ops->cleanup != NULL)
545 cur_ops->cleanup();
546
547 torture_cleanup_end();
548}
549
550/*
551 * Return the number if non-negative. If -1, the number of CPUs.
552 * If less than -1, that much less than the number of CPUs, but
553 * at least one.
554 */
555static int compute_real(int n)
556{
557 int nr;
558
559 if (n >= 0) {
560 nr = n;
561 } else {
562 nr = num_online_cpus() + 1 + n;
563 if (nr <= 0)
564 nr = 1;
565 }
566 return nr;
567}
568
569/*
570 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
571 * down system.
572 */
573static int
574rcu_perf_shutdown(void *arg)
575{
576 do {
577 wait_event(shutdown_wq,
578 atomic_read(&n_rcu_perf_writer_finished) >=
579 nrealwriters);
580 } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
581 smp_mb(); /* Wake before output. */
582 rcu_perf_cleanup();
583 kernel_power_off();
584 return -EINVAL;
585}
586
587static int __init
588rcu_perf_init(void)
589{
590 long i;
591 int firsterr = 0;
592 static struct rcu_perf_ops *perf_ops[] = {
Paul E. McKenney620d2462018-07-07 18:25:10 -0700593 &rcu_ops, &srcu_ops, &srcud_ops, &tasks_ops,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800594 };
595
Paul E. McKenneya2f25772017-11-21 20:19:17 -0800596 if (!torture_init_begin(perf_type, verbose))
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800597 return -EBUSY;
598
599 /* Process args and tell the world that the perf'er is on the job. */
600 for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
601 cur_ops = perf_ops[i];
602 if (strcmp(perf_type, cur_ops->name) == 0)
603 break;
604 }
605 if (i == ARRAY_SIZE(perf_ops)) {
Joe Perchesa7538352018-05-14 13:27:33 -0700606 pr_alert("rcu-perf: invalid perf type: \"%s\"\n", perf_type);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800607 pr_alert("rcu-perf types:");
608 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
Joe Perchesa7538352018-05-14 13:27:33 -0700609 pr_cont(" %s", perf_ops[i]->name);
610 pr_cont("\n");
Paul E. McKenneyf0288062018-07-07 18:26:50 -0700611 WARN_ON(!IS_MODULE(CONFIG_RCU_PERF_TEST));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800612 firsterr = -EINVAL;
Paul E. McKenneyad092c02019-03-21 10:26:41 -0700613 cur_ops = NULL;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800614 goto unwind;
615 }
616 if (cur_ops->init)
617 cur_ops->init();
618
619 nrealwriters = compute_real(nwriters);
620 nrealreaders = compute_real(nreaders);
621 atomic_set(&n_rcu_perf_reader_started, 0);
622 atomic_set(&n_rcu_perf_writer_started, 0);
623 atomic_set(&n_rcu_perf_writer_finished, 0);
624 rcu_perf_print_module_parms(cur_ops, "Start of test");
625
626 /* Start up the kthreads. */
627
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++) {
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800644 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800645 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 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800663 for (i = 0; i < nrealwriters; i++) {
664 writer_durations[i] =
665 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
666 GFP_KERNEL);
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000667 if (!writer_durations[i]) {
668 firsterr = -ENOMEM;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800669 goto unwind;
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000670 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800671 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
679unwind:
680 torture_init_end();
681 rcu_perf_cleanup();
682 return firsterr;
683}
684
685module_init(rcu_perf_init);
686module_exit(rcu_perf_cleanup);