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