blob: 5d42f44e3e1a11a8c5c373c8f0563d891b79b80d [file] [log] [blame]
Paul E. McKenneye9d338a2020-06-24 15:59:59 -07001// 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.");
Paul E. McKenney85558182020-09-24 12:11:57 -070062torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070063torture_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");
Paul E. McKenney1ac78b42020-09-03 13:09:47 -070065torture_param(int, weight_resched, -1, "Testing weight for resched_cpu() operations.");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070066torture_param(int, weight_single, -1, "Testing weight for single-CPU no-wait operations.");
Paul E. McKenney9b9a8062021-06-24 17:53:43 -070067torture_param(int, weight_single_rpc, -1, "Testing weight for single-CPU RPC operations.");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070068torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations.");
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070069torture_param(int, weight_many, -1, "Testing weight for multi-CPU no-wait operations.");
70torture_param(int, weight_many_wait, -1, "Testing weight for multi-CPU operations.");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070071torture_param(int, weight_all, -1, "Testing weight for all-CPU no-wait operations.");
72torture_param(int, weight_all_wait, -1, "Testing weight for all-CPU operations.");
73
74char *torture_type = "";
75
76#ifdef MODULE
77# define SCFTORT_SHUTDOWN 0
78#else
79# define SCFTORT_SHUTDOWN 1
80#endif
81
82torture_param(bool, shutdown, SCFTORT_SHUTDOWN, "Shutdown at end of torture test.");
83
84struct scf_statistics {
85 struct task_struct *task;
86 int cpu;
Paul E. McKenney1ac78b42020-09-03 13:09:47 -070087 long long n_resched;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070088 long long n_single;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070089 long long n_single_ofl;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -070090 long long n_single_rpc;
91 long long n_single_rpc_ofl;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070092 long long n_single_wait;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070093 long long n_single_wait_ofl;
94 long long n_many;
95 long long n_many_wait;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070096 long long n_all;
97 long long n_all_wait;
98};
99
100static struct scf_statistics *scf_stats_p;
101static struct task_struct *scf_torture_stats_task;
102static DEFINE_PER_CPU(long long, scf_invoked_count);
103
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700104// Data for random primitive selection
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700105#define SCF_PRIM_RESCHED 0
106#define SCF_PRIM_SINGLE 1
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700107#define SCF_PRIM_SINGLE_RPC 2
108#define SCF_PRIM_MANY 3
109#define SCF_PRIM_ALL 4
110#define SCF_NPRIMS 8 // Need wait and no-wait versions of each,
111 // except for SCF_PRIM_RESCHED and
112 // SCF_PRIM_SINGLE_RPC.
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700113
114static char *scf_prim_name[] = {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700115 "resched_cpu",
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700116 "smp_call_function_single",
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700117 "smp_call_function_single_rpc",
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700118 "smp_call_function_many",
119 "smp_call_function",
120};
121
122struct scf_selector {
123 unsigned long scfs_weight;
124 int scfs_prim;
125 bool scfs_wait;
126};
127static struct scf_selector scf_sel_array[SCF_NPRIMS];
128static int scf_sel_array_len;
129static unsigned long scf_sel_totweight;
130
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700131// Communicate between caller and handler.
132struct scf_check {
133 bool scfc_in;
134 bool scfc_out;
135 int scfc_cpu; // -1 for not _single().
136 bool scfc_wait;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700137 bool scfc_rpc;
138 struct completion scfc_completion;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700139};
140
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700141// Use to wait for all threads to start.
142static atomic_t n_started;
143static atomic_t n_errs;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700144static atomic_t n_mb_in_errs;
145static atomic_t n_mb_out_errs;
146static atomic_t n_alloc_errs;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700147static bool scfdone;
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700148static char *bangstr = "";
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700149
Wei Yongjun9a52a5742020-07-02 09:56:50 -0700150static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700151
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700152extern void resched_cpu(int cpu); // An alternative IPI vector.
153
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700154// Print torture statistics. Caller must ensure serialization.
155static void scf_torture_stats_print(void)
156{
157 int cpu;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700158 int i;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700159 long long invoked_count = 0;
160 bool isdone = READ_ONCE(scfdone);
Paul E. McKenneydba31422020-06-30 16:13:37 -0700161 struct scf_statistics scfs = {};
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700162
163 for_each_possible_cpu(cpu)
164 invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
Paul E. McKenneydba31422020-06-30 16:13:37 -0700165 for (i = 0; i < nthreads; i++) {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700166 scfs.n_resched += scf_stats_p[i].n_resched;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700167 scfs.n_single += scf_stats_p[i].n_single;
168 scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700169 scfs.n_single_rpc += scf_stats_p[i].n_single_rpc;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700170 scfs.n_single_wait += scf_stats_p[i].n_single_wait;
171 scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl;
172 scfs.n_many += scf_stats_p[i].n_many;
173 scfs.n_many_wait += scf_stats_p[i].n_many_wait;
174 scfs.n_all += scf_stats_p[i].n_all;
175 scfs.n_all_wait += scf_stats_p[i].n_all_wait;
176 }
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700177 if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) ||
178 atomic_read(&n_mb_out_errs) || atomic_read(&n_alloc_errs))
179 bangstr = "!!! ";
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700180 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 ",
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700181 SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count, scfs.n_resched,
Paul E. McKenneydba31422020-06-30 16:13:37 -0700182 scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700183 scfs.n_single_rpc, scfs.n_single_rpc_ofl,
Paul E. McKenneydba31422020-06-30 16:13:37 -0700184 scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700185 torture_onoff_stats();
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700186 pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs),
187 atomic_read(&n_mb_in_errs), atomic_read(&n_mb_out_errs),
188 atomic_read(&n_alloc_errs));
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700189}
190
191// Periodically prints torture statistics, if periodic statistics printing
192// was specified via the stat_interval module parameter.
193static int
194scf_torture_stats(void *arg)
195{
196 VERBOSE_TOROUT_STRING("scf_torture_stats task started");
197 do {
198 schedule_timeout_interruptible(stat_interval * HZ);
199 scf_torture_stats_print();
200 torture_shutdown_absorb("scf_torture_stats");
201 } while (!torture_must_stop());
202 torture_kthread_stopping("scf_torture_stats");
203 return 0;
204}
205
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700206// Add a primitive to the scf_sel_array[].
207static void scf_sel_add(unsigned long weight, int prim, bool wait)
208{
209 struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len];
210
211 // If no weight, if array would overflow, if computing three-place
212 // percentages would overflow, or if the scf_prim_name[] array would
213 // overflow, don't bother. In the last three two cases, complain.
214 if (!weight ||
215 WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) ||
216 WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) ||
217 WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name)))
218 return;
219 scf_sel_totweight += weight;
220 scfsp->scfs_weight = scf_sel_totweight;
221 scfsp->scfs_prim = prim;
222 scfsp->scfs_wait = wait;
223 scf_sel_array_len++;
224}
225
226// Dump out weighting percentages for scf_prim_name[] array.
227static void scf_sel_dump(void)
228{
229 int i;
230 unsigned long oldw = 0;
231 struct scf_selector *scfsp;
232 unsigned long w;
233
234 for (i = 0; i < scf_sel_array_len; i++) {
235 scfsp = &scf_sel_array[i];
236 w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight;
237 pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000,
238 scf_prim_name[scfsp->scfs_prim],
239 scfsp->scfs_wait ? "wait" : "nowait");
240 oldw = scfsp->scfs_weight;
241 }
242}
243
244// Randomly pick a primitive and wait/nowait, based on weightings.
245static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp)
246{
247 int i;
248 unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1);
249
250 for (i = 0; i < scf_sel_array_len; i++)
251 if (scf_sel_array[i].scfs_weight >= w)
252 return &scf_sel_array[i];
253 WARN_ON_ONCE(1);
254 return &scf_sel_array[0];
255}
256
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700257// Update statistics and occasionally burn up mass quantities of CPU time,
258// if told to do so via scftorture.longwait. Otherwise, occasionally burn
259// a little bit.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700260static void scf_handler(void *scfc_in)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700261{
262 int i;
263 int j;
264 unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700265 struct scf_check *scfcp = scfc_in;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700266
Paul E. McKenney980205e2020-07-01 12:30:02 -0700267 if (likely(scfcp)) {
268 WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers.
269 if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in))))
270 atomic_inc(&n_mb_in_errs);
271 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700272 this_cpu_inc(scf_invoked_count);
273 if (longwait <= 0) {
274 if (!(r & 0xffc0))
275 udelay(r & 0x3f);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700276 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700277 }
278 if (r & 0xfff)
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700279 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700280 r = (r >> 12);
281 if (longwait <= 0) {
282 udelay((r & 0xff) + 1);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700283 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700284 }
285 r = r % longwait + 1;
286 for (i = 0; i < r; i++) {
287 for (j = 0; j < 1000; j++) {
288 udelay(1000);
289 cpu_relax();
290 }
291 }
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700292out:
293 if (unlikely(!scfcp))
294 return;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700295 if (scfcp->scfc_wait) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700296 WRITE_ONCE(scfcp->scfc_out, true);
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700297 if (scfcp->scfc_rpc)
298 complete(&scfcp->scfc_completion);
299 } else {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700300 kfree(scfcp);
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700301 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700302}
303
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700304// As above, but check for correct CPU.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700305static void scf_handler_1(void *scfc_in)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700306{
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700307 struct scf_check *scfcp = scfc_in;
308
309 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())) {
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700310 atomic_inc(&n_errs);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700311 }
312 scf_handler(scfcp);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700313}
314
315// Randomly do an smp_call_function*() invocation.
316static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
317{
318 uintptr_t cpu;
Paul E. McKenney676e5462020-07-01 14:13:02 -0700319 int ret = 0;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700320 struct scf_check *scfcp = NULL;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700321 struct scf_selector *scfsp = scf_sel_rand(trsp);
322
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700323 if (use_cpus_read_lock)
324 cpus_read_lock();
325 else
326 preempt_disable();
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700327 if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700328 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700329 if (WARN_ON_ONCE(!scfcp)) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700330 atomic_inc(&n_alloc_errs);
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700331 } else {
332 scfcp->scfc_cpu = -1;
333 scfcp->scfc_wait = scfsp->scfs_wait;
334 scfcp->scfc_out = false;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700335 scfcp->scfc_rpc = false;
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700336 }
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700337 }
338 switch (scfsp->scfs_prim) {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700339 case SCF_PRIM_RESCHED:
340 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) {
341 cpu = torture_random(trsp) % nr_cpu_ids;
342 scfp->n_resched++;
343 resched_cpu(cpu);
Paul E. McKenneyc3d02582021-07-14 06:53:51 -0700344 this_cpu_inc(scf_invoked_count);
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700345 }
346 break;
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700347 case SCF_PRIM_SINGLE:
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700348 cpu = torture_random(trsp) % nr_cpu_ids;
349 if (scfsp->scfs_wait)
350 scfp->n_single_wait++;
351 else
352 scfp->n_single++;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700353 if (scfcp) {
354 scfcp->scfc_cpu = cpu;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700355 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700356 scfcp->scfc_in = true;
357 }
358 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700359 if (ret) {
360 if (scfsp->scfs_wait)
361 scfp->n_single_wait_ofl++;
362 else
363 scfp->n_single_ofl++;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700364 kfree(scfcp);
Paul E. McKenney676e5462020-07-01 14:13:02 -0700365 scfcp = NULL;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700366 }
367 break;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700368 case SCF_PRIM_SINGLE_RPC:
369 if (!scfcp)
370 break;
371 cpu = torture_random(trsp) % nr_cpu_ids;
372 scfp->n_single_rpc++;
373 scfcp->scfc_cpu = cpu;
374 scfcp->scfc_wait = true;
375 init_completion(&scfcp->scfc_completion);
376 scfcp->scfc_rpc = true;
377 barrier(); // Prevent race-reduction compiler optimizations.
378 scfcp->scfc_in = true;
379 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, 0);
380 if (!ret) {
381 if (use_cpus_read_lock)
382 cpus_read_unlock();
383 else
384 preempt_enable();
385 wait_for_completion(&scfcp->scfc_completion);
386 if (use_cpus_read_lock)
387 cpus_read_lock();
388 else
389 preempt_disable();
390 } else {
391 scfp->n_single_rpc_ofl++;
392 kfree(scfcp);
393 scfcp = NULL;
394 }
395 break;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700396 case SCF_PRIM_MANY:
397 if (scfsp->scfs_wait)
398 scfp->n_many_wait++;
399 else
400 scfp->n_many++;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700401 if (scfcp) {
402 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney980205e2020-07-01 12:30:02 -0700403 scfcp->scfc_in = true;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700404 }
Paul E. McKenney980205e2020-07-01 12:30:02 -0700405 smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700406 break;
407 case SCF_PRIM_ALL:
408 if (scfsp->scfs_wait)
409 scfp->n_all_wait++;
410 else
411 scfp->n_all++;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700412 if (scfcp) {
413 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700414 scfcp->scfc_in = true;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700415 }
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700416 smp_call_function(scf_handler, scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700417 break;
Paul E. McKenneyde77d4d2020-07-02 12:15:37 -0700418 default:
419 WARN_ON_ONCE(1);
420 if (scfcp)
421 scfcp->scfc_out = true;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700422 }
Paul E. McKenney676e5462020-07-01 14:13:02 -0700423 if (scfcp && scfsp->scfs_wait) {
Paul E. McKenney9e66bf02020-07-03 15:23:19 -0700424 if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp->scfs_prim == SCF_PRIM_SINGLE) &&
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700425 !scfcp->scfc_out)) {
426 pr_warn("%s: Memory-ordering failure, scfs_prim: %d.\n", __func__, scfsp->scfs_prim);
Paul E. McKenney676e5462020-07-01 14:13:02 -0700427 atomic_inc(&n_mb_out_errs); // Leak rather than trash!
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700428 } else {
Paul E. McKenney676e5462020-07-01 14:13:02 -0700429 kfree(scfcp);
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700430 }
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700431 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney676e5462020-07-01 14:13:02 -0700432 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700433 if (use_cpus_read_lock)
434 cpus_read_unlock();
435 else
436 preempt_enable();
437 if (!(torture_random(trsp) & 0xfff))
438 schedule_timeout_uninterruptible(1);
439}
440
441// SCF test kthread. Repeatedly does calls to members of the
442// smp_call_function() family of functions.
443static int scftorture_invoker(void *arg)
444{
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700445 int cpu;
Paul E. McKenneyf3ea9782020-11-11 10:12:05 -0800446 int curcpu;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700447 DEFINE_TORTURE_RANDOM(rand);
448 struct scf_statistics *scfp = (struct scf_statistics *)arg;
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700449 bool was_offline = false;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700450
451 VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700452 cpu = scfp->cpu % nr_cpu_ids;
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700453 WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(cpu)));
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700454 set_user_nice(current, MAX_NICE);
455 if (holdoff)
456 schedule_timeout_interruptible(holdoff * HZ);
457
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700458 VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp->cpu, raw_smp_processor_id());
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700459
460 // Make sure that the CPU is affinitized appropriately during testing.
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700461 curcpu = raw_smp_processor_id();
Paul E. McKenneyf3ea9782020-11-11 10:12:05 -0800462 WARN_ONCE(curcpu != scfp->cpu % nr_cpu_ids,
463 "%s: Wanted CPU %d, running on %d, nr_cpu_ids = %d\n",
464 __func__, scfp->cpu, curcpu, nr_cpu_ids);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700465
466 if (!atomic_dec_return(&n_started))
467 while (atomic_read_acquire(&n_started)) {
468 if (torture_must_stop()) {
469 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
470 goto end;
471 }
472 schedule_timeout_uninterruptible(1);
473 }
474
475 VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
476
477 do {
478 scftorture_invoke_one(scfp, &rand);
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700479 while (cpu_is_offline(cpu) && !torture_must_stop()) {
480 schedule_timeout_interruptible(HZ / 5);
481 was_offline = true;
482 }
483 if (was_offline) {
484 set_cpus_allowed_ptr(current, cpumask_of(cpu));
485 was_offline = false;
486 }
Paul E. McKenney65bd77f2020-07-23 15:53:02 -0700487 cond_resched();
Paul E. McKenney85558182020-09-24 12:11:57 -0700488 stutter_wait("scftorture_invoker");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700489 } while (!torture_must_stop());
490
491 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
492end:
493 torture_kthread_stopping("scftorture_invoker");
494 return 0;
495}
496
497static void
498scftorture_print_module_parms(const char *tag)
499{
500 pr_alert(SCFTORT_FLAG
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700501 "--- %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,
502 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);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700503}
504
505static void scf_cleanup_handler(void *unused)
506{
507}
508
509static void scf_torture_cleanup(void)
510{
511 int i;
512
513 if (torture_cleanup_begin())
514 return;
515
516 WRITE_ONCE(scfdone, true);
Paul E. McKenney586e4d42021-07-09 17:53:27 -0700517 if (nthreads && scf_stats_p)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700518 for (i = 0; i < nthreads; i++)
519 torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
520 else
521 goto end;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700522 smp_call_function(scf_cleanup_handler, NULL, 0);
523 torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
524 scf_torture_stats_print(); // -After- the stats thread is stopped!
Paul E. McKenneydba31422020-06-30 16:13:37 -0700525 kfree(scf_stats_p); // -After- the last stats print has completed!
526 scf_stats_p = NULL;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700527
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700528 if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) || atomic_read(&n_mb_out_errs))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700529 scftorture_print_module_parms("End of test: FAILURE");
530 else if (torture_onoff_failures())
531 scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
532 else
533 scftorture_print_module_parms("End of test: SUCCESS");
534
535end:
536 torture_cleanup_end();
537}
538
539static int __init scf_torture_init(void)
540{
541 long i;
542 int firsterr = 0;
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700543 unsigned long weight_resched1 = weight_resched;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700544 unsigned long weight_single1 = weight_single;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700545 unsigned long weight_single_rpc1 = weight_single_rpc;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700546 unsigned long weight_single_wait1 = weight_single_wait;
547 unsigned long weight_many1 = weight_many;
548 unsigned long weight_many_wait1 = weight_many_wait;
549 unsigned long weight_all1 = weight_all;
550 unsigned long weight_all_wait1 = weight_all_wait;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700551
552 if (!torture_init_begin(SCFTORT_STRING, verbose))
553 return -EBUSY;
554
555 scftorture_print_module_parms("Start of test");
556
Paul E. McKenney2f611d02021-07-13 14:12:54 -0700557 if (weight_resched <= 0 &&
558 weight_single <= 0 && weight_single_rpc <= 0 && weight_single_wait <= 0 &&
559 weight_many <= 0 && weight_many_wait <= 0 &&
560 weight_all <= 0 && weight_all_wait <= 0) {
561 weight_resched1 = weight_resched == 0 ? 0 : 2 * nr_cpu_ids;
562 weight_single1 = weight_single == 0 ? 0 : 2 * nr_cpu_ids;
563 weight_single_rpc1 = weight_single_rpc == 0 ? 0 : 2 * nr_cpu_ids;
564 weight_single_wait1 = weight_single_wait == 0 ? 0 : 2 * nr_cpu_ids;
565 weight_many1 = weight_many == 0 ? 0 : 2;
566 weight_many_wait1 = weight_many_wait == 0 ? 0 : 2;
567 weight_all1 = weight_all == 0 ? 0 : 1;
568 weight_all_wait1 = weight_all_wait == 0 ? 0 : 1;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700569 } else {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700570 if (weight_resched == -1)
571 weight_resched1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700572 if (weight_single == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700573 weight_single1 = 0;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700574 if (weight_single_rpc == -1)
575 weight_single_rpc1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700576 if (weight_single_wait == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700577 weight_single_wait1 = 0;
578 if (weight_many == -1)
579 weight_many1 = 0;
580 if (weight_many_wait == -1)
581 weight_many_wait1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700582 if (weight_all == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700583 weight_all1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700584 if (weight_all_wait == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700585 weight_all_wait1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700586 }
Paul E. McKenneyda9366c2021-07-13 15:13:56 -0700587 if (weight_resched1 == 0 && weight_single1 == 0 && weight_single_rpc1 == 0 &&
588 weight_single_wait1 == 0 && weight_many1 == 0 && weight_many_wait1 == 0 &&
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700589 weight_all1 == 0 && weight_all_wait1 == 0) {
590 VERBOSE_SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700591 firsterr = -EINVAL;
592 goto unwind;
593 }
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700594 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST))
595 scf_sel_add(weight_resched1, SCF_PRIM_RESCHED, false);
596 else if (weight_resched1)
597 VERBOSE_SCFTORTOUT_ERRSTRING("built as module, weight_resched ignored");
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700598 scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700599 scf_sel_add(weight_single_rpc1, SCF_PRIM_SINGLE_RPC, true);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700600 scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
601 scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
602 scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
603 scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
604 scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
605 scf_sel_dump();
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700606
607 if (onoff_interval > 0) {
608 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700609 if (torture_init_error(firsterr))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700610 goto unwind;
611 }
612 if (shutdown_secs > 0) {
613 firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700614 if (torture_init_error(firsterr))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700615 goto unwind;
616 }
Paul E. McKenney85558182020-09-24 12:11:57 -0700617 if (stutter > 0) {
618 firsterr = torture_stutter_init(stutter, stutter);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700619 if (torture_init_error(firsterr))
Paul E. McKenney85558182020-09-24 12:11:57 -0700620 goto unwind;
621 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700622
623 // Worker tasks invoking smp_call_function().
624 if (nthreads < 0)
625 nthreads = num_online_cpus();
626 scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL);
627 if (!scf_stats_p) {
628 VERBOSE_SCFTORTOUT_ERRSTRING("out of memory");
629 firsterr = -ENOMEM;
630 goto unwind;
631 }
632
633 VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads\n", nthreads);
634
635 atomic_set(&n_started, nthreads);
636 for (i = 0; i < nthreads; i++) {
637 scf_stats_p[i].cpu = i;
638 firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
639 scf_stats_p[i].task);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700640 if (torture_init_error(firsterr))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700641 goto unwind;
642 }
643 if (stat_interval > 0) {
644 firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700645 if (torture_init_error(firsterr))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700646 goto unwind;
647 }
648
649 torture_init_end();
650 return 0;
651
652unwind:
653 torture_init_end();
654 scf_torture_cleanup();
Paul E. McKenney2b1388f2021-07-13 14:20:35 -0700655 if (shutdown_secs) {
656 WARN_ON(!IS_MODULE(CONFIG_SCF_TORTURE_TEST));
657 kernel_power_off();
658 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700659 return firsterr;
660}
661
662module_init(scf_torture_init);
663module_exit(scf_torture_cleanup);