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