Commit | Line | Data |
---|---|---|
e9d338a0 PM |
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 | ||
e9d338a0 | 41 | #define VERBOSE_SCFTORTOUT(s, x...) \ |
71f6ea2a | 42 | do { if (verbose) pr_alert(SCFTORT_FLAG s "\n", ## x); } while (0) |
e9d338a0 | 43 | |
809da9bf | 44 | #define SCFTORTOUT_ERRSTRING(s, x...) pr_alert(SCFTORT_FLAG "!!! " s "\n", ## x) |
e9d338a0 | 45 | |
d68dc773 | 46 | MODULE_DESCRIPTION("Torture tests on the smp_call_function() family of primitives"); |
e9d338a0 PM |
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."); | |
85558182 | 59 | torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable"); |
e9d338a0 PM |
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"); | |
1ac78b49 | 62 | torture_param(int, weight_resched, -1, "Testing weight for resched_cpu() operations."); |
e9d338a0 | 63 | torture_param(int, weight_single, -1, "Testing weight for single-CPU no-wait operations."); |
9b9a8067 | 64 | torture_param(int, weight_single_rpc, -1, "Testing weight for single-CPU RPC operations."); |
e9d338a0 | 65 | torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations."); |
5022b8ac PM |
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."); | |
e9d338a0 PM |
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 | ||
d4641fa6 | 71 | static char *torture_type = ""; |
e9d338a0 PM |
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; | |
1ac78b49 | 84 | long long n_resched; |
e9d338a0 | 85 | long long n_single; |
5022b8ac | 86 | long long n_single_ofl; |
9b9a8067 PM |
87 | long long n_single_rpc; |
88 | long long n_single_rpc_ofl; | |
e9d338a0 | 89 | long long n_single_wait; |
5022b8ac PM |
90 | long long n_single_wait_ofl; |
91 | long long n_many; | |
92 | long long n_many_wait; | |
e9d338a0 PM |
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); | |
4788c861 | 100 | static DEFINE_PER_CPU(struct llist_head, scf_free_pool); |
e9d338a0 | 101 | |
5022b8ac | 102 | // Data for random primitive selection |
1ac78b49 PM |
103 | #define SCF_PRIM_RESCHED 0 |
104 | #define SCF_PRIM_SINGLE 1 | |
9b9a8067 PM |
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. | |
5022b8ac PM |
111 | |
112 | static char *scf_prim_name[] = { | |
1ac78b49 | 113 | "resched_cpu", |
5022b8ac | 114 | "smp_call_function_single", |
9b9a8067 | 115 | "smp_call_function_single_rpc", |
5022b8ac PM |
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 | ||
b93e21a5 PM |
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; | |
9b9a8067 PM |
135 | bool scfc_rpc; |
136 | struct completion scfc_completion; | |
4788c861 | 137 | struct llist_node scf_node; |
b93e21a5 PM |
138 | }; |
139 | ||
e9d338a0 PM |
140 | // Use to wait for all threads to start. |
141 | static atomic_t n_started; | |
142 | static atomic_t n_errs; | |
b93e21a5 PM |
143 | static atomic_t n_mb_in_errs; |
144 | static atomic_t n_mb_out_errs; | |
145 | static atomic_t n_alloc_errs; | |
e9d338a0 | 146 | static bool scfdone; |
dbf83b65 | 147 | static char *bangstr = ""; |
e9d338a0 | 148 | |
9a52a574 | 149 | static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand); |
e9d338a0 | 150 | |
1ac78b49 PM |
151 | extern void resched_cpu(int cpu); // An alternative IPI vector. |
152 | ||
4788c861 SAS |
153 | static void scf_add_to_free_list(struct scf_check *scfcp) |
154 | { | |
155 | struct llist_head *pool; | |
156 | unsigned int cpu; | |
157 | ||
f946cae8 SAS |
158 | if (!scfcp) |
159 | return; | |
4788c861 SAS |
160 | cpu = raw_smp_processor_id() % nthreads; |
161 | pool = &per_cpu(scf_free_pool, cpu); | |
162 | llist_add(&scfcp->scf_node, pool); | |
163 | } | |
164 | ||
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 | ||
e9d338a0 PM |
180 | // Print torture statistics. Caller must ensure serialization. |
181 | static void scf_torture_stats_print(void) | |
182 | { | |
183 | int cpu; | |
dba3142b | 184 | int i; |
e9d338a0 PM |
185 | long long invoked_count = 0; |
186 | bool isdone = READ_ONCE(scfdone); | |
dba3142b | 187 | struct scf_statistics scfs = {}; |
e9d338a0 PM |
188 | |
189 | for_each_possible_cpu(cpu) | |
190 | invoked_count += data_race(per_cpu(scf_invoked_count, cpu)); | |
dba3142b | 191 | for (i = 0; i < nthreads; i++) { |
1ac78b49 | 192 | scfs.n_resched += scf_stats_p[i].n_resched; |
dba3142b PM |
193 | scfs.n_single += scf_stats_p[i].n_single; |
194 | scfs.n_single_ofl += scf_stats_p[i].n_single_ofl; | |
9b9a8067 | 195 | scfs.n_single_rpc += scf_stats_p[i].n_single_rpc; |
dba3142b PM |
196 | scfs.n_single_wait += scf_stats_p[i].n_single_wait; |
197 | scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl; | |
198 | scfs.n_many += scf_stats_p[i].n_many; | |
199 | scfs.n_many_wait += scf_stats_p[i].n_many_wait; | |
200 | scfs.n_all += scf_stats_p[i].n_all; | |
201 | scfs.n_all_wait += scf_stats_p[i].n_all_wait; | |
202 | } | |
dbf83b65 | 203 | if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) || |
013608cd PM |
204 | atomic_read(&n_mb_out_errs) || |
205 | (!IS_ENABLED(CONFIG_KASAN) && atomic_read(&n_alloc_errs))) | |
dbf83b65 | 206 | bangstr = "!!! "; |
9b9a8067 | 207 | 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 ", |
1ac78b49 | 208 | SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count, scfs.n_resched, |
dba3142b | 209 | scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl, |
9b9a8067 | 210 | scfs.n_single_rpc, scfs.n_single_rpc_ofl, |
dba3142b | 211 | scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait); |
e9d338a0 | 212 | torture_onoff_stats(); |
dbf83b65 PM |
213 | pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs), |
214 | atomic_read(&n_mb_in_errs), atomic_read(&n_mb_out_errs), | |
215 | atomic_read(&n_alloc_errs)); | |
e9d338a0 PM |
216 | } |
217 | ||
218 | // Periodically prints torture statistics, if periodic statistics printing | |
219 | // was specified via the stat_interval module parameter. | |
220 | static int | |
221 | scf_torture_stats(void *arg) | |
222 | { | |
223 | VERBOSE_TOROUT_STRING("scf_torture_stats task started"); | |
224 | do { | |
225 | schedule_timeout_interruptible(stat_interval * HZ); | |
226 | scf_torture_stats_print(); | |
227 | torture_shutdown_absorb("scf_torture_stats"); | |
228 | } while (!torture_must_stop()); | |
229 | torture_kthread_stopping("scf_torture_stats"); | |
230 | return 0; | |
231 | } | |
232 | ||
5022b8ac PM |
233 | // Add a primitive to the scf_sel_array[]. |
234 | static void scf_sel_add(unsigned long weight, int prim, bool wait) | |
235 | { | |
236 | struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len]; | |
237 | ||
238 | // If no weight, if array would overflow, if computing three-place | |
239 | // percentages would overflow, or if the scf_prim_name[] array would | |
240 | // overflow, don't bother. In the last three two cases, complain. | |
241 | if (!weight || | |
242 | WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) || | |
243 | WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) || | |
244 | WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name))) | |
245 | return; | |
246 | scf_sel_totweight += weight; | |
247 | scfsp->scfs_weight = scf_sel_totweight; | |
248 | scfsp->scfs_prim = prim; | |
249 | scfsp->scfs_wait = wait; | |
250 | scf_sel_array_len++; | |
251 | } | |
252 | ||
253 | // Dump out weighting percentages for scf_prim_name[] array. | |
254 | static void scf_sel_dump(void) | |
255 | { | |
256 | int i; | |
257 | unsigned long oldw = 0; | |
258 | struct scf_selector *scfsp; | |
259 | unsigned long w; | |
260 | ||
261 | for (i = 0; i < scf_sel_array_len; i++) { | |
262 | scfsp = &scf_sel_array[i]; | |
263 | w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight; | |
264 | pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000, | |
265 | scf_prim_name[scfsp->scfs_prim], | |
266 | scfsp->scfs_wait ? "wait" : "nowait"); | |
267 | oldw = scfsp->scfs_weight; | |
268 | } | |
269 | } | |
270 | ||
271 | // Randomly pick a primitive and wait/nowait, based on weightings. | |
272 | static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp) | |
273 | { | |
274 | int i; | |
275 | unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1); | |
276 | ||
277 | for (i = 0; i < scf_sel_array_len; i++) | |
278 | if (scf_sel_array[i].scfs_weight >= w) | |
279 | return &scf_sel_array[i]; | |
280 | WARN_ON_ONCE(1); | |
281 | return &scf_sel_array[0]; | |
282 | } | |
283 | ||
e9d338a0 PM |
284 | // Update statistics and occasionally burn up mass quantities of CPU time, |
285 | // if told to do so via scftorture.longwait. Otherwise, occasionally burn | |
286 | // a little bit. | |
b93e21a5 | 287 | static void scf_handler(void *scfc_in) |
e9d338a0 PM |
288 | { |
289 | int i; | |
290 | int j; | |
291 | unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand)); | |
b93e21a5 | 292 | struct scf_check *scfcp = scfc_in; |
e9d338a0 | 293 | |
980205ee PM |
294 | if (likely(scfcp)) { |
295 | WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers. | |
296 | if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in)))) | |
297 | atomic_inc(&n_mb_in_errs); | |
298 | } | |
e9d338a0 PM |
299 | this_cpu_inc(scf_invoked_count); |
300 | if (longwait <= 0) { | |
8106bddb | 301 | if (!(r & 0xffc0)) { |
e9d338a0 | 302 | udelay(r & 0x3f); |
8106bddb PM |
303 | goto out; |
304 | } | |
e9d338a0 PM |
305 | } |
306 | if (r & 0xfff) | |
b93e21a5 | 307 | goto out; |
e9d338a0 PM |
308 | r = (r >> 12); |
309 | if (longwait <= 0) { | |
310 | udelay((r & 0xff) + 1); | |
b93e21a5 | 311 | goto out; |
e9d338a0 PM |
312 | } |
313 | r = r % longwait + 1; | |
314 | for (i = 0; i < r; i++) { | |
315 | for (j = 0; j < 1000; j++) { | |
316 | udelay(1000); | |
317 | cpu_relax(); | |
318 | } | |
319 | } | |
b93e21a5 PM |
320 | out: |
321 | if (unlikely(!scfcp)) | |
322 | return; | |
9b9a8067 | 323 | if (scfcp->scfc_wait) { |
b93e21a5 | 324 | WRITE_ONCE(scfcp->scfc_out, true); |
9b9a8067 PM |
325 | if (scfcp->scfc_rpc) |
326 | complete(&scfcp->scfc_completion); | |
327 | } else { | |
4788c861 | 328 | scf_add_to_free_list(scfcp); |
9b9a8067 | 329 | } |
e9d338a0 PM |
330 | } |
331 | ||
5022b8ac | 332 | // As above, but check for correct CPU. |
b93e21a5 | 333 | static void scf_handler_1(void *scfc_in) |
5022b8ac | 334 | { |
b93e21a5 PM |
335 | struct scf_check *scfcp = scfc_in; |
336 | ||
337 | 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())) { | |
5022b8ac | 338 | atomic_inc(&n_errs); |
b93e21a5 PM |
339 | } |
340 | scf_handler(scfcp); | |
5022b8ac PM |
341 | } |
342 | ||
e9d338a0 | 343 | // Randomly do an smp_call_function*() invocation. |
5022b8ac | 344 | static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp) |
e9d338a0 | 345 | { |
4a71be93 | 346 | bool allocfail = false; |
5022b8ac | 347 | uintptr_t cpu; |
676e5469 | 348 | int ret = 0; |
b93e21a5 | 349 | struct scf_check *scfcp = NULL; |
5022b8ac PM |
350 | struct scf_selector *scfsp = scf_sel_rand(trsp); |
351 | ||
34e8c483 | 352 | if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) { |
b93e21a5 | 353 | scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC); |
013608cd PM |
354 | if (!scfcp) { |
355 | WARN_ON_ONCE(!IS_ENABLED(CONFIG_KASAN)); | |
b93e21a5 | 356 | atomic_inc(&n_alloc_errs); |
4a71be93 | 357 | allocfail = true; |
4df55bdd PM |
358 | } else { |
359 | scfcp->scfc_cpu = -1; | |
360 | scfcp->scfc_wait = scfsp->scfs_wait; | |
361 | scfcp->scfc_out = false; | |
9b9a8067 | 362 | scfcp->scfc_rpc = false; |
4df55bdd | 363 | } |
34e8c483 | 364 | } |
64bdaf96 SAS |
365 | if (use_cpus_read_lock) |
366 | cpus_read_lock(); | |
367 | else | |
368 | preempt_disable(); | |
34e8c483 | 369 | switch (scfsp->scfs_prim) { |
1ac78b49 PM |
370 | case SCF_PRIM_RESCHED: |
371 | if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) { | |
372 | cpu = torture_random(trsp) % nr_cpu_ids; | |
373 | scfp->n_resched++; | |
374 | resched_cpu(cpu); | |
c3d0258d | 375 | this_cpu_inc(scf_invoked_count); |
1ac78b49 PM |
376 | } |
377 | break; | |
34e8c483 | 378 | case SCF_PRIM_SINGLE: |
5022b8ac PM |
379 | cpu = torture_random(trsp) % nr_cpu_ids; |
380 | if (scfsp->scfs_wait) | |
381 | scfp->n_single_wait++; | |
382 | else | |
383 | scfp->n_single++; | |
b93e21a5 PM |
384 | if (scfcp) { |
385 | scfcp->scfc_cpu = cpu; | |
ee7035d2 | 386 | barrier(); // Prevent race-reduction compiler optimizations. |
b93e21a5 PM |
387 | scfcp->scfc_in = true; |
388 | } | |
389 | ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait); | |
5022b8ac PM |
390 | if (ret) { |
391 | if (scfsp->scfs_wait) | |
392 | scfp->n_single_wait_ofl++; | |
393 | else | |
394 | scfp->n_single_ofl++; | |
4788c861 | 395 | scf_add_to_free_list(scfcp); |
676e5469 | 396 | scfcp = NULL; |
5022b8ac PM |
397 | } |
398 | break; | |
9b9a8067 PM |
399 | case SCF_PRIM_SINGLE_RPC: |
400 | if (!scfcp) | |
401 | break; | |
402 | cpu = torture_random(trsp) % nr_cpu_ids; | |
403 | scfp->n_single_rpc++; | |
404 | scfcp->scfc_cpu = cpu; | |
405 | scfcp->scfc_wait = true; | |
406 | init_completion(&scfcp->scfc_completion); | |
407 | scfcp->scfc_rpc = true; | |
408 | barrier(); // Prevent race-reduction compiler optimizations. | |
409 | scfcp->scfc_in = true; | |
410 | ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, 0); | |
411 | if (!ret) { | |
412 | if (use_cpus_read_lock) | |
413 | cpus_read_unlock(); | |
414 | else | |
415 | preempt_enable(); | |
416 | wait_for_completion(&scfcp->scfc_completion); | |
417 | if (use_cpus_read_lock) | |
418 | cpus_read_lock(); | |
419 | else | |
420 | preempt_disable(); | |
421 | } else { | |
422 | scfp->n_single_rpc_ofl++; | |
4788c861 | 423 | scf_add_to_free_list(scfcp); |
9b9a8067 PM |
424 | scfcp = NULL; |
425 | } | |
426 | break; | |
5022b8ac PM |
427 | case SCF_PRIM_MANY: |
428 | if (scfsp->scfs_wait) | |
429 | scfp->n_many_wait++; | |
430 | else | |
431 | scfp->n_many++; | |
ee7035d2 PM |
432 | if (scfcp) { |
433 | barrier(); // Prevent race-reduction compiler optimizations. | |
980205ee | 434 | scfcp->scfc_in = true; |
ee7035d2 | 435 | } |
980205ee | 436 | smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait); |
5022b8ac PM |
437 | break; |
438 | case SCF_PRIM_ALL: | |
439 | if (scfsp->scfs_wait) | |
440 | scfp->n_all_wait++; | |
441 | else | |
442 | scfp->n_all++; | |
ee7035d2 PM |
443 | if (scfcp) { |
444 | barrier(); // Prevent race-reduction compiler optimizations. | |
34e8c483 | 445 | scfcp->scfc_in = true; |
ee7035d2 | 446 | } |
34e8c483 | 447 | smp_call_function(scf_handler, scfcp, scfsp->scfs_wait); |
5022b8ac | 448 | break; |
de77d4da PM |
449 | default: |
450 | WARN_ON_ONCE(1); | |
451 | if (scfcp) | |
452 | scfcp->scfc_out = true; | |
5022b8ac | 453 | } |
676e5469 | 454 | if (scfcp && scfsp->scfs_wait) { |
9e66bf03 | 455 | if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp->scfs_prim == SCF_PRIM_SINGLE) && |
9b9a8067 PM |
456 | !scfcp->scfc_out)) { |
457 | pr_warn("%s: Memory-ordering failure, scfs_prim: %d.\n", __func__, scfsp->scfs_prim); | |
676e5469 | 458 | atomic_inc(&n_mb_out_errs); // Leak rather than trash! |
9b9a8067 | 459 | } else { |
4788c861 | 460 | scf_add_to_free_list(scfcp); |
9b9a8067 | 461 | } |
ee7035d2 | 462 | barrier(); // Prevent race-reduction compiler optimizations. |
676e5469 | 463 | } |
e9d338a0 PM |
464 | if (use_cpus_read_lock) |
465 | cpus_read_unlock(); | |
466 | else | |
467 | preempt_enable(); | |
4a71be93 PM |
468 | if (allocfail) |
469 | schedule_timeout_idle((1 + longwait) * HZ); // Let no-wait handlers complete. | |
470 | else if (!(torture_random(trsp) & 0xfff)) | |
e9d338a0 PM |
471 | schedule_timeout_uninterruptible(1); |
472 | } | |
473 | ||
474 | // SCF test kthread. Repeatedly does calls to members of the | |
475 | // smp_call_function() family of functions. | |
476 | static int scftorture_invoker(void *arg) | |
477 | { | |
a7c072ef | 478 | int cpu; |
f3ea978b | 479 | int curcpu; |
e9d338a0 PM |
480 | DEFINE_TORTURE_RANDOM(rand); |
481 | struct scf_statistics *scfp = (struct scf_statistics *)arg; | |
a7c072ef | 482 | bool was_offline = false; |
e9d338a0 PM |
483 | |
484 | VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu); | |
a7c072ef | 485 | cpu = scfp->cpu % nr_cpu_ids; |
22b6d149 | 486 | WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(cpu))); |
e9d338a0 PM |
487 | set_user_nice(current, MAX_NICE); |
488 | if (holdoff) | |
489 | schedule_timeout_interruptible(holdoff * HZ); | |
490 | ||
22b6d149 | 491 | VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp->cpu, raw_smp_processor_id()); |
e9d338a0 PM |
492 | |
493 | // Make sure that the CPU is affinitized appropriately during testing. | |
22b6d149 | 494 | curcpu = raw_smp_processor_id(); |
42eeb3b5 | 495 | WARN_ONCE(curcpu != cpu, |
f3ea978b PM |
496 | "%s: Wanted CPU %d, running on %d, nr_cpu_ids = %d\n", |
497 | __func__, scfp->cpu, curcpu, nr_cpu_ids); | |
e9d338a0 PM |
498 | |
499 | if (!atomic_dec_return(&n_started)) | |
500 | while (atomic_read_acquire(&n_started)) { | |
501 | if (torture_must_stop()) { | |
502 | VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu); | |
503 | goto end; | |
504 | } | |
505 | schedule_timeout_uninterruptible(1); | |
506 | } | |
507 | ||
508 | VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu); | |
509 | ||
510 | do { | |
4788c861 SAS |
511 | scf_cleanup_free_list(cpu); |
512 | ||
e9d338a0 | 513 | scftorture_invoke_one(scfp, &rand); |
a7c072ef PM |
514 | while (cpu_is_offline(cpu) && !torture_must_stop()) { |
515 | schedule_timeout_interruptible(HZ / 5); | |
516 | was_offline = true; | |
517 | } | |
518 | if (was_offline) { | |
519 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); | |
520 | was_offline = false; | |
521 | } | |
65bd77f5 | 522 | cond_resched(); |
85558182 | 523 | stutter_wait("scftorture_invoker"); |
e9d338a0 PM |
524 | } while (!torture_must_stop()); |
525 | ||
526 | VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu); | |
527 | end: | |
528 | torture_kthread_stopping("scftorture_invoker"); | |
529 | return 0; | |
530 | } | |
531 | ||
532 | static void | |
533 | scftorture_print_module_parms(const char *tag) | |
534 | { | |
535 | pr_alert(SCFTORT_FLAG | |
9b9a8067 PM |
536 | "--- %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, |
537 | 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); | |
e9d338a0 PM |
538 | } |
539 | ||
540 | static void scf_cleanup_handler(void *unused) | |
541 | { | |
542 | } | |
543 | ||
544 | static void scf_torture_cleanup(void) | |
545 | { | |
546 | int i; | |
547 | ||
548 | if (torture_cleanup_begin()) | |
549 | return; | |
550 | ||
551 | WRITE_ONCE(scfdone, true); | |
586e4d41 | 552 | if (nthreads && scf_stats_p) |
e9d338a0 PM |
553 | for (i = 0; i < nthreads; i++) |
554 | torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task); | |
555 | else | |
556 | goto end; | |
43082cd5 | 557 | smp_call_function(scf_cleanup_handler, NULL, 1); |
e9d338a0 PM |
558 | torture_stop_kthread(scf_torture_stats, scf_torture_stats_task); |
559 | scf_torture_stats_print(); // -After- the stats thread is stopped! | |
dba3142b PM |
560 | kfree(scf_stats_p); // -After- the last stats print has completed! |
561 | scf_stats_p = NULL; | |
e9d338a0 | 562 | |
4788c861 SAS |
563 | for (i = 0; i < nr_cpu_ids; i++) |
564 | scf_cleanup_free_list(i); | |
565 | ||
dbf83b65 | 566 | if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) || atomic_read(&n_mb_out_errs)) |
e9d338a0 PM |
567 | scftorture_print_module_parms("End of test: FAILURE"); |
568 | else if (torture_onoff_failures()) | |
569 | scftorture_print_module_parms("End of test: LOCK_HOTPLUG"); | |
570 | else | |
571 | scftorture_print_module_parms("End of test: SUCCESS"); | |
572 | ||
573 | end: | |
574 | torture_cleanup_end(); | |
575 | } | |
576 | ||
577 | static int __init scf_torture_init(void) | |
578 | { | |
579 | long i; | |
580 | int firsterr = 0; | |
1ac78b49 | 581 | unsigned long weight_resched1 = weight_resched; |
5022b8ac | 582 | unsigned long weight_single1 = weight_single; |
9b9a8067 | 583 | unsigned long weight_single_rpc1 = weight_single_rpc; |
5022b8ac PM |
584 | unsigned long weight_single_wait1 = weight_single_wait; |
585 | unsigned long weight_many1 = weight_many; | |
586 | unsigned long weight_many_wait1 = weight_many_wait; | |
587 | unsigned long weight_all1 = weight_all; | |
588 | unsigned long weight_all_wait1 = weight_all_wait; | |
e9d338a0 PM |
589 | |
590 | if (!torture_init_begin(SCFTORT_STRING, verbose)) | |
591 | return -EBUSY; | |
592 | ||
593 | scftorture_print_module_parms("Start of test"); | |
594 | ||
2f611d04 PM |
595 | if (weight_resched <= 0 && |
596 | weight_single <= 0 && weight_single_rpc <= 0 && weight_single_wait <= 0 && | |
597 | weight_many <= 0 && weight_many_wait <= 0 && | |
598 | weight_all <= 0 && weight_all_wait <= 0) { | |
599 | weight_resched1 = weight_resched == 0 ? 0 : 2 * nr_cpu_ids; | |
600 | weight_single1 = weight_single == 0 ? 0 : 2 * nr_cpu_ids; | |
601 | weight_single_rpc1 = weight_single_rpc == 0 ? 0 : 2 * nr_cpu_ids; | |
602 | weight_single_wait1 = weight_single_wait == 0 ? 0 : 2 * nr_cpu_ids; | |
603 | weight_many1 = weight_many == 0 ? 0 : 2; | |
604 | weight_many_wait1 = weight_many_wait == 0 ? 0 : 2; | |
605 | weight_all1 = weight_all == 0 ? 0 : 1; | |
606 | weight_all_wait1 = weight_all_wait == 0 ? 0 : 1; | |
e9d338a0 | 607 | } else { |
1ac78b49 PM |
608 | if (weight_resched == -1) |
609 | weight_resched1 = 0; | |
e9d338a0 | 610 | if (weight_single == -1) |
5022b8ac | 611 | weight_single1 = 0; |
9b9a8067 PM |
612 | if (weight_single_rpc == -1) |
613 | weight_single_rpc1 = 0; | |
e9d338a0 | 614 | if (weight_single_wait == -1) |
5022b8ac PM |
615 | weight_single_wait1 = 0; |
616 | if (weight_many == -1) | |
617 | weight_many1 = 0; | |
618 | if (weight_many_wait == -1) | |
619 | weight_many_wait1 = 0; | |
e9d338a0 | 620 | if (weight_all == -1) |
5022b8ac | 621 | weight_all1 = 0; |
e9d338a0 | 622 | if (weight_all_wait == -1) |
5022b8ac | 623 | weight_all_wait1 = 0; |
e9d338a0 | 624 | } |
da9366c6 PM |
625 | if (weight_resched1 == 0 && weight_single1 == 0 && weight_single_rpc1 == 0 && |
626 | weight_single_wait1 == 0 && weight_many1 == 0 && weight_many_wait1 == 0 && | |
5022b8ac | 627 | weight_all1 == 0 && weight_all_wait1 == 0) { |
809da9bf | 628 | SCFTORTOUT_ERRSTRING("all zero weights makes no sense"); |
e9d338a0 PM |
629 | firsterr = -EINVAL; |
630 | goto unwind; | |
631 | } | |
1ac78b49 PM |
632 | if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) |
633 | scf_sel_add(weight_resched1, SCF_PRIM_RESCHED, false); | |
634 | else if (weight_resched1) | |
809da9bf | 635 | SCFTORTOUT_ERRSTRING("built as module, weight_resched ignored"); |
5022b8ac | 636 | scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false); |
9b9a8067 | 637 | scf_sel_add(weight_single_rpc1, SCF_PRIM_SINGLE_RPC, true); |
5022b8ac PM |
638 | scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true); |
639 | scf_sel_add(weight_many1, SCF_PRIM_MANY, false); | |
640 | scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true); | |
641 | scf_sel_add(weight_all1, SCF_PRIM_ALL, false); | |
642 | scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true); | |
643 | scf_sel_dump(); | |
e9d338a0 PM |
644 | |
645 | if (onoff_interval > 0) { | |
646 | firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL); | |
f2bdf7dc | 647 | if (torture_init_error(firsterr)) |
e9d338a0 PM |
648 | goto unwind; |
649 | } | |
650 | if (shutdown_secs > 0) { | |
651 | firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup); | |
f2bdf7dc | 652 | if (torture_init_error(firsterr)) |
e9d338a0 PM |
653 | goto unwind; |
654 | } | |
85558182 PM |
655 | if (stutter > 0) { |
656 | firsterr = torture_stutter_init(stutter, stutter); | |
f2bdf7dc | 657 | if (torture_init_error(firsterr)) |
85558182 PM |
658 | goto unwind; |
659 | } | |
e9d338a0 PM |
660 | |
661 | // Worker tasks invoking smp_call_function(). | |
662 | if (nthreads < 0) | |
663 | nthreads = num_online_cpus(); | |
664 | scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL); | |
665 | if (!scf_stats_p) { | |
809da9bf | 666 | SCFTORTOUT_ERRSTRING("out of memory"); |
e9d338a0 PM |
667 | firsterr = -ENOMEM; |
668 | goto unwind; | |
669 | } | |
670 | ||
71f6ea2a | 671 | VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads", nthreads); |
e9d338a0 PM |
672 | |
673 | atomic_set(&n_started, nthreads); | |
674 | for (i = 0; i < nthreads; i++) { | |
675 | scf_stats_p[i].cpu = i; | |
676 | firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i], | |
677 | scf_stats_p[i].task); | |
f2bdf7dc | 678 | if (torture_init_error(firsterr)) |
e9d338a0 PM |
679 | goto unwind; |
680 | } | |
681 | if (stat_interval > 0) { | |
682 | firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task); | |
f2bdf7dc | 683 | if (torture_init_error(firsterr)) |
e9d338a0 PM |
684 | goto unwind; |
685 | } | |
686 | ||
687 | torture_init_end(); | |
688 | return 0; | |
689 | ||
690 | unwind: | |
691 | torture_init_end(); | |
692 | scf_torture_cleanup(); | |
2b1388f8 PM |
693 | if (shutdown_secs) { |
694 | WARN_ON(!IS_MODULE(CONFIG_SCF_TORTURE_TEST)); | |
695 | kernel_power_off(); | |
696 | } | |
e9d338a0 PM |
697 | return firsterr; |
698 | } | |
699 | ||
700 | module_init(scf_torture_init); | |
701 | module_exit(scf_torture_cleanup); |