refscale: Always log the error message
[sfrench/cifs-2.6.git] / kernel / rcu / refscale.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Scalability test comparing RCU vs other mechanisms
4 // for acquiring references on objects.
5 //
6 // Copyright (C) Google, 2020.
7 //
8 // Author: Joel Fernandes <joel@joelfernandes.org>
9
10 #define pr_fmt(fmt) fmt
11
12 #include <linux/atomic.h>
13 #include <linux/bitops.h>
14 #include <linux/completion.h>
15 #include <linux/cpu.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kthread.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/notifier.h>
26 #include <linux/percpu.h>
27 #include <linux/rcupdate.h>
28 #include <linux/rcupdate_trace.h>
29 #include <linux/reboot.h>
30 #include <linux/sched.h>
31 #include <linux/spinlock.h>
32 #include <linux/smp.h>
33 #include <linux/stat.h>
34 #include <linux/srcu.h>
35 #include <linux/slab.h>
36 #include <linux/torture.h>
37 #include <linux/types.h>
38
39 #include "rcu.h"
40
41 #define SCALE_FLAG "-ref-scale: "
42
43 #define SCALEOUT(s, x...) \
44         pr_alert("%s" SCALE_FLAG s, scale_type, ## x)
45
46 #define VERBOSE_SCALEOUT(s, x...) \
47         do { if (verbose) pr_alert("%s" SCALE_FLAG s, scale_type, ## x); } while (0)
48
49 static atomic_t verbose_batch_ctr;
50
51 #define VERBOSE_SCALEOUT_BATCH(s, x...)                                                 \
52 do {                                                                                    \
53         if (verbose &&                                                                  \
54             (verbose_batched <= 0 ||                                                    \
55              !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) {             \
56                 schedule_timeout_uninterruptible(1);                                    \
57                 pr_alert("%s" SCALE_FLAG s, scale_type, ## x);                          \
58         }                                                                               \
59 } while (0)
60
61 #define SCALEOUT_ERRSTRING(s, x...) pr_alert("%s" SCALE_FLAG "!!! " s, scale_type, ## x)
62
63 MODULE_LICENSE("GPL");
64 MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>");
65
66 static char *scale_type = "rcu";
67 module_param(scale_type, charp, 0444);
68 MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock.");
69
70 torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
71 torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s");
72
73 // Wait until there are multiple CPUs before starting test.
74 torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0,
75               "Holdoff time before test start (s)");
76 // Number of loops per experiment, all readers execute operations concurrently.
77 torture_param(long, loops, 10000, "Number of loops per experiment.");
78 // Number of readers, with -1 defaulting to about 75% of the CPUs.
79 torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs.");
80 // Number of runs.
81 torture_param(int, nruns, 30, "Number of experiments to run.");
82 // Reader delay in nanoseconds, 0 for no delay.
83 torture_param(int, readdelay, 0, "Read-side delay in nanoseconds.");
84
85 #ifdef MODULE
86 # define REFSCALE_SHUTDOWN 0
87 #else
88 # define REFSCALE_SHUTDOWN 1
89 #endif
90
91 torture_param(bool, shutdown, REFSCALE_SHUTDOWN,
92               "Shutdown at end of scalability tests.");
93
94 struct reader_task {
95         struct task_struct *task;
96         int start_reader;
97         wait_queue_head_t wq;
98         u64 last_duration_ns;
99 };
100
101 static struct task_struct *shutdown_task;
102 static wait_queue_head_t shutdown_wq;
103
104 static struct task_struct *main_task;
105 static wait_queue_head_t main_wq;
106 static int shutdown_start;
107
108 static struct reader_task *reader_tasks;
109
110 // Number of readers that are part of the current experiment.
111 static atomic_t nreaders_exp;
112
113 // Use to wait for all threads to start.
114 static atomic_t n_init;
115 static atomic_t n_started;
116 static atomic_t n_warmedup;
117 static atomic_t n_cooleddown;
118
119 // Track which experiment is currently running.
120 static int exp_idx;
121
122 // Operations vector for selecting different types of tests.
123 struct ref_scale_ops {
124         void (*init)(void);
125         void (*cleanup)(void);
126         void (*readsection)(const int nloops);
127         void (*delaysection)(const int nloops, const int udl, const int ndl);
128         const char *name;
129 };
130
131 static struct ref_scale_ops *cur_ops;
132
133 static void un_delay(const int udl, const int ndl)
134 {
135         if (udl)
136                 udelay(udl);
137         if (ndl)
138                 ndelay(ndl);
139 }
140
141 static void ref_rcu_read_section(const int nloops)
142 {
143         int i;
144
145         for (i = nloops; i >= 0; i--) {
146                 rcu_read_lock();
147                 rcu_read_unlock();
148         }
149 }
150
151 static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl)
152 {
153         int i;
154
155         for (i = nloops; i >= 0; i--) {
156                 rcu_read_lock();
157                 un_delay(udl, ndl);
158                 rcu_read_unlock();
159         }
160 }
161
162 static void rcu_sync_scale_init(void)
163 {
164 }
165
166 static struct ref_scale_ops rcu_ops = {
167         .init           = rcu_sync_scale_init,
168         .readsection    = ref_rcu_read_section,
169         .delaysection   = ref_rcu_delay_section,
170         .name           = "rcu"
171 };
172
173 // Definitions for SRCU ref scale testing.
174 DEFINE_STATIC_SRCU(srcu_refctl_scale);
175 static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale;
176
177 static void srcu_ref_scale_read_section(const int nloops)
178 {
179         int i;
180         int idx;
181
182         for (i = nloops; i >= 0; i--) {
183                 idx = srcu_read_lock(srcu_ctlp);
184                 srcu_read_unlock(srcu_ctlp, idx);
185         }
186 }
187
188 static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
189 {
190         int i;
191         int idx;
192
193         for (i = nloops; i >= 0; i--) {
194                 idx = srcu_read_lock(srcu_ctlp);
195                 un_delay(udl, ndl);
196                 srcu_read_unlock(srcu_ctlp, idx);
197         }
198 }
199
200 static struct ref_scale_ops srcu_ops = {
201         .init           = rcu_sync_scale_init,
202         .readsection    = srcu_ref_scale_read_section,
203         .delaysection   = srcu_ref_scale_delay_section,
204         .name           = "srcu"
205 };
206
207 // Definitions for RCU Tasks ref scale testing: Empty read markers.
208 // These definitions also work for RCU Rude readers.
209 static void rcu_tasks_ref_scale_read_section(const int nloops)
210 {
211         int i;
212
213         for (i = nloops; i >= 0; i--)
214                 continue;
215 }
216
217 static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
218 {
219         int i;
220
221         for (i = nloops; i >= 0; i--)
222                 un_delay(udl, ndl);
223 }
224
225 static struct ref_scale_ops rcu_tasks_ops = {
226         .init           = rcu_sync_scale_init,
227         .readsection    = rcu_tasks_ref_scale_read_section,
228         .delaysection   = rcu_tasks_ref_scale_delay_section,
229         .name           = "rcu-tasks"
230 };
231
232 // Definitions for RCU Tasks Trace ref scale testing.
233 static void rcu_trace_ref_scale_read_section(const int nloops)
234 {
235         int i;
236
237         for (i = nloops; i >= 0; i--) {
238                 rcu_read_lock_trace();
239                 rcu_read_unlock_trace();
240         }
241 }
242
243 static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
244 {
245         int i;
246
247         for (i = nloops; i >= 0; i--) {
248                 rcu_read_lock_trace();
249                 un_delay(udl, ndl);
250                 rcu_read_unlock_trace();
251         }
252 }
253
254 static struct ref_scale_ops rcu_trace_ops = {
255         .init           = rcu_sync_scale_init,
256         .readsection    = rcu_trace_ref_scale_read_section,
257         .delaysection   = rcu_trace_ref_scale_delay_section,
258         .name           = "rcu-trace"
259 };
260
261 // Definitions for reference count
262 static atomic_t refcnt;
263
264 static void ref_refcnt_section(const int nloops)
265 {
266         int i;
267
268         for (i = nloops; i >= 0; i--) {
269                 atomic_inc(&refcnt);
270                 atomic_dec(&refcnt);
271         }
272 }
273
274 static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl)
275 {
276         int i;
277
278         for (i = nloops; i >= 0; i--) {
279                 atomic_inc(&refcnt);
280                 un_delay(udl, ndl);
281                 atomic_dec(&refcnt);
282         }
283 }
284
285 static struct ref_scale_ops refcnt_ops = {
286         .init           = rcu_sync_scale_init,
287         .readsection    = ref_refcnt_section,
288         .delaysection   = ref_refcnt_delay_section,
289         .name           = "refcnt"
290 };
291
292 // Definitions for rwlock
293 static rwlock_t test_rwlock;
294
295 static void ref_rwlock_init(void)
296 {
297         rwlock_init(&test_rwlock);
298 }
299
300 static void ref_rwlock_section(const int nloops)
301 {
302         int i;
303
304         for (i = nloops; i >= 0; i--) {
305                 read_lock(&test_rwlock);
306                 read_unlock(&test_rwlock);
307         }
308 }
309
310 static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl)
311 {
312         int i;
313
314         for (i = nloops; i >= 0; i--) {
315                 read_lock(&test_rwlock);
316                 un_delay(udl, ndl);
317                 read_unlock(&test_rwlock);
318         }
319 }
320
321 static struct ref_scale_ops rwlock_ops = {
322         .init           = ref_rwlock_init,
323         .readsection    = ref_rwlock_section,
324         .delaysection   = ref_rwlock_delay_section,
325         .name           = "rwlock"
326 };
327
328 // Definitions for rwsem
329 static struct rw_semaphore test_rwsem;
330
331 static void ref_rwsem_init(void)
332 {
333         init_rwsem(&test_rwsem);
334 }
335
336 static void ref_rwsem_section(const int nloops)
337 {
338         int i;
339
340         for (i = nloops; i >= 0; i--) {
341                 down_read(&test_rwsem);
342                 up_read(&test_rwsem);
343         }
344 }
345
346 static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl)
347 {
348         int i;
349
350         for (i = nloops; i >= 0; i--) {
351                 down_read(&test_rwsem);
352                 un_delay(udl, ndl);
353                 up_read(&test_rwsem);
354         }
355 }
356
357 static struct ref_scale_ops rwsem_ops = {
358         .init           = ref_rwsem_init,
359         .readsection    = ref_rwsem_section,
360         .delaysection   = ref_rwsem_delay_section,
361         .name           = "rwsem"
362 };
363
364 // Definitions for global spinlock
365 static DEFINE_SPINLOCK(test_lock);
366
367 static void ref_lock_section(const int nloops)
368 {
369         int i;
370
371         preempt_disable();
372         for (i = nloops; i >= 0; i--) {
373                 spin_lock(&test_lock);
374                 spin_unlock(&test_lock);
375         }
376         preempt_enable();
377 }
378
379 static void ref_lock_delay_section(const int nloops, const int udl, const int ndl)
380 {
381         int i;
382
383         preempt_disable();
384         for (i = nloops; i >= 0; i--) {
385                 spin_lock(&test_lock);
386                 un_delay(udl, ndl);
387                 spin_unlock(&test_lock);
388         }
389         preempt_enable();
390 }
391
392 static struct ref_scale_ops lock_ops = {
393         .readsection    = ref_lock_section,
394         .delaysection   = ref_lock_delay_section,
395         .name           = "lock"
396 };
397
398 // Definitions for global irq-save spinlock
399
400 static void ref_lock_irq_section(const int nloops)
401 {
402         unsigned long flags;
403         int i;
404
405         preempt_disable();
406         for (i = nloops; i >= 0; i--) {
407                 spin_lock_irqsave(&test_lock, flags);
408                 spin_unlock_irqrestore(&test_lock, flags);
409         }
410         preempt_enable();
411 }
412
413 static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl)
414 {
415         unsigned long flags;
416         int i;
417
418         preempt_disable();
419         for (i = nloops; i >= 0; i--) {
420                 spin_lock_irqsave(&test_lock, flags);
421                 un_delay(udl, ndl);
422                 spin_unlock_irqrestore(&test_lock, flags);
423         }
424         preempt_enable();
425 }
426
427 static struct ref_scale_ops lock_irq_ops = {
428         .readsection    = ref_lock_irq_section,
429         .delaysection   = ref_lock_irq_delay_section,
430         .name           = "lock-irq"
431 };
432
433 // Definitions acquire-release.
434 static DEFINE_PER_CPU(unsigned long, test_acqrel);
435
436 static void ref_acqrel_section(const int nloops)
437 {
438         unsigned long x;
439         int i;
440
441         preempt_disable();
442         for (i = nloops; i >= 0; i--) {
443                 x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
444                 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
445         }
446         preempt_enable();
447 }
448
449 static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl)
450 {
451         unsigned long x;
452         int i;
453
454         preempt_disable();
455         for (i = nloops; i >= 0; i--) {
456                 x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
457                 un_delay(udl, ndl);
458                 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
459         }
460         preempt_enable();
461 }
462
463 static struct ref_scale_ops acqrel_ops = {
464         .readsection    = ref_acqrel_section,
465         .delaysection   = ref_acqrel_delay_section,
466         .name           = "acqrel"
467 };
468
469 static volatile u64 stopopts;
470
471 static void ref_clock_section(const int nloops)
472 {
473         u64 x = 0;
474         int i;
475
476         preempt_disable();
477         for (i = nloops; i >= 0; i--)
478                 x += ktime_get_real_fast_ns();
479         preempt_enable();
480         stopopts = x;
481 }
482
483 static void ref_clock_delay_section(const int nloops, const int udl, const int ndl)
484 {
485         u64 x = 0;
486         int i;
487
488         preempt_disable();
489         for (i = nloops; i >= 0; i--) {
490                 x += ktime_get_real_fast_ns();
491                 un_delay(udl, ndl);
492         }
493         preempt_enable();
494         stopopts = x;
495 }
496
497 static struct ref_scale_ops clock_ops = {
498         .readsection    = ref_clock_section,
499         .delaysection   = ref_clock_delay_section,
500         .name           = "clock"
501 };
502
503 static void rcu_scale_one_reader(void)
504 {
505         if (readdelay <= 0)
506                 cur_ops->readsection(loops);
507         else
508                 cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000);
509 }
510
511 // Reader kthread.  Repeatedly does empty RCU read-side
512 // critical section, minimizing update-side interference.
513 static int
514 ref_scale_reader(void *arg)
515 {
516         unsigned long flags;
517         long me = (long)arg;
518         struct reader_task *rt = &(reader_tasks[me]);
519         u64 start;
520         s64 duration;
521
522         VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me);
523         WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)));
524         set_user_nice(current, MAX_NICE);
525         atomic_inc(&n_init);
526         if (holdoff)
527                 schedule_timeout_interruptible(holdoff * HZ);
528 repeat:
529         VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id());
530
531         // Wait for signal that this reader can start.
532         wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) ||
533                            torture_must_stop());
534
535         if (torture_must_stop())
536                 goto end;
537
538         // Make sure that the CPU is affinitized appropriately during testing.
539         WARN_ON_ONCE(raw_smp_processor_id() != me);
540
541         WRITE_ONCE(rt->start_reader, 0);
542         if (!atomic_dec_return(&n_started))
543                 while (atomic_read_acquire(&n_started))
544                         cpu_relax();
545
546         VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx);
547
548
549         // To reduce noise, do an initial cache-warming invocation, check
550         // in, and then keep warming until everyone has checked in.
551         rcu_scale_one_reader();
552         if (!atomic_dec_return(&n_warmedup))
553                 while (atomic_read_acquire(&n_warmedup))
554                         rcu_scale_one_reader();
555         // Also keep interrupts disabled.  This also has the effect
556         // of preventing entries into slow path for rcu_read_unlock().
557         local_irq_save(flags);
558         start = ktime_get_mono_fast_ns();
559
560         rcu_scale_one_reader();
561
562         duration = ktime_get_mono_fast_ns() - start;
563         local_irq_restore(flags);
564
565         rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration;
566         // To reduce runtime-skew noise, do maintain-load invocations until
567         // everyone is done.
568         if (!atomic_dec_return(&n_cooleddown))
569                 while (atomic_read_acquire(&n_cooleddown))
570                         rcu_scale_one_reader();
571
572         if (atomic_dec_and_test(&nreaders_exp))
573                 wake_up(&main_wq);
574
575         VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)",
576                                 me, exp_idx, atomic_read(&nreaders_exp));
577
578         if (!torture_must_stop())
579                 goto repeat;
580 end:
581         torture_kthread_stopping("ref_scale_reader");
582         return 0;
583 }
584
585 static void reset_readers(void)
586 {
587         int i;
588         struct reader_task *rt;
589
590         for (i = 0; i < nreaders; i++) {
591                 rt = &(reader_tasks[i]);
592
593                 rt->last_duration_ns = 0;
594         }
595 }
596
597 // Print the results of each reader and return the sum of all their durations.
598 static u64 process_durations(int n)
599 {
600         int i;
601         struct reader_task *rt;
602         char buf1[64];
603         char *buf;
604         u64 sum = 0;
605
606         buf = kmalloc(800 + 64, GFP_KERNEL);
607         if (!buf)
608                 return 0;
609         buf[0] = 0;
610         sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
611                 exp_idx);
612
613         for (i = 0; i < n && !torture_must_stop(); i++) {
614                 rt = &(reader_tasks[i]);
615                 sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns);
616
617                 if (i % 5 == 0)
618                         strcat(buf, "\n");
619                 if (strlen(buf) >= 800) {
620                         pr_alert("%s", buf);
621                         buf[0] = 0;
622                 }
623                 strcat(buf, buf1);
624
625                 sum += rt->last_duration_ns;
626         }
627         pr_alert("%s\n", buf);
628
629         kfree(buf);
630         return sum;
631 }
632
633 // The main_func is the main orchestrator, it performs a bunch of
634 // experiments.  For every experiment, it orders all the readers
635 // involved to start and waits for them to finish the experiment. It
636 // then reads their timestamps and starts the next experiment. Each
637 // experiment progresses from 1 concurrent reader to N of them at which
638 // point all the timestamps are printed.
639 static int main_func(void *arg)
640 {
641         int exp, r;
642         char buf1[64];
643         char *buf;
644         u64 *result_avg;
645
646         set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids));
647         set_user_nice(current, MAX_NICE);
648
649         VERBOSE_SCALEOUT("main_func task started");
650         result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL);
651         buf = kzalloc(800 + 64, GFP_KERNEL);
652         if (!result_avg || !buf) {
653                 SCALEOUT_ERRSTRING("out of memory");
654                 goto oom_exit;
655         }
656         if (holdoff)
657                 schedule_timeout_interruptible(holdoff * HZ);
658
659         // Wait for all threads to start.
660         atomic_inc(&n_init);
661         while (atomic_read(&n_init) < nreaders + 1)
662                 schedule_timeout_uninterruptible(1);
663
664         // Start exp readers up per experiment
665         for (exp = 0; exp < nruns && !torture_must_stop(); exp++) {
666                 if (torture_must_stop())
667                         goto end;
668
669                 reset_readers();
670                 atomic_set(&nreaders_exp, nreaders);
671                 atomic_set(&n_started, nreaders);
672                 atomic_set(&n_warmedup, nreaders);
673                 atomic_set(&n_cooleddown, nreaders);
674
675                 exp_idx = exp;
676
677                 for (r = 0; r < nreaders; r++) {
678                         smp_store_release(&reader_tasks[r].start_reader, 1);
679                         wake_up(&reader_tasks[r].wq);
680                 }
681
682                 VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers",
683                                 nreaders);
684
685                 wait_event(main_wq,
686                            !atomic_read(&nreaders_exp) || torture_must_stop());
687
688                 VERBOSE_SCALEOUT("main_func: experiment ended");
689
690                 if (torture_must_stop())
691                         goto end;
692
693                 result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops);
694         }
695
696         // Print the average of all experiments
697         SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n");
698
699         pr_alert("Runs\tTime(ns)\n");
700         for (exp = 0; exp < nruns; exp++) {
701                 u64 avg;
702                 u32 rem;
703
704                 avg = div_u64_rem(result_avg[exp], 1000, &rem);
705                 sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem);
706                 strcat(buf, buf1);
707                 if (strlen(buf) >= 800) {
708                         pr_alert("%s", buf);
709                         buf[0] = 0;
710                 }
711         }
712
713         pr_alert("%s", buf);
714
715 oom_exit:
716         // This will shutdown everything including us.
717         if (shutdown) {
718                 shutdown_start = 1;
719                 wake_up(&shutdown_wq);
720         }
721
722         // Wait for torture to stop us
723         while (!torture_must_stop())
724                 schedule_timeout_uninterruptible(1);
725
726 end:
727         torture_kthread_stopping("main_func");
728         kfree(result_avg);
729         kfree(buf);
730         return 0;
731 }
732
733 static void
734 ref_scale_print_module_parms(struct ref_scale_ops *cur_ops, const char *tag)
735 {
736         pr_alert("%s" SCALE_FLAG
737                  "--- %s:  verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag,
738                  verbose, shutdown, holdoff, loops, nreaders, nruns, readdelay);
739 }
740
741 static void
742 ref_scale_cleanup(void)
743 {
744         int i;
745
746         if (torture_cleanup_begin())
747                 return;
748
749         if (!cur_ops) {
750                 torture_cleanup_end();
751                 return;
752         }
753
754         if (reader_tasks) {
755                 for (i = 0; i < nreaders; i++)
756                         torture_stop_kthread("ref_scale_reader",
757                                              reader_tasks[i].task);
758         }
759         kfree(reader_tasks);
760
761         torture_stop_kthread("main_task", main_task);
762         kfree(main_task);
763
764         // Do scale-type-specific cleanup operations.
765         if (cur_ops->cleanup != NULL)
766                 cur_ops->cleanup();
767
768         torture_cleanup_end();
769 }
770
771 // Shutdown kthread.  Just waits to be awakened, then shuts down system.
772 static int
773 ref_scale_shutdown(void *arg)
774 {
775         wait_event(shutdown_wq, shutdown_start);
776
777         smp_mb(); // Wake before output.
778         ref_scale_cleanup();
779         kernel_power_off();
780
781         return -EINVAL;
782 }
783
784 static int __init
785 ref_scale_init(void)
786 {
787         long i;
788         int firsterr = 0;
789         static struct ref_scale_ops *scale_ops[] = {
790                 &rcu_ops, &srcu_ops, &rcu_trace_ops, &rcu_tasks_ops, &refcnt_ops, &rwlock_ops,
791                 &rwsem_ops, &lock_ops, &lock_irq_ops, &acqrel_ops, &clock_ops,
792         };
793
794         if (!torture_init_begin(scale_type, verbose))
795                 return -EBUSY;
796
797         for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
798                 cur_ops = scale_ops[i];
799                 if (strcmp(scale_type, cur_ops->name) == 0)
800                         break;
801         }
802         if (i == ARRAY_SIZE(scale_ops)) {
803                 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
804                 pr_alert("rcu-scale types:");
805                 for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
806                         pr_cont(" %s", scale_ops[i]->name);
807                 pr_cont("\n");
808                 firsterr = -EINVAL;
809                 cur_ops = NULL;
810                 goto unwind;
811         }
812         if (cur_ops->init)
813                 cur_ops->init();
814
815         ref_scale_print_module_parms(cur_ops, "Start of test");
816
817         // Shutdown task
818         if (shutdown) {
819                 init_waitqueue_head(&shutdown_wq);
820                 firsterr = torture_create_kthread(ref_scale_shutdown, NULL,
821                                                   shutdown_task);
822                 if (torture_init_error(firsterr))
823                         goto unwind;
824                 schedule_timeout_uninterruptible(1);
825         }
826
827         // Reader tasks (default to ~75% of online CPUs).
828         if (nreaders < 0)
829                 nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2);
830         if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops))
831                 loops = 1;
832         if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders))
833                 nreaders = 1;
834         if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns))
835                 nruns = 1;
836         reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]),
837                                GFP_KERNEL);
838         if (!reader_tasks) {
839                 SCALEOUT_ERRSTRING("out of memory");
840                 firsterr = -ENOMEM;
841                 goto unwind;
842         }
843
844         VERBOSE_SCALEOUT("Starting %d reader threads\n", nreaders);
845
846         for (i = 0; i < nreaders; i++) {
847                 firsterr = torture_create_kthread(ref_scale_reader, (void *)i,
848                                                   reader_tasks[i].task);
849                 if (torture_init_error(firsterr))
850                         goto unwind;
851
852                 init_waitqueue_head(&(reader_tasks[i].wq));
853         }
854
855         // Main Task
856         init_waitqueue_head(&main_wq);
857         firsterr = torture_create_kthread(main_func, NULL, main_task);
858         if (torture_init_error(firsterr))
859                 goto unwind;
860
861         torture_init_end();
862         return 0;
863
864 unwind:
865         torture_init_end();
866         ref_scale_cleanup();
867         if (shutdown) {
868                 WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST));
869                 kernel_power_off();
870         }
871         return firsterr;
872 }
873
874 module_init(ref_scale_init);
875 module_exit(ref_scale_cleanup);