GNU Linux-libre 5.15.137-gnu
[releases.git] / kernel / scftorture.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Torture test for smp_call_function() and friends.
4 //
5 // Copyright (C) Facebook, 2020.
6 //
7 // Author: Paul E. McKenney <paulmck@kernel.org>
8
9 #define pr_fmt(fmt) fmt
10
11 #include <linux/atomic.h>
12 #include <linux/bitops.h>
13 #include <linux/completion.h>
14 #include <linux/cpu.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/kthread.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/notifier.h>
25 #include <linux/percpu.h>
26 #include <linux/rcupdate.h>
27 #include <linux/rcupdate_trace.h>
28 #include <linux/reboot.h>
29 #include <linux/sched.h>
30 #include <linux/spinlock.h>
31 #include <linux/smp.h>
32 #include <linux/stat.h>
33 #include <linux/srcu.h>
34 #include <linux/slab.h>
35 #include <linux/torture.h>
36 #include <linux/types.h>
37
38 #define SCFTORT_STRING "scftorture"
39 #define SCFTORT_FLAG SCFTORT_STRING ": "
40
41 #define SCFTORTOUT(s, x...) \
42         pr_alert(SCFTORT_FLAG s, ## x)
43
44 #define VERBOSE_SCFTORTOUT(s, x...) \
45         do { if (verbose) pr_alert(SCFTORT_FLAG s, ## x); } while (0)
46
47 #define VERBOSE_SCFTORTOUT_ERRSTRING(s, x...) \
48         do { if (verbose) pr_alert(SCFTORT_FLAG "!!! " s, ## x); } while (0)
49
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Paul E. McKenney <paulmck@kernel.org>");
52
53 // Wait until there are multiple CPUs before starting test.
54 torture_param(int, holdoff, IS_BUILTIN(CONFIG_SCF_TORTURE_TEST) ? 10 : 0,
55               "Holdoff time before test start (s)");
56 torture_param(int, longwait, 0, "Include ridiculously long waits? (seconds)");
57 torture_param(int, nthreads, -1, "# threads, defaults to -1 for all CPUs.");
58 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
59 torture_param(int, onoff_interval, 0, "Time between CPU hotplugs (s), 0=disable");
60 torture_param(int, shutdown_secs, 0, "Shutdown time (ms), <= zero to disable.");
61 torture_param(int, stat_interval, 60, "Number of seconds between stats printk()s.");
62 torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
63 torture_param(bool, use_cpus_read_lock, 0, "Use cpus_read_lock() to exclude CPU hotplug.");
64 torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
65 torture_param(int, weight_resched, -1, "Testing weight for resched_cpu() operations.");
66 torture_param(int, weight_single, -1, "Testing weight for single-CPU no-wait operations.");
67 torture_param(int, weight_single_rpc, -1, "Testing weight for single-CPU RPC operations.");
68 torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations.");
69 torture_param(int, weight_many, -1, "Testing weight for multi-CPU no-wait operations.");
70 torture_param(int, weight_many_wait, -1, "Testing weight for multi-CPU operations.");
71 torture_param(int, weight_all, -1, "Testing weight for all-CPU no-wait operations.");
72 torture_param(int, weight_all_wait, -1, "Testing weight for all-CPU operations.");
73
74 char *torture_type = "";
75
76 #ifdef MODULE
77 # define SCFTORT_SHUTDOWN 0
78 #else
79 # define SCFTORT_SHUTDOWN 1
80 #endif
81
82 torture_param(bool, shutdown, SCFTORT_SHUTDOWN, "Shutdown at end of torture test.");
83
84 struct scf_statistics {
85         struct task_struct *task;
86         int cpu;
87         long long n_resched;
88         long long n_single;
89         long long n_single_ofl;
90         long long n_single_rpc;
91         long long n_single_rpc_ofl;
92         long long n_single_wait;
93         long long n_single_wait_ofl;
94         long long n_many;
95         long long n_many_wait;
96         long long n_all;
97         long long n_all_wait;
98 };
99
100 static struct scf_statistics *scf_stats_p;
101 static struct task_struct *scf_torture_stats_task;
102 static DEFINE_PER_CPU(long long, scf_invoked_count);
103
104 // Data for random primitive selection
105 #define SCF_PRIM_RESCHED        0
106 #define SCF_PRIM_SINGLE         1
107 #define SCF_PRIM_SINGLE_RPC     2
108 #define SCF_PRIM_MANY           3
109 #define SCF_PRIM_ALL            4
110 #define SCF_NPRIMS              8 // Need wait and no-wait versions of each,
111                                   //  except for SCF_PRIM_RESCHED and
112                                   //  SCF_PRIM_SINGLE_RPC.
113
114 static char *scf_prim_name[] = {
115         "resched_cpu",
116         "smp_call_function_single",
117         "smp_call_function_single_rpc",
118         "smp_call_function_many",
119         "smp_call_function",
120 };
121
122 struct scf_selector {
123         unsigned long scfs_weight;
124         int scfs_prim;
125         bool scfs_wait;
126 };
127 static struct scf_selector scf_sel_array[SCF_NPRIMS];
128 static int scf_sel_array_len;
129 static unsigned long scf_sel_totweight;
130
131 // Communicate between caller and handler.
132 struct scf_check {
133         bool scfc_in;
134         bool scfc_out;
135         int scfc_cpu; // -1 for not _single().
136         bool scfc_wait;
137         bool scfc_rpc;
138         struct completion scfc_completion;
139 };
140
141 // Use to wait for all threads to start.
142 static atomic_t n_started;
143 static atomic_t n_errs;
144 static atomic_t n_mb_in_errs;
145 static atomic_t n_mb_out_errs;
146 static atomic_t n_alloc_errs;
147 static bool scfdone;
148 static char *bangstr = "";
149
150 static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
151
152 extern void resched_cpu(int cpu); // An alternative IPI vector.
153
154 // Print torture statistics.  Caller must ensure serialization.
155 static void scf_torture_stats_print(void)
156 {
157         int cpu;
158         int i;
159         long long invoked_count = 0;
160         bool isdone = READ_ONCE(scfdone);
161         struct scf_statistics scfs = {};
162
163         for_each_possible_cpu(cpu)
164                 invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
165         for (i = 0; i < nthreads; i++) {
166                 scfs.n_resched += scf_stats_p[i].n_resched;
167                 scfs.n_single += scf_stats_p[i].n_single;
168                 scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
169                 scfs.n_single_rpc += scf_stats_p[i].n_single_rpc;
170                 scfs.n_single_wait += scf_stats_p[i].n_single_wait;
171                 scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl;
172                 scfs.n_many += scf_stats_p[i].n_many;
173                 scfs.n_many_wait += scf_stats_p[i].n_many_wait;
174                 scfs.n_all += scf_stats_p[i].n_all;
175                 scfs.n_all_wait += scf_stats_p[i].n_all_wait;
176         }
177         if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) ||
178             atomic_read(&n_mb_out_errs) ||
179             (!IS_ENABLED(CONFIG_KASAN) && atomic_read(&n_alloc_errs)))
180                 bangstr = "!!! ";
181         pr_alert("%s %sscf_invoked_count %s: %lld resched: %lld single: %lld/%lld single_ofl: %lld/%lld single_rpc: %lld single_rpc_ofl: %lld many: %lld/%lld all: %lld/%lld ",
182                  SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count, scfs.n_resched,
183                  scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
184                  scfs.n_single_rpc, scfs.n_single_rpc_ofl,
185                  scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
186         torture_onoff_stats();
187         pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs),
188                 atomic_read(&n_mb_in_errs), atomic_read(&n_mb_out_errs),
189                 atomic_read(&n_alloc_errs));
190 }
191
192 // Periodically prints torture statistics, if periodic statistics printing
193 // was specified via the stat_interval module parameter.
194 static int
195 scf_torture_stats(void *arg)
196 {
197         VERBOSE_TOROUT_STRING("scf_torture_stats task started");
198         do {
199                 schedule_timeout_interruptible(stat_interval * HZ);
200                 scf_torture_stats_print();
201                 torture_shutdown_absorb("scf_torture_stats");
202         } while (!torture_must_stop());
203         torture_kthread_stopping("scf_torture_stats");
204         return 0;
205 }
206
207 // Add a primitive to the scf_sel_array[].
208 static void scf_sel_add(unsigned long weight, int prim, bool wait)
209 {
210         struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len];
211
212         // If no weight, if array would overflow, if computing three-place
213         // percentages would overflow, or if the scf_prim_name[] array would
214         // overflow, don't bother.  In the last three two cases, complain.
215         if (!weight ||
216             WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) ||
217             WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) ||
218             WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name)))
219                 return;
220         scf_sel_totweight += weight;
221         scfsp->scfs_weight = scf_sel_totweight;
222         scfsp->scfs_prim = prim;
223         scfsp->scfs_wait = wait;
224         scf_sel_array_len++;
225 }
226
227 // Dump out weighting percentages for scf_prim_name[] array.
228 static void scf_sel_dump(void)
229 {
230         int i;
231         unsigned long oldw = 0;
232         struct scf_selector *scfsp;
233         unsigned long w;
234
235         for (i = 0; i < scf_sel_array_len; i++) {
236                 scfsp = &scf_sel_array[i];
237                 w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight;
238                 pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000,
239                         scf_prim_name[scfsp->scfs_prim],
240                         scfsp->scfs_wait ? "wait" : "nowait");
241                 oldw = scfsp->scfs_weight;
242         }
243 }
244
245 // Randomly pick a primitive and wait/nowait, based on weightings.
246 static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp)
247 {
248         int i;
249         unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1);
250
251         for (i = 0; i < scf_sel_array_len; i++)
252                 if (scf_sel_array[i].scfs_weight >= w)
253                         return &scf_sel_array[i];
254         WARN_ON_ONCE(1);
255         return &scf_sel_array[0];
256 }
257
258 // Update statistics and occasionally burn up mass quantities of CPU time,
259 // if told to do so via scftorture.longwait.  Otherwise, occasionally burn
260 // a little bit.
261 static void scf_handler(void *scfc_in)
262 {
263         int i;
264         int j;
265         unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
266         struct scf_check *scfcp = scfc_in;
267
268         if (likely(scfcp)) {
269                 WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers.
270                 if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in))))
271                         atomic_inc(&n_mb_in_errs);
272         }
273         this_cpu_inc(scf_invoked_count);
274         if (longwait <= 0) {
275                 if (!(r & 0xffc0)) {
276                         udelay(r & 0x3f);
277                         goto out;
278                 }
279         }
280         if (r & 0xfff)
281                 goto out;
282         r = (r >> 12);
283         if (longwait <= 0) {
284                 udelay((r & 0xff) + 1);
285                 goto out;
286         }
287         r = r % longwait + 1;
288         for (i = 0; i < r; i++) {
289                 for (j = 0; j < 1000; j++) {
290                         udelay(1000);
291                         cpu_relax();
292                 }
293         }
294 out:
295         if (unlikely(!scfcp))
296                 return;
297         if (scfcp->scfc_wait) {
298                 WRITE_ONCE(scfcp->scfc_out, true);
299                 if (scfcp->scfc_rpc)
300                         complete(&scfcp->scfc_completion);
301         } else {
302                 kfree(scfcp);
303         }
304 }
305
306 // As above, but check for correct CPU.
307 static void scf_handler_1(void *scfc_in)
308 {
309         struct scf_check *scfcp = scfc_in;
310
311         if (likely(scfcp) && WARN_ONCE(smp_processor_id() != scfcp->scfc_cpu, "%s: Wanted CPU %d got CPU %d\n", __func__, scfcp->scfc_cpu, smp_processor_id())) {
312                 atomic_inc(&n_errs);
313         }
314         scf_handler(scfcp);
315 }
316
317 // Randomly do an smp_call_function*() invocation.
318 static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
319 {
320         uintptr_t cpu;
321         int ret = 0;
322         struct scf_check *scfcp = NULL;
323         struct scf_selector *scfsp = scf_sel_rand(trsp);
324
325         if (use_cpus_read_lock)
326                 cpus_read_lock();
327         else
328                 preempt_disable();
329         if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
330                 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
331                 if (!scfcp) {
332                         WARN_ON_ONCE(!IS_ENABLED(CONFIG_KASAN));
333                         atomic_inc(&n_alloc_errs);
334                 } else {
335                         scfcp->scfc_cpu = -1;
336                         scfcp->scfc_wait = scfsp->scfs_wait;
337                         scfcp->scfc_out = false;
338                         scfcp->scfc_rpc = false;
339                 }
340         }
341         switch (scfsp->scfs_prim) {
342         case SCF_PRIM_RESCHED:
343                 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) {
344                         cpu = torture_random(trsp) % nr_cpu_ids;
345                         scfp->n_resched++;
346                         resched_cpu(cpu);
347                 }
348                 break;
349         case SCF_PRIM_SINGLE:
350                 cpu = torture_random(trsp) % nr_cpu_ids;
351                 if (scfsp->scfs_wait)
352                         scfp->n_single_wait++;
353                 else
354                         scfp->n_single++;
355                 if (scfcp) {
356                         scfcp->scfc_cpu = cpu;
357                         barrier(); // Prevent race-reduction compiler optimizations.
358                         scfcp->scfc_in = true;
359                 }
360                 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
361                 if (ret) {
362                         if (scfsp->scfs_wait)
363                                 scfp->n_single_wait_ofl++;
364                         else
365                                 scfp->n_single_ofl++;
366                         kfree(scfcp);
367                         scfcp = NULL;
368                 }
369                 break;
370         case SCF_PRIM_SINGLE_RPC:
371                 if (!scfcp)
372                         break;
373                 cpu = torture_random(trsp) % nr_cpu_ids;
374                 scfp->n_single_rpc++;
375                 scfcp->scfc_cpu = cpu;
376                 scfcp->scfc_wait = true;
377                 init_completion(&scfcp->scfc_completion);
378                 scfcp->scfc_rpc = true;
379                 barrier(); // Prevent race-reduction compiler optimizations.
380                 scfcp->scfc_in = true;
381                 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, 0);
382                 if (!ret) {
383                         if (use_cpus_read_lock)
384                                 cpus_read_unlock();
385                         else
386                                 preempt_enable();
387                         wait_for_completion(&scfcp->scfc_completion);
388                         if (use_cpus_read_lock)
389                                 cpus_read_lock();
390                         else
391                                 preempt_disable();
392                 } else {
393                         scfp->n_single_rpc_ofl++;
394                         kfree(scfcp);
395                         scfcp = NULL;
396                 }
397                 break;
398         case SCF_PRIM_MANY:
399                 if (scfsp->scfs_wait)
400                         scfp->n_many_wait++;
401                 else
402                         scfp->n_many++;
403                 if (scfcp) {
404                         barrier(); // Prevent race-reduction compiler optimizations.
405                         scfcp->scfc_in = true;
406                 }
407                 smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
408                 break;
409         case SCF_PRIM_ALL:
410                 if (scfsp->scfs_wait)
411                         scfp->n_all_wait++;
412                 else
413                         scfp->n_all++;
414                 if (scfcp) {
415                         barrier(); // Prevent race-reduction compiler optimizations.
416                         scfcp->scfc_in = true;
417                 }
418                 smp_call_function(scf_handler, scfcp, scfsp->scfs_wait);
419                 break;
420         default:
421                 WARN_ON_ONCE(1);
422                 if (scfcp)
423                         scfcp->scfc_out = true;
424         }
425         if (scfcp && scfsp->scfs_wait) {
426                 if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp->scfs_prim == SCF_PRIM_SINGLE) &&
427                                  !scfcp->scfc_out)) {
428                         pr_warn("%s: Memory-ordering failure, scfs_prim: %d.\n", __func__, scfsp->scfs_prim);
429                         atomic_inc(&n_mb_out_errs); // Leak rather than trash!
430                 } else {
431                         kfree(scfcp);
432                 }
433                 barrier(); // Prevent race-reduction compiler optimizations.
434         }
435         if (use_cpus_read_lock)
436                 cpus_read_unlock();
437         else
438                 preempt_enable();
439         if (!(torture_random(trsp) & 0xfff))
440                 schedule_timeout_uninterruptible(1);
441 }
442
443 // SCF test kthread.  Repeatedly does calls to members of the
444 // smp_call_function() family of functions.
445 static int scftorture_invoker(void *arg)
446 {
447         int cpu;
448         int curcpu;
449         DEFINE_TORTURE_RANDOM(rand);
450         struct scf_statistics *scfp = (struct scf_statistics *)arg;
451         bool was_offline = false;
452
453         VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
454         cpu = scfp->cpu % nr_cpu_ids;
455         WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(cpu)));
456         set_user_nice(current, MAX_NICE);
457         if (holdoff)
458                 schedule_timeout_interruptible(holdoff * HZ);
459
460         VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp->cpu, raw_smp_processor_id());
461
462         // Make sure that the CPU is affinitized appropriately during testing.
463         curcpu = raw_smp_processor_id();
464         WARN_ONCE(curcpu != scfp->cpu % nr_cpu_ids,
465                   "%s: Wanted CPU %d, running on %d, nr_cpu_ids = %d\n",
466                   __func__, scfp->cpu, curcpu, nr_cpu_ids);
467
468         if (!atomic_dec_return(&n_started))
469                 while (atomic_read_acquire(&n_started)) {
470                         if (torture_must_stop()) {
471                                 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
472                                 goto end;
473                         }
474                         schedule_timeout_uninterruptible(1);
475                 }
476
477         VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
478
479         do {
480                 scftorture_invoke_one(scfp, &rand);
481                 while (cpu_is_offline(cpu) && !torture_must_stop()) {
482                         schedule_timeout_interruptible(HZ / 5);
483                         was_offline = true;
484                 }
485                 if (was_offline) {
486                         set_cpus_allowed_ptr(current, cpumask_of(cpu));
487                         was_offline = false;
488                 }
489                 cond_resched();
490                 stutter_wait("scftorture_invoker");
491         } while (!torture_must_stop());
492
493         VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
494 end:
495         torture_kthread_stopping("scftorture_invoker");
496         return 0;
497 }
498
499 static void
500 scftorture_print_module_parms(const char *tag)
501 {
502         pr_alert(SCFTORT_FLAG
503                  "--- %s:  verbose=%d holdoff=%d longwait=%d nthreads=%d onoff_holdoff=%d onoff_interval=%d shutdown_secs=%d stat_interval=%d stutter=%d use_cpus_read_lock=%d, weight_resched=%d, weight_single=%d, weight_single_rpc=%d, weight_single_wait=%d, weight_many=%d, weight_many_wait=%d, weight_all=%d, weight_all_wait=%d\n", tag,
504                  verbose, holdoff, longwait, nthreads, onoff_holdoff, onoff_interval, shutdown, stat_interval, stutter, use_cpus_read_lock, weight_resched, weight_single, weight_single_rpc, weight_single_wait, weight_many, weight_many_wait, weight_all, weight_all_wait);
505 }
506
507 static void scf_cleanup_handler(void *unused)
508 {
509 }
510
511 static void scf_torture_cleanup(void)
512 {
513         int i;
514
515         if (torture_cleanup_begin())
516                 return;
517
518         WRITE_ONCE(scfdone, true);
519         if (nthreads && scf_stats_p)
520                 for (i = 0; i < nthreads; i++)
521                         torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
522         else
523                 goto end;
524         smp_call_function(scf_cleanup_handler, NULL, 0);
525         torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
526         scf_torture_stats_print();  // -After- the stats thread is stopped!
527         kfree(scf_stats_p);  // -After- the last stats print has completed!
528         scf_stats_p = NULL;
529
530         if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) || atomic_read(&n_mb_out_errs))
531                 scftorture_print_module_parms("End of test: FAILURE");
532         else if (torture_onoff_failures())
533                 scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
534         else
535                 scftorture_print_module_parms("End of test: SUCCESS");
536
537 end:
538         torture_cleanup_end();
539 }
540
541 static int __init scf_torture_init(void)
542 {
543         long i;
544         int firsterr = 0;
545         unsigned long weight_resched1 = weight_resched;
546         unsigned long weight_single1 = weight_single;
547         unsigned long weight_single_rpc1 = weight_single_rpc;
548         unsigned long weight_single_wait1 = weight_single_wait;
549         unsigned long weight_many1 = weight_many;
550         unsigned long weight_many_wait1 = weight_many_wait;
551         unsigned long weight_all1 = weight_all;
552         unsigned long weight_all_wait1 = weight_all_wait;
553
554         if (!torture_init_begin(SCFTORT_STRING, verbose))
555                 return -EBUSY;
556
557         scftorture_print_module_parms("Start of test");
558
559         if (weight_resched == -1 &&
560             weight_single == -1 && weight_single_rpc == -1 && weight_single_wait == -1 &&
561             weight_many == -1 && weight_many_wait == -1 &&
562             weight_all == -1 && weight_all_wait == -1) {
563                 weight_resched1 = 2 * nr_cpu_ids;
564                 weight_single1 = 2 * nr_cpu_ids;
565                 weight_single_rpc1 = 2 * nr_cpu_ids;
566                 weight_single_wait1 = 2 * nr_cpu_ids;
567                 weight_many1 = 2;
568                 weight_many_wait1 = 2;
569                 weight_all1 = 1;
570                 weight_all_wait1 = 1;
571         } else {
572                 if (weight_resched == -1)
573                         weight_resched1 = 0;
574                 if (weight_single == -1)
575                         weight_single1 = 0;
576                 if (weight_single_rpc == -1)
577                         weight_single_rpc1 = 0;
578                 if (weight_single_wait == -1)
579                         weight_single_wait1 = 0;
580                 if (weight_many == -1)
581                         weight_many1 = 0;
582                 if (weight_many_wait == -1)
583                         weight_many_wait1 = 0;
584                 if (weight_all == -1)
585                         weight_all1 = 0;
586                 if (weight_all_wait == -1)
587                         weight_all_wait1 = 0;
588         }
589         if (weight_single1 == 0 && weight_single_rpc1 == 0 && weight_single_wait1 == 0 &&
590             weight_many1 == 0 && weight_many_wait1 == 0 &&
591             weight_all1 == 0 && weight_all_wait1 == 0) {
592                 VERBOSE_SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
593                 firsterr = -EINVAL;
594                 goto unwind;
595         }
596         if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST))
597                 scf_sel_add(weight_resched1, SCF_PRIM_RESCHED, false);
598         else if (weight_resched1)
599                 VERBOSE_SCFTORTOUT_ERRSTRING("built as module, weight_resched ignored");
600         scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
601         scf_sel_add(weight_single_rpc1, SCF_PRIM_SINGLE_RPC, true);
602         scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
603         scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
604         scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
605         scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
606         scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
607         scf_sel_dump();
608
609         if (onoff_interval > 0) {
610                 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
611                 if (firsterr)
612                         goto unwind;
613         }
614         if (shutdown_secs > 0) {
615                 firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
616                 if (firsterr)
617                         goto unwind;
618         }
619         if (stutter > 0) {
620                 firsterr = torture_stutter_init(stutter, stutter);
621                 if (firsterr)
622                         goto unwind;
623         }
624
625         // Worker tasks invoking smp_call_function().
626         if (nthreads < 0)
627                 nthreads = num_online_cpus();
628         scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL);
629         if (!scf_stats_p) {
630                 VERBOSE_SCFTORTOUT_ERRSTRING("out of memory");
631                 firsterr = -ENOMEM;
632                 goto unwind;
633         }
634
635         VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads\n", nthreads);
636
637         atomic_set(&n_started, nthreads);
638         for (i = 0; i < nthreads; i++) {
639                 scf_stats_p[i].cpu = i;
640                 firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
641                                                   scf_stats_p[i].task);
642                 if (firsterr)
643                         goto unwind;
644         }
645         if (stat_interval > 0) {
646                 firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
647                 if (firsterr)
648                         goto unwind;
649         }
650
651         torture_init_end();
652         return 0;
653
654 unwind:
655         torture_init_end();
656         scf_torture_cleanup();
657         return firsterr;
658 }
659
660 module_init(scf_torture_init);
661 module_exit(scf_torture_cleanup);