1 // SPDX-License-Identifier: GPL-2.0+
3 // Torture test for smp_call_function() and friends.
5 // Copyright (C) Facebook, 2020.
7 // Author: Paul E. McKenney <paulmck@kernel.org>
9 #define pr_fmt(fmt) fmt
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>
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>
38 #define SCFTORT_STRING "scftorture"
39 #define SCFTORT_FLAG SCFTORT_STRING ": "
41 #define SCFTORTOUT(s, x...) \
42 pr_alert(SCFTORT_FLAG s, ## x)
44 #define VERBOSE_SCFTORTOUT(s, x...) \
45 do { if (verbose) pr_alert(SCFTORT_FLAG s, ## x); } while (0)
47 #define VERBOSE_SCFTORTOUT_ERRSTRING(s, x...) \
48 do { if (verbose) pr_alert(SCFTORT_FLAG "!!! " s, ## x); } while (0)
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Paul E. McKenney <paulmck@kernel.org>");
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_wait
, -1, "Testing weight for single-CPU operations.");
68 torture_param(int, weight_many
, -1, "Testing weight for multi-CPU no-wait operations.");
69 torture_param(int, weight_many_wait
, -1, "Testing weight for multi-CPU operations.");
70 torture_param(int, weight_all
, -1, "Testing weight for all-CPU no-wait operations.");
71 torture_param(int, weight_all_wait
, -1, "Testing weight for all-CPU operations.");
73 char *torture_type
= "";
76 # define SCFTORT_SHUTDOWN 0
78 # define SCFTORT_SHUTDOWN 1
81 torture_param(bool, shutdown
, SCFTORT_SHUTDOWN
, "Shutdown at end of torture test.");
83 struct scf_statistics
{
84 struct task_struct
*task
;
88 long long n_single_ofl
;
89 long long n_single_wait
;
90 long long n_single_wait_ofl
;
92 long long n_many_wait
;
97 static struct scf_statistics
*scf_stats_p
;
98 static struct task_struct
*scf_torture_stats_task
;
99 static DEFINE_PER_CPU(long long, scf_invoked_count
);
101 // Data for random primitive selection
102 #define SCF_PRIM_RESCHED 0
103 #define SCF_PRIM_SINGLE 1
104 #define SCF_PRIM_MANY 2
105 #define SCF_PRIM_ALL 3
106 #define SCF_NPRIMS 7 // Need wait and no-wait versions of each,
107 // except for SCF_PRIM_RESCHED.
109 static char *scf_prim_name
[] = {
111 "smp_call_function_single",
112 "smp_call_function_many",
116 struct scf_selector
{
117 unsigned long scfs_weight
;
121 static struct scf_selector scf_sel_array
[SCF_NPRIMS
];
122 static int scf_sel_array_len
;
123 static unsigned long scf_sel_totweight
;
125 // Communicate between caller and handler.
129 int scfc_cpu
; // -1 for not _single().
133 // Use to wait for all threads to start.
134 static atomic_t n_started
;
135 static atomic_t n_errs
;
136 static atomic_t n_mb_in_errs
;
137 static atomic_t n_mb_out_errs
;
138 static atomic_t n_alloc_errs
;
140 static char *bangstr
= "";
142 static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand
);
144 extern void resched_cpu(int cpu
); // An alternative IPI vector.
146 // Print torture statistics. Caller must ensure serialization.
147 static void scf_torture_stats_print(void)
151 long long invoked_count
= 0;
152 bool isdone
= READ_ONCE(scfdone
);
153 struct scf_statistics scfs
= {};
155 for_each_possible_cpu(cpu
)
156 invoked_count
+= data_race(per_cpu(scf_invoked_count
, cpu
));
157 for (i
= 0; i
< nthreads
; i
++) {
158 scfs
.n_resched
+= scf_stats_p
[i
].n_resched
;
159 scfs
.n_single
+= scf_stats_p
[i
].n_single
;
160 scfs
.n_single_ofl
+= scf_stats_p
[i
].n_single_ofl
;
161 scfs
.n_single_wait
+= scf_stats_p
[i
].n_single_wait
;
162 scfs
.n_single_wait_ofl
+= scf_stats_p
[i
].n_single_wait_ofl
;
163 scfs
.n_many
+= scf_stats_p
[i
].n_many
;
164 scfs
.n_many_wait
+= scf_stats_p
[i
].n_many_wait
;
165 scfs
.n_all
+= scf_stats_p
[i
].n_all
;
166 scfs
.n_all_wait
+= scf_stats_p
[i
].n_all_wait
;
168 if (atomic_read(&n_errs
) || atomic_read(&n_mb_in_errs
) ||
169 atomic_read(&n_mb_out_errs
) || atomic_read(&n_alloc_errs
))
171 pr_alert("%s %sscf_invoked_count %s: %lld resched: %lld single: %lld/%lld single_ofl: %lld/%lld many: %lld/%lld all: %lld/%lld ",
172 SCFTORT_FLAG
, bangstr
, isdone
? "VER" : "ver", invoked_count
, scfs
.n_resched
,
173 scfs
.n_single
, scfs
.n_single_wait
, scfs
.n_single_ofl
, scfs
.n_single_wait_ofl
,
174 scfs
.n_many
, scfs
.n_many_wait
, scfs
.n_all
, scfs
.n_all_wait
);
175 torture_onoff_stats();
176 pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs
),
177 atomic_read(&n_mb_in_errs
), atomic_read(&n_mb_out_errs
),
178 atomic_read(&n_alloc_errs
));
181 // Periodically prints torture statistics, if periodic statistics printing
182 // was specified via the stat_interval module parameter.
184 scf_torture_stats(void *arg
)
186 VERBOSE_TOROUT_STRING("scf_torture_stats task started");
188 schedule_timeout_interruptible(stat_interval
* HZ
);
189 scf_torture_stats_print();
190 torture_shutdown_absorb("scf_torture_stats");
191 } while (!torture_must_stop());
192 torture_kthread_stopping("scf_torture_stats");
196 // Add a primitive to the scf_sel_array[].
197 static void scf_sel_add(unsigned long weight
, int prim
, bool wait
)
199 struct scf_selector
*scfsp
= &scf_sel_array
[scf_sel_array_len
];
201 // If no weight, if array would overflow, if computing three-place
202 // percentages would overflow, or if the scf_prim_name[] array would
203 // overflow, don't bother. In the last three two cases, complain.
205 WARN_ON_ONCE(scf_sel_array_len
>= ARRAY_SIZE(scf_sel_array
)) ||
206 WARN_ON_ONCE(0 - 100000 * weight
<= 100000 * scf_sel_totweight
) ||
207 WARN_ON_ONCE(prim
>= ARRAY_SIZE(scf_prim_name
)))
209 scf_sel_totweight
+= weight
;
210 scfsp
->scfs_weight
= scf_sel_totweight
;
211 scfsp
->scfs_prim
= prim
;
212 scfsp
->scfs_wait
= wait
;
216 // Dump out weighting percentages for scf_prim_name[] array.
217 static void scf_sel_dump(void)
220 unsigned long oldw
= 0;
221 struct scf_selector
*scfsp
;
224 for (i
= 0; i
< scf_sel_array_len
; i
++) {
225 scfsp
= &scf_sel_array
[i
];
226 w
= (scfsp
->scfs_weight
- oldw
) * 100000 / scf_sel_totweight
;
227 pr_info("%s: %3lu.%03lu %s(%s)\n", __func__
, w
/ 1000, w
% 1000,
228 scf_prim_name
[scfsp
->scfs_prim
],
229 scfsp
->scfs_wait
? "wait" : "nowait");
230 oldw
= scfsp
->scfs_weight
;
234 // Randomly pick a primitive and wait/nowait, based on weightings.
235 static struct scf_selector
*scf_sel_rand(struct torture_random_state
*trsp
)
238 unsigned long w
= torture_random(trsp
) % (scf_sel_totweight
+ 1);
240 for (i
= 0; i
< scf_sel_array_len
; i
++)
241 if (scf_sel_array
[i
].scfs_weight
>= w
)
242 return &scf_sel_array
[i
];
244 return &scf_sel_array
[0];
247 // Update statistics and occasionally burn up mass quantities of CPU time,
248 // if told to do so via scftorture.longwait. Otherwise, occasionally burn
250 static void scf_handler(void *scfc_in
)
254 unsigned long r
= torture_random(this_cpu_ptr(&scf_torture_rand
));
255 struct scf_check
*scfcp
= scfc_in
;
258 WRITE_ONCE(scfcp
->scfc_out
, false); // For multiple receivers.
259 if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp
->scfc_in
))))
260 atomic_inc(&n_mb_in_errs
);
262 this_cpu_inc(scf_invoked_count
);
272 udelay((r
& 0xff) + 1);
275 r
= r
% longwait
+ 1;
276 for (i
= 0; i
< r
; i
++) {
277 for (j
= 0; j
< 1000; j
++) {
283 if (unlikely(!scfcp
))
285 if (scfcp
->scfc_wait
)
286 WRITE_ONCE(scfcp
->scfc_out
, true);
291 // As above, but check for correct CPU.
292 static void scf_handler_1(void *scfc_in
)
294 struct scf_check
*scfcp
= scfc_in
;
296 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())) {
302 // Randomly do an smp_call_function*() invocation.
303 static void scftorture_invoke_one(struct scf_statistics
*scfp
, struct torture_random_state
*trsp
)
307 struct scf_check
*scfcp
= NULL
;
308 struct scf_selector
*scfsp
= scf_sel_rand(trsp
);
310 if (use_cpus_read_lock
)
314 if (scfsp
->scfs_prim
== SCF_PRIM_SINGLE
|| scfsp
->scfs_wait
) {
315 scfcp
= kmalloc(sizeof(*scfcp
), GFP_ATOMIC
);
316 if (WARN_ON_ONCE(!scfcp
)) {
317 atomic_inc(&n_alloc_errs
);
319 scfcp
->scfc_cpu
= -1;
320 scfcp
->scfc_wait
= scfsp
->scfs_wait
;
321 scfcp
->scfc_out
= false;
324 switch (scfsp
->scfs_prim
) {
325 case SCF_PRIM_RESCHED
:
326 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST
)) {
327 cpu
= torture_random(trsp
) % nr_cpu_ids
;
332 case SCF_PRIM_SINGLE
:
333 cpu
= torture_random(trsp
) % nr_cpu_ids
;
334 if (scfsp
->scfs_wait
)
335 scfp
->n_single_wait
++;
339 scfcp
->scfc_cpu
= cpu
;
340 barrier(); // Prevent race-reduction compiler optimizations.
341 scfcp
->scfc_in
= true;
343 ret
= smp_call_function_single(cpu
, scf_handler_1
, (void *)scfcp
, scfsp
->scfs_wait
);
345 if (scfsp
->scfs_wait
)
346 scfp
->n_single_wait_ofl
++;
348 scfp
->n_single_ofl
++;
354 if (scfsp
->scfs_wait
)
359 barrier(); // Prevent race-reduction compiler optimizations.
360 scfcp
->scfc_in
= true;
362 smp_call_function_many(cpu_online_mask
, scf_handler
, scfcp
, scfsp
->scfs_wait
);
365 if (scfsp
->scfs_wait
)
370 barrier(); // Prevent race-reduction compiler optimizations.
371 scfcp
->scfc_in
= true;
373 smp_call_function(scf_handler
, scfcp
, scfsp
->scfs_wait
);
378 scfcp
->scfc_out
= true;
380 if (scfcp
&& scfsp
->scfs_wait
) {
381 if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp
->scfs_prim
== SCF_PRIM_SINGLE
) &&
383 atomic_inc(&n_mb_out_errs
); // Leak rather than trash!
386 barrier(); // Prevent race-reduction compiler optimizations.
388 if (use_cpus_read_lock
)
392 if (!(torture_random(trsp
) & 0xfff))
393 schedule_timeout_uninterruptible(1);
396 // SCF test kthread. Repeatedly does calls to members of the
397 // smp_call_function() family of functions.
398 static int scftorture_invoker(void *arg
)
401 DEFINE_TORTURE_RANDOM(rand
);
402 struct scf_statistics
*scfp
= (struct scf_statistics
*)arg
;
403 bool was_offline
= false;
405 VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp
->cpu
);
406 cpu
= scfp
->cpu
% nr_cpu_ids
;
407 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
408 set_user_nice(current
, MAX_NICE
);
410 schedule_timeout_interruptible(holdoff
* HZ
);
412 VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp
->cpu
, smp_processor_id());
414 // Make sure that the CPU is affinitized appropriately during testing.
415 WARN_ON_ONCE(smp_processor_id() != scfp
->cpu
);
417 if (!atomic_dec_return(&n_started
))
418 while (atomic_read_acquire(&n_started
)) {
419 if (torture_must_stop()) {
420 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp
->cpu
);
423 schedule_timeout_uninterruptible(1);
426 VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp
->cpu
);
429 scftorture_invoke_one(scfp
, &rand
);
430 while (cpu_is_offline(cpu
) && !torture_must_stop()) {
431 schedule_timeout_interruptible(HZ
/ 5);
435 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
439 stutter_wait("scftorture_invoker");
440 } while (!torture_must_stop());
442 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp
->cpu
);
444 torture_kthread_stopping("scftorture_invoker");
449 scftorture_print_module_parms(const char *tag
)
451 pr_alert(SCFTORT_FLAG
452 "--- %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_wait=%d, weight_many=%d, weight_many_wait=%d, weight_all=%d, weight_all_wait=%d\n", tag
,
453 verbose
, holdoff
, longwait
, nthreads
, onoff_holdoff
, onoff_interval
, shutdown
, stat_interval
, stutter
, use_cpus_read_lock
, weight_resched
, weight_single
, weight_single_wait
, weight_many
, weight_many_wait
, weight_all
, weight_all_wait
);
456 static void scf_cleanup_handler(void *unused
)
460 static void scf_torture_cleanup(void)
464 if (torture_cleanup_begin())
467 WRITE_ONCE(scfdone
, true);
469 for (i
= 0; i
< nthreads
; i
++)
470 torture_stop_kthread("scftorture_invoker", scf_stats_p
[i
].task
);
473 smp_call_function(scf_cleanup_handler
, NULL
, 0);
474 torture_stop_kthread(scf_torture_stats
, scf_torture_stats_task
);
475 scf_torture_stats_print(); // -After- the stats thread is stopped!
476 kfree(scf_stats_p
); // -After- the last stats print has completed!
479 if (atomic_read(&n_errs
) || atomic_read(&n_mb_in_errs
) || atomic_read(&n_mb_out_errs
))
480 scftorture_print_module_parms("End of test: FAILURE");
481 else if (torture_onoff_failures())
482 scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
484 scftorture_print_module_parms("End of test: SUCCESS");
487 torture_cleanup_end();
490 static int __init
scf_torture_init(void)
494 unsigned long weight_resched1
= weight_resched
;
495 unsigned long weight_single1
= weight_single
;
496 unsigned long weight_single_wait1
= weight_single_wait
;
497 unsigned long weight_many1
= weight_many
;
498 unsigned long weight_many_wait1
= weight_many_wait
;
499 unsigned long weight_all1
= weight_all
;
500 unsigned long weight_all_wait1
= weight_all_wait
;
502 if (!torture_init_begin(SCFTORT_STRING
, verbose
))
505 scftorture_print_module_parms("Start of test");
507 if (weight_resched
== -1 && weight_single
== -1 && weight_single_wait
== -1 &&
508 weight_many
== -1 && weight_many_wait
== -1 &&
509 weight_all
== -1 && weight_all_wait
== -1) {
510 weight_resched1
= 2 * nr_cpu_ids
;
511 weight_single1
= 2 * nr_cpu_ids
;
512 weight_single_wait1
= 2 * nr_cpu_ids
;
514 weight_many_wait1
= 2;
516 weight_all_wait1
= 1;
518 if (weight_resched
== -1)
520 if (weight_single
== -1)
522 if (weight_single_wait
== -1)
523 weight_single_wait1
= 0;
524 if (weight_many
== -1)
526 if (weight_many_wait
== -1)
527 weight_many_wait1
= 0;
528 if (weight_all
== -1)
530 if (weight_all_wait
== -1)
531 weight_all_wait1
= 0;
533 if (weight_single1
== 0 && weight_single_wait1
== 0 &&
534 weight_many1
== 0 && weight_many_wait1
== 0 &&
535 weight_all1
== 0 && weight_all_wait1
== 0) {
536 VERBOSE_SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
540 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST
))
541 scf_sel_add(weight_resched1
, SCF_PRIM_RESCHED
, false);
542 else if (weight_resched1
)
543 VERBOSE_SCFTORTOUT_ERRSTRING("built as module, weight_resched ignored");
544 scf_sel_add(weight_single1
, SCF_PRIM_SINGLE
, false);
545 scf_sel_add(weight_single_wait1
, SCF_PRIM_SINGLE
, true);
546 scf_sel_add(weight_many1
, SCF_PRIM_MANY
, false);
547 scf_sel_add(weight_many_wait1
, SCF_PRIM_MANY
, true);
548 scf_sel_add(weight_all1
, SCF_PRIM_ALL
, false);
549 scf_sel_add(weight_all_wait1
, SCF_PRIM_ALL
, true);
552 if (onoff_interval
> 0) {
553 firsterr
= torture_onoff_init(onoff_holdoff
* HZ
, onoff_interval
, NULL
);
557 if (shutdown_secs
> 0) {
558 firsterr
= torture_shutdown_init(shutdown_secs
, scf_torture_cleanup
);
563 firsterr
= torture_stutter_init(stutter
, stutter
);
568 // Worker tasks invoking smp_call_function().
570 nthreads
= num_online_cpus();
571 scf_stats_p
= kcalloc(nthreads
, sizeof(scf_stats_p
[0]), GFP_KERNEL
);
573 VERBOSE_SCFTORTOUT_ERRSTRING("out of memory");
578 VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads\n", nthreads
);
580 atomic_set(&n_started
, nthreads
);
581 for (i
= 0; i
< nthreads
; i
++) {
582 scf_stats_p
[i
].cpu
= i
;
583 firsterr
= torture_create_kthread(scftorture_invoker
, (void *)&scf_stats_p
[i
],
584 scf_stats_p
[i
].task
);
588 if (stat_interval
> 0) {
589 firsterr
= torture_create_kthread(scf_torture_stats
, NULL
, scf_torture_stats_task
);
599 scf_torture_cleanup();
603 module_init(scf_torture_init
);
604 module_exit(scf_torture_cleanup
);