blob: 554a521ee235e80e593b32d930b0106e605ea671 [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.");
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.");
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070067torture_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.");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070069torture_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;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070086 long long n_single_ofl;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070087 long long n_single_wait;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070088 long long n_single_wait_ofl;
89 long long n_many;
90 long long n_many_wait;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -070091 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
Paul E. McKenney5022b8a2020-06-25 17:05:58 -070099// 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
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700120// 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
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700128// Use to wait for all threads to start.
129static atomic_t n_started;
130static atomic_t n_errs;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700131static atomic_t n_mb_in_errs;
132static atomic_t n_mb_out_errs;
133static atomic_t n_alloc_errs;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700134static bool scfdone;
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700135static char *bangstr = "";
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700136
Wei Yongjun9a52a5742020-07-02 09:56:50 -0700137static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700138
139// Print torture statistics. Caller must ensure serialization.
140static void scf_torture_stats_print(void)
141{
142 int cpu;
Paul E. McKenneydba31422020-06-30 16:13:37 -0700143 int i;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700144 long long invoked_count = 0;
145 bool isdone = READ_ONCE(scfdone);
Paul E. McKenneydba31422020-06-30 16:13:37 -0700146 struct scf_statistics scfs = {};
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700147
148 for_each_possible_cpu(cpu)
149 invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
Paul E. McKenneydba31422020-06-30 16:13:37 -0700150 for (i = 0; i < nthreads; i++) {
151 scfs.n_single += scf_stats_p[i].n_single;
152 scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
153 scfs.n_single_wait += scf_stats_p[i].n_single_wait;
154 scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl;
155 scfs.n_many += scf_stats_p[i].n_many;
156 scfs.n_many_wait += scf_stats_p[i].n_many_wait;
157 scfs.n_all += scf_stats_p[i].n_all;
158 scfs.n_all_wait += scf_stats_p[i].n_all_wait;
159 }
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700160 if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) ||
161 atomic_read(&n_mb_out_errs) || atomic_read(&n_alloc_errs))
162 bangstr = "!!! ";
163 pr_alert("%s %sscf_invoked_count %s: %lld single: %lld/%lld single_ofl: %lld/%lld many: %lld/%lld all: %lld/%lld ",
164 SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count,
Paul E. McKenneydba31422020-06-30 16:13:37 -0700165 scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
166 scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700167 torture_onoff_stats();
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700168 pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs),
169 atomic_read(&n_mb_in_errs), atomic_read(&n_mb_out_errs),
170 atomic_read(&n_alloc_errs));
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700171}
172
173// Periodically prints torture statistics, if periodic statistics printing
174// was specified via the stat_interval module parameter.
175static int
176scf_torture_stats(void *arg)
177{
178 VERBOSE_TOROUT_STRING("scf_torture_stats task started");
179 do {
180 schedule_timeout_interruptible(stat_interval * HZ);
181 scf_torture_stats_print();
182 torture_shutdown_absorb("scf_torture_stats");
183 } while (!torture_must_stop());
184 torture_kthread_stopping("scf_torture_stats");
185 return 0;
186}
187
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700188// Add a primitive to the scf_sel_array[].
189static void scf_sel_add(unsigned long weight, int prim, bool wait)
190{
191 struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len];
192
193 // If no weight, if array would overflow, if computing three-place
194 // percentages would overflow, or if the scf_prim_name[] array would
195 // overflow, don't bother. In the last three two cases, complain.
196 if (!weight ||
197 WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) ||
198 WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) ||
199 WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name)))
200 return;
201 scf_sel_totweight += weight;
202 scfsp->scfs_weight = scf_sel_totweight;
203 scfsp->scfs_prim = prim;
204 scfsp->scfs_wait = wait;
205 scf_sel_array_len++;
206}
207
208// Dump out weighting percentages for scf_prim_name[] array.
209static void scf_sel_dump(void)
210{
211 int i;
212 unsigned long oldw = 0;
213 struct scf_selector *scfsp;
214 unsigned long w;
215
216 for (i = 0; i < scf_sel_array_len; i++) {
217 scfsp = &scf_sel_array[i];
218 w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight;
219 pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000,
220 scf_prim_name[scfsp->scfs_prim],
221 scfsp->scfs_wait ? "wait" : "nowait");
222 oldw = scfsp->scfs_weight;
223 }
224}
225
226// Randomly pick a primitive and wait/nowait, based on weightings.
227static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp)
228{
229 int i;
230 unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1);
231
232 for (i = 0; i < scf_sel_array_len; i++)
233 if (scf_sel_array[i].scfs_weight >= w)
234 return &scf_sel_array[i];
235 WARN_ON_ONCE(1);
236 return &scf_sel_array[0];
237}
238
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700239// Update statistics and occasionally burn up mass quantities of CPU time,
240// if told to do so via scftorture.longwait. Otherwise, occasionally burn
241// a little bit.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700242static void scf_handler(void *scfc_in)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700243{
244 int i;
245 int j;
246 unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700247 struct scf_check *scfcp = scfc_in;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700248
Paul E. McKenney980205e2020-07-01 12:30:02 -0700249 if (likely(scfcp)) {
250 WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers.
251 if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in))))
252 atomic_inc(&n_mb_in_errs);
253 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700254 this_cpu_inc(scf_invoked_count);
255 if (longwait <= 0) {
256 if (!(r & 0xffc0))
257 udelay(r & 0x3f);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700258 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700259 }
260 if (r & 0xfff)
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700261 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700262 r = (r >> 12);
263 if (longwait <= 0) {
264 udelay((r & 0xff) + 1);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700265 goto out;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700266 }
267 r = r % longwait + 1;
268 for (i = 0; i < r; i++) {
269 for (j = 0; j < 1000; j++) {
270 udelay(1000);
271 cpu_relax();
272 }
273 }
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700274out:
275 if (unlikely(!scfcp))
276 return;
277 if (scfcp->scfc_wait)
278 WRITE_ONCE(scfcp->scfc_out, true);
279 else
280 kfree(scfcp);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700281}
282
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700283// As above, but check for correct CPU.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700284static void scf_handler_1(void *scfc_in)
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700285{
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700286 struct scf_check *scfcp = scfc_in;
287
288 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 -0700289 atomic_inc(&n_errs);
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700290 }
291 scf_handler(scfcp);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700292}
293
294// Randomly do an smp_call_function*() invocation.
295static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
296{
297 uintptr_t cpu;
Paul E. McKenney676e5462020-07-01 14:13:02 -0700298 int ret = 0;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700299 struct scf_check *scfcp = NULL;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700300 struct scf_selector *scfsp = scf_sel_rand(trsp);
301
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700302 if (use_cpus_read_lock)
303 cpus_read_lock();
304 else
305 preempt_disable();
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700306 if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700307 scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700308 if (WARN_ON_ONCE(!scfcp)) {
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700309 atomic_inc(&n_alloc_errs);
Paul E. McKenney4df55bd2020-07-09 13:58:32 -0700310 } else {
311 scfcp->scfc_cpu = -1;
312 scfcp->scfc_wait = scfsp->scfs_wait;
313 scfcp->scfc_out = false;
314 }
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700315 }
316 switch (scfsp->scfs_prim) {
317 case SCF_PRIM_SINGLE:
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700318 cpu = torture_random(trsp) % nr_cpu_ids;
319 if (scfsp->scfs_wait)
320 scfp->n_single_wait++;
321 else
322 scfp->n_single++;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700323 if (scfcp) {
324 scfcp->scfc_cpu = cpu;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700325 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700326 scfcp->scfc_in = true;
327 }
328 ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700329 if (ret) {
330 if (scfsp->scfs_wait)
331 scfp->n_single_wait_ofl++;
332 else
333 scfp->n_single_ofl++;
Paul E. McKenneyb93e21a2020-06-30 20:49:50 -0700334 kfree(scfcp);
Paul E. McKenney676e5462020-07-01 14:13:02 -0700335 scfcp = NULL;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700336 }
337 break;
338 case SCF_PRIM_MANY:
339 if (scfsp->scfs_wait)
340 scfp->n_many_wait++;
341 else
342 scfp->n_many++;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700343 if (scfcp) {
344 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney980205e2020-07-01 12:30:02 -0700345 scfcp->scfc_in = true;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700346 }
Paul E. McKenney980205e2020-07-01 12:30:02 -0700347 smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700348 break;
349 case SCF_PRIM_ALL:
350 if (scfsp->scfs_wait)
351 scfp->n_all_wait++;
352 else
353 scfp->n_all++;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700354 if (scfcp) {
355 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700356 scfcp->scfc_in = true;
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700357 }
Paul E. McKenney34e8c482020-07-01 13:49:06 -0700358 smp_call_function(scf_handler, scfcp, scfsp->scfs_wait);
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700359 break;
Paul E. McKenneyde77d4d2020-07-02 12:15:37 -0700360 default:
361 WARN_ON_ONCE(1);
362 if (scfcp)
363 scfcp->scfc_out = true;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700364 }
Paul E. McKenney676e5462020-07-01 14:13:02 -0700365 if (scfcp && scfsp->scfs_wait) {
Paul E. McKenney9e66bf02020-07-03 15:23:19 -0700366 if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp->scfs_prim == SCF_PRIM_SINGLE) &&
367 !scfcp->scfc_out))
Paul E. McKenney676e5462020-07-01 14:13:02 -0700368 atomic_inc(&n_mb_out_errs); // Leak rather than trash!
369 else
370 kfree(scfcp);
Paul E. McKenneyee7035d2020-07-01 16:38:16 -0700371 barrier(); // Prevent race-reduction compiler optimizations.
Paul E. McKenney676e5462020-07-01 14:13:02 -0700372 }
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700373 if (use_cpus_read_lock)
374 cpus_read_unlock();
375 else
376 preempt_enable();
377 if (!(torture_random(trsp) & 0xfff))
378 schedule_timeout_uninterruptible(1);
379}
380
381// SCF test kthread. Repeatedly does calls to members of the
382// smp_call_function() family of functions.
383static int scftorture_invoker(void *arg)
384{
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700385 int cpu;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700386 DEFINE_TORTURE_RANDOM(rand);
387 struct scf_statistics *scfp = (struct scf_statistics *)arg;
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700388 bool was_offline = false;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700389
390 VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700391 cpu = scfp->cpu % nr_cpu_ids;
392 set_cpus_allowed_ptr(current, cpumask_of(cpu));
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700393 set_user_nice(current, MAX_NICE);
394 if (holdoff)
395 schedule_timeout_interruptible(holdoff * HZ);
396
397 VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp->cpu, smp_processor_id());
398
399 // Make sure that the CPU is affinitized appropriately during testing.
400 WARN_ON_ONCE(smp_processor_id() != scfp->cpu);
401
402 if (!atomic_dec_return(&n_started))
403 while (atomic_read_acquire(&n_started)) {
404 if (torture_must_stop()) {
405 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
406 goto end;
407 }
408 schedule_timeout_uninterruptible(1);
409 }
410
411 VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
412
413 do {
414 scftorture_invoke_one(scfp, &rand);
Paul E. McKenneya7c072ef2020-07-02 14:15:33 -0700415 while (cpu_is_offline(cpu) && !torture_must_stop()) {
416 schedule_timeout_interruptible(HZ / 5);
417 was_offline = true;
418 }
419 if (was_offline) {
420 set_cpus_allowed_ptr(current, cpumask_of(cpu));
421 was_offline = false;
422 }
Paul E. McKenney65bd77f2020-07-23 15:53:02 -0700423 cond_resched();
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700424 } while (!torture_must_stop());
425
426 VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
427end:
428 torture_kthread_stopping("scftorture_invoker");
429 return 0;
430}
431
432static void
433scftorture_print_module_parms(const char *tag)
434{
435 pr_alert(SCFTORT_FLAG
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700436 "--- %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,
437 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);
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700438}
439
440static void scf_cleanup_handler(void *unused)
441{
442}
443
444static void scf_torture_cleanup(void)
445{
446 int i;
447
448 if (torture_cleanup_begin())
449 return;
450
451 WRITE_ONCE(scfdone, true);
452 if (nthreads)
453 for (i = 0; i < nthreads; i++)
454 torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
455 else
456 goto end;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700457 smp_call_function(scf_cleanup_handler, NULL, 0);
458 torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
459 scf_torture_stats_print(); // -After- the stats thread is stopped!
Paul E. McKenneydba31422020-06-30 16:13:37 -0700460 kfree(scf_stats_p); // -After- the last stats print has completed!
461 scf_stats_p = NULL;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700462
Paul E. McKenneydbf83b62020-07-01 16:06:22 -0700463 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 -0700464 scftorture_print_module_parms("End of test: FAILURE");
465 else if (torture_onoff_failures())
466 scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
467 else
468 scftorture_print_module_parms("End of test: SUCCESS");
469
470end:
471 torture_cleanup_end();
472}
473
474static int __init scf_torture_init(void)
475{
476 long i;
477 int firsterr = 0;
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700478 unsigned long weight_single1 = weight_single;
479 unsigned long weight_single_wait1 = weight_single_wait;
480 unsigned long weight_many1 = weight_many;
481 unsigned long weight_many_wait1 = weight_many_wait;
482 unsigned long weight_all1 = weight_all;
483 unsigned long weight_all_wait1 = weight_all_wait;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700484
485 if (!torture_init_begin(SCFTORT_STRING, verbose))
486 return -EBUSY;
487
488 scftorture_print_module_parms("Start of test");
489
490 if (weight_single == -1 && weight_single_wait == -1 &&
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700491 weight_many == -1 && weight_many_wait == -1 &&
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700492 weight_all == -1 && weight_all_wait == -1) {
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700493 weight_single1 = 2 * nr_cpu_ids;
494 weight_single_wait1 = 2 * nr_cpu_ids;
495 weight_many1 = 2;
496 weight_many_wait1 = 2;
497 weight_all1 = 1;
498 weight_all_wait1 = 1;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700499 } else {
500 if (weight_single == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700501 weight_single1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700502 if (weight_single_wait == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700503 weight_single_wait1 = 0;
504 if (weight_many == -1)
505 weight_many1 = 0;
506 if (weight_many_wait == -1)
507 weight_many_wait1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700508 if (weight_all == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700509 weight_all1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700510 if (weight_all_wait == -1)
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700511 weight_all_wait1 = 0;
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700512 }
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700513 if (weight_single1 == 0 && weight_single_wait1 == 0 &&
514 weight_many1 == 0 && weight_many_wait1 == 0 &&
515 weight_all1 == 0 && weight_all_wait1 == 0) {
516 VERBOSE_SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700517 firsterr = -EINVAL;
518 goto unwind;
519 }
Paul E. McKenney5022b8a2020-06-25 17:05:58 -0700520 scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
521 scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
522 scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
523 scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
524 scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
525 scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
526 scf_sel_dump();
Paul E. McKenneye9d338a2020-06-24 15:59:59 -0700527
528 if (onoff_interval > 0) {
529 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
530 if (firsterr)
531 goto unwind;
532 }
533 if (shutdown_secs > 0) {
534 firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
535 if (firsterr)
536 goto unwind;
537 }
538
539 // Worker tasks invoking smp_call_function().
540 if (nthreads < 0)
541 nthreads = num_online_cpus();
542 scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL);
543 if (!scf_stats_p) {
544 VERBOSE_SCFTORTOUT_ERRSTRING("out of memory");
545 firsterr = -ENOMEM;
546 goto unwind;
547 }
548
549 VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads\n", nthreads);
550
551 atomic_set(&n_started, nthreads);
552 for (i = 0; i < nthreads; i++) {
553 scf_stats_p[i].cpu = i;
554 firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
555 scf_stats_p[i].task);
556 if (firsterr)
557 goto unwind;
558 }
559 if (stat_interval > 0) {
560 firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
561 if (firsterr)
562 goto unwind;
563 }
564
565 torture_init_end();
566 return 0;
567
568unwind:
569 torture_init_end();
570 scf_torture_cleanup();
571 return firsterr;
572}
573
574module_init(scf_torture_init);
575module_exit(scf_torture_cleanup);