blob: 29e8fc5d91a7bc457883cd9329d562d0d1eacbd3 [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.");
67torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations.");
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070068torture_param(int, weight_many, -1, "Testing weight for multi-CPU no-wait operations.");
69torture_param(int, weight_many_wait, -1, "Testing weight for multi-CPU operations.");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070070torture_param(int, weight_all, -1, "Testing weight for all-CPU no-wait operations.");
71torture_param(int, weight_all_wait, -1, "Testing weight for all-CPU operations.");
72
73char *torture_type = "";
74
75#ifdef MODULE
76# define SCFTORT_SHUTDOWN 0
77#else
78# define SCFTORT_SHUTDOWN 1
79#endif
80
81torture_param(bool, shutdown, SCFTORT_SHUTDOWN, "Shutdown at end of torture test.");
82
83struct scf_statistics {
84 struct task_struct *task;
85 int cpu;
Paul E. McKenney1ac78b42020-09-03 13:09:47 -070086 long long n_resched;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070087 long long n_single;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070088 long long n_single_ofl;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070089 long long n_single_wait;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070090 long long n_single_wait_ofl;
91 long long n_many;
92 long long n_many_wait;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070093 long long n_all;
94 long long n_all_wait;
95};
96
97static struct scf_statistics *scf_stats_p;
98static struct task_struct *scf_torture_stats_task;
99static DEFINE_PER_CPU(long long, scf_invoked_count);
100
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700101// Data for random primitive selection
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700102#define SCF_PRIM_RESCHED 0
103#define SCF_PRIM_SINGLE 1
104#define SCF_PRIM_MANY 2
105#define SCF_PRIM_ALL 3
106#define SCF_NPRIMS 7 // Need wait and no-wait versions of each,
107 // except for SCF_PRIM_RESCHED.
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700108
109static char *scf_prim_name[] = {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700110 "resched_cpu",
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700111 "smp_call_function_single",
112 "smp_call_function_many",
113 "smp_call_function",
114};
115
116struct scf_selector {
117 unsigned long scfs_weight;
118 int scfs_prim;
119 bool scfs_wait;
120};
121static struct scf_selector scf_sel_array[SCF_NPRIMS];
122static int scf_sel_array_len;
123static unsigned long scf_sel_totweight;
124
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700125// Communicate between caller and handler.
126struct scf_check {
127 bool scfc_in;
128 bool scfc_out;
129 int scfc_cpu; // -1 for not _single().
130 bool scfc_wait;
131};
132
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700133// Use to wait for all threads to start.
134static atomic_t n_started;
135static atomic_t n_errs;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700136static atomic_t n_mb_in_errs;
137static atomic_t n_mb_out_errs;
138static atomic_t n_alloc_errs;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700139static bool scfdone;
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700140static char *bangstr = "";
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700141
Wei Yongjun9a52a5742020-07-02 09:56:50 -0700142static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700143
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700144extern void resched_cpu(int cpu); // An alternative IPI vector.
145
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700146// Print torture statistics. Caller must ensure serialization.
147static void scf_torture_stats_print(void)
148{
149 int cpu;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700150 int i;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700151 long long invoked_count = 0;
152 bool isdone = READ_ONCE(scfdone);
Paul E. McKenneydba31422020-06-30 16:13:37 -0700153 struct scf_statistics scfs = {};
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700154
155 for_each_possible_cpu(cpu)
156 invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
Paul E. McKenneydba31422020-06-30 16:13:37 -0700157 for (i = 0; i < nthreads; i++) {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700158 scfs.n_resched += scf_stats_p[i].n_resched;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700159 scfs.n_single += scf_stats_p[i].n_single;
160 scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
161 scfs.n_single_wait += scf_stats_p[i].n_single_wait;
162 scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl;
163 scfs.n_many += scf_stats_p[i].n_many;
164 scfs.n_many_wait += scf_stats_p[i].n_many_wait;
165 scfs.n_all += scf_stats_p[i].n_all;
166 scfs.n_all_wait += scf_stats_p[i].n_all_wait;
167 }
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700168 if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) ||
169 atomic_read(&n_mb_out_errs) || atomic_read(&n_alloc_errs))
170 bangstr = "!!! ";
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700171 pr_alert("%s %sscf_invoked_count %s: %lld resched: %lld single: %lld/%lld single_ofl: %lld/%lld many: %lld/%lld all: %lld/%lld ",
172 SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count, scfs.n_resched,
Paul E. McKenneydba31422020-06-30 16:13:37 -0700173 scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
174 scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700175 torture_onoff_stats();
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700176 pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs),
177 atomic_read(&n_mb_in_errs), atomic_read(&n_mb_out_errs),
178 atomic_read(&n_alloc_errs));
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700179}
180
181// Periodically prints torture statistics, if periodic statistics printing
182// was specified via the stat_interval module parameter.
183static int
184scf_torture_stats(void *arg)
185{
186 VERBOSE_TOROUT_STRING("scf_torture_stats task started");
187 do {
188 schedule_timeout_interruptible(stat_interval * HZ);
189 scf_torture_stats_print();
190 torture_shutdown_absorb("scf_torture_stats");
191 } while (!torture_must_stop());
192 torture_kthread_stopping("scf_torture_stats");
193 return 0;
194}
195
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700196// Add a primitive to the scf_sel_array[].
197static void scf_sel_add(unsigned long weight, int prim, bool wait)
198{
199 struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len];
200
201 // If no weight, if array would overflow, if computing three-place
202 // percentages would overflow, or if the scf_prim_name[] array would
203 // overflow, don't bother. In the last three two cases, complain.
204 if (!weight ||
205 WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) ||
206 WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) ||
207 WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name)))
208 return;
209 scf_sel_totweight += weight;
210 scfsp->scfs_weight = scf_sel_totweight;
211 scfsp->scfs_prim = prim;
212 scfsp->scfs_wait = wait;
213 scf_sel_array_len++;
214}
215
216// Dump out weighting percentages for scf_prim_name[] array.
217static void scf_sel_dump(void)
218{
219 int i;
220 unsigned long oldw = 0;
221 struct scf_selector *scfsp;
222 unsigned long w;
223
224 for (i = 0; i < scf_sel_array_len; i++) {
225 scfsp = &scf_sel_array[i];
226 w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight;
227 pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000,
228 scf_prim_name[scfsp->scfs_prim],
229 scfsp->scfs_wait ? "wait" : "nowait");
230 oldw = scfsp->scfs_weight;
231 }
232}
233
234// Randomly pick a primitive and wait/nowait, based on weightings.
235static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp)
236{
237 int i;
238 unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1);
239
240 for (i = 0; i < scf_sel_array_len; i++)
241 if (scf_sel_array[i].scfs_weight >= w)
242 return &scf_sel_array[i];
243 WARN_ON_ONCE(1);
244 return &scf_sel_array[0];
245}
246
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700247// Update statistics and occasionally burn up mass quantities of CPU time,
248// if told to do so via scftorture.longwait. Otherwise, occasionally burn
249// a little bit.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700250static void scf_handler(void *scfc_in)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700251{
252 int i;
253 int j;
254 unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700255 struct scf_check *scfcp = scfc_in;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700256
Paul E. McKenney980205e2020-07-01 12:30:02 -0700257 if (likely(scfcp)) {
258 WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers.
259 if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in))))
260 atomic_inc(&n_mb_in_errs);
261 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700262 this_cpu_inc(scf_invoked_count);
263 if (longwait <= 0) {
264 if (!(r & 0xffc0))
265 udelay(r & 0x3f);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700266 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700267 }
268 if (r & 0xfff)
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700269 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700270 r = (r >> 12);
271 if (longwait <= 0) {
272 udelay((r & 0xff) + 1);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700273 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700274 }
275 r = r % longwait + 1;
276 for (i = 0; i < r; i++) {
277 for (j = 0; j < 1000; j++) {
278 udelay(1000);
279 cpu_relax();
280 }
281 }
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700282out:
283 if (unlikely(!scfcp))
284 return;
285 if (scfcp->scfc_wait)
286 WRITE_ONCE(scfcp->scfc_out, true);
287 else
288 kfree(scfcp);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700289}
290
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700291// As above, but check for correct CPU.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700292static void scf_handler_1(void *scfc_in)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700293{
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700294 struct scf_check *scfcp = scfc_in;
295
296 if (likely(scfcp) && WARN_ONCE(smp_processor_id() != scfcp->scfc_cpu, "%s: Wanted CPU %d got CPU %d\n", __func__, scfcp->scfc_cpu, smp_processor_id())) {
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700297 atomic_inc(&n_errs);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700298 }
299 scf_handler(scfcp);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700300}
301
302// Randomly do an smp_call_function*() invocation.
303static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
304{
305 uintptr_t cpu;
Paul E. McKenney676e5462020-07-01 14:13:02 -0700306 int ret = 0;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700307 struct scf_check *scfcp = NULL;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700308 struct scf_selector *scfsp = scf_sel_rand(trsp);
309
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700310 if (use_cpus_read_lock)
311 cpus_read_lock();
312 else
313 preempt_disable();
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700314 if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700315 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700316 if (WARN_ON_ONCE(!scfcp)) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700317 atomic_inc(&n_alloc_errs);
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700318 } else {
319 scfcp->scfc_cpu = -1;
320 scfcp->scfc_wait = scfsp->scfs_wait;
321 scfcp->scfc_out = false;
322 }
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700323 }
324 switch (scfsp->scfs_prim) {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700325 case SCF_PRIM_RESCHED:
326 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) {
327 cpu = torture_random(trsp) % nr_cpu_ids;
328 scfp->n_resched++;
329 resched_cpu(cpu);
330 }
331 break;
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700332 case SCF_PRIM_SINGLE:
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700333 cpu = torture_random(trsp) % nr_cpu_ids;
334 if (scfsp->scfs_wait)
335 scfp->n_single_wait++;
336 else
337 scfp->n_single++;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700338 if (scfcp) {
339 scfcp->scfc_cpu = cpu;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700340 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700341 scfcp->scfc_in = true;
342 }
343 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700344 if (ret) {
345 if (scfsp->scfs_wait)
346 scfp->n_single_wait_ofl++;
347 else
348 scfp->n_single_ofl++;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700349 kfree(scfcp);
Paul E. McKenney676e5462020-07-01 14:13:02 -0700350 scfcp = NULL;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700351 }
352 break;
353 case SCF_PRIM_MANY:
354 if (scfsp->scfs_wait)
355 scfp->n_many_wait++;
356 else
357 scfp->n_many++;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700358 if (scfcp) {
359 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney980205e2020-07-01 12:30:02 -0700360 scfcp->scfc_in = true;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700361 }
Paul E. McKenney980205e2020-07-01 12:30:02 -0700362 smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700363 break;
364 case SCF_PRIM_ALL:
365 if (scfsp->scfs_wait)
366 scfp->n_all_wait++;
367 else
368 scfp->n_all++;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700369 if (scfcp) {
370 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700371 scfcp->scfc_in = true;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700372 }
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700373 smp_call_function(scf_handler, scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700374 break;
Paul E. McKenneyde77d4d2020-07-02 12:15:37 -0700375 default:
376 WARN_ON_ONCE(1);
377 if (scfcp)
378 scfcp->scfc_out = true;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700379 }
Paul E. McKenney676e5462020-07-01 14:13:02 -0700380 if (scfcp && scfsp->scfs_wait) {
Paul E. McKenney9e66bf02020-07-03 15:23:19 -0700381 if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp->scfs_prim == SCF_PRIM_SINGLE) &&
382 !scfcp->scfc_out))
Paul E. McKenney676e5462020-07-01 14:13:02 -0700383 atomic_inc(&n_mb_out_errs); // Leak rather than trash!
384 else
385 kfree(scfcp);
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700386 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney676e5462020-07-01 14:13:02 -0700387 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700388 if (use_cpus_read_lock)
389 cpus_read_unlock();
390 else
391 preempt_enable();
392 if (!(torture_random(trsp) & 0xfff))
393 schedule_timeout_uninterruptible(1);
394}
395
396// SCF test kthread. Repeatedly does calls to members of the
397// smp_call_function() family of functions.
398static int scftorture_invoker(void *arg)
399{
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700400 int cpu;
Paul E. McKenneyf3ea9782020-11-11 10:12:05 -0800401 int curcpu;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700402 DEFINE_TORTURE_RANDOM(rand);
403 struct scf_statistics *scfp = (struct scf_statistics *)arg;
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700404 bool was_offline = false;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700405
406 VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700407 cpu = scfp->cpu % nr_cpu_ids;
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700408 WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(cpu)));
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700409 set_user_nice(current, MAX_NICE);
410 if (holdoff)
411 schedule_timeout_interruptible(holdoff * HZ);
412
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700413 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 -0700414
415 // Make sure that the CPU is affinitized appropriately during testing.
Paul E. McKenney22b6d142021-06-04 12:37:44 -0700416 curcpu = raw_smp_processor_id();
Paul E. McKenneyf3ea9782020-11-11 10:12:05 -0800417 WARN_ONCE(curcpu != scfp->cpu % nr_cpu_ids,
418 "%s: Wanted CPU %d, running on %d, nr_cpu_ids = %d\n",
419 __func__, scfp->cpu, curcpu, nr_cpu_ids);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700420
421 if (!atomic_dec_return(&n_started))
422 while (atomic_read_acquire(&n_started)) {
423 if (torture_must_stop()) {
424 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
425 goto end;
426 }
427 schedule_timeout_uninterruptible(1);
428 }
429
430 VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
431
432 do {
433 scftorture_invoke_one(scfp, &rand);
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700434 while (cpu_is_offline(cpu) && !torture_must_stop()) {
435 schedule_timeout_interruptible(HZ / 5);
436 was_offline = true;
437 }
438 if (was_offline) {
439 set_cpus_allowed_ptr(current, cpumask_of(cpu));
440 was_offline = false;
441 }
Paul E. McKenney65bd77f2020-07-23 15:53:02 -0700442 cond_resched();
Paul E. McKenney85558182020-09-24 12:11:57 -0700443 stutter_wait("scftorture_invoker");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700444 } while (!torture_must_stop());
445
446 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
447end:
448 torture_kthread_stopping("scftorture_invoker");
449 return 0;
450}
451
452static void
453scftorture_print_module_parms(const char *tag)
454{
455 pr_alert(SCFTORT_FLAG
Paul E. McKenney85558182020-09-24 12:11:57 -0700456 "--- %s: verbose=%d holdoff=%d longwait=%d nthreads=%d onoff_holdoff=%d onoff_interval=%d shutdown_secs=%d stat_interval=%d stutter=%d use_cpus_read_lock=%d, weight_resched=%d, weight_single=%d, weight_single_wait=%d, weight_many=%d, weight_many_wait=%d, weight_all=%d, weight_all_wait=%d\n", tag,
457 verbose, holdoff, longwait, nthreads, onoff_holdoff, onoff_interval, shutdown, stat_interval, stutter, use_cpus_read_lock, weight_resched, weight_single, weight_single_wait, weight_many, weight_many_wait, weight_all, weight_all_wait);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700458}
459
460static void scf_cleanup_handler(void *unused)
461{
462}
463
464static void scf_torture_cleanup(void)
465{
466 int i;
467
468 if (torture_cleanup_begin())
469 return;
470
471 WRITE_ONCE(scfdone, true);
472 if (nthreads)
473 for (i = 0; i < nthreads; i++)
474 torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
475 else
476 goto end;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700477 smp_call_function(scf_cleanup_handler, NULL, 0);
478 torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
479 scf_torture_stats_print(); // -After- the stats thread is stopped!
Paul E. McKenneydba31422020-06-30 16:13:37 -0700480 kfree(scf_stats_p); // -After- the last stats print has completed!
481 scf_stats_p = NULL;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700482
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700483 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 -0700484 scftorture_print_module_parms("End of test: FAILURE");
485 else if (torture_onoff_failures())
486 scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
487 else
488 scftorture_print_module_parms("End of test: SUCCESS");
489
490end:
491 torture_cleanup_end();
492}
493
494static int __init scf_torture_init(void)
495{
496 long i;
497 int firsterr = 0;
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700498 unsigned long weight_resched1 = weight_resched;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700499 unsigned long weight_single1 = weight_single;
500 unsigned long weight_single_wait1 = weight_single_wait;
501 unsigned long weight_many1 = weight_many;
502 unsigned long weight_many_wait1 = weight_many_wait;
503 unsigned long weight_all1 = weight_all;
504 unsigned long weight_all_wait1 = weight_all_wait;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700505
506 if (!torture_init_begin(SCFTORT_STRING, verbose))
507 return -EBUSY;
508
509 scftorture_print_module_parms("Start of test");
510
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700511 if (weight_resched == -1 && weight_single == -1 && weight_single_wait == -1 &&
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700512 weight_many == -1 && weight_many_wait == -1 &&
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700513 weight_all == -1 && weight_all_wait == -1) {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700514 weight_resched1 = 2 * nr_cpu_ids;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700515 weight_single1 = 2 * nr_cpu_ids;
516 weight_single_wait1 = 2 * nr_cpu_ids;
517 weight_many1 = 2;
518 weight_many_wait1 = 2;
519 weight_all1 = 1;
520 weight_all_wait1 = 1;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700521 } else {
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700522 if (weight_resched == -1)
523 weight_resched1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700524 if (weight_single == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700525 weight_single1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700526 if (weight_single_wait == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700527 weight_single_wait1 = 0;
528 if (weight_many == -1)
529 weight_many1 = 0;
530 if (weight_many_wait == -1)
531 weight_many_wait1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700532 if (weight_all == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700533 weight_all1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700534 if (weight_all_wait == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700535 weight_all_wait1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700536 }
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700537 if (weight_single1 == 0 && weight_single_wait1 == 0 &&
538 weight_many1 == 0 && weight_many_wait1 == 0 &&
539 weight_all1 == 0 && weight_all_wait1 == 0) {
540 VERBOSE_SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700541 firsterr = -EINVAL;
542 goto unwind;
543 }
Paul E. McKenney1ac78b42020-09-03 13:09:47 -0700544 if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST))
545 scf_sel_add(weight_resched1, SCF_PRIM_RESCHED, false);
546 else if (weight_resched1)
547 VERBOSE_SCFTORTOUT_ERRSTRING("built as module, weight_resched ignored");
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700548 scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
549 scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
550 scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
551 scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
552 scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
553 scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
554 scf_sel_dump();
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700555
556 if (onoff_interval > 0) {
557 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
558 if (firsterr)
559 goto unwind;
560 }
561 if (shutdown_secs > 0) {
562 firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
563 if (firsterr)
564 goto unwind;
565 }
Paul E. McKenney85558182020-09-24 12:11:57 -0700566 if (stutter > 0) {
567 firsterr = torture_stutter_init(stutter, stutter);
568 if (firsterr)
569 goto unwind;
570 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700571
572 // Worker tasks invoking smp_call_function().
573 if (nthreads < 0)
574 nthreads = num_online_cpus();
575 scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL);
576 if (!scf_stats_p) {
577 VERBOSE_SCFTORTOUT_ERRSTRING("out of memory");
578 firsterr = -ENOMEM;
579 goto unwind;
580 }
581
582 VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads\n", nthreads);
583
584 atomic_set(&n_started, nthreads);
585 for (i = 0; i < nthreads; i++) {
586 scf_stats_p[i].cpu = i;
587 firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
588 scf_stats_p[i].task);
589 if (firsterr)
590 goto unwind;
591 }
592 if (stat_interval > 0) {
593 firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
594 if (firsterr)
595 goto unwind;
596 }
597
598 torture_init_end();
599 return 0;
600
601unwind:
602 torture_init_end();
603 scf_torture_cleanup();
604 return firsterr;
605}
606
607module_init(scf_torture_init);
608module_exit(scf_torture_cleanup);