Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

samples: extend hung_task detector test with semaphore support

Extend the existing hung_task detector test module to support multiple
lock types, including mutex and semaphore, with room for future additions
(e.g., spinlock, etc.). This module creates dummy files under
<debugfs>/hung_task, such as 'mutex' and 'semaphore'. The read process on
any of these files will sleep for enough long time (256 seconds) while
holding the respective lock. As a result, the second process will wait on
the lock for a prolonged duration and be detected by the hung_task
detector.

This change unifies the previous mutex-only sample into a single,
extensible hung_task_tests module, reducing code duplication and improving
maintainability.

Usage is:

> cd /sys/kernel/debug/hung_task
> cat mutex & cat mutex # Test mutex blocking
> cat semaphore & cat semaphore # Test semaphore blocking

Update the Kconfig description to reflect multiple debugfs files support.

Link: https://lkml.kernel.org/r/20250414145945.84916-4-ioworker0@gmail.com
Signed-off-by: Lance Yang <ioworker0@gmail.com>
Signed-off-by: Zi Li <amaindex@outlook.com>
Suggested-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Anna Schumaker <anna.schumaker@oracle.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Joel Granados <joel.granados@kernel.org>
Cc: John Stultz <jstultz@google.com>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Cc: Mingzhe Yang <mingzhe.yang@ly.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Tomasz Figa <tfiga@chromium.org>
Cc: Waiman Long <longman@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yongliang Gao <leonylgao@tencent.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Zi Li and committed by
Andrew Morton
1abf729e 194a9b9e

+103 -71
+5 -4
samples/Kconfig
··· 304 304 tristate "Hung task detector test code" 305 305 depends on DETECT_HUNG_TASK && DEBUG_FS 306 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. 307 + Build a module that provides debugfs files (e.g., mutex, semaphore, 308 + etc.) under <debugfs>/hung_task. If user reads one of these files, 309 + it will sleep long time (256 seconds) with holding a lock. Thus, 310 + if 2 or more processes read the same file concurrently, it will 311 + be detected by the hung_task watchdog. 311 312 312 313 source "samples/rust/Kconfig" 313 314
+1 -1
samples/hung_task/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 - obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task_mutex.o 2 + obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task_tests.o
-66
samples/hung_task/hung_task_mutex.c
··· 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");
+97
samples/hung_task/hung_task_tests.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * hung_task_tests.c - Sample code for testing hung tasks with mutex, 4 + * semaphore, etc. 5 + * 6 + * Usage: Load this module and read `<debugfs>/hung_task/mutex`, 7 + * `<debugfs>/hung_task/semaphore`, etc., with 2 or more processes. 8 + * 9 + * This is for testing kernel hung_task error messages with various locking 10 + * mechanisms (e.g., mutex, semaphore, etc.). Note that this may freeze 11 + * your system or cause a panic. Use only for testing purposes. 12 + */ 13 + 14 + #include <linux/debugfs.h> 15 + #include <linux/delay.h> 16 + #include <linux/fs.h> 17 + #include <linux/module.h> 18 + #include <linux/mutex.h> 19 + #include <linux/semaphore.h> 20 + 21 + #define HUNG_TASK_DIR "hung_task" 22 + #define HUNG_TASK_MUTEX_FILE "mutex" 23 + #define HUNG_TASK_SEM_FILE "semaphore" 24 + #define SLEEP_SECOND 256 25 + 26 + static const char dummy_string[] = "This is a dummy string."; 27 + static DEFINE_MUTEX(dummy_mutex); 28 + static DEFINE_SEMAPHORE(dummy_sem, 1); 29 + static struct dentry *hung_task_dir; 30 + 31 + /* Mutex-based read function */ 32 + static ssize_t read_dummy_mutex(struct file *file, char __user *user_buf, 33 + size_t count, loff_t *ppos) 34 + { 35 + /* Second task waits on mutex, entering uninterruptible sleep */ 36 + guard(mutex)(&dummy_mutex); 37 + 38 + /* First task sleeps here, interruptible */ 39 + msleep_interruptible(SLEEP_SECOND * 1000); 40 + 41 + return simple_read_from_buffer(user_buf, count, ppos, dummy_string, 42 + sizeof(dummy_string)); 43 + } 44 + 45 + /* Semaphore-based read function */ 46 + static ssize_t read_dummy_semaphore(struct file *file, char __user *user_buf, 47 + size_t count, loff_t *ppos) 48 + { 49 + /* Second task waits on semaphore, entering uninterruptible sleep */ 50 + down(&dummy_sem); 51 + 52 + /* First task sleeps here, interruptible */ 53 + msleep_interruptible(SLEEP_SECOND * 1000); 54 + 55 + up(&dummy_sem); 56 + 57 + return simple_read_from_buffer(user_buf, count, ppos, dummy_string, 58 + sizeof(dummy_string)); 59 + } 60 + 61 + /* File operations for mutex */ 62 + static const struct file_operations hung_task_mutex_fops = { 63 + .read = read_dummy_mutex, 64 + }; 65 + 66 + /* File operations for semaphore */ 67 + static const struct file_operations hung_task_sem_fops = { 68 + .read = read_dummy_semaphore, 69 + }; 70 + 71 + static int __init hung_task_tests_init(void) 72 + { 73 + hung_task_dir = debugfs_create_dir(HUNG_TASK_DIR, NULL); 74 + if (IS_ERR(hung_task_dir)) 75 + return PTR_ERR(hung_task_dir); 76 + 77 + /* Create debugfs files for mutex and semaphore tests */ 78 + debugfs_create_file(HUNG_TASK_MUTEX_FILE, 0400, hung_task_dir, NULL, 79 + &hung_task_mutex_fops); 80 + debugfs_create_file(HUNG_TASK_SEM_FILE, 0400, hung_task_dir, NULL, 81 + &hung_task_sem_fops); 82 + 83 + return 0; 84 + } 85 + 86 + static void __exit hung_task_tests_exit(void) 87 + { 88 + debugfs_remove_recursive(hung_task_dir); 89 + } 90 + 91 + module_init(hung_task_tests_init); 92 + module_exit(hung_task_tests_exit); 93 + 94 + MODULE_LICENSE("GPL"); 95 + MODULE_AUTHOR("Masami Hiramatsu <mhiramat@kernel.org>"); 96 + MODULE_AUTHOR("Zi Li <amaindex@outlook.com>"); 97 + MODULE_DESCRIPTION("Simple sleep under lock files for testing hung task");