1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * hung_task_mutex.c - Sample code which causes hung task by mutex 4 * 5 * Usage: load this module and read `<debugfs>/hung_task/mutex` 6 * by 2 or more processes. 7 * 8 * This is for testing kernel hung_task error message. 9 * Note that this will make your system freeze and maybe 10 * cause panic. So do not use this except for the test. 11 */ 12 13 #include <linux/debugfs.h> 14 #include <linux/delay.h> 15 #include <linux/fs.h> 16 #include <linux/module.h> 17 #include <linux/mutex.h> 18 19 #define HUNG_TASK_DIR "hung_task" 20 #define HUNG_TASK_FILE "mutex" 21 #define SLEEP_SECOND 256 22 23 static const char dummy_string[] = "This is a dummy string."; 24 static DEFINE_MUTEX(dummy_mutex); 25 static struct dentry *hung_task_dir; 26 27 static ssize_t read_dummy(struct file *file, char __user *user_buf, 28 size_t count, loff_t *ppos) 29 { 30 /* If the second task waits on the lock, it is uninterruptible sleep. */ 31 guard(mutex)(&dummy_mutex); 32 33 /* When the first task sleep here, it is interruptible. */ 34 msleep_interruptible(SLEEP_SECOND * 1000); 35 36 return simple_read_from_buffer(user_buf, count, ppos, 37 dummy_string, sizeof(dummy_string)); 38 } 39 40 static const struct file_operations hung_task_fops = { 41 .read = read_dummy, 42 }; 43 44 static int __init hung_task_sample_init(void) 45 { 46 hung_task_dir = debugfs_create_dir(HUNG_TASK_DIR, NULL); 47 if (IS_ERR(hung_task_dir)) 48 return PTR_ERR(hung_task_dir); 49 50 debugfs_create_file(HUNG_TASK_FILE, 0400, hung_task_dir, 51 NULL, &hung_task_fops); 52 53 return 0; 54 } 55 56 static void __exit hung_task_sample_exit(void) 57 { 58 debugfs_remove_recursive(hung_task_dir); 59 } 60 61 module_init(hung_task_sample_init); 62 module_exit(hung_task_sample_exit); 63 64 MODULE_LICENSE("GPL"); 65 MODULE_AUTHOR("Masami Hiramatsu"); 66 MODULE_DESCRIPTION("Simple sleep under mutex file for testing hung task"); 67