]>
Commit | Line | Data |
---|---|---|
30428ef5 KK |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Test module to generate lockups | |
4 | */ | |
5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
6 | ||
7 | #include <linux/kernel.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/delay.h> | |
10 | #include <linux/sched.h> | |
11 | #include <linux/sched/signal.h> | |
12 | #include <linux/sched/clock.h> | |
13 | #include <linux/cpu.h> | |
14 | #include <linux/nmi.h> | |
15 | #include <linux/mm.h> | |
16 | #include <linux/uaccess.h> | |
17 | ||
18 | static unsigned int time_secs; | |
19 | module_param(time_secs, uint, 0600); | |
20 | MODULE_PARM_DESC(time_secs, "lockup time in seconds, default 0"); | |
21 | ||
22 | static unsigned int time_nsecs; | |
23 | module_param(time_nsecs, uint, 0600); | |
24 | MODULE_PARM_DESC(time_nsecs, "nanoseconds part of lockup time, default 0"); | |
25 | ||
26 | static unsigned int cooldown_secs; | |
27 | module_param(cooldown_secs, uint, 0600); | |
28 | MODULE_PARM_DESC(cooldown_secs, "cooldown time between iterations in seconds, default 0"); | |
29 | ||
30 | static unsigned int cooldown_nsecs; | |
31 | module_param(cooldown_nsecs, uint, 0600); | |
32 | MODULE_PARM_DESC(cooldown_nsecs, "nanoseconds part of cooldown, default 0"); | |
33 | ||
34 | static unsigned int iterations = 1; | |
35 | module_param(iterations, uint, 0600); | |
36 | MODULE_PARM_DESC(iterations, "lockup iterations, default 1"); | |
37 | ||
38 | static bool all_cpus; | |
39 | module_param(all_cpus, bool, 0400); | |
40 | MODULE_PARM_DESC(all_cpus, "trigger lockup at all cpus at once"); | |
41 | ||
42 | static int wait_state; | |
43 | static char *state = "R"; | |
44 | module_param(state, charp, 0400); | |
45 | MODULE_PARM_DESC(state, "wait in 'R' running (default), 'D' uninterruptible, 'K' killable, 'S' interruptible state"); | |
46 | ||
47 | static bool use_hrtimer; | |
48 | module_param(use_hrtimer, bool, 0400); | |
49 | MODULE_PARM_DESC(use_hrtimer, "use high-resolution timer for sleeping"); | |
50 | ||
51 | static bool iowait; | |
52 | module_param(iowait, bool, 0400); | |
53 | MODULE_PARM_DESC(iowait, "account sleep time as iowait"); | |
54 | ||
55 | static bool lock_read; | |
56 | module_param(lock_read, bool, 0400); | |
57 | MODULE_PARM_DESC(lock_read, "lock read-write locks for read"); | |
58 | ||
59 | static bool lock_single; | |
60 | module_param(lock_single, bool, 0400); | |
61 | MODULE_PARM_DESC(lock_single, "acquire locks only at one cpu"); | |
62 | ||
63 | static bool reacquire_locks; | |
64 | module_param(reacquire_locks, bool, 0400); | |
65 | MODULE_PARM_DESC(reacquire_locks, "release and reacquire locks/irq/preempt between iterations"); | |
66 | ||
67 | static bool touch_softlockup; | |
68 | module_param(touch_softlockup, bool, 0600); | |
69 | MODULE_PARM_DESC(touch_softlockup, "touch soft-lockup watchdog between iterations"); | |
70 | ||
71 | static bool touch_hardlockup; | |
72 | module_param(touch_hardlockup, bool, 0600); | |
73 | MODULE_PARM_DESC(touch_hardlockup, "touch hard-lockup watchdog between iterations"); | |
74 | ||
75 | static bool call_cond_resched; | |
76 | module_param(call_cond_resched, bool, 0600); | |
77 | MODULE_PARM_DESC(call_cond_resched, "call cond_resched() between iterations"); | |
78 | ||
79 | static bool measure_lock_wait; | |
80 | module_param(measure_lock_wait, bool, 0400); | |
81 | MODULE_PARM_DESC(measure_lock_wait, "measure lock wait time"); | |
82 | ||
83 | static unsigned long lock_wait_threshold = ULONG_MAX; | |
84 | module_param(lock_wait_threshold, ulong, 0400); | |
85 | MODULE_PARM_DESC(lock_wait_threshold, "print lock wait time longer than this in nanoseconds, default off"); | |
86 | ||
87 | static bool test_disable_irq; | |
88 | module_param_named(disable_irq, test_disable_irq, bool, 0400); | |
89 | MODULE_PARM_DESC(disable_irq, "disable interrupts: generate hard-lockups"); | |
90 | ||
91 | static bool disable_softirq; | |
92 | module_param(disable_softirq, bool, 0400); | |
93 | MODULE_PARM_DESC(disable_softirq, "disable bottom-half irq handlers"); | |
94 | ||
95 | static bool disable_preempt; | |
96 | module_param(disable_preempt, bool, 0400); | |
97 | MODULE_PARM_DESC(disable_preempt, "disable preemption: generate soft-lockups"); | |
98 | ||
99 | static bool lock_rcu; | |
100 | module_param(lock_rcu, bool, 0400); | |
101 | MODULE_PARM_DESC(lock_rcu, "grab rcu_read_lock: generate rcu stalls"); | |
102 | ||
103 | static bool lock_mmap_sem; | |
104 | module_param(lock_mmap_sem, bool, 0400); | |
105 | MODULE_PARM_DESC(lock_mmap_sem, "lock mm->mmap_sem: block procfs interfaces"); | |
106 | ||
107 | static unsigned long lock_rwsem_ptr; | |
108 | module_param_unsafe(lock_rwsem_ptr, ulong, 0400); | |
109 | MODULE_PARM_DESC(lock_rwsem_ptr, "lock rw_semaphore at address"); | |
110 | ||
111 | static unsigned long lock_mutex_ptr; | |
112 | module_param_unsafe(lock_mutex_ptr, ulong, 0400); | |
113 | MODULE_PARM_DESC(lock_mutex_ptr, "lock mutex at address"); | |
114 | ||
115 | static unsigned long lock_spinlock_ptr; | |
116 | module_param_unsafe(lock_spinlock_ptr, ulong, 0400); | |
117 | MODULE_PARM_DESC(lock_spinlock_ptr, "lock spinlock at address"); | |
118 | ||
119 | static unsigned long lock_rwlock_ptr; | |
120 | module_param_unsafe(lock_rwlock_ptr, ulong, 0400); | |
121 | MODULE_PARM_DESC(lock_rwlock_ptr, "lock rwlock at address"); | |
122 | ||
123 | static unsigned int alloc_pages_nr; | |
124 | module_param_unsafe(alloc_pages_nr, uint, 0600); | |
125 | MODULE_PARM_DESC(alloc_pages_nr, "allocate and free pages under locks"); | |
126 | ||
127 | static unsigned int alloc_pages_order; | |
128 | module_param(alloc_pages_order, uint, 0400); | |
129 | MODULE_PARM_DESC(alloc_pages_order, "page order to allocate"); | |
130 | ||
131 | static gfp_t alloc_pages_gfp = GFP_KERNEL; | |
132 | module_param_unsafe(alloc_pages_gfp, uint, 0400); | |
133 | MODULE_PARM_DESC(alloc_pages_gfp, "allocate pages with this gfp_mask, default GFP_KERNEL"); | |
134 | ||
135 | static bool alloc_pages_atomic; | |
136 | module_param(alloc_pages_atomic, bool, 0400); | |
137 | MODULE_PARM_DESC(alloc_pages_atomic, "allocate pages with GFP_ATOMIC"); | |
138 | ||
139 | static bool reallocate_pages; | |
140 | module_param(reallocate_pages, bool, 0400); | |
141 | MODULE_PARM_DESC(reallocate_pages, "free and allocate pages between iterations"); | |
142 | ||
143 | static atomic_t alloc_pages_failed = ATOMIC_INIT(0); | |
144 | ||
145 | static atomic64_t max_lock_wait = ATOMIC64_INIT(0); | |
146 | ||
147 | static struct task_struct *main_task; | |
148 | static int master_cpu; | |
149 | ||
150 | static void test_lock(bool master, bool verbose) | |
151 | { | |
152 | u64 uninitialized_var(wait_start); | |
153 | ||
154 | if (measure_lock_wait) | |
155 | wait_start = local_clock(); | |
156 | ||
157 | if (lock_mutex_ptr && master) { | |
158 | if (verbose) | |
159 | pr_notice("lock mutex %ps\n", (void *)lock_mutex_ptr); | |
160 | mutex_lock((struct mutex *)lock_mutex_ptr); | |
161 | } | |
162 | ||
163 | if (lock_rwsem_ptr && master) { | |
164 | if (verbose) | |
165 | pr_notice("lock rw_semaphore %ps\n", | |
166 | (void *)lock_rwsem_ptr); | |
167 | if (lock_read) | |
168 | down_read((struct rw_semaphore *)lock_rwsem_ptr); | |
169 | else | |
170 | down_write((struct rw_semaphore *)lock_rwsem_ptr); | |
171 | } | |
172 | ||
173 | if (lock_mmap_sem && master) { | |
174 | if (verbose) | |
175 | pr_notice("lock mmap_sem pid=%d\n", main_task->pid); | |
176 | if (lock_read) | |
177 | down_read(&main_task->mm->mmap_sem); | |
178 | else | |
179 | down_write(&main_task->mm->mmap_sem); | |
180 | } | |
181 | ||
182 | if (test_disable_irq) | |
183 | local_irq_disable(); | |
184 | ||
185 | if (disable_softirq) | |
186 | local_bh_disable(); | |
187 | ||
188 | if (disable_preempt) | |
189 | preempt_disable(); | |
190 | ||
191 | if (lock_rcu) | |
192 | rcu_read_lock(); | |
193 | ||
194 | if (lock_spinlock_ptr && master) { | |
195 | if (verbose) | |
196 | pr_notice("lock spinlock %ps\n", | |
197 | (void *)lock_spinlock_ptr); | |
198 | spin_lock((spinlock_t *)lock_spinlock_ptr); | |
199 | } | |
200 | ||
201 | if (lock_rwlock_ptr && master) { | |
202 | if (verbose) | |
203 | pr_notice("lock rwlock %ps\n", | |
204 | (void *)lock_rwlock_ptr); | |
205 | if (lock_read) | |
206 | read_lock((rwlock_t *)lock_rwlock_ptr); | |
207 | else | |
208 | write_lock((rwlock_t *)lock_rwlock_ptr); | |
209 | } | |
210 | ||
211 | if (measure_lock_wait) { | |
212 | s64 cur_wait = local_clock() - wait_start; | |
213 | s64 max_wait = atomic64_read(&max_lock_wait); | |
214 | ||
215 | do { | |
216 | if (cur_wait < max_wait) | |
217 | break; | |
218 | max_wait = atomic64_cmpxchg(&max_lock_wait, | |
219 | max_wait, cur_wait); | |
220 | } while (max_wait != cur_wait); | |
221 | ||
222 | if (cur_wait > lock_wait_threshold) | |
223 | pr_notice_ratelimited("lock wait %lld ns\n", cur_wait); | |
224 | } | |
225 | } | |
226 | ||
227 | static void test_unlock(bool master, bool verbose) | |
228 | { | |
229 | if (lock_rwlock_ptr && master) { | |
230 | if (lock_read) | |
231 | read_unlock((rwlock_t *)lock_rwlock_ptr); | |
232 | else | |
233 | write_unlock((rwlock_t *)lock_rwlock_ptr); | |
234 | if (verbose) | |
235 | pr_notice("unlock rwlock %ps\n", | |
236 | (void *)lock_rwlock_ptr); | |
237 | } | |
238 | ||
239 | if (lock_spinlock_ptr && master) { | |
240 | spin_unlock((spinlock_t *)lock_spinlock_ptr); | |
241 | if (verbose) | |
242 | pr_notice("unlock spinlock %ps\n", | |
243 | (void *)lock_spinlock_ptr); | |
244 | } | |
245 | ||
246 | if (lock_rcu) | |
247 | rcu_read_unlock(); | |
248 | ||
249 | if (disable_preempt) | |
250 | preempt_enable(); | |
251 | ||
252 | if (disable_softirq) | |
253 | local_bh_enable(); | |
254 | ||
255 | if (test_disable_irq) | |
256 | local_irq_enable(); | |
257 | ||
258 | if (lock_mmap_sem && master) { | |
259 | if (lock_read) | |
260 | up_read(&main_task->mm->mmap_sem); | |
261 | else | |
262 | up_write(&main_task->mm->mmap_sem); | |
263 | if (verbose) | |
264 | pr_notice("unlock mmap_sem pid=%d\n", main_task->pid); | |
265 | } | |
266 | ||
267 | if (lock_rwsem_ptr && master) { | |
268 | if (lock_read) | |
269 | up_read((struct rw_semaphore *)lock_rwsem_ptr); | |
270 | else | |
271 | up_write((struct rw_semaphore *)lock_rwsem_ptr); | |
272 | if (verbose) | |
273 | pr_notice("unlock rw_semaphore %ps\n", | |
274 | (void *)lock_rwsem_ptr); | |
275 | } | |
276 | ||
277 | if (lock_mutex_ptr && master) { | |
278 | mutex_unlock((struct mutex *)lock_mutex_ptr); | |
279 | if (verbose) | |
280 | pr_notice("unlock mutex %ps\n", | |
281 | (void *)lock_mutex_ptr); | |
282 | } | |
283 | } | |
284 | ||
285 | static void test_alloc_pages(struct list_head *pages) | |
286 | { | |
287 | struct page *page; | |
288 | unsigned int i; | |
289 | ||
290 | for (i = 0; i < alloc_pages_nr; i++) { | |
291 | page = alloc_pages(alloc_pages_gfp, alloc_pages_order); | |
292 | if (!page) { | |
293 | atomic_inc(&alloc_pages_failed); | |
294 | break; | |
295 | } | |
296 | list_add(&page->lru, pages); | |
297 | } | |
298 | } | |
299 | ||
300 | static void test_free_pages(struct list_head *pages) | |
301 | { | |
302 | struct page *page, *next; | |
303 | ||
304 | list_for_each_entry_safe(page, next, pages, lru) | |
305 | __free_pages(page, alloc_pages_order); | |
306 | INIT_LIST_HEAD(pages); | |
307 | } | |
308 | ||
309 | static void test_wait(unsigned int secs, unsigned int nsecs) | |
310 | { | |
311 | if (wait_state == TASK_RUNNING) { | |
312 | if (secs) | |
313 | mdelay(secs * MSEC_PER_SEC); | |
314 | if (nsecs) | |
315 | ndelay(nsecs); | |
316 | return; | |
317 | } | |
318 | ||
319 | __set_current_state(wait_state); | |
320 | if (use_hrtimer) { | |
321 | ktime_t time; | |
322 | ||
323 | time = ns_to_ktime((u64)secs * NSEC_PER_SEC + nsecs); | |
324 | schedule_hrtimeout(&time, HRTIMER_MODE_REL); | |
325 | } else { | |
326 | schedule_timeout(secs * HZ + nsecs_to_jiffies(nsecs)); | |
327 | } | |
328 | } | |
329 | ||
330 | static void test_lockup(bool master) | |
331 | { | |
332 | u64 lockup_start = local_clock(); | |
333 | unsigned int iter = 0; | |
334 | LIST_HEAD(pages); | |
335 | ||
336 | pr_notice("Start on CPU%d\n", raw_smp_processor_id()); | |
337 | ||
338 | test_lock(master, true); | |
339 | ||
340 | test_alloc_pages(&pages); | |
341 | ||
342 | while (iter++ < iterations && !signal_pending(main_task)) { | |
343 | ||
344 | if (iowait) | |
345 | current->in_iowait = 1; | |
346 | ||
347 | test_wait(time_secs, time_nsecs); | |
348 | ||
349 | if (iowait) | |
350 | current->in_iowait = 0; | |
351 | ||
352 | if (reallocate_pages) | |
353 | test_free_pages(&pages); | |
354 | ||
355 | if (reacquire_locks) | |
356 | test_unlock(master, false); | |
357 | ||
358 | if (touch_softlockup) | |
359 | touch_softlockup_watchdog(); | |
360 | ||
361 | if (touch_hardlockup) | |
362 | touch_nmi_watchdog(); | |
363 | ||
364 | if (call_cond_resched) | |
365 | cond_resched(); | |
366 | ||
367 | test_wait(cooldown_secs, cooldown_nsecs); | |
368 | ||
369 | if (reacquire_locks) | |
370 | test_lock(master, false); | |
371 | ||
372 | if (reallocate_pages) | |
373 | test_alloc_pages(&pages); | |
374 | } | |
375 | ||
376 | pr_notice("Finish on CPU%d in %lld ns\n", raw_smp_processor_id(), | |
377 | local_clock() - lockup_start); | |
378 | ||
379 | test_free_pages(&pages); | |
380 | ||
381 | test_unlock(master, true); | |
382 | } | |
383 | ||
384 | DEFINE_PER_CPU(struct work_struct, test_works); | |
385 | ||
386 | static void test_work_fn(struct work_struct *work) | |
387 | { | |
388 | test_lockup(!lock_single || | |
389 | work == per_cpu_ptr(&test_works, master_cpu)); | |
390 | } | |
391 | ||
392 | static bool test_kernel_ptr(unsigned long addr, int size) | |
393 | { | |
394 | void *ptr = (void *)addr; | |
395 | char buf; | |
396 | ||
397 | if (!addr) | |
398 | return false; | |
399 | ||
400 | /* should be at least readable kernel address */ | |
401 | if (access_ok(ptr, 1) || | |
402 | access_ok(ptr + size - 1, 1) || | |
403 | probe_kernel_address(ptr, buf) || | |
404 | probe_kernel_address(ptr + size - 1, buf)) { | |
405 | pr_err("invalid kernel ptr: %#lx\n", addr); | |
406 | return true; | |
407 | } | |
408 | ||
409 | return false; | |
410 | } | |
411 | ||
412 | static bool __maybe_unused test_magic(unsigned long addr, int offset, | |
413 | unsigned int expected) | |
414 | { | |
415 | void *ptr = (void *)addr + offset; | |
416 | unsigned int magic = 0; | |
417 | ||
418 | if (!addr) | |
419 | return false; | |
420 | ||
421 | if (probe_kernel_address(ptr, magic) || magic != expected) { | |
422 | pr_err("invalid magic at %#lx + %#x = %#x, expected %#x\n", | |
423 | addr, offset, magic, expected); | |
424 | return true; | |
425 | } | |
426 | ||
427 | return false; | |
428 | } | |
429 | ||
430 | static int __init test_lockup_init(void) | |
431 | { | |
432 | u64 test_start = local_clock(); | |
433 | ||
434 | main_task = current; | |
435 | ||
436 | switch (state[0]) { | |
437 | case 'S': | |
438 | wait_state = TASK_INTERRUPTIBLE; | |
439 | break; | |
440 | case 'D': | |
441 | wait_state = TASK_UNINTERRUPTIBLE; | |
442 | break; | |
443 | case 'K': | |
444 | wait_state = TASK_KILLABLE; | |
445 | break; | |
446 | case 'R': | |
447 | wait_state = TASK_RUNNING; | |
448 | break; | |
449 | default: | |
450 | pr_err("unknown state=%s\n", state); | |
451 | return -EINVAL; | |
452 | } | |
453 | ||
454 | if (alloc_pages_atomic) | |
455 | alloc_pages_gfp = GFP_ATOMIC; | |
456 | ||
457 | if (test_kernel_ptr(lock_spinlock_ptr, sizeof(spinlock_t)) || | |
458 | test_kernel_ptr(lock_rwlock_ptr, sizeof(rwlock_t)) || | |
459 | test_kernel_ptr(lock_mutex_ptr, sizeof(struct mutex)) || | |
460 | test_kernel_ptr(lock_rwsem_ptr, sizeof(struct rw_semaphore))) | |
461 | return -EINVAL; | |
462 | ||
463 | #ifdef CONFIG_DEBUG_SPINLOCK | |
464 | if (test_magic(lock_spinlock_ptr, | |
465 | offsetof(spinlock_t, rlock.magic), | |
466 | SPINLOCK_MAGIC) || | |
467 | test_magic(lock_rwlock_ptr, | |
468 | offsetof(rwlock_t, magic), | |
469 | RWLOCK_MAGIC) || | |
470 | test_magic(lock_mutex_ptr, | |
471 | offsetof(struct mutex, wait_lock.rlock.magic), | |
472 | SPINLOCK_MAGIC) || | |
473 | test_magic(lock_rwsem_ptr, | |
474 | offsetof(struct rw_semaphore, wait_lock.magic), | |
475 | SPINLOCK_MAGIC)) | |
476 | return -EINVAL; | |
477 | #endif | |
478 | ||
479 | if ((wait_state != TASK_RUNNING || | |
480 | (call_cond_resched && !reacquire_locks) || | |
481 | (alloc_pages_nr && gfpflags_allow_blocking(alloc_pages_gfp))) && | |
482 | (test_disable_irq || disable_softirq || disable_preempt || | |
483 | lock_rcu || lock_spinlock_ptr || lock_rwlock_ptr)) { | |
484 | pr_err("refuse to sleep in atomic context\n"); | |
485 | return -EINVAL; | |
486 | } | |
487 | ||
488 | if (lock_mmap_sem && !main_task->mm) { | |
489 | pr_err("no mm to lock mmap_sem\n"); | |
490 | return -EINVAL; | |
491 | } | |
492 | ||
493 | pr_notice("START pid=%d time=%u +%u ns cooldown=%u +%u ns iteraions=%u state=%s %s%s%s%s%s%s%s%s%s%s%s\n", | |
494 | main_task->pid, time_secs, time_nsecs, | |
495 | cooldown_secs, cooldown_nsecs, iterations, state, | |
496 | all_cpus ? "all_cpus " : "", | |
497 | iowait ? "iowait " : "", | |
498 | test_disable_irq ? "disable_irq " : "", | |
499 | disable_softirq ? "disable_softirq " : "", | |
500 | disable_preempt ? "disable_preempt " : "", | |
501 | lock_rcu ? "lock_rcu " : "", | |
502 | lock_read ? "lock_read " : "", | |
503 | touch_softlockup ? "touch_softlockup " : "", | |
504 | touch_hardlockup ? "touch_hardlockup " : "", | |
505 | call_cond_resched ? "call_cond_resched " : "", | |
506 | reacquire_locks ? "reacquire_locks " : ""); | |
507 | ||
508 | if (alloc_pages_nr) | |
509 | pr_notice("ALLOCATE PAGES nr=%u order=%u gfp=%pGg %s\n", | |
510 | alloc_pages_nr, alloc_pages_order, &alloc_pages_gfp, | |
511 | reallocate_pages ? "reallocate_pages " : ""); | |
512 | ||
513 | if (all_cpus) { | |
514 | unsigned int cpu; | |
515 | ||
516 | cpus_read_lock(); | |
517 | ||
518 | preempt_disable(); | |
519 | master_cpu = smp_processor_id(); | |
520 | for_each_online_cpu(cpu) { | |
521 | INIT_WORK(per_cpu_ptr(&test_works, cpu), test_work_fn); | |
522 | queue_work_on(cpu, system_highpri_wq, | |
523 | per_cpu_ptr(&test_works, cpu)); | |
524 | } | |
525 | preempt_enable(); | |
526 | ||
527 | for_each_online_cpu(cpu) | |
528 | flush_work(per_cpu_ptr(&test_works, cpu)); | |
529 | ||
530 | cpus_read_unlock(); | |
531 | } else { | |
532 | test_lockup(true); | |
533 | } | |
534 | ||
535 | if (measure_lock_wait) | |
536 | pr_notice("Maximum lock wait: %lld ns\n", | |
537 | atomic64_read(&max_lock_wait)); | |
538 | ||
539 | if (alloc_pages_nr) | |
540 | pr_notice("Page allocation failed %u times\n", | |
541 | atomic_read(&alloc_pages_failed)); | |
542 | ||
543 | pr_notice("FINISH in %llu ns\n", local_clock() - test_start); | |
544 | ||
545 | if (signal_pending(main_task)) | |
546 | return -EINTR; | |
547 | ||
548 | return -EAGAIN; | |
549 | } | |
550 | module_init(test_lockup_init); | |
551 | ||
552 | MODULE_LICENSE("GPL"); | |
553 | MODULE_AUTHOR("Konstantin Khlebnikov <[email protected]>"); | |
554 | MODULE_DESCRIPTION("Test module to generate lockups"); |