Skip to content

Commit 2158599

Browse files
mhiramatakpm00
authored andcommitted
samples: add hung_task detector mutex blocking sample
Add a hung_task detector mutex blocking test sample code. This module will create a dummy file on the debugfs. That file will cause the read process to sleep for enough long time (256 seconds) while holding a mutex. As a result, the second process will wait on the mutex for a prolonged duration and be detected by the hung_task detector. Usage is; > cd /sys/kernel/debug/hung_task > cat mutex & cat mutex and wait for hung_task message. [[email protected]: make `hung_task_dir' static] Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/ Link: https://lkml.kernel.org/r/174046696281.2194069.4567490148001547311.stgit@mhiramat.tok.corp.google.com Signed-off-by: Masami Hiramatsu (Google) <[email protected]> Cc: Anna Schumaker <[email protected]> Cc: Boqun Feng <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Joel Granados <[email protected]> Cc: Kent Overstreet <[email protected]> Cc: Lance Yang <[email protected]> Cc: Sergey Senozhatsky <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: Tomasz Figa <[email protected]> Cc: Waiman Long <[email protected]> Cc: Will Deacon <[email protected]> Cc: Yongliang Gao <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3cf67d6 commit 2158599

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

samples/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ config SAMPLE_CHECK_EXEC
300300
demonstrate how they should be used with execveat(2) +
301301
AT_EXECVE_CHECK.
302302

303+
config SAMPLE_HUNG_TASK
304+
tristate "Hung task detector test code"
305+
depends on DETECT_HUNG_TASK && DEBUG_FS
306+
help
307+
Build a module which provide a simple debugfs file. If user reads
308+
the file, it will sleep long time (256 seconds) with holding a
309+
mutex. Thus if there are 2 or more processes read this file, it
310+
will be detected by the hung_task watchdog.
311+
303312
source "samples/rust/Kconfig"
304313

305314
source "samples/damon/Kconfig"

samples/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/
4242
obj-$(CONFIG_SAMPLES_RUST) += rust/
4343
obj-$(CONFIG_SAMPLE_DAMON_WSSE) += damon/
4444
obj-$(CONFIG_SAMPLE_DAMON_PRCL) += damon/
45+
obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task/

samples/hung_task/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task_mutex.o

samples/hung_task/hung_task_mutex.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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");

0 commit comments

Comments
 (0)