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