]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/rcu/rcuperf.c
Merge tag 'perf-core-for-mingo-4.11-20170306' of git://git.kernel.org/pub/scm/linux...
[karo-tx-linux.git] / kernel / rcu / rcuperf.c
1 /*
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>
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 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
53
54 #define PERF_FLAG "-perf:"
55 #define PERFOUT_STRING(s) \
56         pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
57 #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
62 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
63 torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
64 torture_param(int, nreaders, -1, "Number of RCU reader threads");
65 torture_param(int, nwriters, -1, "Number of RCU updater threads");
66 torture_param(bool, shutdown, false, "Shutdown at end of performance tests.");
67 torture_param(bool, verbose, true, "Enable verbose debugging printk()s");
68
69 static char *perf_type = "rcu";
70 module_param(perf_type, charp, 0444);
71 MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
72
73 static int nrealreaders;
74 static int nrealwriters;
75 static struct task_struct **writer_tasks;
76 static struct task_struct **reader_tasks;
77 static struct task_struct *shutdown_task;
78
79 static u64 **writer_durations;
80 static int *writer_n_durations;
81 static atomic_t n_rcu_perf_reader_started;
82 static atomic_t n_rcu_perf_writer_started;
83 static atomic_t n_rcu_perf_writer_finished;
84 static wait_queue_head_t shutdown_wq;
85 static u64 t_rcu_perf_writer_started;
86 static u64 t_rcu_perf_writer_finished;
87 static unsigned long b_rcu_perf_writer_started;
88 static unsigned long b_rcu_perf_writer_finished;
89
90 static int rcu_perf_writer_state;
91 #define RTWS_INIT               0
92 #define RTWS_EXP_SYNC           1
93 #define RTWS_SYNC               2
94 #define RTWS_IDLE               2
95 #define RTWS_STOPPING           3
96
97 #define MAX_MEAS 10000
98 #define MIN_MEAS 100
99
100 static int perf_runnable = IS_ENABLED(MODULE);
101 module_param(perf_runnable, int, 0444);
102 MODULE_PARM_DESC(perf_runnable, "Start rcuperf at boot");
103
104 /*
105  * Operations vector for selecting different types of tests.
106  */
107
108 struct rcu_perf_ops {
109         int ptype;
110         void (*init)(void);
111         void (*cleanup)(void);
112         int (*readlock)(void);
113         void (*readunlock)(int idx);
114         unsigned long (*started)(void);
115         unsigned long (*completed)(void);
116         unsigned long (*exp_completed)(void);
117         void (*sync)(void);
118         void (*exp_sync)(void);
119         const char *name;
120 };
121
122 static struct rcu_perf_ops *cur_ops;
123
124 /*
125  * Definitions for rcu perf testing.
126  */
127
128 static int rcu_perf_read_lock(void) __acquires(RCU)
129 {
130         rcu_read_lock();
131         return 0;
132 }
133
134 static void rcu_perf_read_unlock(int idx) __releases(RCU)
135 {
136         rcu_read_unlock();
137 }
138
139 static unsigned long __maybe_unused rcu_no_completed(void)
140 {
141         return 0;
142 }
143
144 static void rcu_sync_perf_init(void)
145 {
146 }
147
148 static struct rcu_perf_ops rcu_ops = {
149         .ptype          = RCU_FLAVOR,
150         .init           = rcu_sync_perf_init,
151         .readlock       = rcu_perf_read_lock,
152         .readunlock     = rcu_perf_read_unlock,
153         .started        = rcu_batches_started,
154         .completed      = rcu_batches_completed,
155         .exp_completed  = rcu_exp_batches_completed,
156         .sync           = synchronize_rcu,
157         .exp_sync       = synchronize_rcu_expedited,
158         .name           = "rcu"
159 };
160
161 /*
162  * Definitions for rcu_bh perf testing.
163  */
164
165 static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH)
166 {
167         rcu_read_lock_bh();
168         return 0;
169 }
170
171 static void rcu_bh_perf_read_unlock(int idx) __releases(RCU_BH)
172 {
173         rcu_read_unlock_bh();
174 }
175
176 static struct rcu_perf_ops rcu_bh_ops = {
177         .ptype          = RCU_BH_FLAVOR,
178         .init           = rcu_sync_perf_init,
179         .readlock       = rcu_bh_perf_read_lock,
180         .readunlock     = rcu_bh_perf_read_unlock,
181         .started        = rcu_batches_started_bh,
182         .completed      = rcu_batches_completed_bh,
183         .exp_completed  = rcu_exp_batches_completed_sched,
184         .sync           = synchronize_rcu_bh,
185         .exp_sync       = synchronize_rcu_bh_expedited,
186         .name           = "rcu_bh"
187 };
188
189 /*
190  * Definitions for srcu perf testing.
191  */
192
193 DEFINE_STATIC_SRCU(srcu_ctl_perf);
194 static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
195
196 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
197 {
198         return srcu_read_lock(srcu_ctlp);
199 }
200
201 static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
202 {
203         srcu_read_unlock(srcu_ctlp, idx);
204 }
205
206 static unsigned long srcu_perf_completed(void)
207 {
208         return srcu_batches_completed(srcu_ctlp);
209 }
210
211 static void srcu_perf_synchronize(void)
212 {
213         synchronize_srcu(srcu_ctlp);
214 }
215
216 static void srcu_perf_synchronize_expedited(void)
217 {
218         synchronize_srcu_expedited(srcu_ctlp);
219 }
220
221 static 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,
226         .started        = NULL,
227         .completed      = srcu_perf_completed,
228         .exp_completed  = srcu_perf_completed,
229         .sync           = srcu_perf_synchronize,
230         .exp_sync       = srcu_perf_synchronize_expedited,
231         .name           = "srcu"
232 };
233
234 /*
235  * Definitions for sched perf testing.
236  */
237
238 static int sched_perf_read_lock(void)
239 {
240         preempt_disable();
241         return 0;
242 }
243
244 static void sched_perf_read_unlock(int idx)
245 {
246         preempt_enable();
247 }
248
249 static struct rcu_perf_ops sched_ops = {
250         .ptype          = RCU_SCHED_FLAVOR,
251         .init           = rcu_sync_perf_init,
252         .readlock       = sched_perf_read_lock,
253         .readunlock     = sched_perf_read_unlock,
254         .started        = rcu_batches_started_sched,
255         .completed      = rcu_batches_completed_sched,
256         .exp_completed  = rcu_exp_batches_completed_sched,
257         .sync           = synchronize_sched,
258         .exp_sync       = synchronize_sched_expedited,
259         .name           = "sched"
260 };
261
262 #ifdef CONFIG_TASKS_RCU
263
264 /*
265  * Definitions for RCU-tasks perf testing.
266  */
267
268 static int tasks_perf_read_lock(void)
269 {
270         return 0;
271 }
272
273 static void tasks_perf_read_unlock(int idx)
274 {
275 }
276
277 static struct rcu_perf_ops tasks_ops = {
278         .ptype          = RCU_TASKS_FLAVOR,
279         .init           = rcu_sync_perf_init,
280         .readlock       = tasks_perf_read_lock,
281         .readunlock     = tasks_perf_read_unlock,
282         .started        = rcu_no_completed,
283         .completed      = rcu_no_completed,
284         .sync           = synchronize_rcu_tasks,
285         .exp_sync       = synchronize_rcu_tasks,
286         .name           = "tasks"
287 };
288
289 #define RCUPERF_TASKS_OPS &tasks_ops,
290
291 static bool __maybe_unused torturing_tasks(void)
292 {
293         return cur_ops == &tasks_ops;
294 }
295
296 #else /* #ifdef CONFIG_TASKS_RCU */
297
298 #define RCUPERF_TASKS_OPS
299
300 static bool __maybe_unused torturing_tasks(void)
301 {
302         return false;
303 }
304
305 #endif /* #else #ifdef CONFIG_TASKS_RCU */
306
307 /*
308  * If performance tests complete, wait for shutdown to commence.
309  */
310 static void rcu_perf_wait_shutdown(void)
311 {
312         cond_resched_rcu_qs();
313         if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
314                 return;
315         while (!torture_must_stop())
316                 schedule_timeout_uninterruptible(1);
317 }
318
319 /*
320  * RCU perf reader kthread.  Repeatedly does empty RCU read-side
321  * critical section, minimizing update-side interference.
322  */
323 static int
324 rcu_perf_reader(void *arg)
325 {
326         unsigned long flags;
327         int idx;
328         long me = (long)arg;
329
330         VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
331         set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
332         set_user_nice(current, MAX_NICE);
333         atomic_inc(&n_rcu_perf_reader_started);
334
335         do {
336                 local_irq_save(flags);
337                 idx = cur_ops->readlock();
338                 cur_ops->readunlock(idx);
339                 local_irq_restore(flags);
340                 rcu_perf_wait_shutdown();
341         } while (!torture_must_stop());
342         torture_kthread_stopping("rcu_perf_reader");
343         return 0;
344 }
345
346 /*
347  * RCU perf writer kthread.  Repeatedly does a grace period.
348  */
349 static int
350 rcu_perf_writer(void *arg)
351 {
352         int i = 0;
353         int i_max;
354         long me = (long)arg;
355         struct sched_param sp;
356         bool started = false, done = false, alldone = false;
357         u64 t;
358         u64 *wdp;
359         u64 *wdpp = writer_durations[me];
360
361         VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
362         WARN_ON(!wdpp);
363         set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
364         sp.sched_priority = 1;
365         sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
366
367         if (holdoff)
368                 schedule_timeout_uninterruptible(holdoff * HZ);
369
370         t = ktime_get_mono_fast_ns();
371         if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
372                 t_rcu_perf_writer_started = t;
373                 if (gp_exp) {
374                         b_rcu_perf_writer_started =
375                                 cur_ops->exp_completed() / 2;
376                 } else {
377                         b_rcu_perf_writer_started =
378                                 cur_ops->completed();
379                 }
380         }
381
382         do {
383                 wdp = &wdpp[i];
384                 *wdp = ktime_get_mono_fast_ns();
385                 if (gp_exp) {
386                         rcu_perf_writer_state = RTWS_EXP_SYNC;
387                         cur_ops->exp_sync();
388                 } else {
389                         rcu_perf_writer_state = RTWS_SYNC;
390                         cur_ops->sync();
391                 }
392                 rcu_perf_writer_state = RTWS_IDLE;
393                 t = ktime_get_mono_fast_ns();
394                 *wdp = t - *wdp;
395                 i_max = i;
396                 if (!started &&
397                     atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
398                         started = true;
399                 if (!done && i >= MIN_MEAS) {
400                         done = true;
401                         sp.sched_priority = 0;
402                         sched_setscheduler_nocheck(current,
403                                                    SCHED_NORMAL, &sp);
404                         pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
405                                  perf_type, PERF_FLAG, me, MIN_MEAS);
406                         if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
407                             nrealwriters) {
408                                 schedule_timeout_interruptible(10);
409                                 rcu_ftrace_dump(DUMP_ALL);
410                                 PERFOUT_STRING("Test complete");
411                                 t_rcu_perf_writer_finished = t;
412                                 if (gp_exp) {
413                                         b_rcu_perf_writer_finished =
414                                                 cur_ops->exp_completed() / 2;
415                                 } else {
416                                         b_rcu_perf_writer_finished =
417                                                 cur_ops->completed();
418                                 }
419                                 if (shutdown) {
420                                         smp_mb(); /* Assign before wake. */
421                                         wake_up(&shutdown_wq);
422                                 }
423                         }
424                 }
425                 if (done && !alldone &&
426                     atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
427                         alldone = true;
428                 if (started && !alldone && i < MAX_MEAS - 1)
429                         i++;
430                 rcu_perf_wait_shutdown();
431         } while (!torture_must_stop());
432         rcu_perf_writer_state = RTWS_STOPPING;
433         writer_n_durations[me] = i_max;
434         torture_kthread_stopping("rcu_perf_writer");
435         return 0;
436 }
437
438 static inline void
439 rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
440 {
441         pr_alert("%s" PERF_FLAG
442                  "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
443                  perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
444 }
445
446 static void
447 rcu_perf_cleanup(void)
448 {
449         int i;
450         int j;
451         int ngps = 0;
452         u64 *wdp;
453         u64 *wdpp;
454
455         if (torture_cleanup_begin())
456                 return;
457
458         if (reader_tasks) {
459                 for (i = 0; i < nrealreaders; i++)
460                         torture_stop_kthread(rcu_perf_reader,
461                                              reader_tasks[i]);
462                 kfree(reader_tasks);
463         }
464
465         if (writer_tasks) {
466                 for (i = 0; i < nrealwriters; i++) {
467                         torture_stop_kthread(rcu_perf_writer,
468                                              writer_tasks[i]);
469                         if (!writer_n_durations)
470                                 continue;
471                         j = writer_n_durations[i];
472                         pr_alert("%s%s writer %d gps: %d\n",
473                                  perf_type, PERF_FLAG, i, j);
474                         ngps += j;
475                 }
476                 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
477                          perf_type, PERF_FLAG,
478                          t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
479                          t_rcu_perf_writer_finished -
480                          t_rcu_perf_writer_started,
481                          ngps,
482                          b_rcu_perf_writer_finished -
483                          b_rcu_perf_writer_started);
484                 for (i = 0; i < nrealwriters; i++) {
485                         if (!writer_durations)
486                                 break;
487                         if (!writer_n_durations)
488                                 continue;
489                         wdpp = writer_durations[i];
490                         if (!wdpp)
491                                 continue;
492                         for (j = 0; j <= writer_n_durations[i]; j++) {
493                                 wdp = &wdpp[j];
494                                 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
495                                         perf_type, PERF_FLAG,
496                                         i, j, *wdp);
497                                 if (j % 100 == 0)
498                                         schedule_timeout_uninterruptible(1);
499                         }
500                         kfree(writer_durations[i]);
501                 }
502                 kfree(writer_tasks);
503                 kfree(writer_durations);
504                 kfree(writer_n_durations);
505         }
506
507         /* Do flavor-specific cleanup operations.  */
508         if (cur_ops->cleanup != NULL)
509                 cur_ops->cleanup();
510
511         torture_cleanup_end();
512 }
513
514 /*
515  * Return the number if non-negative.  If -1, the number of CPUs.
516  * If less than -1, that much less than the number of CPUs, but
517  * at least one.
518  */
519 static int compute_real(int n)
520 {
521         int nr;
522
523         if (n >= 0) {
524                 nr = n;
525         } else {
526                 nr = num_online_cpus() + 1 + n;
527                 if (nr <= 0)
528                         nr = 1;
529         }
530         return nr;
531 }
532
533 /*
534  * RCU perf shutdown kthread.  Just waits to be awakened, then shuts
535  * down system.
536  */
537 static int
538 rcu_perf_shutdown(void *arg)
539 {
540         do {
541                 wait_event(shutdown_wq,
542                            atomic_read(&n_rcu_perf_writer_finished) >=
543                            nrealwriters);
544         } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
545         smp_mb(); /* Wake before output. */
546         rcu_perf_cleanup();
547         kernel_power_off();
548         return -EINVAL;
549 }
550
551 static int __init
552 rcu_perf_init(void)
553 {
554         long i;
555         int firsterr = 0;
556         static struct rcu_perf_ops *perf_ops[] = {
557                 &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
558                 RCUPERF_TASKS_OPS
559         };
560
561         if (!torture_init_begin(perf_type, verbose, &perf_runnable))
562                 return -EBUSY;
563
564         /* Process args and tell the world that the perf'er is on the job. */
565         for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
566                 cur_ops = perf_ops[i];
567                 if (strcmp(perf_type, cur_ops->name) == 0)
568                         break;
569         }
570         if (i == ARRAY_SIZE(perf_ops)) {
571                 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
572                          perf_type);
573                 pr_alert("rcu-perf types:");
574                 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
575                         pr_alert(" %s", perf_ops[i]->name);
576                 pr_alert("\n");
577                 firsterr = -EINVAL;
578                 goto unwind;
579         }
580         if (cur_ops->init)
581                 cur_ops->init();
582
583         nrealwriters = compute_real(nwriters);
584         nrealreaders = compute_real(nreaders);
585         atomic_set(&n_rcu_perf_reader_started, 0);
586         atomic_set(&n_rcu_perf_writer_started, 0);
587         atomic_set(&n_rcu_perf_writer_finished, 0);
588         rcu_perf_print_module_parms(cur_ops, "Start of test");
589
590         /* Start up the kthreads. */
591
592         if (shutdown) {
593                 init_waitqueue_head(&shutdown_wq);
594                 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
595                                                   shutdown_task);
596                 if (firsterr)
597                         goto unwind;
598                 schedule_timeout_uninterruptible(1);
599         }
600         reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
601                                GFP_KERNEL);
602         if (reader_tasks == NULL) {
603                 VERBOSE_PERFOUT_ERRSTRING("out of memory");
604                 firsterr = -ENOMEM;
605                 goto unwind;
606         }
607         for (i = 0; i < nrealreaders; i++) {
608                 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
609                                                   reader_tasks[i]);
610                 if (firsterr)
611                         goto unwind;
612         }
613         while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
614                 schedule_timeout_uninterruptible(1);
615         writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
616                                GFP_KERNEL);
617         writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
618                                    GFP_KERNEL);
619         writer_n_durations =
620                 kcalloc(nrealwriters, sizeof(*writer_n_durations),
621                         GFP_KERNEL);
622         if (!writer_tasks || !writer_durations || !writer_n_durations) {
623                 VERBOSE_PERFOUT_ERRSTRING("out of memory");
624                 firsterr = -ENOMEM;
625                 goto unwind;
626         }
627         if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp) {
628                 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
629                 firsterr = -EINVAL;
630                 goto unwind;
631         }
632         if (rcu_gp_is_normal() && gp_exp) {
633                 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
634                 firsterr = -EINVAL;
635                 goto unwind;
636         }
637         for (i = 0; i < nrealwriters; i++) {
638                 writer_durations[i] =
639                         kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
640                                 GFP_KERNEL);
641                 if (!writer_durations[i]) {
642                         firsterr = -ENOMEM;
643                         goto unwind;
644                 }
645                 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
646                                                   writer_tasks[i]);
647                 if (firsterr)
648                         goto unwind;
649         }
650         torture_init_end();
651         return 0;
652
653 unwind:
654         torture_init_end();
655         rcu_perf_cleanup();
656         return firsterr;
657 }
658
659 module_init(rcu_perf_init);
660 module_exit(rcu_perf_cleanup);