Merge tag 'xtensa-20200805' of git://github.com/jcmvbkbc/linux-xtensa
[linux-2.6-microblaze.git] / kernel / rcu / rcuperf.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Read-Copy Update module-based performance-test facility
4  *
5  * Copyright (C) IBM Corporation, 2015
6  *
7  * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
8  */
9
10 #define pr_fmt(fmt) fmt
11
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/mm.h>
16 #include <linux/module.h>
17 #include <linux/kthread.h>
18 #include <linux/err.h>
19 #include <linux/spinlock.h>
20 #include <linux/smp.h>
21 #include <linux/rcupdate.h>
22 #include <linux/interrupt.h>
23 #include <linux/sched.h>
24 #include <uapi/linux/sched/types.h>
25 #include <linux/atomic.h>
26 #include <linux/bitops.h>
27 #include <linux/completion.h>
28 #include <linux/moduleparam.h>
29 #include <linux/percpu.h>
30 #include <linux/notifier.h>
31 #include <linux/reboot.h>
32 #include <linux/freezer.h>
33 #include <linux/cpu.h>
34 #include <linux/delay.h>
35 #include <linux/stat.h>
36 #include <linux/srcu.h>
37 #include <linux/slab.h>
38 #include <asm/byteorder.h>
39 #include <linux/torture.h>
40 #include <linux/vmalloc.h>
41
42 #include "rcu.h"
43
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
46
47 #define PERF_FLAG "-perf:"
48 #define PERFOUT_STRING(s) \
49         pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
50 #define VERBOSE_PERFOUT_STRING(s) \
51         do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
52 #define VERBOSE_PERFOUT_ERRSTRING(s) \
53         do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
54
55 /*
56  * The intended use cases for the nreaders and nwriters module parameters
57  * are as follows:
58  *
59  * 1.   Specify only the nr_cpus kernel boot parameter.  This will
60  *      set both nreaders and nwriters to the value specified by
61  *      nr_cpus for a mixed reader/writer test.
62  *
63  * 2.   Specify the nr_cpus kernel boot parameter, but set
64  *      rcuperf.nreaders to zero.  This will set nwriters to the
65  *      value specified by nr_cpus for an update-only test.
66  *
67  * 3.   Specify the nr_cpus kernel boot parameter, but set
68  *      rcuperf.nwriters to zero.  This will set nreaders to the
69  *      value specified by nr_cpus for a read-only test.
70  *
71  * Various other use cases may of course be specified.
72  *
73  * Note that this test's readers are intended only as a test load for
74  * the writers.  The reader performance statistics will be overly
75  * pessimistic due to the per-critical-section interrupt disabling,
76  * test-end checks, and the pair of calls through pointers.
77  */
78
79 #ifdef MODULE
80 # define RCUPERF_SHUTDOWN 0
81 #else
82 # define RCUPERF_SHUTDOWN 1
83 #endif
84
85 torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
86 torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
87 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
88 torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
89 torture_param(int, nreaders, -1, "Number of RCU reader threads");
90 torture_param(int, nwriters, -1, "Number of RCU updater threads");
91 torture_param(bool, shutdown, RCUPERF_SHUTDOWN,
92               "Shutdown at end of performance tests.");
93 torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
94 torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
95 torture_param(int, kfree_rcu_test, 0, "Do we run a kfree_rcu() perf test?");
96 torture_param(int, kfree_mult, 1, "Multiple of kfree_obj size to allocate.");
97
98 static char *perf_type = "rcu";
99 module_param(perf_type, charp, 0444);
100 MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, srcu, ...)");
101
102 static int nrealreaders;
103 static int nrealwriters;
104 static struct task_struct **writer_tasks;
105 static struct task_struct **reader_tasks;
106 static struct task_struct *shutdown_task;
107
108 static u64 **writer_durations;
109 static int *writer_n_durations;
110 static atomic_t n_rcu_perf_reader_started;
111 static atomic_t n_rcu_perf_writer_started;
112 static atomic_t n_rcu_perf_writer_finished;
113 static wait_queue_head_t shutdown_wq;
114 static u64 t_rcu_perf_writer_started;
115 static u64 t_rcu_perf_writer_finished;
116 static unsigned long b_rcu_gp_test_started;
117 static unsigned long b_rcu_gp_test_finished;
118 static DEFINE_PER_CPU(atomic_t, n_async_inflight);
119
120 #define MAX_MEAS 10000
121 #define MIN_MEAS 100
122
123 /*
124  * Operations vector for selecting different types of tests.
125  */
126
127 struct rcu_perf_ops {
128         int ptype;
129         void (*init)(void);
130         void (*cleanup)(void);
131         int (*readlock)(void);
132         void (*readunlock)(int idx);
133         unsigned long (*get_gp_seq)(void);
134         unsigned long (*gp_diff)(unsigned long new, unsigned long old);
135         unsigned long (*exp_completed)(void);
136         void (*async)(struct rcu_head *head, rcu_callback_t func);
137         void (*gp_barrier)(void);
138         void (*sync)(void);
139         void (*exp_sync)(void);
140         const char *name;
141 };
142
143 static struct rcu_perf_ops *cur_ops;
144
145 /*
146  * Definitions for rcu perf testing.
147  */
148
149 static int rcu_perf_read_lock(void) __acquires(RCU)
150 {
151         rcu_read_lock();
152         return 0;
153 }
154
155 static void rcu_perf_read_unlock(int idx) __releases(RCU)
156 {
157         rcu_read_unlock();
158 }
159
160 static unsigned long __maybe_unused rcu_no_completed(void)
161 {
162         return 0;
163 }
164
165 static void rcu_sync_perf_init(void)
166 {
167 }
168
169 static struct rcu_perf_ops rcu_ops = {
170         .ptype          = RCU_FLAVOR,
171         .init           = rcu_sync_perf_init,
172         .readlock       = rcu_perf_read_lock,
173         .readunlock     = rcu_perf_read_unlock,
174         .get_gp_seq     = rcu_get_gp_seq,
175         .gp_diff        = rcu_seq_diff,
176         .exp_completed  = rcu_exp_batches_completed,
177         .async          = call_rcu,
178         .gp_barrier     = rcu_barrier,
179         .sync           = synchronize_rcu,
180         .exp_sync       = synchronize_rcu_expedited,
181         .name           = "rcu"
182 };
183
184 /*
185  * Definitions for srcu perf testing.
186  */
187
188 DEFINE_STATIC_SRCU(srcu_ctl_perf);
189 static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
190
191 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
192 {
193         return srcu_read_lock(srcu_ctlp);
194 }
195
196 static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
197 {
198         srcu_read_unlock(srcu_ctlp, idx);
199 }
200
201 static unsigned long srcu_perf_completed(void)
202 {
203         return srcu_batches_completed(srcu_ctlp);
204 }
205
206 static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
207 {
208         call_srcu(srcu_ctlp, head, func);
209 }
210
211 static void srcu_rcu_barrier(void)
212 {
213         srcu_barrier(srcu_ctlp);
214 }
215
216 static void srcu_perf_synchronize(void)
217 {
218         synchronize_srcu(srcu_ctlp);
219 }
220
221 static void srcu_perf_synchronize_expedited(void)
222 {
223         synchronize_srcu_expedited(srcu_ctlp);
224 }
225
226 static struct rcu_perf_ops srcu_ops = {
227         .ptype          = SRCU_FLAVOR,
228         .init           = rcu_sync_perf_init,
229         .readlock       = srcu_perf_read_lock,
230         .readunlock     = srcu_perf_read_unlock,
231         .get_gp_seq     = srcu_perf_completed,
232         .gp_diff        = rcu_seq_diff,
233         .exp_completed  = srcu_perf_completed,
234         .async          = srcu_call_rcu,
235         .gp_barrier     = srcu_rcu_barrier,
236         .sync           = srcu_perf_synchronize,
237         .exp_sync       = srcu_perf_synchronize_expedited,
238         .name           = "srcu"
239 };
240
241 static struct srcu_struct srcud;
242
243 static void srcu_sync_perf_init(void)
244 {
245         srcu_ctlp = &srcud;
246         init_srcu_struct(srcu_ctlp);
247 }
248
249 static void srcu_sync_perf_cleanup(void)
250 {
251         cleanup_srcu_struct(srcu_ctlp);
252 }
253
254 static struct rcu_perf_ops srcud_ops = {
255         .ptype          = SRCU_FLAVOR,
256         .init           = srcu_sync_perf_init,
257         .cleanup        = srcu_sync_perf_cleanup,
258         .readlock       = srcu_perf_read_lock,
259         .readunlock     = srcu_perf_read_unlock,
260         .get_gp_seq     = srcu_perf_completed,
261         .gp_diff        = rcu_seq_diff,
262         .exp_completed  = srcu_perf_completed,
263         .async          = srcu_call_rcu,
264         .gp_barrier     = srcu_rcu_barrier,
265         .sync           = srcu_perf_synchronize,
266         .exp_sync       = srcu_perf_synchronize_expedited,
267         .name           = "srcud"
268 };
269
270 /*
271  * Definitions for RCU-tasks perf testing.
272  */
273
274 static int tasks_perf_read_lock(void)
275 {
276         return 0;
277 }
278
279 static void tasks_perf_read_unlock(int idx)
280 {
281 }
282
283 static struct rcu_perf_ops tasks_ops = {
284         .ptype          = RCU_TASKS_FLAVOR,
285         .init           = rcu_sync_perf_init,
286         .readlock       = tasks_perf_read_lock,
287         .readunlock     = tasks_perf_read_unlock,
288         .get_gp_seq     = rcu_no_completed,
289         .gp_diff        = rcu_seq_diff,
290         .async          = call_rcu_tasks,
291         .gp_barrier     = rcu_barrier_tasks,
292         .sync           = synchronize_rcu_tasks,
293         .exp_sync       = synchronize_rcu_tasks,
294         .name           = "tasks"
295 };
296
297 static unsigned long rcuperf_seq_diff(unsigned long new, unsigned long old)
298 {
299         if (!cur_ops->gp_diff)
300                 return new - old;
301         return cur_ops->gp_diff(new, old);
302 }
303
304 /*
305  * If performance tests complete, wait for shutdown to commence.
306  */
307 static void rcu_perf_wait_shutdown(void)
308 {
309         cond_resched_tasks_rcu_qs();
310         if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
311                 return;
312         while (!torture_must_stop())
313                 schedule_timeout_uninterruptible(1);
314 }
315
316 /*
317  * RCU perf reader kthread.  Repeatedly does empty RCU read-side critical
318  * section, minimizing update-side interference.  However, the point of
319  * this test is not to evaluate reader performance, but instead to serve
320  * as a test load for update-side performance testing.
321  */
322 static int
323 rcu_perf_reader(void *arg)
324 {
325         unsigned long flags;
326         int idx;
327         long me = (long)arg;
328
329         VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
330         set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
331         set_user_nice(current, MAX_NICE);
332         atomic_inc(&n_rcu_perf_reader_started);
333
334         do {
335                 local_irq_save(flags);
336                 idx = cur_ops->readlock();
337                 cur_ops->readunlock(idx);
338                 local_irq_restore(flags);
339                 rcu_perf_wait_shutdown();
340         } while (!torture_must_stop());
341         torture_kthread_stopping("rcu_perf_reader");
342         return 0;
343 }
344
345 /*
346  * Callback function for asynchronous grace periods from rcu_perf_writer().
347  */
348 static void rcu_perf_async_cb(struct rcu_head *rhp)
349 {
350         atomic_dec(this_cpu_ptr(&n_async_inflight));
351         kfree(rhp);
352 }
353
354 /*
355  * RCU perf writer kthread.  Repeatedly does a grace period.
356  */
357 static int
358 rcu_perf_writer(void *arg)
359 {
360         int i = 0;
361         int i_max;
362         long me = (long)arg;
363         struct rcu_head *rhp = NULL;
364         struct sched_param sp;
365         bool started = false, done = false, alldone = false;
366         u64 t;
367         u64 *wdp;
368         u64 *wdpp = writer_durations[me];
369
370         VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
371         WARN_ON(!wdpp);
372         set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
373         sp.sched_priority = 1;
374         sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
375
376         if (holdoff)
377                 schedule_timeout_uninterruptible(holdoff * HZ);
378
379         /*
380          * Wait until rcu_end_inkernel_boot() is called for normal GP tests
381          * so that RCU is not always expedited for normal GP tests.
382          * The system_state test is approximate, but works well in practice.
383          */
384         while (!gp_exp && system_state != SYSTEM_RUNNING)
385                 schedule_timeout_uninterruptible(1);
386
387         t = ktime_get_mono_fast_ns();
388         if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
389                 t_rcu_perf_writer_started = t;
390                 if (gp_exp) {
391                         b_rcu_gp_test_started =
392                                 cur_ops->exp_completed() / 2;
393                 } else {
394                         b_rcu_gp_test_started = cur_ops->get_gp_seq();
395                 }
396         }
397
398         do {
399                 if (writer_holdoff)
400                         udelay(writer_holdoff);
401                 wdp = &wdpp[i];
402                 *wdp = ktime_get_mono_fast_ns();
403                 if (gp_async) {
404 retry:
405                         if (!rhp)
406                                 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
407                         if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
408                                 atomic_inc(this_cpu_ptr(&n_async_inflight));
409                                 cur_ops->async(rhp, rcu_perf_async_cb);
410                                 rhp = NULL;
411                         } else if (!kthread_should_stop()) {
412                                 cur_ops->gp_barrier();
413                                 goto retry;
414                         } else {
415                                 kfree(rhp); /* Because we are stopping. */
416                         }
417                 } else if (gp_exp) {
418                         cur_ops->exp_sync();
419                 } else {
420                         cur_ops->sync();
421                 }
422                 t = ktime_get_mono_fast_ns();
423                 *wdp = t - *wdp;
424                 i_max = i;
425                 if (!started &&
426                     atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
427                         started = true;
428                 if (!done && i >= MIN_MEAS) {
429                         done = true;
430                         sp.sched_priority = 0;
431                         sched_setscheduler_nocheck(current,
432                                                    SCHED_NORMAL, &sp);
433                         pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
434                                  perf_type, PERF_FLAG, me, MIN_MEAS);
435                         if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
436                             nrealwriters) {
437                                 schedule_timeout_interruptible(10);
438                                 rcu_ftrace_dump(DUMP_ALL);
439                                 PERFOUT_STRING("Test complete");
440                                 t_rcu_perf_writer_finished = t;
441                                 if (gp_exp) {
442                                         b_rcu_gp_test_finished =
443                                                 cur_ops->exp_completed() / 2;
444                                 } else {
445                                         b_rcu_gp_test_finished =
446                                                 cur_ops->get_gp_seq();
447                                 }
448                                 if (shutdown) {
449                                         smp_mb(); /* Assign before wake. */
450                                         wake_up(&shutdown_wq);
451                                 }
452                         }
453                 }
454                 if (done && !alldone &&
455                     atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
456                         alldone = true;
457                 if (started && !alldone && i < MAX_MEAS - 1)
458                         i++;
459                 rcu_perf_wait_shutdown();
460         } while (!torture_must_stop());
461         if (gp_async) {
462                 cur_ops->gp_barrier();
463         }
464         writer_n_durations[me] = i_max;
465         torture_kthread_stopping("rcu_perf_writer");
466         return 0;
467 }
468
469 static void
470 rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
471 {
472         pr_alert("%s" PERF_FLAG
473                  "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
474                  perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
475 }
476
477 static void
478 rcu_perf_cleanup(void)
479 {
480         int i;
481         int j;
482         int ngps = 0;
483         u64 *wdp;
484         u64 *wdpp;
485
486         /*
487          * Would like warning at start, but everything is expedited
488          * during the mid-boot phase, so have to wait till the end.
489          */
490         if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
491                 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
492         if (rcu_gp_is_normal() && gp_exp)
493                 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
494         if (gp_exp && gp_async)
495                 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
496
497         if (torture_cleanup_begin())
498                 return;
499         if (!cur_ops) {
500                 torture_cleanup_end();
501                 return;
502         }
503
504         if (reader_tasks) {
505                 for (i = 0; i < nrealreaders; i++)
506                         torture_stop_kthread(rcu_perf_reader,
507                                              reader_tasks[i]);
508                 kfree(reader_tasks);
509         }
510
511         if (writer_tasks) {
512                 for (i = 0; i < nrealwriters; i++) {
513                         torture_stop_kthread(rcu_perf_writer,
514                                              writer_tasks[i]);
515                         if (!writer_n_durations)
516                                 continue;
517                         j = writer_n_durations[i];
518                         pr_alert("%s%s writer %d gps: %d\n",
519                                  perf_type, PERF_FLAG, i, j);
520                         ngps += j;
521                 }
522                 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
523                          perf_type, PERF_FLAG,
524                          t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
525                          t_rcu_perf_writer_finished -
526                          t_rcu_perf_writer_started,
527                          ngps,
528                          rcuperf_seq_diff(b_rcu_gp_test_finished,
529                                           b_rcu_gp_test_started));
530                 for (i = 0; i < nrealwriters; i++) {
531                         if (!writer_durations)
532                                 break;
533                         if (!writer_n_durations)
534                                 continue;
535                         wdpp = writer_durations[i];
536                         if (!wdpp)
537                                 continue;
538                         for (j = 0; j <= writer_n_durations[i]; j++) {
539                                 wdp = &wdpp[j];
540                                 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
541                                         perf_type, PERF_FLAG,
542                                         i, j, *wdp);
543                                 if (j % 100 == 0)
544                                         schedule_timeout_uninterruptible(1);
545                         }
546                         kfree(writer_durations[i]);
547                 }
548                 kfree(writer_tasks);
549                 kfree(writer_durations);
550                 kfree(writer_n_durations);
551         }
552
553         /* Do torture-type-specific cleanup operations.  */
554         if (cur_ops->cleanup != NULL)
555                 cur_ops->cleanup();
556
557         torture_cleanup_end();
558 }
559
560 /*
561  * Return the number if non-negative.  If -1, the number of CPUs.
562  * If less than -1, that much less than the number of CPUs, but
563  * at least one.
564  */
565 static int compute_real(int n)
566 {
567         int nr;
568
569         if (n >= 0) {
570                 nr = n;
571         } else {
572                 nr = num_online_cpus() + 1 + n;
573                 if (nr <= 0)
574                         nr = 1;
575         }
576         return nr;
577 }
578
579 /*
580  * RCU perf shutdown kthread.  Just waits to be awakened, then shuts
581  * down system.
582  */
583 static int
584 rcu_perf_shutdown(void *arg)
585 {
586         wait_event(shutdown_wq,
587                    atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters);
588         smp_mb(); /* Wake before output. */
589         rcu_perf_cleanup();
590         kernel_power_off();
591         return -EINVAL;
592 }
593
594 /*
595  * kfree_rcu() performance tests: Start a kfree_rcu() loop on all CPUs for number
596  * of iterations and measure total time and number of GP for all iterations to complete.
597  */
598
599 torture_param(int, kfree_nthreads, -1, "Number of threads running loops of kfree_rcu().");
600 torture_param(int, kfree_alloc_num, 8000, "Number of allocations and frees done in an iteration.");
601 torture_param(int, kfree_loops, 10, "Number of loops doing kfree_alloc_num allocations and frees.");
602
603 static struct task_struct **kfree_reader_tasks;
604 static int kfree_nrealthreads;
605 static atomic_t n_kfree_perf_thread_started;
606 static atomic_t n_kfree_perf_thread_ended;
607
608 struct kfree_obj {
609         char kfree_obj[8];
610         struct rcu_head rh;
611 };
612
613 static int
614 kfree_perf_thread(void *arg)
615 {
616         int i, loop = 0;
617         long me = (long)arg;
618         struct kfree_obj *alloc_ptr;
619         u64 start_time, end_time;
620         long long mem_begin, mem_during = 0;
621
622         VERBOSE_PERFOUT_STRING("kfree_perf_thread task started");
623         set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
624         set_user_nice(current, MAX_NICE);
625
626         start_time = ktime_get_mono_fast_ns();
627
628         if (atomic_inc_return(&n_kfree_perf_thread_started) >= kfree_nrealthreads) {
629                 if (gp_exp)
630                         b_rcu_gp_test_started = cur_ops->exp_completed() / 2;
631                 else
632                         b_rcu_gp_test_started = cur_ops->get_gp_seq();
633         }
634
635         do {
636                 if (!mem_during) {
637                         mem_during = mem_begin = si_mem_available();
638                 } else if (loop % (kfree_loops / 4) == 0) {
639                         mem_during = (mem_during + si_mem_available()) / 2;
640                 }
641
642                 for (i = 0; i < kfree_alloc_num; i++) {
643                         alloc_ptr = kmalloc(kfree_mult * sizeof(struct kfree_obj), GFP_KERNEL);
644                         if (!alloc_ptr)
645                                 return -ENOMEM;
646
647                         kfree_rcu(alloc_ptr, rh);
648                 }
649
650                 cond_resched();
651         } while (!torture_must_stop() && ++loop < kfree_loops);
652
653         if (atomic_inc_return(&n_kfree_perf_thread_ended) >= kfree_nrealthreads) {
654                 end_time = ktime_get_mono_fast_ns();
655
656                 if (gp_exp)
657                         b_rcu_gp_test_finished = cur_ops->exp_completed() / 2;
658                 else
659                         b_rcu_gp_test_finished = cur_ops->get_gp_seq();
660
661                 pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n",
662                        (unsigned long long)(end_time - start_time), kfree_loops,
663                        rcuperf_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started),
664                        (mem_begin - mem_during) >> (20 - PAGE_SHIFT));
665
666                 if (shutdown) {
667                         smp_mb(); /* Assign before wake. */
668                         wake_up(&shutdown_wq);
669                 }
670         }
671
672         torture_kthread_stopping("kfree_perf_thread");
673         return 0;
674 }
675
676 static void
677 kfree_perf_cleanup(void)
678 {
679         int i;
680
681         if (torture_cleanup_begin())
682                 return;
683
684         if (kfree_reader_tasks) {
685                 for (i = 0; i < kfree_nrealthreads; i++)
686                         torture_stop_kthread(kfree_perf_thread,
687                                              kfree_reader_tasks[i]);
688                 kfree(kfree_reader_tasks);
689         }
690
691         torture_cleanup_end();
692 }
693
694 /*
695  * shutdown kthread.  Just waits to be awakened, then shuts down system.
696  */
697 static int
698 kfree_perf_shutdown(void *arg)
699 {
700         wait_event(shutdown_wq,
701                    atomic_read(&n_kfree_perf_thread_ended) >= kfree_nrealthreads);
702
703         smp_mb(); /* Wake before output. */
704
705         kfree_perf_cleanup();
706         kernel_power_off();
707         return -EINVAL;
708 }
709
710 static int __init
711 kfree_perf_init(void)
712 {
713         long i;
714         int firsterr = 0;
715
716         kfree_nrealthreads = compute_real(kfree_nthreads);
717         /* Start up the kthreads. */
718         if (shutdown) {
719                 init_waitqueue_head(&shutdown_wq);
720                 firsterr = torture_create_kthread(kfree_perf_shutdown, NULL,
721                                                   shutdown_task);
722                 if (firsterr)
723                         goto unwind;
724                 schedule_timeout_uninterruptible(1);
725         }
726
727         pr_alert("kfree object size=%zu\n", kfree_mult * sizeof(struct kfree_obj));
728
729         kfree_reader_tasks = kcalloc(kfree_nrealthreads, sizeof(kfree_reader_tasks[0]),
730                                GFP_KERNEL);
731         if (kfree_reader_tasks == NULL) {
732                 firsterr = -ENOMEM;
733                 goto unwind;
734         }
735
736         for (i = 0; i < kfree_nrealthreads; i++) {
737                 firsterr = torture_create_kthread(kfree_perf_thread, (void *)i,
738                                                   kfree_reader_tasks[i]);
739                 if (firsterr)
740                         goto unwind;
741         }
742
743         while (atomic_read(&n_kfree_perf_thread_started) < kfree_nrealthreads)
744                 schedule_timeout_uninterruptible(1);
745
746         torture_init_end();
747         return 0;
748
749 unwind:
750         torture_init_end();
751         kfree_perf_cleanup();
752         return firsterr;
753 }
754
755 static int __init
756 rcu_perf_init(void)
757 {
758         long i;
759         int firsterr = 0;
760         static struct rcu_perf_ops *perf_ops[] = {
761                 &rcu_ops, &srcu_ops, &srcud_ops, &tasks_ops,
762         };
763
764         if (!torture_init_begin(perf_type, verbose))
765                 return -EBUSY;
766
767         /* Process args and tell the world that the perf'er is on the job. */
768         for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
769                 cur_ops = perf_ops[i];
770                 if (strcmp(perf_type, cur_ops->name) == 0)
771                         break;
772         }
773         if (i == ARRAY_SIZE(perf_ops)) {
774                 pr_alert("rcu-perf: invalid perf type: \"%s\"\n", perf_type);
775                 pr_alert("rcu-perf types:");
776                 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
777                         pr_cont(" %s", perf_ops[i]->name);
778                 pr_cont("\n");
779                 WARN_ON(!IS_MODULE(CONFIG_RCU_PERF_TEST));
780                 firsterr = -EINVAL;
781                 cur_ops = NULL;
782                 goto unwind;
783         }
784         if (cur_ops->init)
785                 cur_ops->init();
786
787         if (kfree_rcu_test)
788                 return kfree_perf_init();
789
790         nrealwriters = compute_real(nwriters);
791         nrealreaders = compute_real(nreaders);
792         atomic_set(&n_rcu_perf_reader_started, 0);
793         atomic_set(&n_rcu_perf_writer_started, 0);
794         atomic_set(&n_rcu_perf_writer_finished, 0);
795         rcu_perf_print_module_parms(cur_ops, "Start of test");
796
797         /* Start up the kthreads. */
798
799         if (shutdown) {
800                 init_waitqueue_head(&shutdown_wq);
801                 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
802                                                   shutdown_task);
803                 if (firsterr)
804                         goto unwind;
805                 schedule_timeout_uninterruptible(1);
806         }
807         reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
808                                GFP_KERNEL);
809         if (reader_tasks == NULL) {
810                 VERBOSE_PERFOUT_ERRSTRING("out of memory");
811                 firsterr = -ENOMEM;
812                 goto unwind;
813         }
814         for (i = 0; i < nrealreaders; i++) {
815                 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
816                                                   reader_tasks[i]);
817                 if (firsterr)
818                         goto unwind;
819         }
820         while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
821                 schedule_timeout_uninterruptible(1);
822         writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
823                                GFP_KERNEL);
824         writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
825                                    GFP_KERNEL);
826         writer_n_durations =
827                 kcalloc(nrealwriters, sizeof(*writer_n_durations),
828                         GFP_KERNEL);
829         if (!writer_tasks || !writer_durations || !writer_n_durations) {
830                 VERBOSE_PERFOUT_ERRSTRING("out of memory");
831                 firsterr = -ENOMEM;
832                 goto unwind;
833         }
834         for (i = 0; i < nrealwriters; i++) {
835                 writer_durations[i] =
836                         kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
837                                 GFP_KERNEL);
838                 if (!writer_durations[i]) {
839                         firsterr = -ENOMEM;
840                         goto unwind;
841                 }
842                 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
843                                                   writer_tasks[i]);
844                 if (firsterr)
845                         goto unwind;
846         }
847         torture_init_end();
848         return 0;
849
850 unwind:
851         torture_init_end();
852         rcu_perf_cleanup();
853         return firsterr;
854 }
855
856 module_init(rcu_perf_init);
857 module_exit(rcu_perf_cleanup);