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