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