public inbox for linux-trace-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] tracing: Fix deadlock in hwlat tracer
@ 2024-11-21  2:18 Wei Li
  2024-11-21  2:18 ` [PATCH v2 1/2] tracing: Allow custom read/write processing in trace_min_max_{write,read}() Wei Li
  2024-11-21  2:18 ` [PATCH v2 2/2] tracing/hwlat: Fix deadlock in cpuhp processing Wei Li
  0 siblings, 2 replies; 4+ messages in thread
From: Wei Li @ 2024-11-21  2:18 UTC (permalink / raw)
  To: linux-trace-kernel
  Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers, xiexiuqi

This patchset fixes a deadlock issue found in concurrent CPU-hotplug and
tracer-toggling testing:

Background: *test_hotplug.sh*
```
#!/bin/sh

while true
do
	echo 0 > /sys/devices/system/cpu/cpu1/online
	echo 1 > /sys/devices/system/cpu/cpu1/online
done
```

Test: *test_hwlat.sh*
```
#!/bin/sh

echo per-cpu > /sys/kernel/debug/tracing/hwlat_detector/mode
while true
do
	echo hwlat > /sys/kernel/debug/tracing/current_tracer
	echo nop > /sys/kernel/debug/tracing/current_tracer
done
```

v1 -> v2:
 - The previous solution try to make the mutex obtaining in kthread_fn()
 interruptible to fix this issue. To make the logic clearer, change to
 remove the mutex_lock in this version. 

Links:
 v1: https://lore.kernel.org/linux-trace-kernel/20240924094515.3561410-6-liwei391@huawei.com/

Wei Li (2):
  tracing: Allow custom read/write processing in
    trace_min_max_{write,read}()
  tracing/hwlat: Fix deadlock in cpuhp processing

 kernel/trace/trace.c       | 13 ++++++++++---
 kernel/trace/trace.h       |  2 ++
 kernel/trace/trace_hwlat.c | 15 +++++++++++----
 3 files changed, 23 insertions(+), 7 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] tracing: Allow custom read/write processing in trace_min_max_{write,read}()
  2024-11-21  2:18 [PATCH v2 0/2] tracing: Fix deadlock in hwlat tracer Wei Li
@ 2024-11-21  2:18 ` Wei Li
  2024-12-11  3:56   ` Steven Rostedt
  2024-11-21  2:18 ` [PATCH v2 2/2] tracing/hwlat: Fix deadlock in cpuhp processing Wei Li
  1 sibling, 1 reply; 4+ messages in thread
From: Wei Li @ 2024-11-21  2:18 UTC (permalink / raw)
  To: linux-trace-kernel
  Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers, xiexiuqi

The 'trace_min_max_fops' implements a generic function to read/write u64
values from tracefs, add support of custom read/write processing to allow
special requirements.

Signed-off-by: Wei Li <liwei391@huawei.com>
---
 kernel/trace/trace.c | 13 ++++++++++---
 kernel/trace/trace.h |  2 ++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 2b64b3ec67d9..ab5ea6d7148c 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -7689,8 +7689,12 @@ trace_min_max_write(struct file *filp, const char __user *ubuf, size_t cnt, loff
 	if (param->max && val > *param->max)
 		err = -EINVAL;
 
-	if (!err)
-		*param->val = val;
+	if (!err) {
+		if (unlikely(param->write))
+			param->write(param->val, val);
+		else
+			*param->val = val;
+	}
 
 	if (param->lock)
 		mutex_unlock(param->lock);
@@ -7723,7 +7727,10 @@ trace_min_max_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppo
 	if (!param)
 		return -EFAULT;
 
-	val = *param->val;
+	if (unlikely(param->read))
+		val = param->read(param->val);
+	else
+		val = *param->val;
 
 	if (cnt > sizeof(buf))
 		cnt = sizeof(buf);
diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index c866991b9c78..2aaf3030c466 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -2159,6 +2159,8 @@ static inline void sanitize_event_name(char *name)
 struct trace_min_max_param {
 	struct mutex	*lock;
 	u64		*val;
+	u64		(*read)(u64 *val);
+	void		(*write)(u64 *val, u64 data);
 	u64		*min;
 	u64		*max;
 };
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] tracing/hwlat: Fix deadlock in cpuhp processing
  2024-11-21  2:18 [PATCH v2 0/2] tracing: Fix deadlock in hwlat tracer Wei Li
  2024-11-21  2:18 ` [PATCH v2 1/2] tracing: Allow custom read/write processing in trace_min_max_{write,read}() Wei Li
@ 2024-11-21  2:18 ` Wei Li
  1 sibling, 0 replies; 4+ messages in thread
From: Wei Li @ 2024-11-21  2:18 UTC (permalink / raw)
  To: linux-trace-kernel
  Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers, xiexiuqi

One "hung task" error was reported during the test, and i figured out
the deadlock scenario is as follows:

T1 [BP]               | T2 [AP]                     | T3 [hwlatd/1]                  | T4
work_for_cpu_fn()     | cpuhp_thread_fun()          | kthread_fn()                   | hwlat_hotplug_workfn()
  _cpu_down()         |   stop_cpu_kthread()        |                                |   mutex_lock(&hwlat_data.lock)
    cpus_write_lock() |     kthread_stop(hwlatd/1)  |   mutex_lock(&hwlat_data.lock) |
    __cpuhp_kick_ap() |       wait_for_completion() |                                |   cpus_read_lock()

It constitutes ABBA deadlock indirectly between "cpu_hotplug_lock" and
"hwlat_data.lock", remove the mutex_lock in kthread_fn() to fix this.

Fixes: ba998f7d9531 ("trace/hwlat: Support hotplug operations")
Signed-off-by: Wei Li <liwei391@huawei.com>
---
 kernel/trace/trace_hwlat.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/trace_hwlat.c b/kernel/trace/trace_hwlat.c
index 3bd6071441ad..7d50b5ea999a 100644
--- a/kernel/trace/trace_hwlat.c
+++ b/kernel/trace/trace_hwlat.c
@@ -108,6 +108,7 @@ static struct hwlat_data {
 
 	u64	sample_window;		/* total sampling window (on+off) */
 	u64	sample_width;		/* active sampling portion of window */
+	u64	sample_interval;	/* sample_window - sample_width */
 
 	int	thread_mode;		/* thread mode */
 
@@ -370,10 +371,7 @@ static int kthread_fn(void *data)
 		get_sample();
 		local_irq_enable();
 
-		mutex_lock(&hwlat_data.lock);
-		interval = hwlat_data.sample_window - hwlat_data.sample_width;
-		mutex_unlock(&hwlat_data.lock);
-
+		interval = READ_ONCE(hwlat_data.sample_interval);
 		do_div(interval, USEC_PER_MSEC); /* modifies interval value */
 
 		/* Always sleep for at least 1ms */
@@ -729,6 +727,13 @@ static ssize_t hwlat_mode_write(struct file *filp, const char __user *ubuf,
 	return ret;
 }
 
+static void hwlat_width_window_write(u64 *val, u64 data)
+{
+	*val = data;
+	WRITE_ONCE(hwlat_data.sample_interval,
+		   hwlat_data.sample_window - hwlat_data.sample_width);
+}
+
 /*
  * The width parameter is read/write using the generic trace_min_max_param
  * method. The *val is protected by the hwlat_data lock and is upper
@@ -737,6 +742,7 @@ static ssize_t hwlat_mode_write(struct file *filp, const char __user *ubuf,
 static struct trace_min_max_param hwlat_width = {
 	.lock		= &hwlat_data.lock,
 	.val		= &hwlat_data.sample_width,
+	.write		= &hwlat_width_window_write,
 	.max		= &hwlat_data.sample_window,
 	.min		= NULL,
 };
@@ -749,6 +755,7 @@ static struct trace_min_max_param hwlat_width = {
 static struct trace_min_max_param hwlat_window = {
 	.lock		= &hwlat_data.lock,
 	.val		= &hwlat_data.sample_window,
+	.write		= &hwlat_width_window_write,
 	.max		= NULL,
 	.min		= &hwlat_data.sample_width,
 };
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] tracing: Allow custom read/write processing in trace_min_max_{write,read}()
  2024-11-21  2:18 ` [PATCH v2 1/2] tracing: Allow custom read/write processing in trace_min_max_{write,read}() Wei Li
@ 2024-12-11  3:56   ` Steven Rostedt
  0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2024-12-11  3:56 UTC (permalink / raw)
  To: Wei Li; +Cc: linux-trace-kernel, Masami Hiramatsu, Mathieu Desnoyers, xiexiuqi

On Thu, 21 Nov 2024 10:18:22 +0800
Wei Li <liwei391@huawei.com> wrote:

> The 'trace_min_max_fops' implements a generic function to read/write
> u64 values from tracefs, add support of custom read/write processing
> to allow special requirements.


Instead of making a new parameter in trace_min_max_param, why not just
change the hwlat "width" file to have its own ops. Then you can make
the trace_min_max_read() non-static, reuse that, and then have the
hwlat use its own write function. It would make it much more straight
forward.

That could be done for a fix that would be tagged as stable, and it
would also be a single patch.

Really, for a clean up (after that is done), the trace_min_max_fops
should be replaced to get rid of the "lock" parameter. Perhaps have a
trace_min_max_lock_fops. But Linus hates conditional locking, and I
don't blame him. I'm trying to clean up this code with using guard()s
and that conditional locking makes that not possible.

-- Steve



> 
> Signed-off-by: Wei Li <liwei391@huawei.com>
> ---
>  kernel/trace/trace.c | 13 ++++++++++---
>  kernel/trace/trace.h |  2 ++
>  2 files changed, 12 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 2b64b3ec67d9..ab5ea6d7148c 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -7689,8 +7689,12 @@ trace_min_max_write(struct file *filp, const
> char __user *ubuf, size_t cnt, loff if (param->max && val >
> *param->max) err = -EINVAL;
>  
> -	if (!err)
> -		*param->val = val;
> +	if (!err) {
> +		if (unlikely(param->write))
> +			param->write(param->val, val);
> +		else
> +			*param->val = val;
> +	}
>  
>  	if (param->lock)
>  		mutex_unlock(param->lock);
> @@ -7723,7 +7727,10 @@ trace_min_max_read(struct file *filp, char
> __user *ubuf, size_t cnt, loff_t *ppo if (!param)
>  		return -EFAULT;
>  
> -	val = *param->val;
> +	if (unlikely(param->read))
> +		val = param->read(param->val);
> +	else
> +		val = *param->val;
>  
>  	if (cnt > sizeof(buf))
>  		cnt = sizeof(buf);
> diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
> index c866991b9c78..2aaf3030c466 100644
> --- a/kernel/trace/trace.h
> +++ b/kernel/trace/trace.h
> @@ -2159,6 +2159,8 @@ static inline void sanitize_event_name(char
> *name) struct trace_min_max_param {
>  	struct mutex	*lock;
>  	u64		*val;
> +	u64		(*read)(u64 *val);
> +	void		(*write)(u64 *val, u64 data);
>  	u64		*min;
>  	u64		*max;
>  };


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-12-11  3:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-21  2:18 [PATCH v2 0/2] tracing: Fix deadlock in hwlat tracer Wei Li
2024-11-21  2:18 ` [PATCH v2 1/2] tracing: Allow custom read/write processing in trace_min_max_{write,read}() Wei Li
2024-12-11  3:56   ` Steven Rostedt
2024-11-21  2:18 ` [PATCH v2 2/2] tracing/hwlat: Fix deadlock in cpuhp processing Wei Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox