blob: dcb0410950e45cd6eb0d436c185f1dd281631614 [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
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070041#define VERBOSE_SCFTORTOUT(s, x...) \
Li Zhijian71f6ea2a2021-10-29 17:40:25 +080042 do { if (verbose) pr_alert(SCFTORT_FLAG s "\n", ## x); } while (0)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070043
Li Zhijian809da9b2021-11-03 16:30:27 +080044#define SCFTORTOUT_ERRSTRING(s, x...) pr_alert(SCFTORT_FLAG "!!! " s "\n", ## x)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070045
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.");
Paul E. McKenney85558182020-09-24 12:11:57 -070058torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070059torture_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");
Paul E. McKenney1ac78b42020-09-03 13:09:47 -070061torture_param(int, weight_resched, -1, "Testing weight for resched_cpu() operations.");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070062torture_param(int, weight_single, -1, "Testing weight for single-CPU no-wait operations.");
Paul E. McKenney9b9a8062021-06-24 17:53:43 -070063torture_param(int, weight_single_rpc, -1, "Testing weight for single-CPU RPC operations.");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070064torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations.");
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070065torture_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.");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070067torture_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;
Paul E. McKenney1ac78b42020-09-03 13:09:47 -070083 long long n_resched;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070084 long long n_single;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070085 long long n_single_ofl;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -070086 long long n_single_rpc;
87 long long n_single_rpc_ofl;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070088 long long n_single_wait;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070089 long long n_single_wait_ofl;
90 long long n_many;
91 long long n_many_wait;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070092 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
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700100// Data for random primitive selection
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700101#define SCF_PRIM_RESCHED 0
102#define SCF_PRIM_SINGLE 1
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700103#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.
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700109
110static char *scf_prim_name[] = {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700111 "resched_cpu",
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700112 "smp_call_function_single",
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700113 "smp_call_function_single_rpc",
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700114 "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
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700127// 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;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700133 bool scfc_rpc;
134 struct completion scfc_completion;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700135};
136
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700137// Use to wait for all threads to start.
138static atomic_t n_started;
139static atomic_t n_errs;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700140static atomic_t n_mb_in_errs;
141static atomic_t n_mb_out_errs;
142static atomic_t n_alloc_errs;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700143static bool scfdone;
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700144static char *bangstr = "";
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700145
Wei Yongjun9a52a5742020-07-02 09:56:50 -0700146static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700147
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700148extern void resched_cpu(int cpu); // An alternative IPI vector.
149
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700150// Print torture statistics. Caller must ensure serialization.
151static void scf_torture_stats_print(void)
152{
153 int cpu;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700154 int i;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700155 long long invoked_count = 0;
156 bool isdone = READ_ONCE(scfdone);
Paul E. McKenneydba31422020-06-30 16:13:37 -0700157 struct scf_statistics scfs = {};
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700158
159 for_each_possible_cpu(cpu)
160 invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
Paul E. McKenneydba31422020-06-30 16:13:37 -0700161 for (i = 0; i < nthreads; i++) {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700162 scfs.n_resched += scf_stats_p[i].n_resched;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700163 scfs.n_single += scf_stats_p[i].n_single;
164 scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700165 scfs.n_single_rpc += scf_stats_p[i].n_single_rpc;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700166 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 }
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700173 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 = "!!! ";
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700176 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 -0700177 SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count, scfs.n_resched,
Paul E. McKenneydba31422020-06-30 16:13:37 -0700178 scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700179 scfs.n_single_rpc, scfs.n_single_rpc_ofl,
Paul E. McKenneydba31422020-06-30 16:13:37 -0700180 scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700181 torture_onoff_stats();
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700182 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));
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700185}
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
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700202// 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
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700253// 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.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700256static void scf_handler(void *scfc_in)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700257{
258 int i;
259 int j;
260 unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700261 struct scf_check *scfcp = scfc_in;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700262
Paul E. McKenney980205e2020-07-01 12:30:02 -0700263 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 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700268 this_cpu_inc(scf_invoked_count);
269 if (longwait <= 0) {
270 if (!(r & 0xffc0))
271 udelay(r & 0x3f);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700272 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700273 }
274 if (r & 0xfff)
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700275 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700276 r = (r >> 12);
277 if (longwait <= 0) {
278 udelay((r & 0xff) + 1);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700279 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700280 }
281 r = r % longwait + 1;
282 for (i = 0; i < r; i++) {
283 for (j = 0; j < 1000; j++) {
284 udelay(1000);
285 cpu_relax();
286 }
287 }
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700288out:
289 if (unlikely(!scfcp))
290 return;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700291 if (scfcp->scfc_wait) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700292 WRITE_ONCE(scfcp->scfc_out, true);
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700293 if (scfcp->scfc_rpc)
294 complete(&scfcp->scfc_completion);
295 } else {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700296 kfree(scfcp);
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700297 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700298}
299
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700300// As above, but check for correct CPU.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700301static void scf_handler_1(void *scfc_in)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700302{
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700303 struct scf_check *scfcp = scfc_in;
304
305 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 -0700306 atomic_inc(&n_errs);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700307 }
308 scf_handler(scfcp);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700309}
310
311// Randomly do an smp_call_function*() invocation.
312static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
313{
314 uintptr_t cpu;
Paul E. McKenney676e5462020-07-01 14:13:02 -0700315 int ret = 0;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700316 struct scf_check *scfcp = NULL;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700317 struct scf_selector *scfsp = scf_sel_rand(trsp);
318
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700319 if (use_cpus_read_lock)
320 cpus_read_lock();
321 else
322 preempt_disable();
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700323 if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700324 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700325 if (WARN_ON_ONCE(!scfcp)) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700326 atomic_inc(&n_alloc_errs);
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700327 } else {
328 scfcp->scfc_cpu = -1;
329 scfcp->scfc_wait = scfsp->scfs_wait;
330 scfcp->scfc_out = false;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700331 scfcp->scfc_rpc = false;
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700332 }
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700333 }
334 switch (scfsp->scfs_prim) {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700335 case SCF_PRIM_RESCHED:
336 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) {
337 cpu = torture_random(trsp) % nr_cpu_ids;
338 scfp->n_resched++;
339 resched_cpu(cpu);
Paul E. McKenneyc3d02582021-07-14 06:53:51 -0700340 this_cpu_inc(scf_invoked_count);
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700341 }
342 break;
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700343 case SCF_PRIM_SINGLE:
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700344 cpu = torture_random(trsp) % nr_cpu_ids;
345 if (scfsp->scfs_wait)
346 scfp->n_single_wait++;
347 else
348 scfp->n_single++;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700349 if (scfcp) {
350 scfcp->scfc_cpu = cpu;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700351 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700352 scfcp->scfc_in = true;
353 }
354 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700355 if (ret) {
356 if (scfsp->scfs_wait)
357 scfp->n_single_wait_ofl++;
358 else
359 scfp->n_single_ofl++;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700360 kfree(scfcp);
Paul E. McKenney676e5462020-07-01 14:13:02 -0700361 scfcp = NULL;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700362 }
363 break;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700364 case SCF_PRIM_SINGLE_RPC:
365 if (!scfcp)
366 break;
367 cpu = torture_random(trsp) % nr_cpu_ids;
368 scfp->n_single_rpc++;
369 scfcp->scfc_cpu = cpu;
370 scfcp->scfc_wait = true;
371 init_completion(&scfcp->scfc_completion);
372 scfcp->scfc_rpc = true;
373 barrier(); // Prevent race-reduction compiler optimizations.
374 scfcp->scfc_in = true;
375 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, 0);
376 if (!ret) {
377 if (use_cpus_read_lock)
378 cpus_read_unlock();
379 else
380 preempt_enable();
381 wait_for_completion(&scfcp->scfc_completion);
382 if (use_cpus_read_lock)
383 cpus_read_lock();
384 else
385 preempt_disable();
386 } else {
387 scfp->n_single_rpc_ofl++;
388 kfree(scfcp);
389 scfcp = NULL;
390 }
391 break;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700392 case SCF_PRIM_MANY:
393 if (scfsp->scfs_wait)
394 scfp->n_many_wait++;
395 else
396 scfp->n_many++;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700397 if (scfcp) {
398 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney980205e2020-07-01 12:30:02 -0700399 scfcp->scfc_in = true;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700400 }
Paul E. McKenney980205e2020-07-01 12:30:02 -0700401 smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700402 break;
403 case SCF_PRIM_ALL:
404 if (scfsp->scfs_wait)
405 scfp->n_all_wait++;
406 else
407 scfp->n_all++;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700408 if (scfcp) {
409 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700410 scfcp->scfc_in = true;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700411 }
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700412 smp_call_function(scf_handler, scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700413 break;
Paul E. McKenneyde77d4d2020-07-02 12:15:37 -0700414 default:
415 WARN_ON_ONCE(1);
416 if (scfcp)
417 scfcp->scfc_out = true;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700418 }
Paul E. McKenney676e5462020-07-01 14:13:02 -0700419 if (scfcp && scfsp->scfs_wait) {
Paul E. McKenney9e66bf02020-07-03 15:23:19 -0700420 if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp->scfs_prim == SCF_PRIM_SINGLE) &&
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700421 !scfcp->scfc_out)) {
422 pr_warn("%s: Memory-ordering failure, scfs_prim: %d.\n", __func__, scfsp->scfs_prim);
Paul E. McKenney676e5462020-07-01 14:13:02 -0700423 atomic_inc(&n_mb_out_errs); // Leak rather than trash!
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700424 } else {
Paul E. McKenney676e5462020-07-01 14:13:02 -0700425 kfree(scfcp);
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700426 }
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700427 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney676e5462020-07-01 14:13:02 -0700428 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700429 if (use_cpus_read_lock)
430 cpus_read_unlock();
431 else
432 preempt_enable();
433 if (!(torture_random(trsp) & 0xfff))
434 schedule_timeout_uninterruptible(1);
435}
436
437// SCF test kthread. Repeatedly does calls to members of the
438// smp_call_function() family of functions.
439static int scftorture_invoker(void *arg)
440{
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700441 int cpu;
Paul E. McKenneyf3ea9782020-11-11 10:12:05 -0800442 int curcpu;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700443 DEFINE_TORTURE_RANDOM(rand);
444 struct scf_statistics *scfp = (struct scf_statistics *)arg;
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700445 bool was_offline = false;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700446
447 VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700448 cpu = scfp->cpu % nr_cpu_ids;
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700449 WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(cpu)));
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700450 set_user_nice(current, MAX_NICE);
451 if (holdoff)
452 schedule_timeout_interruptible(holdoff * HZ);
453
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700454 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 -0700455
456 // Make sure that the CPU is affinitized appropriately during testing.
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700457 curcpu = raw_smp_processor_id();
Paul E. McKenneyf3ea9782020-11-11 10:12:05 -0800458 WARN_ONCE(curcpu != scfp->cpu % nr_cpu_ids,
459 "%s: Wanted CPU %d, running on %d, nr_cpu_ids = %d\n",
460 __func__, scfp->cpu, curcpu, nr_cpu_ids);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700461
462 if (!atomic_dec_return(&n_started))
463 while (atomic_read_acquire(&n_started)) {
464 if (torture_must_stop()) {
465 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
466 goto end;
467 }
468 schedule_timeout_uninterruptible(1);
469 }
470
471 VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
472
473 do {
474 scftorture_invoke_one(scfp, &rand);
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700475 while (cpu_is_offline(cpu) && !torture_must_stop()) {
476 schedule_timeout_interruptible(HZ / 5);
477 was_offline = true;
478 }
479 if (was_offline) {
480 set_cpus_allowed_ptr(current, cpumask_of(cpu));
481 was_offline = false;
482 }
Paul E. McKenney65bd77f2020-07-23 15:53:02 -0700483 cond_resched();
Paul E. McKenney85558182020-09-24 12:11:57 -0700484 stutter_wait("scftorture_invoker");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700485 } while (!torture_must_stop());
486
487 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
488end:
489 torture_kthread_stopping("scftorture_invoker");
490 return 0;
491}
492
493static void
494scftorture_print_module_parms(const char *tag)
495{
496 pr_alert(SCFTORT_FLAG
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700497 "--- %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,
498 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 -0700499}
500
501static void scf_cleanup_handler(void *unused)
502{
503}
504
505static void scf_torture_cleanup(void)
506{
507 int i;
508
509 if (torture_cleanup_begin())
510 return;
511
512 WRITE_ONCE(scfdone, true);
Paul E. McKenney586e4d42021-07-09 17:53:27 -0700513 if (nthreads && scf_stats_p)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700514 for (i = 0; i < nthreads; i++)
515 torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
516 else
517 goto end;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700518 smp_call_function(scf_cleanup_handler, NULL, 0);
519 torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
520 scf_torture_stats_print(); // -After- the stats thread is stopped!
Paul E. McKenneydba31422020-06-30 16:13:37 -0700521 kfree(scf_stats_p); // -After- the last stats print has completed!
522 scf_stats_p = NULL;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700523
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700524 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 -0700525 scftorture_print_module_parms("End of test: FAILURE");
526 else if (torture_onoff_failures())
527 scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
528 else
529 scftorture_print_module_parms("End of test: SUCCESS");
530
531end:
532 torture_cleanup_end();
533}
534
535static int __init scf_torture_init(void)
536{
537 long i;
538 int firsterr = 0;
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700539 unsigned long weight_resched1 = weight_resched;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700540 unsigned long weight_single1 = weight_single;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700541 unsigned long weight_single_rpc1 = weight_single_rpc;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700542 unsigned long weight_single_wait1 = weight_single_wait;
543 unsigned long weight_many1 = weight_many;
544 unsigned long weight_many_wait1 = weight_many_wait;
545 unsigned long weight_all1 = weight_all;
546 unsigned long weight_all_wait1 = weight_all_wait;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700547
548 if (!torture_init_begin(SCFTORT_STRING, verbose))
549 return -EBUSY;
550
551 scftorture_print_module_parms("Start of test");
552
Paul E. McKenney2f611d02021-07-13 14:12:54 -0700553 if (weight_resched <= 0 &&
554 weight_single <= 0 && weight_single_rpc <= 0 && weight_single_wait <= 0 &&
555 weight_many <= 0 && weight_many_wait <= 0 &&
556 weight_all <= 0 && weight_all_wait <= 0) {
557 weight_resched1 = weight_resched == 0 ? 0 : 2 * nr_cpu_ids;
558 weight_single1 = weight_single == 0 ? 0 : 2 * nr_cpu_ids;
559 weight_single_rpc1 = weight_single_rpc == 0 ? 0 : 2 * nr_cpu_ids;
560 weight_single_wait1 = weight_single_wait == 0 ? 0 : 2 * nr_cpu_ids;
561 weight_many1 = weight_many == 0 ? 0 : 2;
562 weight_many_wait1 = weight_many_wait == 0 ? 0 : 2;
563 weight_all1 = weight_all == 0 ? 0 : 1;
564 weight_all_wait1 = weight_all_wait == 0 ? 0 : 1;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700565 } else {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700566 if (weight_resched == -1)
567 weight_resched1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700568 if (weight_single == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700569 weight_single1 = 0;
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700570 if (weight_single_rpc == -1)
571 weight_single_rpc1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700572 if (weight_single_wait == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700573 weight_single_wait1 = 0;
574 if (weight_many == -1)
575 weight_many1 = 0;
576 if (weight_many_wait == -1)
577 weight_many_wait1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700578 if (weight_all == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700579 weight_all1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700580 if (weight_all_wait == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700581 weight_all_wait1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700582 }
Paul E. McKenneyda9366c2021-07-13 15:13:56 -0700583 if (weight_resched1 == 0 && weight_single1 == 0 && weight_single_rpc1 == 0 &&
584 weight_single_wait1 == 0 && weight_many1 == 0 && weight_many_wait1 == 0 &&
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700585 weight_all1 == 0 && weight_all_wait1 == 0) {
Li Zhijian809da9b2021-11-03 16:30:27 +0800586 SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700587 firsterr = -EINVAL;
588 goto unwind;
589 }
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700590 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST))
591 scf_sel_add(weight_resched1, SCF_PRIM_RESCHED, false);
592 else if (weight_resched1)
Li Zhijian809da9b2021-11-03 16:30:27 +0800593 SCFTORTOUT_ERRSTRING("built as module, weight_resched ignored");
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700594 scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
Paul E. McKenney9b9a8062021-06-24 17:53:43 -0700595 scf_sel_add(weight_single_rpc1, SCF_PRIM_SINGLE_RPC, true);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700596 scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
597 scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
598 scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
599 scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
600 scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
601 scf_sel_dump();
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700602
603 if (onoff_interval > 0) {
604 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700605 if (torture_init_error(firsterr))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700606 goto unwind;
607 }
608 if (shutdown_secs > 0) {
609 firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700610 if (torture_init_error(firsterr))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700611 goto unwind;
612 }
Paul E. McKenney85558182020-09-24 12:11:57 -0700613 if (stutter > 0) {
614 firsterr = torture_stutter_init(stutter, stutter);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700615 if (torture_init_error(firsterr))
Paul E. McKenney85558182020-09-24 12:11:57 -0700616 goto unwind;
617 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700618
619 // Worker tasks invoking smp_call_function().
620 if (nthreads < 0)
621 nthreads = num_online_cpus();
622 scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL);
623 if (!scf_stats_p) {
Li Zhijian809da9b2021-11-03 16:30:27 +0800624 SCFTORTOUT_ERRSTRING("out of memory");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700625 firsterr = -ENOMEM;
626 goto unwind;
627 }
628
Li Zhijian71f6ea2a2021-10-29 17:40:25 +0800629 VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads", nthreads);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700630
631 atomic_set(&n_started, nthreads);
632 for (i = 0; i < nthreads; i++) {
633 scf_stats_p[i].cpu = i;
634 firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
635 scf_stats_p[i].task);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700636 if (torture_init_error(firsterr))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700637 goto unwind;
638 }
639 if (stat_interval > 0) {
640 firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
Paul E. McKenneyf2bdf7d2021-08-05 16:01:51 -0700641 if (torture_init_error(firsterr))
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700642 goto unwind;
643 }
644
645 torture_init_end();
646 return 0;
647
648unwind:
649 torture_init_end();
650 scf_torture_cleanup();
Paul E. McKenney2b1388f2021-07-13 14:20:35 -0700651 if (shutdown_secs) {
652 WARN_ON(!IS_MODULE(CONFIG_SCF_TORTURE_TEST));
653 kernel_power_off();
654 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700655 return firsterr;
656}
657
658module_init(scf_torture_init);
659module_exit(scf_torture_cleanup);