scftorture: Add smp_call_function_many() memory-ordering checks
[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
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.");
62torture_param(int, stutter_cpus, 5, "Number of jiffies to change CPUs under test, 0=disable");
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");
65torture_param(int, weight_single, -1, "Testing weight for single-CPU no-wait operations.");
66torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations.");
5022b8ac
PM
67torture_param(int, weight_many, -1, "Testing weight for multi-CPU no-wait operations.");
68torture_param(int, weight_many_wait, -1, "Testing weight for multi-CPU operations.");
e9d338a0
PM
69torture_param(int, weight_all, -1, "Testing weight for all-CPU no-wait operations.");
70torture_param(int, weight_all_wait, -1, "Testing weight for all-CPU operations.");
71
72char *torture_type = "";
73
74#ifdef MODULE
75# define SCFTORT_SHUTDOWN 0
76#else
77# define SCFTORT_SHUTDOWN 1
78#endif
79
80torture_param(bool, shutdown, SCFTORT_SHUTDOWN, "Shutdown at end of torture test.");
81
82struct scf_statistics {
83 struct task_struct *task;
84 int cpu;
85 long long n_single;
5022b8ac 86 long long n_single_ofl;
e9d338a0 87 long long n_single_wait;
5022b8ac
PM
88 long long n_single_wait_ofl;
89 long long n_many;
90 long long n_many_wait;
e9d338a0
PM
91 long long n_all;
92 long long n_all_wait;
93};
94
95static struct scf_statistics *scf_stats_p;
96static struct task_struct *scf_torture_stats_task;
97static DEFINE_PER_CPU(long long, scf_invoked_count);
98
5022b8ac
PM
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.
104
105static char *scf_prim_name[] = {
106 "smp_call_function_single",
107 "smp_call_function_many",
108 "smp_call_function",
109};
110
111struct scf_selector {
112 unsigned long scfs_weight;
113 int scfs_prim;
114 bool scfs_wait;
115};
116static struct scf_selector scf_sel_array[SCF_NPRIMS];
117static int scf_sel_array_len;
118static unsigned long scf_sel_totweight;
119
b93e21a5
PM
120// Communicate between caller and handler.
121struct scf_check {
122 bool scfc_in;
123 bool scfc_out;
124 int scfc_cpu; // -1 for not _single().
125 bool scfc_wait;
126};
127
e9d338a0
PM
128// Use to wait for all threads to start.
129static atomic_t n_started;
130static atomic_t n_errs;
b93e21a5
PM
131static atomic_t n_mb_in_errs;
132static atomic_t n_mb_out_errs;
133static atomic_t n_alloc_errs;
e9d338a0
PM
134static bool scfdone;
135
136DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
137
138// Print torture statistics. Caller must ensure serialization.
139static void scf_torture_stats_print(void)
140{
141 int cpu;
dba3142b 142 int i;
e9d338a0
PM
143 long long invoked_count = 0;
144 bool isdone = READ_ONCE(scfdone);
dba3142b 145 struct scf_statistics scfs = {};
e9d338a0
PM
146
147 for_each_possible_cpu(cpu)
148 invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
dba3142b
PM
149 for (i = 0; i < nthreads; i++) {
150 scfs.n_single += scf_stats_p[i].n_single;
151 scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
152 scfs.n_single_wait += scf_stats_p[i].n_single_wait;
153 scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl;
154 scfs.n_many += scf_stats_p[i].n_many;
155 scfs.n_many_wait += scf_stats_p[i].n_many_wait;
156 scfs.n_all += scf_stats_p[i].n_all;
157 scfs.n_all_wait += scf_stats_p[i].n_all_wait;
158 }
159 pr_alert("%s scf_invoked_count %s: %lld single: %lld/%lld single_ofl: %lld/%lld many: %lld/%lld all: %lld/%lld ",
160 SCFTORT_FLAG, isdone ? "VER" : "ver", invoked_count,
161 scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
162 scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
e9d338a0
PM
163 torture_onoff_stats();
164 pr_cont("\n");
165}
166
167// Periodically prints torture statistics, if periodic statistics printing
168// was specified via the stat_interval module parameter.
169static int
170scf_torture_stats(void *arg)
171{
172 VERBOSE_TOROUT_STRING("scf_torture_stats task started");
173 do {
174 schedule_timeout_interruptible(stat_interval * HZ);
175 scf_torture_stats_print();
176 torture_shutdown_absorb("scf_torture_stats");
177 } while (!torture_must_stop());
178 torture_kthread_stopping("scf_torture_stats");
179 return 0;
180}
181
5022b8ac
PM
182// Add a primitive to the scf_sel_array[].
183static void scf_sel_add(unsigned long weight, int prim, bool wait)
184{
185 struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len];
186
187 // If no weight, if array would overflow, if computing three-place
188 // percentages would overflow, or if the scf_prim_name[] array would
189 // overflow, don't bother. In the last three two cases, complain.
190 if (!weight ||
191 WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) ||
192 WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) ||
193 WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name)))
194 return;
195 scf_sel_totweight += weight;
196 scfsp->scfs_weight = scf_sel_totweight;
197 scfsp->scfs_prim = prim;
198 scfsp->scfs_wait = wait;
199 scf_sel_array_len++;
200}
201
202// Dump out weighting percentages for scf_prim_name[] array.
203static void scf_sel_dump(void)
204{
205 int i;
206 unsigned long oldw = 0;
207 struct scf_selector *scfsp;
208 unsigned long w;
209
210 for (i = 0; i < scf_sel_array_len; i++) {
211 scfsp = &scf_sel_array[i];
212 w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight;
213 pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000,
214 scf_prim_name[scfsp->scfs_prim],
215 scfsp->scfs_wait ? "wait" : "nowait");
216 oldw = scfsp->scfs_weight;
217 }
218}
219
220// Randomly pick a primitive and wait/nowait, based on weightings.
221static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp)
222{
223 int i;
224 unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1);
225
226 for (i = 0; i < scf_sel_array_len; i++)
227 if (scf_sel_array[i].scfs_weight >= w)
228 return &scf_sel_array[i];
229 WARN_ON_ONCE(1);
230 return &scf_sel_array[0];
231}
232
e9d338a0
PM
233// Update statistics and occasionally burn up mass quantities of CPU time,
234// if told to do so via scftorture.longwait. Otherwise, occasionally burn
235// a little bit.
b93e21a5 236static void scf_handler(void *scfc_in)
e9d338a0
PM
237{
238 int i;
239 int j;
240 unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
b93e21a5 241 struct scf_check *scfcp = scfc_in;
e9d338a0 242
980205ee
PM
243 if (likely(scfcp)) {
244 WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers.
245 if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in))))
246 atomic_inc(&n_mb_in_errs);
247 }
e9d338a0
PM
248 this_cpu_inc(scf_invoked_count);
249 if (longwait <= 0) {
250 if (!(r & 0xffc0))
251 udelay(r & 0x3f);
b93e21a5 252 goto out;
e9d338a0
PM
253 }
254 if (r & 0xfff)
b93e21a5 255 goto out;
e9d338a0
PM
256 r = (r >> 12);
257 if (longwait <= 0) {
258 udelay((r & 0xff) + 1);
b93e21a5 259 goto out;
e9d338a0
PM
260 }
261 r = r % longwait + 1;
262 for (i = 0; i < r; i++) {
263 for (j = 0; j < 1000; j++) {
264 udelay(1000);
265 cpu_relax();
266 }
267 }
b93e21a5
PM
268out:
269 if (unlikely(!scfcp))
270 return;
271 if (scfcp->scfc_wait)
272 WRITE_ONCE(scfcp->scfc_out, true);
273 else
274 kfree(scfcp);
e9d338a0
PM
275}
276
5022b8ac 277// As above, but check for correct CPU.
b93e21a5 278static void scf_handler_1(void *scfc_in)
5022b8ac 279{
b93e21a5
PM
280 struct scf_check *scfcp = scfc_in;
281
282 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 283 atomic_inc(&n_errs);
b93e21a5
PM
284 }
285 scf_handler(scfcp);
5022b8ac
PM
286}
287
e9d338a0 288// Randomly do an smp_call_function*() invocation.
5022b8ac 289static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
e9d338a0 290{
5022b8ac
PM
291 uintptr_t cpu;
292 int ret;
b93e21a5 293 struct scf_check *scfcp = NULL;
5022b8ac
PM
294 struct scf_selector *scfsp = scf_sel_rand(trsp);
295
e9d338a0
PM
296 if (use_cpus_read_lock)
297 cpus_read_lock();
298 else
299 preempt_disable();
5022b8ac
PM
300 switch (scfsp->scfs_prim) {
301 case SCF_PRIM_SINGLE:
b93e21a5
PM
302 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
303 if (WARN_ON_ONCE(!scfcp))
304 atomic_inc(&n_alloc_errs);
5022b8ac
PM
305 cpu = torture_random(trsp) % nr_cpu_ids;
306 if (scfsp->scfs_wait)
307 scfp->n_single_wait++;
308 else
309 scfp->n_single++;
b93e21a5
PM
310 if (scfcp) {
311 scfcp->scfc_cpu = cpu;
312 scfcp->scfc_wait = scfsp->scfs_wait;
313 scfcp->scfc_out = false;
314 scfcp->scfc_in = true;
315 }
316 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
5022b8ac
PM
317 if (ret) {
318 if (scfsp->scfs_wait)
319 scfp->n_single_wait_ofl++;
320 else
321 scfp->n_single_ofl++;
b93e21a5
PM
322 kfree(scfcp);
323 } else if (scfcp && scfsp->scfs_wait) {
324 if (WARN_ON_ONCE(!scfcp->scfc_out))
325 atomic_inc(&n_mb_out_errs); // Leak rather than trash!
326 else
327 kfree(scfcp);
5022b8ac
PM
328 }
329 break;
330 case SCF_PRIM_MANY:
980205ee
PM
331 if (scfsp->scfs_wait) {
332 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
333 if (WARN_ON_ONCE(!scfcp))
334 atomic_inc(&n_alloc_errs);
335 }
5022b8ac
PM
336 if (scfsp->scfs_wait)
337 scfp->n_many_wait++;
338 else
339 scfp->n_many++;
980205ee
PM
340 if (scfcp) {
341 scfcp->scfc_cpu = -1;
342 scfcp->scfc_wait = true;
343 scfcp->scfc_out = false;
344 scfcp->scfc_in = true;
345 }
346 smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
347 if (scfcp) {
348 if (WARN_ON_ONCE(!scfcp->scfc_out))
349 atomic_inc(&n_mb_out_errs); // Leak rather than trash!
350 else
351 kfree(scfcp);
352 }
5022b8ac
PM
353 break;
354 case SCF_PRIM_ALL:
355 if (scfsp->scfs_wait)
356 scfp->n_all_wait++;
357 else
358 scfp->n_all++;
359 smp_call_function(scf_handler, NULL, scfsp->scfs_wait);
360 break;
361 }
e9d338a0
PM
362 if (use_cpus_read_lock)
363 cpus_read_unlock();
364 else
365 preempt_enable();
366 if (!(torture_random(trsp) & 0xfff))
367 schedule_timeout_uninterruptible(1);
368}
369
370// SCF test kthread. Repeatedly does calls to members of the
371// smp_call_function() family of functions.
372static int scftorture_invoker(void *arg)
373{
374 DEFINE_TORTURE_RANDOM(rand);
375 struct scf_statistics *scfp = (struct scf_statistics *)arg;
376
377 VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
378 set_cpus_allowed_ptr(current, cpumask_of(scfp->cpu % nr_cpu_ids));
379 set_user_nice(current, MAX_NICE);
380 if (holdoff)
381 schedule_timeout_interruptible(holdoff * HZ);
382
383 VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp->cpu, smp_processor_id());
384
385 // Make sure that the CPU is affinitized appropriately during testing.
386 WARN_ON_ONCE(smp_processor_id() != scfp->cpu);
387
388 if (!atomic_dec_return(&n_started))
389 while (atomic_read_acquire(&n_started)) {
390 if (torture_must_stop()) {
391 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
392 goto end;
393 }
394 schedule_timeout_uninterruptible(1);
395 }
396
397 VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
398
399 do {
400 scftorture_invoke_one(scfp, &rand);
401 } while (!torture_must_stop());
402
403 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
404end:
405 torture_kthread_stopping("scftorture_invoker");
406 return 0;
407}
408
409static void
410scftorture_print_module_parms(const char *tag)
411{
412 pr_alert(SCFTORT_FLAG
5022b8ac
PM
413 "--- %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,
414 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);
e9d338a0
PM
415}
416
417static void scf_cleanup_handler(void *unused)
418{
419}
420
421static void scf_torture_cleanup(void)
422{
423 int i;
424
425 if (torture_cleanup_begin())
426 return;
427
428 WRITE_ONCE(scfdone, true);
429 if (nthreads)
430 for (i = 0; i < nthreads; i++)
431 torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
432 else
433 goto end;
e9d338a0
PM
434 smp_call_function(scf_cleanup_handler, NULL, 0);
435 torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
436 scf_torture_stats_print(); // -After- the stats thread is stopped!
dba3142b
PM
437 kfree(scf_stats_p); // -After- the last stats print has completed!
438 scf_stats_p = NULL;
e9d338a0
PM
439
440 if (atomic_read(&n_errs))
441 scftorture_print_module_parms("End of test: FAILURE");
442 else if (torture_onoff_failures())
443 scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
444 else
445 scftorture_print_module_parms("End of test: SUCCESS");
446
447end:
448 torture_cleanup_end();
449}
450
451static int __init scf_torture_init(void)
452{
453 long i;
454 int firsterr = 0;
5022b8ac
PM
455 unsigned long weight_single1 = weight_single;
456 unsigned long weight_single_wait1 = weight_single_wait;
457 unsigned long weight_many1 = weight_many;
458 unsigned long weight_many_wait1 = weight_many_wait;
459 unsigned long weight_all1 = weight_all;
460 unsigned long weight_all_wait1 = weight_all_wait;
e9d338a0
PM
461
462 if (!torture_init_begin(SCFTORT_STRING, verbose))
463 return -EBUSY;
464
465 scftorture_print_module_parms("Start of test");
466
467 if (weight_single == -1 && weight_single_wait == -1 &&
5022b8ac 468 weight_many == -1 && weight_many_wait == -1 &&
e9d338a0 469 weight_all == -1 && weight_all_wait == -1) {
5022b8ac
PM
470 weight_single1 = 2 * nr_cpu_ids;
471 weight_single_wait1 = 2 * nr_cpu_ids;
472 weight_many1 = 2;
473 weight_many_wait1 = 2;
474 weight_all1 = 1;
475 weight_all_wait1 = 1;
e9d338a0
PM
476 } else {
477 if (weight_single == -1)
5022b8ac 478 weight_single1 = 0;
e9d338a0 479 if (weight_single_wait == -1)
5022b8ac
PM
480 weight_single_wait1 = 0;
481 if (weight_many == -1)
482 weight_many1 = 0;
483 if (weight_many_wait == -1)
484 weight_many_wait1 = 0;
e9d338a0 485 if (weight_all == -1)
5022b8ac 486 weight_all1 = 0;
e9d338a0 487 if (weight_all_wait == -1)
5022b8ac 488 weight_all_wait1 = 0;
e9d338a0 489 }
5022b8ac
PM
490 if (weight_single1 == 0 && weight_single_wait1 == 0 &&
491 weight_many1 == 0 && weight_many_wait1 == 0 &&
492 weight_all1 == 0 && weight_all_wait1 == 0) {
493 VERBOSE_SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
e9d338a0
PM
494 firsterr = -EINVAL;
495 goto unwind;
496 }
5022b8ac
PM
497 scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
498 scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
499 scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
500 scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
501 scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
502 scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
503 scf_sel_dump();
e9d338a0
PM
504
505 if (onoff_interval > 0) {
506 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
507 if (firsterr)
508 goto unwind;
509 }
510 if (shutdown_secs > 0) {
511 firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
512 if (firsterr)
513 goto unwind;
514 }
515
516 // Worker tasks invoking smp_call_function().
517 if (nthreads < 0)
518 nthreads = num_online_cpus();
519 scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL);
520 if (!scf_stats_p) {
521 VERBOSE_SCFTORTOUT_ERRSTRING("out of memory");
522 firsterr = -ENOMEM;
523 goto unwind;
524 }
525
526 VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads\n", nthreads);
527
528 atomic_set(&n_started, nthreads);
529 for (i = 0; i < nthreads; i++) {
530 scf_stats_p[i].cpu = i;
531 firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
532 scf_stats_p[i].task);
533 if (firsterr)
534 goto unwind;
535 }
536 if (stat_interval > 0) {
537 firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
538 if (firsterr)
539 goto unwind;
540 }
541
542 torture_init_end();
543 return 0;
544
545unwind:
546 torture_init_end();
547 scf_torture_cleanup();
548 return firsterr;
549}
550
551module_init(scf_torture_init);
552module_exit(scf_torture_cleanup);