xref: /linux/kernel/scftorture.c (revision 0a0d1d55dad570724bf8c7ea83409639cfb4be9b)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Torture test for smp_call_function() and friends.
4 //
5 // Copyright (C) Facebook, 2020.
6 //
7 // Author: Paul E. McKenney <paulmck@kernel.org>
8 
9 #define pr_fmt(fmt) fmt
10 
11 #include <linux/atomic.h>
12 #include <linux/bitops.h>
13 #include <linux/completion.h>
14 #include <linux/cpu.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/kthread.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/notifier.h>
25 #include <linux/percpu.h>
26 #include <linux/rcupdate.h>
27 #include <linux/rcupdate_trace.h>
28 #include <linux/reboot.h>
29 #include <linux/sched.h>
30 #include <linux/spinlock.h>
31 #include <linux/smp.h>
32 #include <linux/stat.h>
33 #include <linux/srcu.h>
34 #include <linux/slab.h>
35 #include <linux/torture.h>
36 #include <linux/types.h>
37 
38 #define SCFTORT_STRING "scftorture"
39 #define SCFTORT_FLAG SCFTORT_STRING ": "
40 
41 #define VERBOSE_SCFTORTOUT(s, x...) \
42 	do { if (verbose) pr_alert(SCFTORT_FLAG s "\n", ## x); } while (0)
43 
44 #define SCFTORTOUT_ERRSTRING(s, x...) pr_alert(SCFTORT_FLAG "!!! " s "\n", ## x)
45 
46 MODULE_DESCRIPTION("Torture tests on the smp_call_function() family of primitives");
47 MODULE_LICENSE("GPL");
48 MODULE_AUTHOR("Paul E. McKenney <paulmck@kernel.org>");
49 
50 // Wait until there are multiple CPUs before starting test.
51 torture_param(int, holdoff, IS_BUILTIN(CONFIG_SCF_TORTURE_TEST) ? 10 : 0,
52 	      "Holdoff time before test start (s)");
53 torture_param(int, longwait, 0, "Include ridiculously long waits? (seconds)");
54 torture_param(int, nthreads, -1, "# threads, defaults to -1 for all CPUs.");
55 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
56 torture_param(int, onoff_interval, 0, "Time between CPU hotplugs (s), 0=disable");
57 torture_param(int, shutdown_secs, 0, "Shutdown time (ms), <= zero to disable.");
58 torture_param(int, stat_interval, 60, "Number of seconds between stats printk()s.");
59 torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
60 torture_param(bool, use_cpus_read_lock, 0, "Use cpus_read_lock() to exclude CPU hotplug.");
61 torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
62 torture_param(int, weight_resched, -1, "Testing weight for resched_cpu() operations.");
63 torture_param(int, weight_single, -1, "Testing weight for single-CPU no-wait operations.");
64 torture_param(int, weight_single_rpc, -1, "Testing weight for single-CPU RPC operations.");
65 torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations.");
66 torture_param(int, weight_many, -1, "Testing weight for multi-CPU no-wait operations.");
67 torture_param(int, weight_many_wait, -1, "Testing weight for multi-CPU operations.");
68 torture_param(int, weight_all, -1, "Testing weight for all-CPU no-wait operations.");
69 torture_param(int, weight_all_wait, -1, "Testing weight for all-CPU operations.");
70 
71 static char *torture_type = "";
72 
73 #ifdef MODULE
74 # define SCFTORT_SHUTDOWN 0
75 #else
76 # define SCFTORT_SHUTDOWN 1
77 #endif
78 
79 torture_param(bool, shutdown, SCFTORT_SHUTDOWN, "Shutdown at end of torture test.");
80 
81 struct scf_statistics {
82 	struct task_struct *task;
83 	int cpu;
84 	long long n_resched;
85 	long long n_single;
86 	long long n_single_ofl;
87 	long long n_single_rpc;
88 	long long n_single_rpc_ofl;
89 	long long n_single_wait;
90 	long long n_single_wait_ofl;
91 	long long n_many;
92 	long long n_many_wait;
93 	long long n_all;
94 	long long n_all_wait;
95 };
96 
97 static struct scf_statistics *scf_stats_p;
98 static struct task_struct *scf_torture_stats_task;
99 static DEFINE_PER_CPU(long long, scf_invoked_count);
100 static DEFINE_PER_CPU(struct llist_head, scf_free_pool);
101 
102 // Data for random primitive selection
103 #define SCF_PRIM_RESCHED	0
104 #define SCF_PRIM_SINGLE		1
105 #define SCF_PRIM_SINGLE_RPC	2
106 #define SCF_PRIM_MANY		3
107 #define SCF_PRIM_ALL		4
108 #define SCF_NPRIMS		8 // Need wait and no-wait versions of each,
109 				  //  except for SCF_PRIM_RESCHED and
110 				  //  SCF_PRIM_SINGLE_RPC.
111 
112 static char *scf_prim_name[] = {
113 	"resched_cpu",
114 	"smp_call_function_single",
115 	"smp_call_function_single_rpc",
116 	"smp_call_function_many",
117 	"smp_call_function",
118 };
119 
120 struct scf_selector {
121 	unsigned long scfs_weight;
122 	int scfs_prim;
123 	bool scfs_wait;
124 };
125 static struct scf_selector scf_sel_array[SCF_NPRIMS];
126 static int scf_sel_array_len;
127 static unsigned long scf_sel_totweight;
128 
129 // Communicate between caller and handler.
130 struct scf_check {
131 	bool scfc_in;
132 	bool scfc_out;
133 	int scfc_cpu; // -1 for not _single().
134 	bool scfc_wait;
135 	bool scfc_rpc;
136 	struct completion scfc_completion;
137 	struct llist_node scf_node;
138 };
139 
140 // Use to wait for all threads to start.
141 static atomic_t n_started;
142 static atomic_t n_errs;
143 static atomic_t n_mb_in_errs;
144 static atomic_t n_mb_out_errs;
145 static atomic_t n_alloc_errs;
146 static bool scfdone;
147 static char *bangstr = "";
148 
149 static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
150 
151 extern void resched_cpu(int cpu); // An alternative IPI vector.
152 
scf_add_to_free_list(struct scf_check * scfcp)153 static void scf_add_to_free_list(struct scf_check *scfcp)
154 {
155 	struct llist_head *pool;
156 	unsigned int cpu;
157 
158 	if (!scfcp)
159 		return;
160 	cpu = raw_smp_processor_id() % nthreads;
161 	pool = &per_cpu(scf_free_pool, cpu);
162 	llist_add(&scfcp->scf_node, pool);
163 }
164 
scf_cleanup_free_list(unsigned int cpu)165 static void scf_cleanup_free_list(unsigned int cpu)
166 {
167 	struct llist_head *pool;
168 	struct llist_node *node;
169 	struct scf_check *scfcp;
170 
171 	pool = &per_cpu(scf_free_pool, cpu);
172 	node = llist_del_all(pool);
173 	while (node) {
174 		scfcp = llist_entry(node, struct scf_check, scf_node);
175 		node = node->next;
176 		kfree(scfcp);
177 	}
178 }
179 
180 // Print torture statistics.  Caller must ensure serialization.
scf_torture_stats_print(void)181 static void scf_torture_stats_print(void)
182 {
183 	int cpu;
184 	int i;
185 	long long invoked_count = 0;
186 	bool isdone = READ_ONCE(scfdone);
187 	struct scf_statistics scfs = {};
188 
189 	for_each_possible_cpu(cpu)
190 		invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
191 	for (i = 0; i < nthreads; i++) {
192 		scfs.n_resched += scf_stats_p[i].n_resched;
193 		scfs.n_single += scf_stats_p[i].n_single;
194 		scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
195 		scfs.n_single_rpc += scf_stats_p[i].n_single_rpc;
196 		scfs.n_single_rpc_ofl += scf_stats_p[i].n_single_rpc_ofl;
197 		scfs.n_single_wait += scf_stats_p[i].n_single_wait;
198 		scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl;
199 		scfs.n_many += scf_stats_p[i].n_many;
200 		scfs.n_many_wait += scf_stats_p[i].n_many_wait;
201 		scfs.n_all += scf_stats_p[i].n_all;
202 		scfs.n_all_wait += scf_stats_p[i].n_all_wait;
203 	}
204 	if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) ||
205 	    atomic_read(&n_mb_out_errs) ||
206 	    (!IS_ENABLED(CONFIG_KASAN) && atomic_read(&n_alloc_errs)))
207 		bangstr = "!!! ";
208 	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 ",
209 		 SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count, scfs.n_resched,
210 		 scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
211 		 scfs.n_single_rpc, scfs.n_single_rpc_ofl,
212 		 scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
213 	torture_onoff_stats();
214 	pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs),
215 		atomic_read(&n_mb_in_errs), atomic_read(&n_mb_out_errs),
216 		atomic_read(&n_alloc_errs));
217 }
218 
219 // Periodically prints torture statistics, if periodic statistics printing
220 // was specified via the stat_interval module parameter.
221 static int
scf_torture_stats(void * arg)222 scf_torture_stats(void *arg)
223 {
224 	VERBOSE_TOROUT_STRING("scf_torture_stats task started");
225 	do {
226 		schedule_timeout_interruptible(stat_interval * HZ);
227 		scf_torture_stats_print();
228 		torture_shutdown_absorb("scf_torture_stats");
229 	} while (!torture_must_stop());
230 	torture_kthread_stopping("scf_torture_stats");
231 	return 0;
232 }
233 
234 // Add a primitive to the scf_sel_array[].
scf_sel_add(unsigned long weight,int prim,bool wait)235 static void scf_sel_add(unsigned long weight, int prim, bool wait)
236 {
237 	struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len];
238 
239 	// If no weight, if array would overflow, if computing three-place
240 	// percentages would overflow, or if the scf_prim_name[] array would
241 	// overflow, don't bother.  In the last three two cases, complain.
242 	if (!weight ||
243 	    WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) ||
244 	    WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) ||
245 	    WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name)))
246 		return;
247 	scf_sel_totweight += weight;
248 	scfsp->scfs_weight = scf_sel_totweight;
249 	scfsp->scfs_prim = prim;
250 	scfsp->scfs_wait = wait;
251 	scf_sel_array_len++;
252 }
253 
254 // Dump out weighting percentages for scf_prim_name[] array.
scf_sel_dump(void)255 static void scf_sel_dump(void)
256 {
257 	int i;
258 	unsigned long oldw = 0;
259 	struct scf_selector *scfsp;
260 	unsigned long w;
261 
262 	for (i = 0; i < scf_sel_array_len; i++) {
263 		scfsp = &scf_sel_array[i];
264 		w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight;
265 		pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000,
266 			scf_prim_name[scfsp->scfs_prim],
267 			scfsp->scfs_wait ? "wait" : "nowait");
268 		oldw = scfsp->scfs_weight;
269 	}
270 }
271 
272 // Randomly pick a primitive and wait/nowait, based on weightings.
scf_sel_rand(struct torture_random_state * trsp)273 static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp)
274 {
275 	int i;
276 	unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1);
277 
278 	for (i = 0; i < scf_sel_array_len; i++)
279 		if (scf_sel_array[i].scfs_weight >= w)
280 			return &scf_sel_array[i];
281 	WARN_ON_ONCE(1);
282 	return &scf_sel_array[0];
283 }
284 
285 // Update statistics and occasionally burn up mass quantities of CPU time,
286 // if told to do so via scftorture.longwait.  Otherwise, occasionally burn
287 // a little bit.
scf_handler(void * scfc_in)288 static void scf_handler(void *scfc_in)
289 {
290 	int i;
291 	int j;
292 	unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
293 	struct scf_check *scfcp = scfc_in;
294 
295 	if (likely(scfcp)) {
296 		WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers.
297 		if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in))))
298 			atomic_inc(&n_mb_in_errs);
299 	}
300 	this_cpu_inc(scf_invoked_count);
301 	if (longwait <= 0) {
302 		if (!(r & 0xffc0)) {
303 			udelay(r & 0x3f);
304 			goto out;
305 		}
306 	}
307 	if (r & 0xfff)
308 		goto out;
309 	r = (r >> 12);
310 	if (longwait <= 0) {
311 		udelay((r & 0xff) + 1);
312 		goto out;
313 	}
314 	r = r % longwait + 1;
315 	for (i = 0; i < r; i++) {
316 		for (j = 0; j < 1000; j++) {
317 			udelay(1000);
318 			cpu_relax();
319 		}
320 	}
321 out:
322 	if (unlikely(!scfcp))
323 		return;
324 	if (scfcp->scfc_wait) {
325 		WRITE_ONCE(scfcp->scfc_out, true);
326 		if (scfcp->scfc_rpc)
327 			complete(&scfcp->scfc_completion);
328 	} else {
329 		scf_add_to_free_list(scfcp);
330 	}
331 }
332 
333 // As above, but check for correct CPU.
scf_handler_1(void * scfc_in)334 static void scf_handler_1(void *scfc_in)
335 {
336 	struct scf_check *scfcp = scfc_in;
337 
338 	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())) {
339 		atomic_inc(&n_errs);
340 	}
341 	scf_handler(scfcp);
342 }
343 
344 // Randomly do an smp_call_function*() invocation.
scftorture_invoke_one(struct scf_statistics * scfp,struct torture_random_state * trsp)345 static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
346 {
347 	bool allocfail = false;
348 	uintptr_t cpu;
349 	int ret = 0;
350 	struct scf_check *scfcp = NULL;
351 	struct scf_selector *scfsp = scf_sel_rand(trsp);
352 	bool is_single = (scfsp->scfs_prim == SCF_PRIM_SINGLE ||
353 			  scfsp->scfs_prim == SCF_PRIM_SINGLE_RPC);
354 
355 	if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
356 		scfcp = kmalloc_obj(*scfcp, GFP_ATOMIC);
357 		if (!scfcp) {
358 			WARN_ON_ONCE(!IS_ENABLED(CONFIG_KASAN));
359 			atomic_inc(&n_alloc_errs);
360 			allocfail = true;
361 		} else {
362 			scfcp->scfc_cpu = -1;
363 			scfcp->scfc_wait = scfsp->scfs_wait;
364 			scfcp->scfc_out = false;
365 			scfcp->scfc_rpc = false;
366 		}
367 	}
368 	if (use_cpus_read_lock)
369 		cpus_read_lock();
370 	switch (scfsp->scfs_prim) {
371 	case SCF_PRIM_RESCHED:
372 		if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) {
373 			cpu = torture_random(trsp) % nr_cpu_ids;
374 			scfp->n_resched++;
375 			resched_cpu(cpu);
376 			this_cpu_inc(scf_invoked_count);
377 		}
378 		break;
379 	case SCF_PRIM_SINGLE:
380 		cpu = torture_random(trsp) % nr_cpu_ids;
381 		if (scfsp->scfs_wait)
382 			scfp->n_single_wait++;
383 		else
384 			scfp->n_single++;
385 		if (scfcp) {
386 			scfcp->scfc_cpu = cpu;
387 			barrier(); // Prevent race-reduction compiler optimizations.
388 			scfcp->scfc_in = true;
389 		}
390 		ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
391 		if (ret) {
392 			if (scfsp->scfs_wait)
393 				scfp->n_single_wait_ofl++;
394 			else
395 				scfp->n_single_ofl++;
396 			scf_add_to_free_list(scfcp);
397 			scfcp = NULL;
398 		}
399 		break;
400 	case SCF_PRIM_SINGLE_RPC:
401 		if (!scfcp)
402 			break;
403 		cpu = torture_random(trsp) % nr_cpu_ids;
404 		scfp->n_single_rpc++;
405 		scfcp->scfc_cpu = cpu;
406 		scfcp->scfc_wait = true;
407 		init_completion(&scfcp->scfc_completion);
408 		scfcp->scfc_rpc = true;
409 		barrier(); // Prevent race-reduction compiler optimizations.
410 		scfcp->scfc_in = true;
411 		ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, 0);
412 		if (!ret) {
413 			if (use_cpus_read_lock)
414 				cpus_read_unlock();
415 
416 			wait_for_completion(&scfcp->scfc_completion);
417 			if (use_cpus_read_lock)
418 				cpus_read_lock();
419 		} else {
420 			scfp->n_single_rpc_ofl++;
421 			scf_add_to_free_list(scfcp);
422 			scfcp = NULL;
423 		}
424 		break;
425 	case SCF_PRIM_MANY:
426 		if (scfsp->scfs_wait)
427 			scfp->n_many_wait++;
428 		else
429 			scfp->n_many++;
430 		if (scfcp) {
431 			barrier(); // Prevent race-reduction compiler optimizations.
432 			scfcp->scfc_in = true;
433 		}
434 		smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
435 		break;
436 	case SCF_PRIM_ALL:
437 		if (scfsp->scfs_wait)
438 			scfp->n_all_wait++;
439 		else
440 			scfp->n_all++;
441 		if (scfcp) {
442 			barrier(); // Prevent race-reduction compiler optimizations.
443 			scfcp->scfc_in = true;
444 		}
445 		smp_call_function(scf_handler, scfcp, scfsp->scfs_wait);
446 		break;
447 	default:
448 		WARN_ON_ONCE(1);
449 		if (scfcp)
450 			scfcp->scfc_out = true;
451 	}
452 	if (scfcp && scfsp->scfs_wait) {
453 		if (WARN_ON_ONCE(((use_cpus_read_lock && num_online_cpus() > 1) || is_single) &&
454 				 !scfcp->scfc_out)) {
455 			pr_warn("%s: Memory-ordering failure, scfs_prim: %d.\n", __func__, scfsp->scfs_prim);
456 			atomic_inc(&n_mb_out_errs); // Leak rather than trash!
457 		} else {
458 			scf_add_to_free_list(scfcp);
459 		}
460 		barrier(); // Prevent race-reduction compiler optimizations.
461 	}
462 	if (use_cpus_read_lock)
463 		cpus_read_unlock();
464 	if (allocfail)
465 		schedule_timeout_idle((1 + longwait) * HZ);  // Let no-wait handlers complete.
466 	else if (!(torture_random(trsp) & 0xfff))
467 		schedule_timeout_uninterruptible(1);
468 }
469 
470 // SCF test kthread.  Repeatedly does calls to members of the
471 // smp_call_function() family of functions.
scftorture_invoker(void * arg)472 static int scftorture_invoker(void *arg)
473 {
474 	int cpu;
475 	int curcpu;
476 	DEFINE_TORTURE_RANDOM(rand);
477 	struct scf_statistics *scfp = (struct scf_statistics *)arg;
478 	bool was_offline = false;
479 
480 	VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
481 	cpu = scfp->cpu % nr_cpu_ids;
482 	WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(cpu)));
483 	set_user_nice(current, MAX_NICE);
484 	if (holdoff)
485 		schedule_timeout_interruptible(holdoff * HZ);
486 
487 	VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp->cpu, raw_smp_processor_id());
488 
489 	// Make sure that the CPU is affinitized appropriately during testing.
490 	curcpu = raw_smp_processor_id();
491 	WARN_ONCE(curcpu != cpu,
492 		  "%s: Wanted CPU %d, running on %d, nr_cpu_ids = %d\n",
493 		  __func__, scfp->cpu, curcpu, nr_cpu_ids);
494 
495 	if (atomic_dec_return(&n_started))
496 		while (atomic_read_acquire(&n_started)) {
497 			if (torture_must_stop()) {
498 				VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
499 				goto end;
500 			}
501 			schedule_timeout_uninterruptible(1);
502 		}
503 
504 	VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
505 
506 	do {
507 		scf_cleanup_free_list(cpu);
508 
509 		scftorture_invoke_one(scfp, &rand);
510 		while (cpu_is_offline(cpu) && !torture_must_stop()) {
511 			schedule_timeout_interruptible(HZ / 5);
512 			was_offline = true;
513 		}
514 		if (was_offline) {
515 			set_cpus_allowed_ptr(current, cpumask_of(cpu));
516 			was_offline = false;
517 		}
518 		cond_resched();
519 		stutter_wait("scftorture_invoker");
520 	} while (!torture_must_stop());
521 
522 	VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
523 end:
524 	torture_kthread_stopping("scftorture_invoker");
525 	return 0;
526 }
527 
528 static void
scftorture_print_module_parms(const char * tag)529 scftorture_print_module_parms(const char *tag)
530 {
531 	pr_alert(SCFTORT_FLAG
532 		 "--- %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,
533 		 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);
534 }
535 
scf_cleanup_handler(void * unused)536 static void scf_cleanup_handler(void *unused)
537 {
538 }
539 
scf_torture_cleanup(void)540 static void scf_torture_cleanup(void)
541 {
542 	int i;
543 
544 	if (torture_cleanup_begin())
545 		return;
546 
547 	WRITE_ONCE(scfdone, true);
548 	if (nthreads && scf_stats_p)
549 		for (i = 0; i < nthreads; i++)
550 			torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
551 	else
552 		goto end;
553 	smp_call_function(scf_cleanup_handler, NULL, 1);
554 	torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
555 	scf_torture_stats_print();  // -After- the stats thread is stopped!
556 	kfree(scf_stats_p);  // -After- the last stats print has completed!
557 	scf_stats_p = NULL;
558 
559 	for (i = 0; i < nr_cpu_ids; i++)
560 		scf_cleanup_free_list(i);
561 
562 	if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) || atomic_read(&n_mb_out_errs))
563 		scftorture_print_module_parms("End of test: FAILURE");
564 	else if (torture_onoff_failures())
565 		scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
566 	else
567 		scftorture_print_module_parms("End of test: SUCCESS");
568 
569 end:
570 	torture_cleanup_end();
571 }
572 
scf_torture_init(void)573 static int __init scf_torture_init(void)
574 {
575 	long i;
576 	int firsterr = 0;
577 	unsigned long weight_resched1 = weight_resched;
578 	unsigned long weight_single1 = weight_single;
579 	unsigned long weight_single_rpc1 = weight_single_rpc;
580 	unsigned long weight_single_wait1 = weight_single_wait;
581 	unsigned long weight_many1 = weight_many;
582 	unsigned long weight_many_wait1 = weight_many_wait;
583 	unsigned long weight_all1 = weight_all;
584 	unsigned long weight_all_wait1 = weight_all_wait;
585 
586 	if (!torture_init_begin(SCFTORT_STRING, verbose))
587 		return -EBUSY;
588 
589 	scftorture_print_module_parms("Start of test");
590 
591 	if (weight_resched <= 0 &&
592 	    weight_single <= 0 && weight_single_rpc <= 0 && weight_single_wait <= 0 &&
593 	    weight_many <= 0 && weight_many_wait <= 0 &&
594 	    weight_all <= 0 && weight_all_wait <= 0) {
595 		weight_resched1 = weight_resched == 0 ? 0 : 2 * nr_cpu_ids;
596 		weight_single1 = weight_single == 0 ? 0 : 2 * nr_cpu_ids;
597 		weight_single_rpc1 = weight_single_rpc == 0 ? 0 : 2 * nr_cpu_ids;
598 		weight_single_wait1 = weight_single_wait == 0 ? 0 : 2 * nr_cpu_ids;
599 		weight_many1 = weight_many == 0 ? 0 : 2;
600 		weight_many_wait1 = weight_many_wait == 0 ? 0 : 2;
601 		weight_all1 = weight_all == 0 ? 0 : 1;
602 		weight_all_wait1 = weight_all_wait == 0 ? 0 : 1;
603 	} else {
604 		if (weight_resched == -1)
605 			weight_resched1 = 0;
606 		if (weight_single == -1)
607 			weight_single1 = 0;
608 		if (weight_single_rpc == -1)
609 			weight_single_rpc1 = 0;
610 		if (weight_single_wait == -1)
611 			weight_single_wait1 = 0;
612 		if (weight_many == -1)
613 			weight_many1 = 0;
614 		if (weight_many_wait == -1)
615 			weight_many_wait1 = 0;
616 		if (weight_all == -1)
617 			weight_all1 = 0;
618 		if (weight_all_wait == -1)
619 			weight_all_wait1 = 0;
620 	}
621 	if (weight_resched1 == 0 && weight_single1 == 0 && weight_single_rpc1 == 0 &&
622 	    weight_single_wait1 == 0 && weight_many1 == 0 && weight_many_wait1 == 0 &&
623 	    weight_all1 == 0 && weight_all_wait1 == 0) {
624 		SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
625 		firsterr = -EINVAL;
626 		goto unwind;
627 	}
628 	if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST))
629 		scf_sel_add(weight_resched1, SCF_PRIM_RESCHED, false);
630 	else if (weight_resched1)
631 		SCFTORTOUT_ERRSTRING("built as module, weight_resched ignored");
632 	scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
633 	scf_sel_add(weight_single_rpc1, SCF_PRIM_SINGLE_RPC, true);
634 	scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
635 	scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
636 	scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
637 	scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
638 	scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
639 	scf_sel_dump();
640 
641 	if (onoff_interval > 0) {
642 		firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
643 		if (torture_init_error(firsterr))
644 			goto unwind;
645 	}
646 	if (shutdown_secs > 0) {
647 		firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
648 		if (torture_init_error(firsterr))
649 			goto unwind;
650 	}
651 	if (stutter > 0) {
652 		firsterr = torture_stutter_init(stutter, stutter);
653 		if (torture_init_error(firsterr))
654 			goto unwind;
655 	}
656 
657 	// Worker tasks invoking smp_call_function().
658 	if (nthreads < 0)
659 		nthreads = num_online_cpus();
660 	scf_stats_p = kzalloc_objs(scf_stats_p[0], nthreads);
661 	if (!scf_stats_p) {
662 		SCFTORTOUT_ERRSTRING("out of memory");
663 		firsterr = -ENOMEM;
664 		goto unwind;
665 	}
666 
667 	VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads", nthreads);
668 
669 	atomic_set(&n_started, nthreads);
670 	for (i = 0; i < nthreads; i++) {
671 		scf_stats_p[i].cpu = i;
672 		firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
673 						  scf_stats_p[i].task);
674 		if (torture_init_error(firsterr))
675 			goto unwind;
676 	}
677 	if (stat_interval > 0) {
678 		firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
679 		if (torture_init_error(firsterr))
680 			goto unwind;
681 	}
682 
683 	torture_init_end();
684 	return 0;
685 
686 unwind:
687 	torture_init_end();
688 	scf_torture_cleanup();
689 	if (shutdown_secs) {
690 		WARN_ON(!IS_MODULE(CONFIG_SCF_TORTURE_TEST));
691 		kernel_power_off();
692 	}
693 	return firsterr;
694 }
695 
696 module_init(scf_torture_init);
697 module_exit(scf_torture_cleanup);
698