Skip to contentSkip to conversation

x86 stacktrace updates

GIT PULL In review Frederic Weisbecker 5 participants 13 messages May 12, 2011 – Jul 14, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Ingo,

Please pull the perf/stacktrace branch that can be found at:

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
perf/stacktrace

Thanks,
Frederic
---

Frederic Weisbecker (1):
x86: Make the x86-64 stacktrace code safely callable from scheduler

Richard Weinberger (1):
x86: Remove warning and warning_symbol from struct stacktrace_ops

 arch/x86/include/asm/stacktrace.h |    3 ---
 arch/x86/kernel/cpu/perf_event.c  |   13 -------------
 arch/x86/kernel/dumpstack.c       |   16 ----------------
 arch/x86/kernel/dumpstack_64.c    |   14 ++++++++++----
 arch/x86/kernel/stacktrace.c      |   13 -------------
 arch/x86/oprofile/backtrace.c     |   13 -------------
 6 files changed, 10 insertions(+), 62 deletions(-)
#1 lore raw

PATCH 1/2 x86: Remove warning and warning_symbol from struct stacktrace_ops

From: Richard Weinberger <[email protected]>

Both warning and warning_symbol are nowhere used. Let's get rid of them.

Cc Oleg Nesterov <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Soeren Sandmann Pedersen <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Robert Richter <[email protected]>
Cc Paul Mundt <[email protected]>
---
 arch/x86/include/asm/stacktrace.h |    3 ---
 arch/x86/kernel/cpu/perf_event.c  |   13 -------------
 arch/x86/kernel/dumpstack.c       |   16 ----------------
 arch/x86/kernel/stacktrace.c      |   13 -------------
 arch/x86/oprofile/backtrace.c     |   13 -------------
 5 files changed, 0 insertions(+), 58 deletions(-)
arch/x86/include/asm/stacktrace.h +0 -3
index d7e89c8..70bbe39 100644
@@ -37,9 +37,6 @@ print_context_stack_bp(struct thread_info *tinfo,
 /* Generic stack tracer with callbacks */
 
 struct stacktrace_ops {
-	void (*warning)(void *data, char *msg);
-	/* msg must contain %s for the symbol */
-	void (*warning_symbol)(void *data, char *msg, unsigned long symbol);
 	void (*address)(void *data, unsigned long address, int reliable);
 	/* On negative return stop dumping */
 	int (*stack)(void *data, char *name);
arch/x86/kernel/cpu/perf_event.c +0 -13
index 0de6b2b..3a0338b 100644
@@ -1773,17 +1773,6 @@ static struct pmu pmu = {
  * callchain support
  */
 
-static void
-backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
-{
-	/* Ignore warnings */
-}
-
-static void backtrace_warning(void *data, char *msg)
-{
-	/* Ignore warnings */
-}
-
 static int backtrace_stack(void *data, char *name)
 {
 	return 0;
@@ -1797,8 +1786,6 @@ static void backtrace_address(void *data, unsigned long addr, int reliable)
 }
 
 static const struct stacktrace_ops backtrace_ops = {
-	.warning		= backtrace_warning,
-	.warning_symbol		= backtrace_warning_symbol,
 	.stack			= backtrace_stack,
 	.address		= backtrace_address,
 	.walk_stack		= print_context_stack_bp,
arch/x86/kernel/dumpstack.c +0 -16
index e2a3f06..f478ff6 100644
@@ -135,20 +135,6 @@ print_context_stack_bp(struct thread_info *tinfo,
 }
 EXPORT_SYMBOL_GPL(print_context_stack_bp);
 
-
-static void
-print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
-{
-	printk(data);
-	print_symbol(msg, symbol);
-	printk("\n");
-}
-
-static void print_trace_warning(void *data, char *msg)
-{
-	printk("%s%s\n", (char *)data, msg);
-}
-
 static int print_trace_stack(void *data, char *name)
 {
 	printk("%s <%s> ", (char *)data, name);
@@ -166,8 +152,6 @@ static void print_trace_address(void *data, unsigned long addr, int reliable)
 }
 
 static const struct stacktrace_ops print_trace_ops = {
-	.warning		= print_trace_warning,
-	.warning_symbol		= print_trace_warning_symbol,
 	.stack			= print_trace_stack,
 	.address		= print_trace_address,
 	.walk_stack		= print_context_stack,
arch/x86/kernel/stacktrace.c +0 -13
index 6515733..55d9bc0 100644
@@ -9,15 +9,6 @@
 #include <linux/uaccess.h>
 #include <asm/stacktrace.h>
 
-static void save_stack_warning(void *data, char *msg)
-{
-}
-
-static void
-save_stack_warning_symbol(void *data, char *msg, unsigned long symbol)
-{
-}
-
 static int save_stack_stack(void *data, char *name)
 {
 	return 0;
@@ -53,16 +44,12 @@ save_stack_address_nosched(void *data, unsigned long addr, int reliable)
 }
 
 static const struct stacktrace_ops save_stack_ops = {
-	.warning	= save_stack_warning,
-	.warning_symbol	= save_stack_warning_symbol,
 	.stack		= save_stack_stack,
 	.address	= save_stack_address,
 	.walk_stack	= print_context_stack,
 };
 
 static const struct stacktrace_ops save_stack_ops_nosched = {
-	.warning	= save_stack_warning,
-	.warning_symbol	= save_stack_warning_symbol,
 	.stack		= save_stack_stack,
 	.address	= save_stack_address_nosched,
 	.walk_stack	= print_context_stack,
arch/x86/oprofile/backtrace.c +0 -13
index 2d49d4e..a5b64ab 100644
@@ -16,17 +16,6 @@
 #include <asm/stacktrace.h>
 #include <linux/compat.h>
 
-static void backtrace_warning_symbol(void *data, char *msg,
-				     unsigned long symbol)
-{
-	/* Ignore warnings */
-}
-
-static void backtrace_warning(void *data, char *msg)
-{
-	/* Ignore warnings */
-}
-
 static int backtrace_stack(void *data, char *name)
 {
 	/* Yes, we want all stacks */
@@ -42,8 +31,6 @@ static void backtrace_address(void *data, unsigned long addr, int reliable)
 }
 
 static struct stacktrace_ops backtrace_ops = {
-	.warning	= backtrace_warning,
-	.warning_symbol	= backtrace_warning_symbol,
 	.stack		= backtrace_stack,
 	.address	= backtrace_address,
 	.walk_stack	= print_context_stack,
Signature · 1 line
1.7.3.2
#2 lore raw

Re: [PATCH 1/2] x86: Remove warning and warning_symbol from struct stacktrace_ops

Quoted 5 lines · Frederic Weisbecker

From: Richard Weinberger <[email protected]>

Both warning and warning_symbol are nowhere used. Let's get rid of them.

Quoted 13 lines
Cc Oleg Nesterov <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Soeren Sandmann Pedersen <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Robert Richter <[email protected]>
Cc Paul Mundt <[email protected]>

This change breaks systemtap. I know it's out of tree and systemtap doesn't even do anything useful with the symbols as it assigns them to dummy callbacks but the breakage is there.

Signature · 2 lines
Mel Gorman
SUSE Labs
#3 lore raw

Re: [PATCH 1/2] x86: Remove warning and warning_symbol from struct stacktrace_ops

Quoted 20 lines · Mel Gorman
Quoted 5 lines · Frederic Weisbecker

From: Richard Weinberger <[email protected]>

Both warning and warning_symbol are nowhere used. Let's get rid of them.

Quoted 13 lines
Cc Oleg Nesterov <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Soeren Sandmann Pedersen <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Robert Richter <[email protected]>
Cc Paul Mundt <[email protected]>
Quoted 3 lines

This change breaks systemtap. I know it's out of tree and systemtap doesn't even do anything useful with the symbols as it assigns them to dummy callbacks but the breakage is there.

Then it's up to systemtap developers to deal with that. We don't maintain out-of-tree.

#4 lore raw

PATCH 2/2 x86: Make the x86-64 stacktrace code safely callable from scheduler

In reply to Frederic Weisbecker

Avoid potential scheduler recursion and deadlock from the stacktrace code by avoiding rescheduling when we re-enable preemption.

This robustifies some scheduler trace events like sched switch when they are used to produce callchains in perf or ftrace.

Cc Peter Zijlstra <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Steven Rostedt <[email protected]>
---
 arch/x86/kernel/dumpstack_64.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)
arch/x86/kernel/dumpstack_64.c +10 -4
index e71c98d..8e4f1d0 100644
@@ -143,13 +143,16 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
 		unsigned long *stack, unsigned long bp,
 		const struct stacktrace_ops *ops, void *data)
 {
-	const unsigned cpu = get_cpu();
-	unsigned long *irq_stack_end =
-		(unsigned long *)per_cpu(irq_stack_ptr, cpu);
+	unsigned long *irq_stack_end;
 	unsigned used = 0;
 	struct thread_info *tinfo;
 	int graph = 0;
 	unsigned long dummy;
+	int cpu;
+
+	preempt_disable();
+
+	cpu = smp_processor_id();
 
 	if (!task)
 		task = current;
@@ -168,6 +171,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
 	 * exceptions
 	 */
 	tinfo = task_thread_info(task);
+	irq_stack_end = (unsigned long *)__get_cpu_var(irq_stack_ptr);
 	for (;;) {
 		char *id;
 		unsigned long *estack_end;
@@ -219,7 +223,9 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
 	 * This handles the process stack:
 	 */
 	bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
-	put_cpu();
+
+	/* We want stacktrace to be computable anywhere, even in the scheduler */
+	preempt_enable_no_resched();
 }
 EXPORT_SYMBOL(dump_trace);
 
Signature · 1 line
1.7.3.2
#5 lore raw

Re: [PATCH 2/2] x86: Make the x86-64 stacktrace code safely callable from scheduler

Quoted 6 lines · Frederic Weisbecker

Avoid potential scheduler recursion and deadlock from the stacktrace code by avoiding rescheduling when we re-enable preemption.

This robustifies some scheduler trace events like sched switch when they are used to produce callchains in perf or ftrace.

Actually trace events are already safe because they already disable preemption so callchains won't call schedule() there. But still let's be careful, we never know how stacktrace can be used.

#6 lore raw

Re: [PATCH 2/2] x86: Make the x86-64 stacktrace code safely callable from scheduler

In reply to Frederic Weisbecker

arch/x86/kernel/dumpstack_64.c void dump_trace(struct task_struct *task, struct pt_regs *regs, · line 228

Quoted 11 lines · Frederic Weisbecker

Avoid potential scheduler recursion and deadlock from the stacktrace code by avoiding rescheduling when we re-enable preemption.

This robustifies some scheduler trace events like sched switch when they are used to produce callchains in perf or ftrace.

arch/x86/kernel/dumpstack_64.c +3 -1
-	put_cpu();
+
+	/* We want stacktrace to be computable anywhere, even in the scheduler */
+	preempt_enable_no_resched();

So what happens if a callchain profiling happens to be interrupted by a hardirq and the interrupt reschedules the current task? We'll miss the reschedule, right?

preempt_enable_no_resched() is not a magic 'solve scheduler recursions' bullet
- it's to be used only if something else will guarantee the preemption check! But nothing guarantees it here AFAICS.

A better fix would be to use local_irq_save()/restore().

Thanks,

Ingo

#7 lore raw

Re: [PATCH 2/2] x86: Make the x86-64 stacktrace code safely callable from scheduler

Quoted 19 lines · Ingo Molnar

arch/x86/kernel/dumpstack_64.c void dump_trace(struct task_struct *task, struct pt_regs *regs, · line 228

Quoted 11 lines · Frederic Weisbecker

Avoid potential scheduler recursion and deadlock from the stacktrace code by avoiding rescheduling when we re-enable preemption.

This robustifies some scheduler trace events like sched switch when they are used to produce callchains in perf or ftrace.

arch/x86/kernel/dumpstack_64.c +3 -1
-	put_cpu();
+
+	/* We want stacktrace to be computable anywhere, even in the scheduler */
+	preempt_enable_no_resched();

So what happens if a callchain profiling happens to be interrupted by a hardirq and the interrupt reschedules the current task? We'll miss the reschedule, right?

Quoted 5 lines

preempt_enable_no_resched() is not a magic 'solve scheduler recursions' bullet
- it's to be used only if something else will guarantee the preemption check! But nothing guarantees it here AFAICS.

A better fix would be to use local_irq_save()/restore().

Good point, but then lockdep itself might trigger a stacktrace from local_irq_save, leading to a stacktrace recursion.

I can use raw_local_irq_disable(), or may be have a stacktrace recursion protection. I fear the second solution could lead us to potentially lose useful information if a stacktrace interrupts another one. Ok these are extreme cases...

#8 lore raw

Re: [PATCH 2/2] x86: Make the x86-64 stacktrace code safely callable from scheduler

Quoted 26 lines · Frederic Weisbecker
Quoted 19 lines · Ingo Molnar

arch/x86/kernel/dumpstack_64.c void dump_trace(struct task_struct *task, struct pt_regs *regs, · line 228

Quoted 11 lines · Frederic Weisbecker

Avoid potential scheduler recursion and deadlock from the stacktrace code by avoiding rescheduling when we re-enable preemption.

This robustifies some scheduler trace events like sched switch when they are used to produce callchains in perf or ftrace.

arch/x86/kernel/dumpstack_64.c +3 -1
-	put_cpu();
+
+	/* We want stacktrace to be computable anywhere, even in the scheduler */
+	preempt_enable_no_resched();

So what happens if a callchain profiling happens to be interrupted by a hardirq and the interrupt reschedules the current task? We'll miss the reschedule, right?

Quoted 5 lines

preempt_enable_no_resched() is not a magic 'solve scheduler recursions' bullet
- it's to be used only if something else will guarantee the preemption check! But nothing guarantees it here AFAICS.

A better fix would be to use local_irq_save()/restore().

Quoted 7 lines

Good point, but then lockdep itself might trigger a stacktrace from local_irq_save, leading to a stacktrace recursion.

I can use raw_local_irq_disable(), or may be have a stacktrace recursion protection. I fear the second solution could lead us to potentially lose useful information if a stacktrace interrupts another one. Ok these are extreme cases...

i think raw_local_irq_disable() would be justified in this case.

Thanks,

Ingo

#9 lore raw

Re: [PATCH 2/2] x86: Make the x86-64 stacktrace code safely callable from scheduler

In reply to Frederic Weisbecker
Quoted 3 lines · Frederic Weisbecker

Avoid potential scheduler recursion and deadlock from the stacktrace code by avoiding rescheduling when we re-enable preemption.

I'm curious to where you saw this deadlock? As I have the function stack tracer using preempt_disable_notrace and enable_notrace without any issues, and it traces all functions in the kernel[*]. I have no issue with using raw_local_irq_save/restore() if it is to protect the per_cpu variable from interrupt corruption, but I don't see the problem with recursion.

There's only one function I had to worry about with preempt disable, not the entire scheduler. That was the function preempt_schedule(). This function is called by preempt_enable() and that will cause an infinite loop if you have something in preempt_schedule() call preempt_enable().

Remember that ftrace_preempt_disable/enable() crap that I did to try to avoid the scheduler deadlock? I found it was complex and unnecessary because the scheduler itself was not an issue, it was only preempt_schedule(). I replaced all that crappy code with a single line that added notrace to preempt_schedule() and everything just worked.

Thus, if you disable interrupts to protect the cpu data, that's fine, and say so in the change log. I really like to know if you really saw this deadlock. Yes enabling preemption in the scheduler may recurse, but it will only do so once.

I still argue that interrupt enabling is slow. I've seen a large slow down of the code by switching stack tracer from preempt disable to irq disable. I used perf to see why, and it told me that disabling interrupts as fine, but enabling interrupts can cost you quite a bit.

-- Steve

[*] of course function tracing does not trace other notrace functions.

#10 lore raw

Re: [PATCH 2/2] x86: Make the x86-64 stacktrace code safely callable from scheduler

Quoted 28 lines · Steven Rostedt
Quoted 3 lines · Frederic Weisbecker

Avoid potential scheduler recursion and deadlock from the stacktrace code by avoiding rescheduling when we re-enable preemption.

I'm curious to where you saw this deadlock? As I have the function stack tracer using preempt_disable_notrace and enable_notrace without any issues, and it traces all functions in the kernel[*]. I have no issue with using raw_local_irq_save/restore() if it is to protect the per_cpu variable from interrupt corruption, but I don't see the problem with recursion.

There's only one function I had to worry about with preempt disable, not the entire scheduler. That was the function preempt_schedule(). This function is called by preempt_enable() and that will cause an infinite loop if you have something in preempt_schedule() call preempt_enable().

Remember that ftrace_preempt_disable/enable() crap that I did to try to avoid the scheduler deadlock? I found it was complex and unnecessary because the scheduler itself was not an issue, it was only preempt_schedule(). I replaced all that crappy code with a single line that added notrace to preempt_schedule() and everything just worked.

Thus, if you disable interrupts to protect the cpu data, that's fine, and say so in the change log. I really like to know if you really saw this deadlock. Yes enabling preemption in the scheduler may recurse, but it will only do so once.

Quoted 9 lines

I still argue that interrupt enabling is slow. I've seen a large slow down of the code by switching stack tracer from preempt disable to irq disable. I used perf to see why, and it told me that disabling interrupts as fine, but enabling interrupts can cost you quite a bit.

-- Steve

[*] of course function tracing does not trace other notrace functions.

I haven't observed any deadlock. trace events disable preemption and other tracers do too (my changelog was buggy).

I just worried about potential other users, like a WARN_ON in the scheduler or so.

My worry is the following scenario:

schedule() {
	acquire(rq)
							set_tsk_need_resched
	WARN_ON() {
		stack_trace() {
			preempt_enable() {
				preempt_schedule() {
					acquire(rq)
				}
			}
		}
	}
}

I don't know if it happens that one set TIF_NEED_RESCHED remotely, or if TIF_NEED_RESCHED can be set when we hold the rq, and then we can be followed by a WARN_ON, ...
So I preferred to be careful.

#11 lore raw

Re: [PATCH 2/2] x86: Make the x86-64 stacktrace code safely callable from scheduler

Quoted 8 lines · Frederic Weisbecker

I haven't observed any deadlock. trace events disable preemption and other tracers do too (my changelog was buggy).

I just worried about potential other users, like a WARN_ON in the scheduler or so.

My worry is the following scenario:

Quoted 7 lines
schedule() {
        acquire(rq)
                                                        set_tsk_need_resched
        WARN_ON() {
                stack_trace() {
                        preempt_enable() {
                                preempt_schedule() {

Would never happen, because rq->lock is a spinlock which holds another preempt count so preempt_enable() would never schedule.

Quoted 10 lines
                                        acquire(rq)
                                }
                        }
                }
        }
}

I don't know if it happens that one set TIF_NEED_RESCHED remotely,

Yes

Quoted 1 line

or if TIF_NEED_RESCHED can be set when we hold the rq,

Yes

Quoted 2 lines

and then we
can be followed by a WARN_ON, ...

Not quite sure, but possible.

Quoted 1 line

So I preferred to be careful.

Still not quite seeing how all things could go bang.

#12 lore raw

Re: [PATCH 2/2] x86: Make the x86-64 stacktrace code safely callable from scheduler

Quoted 17 lines · Peter Zijlstra
Quoted 8 lines · Frederic Weisbecker

I haven't observed any deadlock. trace events disable preemption and other tracers do too (my changelog was buggy).

I just worried about potential other users, like a WARN_ON in the scheduler or so.

My worry is the following scenario:

Quoted 7 lines
schedule() {
        acquire(rq)
                                                        set_tsk_need_resched
        WARN_ON() {
                stack_trace() {
                        preempt_enable() {
                                preempt_schedule() {
Quoted 2 lines

Would never happen, because rq->lock is a spinlock which holds another preempt count so preempt_enable() would never schedule.

Oh right.

Quoted 18 lines
Quoted 11 lines
                                        acquire(rq)
                                }
                        }
                }
        }
}

I don't know if it happens that one set TIF_NEED_RESCHED remotely,

Yes

Quoted 1 line

or if TIF_NEED_RESCHED can be set when we hold the rq,

Yes

Quoted 8 lines
Quoted 2 lines

and then we
can be followed by a WARN_ON, ...

Not quite sure, but possible.

Quoted 1 line

So I preferred to be careful.

Still not quite seeing how all things could go bang.

Nah, forget about that, I was just confused ;)