18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Test module to generate lockups
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/sched.h>
118c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
128c2ecf20Sopenharmony_ci#include <linux/sched/clock.h>
138c2ecf20Sopenharmony_ci#include <linux/cpu.h>
148c2ecf20Sopenharmony_ci#include <linux/nmi.h>
158c2ecf20Sopenharmony_ci#include <linux/mm.h>
168c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
178c2ecf20Sopenharmony_ci#include <linux/file.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic unsigned int time_secs;
208c2ecf20Sopenharmony_cimodule_param(time_secs, uint, 0600);
218c2ecf20Sopenharmony_ciMODULE_PARM_DESC(time_secs, "lockup time in seconds, default 0");
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic unsigned int time_nsecs;
248c2ecf20Sopenharmony_cimodule_param(time_nsecs, uint, 0600);
258c2ecf20Sopenharmony_ciMODULE_PARM_DESC(time_nsecs, "nanoseconds part of lockup time, default 0");
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic unsigned int cooldown_secs;
288c2ecf20Sopenharmony_cimodule_param(cooldown_secs, uint, 0600);
298c2ecf20Sopenharmony_ciMODULE_PARM_DESC(cooldown_secs, "cooldown time between iterations in seconds, default 0");
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic unsigned int cooldown_nsecs;
328c2ecf20Sopenharmony_cimodule_param(cooldown_nsecs, uint, 0600);
338c2ecf20Sopenharmony_ciMODULE_PARM_DESC(cooldown_nsecs, "nanoseconds part of cooldown, default 0");
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic unsigned int iterations = 1;
368c2ecf20Sopenharmony_cimodule_param(iterations, uint, 0600);
378c2ecf20Sopenharmony_ciMODULE_PARM_DESC(iterations, "lockup iterations, default 1");
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic bool all_cpus;
408c2ecf20Sopenharmony_cimodule_param(all_cpus, bool, 0400);
418c2ecf20Sopenharmony_ciMODULE_PARM_DESC(all_cpus, "trigger lockup at all cpus at once");
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int wait_state;
448c2ecf20Sopenharmony_cistatic char *state = "R";
458c2ecf20Sopenharmony_cimodule_param(state, charp, 0400);
468c2ecf20Sopenharmony_ciMODULE_PARM_DESC(state, "wait in 'R' running (default), 'D' uninterruptible, 'K' killable, 'S' interruptible state");
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic bool use_hrtimer;
498c2ecf20Sopenharmony_cimodule_param(use_hrtimer, bool, 0400);
508c2ecf20Sopenharmony_ciMODULE_PARM_DESC(use_hrtimer, "use high-resolution timer for sleeping");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic bool iowait;
538c2ecf20Sopenharmony_cimodule_param(iowait, bool, 0400);
548c2ecf20Sopenharmony_ciMODULE_PARM_DESC(iowait, "account sleep time as iowait");
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic bool lock_read;
578c2ecf20Sopenharmony_cimodule_param(lock_read, bool, 0400);
588c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_read, "lock read-write locks for read");
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic bool lock_single;
618c2ecf20Sopenharmony_cimodule_param(lock_single, bool, 0400);
628c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_single, "acquire locks only at one cpu");
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic bool reacquire_locks;
658c2ecf20Sopenharmony_cimodule_param(reacquire_locks, bool, 0400);
668c2ecf20Sopenharmony_ciMODULE_PARM_DESC(reacquire_locks, "release and reacquire locks/irq/preempt between iterations");
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic bool touch_softlockup;
698c2ecf20Sopenharmony_cimodule_param(touch_softlockup, bool, 0600);
708c2ecf20Sopenharmony_ciMODULE_PARM_DESC(touch_softlockup, "touch soft-lockup watchdog between iterations");
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic bool touch_hardlockup;
738c2ecf20Sopenharmony_cimodule_param(touch_hardlockup, bool, 0600);
748c2ecf20Sopenharmony_ciMODULE_PARM_DESC(touch_hardlockup, "touch hard-lockup watchdog between iterations");
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic bool call_cond_resched;
778c2ecf20Sopenharmony_cimodule_param(call_cond_resched, bool, 0600);
788c2ecf20Sopenharmony_ciMODULE_PARM_DESC(call_cond_resched, "call cond_resched() between iterations");
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic bool measure_lock_wait;
818c2ecf20Sopenharmony_cimodule_param(measure_lock_wait, bool, 0400);
828c2ecf20Sopenharmony_ciMODULE_PARM_DESC(measure_lock_wait, "measure lock wait time");
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic unsigned long lock_wait_threshold = ULONG_MAX;
858c2ecf20Sopenharmony_cimodule_param(lock_wait_threshold, ulong, 0400);
868c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_wait_threshold, "print lock wait time longer than this in nanoseconds, default off");
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic bool test_disable_irq;
898c2ecf20Sopenharmony_cimodule_param_named(disable_irq, test_disable_irq, bool, 0400);
908c2ecf20Sopenharmony_ciMODULE_PARM_DESC(disable_irq, "disable interrupts: generate hard-lockups");
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic bool disable_softirq;
938c2ecf20Sopenharmony_cimodule_param(disable_softirq, bool, 0400);
948c2ecf20Sopenharmony_ciMODULE_PARM_DESC(disable_softirq, "disable bottom-half irq handlers");
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic bool disable_preempt;
978c2ecf20Sopenharmony_cimodule_param(disable_preempt, bool, 0400);
988c2ecf20Sopenharmony_ciMODULE_PARM_DESC(disable_preempt, "disable preemption: generate soft-lockups");
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic bool lock_rcu;
1018c2ecf20Sopenharmony_cimodule_param(lock_rcu, bool, 0400);
1028c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_rcu, "grab rcu_read_lock: generate rcu stalls");
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic bool lock_mmap_sem;
1058c2ecf20Sopenharmony_cimodule_param(lock_mmap_sem, bool, 0400);
1068c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_mmap_sem, "lock mm->mmap_lock: block procfs interfaces");
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic unsigned long lock_rwsem_ptr;
1098c2ecf20Sopenharmony_cimodule_param_unsafe(lock_rwsem_ptr, ulong, 0400);
1108c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_rwsem_ptr, "lock rw_semaphore at address");
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic unsigned long lock_mutex_ptr;
1138c2ecf20Sopenharmony_cimodule_param_unsafe(lock_mutex_ptr, ulong, 0400);
1148c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_mutex_ptr, "lock mutex at address");
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic unsigned long lock_spinlock_ptr;
1178c2ecf20Sopenharmony_cimodule_param_unsafe(lock_spinlock_ptr, ulong, 0400);
1188c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_spinlock_ptr, "lock spinlock at address");
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic unsigned long lock_rwlock_ptr;
1218c2ecf20Sopenharmony_cimodule_param_unsafe(lock_rwlock_ptr, ulong, 0400);
1228c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_rwlock_ptr, "lock rwlock at address");
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic unsigned int alloc_pages_nr;
1258c2ecf20Sopenharmony_cimodule_param_unsafe(alloc_pages_nr, uint, 0600);
1268c2ecf20Sopenharmony_ciMODULE_PARM_DESC(alloc_pages_nr, "allocate and free pages under locks");
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic unsigned int alloc_pages_order;
1298c2ecf20Sopenharmony_cimodule_param(alloc_pages_order, uint, 0400);
1308c2ecf20Sopenharmony_ciMODULE_PARM_DESC(alloc_pages_order, "page order to allocate");
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic gfp_t alloc_pages_gfp = GFP_KERNEL;
1338c2ecf20Sopenharmony_cimodule_param_unsafe(alloc_pages_gfp, uint, 0400);
1348c2ecf20Sopenharmony_ciMODULE_PARM_DESC(alloc_pages_gfp, "allocate pages with this gfp_mask, default GFP_KERNEL");
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic bool alloc_pages_atomic;
1378c2ecf20Sopenharmony_cimodule_param(alloc_pages_atomic, bool, 0400);
1388c2ecf20Sopenharmony_ciMODULE_PARM_DESC(alloc_pages_atomic, "allocate pages with GFP_ATOMIC");
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic bool reallocate_pages;
1418c2ecf20Sopenharmony_cimodule_param(reallocate_pages, bool, 0400);
1428c2ecf20Sopenharmony_ciMODULE_PARM_DESC(reallocate_pages, "free and allocate pages between iterations");
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistruct file *test_file;
1458c2ecf20Sopenharmony_cistatic struct inode *test_inode;
1468c2ecf20Sopenharmony_cistatic char test_file_path[256];
1478c2ecf20Sopenharmony_cimodule_param_string(file_path, test_file_path, sizeof(test_file_path), 0400);
1488c2ecf20Sopenharmony_ciMODULE_PARM_DESC(file_path, "file path to test");
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic bool test_lock_inode;
1518c2ecf20Sopenharmony_cimodule_param_named(lock_inode, test_lock_inode, bool, 0400);
1528c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_inode, "lock file -> inode -> i_rwsem");
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic bool test_lock_mapping;
1558c2ecf20Sopenharmony_cimodule_param_named(lock_mapping, test_lock_mapping, bool, 0400);
1568c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_mapping, "lock file -> mapping -> i_mmap_rwsem");
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic bool test_lock_sb_umount;
1598c2ecf20Sopenharmony_cimodule_param_named(lock_sb_umount, test_lock_sb_umount, bool, 0400);
1608c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lock_sb_umount, "lock file -> sb -> s_umount");
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic atomic_t alloc_pages_failed = ATOMIC_INIT(0);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic atomic64_t max_lock_wait = ATOMIC64_INIT(0);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic struct task_struct *main_task;
1678c2ecf20Sopenharmony_cistatic int master_cpu;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic void test_lock(bool master, bool verbose)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	u64 wait_start;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (measure_lock_wait)
1748c2ecf20Sopenharmony_ci		wait_start = local_clock();
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (lock_mutex_ptr && master) {
1778c2ecf20Sopenharmony_ci		if (verbose)
1788c2ecf20Sopenharmony_ci			pr_notice("lock mutex %ps\n", (void *)lock_mutex_ptr);
1798c2ecf20Sopenharmony_ci		mutex_lock((struct mutex *)lock_mutex_ptr);
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (lock_rwsem_ptr && master) {
1838c2ecf20Sopenharmony_ci		if (verbose)
1848c2ecf20Sopenharmony_ci			pr_notice("lock rw_semaphore %ps\n",
1858c2ecf20Sopenharmony_ci				  (void *)lock_rwsem_ptr);
1868c2ecf20Sopenharmony_ci		if (lock_read)
1878c2ecf20Sopenharmony_ci			down_read((struct rw_semaphore *)lock_rwsem_ptr);
1888c2ecf20Sopenharmony_ci		else
1898c2ecf20Sopenharmony_ci			down_write((struct rw_semaphore *)lock_rwsem_ptr);
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (lock_mmap_sem && master) {
1938c2ecf20Sopenharmony_ci		if (verbose)
1948c2ecf20Sopenharmony_ci			pr_notice("lock mmap_lock pid=%d\n", main_task->pid);
1958c2ecf20Sopenharmony_ci		if (lock_read)
1968c2ecf20Sopenharmony_ci			mmap_read_lock(main_task->mm);
1978c2ecf20Sopenharmony_ci		else
1988c2ecf20Sopenharmony_ci			mmap_write_lock(main_task->mm);
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (test_disable_irq)
2028c2ecf20Sopenharmony_ci		local_irq_disable();
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (disable_softirq)
2058c2ecf20Sopenharmony_ci		local_bh_disable();
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (disable_preempt)
2088c2ecf20Sopenharmony_ci		preempt_disable();
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (lock_rcu)
2118c2ecf20Sopenharmony_ci		rcu_read_lock();
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (lock_spinlock_ptr && master) {
2148c2ecf20Sopenharmony_ci		if (verbose)
2158c2ecf20Sopenharmony_ci			pr_notice("lock spinlock %ps\n",
2168c2ecf20Sopenharmony_ci				  (void *)lock_spinlock_ptr);
2178c2ecf20Sopenharmony_ci		spin_lock((spinlock_t *)lock_spinlock_ptr);
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	if (lock_rwlock_ptr && master) {
2218c2ecf20Sopenharmony_ci		if (verbose)
2228c2ecf20Sopenharmony_ci			pr_notice("lock rwlock %ps\n",
2238c2ecf20Sopenharmony_ci				  (void *)lock_rwlock_ptr);
2248c2ecf20Sopenharmony_ci		if (lock_read)
2258c2ecf20Sopenharmony_ci			read_lock((rwlock_t *)lock_rwlock_ptr);
2268c2ecf20Sopenharmony_ci		else
2278c2ecf20Sopenharmony_ci			write_lock((rwlock_t *)lock_rwlock_ptr);
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (measure_lock_wait) {
2318c2ecf20Sopenharmony_ci		s64 cur_wait = local_clock() - wait_start;
2328c2ecf20Sopenharmony_ci		s64 max_wait = atomic64_read(&max_lock_wait);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		do {
2358c2ecf20Sopenharmony_ci			if (cur_wait < max_wait)
2368c2ecf20Sopenharmony_ci				break;
2378c2ecf20Sopenharmony_ci			max_wait = atomic64_cmpxchg(&max_lock_wait,
2388c2ecf20Sopenharmony_ci						    max_wait, cur_wait);
2398c2ecf20Sopenharmony_ci		} while (max_wait != cur_wait);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		if (cur_wait > lock_wait_threshold)
2428c2ecf20Sopenharmony_ci			pr_notice_ratelimited("lock wait %lld ns\n", cur_wait);
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic void test_unlock(bool master, bool verbose)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	if (lock_rwlock_ptr && master) {
2498c2ecf20Sopenharmony_ci		if (lock_read)
2508c2ecf20Sopenharmony_ci			read_unlock((rwlock_t *)lock_rwlock_ptr);
2518c2ecf20Sopenharmony_ci		else
2528c2ecf20Sopenharmony_ci			write_unlock((rwlock_t *)lock_rwlock_ptr);
2538c2ecf20Sopenharmony_ci		if (verbose)
2548c2ecf20Sopenharmony_ci			pr_notice("unlock rwlock %ps\n",
2558c2ecf20Sopenharmony_ci				  (void *)lock_rwlock_ptr);
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	if (lock_spinlock_ptr && master) {
2598c2ecf20Sopenharmony_ci		spin_unlock((spinlock_t *)lock_spinlock_ptr);
2608c2ecf20Sopenharmony_ci		if (verbose)
2618c2ecf20Sopenharmony_ci			pr_notice("unlock spinlock %ps\n",
2628c2ecf20Sopenharmony_ci				  (void *)lock_spinlock_ptr);
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (lock_rcu)
2668c2ecf20Sopenharmony_ci		rcu_read_unlock();
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	if (disable_preempt)
2698c2ecf20Sopenharmony_ci		preempt_enable();
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	if (disable_softirq)
2728c2ecf20Sopenharmony_ci		local_bh_enable();
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	if (test_disable_irq)
2758c2ecf20Sopenharmony_ci		local_irq_enable();
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	if (lock_mmap_sem && master) {
2788c2ecf20Sopenharmony_ci		if (lock_read)
2798c2ecf20Sopenharmony_ci			mmap_read_unlock(main_task->mm);
2808c2ecf20Sopenharmony_ci		else
2818c2ecf20Sopenharmony_ci			mmap_write_unlock(main_task->mm);
2828c2ecf20Sopenharmony_ci		if (verbose)
2838c2ecf20Sopenharmony_ci			pr_notice("unlock mmap_lock pid=%d\n", main_task->pid);
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (lock_rwsem_ptr && master) {
2878c2ecf20Sopenharmony_ci		if (lock_read)
2888c2ecf20Sopenharmony_ci			up_read((struct rw_semaphore *)lock_rwsem_ptr);
2898c2ecf20Sopenharmony_ci		else
2908c2ecf20Sopenharmony_ci			up_write((struct rw_semaphore *)lock_rwsem_ptr);
2918c2ecf20Sopenharmony_ci		if (verbose)
2928c2ecf20Sopenharmony_ci			pr_notice("unlock rw_semaphore %ps\n",
2938c2ecf20Sopenharmony_ci				  (void *)lock_rwsem_ptr);
2948c2ecf20Sopenharmony_ci	}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	if (lock_mutex_ptr && master) {
2978c2ecf20Sopenharmony_ci		mutex_unlock((struct mutex *)lock_mutex_ptr);
2988c2ecf20Sopenharmony_ci		if (verbose)
2998c2ecf20Sopenharmony_ci			pr_notice("unlock mutex %ps\n",
3008c2ecf20Sopenharmony_ci				  (void *)lock_mutex_ptr);
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic void test_alloc_pages(struct list_head *pages)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct page *page;
3078c2ecf20Sopenharmony_ci	unsigned int i;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	for (i = 0; i < alloc_pages_nr; i++) {
3108c2ecf20Sopenharmony_ci		page = alloc_pages(alloc_pages_gfp, alloc_pages_order);
3118c2ecf20Sopenharmony_ci		if (!page) {
3128c2ecf20Sopenharmony_ci			atomic_inc(&alloc_pages_failed);
3138c2ecf20Sopenharmony_ci			break;
3148c2ecf20Sopenharmony_ci		}
3158c2ecf20Sopenharmony_ci		list_add(&page->lru, pages);
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic void test_free_pages(struct list_head *pages)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct page *page, *next;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	list_for_each_entry_safe(page, next, pages, lru)
3248c2ecf20Sopenharmony_ci		__free_pages(page, alloc_pages_order);
3258c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(pages);
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic void test_wait(unsigned int secs, unsigned int nsecs)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	if (wait_state == TASK_RUNNING) {
3318c2ecf20Sopenharmony_ci		if (secs)
3328c2ecf20Sopenharmony_ci			mdelay(secs * MSEC_PER_SEC);
3338c2ecf20Sopenharmony_ci		if (nsecs)
3348c2ecf20Sopenharmony_ci			ndelay(nsecs);
3358c2ecf20Sopenharmony_ci		return;
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	__set_current_state(wait_state);
3398c2ecf20Sopenharmony_ci	if (use_hrtimer) {
3408c2ecf20Sopenharmony_ci		ktime_t time;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci		time = ns_to_ktime((u64)secs * NSEC_PER_SEC + nsecs);
3438c2ecf20Sopenharmony_ci		schedule_hrtimeout(&time, HRTIMER_MODE_REL);
3448c2ecf20Sopenharmony_ci	} else {
3458c2ecf20Sopenharmony_ci		schedule_timeout(secs * HZ + nsecs_to_jiffies(nsecs));
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic void test_lockup(bool master)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	u64 lockup_start = local_clock();
3528c2ecf20Sopenharmony_ci	unsigned int iter = 0;
3538c2ecf20Sopenharmony_ci	LIST_HEAD(pages);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	pr_notice("Start on CPU%d\n", raw_smp_processor_id());
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	test_lock(master, true);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	test_alloc_pages(&pages);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	while (iter++ < iterations && !signal_pending(main_task)) {
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		if (iowait)
3648c2ecf20Sopenharmony_ci			current->in_iowait = 1;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci		test_wait(time_secs, time_nsecs);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci		if (iowait)
3698c2ecf20Sopenharmony_ci			current->in_iowait = 0;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		if (reallocate_pages)
3728c2ecf20Sopenharmony_ci			test_free_pages(&pages);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci		if (reacquire_locks)
3758c2ecf20Sopenharmony_ci			test_unlock(master, false);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci		if (touch_softlockup)
3788c2ecf20Sopenharmony_ci			touch_softlockup_watchdog();
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci		if (touch_hardlockup)
3818c2ecf20Sopenharmony_ci			touch_nmi_watchdog();
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci		if (call_cond_resched)
3848c2ecf20Sopenharmony_ci			cond_resched();
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci		test_wait(cooldown_secs, cooldown_nsecs);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		if (reacquire_locks)
3898c2ecf20Sopenharmony_ci			test_lock(master, false);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		if (reallocate_pages)
3928c2ecf20Sopenharmony_ci			test_alloc_pages(&pages);
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	pr_notice("Finish on CPU%d in %lld ns\n", raw_smp_processor_id(),
3968c2ecf20Sopenharmony_ci		  local_clock() - lockup_start);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	test_free_pages(&pages);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	test_unlock(master, true);
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct work_struct, test_works);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic void test_work_fn(struct work_struct *work)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	test_lockup(!lock_single ||
4088c2ecf20Sopenharmony_ci		    work == per_cpu_ptr(&test_works, master_cpu));
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic bool test_kernel_ptr(unsigned long addr, int size)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	void *ptr = (void *)addr;
4148c2ecf20Sopenharmony_ci	char buf;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (!addr)
4178c2ecf20Sopenharmony_ci		return false;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	/* should be at least readable kernel address */
4208c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_ALTERNATE_USER_ADDRESS_SPACE) &&
4218c2ecf20Sopenharmony_ci	    (access_ok((void __user *)ptr, 1) ||
4228c2ecf20Sopenharmony_ci	     access_ok((void __user *)ptr + size - 1, 1))) {
4238c2ecf20Sopenharmony_ci		pr_err("user space ptr invalid in kernel: %#lx\n", addr);
4248c2ecf20Sopenharmony_ci		return true;
4258c2ecf20Sopenharmony_ci	}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	if (get_kernel_nofault(buf, ptr) ||
4288c2ecf20Sopenharmony_ci	    get_kernel_nofault(buf, ptr + size - 1)) {
4298c2ecf20Sopenharmony_ci		pr_err("invalid kernel ptr: %#lx\n", addr);
4308c2ecf20Sopenharmony_ci		return true;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	return false;
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic bool __maybe_unused test_magic(unsigned long addr, int offset,
4378c2ecf20Sopenharmony_ci				      unsigned int expected)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	void *ptr = (void *)addr + offset;
4408c2ecf20Sopenharmony_ci	unsigned int magic = 0;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	if (!addr)
4438c2ecf20Sopenharmony_ci		return false;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	if (get_kernel_nofault(magic, ptr) || magic != expected) {
4468c2ecf20Sopenharmony_ci		pr_err("invalid magic at %#lx + %#x = %#x, expected %#x\n",
4478c2ecf20Sopenharmony_ci		       addr, offset, magic, expected);
4488c2ecf20Sopenharmony_ci		return true;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return false;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic int __init test_lockup_init(void)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	u64 test_start = local_clock();
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	main_task = current;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	switch (state[0]) {
4618c2ecf20Sopenharmony_ci	case 'S':
4628c2ecf20Sopenharmony_ci		wait_state = TASK_INTERRUPTIBLE;
4638c2ecf20Sopenharmony_ci		break;
4648c2ecf20Sopenharmony_ci	case 'D':
4658c2ecf20Sopenharmony_ci		wait_state = TASK_UNINTERRUPTIBLE;
4668c2ecf20Sopenharmony_ci		break;
4678c2ecf20Sopenharmony_ci	case 'K':
4688c2ecf20Sopenharmony_ci		wait_state = TASK_KILLABLE;
4698c2ecf20Sopenharmony_ci		break;
4708c2ecf20Sopenharmony_ci	case 'R':
4718c2ecf20Sopenharmony_ci		wait_state = TASK_RUNNING;
4728c2ecf20Sopenharmony_ci		break;
4738c2ecf20Sopenharmony_ci	default:
4748c2ecf20Sopenharmony_ci		pr_err("unknown state=%s\n", state);
4758c2ecf20Sopenharmony_ci		return -EINVAL;
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	if (alloc_pages_atomic)
4798c2ecf20Sopenharmony_ci		alloc_pages_gfp = GFP_ATOMIC;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	if (test_kernel_ptr(lock_spinlock_ptr, sizeof(spinlock_t)) ||
4828c2ecf20Sopenharmony_ci	    test_kernel_ptr(lock_rwlock_ptr, sizeof(rwlock_t)) ||
4838c2ecf20Sopenharmony_ci	    test_kernel_ptr(lock_mutex_ptr, sizeof(struct mutex)) ||
4848c2ecf20Sopenharmony_ci	    test_kernel_ptr(lock_rwsem_ptr, sizeof(struct rw_semaphore)))
4858c2ecf20Sopenharmony_ci		return -EINVAL;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_SPINLOCK
4888c2ecf20Sopenharmony_ci	if (test_magic(lock_spinlock_ptr,
4898c2ecf20Sopenharmony_ci		       offsetof(spinlock_t, rlock.magic),
4908c2ecf20Sopenharmony_ci		       SPINLOCK_MAGIC) ||
4918c2ecf20Sopenharmony_ci	    test_magic(lock_rwlock_ptr,
4928c2ecf20Sopenharmony_ci		       offsetof(rwlock_t, magic),
4938c2ecf20Sopenharmony_ci		       RWLOCK_MAGIC) ||
4948c2ecf20Sopenharmony_ci	    test_magic(lock_mutex_ptr,
4958c2ecf20Sopenharmony_ci		       offsetof(struct mutex, wait_lock.rlock.magic),
4968c2ecf20Sopenharmony_ci		       SPINLOCK_MAGIC) ||
4978c2ecf20Sopenharmony_ci	    test_magic(lock_rwsem_ptr,
4988c2ecf20Sopenharmony_ci		       offsetof(struct rw_semaphore, wait_lock.magic),
4998c2ecf20Sopenharmony_ci		       SPINLOCK_MAGIC))
5008c2ecf20Sopenharmony_ci		return -EINVAL;
5018c2ecf20Sopenharmony_ci#endif
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if ((wait_state != TASK_RUNNING ||
5048c2ecf20Sopenharmony_ci	     (call_cond_resched && !reacquire_locks) ||
5058c2ecf20Sopenharmony_ci	     (alloc_pages_nr && gfpflags_allow_blocking(alloc_pages_gfp))) &&
5068c2ecf20Sopenharmony_ci	    (test_disable_irq || disable_softirq || disable_preempt ||
5078c2ecf20Sopenharmony_ci	     lock_rcu || lock_spinlock_ptr || lock_rwlock_ptr)) {
5088c2ecf20Sopenharmony_ci		pr_err("refuse to sleep in atomic context\n");
5098c2ecf20Sopenharmony_ci		return -EINVAL;
5108c2ecf20Sopenharmony_ci	}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	if (lock_mmap_sem && !main_task->mm) {
5138c2ecf20Sopenharmony_ci		pr_err("no mm to lock mmap_lock\n");
5148c2ecf20Sopenharmony_ci		return -EINVAL;
5158c2ecf20Sopenharmony_ci	}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (test_file_path[0]) {
5188c2ecf20Sopenharmony_ci		test_file = filp_open(test_file_path, O_RDONLY, 0);
5198c2ecf20Sopenharmony_ci		if (IS_ERR(test_file)) {
5208c2ecf20Sopenharmony_ci			pr_err("failed to open %s: %ld\n", test_file_path, PTR_ERR(test_file));
5218c2ecf20Sopenharmony_ci			return PTR_ERR(test_file);
5228c2ecf20Sopenharmony_ci		}
5238c2ecf20Sopenharmony_ci		test_inode = file_inode(test_file);
5248c2ecf20Sopenharmony_ci	} else if (test_lock_inode ||
5258c2ecf20Sopenharmony_ci		   test_lock_mapping ||
5268c2ecf20Sopenharmony_ci		   test_lock_sb_umount) {
5278c2ecf20Sopenharmony_ci		pr_err("no file to lock\n");
5288c2ecf20Sopenharmony_ci		return -EINVAL;
5298c2ecf20Sopenharmony_ci	}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	if (test_lock_inode && test_inode)
5328c2ecf20Sopenharmony_ci		lock_rwsem_ptr = (unsigned long)&test_inode->i_rwsem;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	if (test_lock_mapping && test_file && test_file->f_mapping)
5358c2ecf20Sopenharmony_ci		lock_rwsem_ptr = (unsigned long)&test_file->f_mapping->i_mmap_rwsem;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	if (test_lock_sb_umount && test_inode)
5388c2ecf20Sopenharmony_ci		lock_rwsem_ptr = (unsigned long)&test_inode->i_sb->s_umount;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	pr_notice("START pid=%d time=%u +%u ns cooldown=%u +%u ns iterations=%u state=%s %s%s%s%s%s%s%s%s%s%s%s\n",
5418c2ecf20Sopenharmony_ci		  main_task->pid, time_secs, time_nsecs,
5428c2ecf20Sopenharmony_ci		  cooldown_secs, cooldown_nsecs, iterations, state,
5438c2ecf20Sopenharmony_ci		  all_cpus ? "all_cpus " : "",
5448c2ecf20Sopenharmony_ci		  iowait ? "iowait " : "",
5458c2ecf20Sopenharmony_ci		  test_disable_irq ? "disable_irq " : "",
5468c2ecf20Sopenharmony_ci		  disable_softirq ? "disable_softirq " : "",
5478c2ecf20Sopenharmony_ci		  disable_preempt ? "disable_preempt " : "",
5488c2ecf20Sopenharmony_ci		  lock_rcu ? "lock_rcu " : "",
5498c2ecf20Sopenharmony_ci		  lock_read ? "lock_read " : "",
5508c2ecf20Sopenharmony_ci		  touch_softlockup ? "touch_softlockup " : "",
5518c2ecf20Sopenharmony_ci		  touch_hardlockup ? "touch_hardlockup " : "",
5528c2ecf20Sopenharmony_ci		  call_cond_resched ? "call_cond_resched " : "",
5538c2ecf20Sopenharmony_ci		  reacquire_locks ? "reacquire_locks " : "");
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	if (alloc_pages_nr)
5568c2ecf20Sopenharmony_ci		pr_notice("ALLOCATE PAGES nr=%u order=%u gfp=%pGg %s\n",
5578c2ecf20Sopenharmony_ci			  alloc_pages_nr, alloc_pages_order, &alloc_pages_gfp,
5588c2ecf20Sopenharmony_ci			  reallocate_pages ? "reallocate_pages " : "");
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	if (all_cpus) {
5618c2ecf20Sopenharmony_ci		unsigned int cpu;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci		cpus_read_lock();
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci		preempt_disable();
5668c2ecf20Sopenharmony_ci		master_cpu = smp_processor_id();
5678c2ecf20Sopenharmony_ci		for_each_online_cpu(cpu) {
5688c2ecf20Sopenharmony_ci			INIT_WORK(per_cpu_ptr(&test_works, cpu), test_work_fn);
5698c2ecf20Sopenharmony_ci			queue_work_on(cpu, system_highpri_wq,
5708c2ecf20Sopenharmony_ci				      per_cpu_ptr(&test_works, cpu));
5718c2ecf20Sopenharmony_ci		}
5728c2ecf20Sopenharmony_ci		preempt_enable();
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		for_each_online_cpu(cpu)
5758c2ecf20Sopenharmony_ci			flush_work(per_cpu_ptr(&test_works, cpu));
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci		cpus_read_unlock();
5788c2ecf20Sopenharmony_ci	} else {
5798c2ecf20Sopenharmony_ci		test_lockup(true);
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	if (measure_lock_wait)
5838c2ecf20Sopenharmony_ci		pr_notice("Maximum lock wait: %lld ns\n",
5848c2ecf20Sopenharmony_ci			  atomic64_read(&max_lock_wait));
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	if (alloc_pages_nr)
5878c2ecf20Sopenharmony_ci		pr_notice("Page allocation failed %u times\n",
5888c2ecf20Sopenharmony_ci			  atomic_read(&alloc_pages_failed));
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	pr_notice("FINISH in %llu ns\n", local_clock() - test_start);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	if (test_file)
5938c2ecf20Sopenharmony_ci		fput(test_file);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	if (signal_pending(main_task))
5968c2ecf20Sopenharmony_ci		return -EINTR;
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	return -EAGAIN;
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_cimodule_init(test_lockup_init);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
6038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Konstantin Khlebnikov <khlebnikov@yandex-team.ru>");
6048c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Test module to generate lockups");
605