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_cpus, 5, "Number of jiffies to change CPUs under 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_single, -1, "Testing weight for single-CPU no-wait operations.");
66 torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations.");
67 torture_param(int, weight_many, -1, "Testing weight for multi-CPU no-wait operations.");
68 torture_param(int, weight_many_wait, -1, "Testing weight for multi-CPU operations.");
69 torture_param(int, weight_all, -1, "Testing weight for all-CPU no-wait operations.");
70 torture_param(int, weight_all_wait, -1, "Testing weight for all-CPU operations.");
72 char *torture_type = "";
75 # define SCFTORT_SHUTDOWN 0
77 # define SCFTORT_SHUTDOWN 1
80 torture_param(bool, shutdown, SCFTORT_SHUTDOWN, "Shutdown at end of torture test.");
82 struct scf_statistics {
83 struct task_struct *task;
86 long long n_single_ofl;
87 long long n_single_wait;
88 long long n_single_wait_ofl;
90 long long n_many_wait;
95 static struct scf_statistics *scf_stats_p;
96 static struct task_struct *scf_torture_stats_task;
97 static DEFINE_PER_CPU(long long, scf_invoked_count);
99 // Data for random primitive selection
100 #define SCF_PRIM_SINGLE 0
101 #define SCF_PRIM_MANY 1
102 #define SCF_PRIM_ALL 2
103 #define SCF_NPRIMS (2 * 3) // Need wait and no-wait versions of each.
105 static char *scf_prim_name[] = {
106 "smp_call_function_single",
107 "smp_call_function_many",
111 struct scf_selector {
112 unsigned long scfs_weight;
116 static struct scf_selector scf_sel_array[SCF_NPRIMS];
117 static int scf_sel_array_len;
118 static unsigned long scf_sel_totweight;
120 // Communicate between caller and handler.
124 int scfc_cpu; // -1 for not _single().
128 // Use to wait for all threads to start.
129 static atomic_t n_started;
130 static atomic_t n_errs;
131 static atomic_t n_mb_in_errs;
132 static atomic_t n_mb_out_errs;
133 static atomic_t n_alloc_errs;
135 static char *bangstr = "";
137 static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
139 // Print torture statistics. Caller must ensure serialization.
140 static void scf_torture_stats_print(void)
144 long long invoked_count = 0;
145 bool isdone = READ_ONCE(scfdone);
146 struct scf_statistics scfs = {};
148 for_each_possible_cpu(cpu)
149 invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
150 for (i = 0; i < nthreads; i++) {
151 scfs.n_single += scf_stats_p[i].n_single;
152 scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
153 scfs.n_single_wait += scf_stats_p[i].n_single_wait;
154 scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl;
155 scfs.n_many += scf_stats_p[i].n_many;
156 scfs.n_many_wait += scf_stats_p[i].n_many_wait;
157 scfs.n_all += scf_stats_p[i].n_all;
158 scfs.n_all_wait += scf_stats_p[i].n_all_wait;
160 if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) ||
161 atomic_read(&n_mb_out_errs) ||
162 (!IS_ENABLED(CONFIG_KASAN) && atomic_read(&n_alloc_errs)))
164 pr_alert("%s %sscf_invoked_count %s: %lld single: %lld/%lld single_ofl: %lld/%lld many: %lld/%lld all: %lld/%lld ",
165 SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count,
166 scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
167 scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
168 torture_onoff_stats();
169 pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs),
170 atomic_read(&n_mb_in_errs), atomic_read(&n_mb_out_errs),
171 atomic_read(&n_alloc_errs));
174 // Periodically prints torture statistics, if periodic statistics printing
175 // was specified via the stat_interval module parameter.
177 scf_torture_stats(void *arg)
179 VERBOSE_TOROUT_STRING("scf_torture_stats task started");
181 schedule_timeout_interruptible(stat_interval * HZ);
182 scf_torture_stats_print();
183 torture_shutdown_absorb("scf_torture_stats");
184 } while (!torture_must_stop());
185 torture_kthread_stopping("scf_torture_stats");
189 // Add a primitive to the scf_sel_array[].
190 static void scf_sel_add(unsigned long weight, int prim, bool wait)
192 struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len];
194 // If no weight, if array would overflow, if computing three-place
195 // percentages would overflow, or if the scf_prim_name[] array would
196 // overflow, don't bother. In the last three two cases, complain.
198 WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) ||
199 WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) ||
200 WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name)))
202 scf_sel_totweight += weight;
203 scfsp->scfs_weight = scf_sel_totweight;
204 scfsp->scfs_prim = prim;
205 scfsp->scfs_wait = wait;
209 // Dump out weighting percentages for scf_prim_name[] array.
210 static void scf_sel_dump(void)
213 unsigned long oldw = 0;
214 struct scf_selector *scfsp;
217 for (i = 0; i < scf_sel_array_len; i++) {
218 scfsp = &scf_sel_array[i];
219 w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight;
220 pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000,
221 scf_prim_name[scfsp->scfs_prim],
222 scfsp->scfs_wait ? "wait" : "nowait");
223 oldw = scfsp->scfs_weight;
227 // Randomly pick a primitive and wait/nowait, based on weightings.
228 static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp)
231 unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1);
233 for (i = 0; i < scf_sel_array_len; i++)
234 if (scf_sel_array[i].scfs_weight >= w)
235 return &scf_sel_array[i];
237 return &scf_sel_array[0];
240 // Update statistics and occasionally burn up mass quantities of CPU time,
241 // if told to do so via scftorture.longwait. Otherwise, occasionally burn
243 static void scf_handler(void *scfc_in)
247 unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
248 struct scf_check *scfcp = scfc_in;
251 WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers.
252 if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in))))
253 atomic_inc(&n_mb_in_errs);
255 this_cpu_inc(scf_invoked_count);
266 udelay((r & 0xff) + 1);
269 r = r % longwait + 1;
270 for (i = 0; i < r; i++) {
271 for (j = 0; j < 1000; j++) {
277 if (unlikely(!scfcp))
279 if (scfcp->scfc_wait)
280 WRITE_ONCE(scfcp->scfc_out, true);
285 // As above, but check for correct CPU.
286 static void scf_handler_1(void *scfc_in)
288 struct scf_check *scfcp = scfc_in;
290 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())) {
296 // Randomly do an smp_call_function*() invocation.
297 static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
301 struct scf_check *scfcp = NULL;
302 struct scf_selector *scfsp = scf_sel_rand(trsp);
304 if (use_cpus_read_lock)
308 if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
309 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
311 WARN_ON_ONCE(!IS_ENABLED(CONFIG_KASAN));
312 atomic_inc(&n_alloc_errs);
314 scfcp->scfc_cpu = -1;
315 scfcp->scfc_wait = scfsp->scfs_wait;
316 scfcp->scfc_out = false;
319 switch (scfsp->scfs_prim) {
320 case SCF_PRIM_SINGLE:
321 cpu = torture_random(trsp) % nr_cpu_ids;
322 if (scfsp->scfs_wait)
323 scfp->n_single_wait++;
327 scfcp->scfc_cpu = cpu;
328 barrier(); // Prevent race-reduction compiler optimizations.
329 scfcp->scfc_in = true;
331 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
333 if (scfsp->scfs_wait)
334 scfp->n_single_wait_ofl++;
336 scfp->n_single_ofl++;
342 if (scfsp->scfs_wait)
347 barrier(); // Prevent race-reduction compiler optimizations.
348 scfcp->scfc_in = true;
350 smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
353 if (scfsp->scfs_wait)
358 barrier(); // Prevent race-reduction compiler optimizations.
359 scfcp->scfc_in = true;
361 smp_call_function(scf_handler, scfcp, scfsp->scfs_wait);
366 scfcp->scfc_out = true;
368 if (scfcp && scfsp->scfs_wait) {
369 if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp->scfs_prim == SCF_PRIM_SINGLE) &&
371 atomic_inc(&n_mb_out_errs); // Leak rather than trash!
374 barrier(); // Prevent race-reduction compiler optimizations.
376 if (use_cpus_read_lock)
380 if (!(torture_random(trsp) & 0xfff))
381 schedule_timeout_uninterruptible(1);
384 // SCF test kthread. Repeatedly does calls to members of the
385 // smp_call_function() family of functions.
386 static int scftorture_invoker(void *arg)
389 DEFINE_TORTURE_RANDOM(rand);
390 struct scf_statistics *scfp = (struct scf_statistics *)arg;
391 bool was_offline = false;
393 VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
394 cpu = scfp->cpu % nr_cpu_ids;
395 set_cpus_allowed_ptr(current, cpumask_of(cpu));
396 set_user_nice(current, MAX_NICE);
398 schedule_timeout_interruptible(holdoff * HZ);
400 VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp->cpu, smp_processor_id());
402 // Make sure that the CPU is affinitized appropriately during testing.
403 WARN_ON_ONCE(smp_processor_id() != scfp->cpu);
405 if (!atomic_dec_return(&n_started))
406 while (atomic_read_acquire(&n_started)) {
407 if (torture_must_stop()) {
408 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
411 schedule_timeout_uninterruptible(1);
414 VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
417 scftorture_invoke_one(scfp, &rand);
418 while (cpu_is_offline(cpu) && !torture_must_stop()) {
419 schedule_timeout_interruptible(HZ / 5);
423 set_cpus_allowed_ptr(current, cpumask_of(cpu));
427 } while (!torture_must_stop());
429 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
431 torture_kthread_stopping("scftorture_invoker");
436 scftorture_print_module_parms(const char *tag)
438 pr_alert(SCFTORT_FLAG
439 "--- %s: verbose=%d holdoff=%d longwait=%d nthreads=%d onoff_holdoff=%d onoff_interval=%d shutdown_secs=%d stat_interval=%d stutter_cpus=%d use_cpus_read_lock=%d, weight_single=%d, weight_single_wait=%d, weight_many=%d, weight_many_wait=%d, weight_all=%d, weight_all_wait=%d\n", tag,
440 verbose, holdoff, longwait, nthreads, onoff_holdoff, onoff_interval, shutdown, stat_interval, stutter_cpus, use_cpus_read_lock, weight_single, weight_single_wait, weight_many, weight_many_wait, weight_all, weight_all_wait);
443 static void scf_cleanup_handler(void *unused)
447 static void scf_torture_cleanup(void)
451 if (torture_cleanup_begin())
454 WRITE_ONCE(scfdone, true);
456 for (i = 0; i < nthreads; i++)
457 torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
460 smp_call_function(scf_cleanup_handler, NULL, 0);
461 torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
462 scf_torture_stats_print(); // -After- the stats thread is stopped!
463 kfree(scf_stats_p); // -After- the last stats print has completed!
466 if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) || atomic_read(&n_mb_out_errs))
467 scftorture_print_module_parms("End of test: FAILURE");
468 else if (torture_onoff_failures())
469 scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
471 scftorture_print_module_parms("End of test: SUCCESS");
474 torture_cleanup_end();
477 static int __init scf_torture_init(void)
481 unsigned long weight_single1 = weight_single;
482 unsigned long weight_single_wait1 = weight_single_wait;
483 unsigned long weight_many1 = weight_many;
484 unsigned long weight_many_wait1 = weight_many_wait;
485 unsigned long weight_all1 = weight_all;
486 unsigned long weight_all_wait1 = weight_all_wait;
488 if (!torture_init_begin(SCFTORT_STRING, verbose))
491 scftorture_print_module_parms("Start of test");
493 if (weight_single == -1 && weight_single_wait == -1 &&
494 weight_many == -1 && weight_many_wait == -1 &&
495 weight_all == -1 && weight_all_wait == -1) {
496 weight_single1 = 2 * nr_cpu_ids;
497 weight_single_wait1 = 2 * nr_cpu_ids;
499 weight_many_wait1 = 2;
501 weight_all_wait1 = 1;
503 if (weight_single == -1)
505 if (weight_single_wait == -1)
506 weight_single_wait1 = 0;
507 if (weight_many == -1)
509 if (weight_many_wait == -1)
510 weight_many_wait1 = 0;
511 if (weight_all == -1)
513 if (weight_all_wait == -1)
514 weight_all_wait1 = 0;
516 if (weight_single1 == 0 && weight_single_wait1 == 0 &&
517 weight_many1 == 0 && weight_many_wait1 == 0 &&
518 weight_all1 == 0 && weight_all_wait1 == 0) {
519 VERBOSE_SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
523 scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
524 scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
525 scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
526 scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
527 scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
528 scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
531 if (onoff_interval > 0) {
532 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
536 if (shutdown_secs > 0) {
537 firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
542 // Worker tasks invoking smp_call_function().
544 nthreads = num_online_cpus();
545 scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL);
547 VERBOSE_SCFTORTOUT_ERRSTRING("out of memory");
552 VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads\n", nthreads);
554 atomic_set(&n_started, nthreads);
555 for (i = 0; i < nthreads; i++) {
556 scf_stats_p[i].cpu = i;
557 firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
558 scf_stats_p[i].task);
562 if (stat_interval > 0) {
563 firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
573 scf_torture_cleanup();
577 module_init(scf_torture_init);
578 module_exit(scf_torture_cleanup);