lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250318103615.bce4a3291786c2bd525f0f02@kernel.org>
Date: Tue, 18 Mar 2025 10:36:15 +0900
From: Masami Hiramatsu (Google) <mhiramat@...nel.org>
To: Lance Yang <ioworker0@...il.com>
Cc: akpm@...ux-foundation.org, will@...nel.org, peterz@...radead.org,
 mingo@...hat.com, longman@...hat.com, mhiramat@...nel.org,
 anna.schumaker@...cle.com, boqun.feng@...il.com, joel.granados@...nel.org,
 kent.overstreet@...ux.dev, leonylgao@...cent.com,
 linux-kernel@...r.kernel.org, rostedt@...dmis.org,
 senozhatsky@...omium.org, tfiga@...omium.org, amaindex@...look.com
Subject: Re: [PATCH RESEND v2 3/3] samples: add hung_task detector semaphore
 blocking sample

On Fri, 14 Mar 2025 22:43:00 +0800
Lance Yang <ioworker0@...il.com> wrote:

> From: Zi Li <amaindex@...look.com>
> 
> Add a hung_task detector semaphore blocking test sample code.
> 
> This module will create a dummy file on the debugfs. That file will cause
> the read process to sleep for a sufficiently long time (256 seconds)
> while holding a semaphore. As a result, the second process will wait on
> the semaphore for a prolonged duration and be detected by the hung_task
> detector.
> 
> Usage is;
> 
>  > cd /sys/kernel/debug/hung_task
>  > cat semaphore & cat semaphore
> 
> and wait for hung_task message.

Thanks for updating the sample code. BTW, do we need to have almost same
2 samples? I think we can unify it (one module provides both "mutex" and
"semaphre" test files.) to "hung_task_tests.c"

That will help us to extend it for other types easier (and less code).

Thank you,

> 
> Signed-off-by: Lance Yang <ioworker0@...il.com>
> Signed-off-by: Zi Li <amaindex@...look.com>
> ---
>  samples/Kconfig                         | 11 ++--
>  samples/hung_task/Makefile              |  3 +-
>  samples/hung_task/hung_task_mutex.c     | 20 ++++---
>  samples/hung_task/hung_task_semaphore.c | 74 +++++++++++++++++++++++++
>  4 files changed, 96 insertions(+), 12 deletions(-)
>  create mode 100644 samples/hung_task/hung_task_semaphore.c
> 
> diff --git a/samples/Kconfig b/samples/Kconfig
> index 09011be2391a..3a073d6b848b 100644
> --- a/samples/Kconfig
> +++ b/samples/Kconfig
> @@ -304,10 +304,13 @@ config SAMPLE_HUNG_TASK
>  	tristate "Hung task detector test code"
>  	depends on DETECT_HUNG_TASK && DEBUG_FS
>  	help
> -	  Build a module which provide a simple debugfs file. If user reads
> -	  the file, it will sleep long time (256 seconds) with holding a
> -	  mutex. Thus if there are 2 or more processes read this file, it
> -	  will be detected by the hung_task watchdog.
> +	  Build multiple modules to test the hung task detector. Each module
> +	  provides a simple debugfs file corresponding to a specific
> +	  synchronization primitive (e.g., mutex, semaphore, etc.). When the
> +	  file is read, the module will sleep for a long time (256 seconds)
> +	  while holding the respective synchronizer. If multiple processes
> +	  attempt to read these files concurrently, the hung_task watchdog
> +	  can detect potential hangs or deadlocks.
>  
>  source "samples/rust/Kconfig"
>  
> diff --git a/samples/hung_task/Makefile b/samples/hung_task/Makefile
> index fe9dde799880..7483c2c0a0ef 100644
> --- a/samples/hung_task/Makefile
> +++ b/samples/hung_task/Makefile
> @@ -1,2 +1,3 @@
>  # SPDX-License-Identifier: GPL-2.0-only
> -obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task_mutex.o
> \ No newline at end of file
> +obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task_mutex.o
> +obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task_semaphore.o
> \ No newline at end of file
> diff --git a/samples/hung_task/hung_task_mutex.c b/samples/hung_task/hung_task_mutex.c
> index 7a29f2246d22..e4d1d69618b8 100644
> --- a/samples/hung_task/hung_task_mutex.c
> +++ b/samples/hung_task/hung_task_mutex.c
> @@ -22,7 +22,7 @@
>  
>  static const char dummy_string[] = "This is a dummy string.";
>  static DEFINE_MUTEX(dummy_mutex);
> -struct dentry *hung_task_dir;
> +static struct dentry *hung_task_dir;
>  
>  static ssize_t read_dummy(struct file *file, char __user *user_buf,
>  			  size_t count, loff_t *ppos)
> @@ -43,19 +43,25 @@ static const struct file_operations hung_task_fops = {
>  
>  static int __init hung_task_sample_init(void)
>  {
> -	hung_task_dir = debugfs_create_dir(HUNG_TASK_DIR, NULL);
> -	if (IS_ERR(hung_task_dir))
> -		return PTR_ERR(hung_task_dir);
> +	hung_task_dir = debugfs_lookup(HUNG_TASK_DIR, NULL);
> +	if (!hung_task_dir) {
> +		hung_task_dir = debugfs_create_dir(HUNG_TASK_DIR, NULL);
> +		if (IS_ERR(hung_task_dir))
> +			return PTR_ERR(hung_task_dir);
> +	}
>  
> -	debugfs_create_file(HUNG_TASK_FILE, 0400, hung_task_dir,
> -			    NULL, &hung_task_fops);
> +	debugfs_create_file(HUNG_TASK_FILE, 0400, hung_task_dir, NULL,
> +			    &hung_task_fops);
>  
>  	return 0;
>  }
>  
>  static void __exit hung_task_sample_exit(void)
>  {
> -	debugfs_remove_recursive(hung_task_dir);
> +	debugfs_lookup_and_remove(HUNG_TASK_FILE, hung_task_dir);
> +
> +	if (simple_empty(hung_task_dir))
> +		debugfs_remove(hung_task_dir);
>  }
>  
>  module_init(hung_task_sample_init);
> diff --git a/samples/hung_task/hung_task_semaphore.c b/samples/hung_task/hung_task_semaphore.c
> new file mode 100644
> index 000000000000..a5814971bfb8
> --- /dev/null
> +++ b/samples/hung_task/hung_task_semaphore.c
> @@ -0,0 +1,74 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * hung_task_semaphore.c - Sample code which causes hung task by semaphore
> + *
> + * Usage: load this module and read `<debugfs>/hung_task/semaphore`
> + *        by 2 or more processes.
> + *
> + * This is for testing kernel hung_task error message.
> + * Note that this will make your system freeze and maybe
> + * cause panic. So do not use this except for the test.
> + */
> +
> +#include <linux/debugfs.h>
> +#include <linux/delay.h>
> +#include <linux/fs.h>
> +#include <linux/module.h>
> +#include <linux/semaphore.h>
> +
> +#define HUNG_TASK_DIR   "hung_task"
> +#define HUNG_TASK_FILE  "semaphore"
> +#define SLEEP_SECOND 256
> +
> +static const char dummy_string[] = "This is a dummy string.";
> +static DEFINE_SEMAPHORE(dummy_sem, 1);
> +static struct dentry *hung_task_dir;
> +
> +static ssize_t read_dummy(struct file *file, char __user *user_buf,
> +			  size_t count, loff_t *ppos)
> +{
> +	/* If the second task waits on the semaphore, it is uninterruptible sleep. */
> +	down(&dummy_sem);
> +
> +	/* When the first task sleep here, it is interruptible. */
> +	msleep_interruptible(SLEEP_SECOND * 1000);
> +
> +	up(&dummy_sem);
> +
> +	return simple_read_from_buffer(user_buf, count, ppos, dummy_string,
> +				       sizeof(dummy_string));
> +}
> +
> +static const struct file_operations hung_task_fops = {
> +	.read = read_dummy,
> +};
> +
> +static int __init hung_task_sample_init(void)
> +{
> +	hung_task_dir = debugfs_lookup(HUNG_TASK_DIR, NULL);
> +	if (!hung_task_dir) {
> +		hung_task_dir = debugfs_create_dir(HUNG_TASK_DIR, NULL);
> +		if (IS_ERR(hung_task_dir))
> +			return PTR_ERR(hung_task_dir);
> +	}
> +
> +	debugfs_create_file(HUNG_TASK_FILE, 0400, hung_task_dir, NULL,
> +			    &hung_task_fops);
> +
> +	return 0;
> +}
> +
> +static void __exit hung_task_sample_exit(void)
> +{
> +	debugfs_lookup_and_remove(HUNG_TASK_FILE, hung_task_dir);
> +
> +	if (simple_empty(hung_task_dir))
> +		debugfs_remove(hung_task_dir);
> +}
> +
> +module_init(hung_task_sample_init);
> +module_exit(hung_task_sample_exit);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Zi Li");
> +MODULE_DESCRIPTION("Simple sleep under semaphore file for testing hung task");
> -- 
> 2.45.2
> 


-- 
Masami Hiramatsu (Google) <mhiramat@...nel.org>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ