mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
To: Lance Yang <ioworker0@gmail.com>
Cc: akpm@linux-foundation.org, will@kernel.org, peterz@infradead.org,
	mingo@redhat.com, longman@redhat.com, mhiramat@kernel.org,
	anna.schumaker@oracle.com, boqun.feng@gmail.com,
	joel.granados@kernel.org, kent.overstreet@linux.dev,
	leonylgao@tencent.com, linux-kernel@vger.kernel.org,
	rostedt@goodmis.org, senozhatsky@chromium.org,
	tfiga@chromium.org, amaindex@outlook.com
Subject: Re: [PATCH RESEND v2 3/3] samples: add hung_task detector semaphore blocking sample
Date: Tue, 18 Mar 2025 10:36:15 +0900	[thread overview]
Message-ID: <20250318103615.bce4a3291786c2bd525f0f02@kernel.org> (raw)
In-Reply-To: <20250314144300.32542-4-ioworker0@gmail.com>

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

> From: Zi Li <amaindex@outlook.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@gmail.com>
> Signed-off-by: Zi Li <amaindex@outlook.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@kernel.org>

  reply	other threads:[~2025-03-18  1:36 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-14 14:42 [PATCH RESEND v2 0/3] hung_task: extend blocking task stacktrace dump to semaphore Lance Yang
2025-03-14 14:42 ` [PATCH RESEND v2 1/3] hung_task: replace blocker_mutex with encoded blocker Lance Yang
2025-03-18  9:41   ` Masami Hiramatsu
2025-03-18 11:35     ` Lance Yang
2025-03-14 14:42 ` [PATCH RESEND v2 2/3] hung_task: show the blocker task if the task is hung on semaphore Lance Yang
2025-03-19 11:55   ` Masami Hiramatsu
2025-03-19 12:11     ` Lance Yang
2025-03-14 14:43 ` [PATCH RESEND v2 3/3] samples: add hung_task detector semaphore blocking sample Lance Yang
2025-03-18  1:36   ` Masami Hiramatsu [this message]
2025-03-18  6:20     ` Amaindex
2025-03-14 17:37 ` [PATCH RESEND v2 0/3] hung_task: extend blocking task stacktrace dump to semaphore Boqun Feng
2025-03-15  3:18   ` Lance Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250318103615.bce4a3291786c2bd525f0f02@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=amaindex@outlook.com \
    --cc=anna.schumaker@oracle.com \
    --cc=boqun.feng@gmail.com \
    --cc=ioworker0@gmail.com \
    --cc=joel.granados@kernel.org \
    --cc=kent.overstreet@linux.dev \
    --cc=leonylgao@tencent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=tfiga@chromium.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®