1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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>
33#include <uapi/linux/sched/types.h>
34#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
51#include "rcu.h"
52
53MODULE_LICENSE("GPL");
54MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
55
56#define PERF_FLAG "-perf:"
57#define PERFOUT_STRING(s) \
58 pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
59#define VERBOSE_PERFOUT_STRING(s) \
60 do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
61#define VERBOSE_PERFOUT_ERRSTRING(s) \
62 do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
63
64torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
65torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
66torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
67torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
68torture_param(int, nreaders, 0, "Number of RCU reader threads");
69torture_param(int, nwriters, -1, "Number of RCU updater threads");
70torture_param(bool, shutdown, !IS_ENABLED(MODULE),
71 "Shutdown at end of performance tests.");
72torture_param(bool, verbose, true, "Enable verbose debugging printk()s");
73torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
74
75static char *perf_type = "rcu";
76module_param(perf_type, charp, 0444);
77MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
78
79static int nrealreaders;
80static int nrealwriters;
81static struct task_struct **writer_tasks;
82static struct task_struct **reader_tasks;
83static struct task_struct *shutdown_task;
84
85static u64 **writer_durations;
86static int *writer_n_durations;
87static atomic_t n_rcu_perf_reader_started;
88static atomic_t n_rcu_perf_writer_started;
89static atomic_t n_rcu_perf_writer_finished;
90static wait_queue_head_t shutdown_wq;
91static u64 t_rcu_perf_writer_started;
92static u64 t_rcu_perf_writer_finished;
93static unsigned long b_rcu_perf_writer_started;
94static unsigned long b_rcu_perf_writer_finished;
95static DEFINE_PER_CPU(atomic_t, n_async_inflight);
96
97static int rcu_perf_writer_state;
98#define RTWS_INIT 0
99#define RTWS_ASYNC 1
100#define RTWS_BARRIER 2
101#define RTWS_EXP_SYNC 3
102#define RTWS_SYNC 4
103#define RTWS_IDLE 5
104#define RTWS_STOPPING 6
105
106#define MAX_MEAS 10000
107#define MIN_MEAS 100
108
109static int perf_runnable = IS_ENABLED(MODULE);
110module_param(perf_runnable, int, 0444);
111MODULE_PARM_DESC(perf_runnable, "Start rcuperf at boot");
112
113
114
115
116
117struct rcu_perf_ops {
118 int ptype;
119 void (*init)(void);
120 void (*cleanup)(void);
121 int (*readlock)(void);
122 void (*readunlock)(int idx);
123 unsigned long (*started)(void);
124 unsigned long (*completed)(void);
125 unsigned long (*exp_completed)(void);
126 void (*async)(struct rcu_head *head, rcu_callback_t func);
127 void (*gp_barrier)(void);
128 void (*sync)(void);
129 void (*exp_sync)(void);
130 const char *name;
131};
132
133static struct rcu_perf_ops *cur_ops;
134
135
136
137
138
139static int rcu_perf_read_lock(void) __acquires(RCU)
140{
141 rcu_read_lock();
142 return 0;
143}
144
145static void rcu_perf_read_unlock(int idx) __releases(RCU)
146{
147 rcu_read_unlock();
148}
149
150static unsigned long __maybe_unused rcu_no_completed(void)
151{
152 return 0;
153}
154
155static void rcu_sync_perf_init(void)
156{
157}
158
159static struct rcu_perf_ops rcu_ops = {
160 .ptype = RCU_FLAVOR,
161 .init = rcu_sync_perf_init,
162 .readlock = rcu_perf_read_lock,
163 .readunlock = rcu_perf_read_unlock,
164 .started = rcu_batches_started,
165 .completed = rcu_batches_completed,
166 .exp_completed = rcu_exp_batches_completed,
167 .async = call_rcu,
168 .gp_barrier = rcu_barrier,
169 .sync = synchronize_rcu,
170 .exp_sync = synchronize_rcu_expedited,
171 .name = "rcu"
172};
173
174
175
176
177
178static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH)
179{
180 rcu_read_lock_bh();
181 return 0;
182}
183
184static void rcu_bh_perf_read_unlock(int idx) __releases(RCU_BH)
185{
186 rcu_read_unlock_bh();
187}
188
189static struct rcu_perf_ops rcu_bh_ops = {
190 .ptype = RCU_BH_FLAVOR,
191 .init = rcu_sync_perf_init,
192 .readlock = rcu_bh_perf_read_lock,
193 .readunlock = rcu_bh_perf_read_unlock,
194 .started = rcu_batches_started_bh,
195 .completed = rcu_batches_completed_bh,
196 .exp_completed = rcu_exp_batches_completed_sched,
197 .async = call_rcu_bh,
198 .gp_barrier = rcu_barrier_bh,
199 .sync = synchronize_rcu_bh,
200 .exp_sync = synchronize_rcu_bh_expedited,
201 .name = "rcu_bh"
202};
203
204
205
206
207
208DEFINE_STATIC_SRCU(srcu_ctl_perf);
209static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
210
211static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
212{
213 return srcu_read_lock(srcu_ctlp);
214}
215
216static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
217{
218 srcu_read_unlock(srcu_ctlp, idx);
219}
220
221static unsigned long srcu_perf_completed(void)
222{
223 return srcu_batches_completed(srcu_ctlp);
224}
225
226static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
227{
228 call_srcu(srcu_ctlp, head, func);
229}
230
231static void srcu_rcu_barrier(void)
232{
233 srcu_barrier(srcu_ctlp);
234}
235
236static void srcu_perf_synchronize(void)
237{
238 synchronize_srcu(srcu_ctlp);
239}
240
241static void srcu_perf_synchronize_expedited(void)
242{
243 synchronize_srcu_expedited(srcu_ctlp);
244}
245
246static struct rcu_perf_ops srcu_ops = {
247 .ptype = SRCU_FLAVOR,
248 .init = rcu_sync_perf_init,
249 .readlock = srcu_perf_read_lock,
250 .readunlock = srcu_perf_read_unlock,
251 .started = NULL,
252 .completed = srcu_perf_completed,
253 .exp_completed = srcu_perf_completed,
254 .async = srcu_call_rcu,
255 .gp_barrier = srcu_rcu_barrier,
256 .sync = srcu_perf_synchronize,
257 .exp_sync = srcu_perf_synchronize_expedited,
258 .name = "srcu"
259};
260
261static struct srcu_struct srcud;
262
263static void srcu_sync_perf_init(void)
264{
265 srcu_ctlp = &srcud;
266 init_srcu_struct(srcu_ctlp);
267}
268
269static void srcu_sync_perf_cleanup(void)
270{
271 cleanup_srcu_struct(srcu_ctlp);
272}
273
274static struct rcu_perf_ops srcud_ops = {
275 .ptype = SRCU_FLAVOR,
276 .init = srcu_sync_perf_init,
277 .cleanup = srcu_sync_perf_cleanup,
278 .readlock = srcu_perf_read_lock,
279 .readunlock = srcu_perf_read_unlock,
280 .started = NULL,
281 .completed = srcu_perf_completed,
282 .exp_completed = srcu_perf_completed,
283 .async = srcu_call_rcu,
284 .gp_barrier = srcu_rcu_barrier,
285 .sync = srcu_perf_synchronize,
286 .exp_sync = srcu_perf_synchronize_expedited,
287 .name = "srcud"
288};
289
290
291
292
293
294static int sched_perf_read_lock(void)
295{
296 preempt_disable();
297 return 0;
298}
299
300static void sched_perf_read_unlock(int idx)
301{
302 preempt_enable();
303}
304
305static struct rcu_perf_ops sched_ops = {
306 .ptype = RCU_SCHED_FLAVOR,
307 .init = rcu_sync_perf_init,
308 .readlock = sched_perf_read_lock,
309 .readunlock = sched_perf_read_unlock,
310 .started = rcu_batches_started_sched,
311 .completed = rcu_batches_completed_sched,
312 .exp_completed = rcu_exp_batches_completed_sched,
313 .async = call_rcu_sched,
314 .gp_barrier = rcu_barrier_sched,
315 .sync = synchronize_sched,
316 .exp_sync = synchronize_sched_expedited,
317 .name = "sched"
318};
319
320
321
322
323
324static int tasks_perf_read_lock(void)
325{
326 return 0;
327}
328
329static void tasks_perf_read_unlock(int idx)
330{
331}
332
333static struct rcu_perf_ops tasks_ops = {
334 .ptype = RCU_TASKS_FLAVOR,
335 .init = rcu_sync_perf_init,
336 .readlock = tasks_perf_read_lock,
337 .readunlock = tasks_perf_read_unlock,
338 .started = rcu_no_completed,
339 .completed = rcu_no_completed,
340 .async = call_rcu_tasks,
341 .gp_barrier = rcu_barrier_tasks,
342 .sync = synchronize_rcu_tasks,
343 .exp_sync = synchronize_rcu_tasks,
344 .name = "tasks"
345};
346
347static bool __maybe_unused torturing_tasks(void)
348{
349 return cur_ops == &tasks_ops;
350}
351
352
353
354
355static void rcu_perf_wait_shutdown(void)
356{
357 cond_resched_rcu_qs();
358 if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
359 return;
360 while (!torture_must_stop())
361 schedule_timeout_uninterruptible(1);
362}
363
364
365
366
367
368static int
369rcu_perf_reader(void *arg)
370{
371 unsigned long flags;
372 int idx;
373 long me = (long)arg;
374
375 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
376 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
377 set_user_nice(current, MAX_NICE);
378 atomic_inc(&n_rcu_perf_reader_started);
379
380 do {
381 local_irq_save(flags);
382 idx = cur_ops->readlock();
383 cur_ops->readunlock(idx);
384 local_irq_restore(flags);
385 rcu_perf_wait_shutdown();
386 } while (!torture_must_stop());
387 torture_kthread_stopping("rcu_perf_reader");
388 return 0;
389}
390
391
392
393
394static void rcu_perf_async_cb(struct rcu_head *rhp)
395{
396 atomic_dec(this_cpu_ptr(&n_async_inflight));
397 kfree(rhp);
398}
399
400
401
402
403static int
404rcu_perf_writer(void *arg)
405{
406 int i = 0;
407 int i_max;
408 long me = (long)arg;
409 struct rcu_head *rhp = NULL;
410 struct sched_param sp;
411 bool started = false, done = false, alldone = false;
412 u64 t;
413 u64 *wdp;
414 u64 *wdpp = writer_durations[me];
415
416 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
417 WARN_ON(!wdpp);
418 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
419 sp.sched_priority = 1;
420 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
421
422 if (holdoff)
423 schedule_timeout_uninterruptible(holdoff * HZ);
424
425 t = ktime_get_mono_fast_ns();
426 if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
427 t_rcu_perf_writer_started = t;
428 if (gp_exp) {
429 b_rcu_perf_writer_started =
430 cur_ops->exp_completed() / 2;
431 } else {
432 b_rcu_perf_writer_started =
433 cur_ops->completed();
434 }
435 }
436
437 do {
438 if (writer_holdoff)
439 udelay(writer_holdoff);
440 wdp = &wdpp[i];
441 *wdp = ktime_get_mono_fast_ns();
442 if (gp_async) {
443retry:
444 if (!rhp)
445 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
446 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
447 rcu_perf_writer_state = RTWS_ASYNC;
448 atomic_inc(this_cpu_ptr(&n_async_inflight));
449 cur_ops->async(rhp, rcu_perf_async_cb);
450 rhp = NULL;
451 } else if (!kthread_should_stop()) {
452 rcu_perf_writer_state = RTWS_BARRIER;
453 cur_ops->gp_barrier();
454 goto retry;
455 } else {
456 kfree(rhp);
457 }
458 } else if (gp_exp) {
459 rcu_perf_writer_state = RTWS_EXP_SYNC;
460 cur_ops->exp_sync();
461 } else {
462 rcu_perf_writer_state = RTWS_SYNC;
463 cur_ops->sync();
464 }
465 rcu_perf_writer_state = RTWS_IDLE;
466 t = ktime_get_mono_fast_ns();
467 *wdp = t - *wdp;
468 i_max = i;
469 if (!started &&
470 atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
471 started = true;
472 if (!done && i >= MIN_MEAS) {
473 done = true;
474 sp.sched_priority = 0;
475 sched_setscheduler_nocheck(current,
476 SCHED_NORMAL, &sp);
477 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
478 perf_type, PERF_FLAG, me, MIN_MEAS);
479 if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
480 nrealwriters) {
481 schedule_timeout_interruptible(10);
482 rcu_ftrace_dump(DUMP_ALL);
483 PERFOUT_STRING("Test complete");
484 t_rcu_perf_writer_finished = t;
485 if (gp_exp) {
486 b_rcu_perf_writer_finished =
487 cur_ops->exp_completed() / 2;
488 } else {
489 b_rcu_perf_writer_finished =
490 cur_ops->completed();
491 }
492 if (shutdown) {
493 smp_mb();
494 wake_up(&shutdown_wq);
495 }
496 }
497 }
498 if (done && !alldone &&
499 atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
500 alldone = true;
501 if (started && !alldone && i < MAX_MEAS - 1)
502 i++;
503 rcu_perf_wait_shutdown();
504 } while (!torture_must_stop());
505 if (gp_async) {
506 rcu_perf_writer_state = RTWS_BARRIER;
507 cur_ops->gp_barrier();
508 }
509 rcu_perf_writer_state = RTWS_STOPPING;
510 writer_n_durations[me] = i_max;
511 torture_kthread_stopping("rcu_perf_writer");
512 return 0;
513}
514
515static inline void
516rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
517{
518 pr_alert("%s" PERF_FLAG
519 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
520 perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
521}
522
523static void
524rcu_perf_cleanup(void)
525{
526 int i;
527 int j;
528 int ngps = 0;
529 u64 *wdp;
530 u64 *wdpp;
531
532
533
534
535
536 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
537 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
538 if (rcu_gp_is_normal() && gp_exp)
539 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
540 if (gp_exp && gp_async)
541 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
542
543 if (torture_cleanup_begin())
544 return;
545
546 if (reader_tasks) {
547 for (i = 0; i < nrealreaders; i++)
548 torture_stop_kthread(rcu_perf_reader,
549 reader_tasks[i]);
550 kfree(reader_tasks);
551 }
552
553 if (writer_tasks) {
554 for (i = 0; i < nrealwriters; i++) {
555 torture_stop_kthread(rcu_perf_writer,
556 writer_tasks[i]);
557 if (!writer_n_durations)
558 continue;
559 j = writer_n_durations[i];
560 pr_alert("%s%s writer %d gps: %d\n",
561 perf_type, PERF_FLAG, i, j);
562 ngps += j;
563 }
564 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
565 perf_type, PERF_FLAG,
566 t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
567 t_rcu_perf_writer_finished -
568 t_rcu_perf_writer_started,
569 ngps,
570 b_rcu_perf_writer_finished -
571 b_rcu_perf_writer_started);
572 for (i = 0; i < nrealwriters; i++) {
573 if (!writer_durations)
574 break;
575 if (!writer_n_durations)
576 continue;
577 wdpp = writer_durations[i];
578 if (!wdpp)
579 continue;
580 for (j = 0; j <= writer_n_durations[i]; j++) {
581 wdp = &wdpp[j];
582 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
583 perf_type, PERF_FLAG,
584 i, j, *wdp);
585 if (j % 100 == 0)
586 schedule_timeout_uninterruptible(1);
587 }
588 kfree(writer_durations[i]);
589 }
590 kfree(writer_tasks);
591 kfree(writer_durations);
592 kfree(writer_n_durations);
593 }
594
595
596 if (cur_ops->cleanup != NULL)
597 cur_ops->cleanup();
598
599 torture_cleanup_end();
600}
601
602
603
604
605
606
607static int compute_real(int n)
608{
609 int nr;
610
611 if (n >= 0) {
612 nr = n;
613 } else {
614 nr = num_online_cpus() + 1 + n;
615 if (nr <= 0)
616 nr = 1;
617 }
618 return nr;
619}
620
621
622
623
624
625static int
626rcu_perf_shutdown(void *arg)
627{
628 do {
629 wait_event(shutdown_wq,
630 atomic_read(&n_rcu_perf_writer_finished) >=
631 nrealwriters);
632 } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
633 smp_mb();
634 rcu_perf_cleanup();
635 kernel_power_off();
636 return -EINVAL;
637}
638
639static int __init
640rcu_perf_init(void)
641{
642 long i;
643 int firsterr = 0;
644 static struct rcu_perf_ops *perf_ops[] = {
645 &rcu_ops, &rcu_bh_ops, &srcu_ops, &srcud_ops, &sched_ops,
646 &tasks_ops,
647 };
648
649 if (!torture_init_begin(perf_type, verbose, &perf_runnable))
650 return -EBUSY;
651
652
653 for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
654 cur_ops = perf_ops[i];
655 if (strcmp(perf_type, cur_ops->name) == 0)
656 break;
657 }
658 if (i == ARRAY_SIZE(perf_ops)) {
659 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
660 perf_type);
661 pr_alert("rcu-perf types:");
662 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
663 pr_alert(" %s", perf_ops[i]->name);
664 pr_alert("\n");
665 firsterr = -EINVAL;
666 goto unwind;
667 }
668 if (cur_ops->init)
669 cur_ops->init();
670
671 nrealwriters = compute_real(nwriters);
672 nrealreaders = compute_real(nreaders);
673 atomic_set(&n_rcu_perf_reader_started, 0);
674 atomic_set(&n_rcu_perf_writer_started, 0);
675 atomic_set(&n_rcu_perf_writer_finished, 0);
676 rcu_perf_print_module_parms(cur_ops, "Start of test");
677
678
679
680 if (shutdown) {
681 init_waitqueue_head(&shutdown_wq);
682 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
683 shutdown_task);
684 if (firsterr)
685 goto unwind;
686 schedule_timeout_uninterruptible(1);
687 }
688 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
689 GFP_KERNEL);
690 if (reader_tasks == NULL) {
691 VERBOSE_PERFOUT_ERRSTRING("out of memory");
692 firsterr = -ENOMEM;
693 goto unwind;
694 }
695 for (i = 0; i < nrealreaders; i++) {
696 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
697 reader_tasks[i]);
698 if (firsterr)
699 goto unwind;
700 }
701 while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
702 schedule_timeout_uninterruptible(1);
703 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
704 GFP_KERNEL);
705 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
706 GFP_KERNEL);
707 writer_n_durations =
708 kcalloc(nrealwriters, sizeof(*writer_n_durations),
709 GFP_KERNEL);
710 if (!writer_tasks || !writer_durations || !writer_n_durations) {
711 VERBOSE_PERFOUT_ERRSTRING("out of memory");
712 firsterr = -ENOMEM;
713 goto unwind;
714 }
715 for (i = 0; i < nrealwriters; i++) {
716 writer_durations[i] =
717 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
718 GFP_KERNEL);
719 if (!writer_durations[i]) {
720 firsterr = -ENOMEM;
721 goto unwind;
722 }
723 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
724 writer_tasks[i]);
725 if (firsterr)
726 goto unwind;
727 }
728 torture_init_end();
729 return 0;
730
731unwind:
732 torture_init_end();
733 rcu_perf_cleanup();
734 return firsterr;
735}
736
737module_init(rcu_perf_init);
738module_exit(rcu_perf_cleanup);
739