1 // SPDX-License-Identifier: GPL-2.0
3 * Test module to generate lockups
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
16 #include <linux/uaccess.h>
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");
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");
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");
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");
34 static unsigned int iterations = 1;
35 module_param(iterations, uint, 0600);
36 MODULE_PARM_DESC(iterations, "lockup iterations, default 1");
39 module_param(all_cpus, bool, 0400);
40 MODULE_PARM_DESC(all_cpus, "trigger lockup at all cpus at once");
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");
47 static bool use_hrtimer;
48 module_param(use_hrtimer, bool, 0400);
49 MODULE_PARM_DESC(use_hrtimer, "use high-resolution timer for sleeping");
52 module_param(iowait, bool, 0400);
53 MODULE_PARM_DESC(iowait, "account sleep time as iowait");
55 static bool lock_read;
56 module_param(lock_read, bool, 0400);
57 MODULE_PARM_DESC(lock_read, "lock read-write locks for read");
59 static bool lock_single;
60 module_param(lock_single, bool, 0400);
61 MODULE_PARM_DESC(lock_single, "acquire locks only at one cpu");
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");
67 static bool touch_softlockup;
68 module_param(touch_softlockup, bool, 0600);
69 MODULE_PARM_DESC(touch_softlockup, "touch soft-lockup watchdog between iterations");
71 static bool touch_hardlockup;
72 module_param(touch_hardlockup, bool, 0600);
73 MODULE_PARM_DESC(touch_hardlockup, "touch hard-lockup watchdog between iterations");
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");
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");
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");
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");
91 static bool disable_softirq;
92 module_param(disable_softirq, bool, 0400);
93 MODULE_PARM_DESC(disable_softirq, "disable bottom-half irq handlers");
95 static bool disable_preempt;
96 module_param(disable_preempt, bool, 0400);
97 MODULE_PARM_DESC(disable_preempt, "disable preemption: generate soft-lockups");
100 module_param(lock_rcu, bool, 0400);
101 MODULE_PARM_DESC(lock_rcu, "grab rcu_read_lock: generate rcu stalls");
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");
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");
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");
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");
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");
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");
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");
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");
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");
139 static bool reallocate_pages;
140 module_param(reallocate_pages, bool, 0400);
141 MODULE_PARM_DESC(reallocate_pages, "free and allocate pages between iterations");
143 static atomic_t alloc_pages_failed = ATOMIC_INIT(0);
145 static atomic64_t max_lock_wait = ATOMIC64_INIT(0);
147 static struct task_struct *main_task;
148 static int master_cpu;
150 static void test_lock(bool master, bool verbose)
152 u64 uninitialized_var(wait_start);
154 if (measure_lock_wait)
155 wait_start = local_clock();
157 if (lock_mutex_ptr && master) {
159 pr_notice("lock mutex %ps\n", (void *)lock_mutex_ptr);
160 mutex_lock((struct mutex *)lock_mutex_ptr);
163 if (lock_rwsem_ptr && master) {
165 pr_notice("lock rw_semaphore %ps\n",
166 (void *)lock_rwsem_ptr);
168 down_read((struct rw_semaphore *)lock_rwsem_ptr);
170 down_write((struct rw_semaphore *)lock_rwsem_ptr);
173 if (lock_mmap_sem && master) {
175 pr_notice("lock mmap_sem pid=%d\n", main_task->pid);
177 down_read(&main_task->mm->mmap_sem);
179 down_write(&main_task->mm->mmap_sem);
182 if (test_disable_irq)
194 if (lock_spinlock_ptr && master) {
196 pr_notice("lock spinlock %ps\n",
197 (void *)lock_spinlock_ptr);
198 spin_lock((spinlock_t *)lock_spinlock_ptr);
201 if (lock_rwlock_ptr && master) {
203 pr_notice("lock rwlock %ps\n",
204 (void *)lock_rwlock_ptr);
206 read_lock((rwlock_t *)lock_rwlock_ptr);
208 write_lock((rwlock_t *)lock_rwlock_ptr);
211 if (measure_lock_wait) {
212 s64 cur_wait = local_clock() - wait_start;
213 s64 max_wait = atomic64_read(&max_lock_wait);
216 if (cur_wait < max_wait)
218 max_wait = atomic64_cmpxchg(&max_lock_wait,
220 } while (max_wait != cur_wait);
222 if (cur_wait > lock_wait_threshold)
223 pr_notice_ratelimited("lock wait %lld ns\n", cur_wait);
227 static void test_unlock(bool master, bool verbose)
229 if (lock_rwlock_ptr && master) {
231 read_unlock((rwlock_t *)lock_rwlock_ptr);
233 write_unlock((rwlock_t *)lock_rwlock_ptr);
235 pr_notice("unlock rwlock %ps\n",
236 (void *)lock_rwlock_ptr);
239 if (lock_spinlock_ptr && master) {
240 spin_unlock((spinlock_t *)lock_spinlock_ptr);
242 pr_notice("unlock spinlock %ps\n",
243 (void *)lock_spinlock_ptr);
255 if (test_disable_irq)
258 if (lock_mmap_sem && master) {
260 up_read(&main_task->mm->mmap_sem);
262 up_write(&main_task->mm->mmap_sem);
264 pr_notice("unlock mmap_sem pid=%d\n", main_task->pid);
267 if (lock_rwsem_ptr && master) {
269 up_read((struct rw_semaphore *)lock_rwsem_ptr);
271 up_write((struct rw_semaphore *)lock_rwsem_ptr);
273 pr_notice("unlock rw_semaphore %ps\n",
274 (void *)lock_rwsem_ptr);
277 if (lock_mutex_ptr && master) {
278 mutex_unlock((struct mutex *)lock_mutex_ptr);
280 pr_notice("unlock mutex %ps\n",
281 (void *)lock_mutex_ptr);
285 static void test_alloc_pages(struct list_head *pages)
290 for (i = 0; i < alloc_pages_nr; i++) {
291 page = alloc_pages(alloc_pages_gfp, alloc_pages_order);
293 atomic_inc(&alloc_pages_failed);
296 list_add(&page->lru, pages);
300 static void test_free_pages(struct list_head *pages)
302 struct page *page, *next;
304 list_for_each_entry_safe(page, next, pages, lru)
305 __free_pages(page, alloc_pages_order);
306 INIT_LIST_HEAD(pages);
309 static void test_wait(unsigned int secs, unsigned int nsecs)
311 if (wait_state == TASK_RUNNING) {
313 mdelay(secs * MSEC_PER_SEC);
319 __set_current_state(wait_state);
323 time = ns_to_ktime((u64)secs * NSEC_PER_SEC + nsecs);
324 schedule_hrtimeout(&time, HRTIMER_MODE_REL);
326 schedule_timeout(secs * HZ + nsecs_to_jiffies(nsecs));
330 static void test_lockup(bool master)
332 u64 lockup_start = local_clock();
333 unsigned int iter = 0;
336 pr_notice("Start on CPU%d\n", raw_smp_processor_id());
338 test_lock(master, true);
340 test_alloc_pages(&pages);
342 while (iter++ < iterations && !signal_pending(main_task)) {
345 current->in_iowait = 1;
347 test_wait(time_secs, time_nsecs);
350 current->in_iowait = 0;
352 if (reallocate_pages)
353 test_free_pages(&pages);
356 test_unlock(master, false);
358 if (touch_softlockup)
359 touch_softlockup_watchdog();
361 if (touch_hardlockup)
362 touch_nmi_watchdog();
364 if (call_cond_resched)
367 test_wait(cooldown_secs, cooldown_nsecs);
370 test_lock(master, false);
372 if (reallocate_pages)
373 test_alloc_pages(&pages);
376 pr_notice("Finish on CPU%d in %lld ns\n", raw_smp_processor_id(),
377 local_clock() - lockup_start);
379 test_free_pages(&pages);
381 test_unlock(master, true);
384 DEFINE_PER_CPU(struct work_struct, test_works);
386 static void test_work_fn(struct work_struct *work)
388 test_lockup(!lock_single ||
389 work == per_cpu_ptr(&test_works, master_cpu));
392 static bool test_kernel_ptr(unsigned long addr, int size)
394 void *ptr = (void *)addr;
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);
412 static bool __maybe_unused test_magic(unsigned long addr, int offset,
413 unsigned int expected)
415 void *ptr = (void *)addr + offset;
416 unsigned int magic = 0;
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);
430 static int __init test_lockup_init(void)
432 u64 test_start = local_clock();
438 wait_state = TASK_INTERRUPTIBLE;
441 wait_state = TASK_UNINTERRUPTIBLE;
444 wait_state = TASK_KILLABLE;
447 wait_state = TASK_RUNNING;
450 pr_err("unknown state=%s\n", state);
454 if (alloc_pages_atomic)
455 alloc_pages_gfp = GFP_ATOMIC;
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)))
463 #ifdef CONFIG_DEBUG_SPINLOCK
464 if (test_magic(lock_spinlock_ptr,
465 offsetof(spinlock_t, rlock.magic),
467 test_magic(lock_rwlock_ptr,
468 offsetof(rwlock_t, magic),
470 test_magic(lock_mutex_ptr,
471 offsetof(struct mutex, wait_lock.rlock.magic),
473 test_magic(lock_rwsem_ptr,
474 offsetof(struct rw_semaphore, wait_lock.magic),
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");
488 if (lock_mmap_sem && !main_task->mm) {
489 pr_err("no mm to lock mmap_sem\n");
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 " : "");
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 " : "");
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));
527 for_each_online_cpu(cpu)
528 flush_work(per_cpu_ptr(&test_works, cpu));
535 if (measure_lock_wait)
536 pr_notice("Maximum lock wait: %lld ns\n",
537 atomic64_read(&max_lock_wait));
540 pr_notice("Page allocation failed %u times\n",
541 atomic_read(&alloc_pages_failed));
543 pr_notice("FINISH in %llu ns\n", local_clock() - test_start);
545 if (signal_pending(main_task))
550 module_init(test_lockup_init);
552 MODULE_LICENSE("GPL");
553 MODULE_AUTHOR("Konstantin Khlebnikov <khlebnikov@yandex-team.ru>");
554 MODULE_DESCRIPTION("Test module to generate lockups");