Thread (4 messages) 4 messages, 2 authors, 2024-12-11
STALE536d

[PATCH v2 2/2] tracing/hwlat: Fix deadlock in cpuhp processing

From: Wei Li <hidden>
Date: 2024-11-21 02:06:14
Subsystem: the rest, tracing, tracing os noise / latency tracers · Maintainers: Linus Torvalds, Steven Rostedt, Masami Hiramatsu

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 <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help