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