Skip to content

Commit 4f48f8b

Browse files
Lai Jiangshanrostedt
authored andcommitted
tracing: Fix circular dead lock in stack trace
When we cat <debugfs>/tracing/stack_trace, we may cause circular lock: sys_read() t_start() arch_spin_lock(&max_stack_lock); t_show() seq_printf(), vsnprintf() .... /* they are all trace-able, when they are traced, max_stack_lock may be required again. */ The following script can trigger this circular dead lock very easy: #!/bin/bash echo 1 > /proc/sys/kernel/stack_tracer_enabled mount -t debugfs xxx /mnt > /dev/null 2>&1 ( # make check_stack() zealous to require max_stack_lock for ((; ;)) { echo 1 > /mnt/tracing/stack_max_size } ) & for ((; ;)) { cat /mnt/tracing/stack_trace > /dev/null } To fix this bug, we increase the percpu trace_active before require the lock. Reported-by: Li Zefan <lizf@cn.fujitsu.com> Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com> LKML-Reference: <4B67D4F9.9080905@cn.fujitsu.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent ab65832 commit 4f48f8b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

kernel/trace/trace_stack.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ stack_max_size_write(struct file *filp, const char __user *ubuf,
157157
unsigned long val, flags;
158158
char buf[64];
159159
int ret;
160+
int cpu;
160161

161162
if (count >= sizeof(buf))
162163
return -EINVAL;
@@ -171,9 +172,20 @@ stack_max_size_write(struct file *filp, const char __user *ubuf,
171172
return ret;
172173

173174
local_irq_save(flags);
175+
176+
/*
177+
* In case we trace inside arch_spin_lock() or after (NMI),
178+
* we will cause circular lock, so we also need to increase
179+
* the percpu trace_active here.
180+
*/
181+
cpu = smp_processor_id();
182+
per_cpu(trace_active, cpu)++;
183+
174184
arch_spin_lock(&max_stack_lock);
175185
*ptr = val;
176186
arch_spin_unlock(&max_stack_lock);
187+
188+
per_cpu(trace_active, cpu)--;
177189
local_irq_restore(flags);
178190

179191
return count;
@@ -206,7 +218,13 @@ t_next(struct seq_file *m, void *v, loff_t *pos)
206218

207219
static void *t_start(struct seq_file *m, loff_t *pos)
208220
{
221+
int cpu;
222+
209223
local_irq_disable();
224+
225+
cpu = smp_processor_id();
226+
per_cpu(trace_active, cpu)++;
227+
210228
arch_spin_lock(&max_stack_lock);
211229

212230
if (*pos == 0)
@@ -217,7 +235,13 @@ static void *t_start(struct seq_file *m, loff_t *pos)
217235

218236
static void t_stop(struct seq_file *m, void *p)
219237
{
238+
int cpu;
239+
220240
arch_spin_unlock(&max_stack_lock);
241+
242+
cpu = smp_processor_id();
243+
per_cpu(trace_active, cpu)--;
244+
221245
local_irq_enable();
222246
}
223247

0 commit comments

Comments
 (0)