x86/dumpstack: more stack dump improvements
Subsystems (5)
cover x86/dumpstack: more stack dump improvements
Some more preparatory patches for the new x86 unwinder (though these generally stand alone as improvements in their own right).
Josh Poimboeuf (6):
perf/x86: check perf_callchain_store() error oprofile/x86: add regs->ip to oprofile trace x86/dumpstack: make printk_stack_address() more generally useful x86/dumpstack: add get_stack_pointer() and get_frame_pointer() x86/dumpstack: remove unnecessary stack pointer arguments x86/dumpstack: allow preemption in show_stack_log_lvl() and
dump_trace()
arch/x86/events/core.c | 3 +- arch/x86/include/asm/stacktrace.h | 41 ++++++++++++++------------ arch/x86/kernel/dumpstack.c | 13 ++++----- arch/x86/kernel/dumpstack_32.c | 41 +++++++------------------- arch/x86/kernel/dumpstack_64.c | 61 +++++++++------------------------------ arch/x86/oprofile/backtrace.c | 12 +++++--- 6 files changed, 64 insertions(+), 107 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/6 perf/x86: check perf_callchain_store() error
Add a check to perf_callchain_kernel() so that it returns early if the callchain entry array is already full.
--- arch/x86/events/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/events/core.c +2 -1
@@ -2297,7 +2297,8 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *rereturn ; } -perf_callchain_store (entry, regs->ip); +if (perf_callchain_store (entry, regs->ip)) +return ;dump_trace (NULL , regs,NULL ,0 , &backtrace_ops, entry); }
Signature · 1 line
2.7.4
PATCH 3/6 x86/dumpstack: make printk_stack_address() more generally useful
In reply to Josh PoimboeufChange printk_stack_address() to be useful when called by an unwinder outside the context of dump_trace().
Specifically:
- printk_stack_address()'s 'data' argument is always used as the log level string. Make that explicit.
- Call touch_nmi_watchdog().
--- arch/x86/kernel/dumpstack.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/dumpstack.c +3 -3
@@ -26,10 +26,11 @@ int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;static int die_counter;static void printk_stack_address (unsigned long address,int reliable, -void *data) +char *log_lvl) { +touch_nmi_watchdog ();printk ("%s [<%p>] %s%pB\n" , - (char *)data, (void *)address, reliable ?"" :"? " , + log_lvl, (void *)address, reliable ?"" :"? " , (void *)address); }
@@ -148,7 +149,6 @@ static int print_trace_stack(void *data, char *name)*/ static int print_trace_address (void *data,unsigned long addr,int reliable) { -touch_nmi_watchdog ();printk_stack_address (addr, reliable, data);return 0 ; }
Signature · 1 line
2.7.4
PATCH 2/6 oprofile/x86: add regs->ip to oprofile trace
In reply to Josh Poimboeufdump_trace() doesn't add the interrupted instruction's address to the trace, so add it manually. This makes the profile more useful, and also makes it more consistent with what perf profiling does.
--- arch/x86/oprofile/backtrace.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/oprofile/backtrace.c +10 -3
@@ -114,9 +114,16 @@ x86_backtrace(struct pt_regs * const regs, unsigned int depth)if (!user_mode (regs)) {unsigned long stack =kernel_stack_pointer (regs); -if (depth) -dump_trace (NULL , regs, (unsigned long *)stack,0 , - &backtrace_ops, &depth); + +if (!depth) +return ; + +oprofile_add_trace (regs->ip); +if (!--depth) +return ; + +dump_trace (NULL , regs, (unsigned long *)stack,0 , + &backtrace_ops, &depth);return ; }
Signature · 1 line
2.7.4
PATCH 5/6 x86/dumpstack: remove unnecessary stack pointer arguments
In reply to Josh PoimboeufWhen calling show_stack_log_lvl() or dump_trace() with a regs argument, providing a stack pointer or frame pointer is redundant.
--- arch/x86/kernel/dumpstack.c | 2 +- arch/x86/kernel/dumpstack_32.c | 2 +- arch/x86/kernel/dumpstack_64.c | 5 +---- arch/x86/oprofile/backtrace.c | 5 +---- 4 files changed, 4 insertions(+), 10 deletions(-)
arch/x86/kernel/dumpstack.c +1 -1
@@ -185,7 +185,7 @@ void show_stack(struct task_struct *task, unsigned long *sp)void show_stack_regs (struct pt_regs *regs) { -show_stack_log_lvl (current, regs, (unsigned long *)regs->sp, regs->bp,"" ); +show_stack_log_lvl (current, regs,NULL ,0 ,"" ); }static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
arch/x86/kernel/dumpstack_32.c +1 -1
@@ -122,7 +122,7 @@ void show_regs(struct pt_regs *regs)u8 *ip;pr_emerg ("Stack:\n" ); -show_stack_log_lvl (NULL , regs, ®s->sp,0 ,KERN_EMERG ); +show_stack_log_lvl (NULL , regs,NULL ,0 ,KERN_EMERG );pr_emerg ("Code:" );
arch/x86/kernel/dumpstack_64.c +1 -4
@@ -283,9 +283,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,void show_regs (struct pt_regs *regs) {int i; -unsigned long sp; - sp = regs->sp;show_regs_print_info (KERN_DEFAULT );__show_regs (regs,1 );
@@ -300,8 +298,7 @@ void show_regs(struct pt_regs *regs)u8 *ip;printk (KERN_DEFAULT "Stack:\n" ); -show_stack_log_lvl (NULL , regs, (unsigned long *)sp, -0 ,KERN_DEFAULT ); +show_stack_log_lvl (NULL , regs,NULL ,0 ,KERN_DEFAULT );printk (KERN_DEFAULT "Code: " );
arch/x86/oprofile/backtrace.c +1 -4
@@ -113,8 +113,6 @@ x86_backtrace(struct pt_regs * const regs, unsigned int depth)struct stack_frame *head = (struct stack_frame *)frame_pointer (regs);if (!user_mode (regs)) { -unsigned long stack =kernel_stack_pointer (regs); -if (!depth)return ;
@@ -122,8 +120,7 @@ x86_backtrace(struct pt_regs * const regs, unsigned int depth)if (!--depth)return ; -dump_trace (NULL , regs, (unsigned long *)stack,0 , - &backtrace_ops, &depth); +dump_trace (NULL , regs,NULL ,0 , &backtrace_ops, &depth);return ; }
Signature · 1 line
2.7.4
PATCH 6/6 x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace()
In reply to Josh Poimboeufshow_stack_log_lvl() and dump_trace() are already preemption safe:
- If they're running in irq or exception context, preemption is already disabled and the percpu stack pointers can be trusted.
- If they're running with preemption enabled, they must be running on the task stack anyway, so it doesn't matter if they're comparing the stack pointer against a percpu stack pointer from this CPU or another
one: either way it won't match.
--- arch/x86/kernel/dumpstack_32.c | 14 ++++++-------- arch/x86/kernel/dumpstack_64.c | 26 +++++++++----------------- 2 files changed, 15 insertions(+), 25 deletions(-)
arch/x86/kernel/dumpstack_32.c +6 -8
@@ -24,16 +24,16 @@ static void *is_irq_stack(void *p, void *irq) } -static void *is_hardirq_stack (unsigned long *stack,int cpu) +static void *is_hardirq_stack (unsigned long *stack) { -void *irq =per_cpu (hardirq_stack, cpu); +void *irq =this_cpu_read (hardirq_stack);return is_irq_stack (stack, irq); } -static void *is_softirq_stack (unsigned long *stack,int cpu) +static void *is_softirq_stack (unsigned long *stack); { -void *irq =per_cpu (softirq_stack, cpu); +void *irq =this_cpu_read (softirq_stack);return is_irq_stack (stack, irq); }
@@ -42,7 +42,6 @@ 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(); int graph = 0;u32 *prev_esp;
@@ -53,9 +52,9 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,for (;;) {void *end_stack; - end_stack =is_hardirq_stack (stack, cpu); + end_stack =is_hardirq_stack (stack);if (!end_stack) - end_stack =is_softirq_stack (stack, cpu); + end_stack =is_softirq_stack (stack); bp = ops->walk_stack (task, stack, bp, ops, data, end_stack, &graph);
@@ -74,7 +73,6 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,break ;touch_nmi_watchdog (); } -put_cpu (); }EXPORT_SYMBOL (dump_trace);
arch/x86/kernel/dumpstack_64.c +9 -17
@@ -31,8 +31,8 @@ static char x86_stack_ids[][8] = {#endif }; -static unsigned long *in_exception_stack (unsigned cpu,unsigned long stack, -unsigned *usedp,char **idp) +static unsigned long *in_exception_stack (unsigned long stack,unsigned *usedp, +char **idp) {unsigned k;
@@ -41,7 +41,7 @@ static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,* 'stack' is in one of them: */ for (k =0 ; k <N_EXCEPTION_STACKS ; k++) { -unsigned long end =per_cpu (orig_ist, cpu).ist[k]; +unsigned long end =raw_cpu_ptr (&orig_ist)->ist[k];/* * Is 'stack' above this exception frame's end? * If yes then skip to the next frame.
@@ -111,7 +111,7 @@ enum stack_type { };static enum stack_type -analyze_stack (int cpu,struct task_struct *task,unsigned long *stack, +analyze_stack (struct task_struct *task,unsigned long *stack,unsigned long **stack_end,unsigned long *irq_stack,unsigned *used,char **id) {
@@ -121,8 +121,7 @@ analyze_stack(int cpu, struct task_struct *task, unsigned long *stack,if ((unsigned long )task_stack_page (task) == addr)return STACK_IS_NORMAL ; - *stack_end =in_exception_stack (cpu, (unsigned long )stack, - used, id); + *stack_end =in_exception_stack ((unsigned long )stack, used, id);if (*stack_end)return STACK_IS_EXCEPTION ;
@@ -149,8 +148,7 @@ 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 = (unsigned long *)per_cpu (irq_stack_ptr, cpu); + unsigned long *irq_stack = (unsigned long *)this_cpu_read(irq_stack_ptr);unsigned used =0 ;int graph =0 ;int done =0 ;
@@ -169,8 +167,8 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,enum stack_type stype;char *id; - stype =analyze_stack (cpu, task, stack, &stack_end, - irq_stack, &used, &id); + stype =analyze_stack (task, stack, &stack_end, irq_stack, &used, + &id);/* Default finish unless specified to continue */ done =1 ;
@@ -225,7 +223,6 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,* This handles the process stack: */ bp = ops->walk_stack (task, stack, bp, ops, data,NULL , &graph); -put_cpu (); }EXPORT_SYMBOL (dump_trace);
@@ -236,13 +233,9 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,unsigned long *irq_stack_end;unsigned long *irq_stack;unsigned long *stack; -int cpu;int i; -preempt_disable (); - cpu =smp_processor_id (); - - irq_stack_end = (unsigned long *)(per_cpu (irq_stack_ptr, cpu)); + irq_stack_end = (unsigned long *)this_cpu_read (irq_stack_ptr); irq_stack = irq_stack_end - (IRQ_STACK_SIZE /sizeof (long )); sp = sp ? :get_stack_pointer (task, regs);
@@ -274,7 +267,6 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, stack++;touch_nmi_watchdog (); } -preempt_enable ();pr_cont ("\n" );show_trace_log_lvl (task, regs, sp, bp, log_lvl);
Signature · 1 line
2.7.4
PATCH 4/6 x86/dumpstack: add get_stack_pointer() and get_frame_pointer()
In reply to Josh PoimboeufThe various functions involved in dumping the stack all do similar things with regard to getting the stack pointer and the frame pointer based on the regs and task arguments. Create helper functions to do that instead.
--- arch/x86/include/asm/stacktrace.h | 41 ++++++++++++++++++++++----------------- arch/x86/kernel/dumpstack.c | 5 ++--- arch/x86/kernel/dumpstack_32.c | 25 ++++-------------------- arch/x86/kernel/dumpstack_64.c | 30 ++++------------------------ 4 files changed, 33 insertions(+), 68 deletions(-)
arch/x86/include/asm/stacktrace.h +23 -18
@@ -50,36 +50,41 @@ void dump_trace(struct task_struct *tsk, struct pt_regs *regs,#ifdef CONFIG_X86_32 #define STACKSLOTS_PER_LINE 8 -#define get_bp (bp) asm("movl %%ebp, %0" : "=r" (bp) :)#else #define STACKSLOTS_PER_LINE 4 -#define get_bp (bp) asm("movq %%rbp, %0" : "=r" (bp) :)#endif #ifdef CONFIG_FRAME_POINTER -static inline unsigned long -stack_frame (struct task_struct *task,struct pt_regs *regs) +static inline unsigned long * +get_frame_pointer (struct task_struct *task,struct pt_regs *regs) { -unsigned long bp; -if (regs) -return regs->bp; +return (unsigned long *)regs->bp; -if (task == current) { -/* Grab bp right from our regs */ -get_bp (bp); -return bp; - } +if (!task || task == current) +return __builtin_frame_address (0 ); -return ((struct inactive_task_frame *)task->thread.sp)->bp; +return (unsigned long *)((struct inactive_task_frame *)task->thread.sp)->bp; }#else -static inline unsigned long -stack_frame (struct task_struct *task,struct pt_regs *regs) +static inline unsigned long * +get_frame_pointer (struct task_struct *task,struct pt_regs *regs) { -return 0 ; +return NULL ; +} +#endif /* CONFIG_FRAME_POINTER */ + +static inline unsigned long * +get_stack_pointer (struct task_struct *task,struct pt_regs *regs) +{ +if (regs) +return (unsigned long *)kernel_stack_pointer (regs); + +if (!task || task == current) +return __builtin_frame_address (0 ); + +return (unsigned long *)task->thread.sp; } -#endif extern void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
@@ -106,7 +111,7 @@ static inline unsigned long caller_frame_pointer(void) {struct stack_frame *frame; -get_bp (frame); + frame =__builtin_frame_address (0 );#ifdef CONFIG_FRAME_POINTER frame = frame->next_frame;
arch/x86/kernel/dumpstack.c +2 -3
@@ -170,15 +170,14 @@ show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,void show_stack (struct task_struct *task,unsigned long *sp) {unsigned long bp =0 ; -unsigned long stack;/* * Stack frames below this one aren't interesting. Don't show them * if we're printing for %current. */ if (!sp && (!task || task == current)) { - sp = &stack; - bp =stack_frame (current,NULL ); + sp =get_stack_pointer (current,NULL ); + bp = (unsigned long )get_frame_pointer (current,NULL ); }show_stack_log_lvl (task,NULL , sp, bp,"" );
arch/x86/kernel/dumpstack_32.c +4 -21
@@ -46,19 +46,9 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,int graph =0 ;u32 *prev_esp; -if (!task) - task = current; - -if (!stack) { -unsigned long dummy; - - stack = &dummy; -if (task != current) - stack = (unsigned long *)task->thread.sp; - } - -if (!bp) - bp =stack_frame (task, regs); + task = task ? : current; + stack = stack ? :get_stack_pointer (task, regs); + bp = bp ? : (unsigned long )get_frame_pointer (task, regs);for (;;) {void *end_stack;
@@ -95,14 +85,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,unsigned long *stack;int i; -if (sp ==NULL ) { -if (regs) - sp = (unsigned long *)regs->sp; -else if (task) - sp = (unsigned long *)task->thread.sp; -else - sp = (unsigned long *)&sp; - } + sp = sp ? :get_stack_pointer (task, regs); stack = sp;for (i =0 ; i < kstack_depth_to_print; i++) {
arch/x86/kernel/dumpstack_64.c +4 -26
@@ -151,25 +151,14 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs, {const unsigned cpu =get_cpu ();unsigned long *irq_stack = (unsigned long *)per_cpu (irq_stack_ptr, cpu); -unsigned long dummy;unsigned used =0 ;int graph =0 ;int done =0 ; -if (!task) - task = current; + task = task ? : current; + stack = stack ? :get_stack_pointer (task, regs); + bp = bp ? : (unsigned long )get_frame_pointer (task, regs); -if (!stack) { -if (regs) - stack = (unsigned long *)regs->sp; -else if (task != current) - stack = (unsigned long *)task->thread.sp; -else - stack = &dummy; - } - -if (!bp) - bp =stack_frame (task, regs);/* * Print function call entries in all stacks, starting at the * current stack address. If the stacks consist of nested
@@ -256,18 +245,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, irq_stack_end = (unsigned long *)(per_cpu (irq_stack_ptr, cpu)); irq_stack = irq_stack_end - (IRQ_STACK_SIZE /sizeof (long )); -/* -* Debugging aid: "show_stack(NULL, NULL);" prints the -* back trace for this cpu: -*/ -if (sp ==NULL ) { -if (regs) - sp = (unsigned long *)regs->sp; -else if (task) - sp = (unsigned long *)task->thread.sp; -else - sp = (unsigned long *)&sp; - } + sp = sp ? :get_stack_pointer (task, regs); stack = sp;for (i =0 ; i < kstack_depth_to_print; i++) {
Signature · 1 line
2.7.4
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Josh PoimboeufQuoted 7 lines · Josh Poimboeuf
Change
printk_stack_address()to be useful when called by an unwinder outside the context ofdump_trace().Specifically:
- printk_stack_address()'s 'data' argument is always used as the log level string. Make that explicit.
If this is true, and I'm not sure it is as I believe there are static strings emitted like EOE and IRQ, shouldn't this bubble up through the calling tree?
Quoted 1 line
- Call
touch_nmi_watchdog().
[]
arch/x86/kernel/dumpstack.c
Quoted 1 line
arch/x86/kernel/dumpstack.c +0
-0
[]
Quoted 15 lines
arch/x86/kernel/dumpstack.c +0
-0@@ -26,10 +26,11 @@ int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE; static int die_counter;
static void printk_stack_address(unsigned long address, int reliable,arch/x86/kernel/dumpstack.c +1
-1- void *data) + char *log_lvl){
arch/x86/kernel/dumpstack.c +1
-0+touch_nmi_watchdog ();printk("%s [<%p>] %s%pB\n",
arch/x86/kernel/dumpstack.c +1
-1- (char *)data, (void *)address, reliable ? "" : "? ", + log_lvl, (void *)address, reliable ? "" : "? ",(void *)address);
}
arch/x86/kernel/dumpstack.c static int print_trace_stack(void *data, char *name) · line 154
Quoted 7 lines
arch/x86/kernel/dumpstack.c +0
-1@@ -148,7 +149,6 @@ static int print_trace_stack(void *data, char *name)*/ static int print_trace_address(void *data, unsigned long addr, int reliable) { -touch_nmi_watchdog (); printk_stack_address(addr, reliable, data); return 0; }
like for data here?
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Peter ZijlstraQuoted 5 lines · Peter Zijlstra
I actively disable KASLR on my dev box and feed these hex numbers into addr2line -ie vmlinux to find where in the function we are.
Having the option to make %pB generate them works for me.
Yeah, considering that this is the only place this is used, changing %pB sounds quite reasonable.
We could perhaps make %pB show the hex numbers and address (so pB would expand to "[<hex>] symbolname".if
(a) not randomizing (so the hex numbers _may_ be useful)
(b) kptr_restrict is 0 (so the hex numbers are "safe" in the dmesg)
and fall back to just the symbolic name if either of those aren't true?
And obviously, if KALLSYMS isn't enabled, you always show hex numbers.. That's already the case (but we might want to add the "[<>}' markers around the hex numbers just to make the user space automation we do have work).
Linus
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Joe PerchesQuoted 33 lines · Joe Perches
Quoted 7 lines · Josh Poimboeuf
Change
printk_stack_address()to be useful when called by an unwinder outside the context ofdump_trace().Specifically:
- printk_stack_address()'s 'data' argument is always used as the log level string. Make that explicit.
If this is true, and I'm not sure it is as I believe there are static strings emitted like EOE and IRQ, shouldn't this bubble up through the calling tree?
Quoted 1 line
- Call
touch_nmi_watchdog().[]
arch/x86/kernel/dumpstack.c
Quoted 1 line
arch/x86/kernel/dumpstack.c +0
-0[]
Quoted 16 lines
arch/x86/kernel/dumpstack.c +0
-0@@ -26,10 +26,11 @@ int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE; static int die_counter;
static void printk_stack_address(unsigned long address, int reliable,arch/x86/kernel/dumpstack.c +1
-1- void *data) + char *log_lvl){
arch/x86/kernel/dumpstack.c +1
-0+touch_nmi_watchdog ();printk("%s [<%p>] %s%pB\n",
arch/x86/kernel/dumpstack.c +1
-1- (char *)data, (void *)address, reliable ? "" : "? ", + log_lvl, (void *)address, reliable ? "" : "? ",(void *)address);
}arch/x86/kernel/dumpstack.c +0
-0@@ -148,7 +149,6 @@ static int print_trace_stack(void *data, char *name)*/
Quoted 8 lines
arch/x86/kernel/dumpstack.c static int print_trace_stack(void *data, char *name) · line 154
Quoted 6 lines
arch/x86/kernel/dumpstack.c +0
-1static int print_trace_address(void *data, unsigned long addr, int reliable) { -touch_nmi_watchdog (); printk_stack_address(addr, reliable, data); return 0; }like for data here?
This function needs to keep its 'void *data' argument because it's a callback for stacktrace_ops, so it has to conform to the callback interface. 'data' is used for passing a pointer to an opaque data structure to the callback.
Also this is the only caller of printk_stack_address(), so there's nowhere else to bubble it up to.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRe: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Linus TorvaldsQuoted 9 lines · Linus Torvalds
For the non-kallsyms case we _could_ also just make the '%pB' format add the [<>] markers back in case somebody still uses the user-space kallsyms script that looks up hex numbers.
Right now the hex numbers are not only useless in stack dumps (since you can't look up symbols using them anyway thanks to randomization), they are noise that makes the stack traces harder to read. So let's just remove them?
I actively disable KASLR on my dev box and feed these hex numbers into addr2line -ie vmlinux to find where in the function we are.
Having the option to make %pB generate them works for me.
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Josh PoimboeufQuoted 2 lines · Josh Poimboeuf
Change
printk_stack_address()to be useful when called by an unwinder outside the context ofdump_trace().
...
arch/x86/kernel/dumpstack.c int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE; · line 34
Quoted 4 lines
printk("%s [<%p>] %s%pB\n",arch/x86/kernel/dumpstack.c +1
-1- (char *)data, (void *)address, reliable ?"" :"? " , + log_lvl, (void *)address, reliable ?"" :"? " , (void *)address);
Side note: we should just remove the "[<hex>]" thing entirely, I think. It's not just redundant, but when people care about kernel address randomization it ends up spilling the beans on the offset in warnings (in oopses you can generally find the data in the stack backtraces, which we probably should remove too as useless).
So it would be just
printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address);instead.
For the non-kallsyms case we _could_ also just make the '%pB' format add the [<>] markers back in case somebody still uses the user-space kallsyms script that looks up hex numbers.
Right now the hex numbers are not only useless in stack dumps (since you can't look up symbols using them anyway thanks to randomization), they are noise that makes the stack traces harder to read. So let's just remove them?
I realize that is entirely orthogonal to this particular patch, but it happened to touch this file.
No objections to this series other than that comment.
Linus
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Josh PoimboeufQuoted 9 lines · Josh Poimboeuf
Quoted 8 lines · Joe Perches
Quoted 7 lines · Josh Poimboeuf
Change
printk_stack_address()to be useful when called by an unwinder outside the context ofdump_trace().Specifically:
- printk_stack_address()'s 'data' argument is always used as the log level string. Make that explicit.
[]
Quoted 4 lines
Quoted 3 lines
If this is true, and I'm not sure it is as I believe there are static strings emitted like EOE and IRQ, shouldn't this bubble up through the calling tree?
Quoted 8 lines
[]
This function needs to keep its 'void *data' argument because it's a callback forstacktrace_ops, so it has to conform to the callback interface. 'data' is used for passing a pointer to an opaque data structure to the callback.Also this is the only caller of
printk_stack_address(), so there's nowhere else to bubble it up to.
And that shows that print_stack_address(data is not always a log level.
ie: walk_stack uses it to print a string not a log level.
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
Quoted 21 lines · Joe Perches
Quoted 9 lines · Josh Poimboeuf
Quoted 8 lines · Joe Perches
Quoted 7 lines · Josh Poimboeuf
Change
printk_stack_address()to be useful when called by an unwinder outside the context ofdump_trace().Specifically:
- printk_stack_address()'s 'data' argument is always used as the log level string. Make that explicit.
[]
Quoted 10 lines
Quoted 3 lines
If this is true, and I'm not sure it is as I believe there are static strings emitted like EOE and IRQ, shouldn't this bubble up through the calling tree?
[]
This function needs to keep its 'void *data' argument because it's a callback forstacktrace_ops, so it has to conform to the callback interface. 'data' is used for passing a pointer to an opaque data structure to the callback.
Quoted 5 lines
Quoted 2 lines
Also this is the only caller of
printk_stack_address(), so there's nowhere else to bubble it up to.And that shows that print_stack_address(data is not always a log level.
ie:walk_stackuses it to print a string not a log level.
Hm, can you be more specific? As far as I can tell, here's the only possible call path to print_trace_address() and printk_stack_address():
show_trace_log_lvl()
dump_trace() // ops is print_trace_op
print_context_stack() // ops->walk_stack
print_trace_address() // ops->address
printk_stack_address()So 'data' is a sneaky way to pass 'log_lvl' from show_trace_log_lvl() to print_trace_address(), without dump_trace() and print_context_stack() knowing what it is, because they're used in other places where 'data' means something else.
Signature · 1 line
Josh
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Linus TorvaldsQuoted 9 lines · Linus Torvalds
Quoted 5 lines · Peter Zijlstra
I actively disable KASLR on my dev box and feed these hex numbers into addr2line -ie vmlinux to find where in the function we are.
Having the option to make %pB generate them works for me.
Yeah, considering that this is the only place this is used, changing %pB sounds quite reasonable.
There's now another use of '%pB' in proc_pid_stack() in the tip tree: I changed it to '%pB' from '%pS'. But I think the modified '%pB' would work there as well.
Quoted 8 lines
We could perhaps make %pB show the hex numbers and address (so pB would expand to "[<hex>] symbolname".if
(a) not randomizing (so the hex numbers _may_ be useful)
(b)
kptr_restrictis 0 (so the hex numbers are "safe" in the dmesg)and fall back to just the symbolic name if either of those aren't true?
Do we really need to check for both? '%pK' only checks kptr_restrict. I'd think we should be consistent with that. And maybe there are some scenarios where the actual text addresses provide useful debug information if KASLR is enabled and kptr_restrict is zero.
Quoted 4 lines
And obviously, if KALLSYMS isn't enabled, you always show hex numbers.. That's already the case (but we might want to add the "[<>}' markers around the hex numbers just to make the user space automation we do have work).
Even if kptr_restrict is set?
Signature · 1 line
Josh
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Josh PoimboeufQuoted 34 lines · Josh Poimboeuf
Quoted 20 lines · Joe Perches
Quoted 9 lines · Josh Poimboeuf
Quoted 8 lines · Joe Perches
Quoted 7 lines · Josh Poimboeuf
Change
printk_stack_address()to be useful when called by an unwinder outside the context ofdump_trace().Specifically:
- printk_stack_address()'s 'data' argument is always used as the log level string. Make that explicit.
[]
Quoted 9 lines
Quoted 3 lines
If this is true, and I'm not sure it is as I believe there are static strings emitted like EOE and IRQ, shouldn't this bubble up through the calling tree?
[]
This function needs to keep its 'void *data' argument because it's a callback forstacktrace_ops, so it has to conform to the callback interface. 'data' is used for passing a pointer to an opaque data structure to the callback.Quoted 4 lines
Quoted 2 lines
Also this is the only caller of
printk_stack_address(), so there's nowhere else to bubble it up to.And that shows that print_stack_address(data is not always a log level.
ie:walk_stackuses it to print a string not a log level.Hm, can you be more specific? As far as I can tell, here's the only possible call path to
print_trace_address()andprintk_stack_address():
show_trace_log_lvl()
dump_trace()// ops isprint_trace_op
print_context_stack() //ops->walk_stack
print_trace_address() //ops->address
printk_stack_address()
Quoted 4 lines
So 'data' is a sneaky way to pass '
log_lvl' fromshow_trace_log_lvl()toprint_trace_address(), withoutdump_trace()andprint_context_stack()knowing what it is, because they're used in other places where 'data' means something else.
hmm, perhaps I got lost in a twisting maze of little callbacks. I'll drop some stuff and see where I am next time.
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Josh PoimboeufQuoted 20 lines · Josh Poimboeuf
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Peter Zijlstra
I actively disable KASLR on my dev box and feed these hex numbers into addr2line -ie vmlinux to find where in the function we are.
Having the option to make %pB generate them works for me.
Yeah, considering that this is the only place this is used, changing %pB sounds quite reasonable.
There's now another use of '%pB' in
proc_pid_stack()in the tip tree: I changed it to '%pB' from '%pS'. But I think the modified '%pB' would work there as well.Quoted 5 lines
We could perhaps make %pB show the hex numbers and address (so pB would expand to "[<hex>] symbolname".if
(a) not randomizing (so the hex numbers _may_ be useful)
Quoted 8 lines
Quoted 3 lines
(b)
kptr_restrictis 0 (so the hex numbers are "safe" in the dmesg)and fall back to just the symbolic name if either of those aren't true?
Do we really need to check for both? '%pK' only checks
kptr_restrict. I'd think we should be consistent with that. And maybe there are some scenarios where the actual text addresses provide useful debug information if KASLR is enabled andkptr_restrictis zero.
So I was looking at implementing this, and I noticed that '%pK' prints "pK-error" if it's called from interrupt context when kptr_restrict==1. Because checking CAP_SYSLOG would be meaningless in that case.
I don't really understand the point of the "pK-error" thing. Any reason why we can't print zero, i.e., just degrade the kptr_restrict from 1 to 2 in an interrupt?
That would make the '%pK' code simpler and usable from interrupt context. Also it would make its behavior consistent with the proposed '%pB' changes, and the kptr_restrict code could be shared between '%pK' and '%pB'.
Kess (or others), any objections if I make that change?
Signature · 1 line
Josh
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
Quoted 38 lines · Josh Poimboeuf
Quoted 20 lines · Josh Poimboeuf
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Peter Zijlstra
I actively disable KASLR on my dev box and feed these hex numbers into addr2line -ie vmlinux to find where in the function we are.
Having the option to make %pB generate them works for me.
Yeah, considering that this is the only place this is used, changing %pB sounds quite reasonable.
There's now another use of '%pB' in
proc_pid_stack()in the tip tree: I changed it to '%pB' from '%pS'. But I think the modified '%pB' would work there as well.Quoted 5 lines
We could perhaps make %pB show the hex numbers and address (so pB would expand to "[<hex>] symbolname".if
(a) not randomizing (so the hex numbers _may_ be useful)
Quoted 8 lines
Quoted 3 lines
(b)
kptr_restrictis 0 (so the hex numbers are "safe" in the dmesg)and fall back to just the symbolic name if either of those aren't true?
Do we really need to check for both? '%pK' only checks
kptr_restrict. I'd think we should be consistent with that. And maybe there are some scenarios where the actual text addresses provide useful debug information if KASLR is enabled andkptr_restrictis zero.So I was looking at implementing this, and I noticed that '%pK' prints "pK-error" if it's called from interrupt context when kptr_restrict==1. Because checking
CAP_SYSLOGwould be meaningless in that case.I don't really understand the point of the "pK-error" thing. Any reason why we can't print zero, i.e., just degrade the
kptr_restrictfrom 1 to 2 in an interrupt?
Quoted 6 lines
That would make the '%pK' code simpler and usable from interrupt context. Also it would make its behavior consistent with the proposed '%pB' changes, and the
kptr_restrictcode could be shared between '%pK' and '%pB'.Kess (or others), any objections if I make that change?
I don't mind this becoming "0" on error. I suspect the rationale was to make it a discoverable condition and to avoid confusion.
As far as expanding the usage, I'm still in favor, though there is work planned to make kptr_restrict go away in favor of having blacklisted destination buffers, etc. I'm hoping to have this as part of the continuing usercopy hardening work.
Regardless, aren't these values being written to dmesg buffer? Traditionally we've not bothered censoring values that go there, as "dmesg_restrict" exists to protect those contents.
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
Quoted 39 lines · Kees Cook
Quoted 38 lines · Josh Poimboeuf
Quoted 20 lines · Josh Poimboeuf
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Peter Zijlstra
I actively disable KASLR on my dev box and feed these hex numbers into addr2line -ie vmlinux to find where in the function we are.
Having the option to make %pB generate them works for me.
Yeah, considering that this is the only place this is used, changing %pB sounds quite reasonable.
There's now another use of '%pB' in
proc_pid_stack()in the tip tree: I changed it to '%pB' from '%pS'. But I think the modified '%pB' would work there as well.Quoted 5 lines
We could perhaps make %pB show the hex numbers and address (so pB would expand to "[<hex>] symbolname".if
(a) not randomizing (so the hex numbers _may_ be useful)
Quoted 8 lines
Quoted 3 lines
(b)
kptr_restrictis 0 (so the hex numbers are "safe" in the dmesg)and fall back to just the symbolic name if either of those aren't true?
Do we really need to check for both? '%pK' only checks
kptr_restrict. I'd think we should be consistent with that. And maybe there are some scenarios where the actual text addresses provide useful debug information if KASLR is enabled andkptr_restrictis zero.So I was looking at implementing this, and I noticed that '%pK' prints "pK-error" if it's called from interrupt context when kptr_restrict==1. Because checking
CAP_SYSLOGwould be meaningless in that case.I don't really understand the point of the "pK-error" thing. Any reason why we can't print zero, i.e., just degrade the
kptr_restrictfrom 1 to 2 in an interrupt?
Quoted 6 lines
Quoted 6 lines
That would make the '%pK' code simpler and usable from interrupt context. Also it would make its behavior consistent with the proposed '%pB' changes, and the
kptr_restrictcode could be shared between '%pK' and '%pB'.Kess (or others), any objections if I make that change?
Ahem, Kees, sorry :-)
Quoted 11 lines
I don't mind this becoming "0" on error. I suspect the rationale was to make it a discoverable condition and to avoid confusion.
As far as expanding the usage, I'm still in favor, though there is work planned to make
kptr_restrictgo away in favor of having blacklisted destination buffers, etc. I'm hoping to have this as part of the continuing usercopy hardening work.Regardless, aren't these values being written to dmesg buffer? Traditionally we've not bothered censoring values that go there, as "
dmesg_restrict" exists to protect those contents.
Ah, the plot thickens. I didn't know about 'dmesg_restrict'. So I guess we don't have to restrict the stack dump addresses after all, since the entire dmesg buffer is protected by syslog()?
If so, I'm thinking that expanding '%pB' wouldn't be worthwhile after all, because its two users would have two different requirements for printing the address: /proc/<pid>/stack needs to use kptr_restrict but the unwinder doesn't.
In which case I think the current code is fine.
Signature · 1 line
Josh
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to a message not in the archiveQuoted 10 lines · Linus Torvalds
Quoted 4 lines · Josh Poimboeuf
Ah, the plot thickens. I didn't know about '
dmesg_restrict'. So I guess we don't have to restrict the stack dump addresses after all, since the entire dmesg buffer is protected bysyslog()?No.
Guys, the whole
dmesg_restrictthing is a joke. You can't restrict access to system messages in general. It's just a stupid idea.
I'm not advocating that it's a globally useful protection, I was just trying to point out that so much stuff is already exposed in the system log that it's likely not a great use of time to think about censoring things there right now. Obviously if it both improves debuggability _and_ removes raw addresses from the log, I'm all for it. :)
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to a message not in the archiveQuoted 11 lines · Linus Torvalds
Quoted 4 lines · Josh Poimboeuf
Ah, the plot thickens. I didn't know about '
dmesg_restrict'. So I guess we don't have to restrict the stack dump addresses after all, since the entire dmesg buffer is protected bysyslog()?No.
Guys, the whole
dmesg_restrictthing is a joke. You can't restrict access to system messages in general. It's just a stupid idea.
Quoted 8 lines
So stop thinking like some theoretical security people - we have more than enough of those. Start thinking like *practical* people that actually care about the user experience, and interfaces, and then within those confined, think about security.
Because remember: if you don't get that security comes *second*, you're not a security person, you're just a joke. People actually *using* the system come first.
So I'm not all that familiar with this pointer restriction stuff. And I don't claim to be a security person. I'm just trying to understand what the real issues are so we can fix them.
And anyway I thought this whole discussion was trying to resolve a security concern which you brought up: not leaking kernel addresses to unprivileged user space.
Quoted 6 lines
So stop with the idiotic security flags that same people cannot actually use, either because they restrict use too much, or because they are so expensive that they aren't practical by default.
The things is, we should strive to do something *useful*. And
dmesg_restrictis not that.
For an oops, there are other opportunities for address leakage besides the stack trace function addresses. There's the raw stack dump which dumps the first 12 stack entries. And there's the register dump. I'm pretty sure we don't want to get rid of those.
I suppose we could come up with some innovative way to filter or sanitize kernel addresses from the stack dump and the registers. But that probably hurts usability for kernel developers.
Another issue is that there are a lot of duplicate symbol names in the kernel. So the symbol name alone might not be enough to disambiguate the function address.
Not to mention the fact that today there are a gazillion uses of printk() with '%p' in the kernel.
So yes, dmesg_restrict sounds useful to me. It's a way to prevent users from seeing kernel addresses without affecting my ability to debug issues. For a locked down system, why would non-root users need to access dmesg anyway?
Quoted 3 lines
In contrast, striving to just get the symbol names - but not the hex addresses - is actually both more useful and more secure than what we have now.
How exactly does removing data from the stack dump make it more useful?
Quoted 2 lines
And dammit, even addr2line should just be able to understand the "symbol+ offset" format, so even that is a really bad reason to show the hex number.
Maybe addr2line *should* be able to understand "symbol+offset", but it doesn't, so it breaks my workflow. Is there another lightweight tool out there (i.e., not gdb) which does it?
I also wonder if other tools might have similar issues. Though I can't think of anything off the top of my head.
Signature · 1 line
Josh
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
Quoted 4 lines · Josh Poimboeuf
For an oops, there are other opportunities for address leakage besides the stack trace function addresses. There's the raw stack dump which dumps the first 12 stack entries. And there's the register dump. I'm pretty sure we don't want to get rid of those.
We actually probably *do* want to get rid of the stack dump. It's likely not really useful any more, and more legacy noise.
The register contents we definitely don't want to remove, obviously. But apart from EIP itself (and LR etc on other architectures), those actually rather seldom contain code addresses, so that kind of data is rather harder to misuse.
That said, if you can trigger an oops, you do quite likely have a security problem already.
So oopses aren't necessarily the first thing to worry about. I'd worry more about things like WARN_ON_ONCE() that are much more likely things that might be triggerable (ie we do occasionally have things like warning for legacy system calls that shouldn't be used any more)
Quoted 3 lines
I suppose we could come up with some innovative way to filter or sanitize kernel addresses from the stack dump and the registers. But that probably hurts usability for kernel developers.
Yeah, but see above: an oops really does tend to often be a security issue on its own, especially if it can be triggered arbitrarily by an attacker.
Quoted 3 lines
Another issue is that there are a lot of duplicate symbol names in the kernel. So the symbol name alone might not be enough to disambiguate the function address.
That is true. It's seldom a big issue, though. Nobody actually uses the address for anything _anyway_, so people end up disambiguating those things based on context regardless.
Again, something like addr2line could be an exception, but (a) that thing is useless in most situations due to randomization and (b) if you don't randomize then the whole discussion is moot.
Plus add2line could just show all options, and then you end up doing human disambiguation anyway.
Quoted 2 lines
Not to mention the fact that today there are a gazillion uses of
printk()with '%p' in the kernel.
Yes. And some of them have been stupiud security issues on their own, and have nothing to do with symbol names. See for example commit 31b0b385f69d ("nf_conntrack: avoid kernel pointer value leak in slab name")
Quoted 4 lines
So yes,
dmesg_restrictsounds useful to me. It's a way to prevent users from seeing kernel addresses without affecting my ability to debug issues. For a locked down system, why would non-root users need to access dmesg anyway?
That's the point. It is only useful for locked-down systems.
But that also means that IT IS NOT USEFUL AS A SECURITY ARGUMENT - since it's simply not relevant to most systems out there.
Most systems aren't locked down.
Quoted 1 line
How exactly does removing data from the stack dump make it more useful?
I actually spend time cleaning up commit messages in logs, because useless data that isn't actually information (random hex numbers) is actively detrimental.
It makes commit logs less legible.
It also makes it harder to parse dumps.
It's not useful. That makes it actively bad.
I probably look at more oops reports than most people. I have not found the hex numbers useful for the last five years, because they are just randomized crap.
The stack content thing just makes code scroll off the screen etc, for example.
So in order for data to be useful, it has to be more than "data". It has to be "information". More random useless hex noise is not good.
Linus
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · Josh Poimboeuf
So yes,
dmesg_restrictsounds useful to me. It's a way to prevent users from seeing kernel addresses without affecting my ability to debug issues. For a locked down system, why would non-root users need to access dmesg anyway?
Quoted 6 lines
That's the point. It is only useful for locked-down systems.
But that also means that IT IS NOT USEFUL AS A SECURITY ARGUMENT - since it's simply not relevant to most systems out there.
Most systems aren't locked down.
Ok, so maybe removing kernel text addresses from the stack dump wouldn't be the end of the world.
But I still don't quite understand your statement that dmesg_restrict is only useful for locked down systems.
To prevent kernel address disclosure, it seems we already rely on the user setting kptr_restrict today, otherwise I can do cat /proc/self/stack and the game is already lost, right?
So what's the difference between expecting the user to set kptr_restrict vs dmesg_restrict?
Signature · 1 line
Josh
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to a message not in the archiveQuoted 25 lines · Linus Torvalds
Quoted 7 lines · Josh Poimboeuf
But I still don't quite understand your statement that
dmesg_restrictis only useful for locked down systems.To prevent kernel address disclosure, it seems we already rely on the user setting
kptr_restricttoday, otherwise I can do cat/proc/self/stackand the game is already lost, right?The point is:
kptr_restrictactually makes sense, and is widely useful. It doesn't really end up hurting normal things. It's a pretty targeted thing, and generally doesn't actually hurt. You can still do basic health monitoring without having to get elevated privileges, for example.Even system maintainers don't want to be root all the time. In fact, I suspect that the better a system maintainer you are, the less you want to be root - but you'll still want to see logs etc.
So note the difference between
kptr_restrictanddmesg_restrict.One is useful in a pretty wide environment, the other simply is not.
Quoted 2 lines
So what's the difference between expecting the user to set
kptr_restrictvsdmesg_restrict?
Quoted 7 lines
Do you see the difference now?
kptr_restrictsimply doesn't hurt as much asdmesg_restrict, so you can enable it fairly widely by default.That makes it the *much* better security option. Because security options that you can't enable aren't actually useful.
Yeah, at least for human-administered systems, that does make sense. Grumpy sysadmins don't want to type "sudo dmesg" or "sudo journalctl" because a) they don't like change; and b) using sudo adds risk.
And a security option which is never used is indeed useless. So *maybe* that's a good enough argument for expecting the user to only enable kptr_restrict instead of both.
But with cloud, devops, mobile, embedded, IoT, [insert buzzword], it seems most systems are actually managed by software nowadays. Then the above arguments don't seem to apply, and dmesg_restrict could still be quite widely useful, no?
Signature · 1 line
Josh
Re: [PATCH 2/6] oprofile/x86: add regs->ip to oprofile trace
In reply to Josh PoimboeufQuoted 21 lines · Josh Poimboeuf
dump_trace()doesn't add the interrupted instruction's address to the trace, so add it manually. This makes the profile more useful, and also makes it more consistent with what perf profiling does.Cc Robert Richter <[email protected]>Signed-off-by Josh Poimboeuf <[email protected]>--- arch/x86/oprofile/backtrace.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)arch/x86/oprofile/backtrace.c +0
-3@@ -114,9 +114,16 @@ x86_backtrace(struct pt_regs * const regs, unsigned int depth)if (!user_mode (regs)) {unsigned long stack =kernel_stack_pointer (regs); -if (depth) -dump_trace (NULL , regs, (unsigned long *)stack,0 , - &backtrace_ops, &depth);
arch/x86/oprofile/backtrace.c x86_backtrace(struct pt_regs * const regs, unsigned int depth) · line 126
Quoted 10 lines
arch/x86/oprofile/backtrace.c +10
-0+ +if (!depth) +return ; + +oprofile_add_trace (regs->ip); +if (!--depth) +return ; + +dump_trace (NULL , regs, (unsigned long *)stack,0 , + &backtrace_ops, &depth);
Adding oprofile-list in case someone has objections since this may break userland tools.
Ingo, please apply the patch to tip once this series is ok to you.
Thanks,
-Robert
Quoted 6 lines
arch/x86/oprofile/backtrace.c +0
-0return ; }Signature · 1 line
2.7.4
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Linus TorvaldsQuoted 15 lines · Linus Torvalds
Quoted 5 lines · Peter Zijlstra
I actively disable KASLR on my dev box and feed these hex numbers into addr2line -ie vmlinux to find where in the function we are.
Having the option to make %pB generate them works for me.
Yeah, considering that this is the only place this is used, changing %pB sounds quite reasonable.
We could perhaps make %pB show the hex numbers and address (so pB would expand to "[<hex>] symbolname".if
(a) not randomizing (so the hex numbers _may_ be useful)
Quoted 8 lines
(b)
kptr_restrictis 0 (so the hex numbers are "safe" in the dmesg)and fall back to just the symbolic name if either of those aren't true?
And obviously, if KALLSYMS isn't enabled, you always show hex numbers.. That's already the case (but we might want to add the "[<>}' markers around the hex numbers just to make the user space automation we do have work).
Here's a simple patch to skip the kernel text addresses in the stack dump, based on top of the up-thread patch set.
It doesn't do the '%pB' thing because now in tip there's a non-printk user of it in /proc/<pid>/stack, for which there are different rules (print zeros on kptr_restrict vs skipping the number altogether, for example). And anyway that could maybe be added later.
It also doesn't check for randomization or kptr_restrict, which keeps it simple and maximizes its "usefulness" ;-)
Thoughts?
---
From: Josh Poimboeuf <[email protected]>
Subject: [PATCH] x86/dumpstack: don't print kernel text addresses in stack dump
Printing kernel text addresses in stack dumps is of questionable value, especially now that address randomization is becoming common.
It can be a security issue because it leaks kernel addresses.
It also affects the usefulness of the stack dump. Linus says:
"I actually spend time cleaning up commit messages in logs, because useless data that isn't actually information (random hex numbers) is actively detrimental.
It makes commit logs less legible.
It also makes it harder to parse dumps.
It's not useful. That makes it actively bad.
I probably look at more oops reports than most people. I have not found the hex numbers useful for the last five years, because they are just randomized crap.
The stack content thing just makes code scroll off the screen etc, for example."
The only real downside to removing these addresses is that they can be used to disambiguate duplicate symbol names. However such cases are rare, and the context of the stack dump should be enough to be able to figure it out.
Some kernel developers like to use addr2line to convert the addresses to a file name and line (which only works without randomization). They can instead use gdb for that purpose:
$ echo "list *driver_probe_device+0x223" |gdb vmlinux |grep "is in" (gdb) 0xffffffff815b5d83 is in driver_probe_device (/home/jpoimboe/git/linux/drivers/base/dd.c:378).
(But note that when there are duplicate symbol names, that will only show the first symbol it finds.)
Here's an example of what a stack dump looks like after this change:
kernel BUG at /home/jpoimboe/git/linux/include/linux/scatterlist.h:140! invalid opcode: 0000 [#1] PREEMPT SMP
Modules linked in: ... CPU: 0 PID: 338 Comm: systemd-udevd Not tainted 4.8.0-rc3+ #7 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.9.1-1.fc24 04/01/2014 task: ffff880078978000 task.stack: ffffc900008f0000 RIP: sg_init_one+0x87/0xa0 RSP: 0018:ffffc900008f3a68 EFLAGS: 00010246 RAX: 0000000000000000 RBX: ffffc900008f3a98 RCX: 0000000000000028 RDX: 0000000000000041 RSI: 0000000000000000 RDI: 00004100008f3a98 RBP: ffffc900008f3a80 R08: 00000002a014a073 R09: ffffc900008f3aa0 R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000008 R13: ffffc900008f3aa0 R14: 0000000000000000 R15: 0000000000000001 FS: 00007fa18fc20880(0000) GS:ffff88007d200000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fa18fc29000 CR3: 00000000799f8000 CR4: 00000000001406f0 Stack: ffff880079af7350 ffff880079905400 0000000000000000 ffffc900008f3ae0 ffffffffa0196610 0000000000000001 00010000ffffffff 0000000087654321 0000000000000002 0000000000000000 0000000000000000 0000000000000000 Call Trace: __send_control_msg+0x80/0xf0 [virtio_console] virtcons_probe+0x256/0x400 [virtio_console] virtio_dev_probe+0x144/0x1e0 [virtio] driver_probe_device+0x223/0x430 __driver_attach+0xe3/0xf0 ? driver_probe_device+0x430/0x430 bus_for_each_dev+0x73/0xc0 driver_attach+0x1e/0x20 bus_add_driver+0x173/0x270 ? virtio_cons_early_init+0x1d/0x1d [virtio_console] driver_register+0x60/0xe0 ? virtio_cons_early_init+0x1d/0x1d [virtio_console] register_virtio_driver+0x20/0x30 [virtio] init+0x9f/0xfe3 [virtio_console] do_one_initcall+0x50/0x180 ? do_init_module+0x27/0x1f9 ? rcu_read_lock_sched_held+0x45/0x80 ? kmem_cache_alloc_trace+0x28a/0x2f0 do_init_module+0x5f/0x1f9 load_module+0x270b/0x2c20 ? __symbol_put+0x90/0x90 ? show_coresize+0x30/0x30 ? vfs_read+0x121/0x140 SYSC_finit_module+0xdf/0x110 SyS_finit_module+0xe/0x10 entry_SYSCALL_64_fastpath+0x1f/0xbd Code: ff 83 e2 03 48 c1 e8 0c 81 e3 ff 0f 00 00 45 89 65 14 48 c1 e0 06 41 89 5d 10 48 01 c8 48 09 d0 5b 49 89 45 08 41 5c 41 5d 5d c3 <0f> 0b 0f 0b 0f 0b 48 8b 0d fc c6 9a 00 eb a6 66 2e 0f 1f 84 00 RIP: sg_init_one+0x87/0xa0 RSP: ffffc900008f3a68
--- arch/x86/include/asm/kdebug.h | 1 - arch/x86/kernel/dumpstack.c | 18 ++++-------------- arch/x86/kernel/process_32.c | 7 +++---- arch/x86/kernel/process_64.c | 3 +-- arch/x86/mm/fault.c | 3 +-- arch/x86/platform/uv/uv_nmi.c | 4 ++-- 6 files changed, 11 insertions(+), 25 deletions(-)
arch/x86/include/asm/kdebug.h +0 -1
@@ -21,7 +21,6 @@ enum die_val { DIE_NMIUNKNOWN, }; -extern void printk_address (unsigned long address);extern void die (const char *,struct pt_regs *,long );extern int __must_check __die (const char *,struct pt_regs *,long );extern void show_stack_regs (struct pt_regs *regs);
arch/x86/kernel/dumpstack.c +4 -14
@@ -29,14 +29,7 @@ static void printk_stack_address(unsigned long address, int reliable, char *log_lvl) {touch_nmi_watchdog (); -printk ("%s [<%p>] %s%pB\n" , - log_lvl, (void *)address, reliable ?"" :"? " , - (void *)address); -} - -void printk_address (unsigned long address) -{ -pr_cont (" [<%p>] %pS\n" , (void *)address, (void *)address); +printk ("%s %s%pB\n" , log_lvl, reliable ?"" :"? " , (void *)address); }/*
@@ -277,14 +270,11 @@ int __die(const char *str, struct pt_regs *regs, long err) sp =kernel_stack_pointer (regs); savesegment(ss, ss); } -printk (KERN_EMERG "EIP: [<%08lx>] " , regs->ip); -print_symbol ("%s" , regs->ip); -printk (" SS:ESP %04x:%08lx\n" , ss, sp); +printk (KERN_EMERG "EIP: %pS SS:ESP: %04x:%08lx\n" , + (void *)regs->ip, ss, sp);#else /* Executive summary in case the oops scrolled away */ -printk (KERN_ALERT "RIP " ); -printk_address (regs->ip); -printk (" RSP <%016lx>\n" , regs->sp); +printk (KERN_ALERT "RIP: %pS RSP: %016lx\n" , (void *)regs->ip, regs->sp);#endif return 0 ; }
arch/x86/kernel/process_32.c +3 -4
@@ -72,10 +72,9 @@ void __show_regs(struct pt_regs *regs, int all)savesegment (gs, gs); } -printk (KERN_DEFAULT "EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n" , - (u16 )regs->cs, regs->ip, regs->flags, -smp_processor_id ()); -print_symbol ("EIP is at %s\n" , regs->ip); +printk (KERN_DEFAULT "EIP: %pS\n" , (void *)regs->ip); +printk (KERN_DEFAULT "EFLAGS: %08lx CPU: %d\n" , regs->flags, +smp_processor_id ());printk (KERN_DEFAULT "EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n" , regs->ax, regs->bx, regs->cx, regs->dx);
arch/x86/kernel/process_64.c +1 -2
@@ -60,8 +60,7 @@ void __show_regs(struct pt_regs *regs, int all)unsigned int fsindex, gsindex;unsigned int ds, cs, es; -printk (KERN_DEFAULT "RIP: %04lx:[<%016lx>] " , regs->cs &0xffff , regs->ip); -printk_address (regs->ip); +printk (KERN_DEFAULT "RIP: %pS\n" , (void *)regs->ip);printk (KERN_DEFAULT "RSP: %04lx:%016lx EFLAGS: %08lx\n" , regs->ss, regs->sp, regs->flags); printk(KERN_DEFAULT "RAX: %016lx RBX: %016lx RCX: %016lx\n",
arch/x86/mm/fault.c +1 -2
@@ -679,8 +679,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code,printk (KERN_CONT "paging request" );printk (KERN_CONT " at %p\n" , (void *) address); -printk (KERN_ALERT "IP:" ); -printk_address (regs->ip); +printk (KERN_ALERT "IP: %pS\n" , (void *)regs->ip);dump_pagetable (address); }
arch/x86/platform/uv/uv_nmi.c +2 -2
@@ -387,8 +387,8 @@ static void uv_nmi_dump_cpu_ip_hdr(void)/* Dump Instruction Pointer info */ static void uv_nmi_dump_cpu_ip (int cpu,struct pt_regs *regs) { -pr_info ("UV: %4d %6d %-32.32s " , cpu, current->pid, current->comm); -printk_address (regs->ip); +pr_info ("UV: %4d %6d %-32.32s %pS" , + cpu, current->pid, current->comm, (void *)regs->ip); }/*
Signature · 1 line
2.7.4
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
Quoted 2 lines · Josh Poimboeuf
Here's an example of what a stack dump looks like after this change:
Looks good, but it also shows:
Quoted 1 line
RSP: 0018:ffffc900008f3a68 EFLAGS: 00010246
Ok, we know the stack pointer now...
Quoted 1 line
RBP:
ffffc900008f3a80R08:00000002a014a073R09:ffffc900008f3aa0
.. and it looks like we have a frame pointer too, which is pretty close to the stack pointer. So let's look at the stack dump:
Quoted 3 lines
Stack: ffff880079af7350 ffff880079905400 0000000000000000 ffffc900008f3ae0 ffffffffa0196610 0000000000000001 00010000ffffffff 0000000087654321
Yeah, counting down, the frame pointer points to the next frame, and right after that you see the return address: 0xffffffffa0196610.
So let's look at the call trace:
Quoted 2 lines
Call Trace: __send_control_msg+0x80/0xf0 [virtio_console]
Ok, so now we can match up that value to a symbol.
And that means that now we can match up *all* symbols in that module. Even if the module isn't readable on that machine, you can just look it up by checking the distro modules on another machine.
And if it had been core kernel code, you'd haev had the whole core kernel ofdfsets.
So I think the patch is good, and I think the oops looks great, but I think we should also just remove the stack dump. Sure, the register state *can* contain these things too, but almost never do (and didn't, in this example).
The stack dump actually goes back to forever, and it used to be useful back in 1992 or so. But it used to be useful mainly because stacks were simpler and we didn't have very good call traces anyway. I definitely remember having used them - I just do not remember having used them in the last ten+ years.
Of course, it's still true that if you can trigger an oops, you've likely already lost the security game, but since the stack dump is so useless, let's aim to just remove it and make games like the above harder.
I'm also sure that we probably have a lot of other addresses in dmesg that we should make sure aren't leaked. I did a quick grep and found
Base memory trampoline at [ffff8f5e00097000] 97000 size 24576
percpu: Embedded 35 pages/cpu @ffff8f6236c00000 s103640 r8192 d31528 u262144 Freeing SMP alternatives memory: 32K (ffffffffaaec1000 - ffffffffaaec9000)
and a few more, and didn't check if those might give load addresses away, but it would be good to check.
Linus
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
Quoted 11 lines · Linus Torvalds
So I think the patch is good, and I think the oops looks great, but I think we should also just remove the stack dump. Sure, the register state *can* contain these things too, but almost never do (and didn't, in this example).
The stack dump actually goes back to forever, and it used to be useful back in 1992 or so. But it used to be useful mainly because stacks were simpler and we didn't have very good call traces anyway. I definitely remember having used them - I just do not remember having used them in the last ten+ years.
Quoted 4 lines
Of course, it's still true that if you can trigger an oops, you've likely already lost the security game, but since the stack dump is so useless, let's aim to just remove it and make games like the above harder.
Yeah. I'll do another patch to get rid of the raw stack dump (though maybe I'll wait until the other patches get merged first so I don't have patches flying around everywhere).
Quoted 9 lines
I'm also sure that we probably have a lot of other addresses in dmesg that we should make sure aren't leaked. I did a quick grep and found
Base memory trampoline at [
ffff8f5e00097000] 97000 size 24576
percpu: Embedded 35 pages/cpu @ffff8f6236c00000 s103640 r8192 d31528 u262144 Freeing SMP alternatives memory: 32K (ffffffffaaec1000-ffffffffaaec9000)and a few more, and didn't check if those might give load addresses away, but it would be good to check.
On my system, a grep found these:
$ dmesg |grep "ffff[8-e]\|ffffffff[8-e]" [ 0.000000] found SMP MP-table at [mem 0x000f6b40-0x000f6b4f] mapped at [ffffa0b7000f6b40] [ 0.000000] Base memory trampoline at [ffffa0b700099000] 99000 size 24576 [ 0.000000] percpu: Embedded 485 pages/cpu @ffffa0b77d200000 s1946904 r8192 d31464 u2097152 [ 0.475975] Freeing SMP alternatives memory: 32K (ffffffff9e309000 - ffffffff9e311000) [ 2.656380] Freeing initrd memory: 10588K (ffffa0b736b42000 - ffffa0b737599000) [ 4.444099] Freeing unused kernel memory: 3592K (ffffffff9df87000 - ffffffff9e309000) [ 4.447080] Freeing unused kernel memory: 1352K (ffffa0b7288ae000 - ffffa0b728a00000) [ 4.449517] Freeing unused kernel memory: 632K (ffffa0b728d62000 - ffffa0b728e00000)
The text starts at 0xffffa0b728000000 and 0xffffffff9d000000. I think only the "Freeing" messages would give away the vmlinux mappings.
I'm wonder if it might be useful to encode the addresses somehow; they could conceivably be used to debug use-after-free issues. Or we could just remove them.
Signature · 1 line
Josh
Quoted 2 lines · Josh Poimboeuf
Some more preparatory patches for the new x86 unwinder (though these generally stand alone as improvements in their own right).
Ingo, would you be willing to take this series? There have been no objections other than Linus's orthogonal comment.
Signature · 1 line
Josh
Re: [PATCH 3/6] x86/dumpstack: make printk_stack_address() more generally useful
In reply to Josh PoimboeufQuoted 4 lines · Josh Poimboeuf
I'm wonder if it might be useful to encode the addresses somehow; they could conceivably be used to debug use-after-free issues. Or we could just remove them.
I suspect we should just remove them. I'm sure they are useful in theory, but I suspect they were more useful back when the whole "free init memory" was originally done.
These days, if we have a use-after-free, I suspect the init-mem situation is the easiest situation by far. Compared to all the dynamic allocations which are much more likely to show it anyway. So having debug output for that case is likely not all that productive.
Linus
Re: [PATCH 6/6] x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace()
In reply to Josh Poimboeufarch/x86/kernel/dumpstack_32.c static void *is_irq_stack(void *p, void *irq) · line 35
Quoted 7 lines · Josh Poimboeuf
arch/x86/kernel/dumpstack_32.c | 14 ++++++--------arch/x86/kernel/dumpstack_32.c +1
-0+static void *is_softirq_stack (unsigned long *stack); {
FYI, this bit clearly wasn't build tested on 32-bit even once - I'm skipping this patch.
Thanks,
Ingo
tip-bot for Josh Poimboeuf
merge bot[tip:x86/asm] perf/x86: Check perf_callchain_store() error
In reply to Josh Poimboeufperf/x86: Check perf_callchain_store() error
Commit-ID: 019e579d395733d14097c2d29c8c43226dad1617 Gitweb: http://git.kernel.org/tip/019e579d395733d14097c2d29c8c43226dad1617 Author: Josh Poimboeuf <[email protected]> AuthorDate: Wed, 24 Aug 2016 11:50:14 -0500 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 8 Sep 2016 08:58:40 +0200
perf/x86: Check perf_callchain_store() error
Add a check to perf_callchain_kernel() so that it returns early if the callchain entry array is already full.
--- arch/x86/events/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/events/core.c +2 -1
@@ -2277,7 +2277,8 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *rereturn ; } -perf_callchain_store (entry, regs->ip); +if (perf_callchain_store (entry, regs->ip)) +return ;dump_trace (NULL , regs,NULL ,0 , &backtrace_ops, entry); }
tip-bot for Josh Poimboeuf
merge bot[tip:x86/asm] oprofile/x86: Add regs->ip to oprofile trace
In reply to Josh Poimboeufoprofile/x86: Add regs->ip to oprofile trace
Commit-ID: 3e344a0db900757caaf0beeb749de4c7b59bfd60 Gitweb: http://git.kernel.org/tip/3e344a0db900757caaf0beeb749de4c7b59bfd60 Author: Josh Poimboeuf <[email protected]> AuthorDate: Wed, 24 Aug 2016 11:50:15 -0500 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 8 Sep 2016 08:58:40 +0200
oprofile/x86: Add regs->ip to oprofile trace
dump_trace() doesn't add the interrupted instruction's address to the trace, so add it manually. This makes the profile more useful, and also makes it more consistent with what perf profiling does.
--- arch/x86/oprofile/backtrace.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/oprofile/backtrace.c +10 -3
@@ -114,9 +114,16 @@ x86_backtrace(struct pt_regs * const regs, unsigned int depth)if (!user_mode (regs)) {unsigned long stack =kernel_stack_pointer (regs); -if (depth) -dump_trace (NULL , regs, (unsigned long *)stack,0 , - &backtrace_ops, &depth); + +if (!depth) +return ; + +oprofile_add_trace (regs->ip); +if (!--depth) +return ; + +dump_trace (NULL , regs, (unsigned long *)stack,0 , + &backtrace_ops, &depth);return ; }
tip-bot for Josh Poimboeuf
merge bot[tip:x86/asm] x86/dumpstack: Make printk_stack_address() more generally useful
In reply to Josh Poimboeufx86/dumpstack: Make printk_stack_address() more generally useful
Commit-ID: d438f5fda30ec087512355e405e9c8955d8bd337 Gitweb: http://git.kernel.org/tip/d438f5fda30ec087512355e405e9c8955d8bd337 Author: Josh Poimboeuf <[email protected]> AuthorDate: Wed, 24 Aug 2016 11:50:16 -0500 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 8 Sep 2016 08:58:40 +0200
x86/dumpstack: Make printk_stack_address() more generally useful
Change printk_stack_address() to be useful when called by an unwinder outside the context of dump_trace().
Specifically:
- printk_stack_address()'s 'data' argument is always used as the log level string. Make that explicit.
- Call touch_nmi_watchdog().
--- arch/x86/kernel/dumpstack.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/dumpstack.c +3 -3
@@ -26,10 +26,11 @@ int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;static int die_counter;static void printk_stack_address (unsigned long address,int reliable, -void *data) +char *log_lvl) { +touch_nmi_watchdog ();printk ("%s [<%p>] %s%pB\n" , - (char *)data, (void *)address, reliable ?"" :"? " , + log_lvl, (void *)address, reliable ?"" :"? " , (void *)address); }
@@ -148,7 +149,6 @@ static int print_trace_stack(void *data, char *name)*/ static int print_trace_address (void *data,unsigned long addr,int reliable) { -touch_nmi_watchdog ();printk_stack_address (addr, reliable, data);return 0 ; }
tip-bot for Josh Poimboeuf
merge bot[tip:x86/asm] x86/dumpstack: Add get_stack_pointer() and get_frame_pointer()
In reply to Josh Poimboeufx86/dumpstack: Add get_stack_pointer() and get_frame_pointer()
Commit-ID: 4b8afafbe743be1a81c96ddcd75b19c534d5e262 Gitweb: http://git.kernel.org/tip/4b8afafbe743be1a81c96ddcd75b19c534d5e262 Author: Josh Poimboeuf <[email protected]> AuthorDate: Wed, 24 Aug 2016 11:50:17 -0500 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 8 Sep 2016 08:58:40 +0200
x86/dumpstack: Add get_stack_pointer() and get_frame_pointer()
The various functions involved in dumping the stack all do similar things with regard to getting the stack pointer and the frame pointer based on the regs and task arguments. Create helper functions to do that instead.
--- arch/x86/include/asm/stacktrace.h | 41 ++++++++++++++++++++++----------------- arch/x86/kernel/dumpstack.c | 5 ++--- arch/x86/kernel/dumpstack_32.c | 25 ++++-------------------- arch/x86/kernel/dumpstack_64.c | 30 ++++------------------------ 4 files changed, 33 insertions(+), 68 deletions(-)
arch/x86/include/asm/stacktrace.h +23 -18
@@ -50,36 +50,41 @@ void dump_trace(struct task_struct *tsk, struct pt_regs *regs,#ifdef CONFIG_X86_32 #define STACKSLOTS_PER_LINE 8 -#define get_bp (bp) asm("movl %%ebp, %0" : "=r" (bp) :)#else #define STACKSLOTS_PER_LINE 4 -#define get_bp (bp) asm("movq %%rbp, %0" : "=r" (bp) :)#endif #ifdef CONFIG_FRAME_POINTER -static inline unsigned long -stack_frame (struct task_struct *task,struct pt_regs *regs) +static inline unsigned long * +get_frame_pointer (struct task_struct *task,struct pt_regs *regs) { -unsigned long bp; -if (regs) -return regs->bp; +return (unsigned long *)regs->bp; -if (task == current) { -/* Grab bp right from our regs */ -get_bp (bp); -return bp; - } +if (!task || task == current) +return __builtin_frame_address (0 ); -return ((struct inactive_task_frame *)task->thread.sp)->bp; +return (unsigned long *)((struct inactive_task_frame *)task->thread.sp)->bp; }#else -static inline unsigned long -stack_frame (struct task_struct *task,struct pt_regs *regs) +static inline unsigned long * +get_frame_pointer (struct task_struct *task,struct pt_regs *regs) { -return 0 ; +return NULL ; +} +#endif /* CONFIG_FRAME_POINTER */ + +static inline unsigned long * +get_stack_pointer (struct task_struct *task,struct pt_regs *regs) +{ +if (regs) +return (unsigned long *)kernel_stack_pointer (regs); + +if (!task || task == current) +return __builtin_frame_address (0 ); + +return (unsigned long *)task->thread.sp; } -#endif extern void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
@@ -106,7 +111,7 @@ static inline unsigned long caller_frame_pointer(void) {struct stack_frame *frame; -get_bp (frame); + frame =__builtin_frame_address (0 );#ifdef CONFIG_FRAME_POINTER frame = frame->next_frame;
arch/x86/kernel/dumpstack.c +2 -3
@@ -170,15 +170,14 @@ show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,void show_stack (struct task_struct *task,unsigned long *sp) {unsigned long bp =0 ; -unsigned long stack;/* * Stack frames below this one aren't interesting. Don't show them * if we're printing for %current. */ if (!sp && (!task || task == current)) { - sp = &stack; - bp =stack_frame (current,NULL ); + sp =get_stack_pointer (current,NULL ); + bp = (unsigned long )get_frame_pointer (current,NULL ); }show_stack_log_lvl (task,NULL , sp, bp,"" );
arch/x86/kernel/dumpstack_32.c +4 -21
@@ -46,19 +46,9 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,int graph =0 ;u32 *prev_esp; -if (!task) - task = current; - -if (!stack) { -unsigned long dummy; - - stack = &dummy; -if (task != current) - stack = (unsigned long *)task->thread.sp; - } - -if (!bp) - bp =stack_frame (task, regs); + task = task ? : current; + stack = stack ? :get_stack_pointer (task, regs); + bp = bp ? : (unsigned long )get_frame_pointer (task, regs);for (;;) {void *end_stack;
@@ -95,14 +85,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,unsigned long *stack;int i; -if (sp ==NULL ) { -if (regs) - sp = (unsigned long *)regs->sp; -else if (task) - sp = (unsigned long *)task->thread.sp; -else - sp = (unsigned long *)&sp; - } + sp = sp ? :get_stack_pointer (task, regs); stack = sp;for (i =0 ; i < kstack_depth_to_print; i++) {
arch/x86/kernel/dumpstack_64.c +4 -26
@@ -151,25 +151,14 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs, {const unsigned cpu =get_cpu ();unsigned long *irq_stack = (unsigned long *)per_cpu (irq_stack_ptr, cpu); -unsigned long dummy;unsigned used =0 ;int graph =0 ;int done =0 ; -if (!task) - task = current; + task = task ? : current; + stack = stack ? :get_stack_pointer (task, regs); + bp = bp ? : (unsigned long )get_frame_pointer (task, regs); -if (!stack) { -if (regs) - stack = (unsigned long *)regs->sp; -else if (task != current) - stack = (unsigned long *)task->thread.sp; -else - stack = &dummy; - } - -if (!bp) - bp =stack_frame (task, regs);/* * Print function call entries in all stacks, starting at the * current stack address. If the stacks consist of nested
@@ -256,18 +245,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, irq_stack_end = (unsigned long *)(per_cpu (irq_stack_ptr, cpu)); irq_stack = irq_stack_end - (IRQ_STACK_SIZE /sizeof (long )); -/* -* Debugging aid: "show_stack(NULL, NULL);" prints the -* back trace for this cpu: -*/ -if (sp ==NULL ) { -if (regs) - sp = (unsigned long *)regs->sp; -else if (task) - sp = (unsigned long *)task->thread.sp; -else - sp = (unsigned long *)&sp; - } + sp = sp ? :get_stack_pointer (task, regs); stack = sp;for (i =0 ; i < kstack_depth_to_print; i++) {
tip-bot for Josh Poimboeuf
merge bot[tip:x86/asm] x86/dumpstack: Remove unnecessary stack pointer arguments
In reply to Josh Poimboeufx86/dumpstack: Remove unnecessary stack pointer arguments
Commit-ID: 5a8ff54c260ecfed3de9b8d1272eb87826935df8 Gitweb: http://git.kernel.org/tip/5a8ff54c260ecfed3de9b8d1272eb87826935df8 Author: Josh Poimboeuf <[email protected]> AuthorDate: Wed, 24 Aug 2016 11:50:18 -0500 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 8 Sep 2016 08:58:40 +0200
x86/dumpstack: Remove unnecessary stack pointer arguments
When calling show_stack_log_lvl() or dump_trace() with a regs argument, providing a stack pointer or frame pointer is redundant.
--- arch/x86/kernel/dumpstack.c | 2 +- arch/x86/kernel/dumpstack_32.c | 2 +- arch/x86/kernel/dumpstack_64.c | 5 +---- arch/x86/oprofile/backtrace.c | 5 +---- 4 files changed, 4 insertions(+), 10 deletions(-)
arch/x86/kernel/dumpstack.c +1 -1
@@ -185,7 +185,7 @@ void show_stack(struct task_struct *task, unsigned long *sp)void show_stack_regs (struct pt_regs *regs) { -show_stack_log_lvl (current, regs, (unsigned long *)regs->sp, regs->bp,"" ); +show_stack_log_lvl (current, regs,NULL ,0 ,"" ); }static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
arch/x86/kernel/dumpstack_32.c +1 -1
@@ -122,7 +122,7 @@ void show_regs(struct pt_regs *regs)u8 *ip;pr_emerg ("Stack:\n" ); -show_stack_log_lvl (NULL , regs, ®s->sp,0 ,KERN_EMERG ); +show_stack_log_lvl (NULL , regs,NULL ,0 ,KERN_EMERG );pr_emerg ("Code:" );
arch/x86/kernel/dumpstack_64.c +1 -4
@@ -283,9 +283,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,void show_regs (struct pt_regs *regs) {int i; -unsigned long sp; - sp = regs->sp;show_regs_print_info (KERN_DEFAULT );__show_regs (regs,1 );
@@ -300,8 +298,7 @@ void show_regs(struct pt_regs *regs)u8 *ip;printk (KERN_DEFAULT "Stack:\n" ); -show_stack_log_lvl (NULL , regs, (unsigned long *)sp, -0 ,KERN_DEFAULT ); +show_stack_log_lvl (NULL , regs,NULL ,0 ,KERN_DEFAULT );printk (KERN_DEFAULT "Code: " );
arch/x86/oprofile/backtrace.c +1 -4
@@ -113,8 +113,6 @@ x86_backtrace(struct pt_regs * const regs, unsigned int depth)struct stack_frame *head = (struct stack_frame *)frame_pointer (regs);if (!user_mode (regs)) { -unsigned long stack =kernel_stack_pointer (regs); -if (!depth)return ;
@@ -122,8 +120,7 @@ x86_backtrace(struct pt_regs * const regs, unsigned int depth)if (!--depth)return ; -dump_trace (NULL , regs, (unsigned long *)stack,0 , - &backtrace_ops, &depth); +dump_trace (NULL , regs,NULL ,0 , &backtrace_ops, &depth);return ; }
Re: [PATCH 6/6] x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace()
In reply to Ingo MolnarQuoted 5 lines · Ingo Molnar
Quoted 1 line · Josh Poimboeuf
arch/x86/kernel/dumpstack_32.c | 14 ++++++--------
Quoted 8 lines
arch/x86/kernel/dumpstack_32.c static void *is_irq_stack(void *p, void *irq) · line 35
Quoted 5 lines
arch/x86/kernel/dumpstack_32.c +1
-0+static void *is_softirq_stack (unsigned long *stack); {FYI, this bit clearly wasn't build tested on 32-bit even once - I'm skipping this patch.
Argh, sorry about that. I did a lot of testing on 32-bit with this patch before. I guess I goofed somehow when I split up the patches.
Signature · 1 line
Josh
PATCH v2 1/1 x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace()
show_stack_log_lvl() and dump_trace() are already preemption safe:
- If they're running in irq or exception context, preemption is already disabled and the percpu stack pointers can be trusted.
- If they're running with preemption enabled, they must be running on the task stack anyway, so it doesn't matter if they're comparing the stack pointer against a percpu stack pointer from this CPU or another
one: either way it won't match.
--- arch/x86/kernel/dumpstack_32.c | 14 ++++++-------- arch/x86/kernel/dumpstack_64.c | 26 +++++++++----------------- 2 files changed, 15 insertions(+), 25 deletions(-)
arch/x86/kernel/dumpstack_32.c +6 -8
@@ -24,16 +24,16 @@ static void *is_irq_stack(void *p, void *irq) } -static void *is_hardirq_stack (unsigned long *stack,int cpu) +static void *is_hardirq_stack (unsigned long *stack) { -void *irq =per_cpu (hardirq_stack, cpu); +void *irq =this_cpu_read (hardirq_stack);return is_irq_stack (stack, irq); } -static void *is_softirq_stack (unsigned long *stack,int cpu) +static void *is_softirq_stack (unsigned long *stack) { -void *irq =per_cpu (softirq_stack, cpu); +void *irq =this_cpu_read (softirq_stack);return is_irq_stack (stack, irq); }
@@ -42,7 +42,6 @@ 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(); int graph = 0;u32 *prev_esp;
@@ -53,9 +52,9 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,for (;;) {void *end_stack; - end_stack =is_hardirq_stack (stack, cpu); + end_stack =is_hardirq_stack (stack);if (!end_stack) - end_stack =is_softirq_stack (stack, cpu); + end_stack =is_softirq_stack (stack); bp = ops->walk_stack (task, stack, bp, ops, data, end_stack, &graph);
@@ -74,7 +73,6 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,break ;touch_nmi_watchdog (); } -put_cpu (); }EXPORT_SYMBOL (dump_trace);
arch/x86/kernel/dumpstack_64.c +9 -17
@@ -31,8 +31,8 @@ static char x86_stack_ids[][8] = {#endif }; -static unsigned long *in_exception_stack (unsigned cpu,unsigned long stack, -unsigned *usedp,char **idp) +static unsigned long *in_exception_stack (unsigned long stack,unsigned *usedp, +char **idp) {unsigned k;
@@ -41,7 +41,7 @@ static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,* 'stack' is in one of them: */ for (k =0 ; k <N_EXCEPTION_STACKS ; k++) { -unsigned long end =per_cpu (orig_ist, cpu).ist[k]; +unsigned long end =raw_cpu_ptr (&orig_ist)->ist[k];/* * Is 'stack' above this exception frame's end? * If yes then skip to the next frame.
@@ -111,7 +111,7 @@ enum stack_type { };static enum stack_type -analyze_stack (int cpu,struct task_struct *task,unsigned long *stack, +analyze_stack (struct task_struct *task,unsigned long *stack,unsigned long **stack_end,unsigned long *irq_stack,unsigned *used,char **id) {
@@ -121,8 +121,7 @@ analyze_stack(int cpu, struct task_struct *task, unsigned long *stack,if ((unsigned long )task_stack_page (task) == addr)return STACK_IS_NORMAL ; - *stack_end =in_exception_stack (cpu, (unsigned long )stack, - used, id); + *stack_end =in_exception_stack ((unsigned long )stack, used, id);if (*stack_end)return STACK_IS_EXCEPTION ;
@@ -149,8 +148,7 @@ 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 = (unsigned long *)per_cpu (irq_stack_ptr, cpu); + unsigned long *irq_stack = (unsigned long *)this_cpu_read(irq_stack_ptr);unsigned used =0 ;int graph =0 ;int done =0 ;
@@ -169,8 +167,8 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,enum stack_type stype;char *id; - stype =analyze_stack (cpu, task, stack, &stack_end, - irq_stack, &used, &id); + stype =analyze_stack (task, stack, &stack_end, irq_stack, &used, + &id);/* Default finish unless specified to continue */ done =1 ;
@@ -225,7 +223,6 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,* This handles the process stack: */ bp = ops->walk_stack (task, stack, bp, ops, data,NULL , &graph); -put_cpu (); }EXPORT_SYMBOL (dump_trace);
@@ -236,13 +233,9 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,unsigned long *irq_stack_end;unsigned long *irq_stack;unsigned long *stack; -int cpu;int i; -preempt_disable (); - cpu =smp_processor_id (); - - irq_stack_end = (unsigned long *)(per_cpu (irq_stack_ptr, cpu)); + irq_stack_end = (unsigned long *)this_cpu_read (irq_stack_ptr); irq_stack = irq_stack_end - (IRQ_STACK_SIZE /sizeof (long )); sp = sp ? :get_stack_pointer (task, regs);
@@ -274,7 +267,6 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, stack++;touch_nmi_watchdog (); } -preempt_enable ();pr_cont ("\n" );show_trace_log_lvl (task, regs, sp, bp, log_lvl);
Signature · 1 line
2.7.4
Re: [PATCH 6/6] x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace()
In reply to Josh PoimboeufQuoted 9 lines · Josh Poimboeuf
Quoted 8 lines · Ingo Molnar
Quoted 4 lines · Josh Poimboeuf
arch/x86/kernel/dumpstack_32.c | 14 ++++++--------arch/x86/kernel/dumpstack_32.c +0
-0
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
+static void *is_softirq_stack(unsigned long *stack);
{FYI, this bit clearly wasn't build tested on 32-bit even once - I'm skipping this patch.
Argh, sorry about that. I did a lot of testing on 32-bit with this patch before. I guess I goofed somehow when I split up the patches.
No problem!
Thanks,
Ingo
Re: [PATCH v2] x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace()
In reply to Josh PoimboeufQuoted 9 lines · Josh Poimboeuf
show_stack_log_lvl()anddump_trace()are already preemption safe:- If they're running in irq or exception context, preemption is already disabled and the percpu stack pointers can be trusted.
- If they're running with preemption enabled, they must be running on the task stack anyway, so it doesn't matter if they're comparing the stack pointer against a percpu stack pointer from this CPU or another
one: either way it won't match.
Yeah, so I'm having second thoughts about this patch. My worry here is: what if we get preempted in this sequence?
If the kernel is borked real bad then we could get technically correct but really, really weird looking stack traces if for example the task stack is getting corrupted or something like that.
Dunno. How long does the worst-case processing here take on a typical x86 system, does it really matter to scheduling latency?
Thanks,
Ingo
Re: [PATCH v2] x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace()
Quoted 13 lines · Ingo Molnar
Quoted 9 lines · Josh Poimboeuf
show_stack_log_lvl()anddump_trace()are already preemption safe:- If they're running in irq or exception context, preemption is already disabled and the percpu stack pointers can be trusted.
- If they're running with preemption enabled, they must be running on the task stack anyway, so it doesn't matter if they're comparing the stack pointer against a percpu stack pointer from this CPU or another
one: either way it won't match.
Quoted 6 lines
Yeah, so I'm having second thoughts about this patch. My worry here is: what if we get preempted in this sequence?
If the kernel is borked real bad then we could get technically correct but really, really weird looking stack traces if for example the task stack is getting corrupted or something like that.
If it's in the oops or BUG path, there can't be preemption anyway because oops_begin() disables interrupts.
It does look like the WARN path could get preempted. Not to mention all the other callers of show_regs(), dump_stack(), show_stack_log_lvl(), etc. In those cases, if the stack dump got preempted in the middle, and then another task dumped its stack, the two dumps could be interspersed a bit which would indeed be a little confusing.
But that would be quite rare. And anyway, we already have the same issue today when two CPUs are dumping the stack at the same time. So I don't think it's much of an issue.
Quoted 2 lines
Dunno. How long does the worst-case processing here take on a typical x86 system, does it really matter to scheduling latency?
I haven't heard any complaints about latency. The goal was just to try to simplify the code a bit.
Signature · 1 line
Josh
tip-bot for Josh Poimboeuf
merge bot[tip:x86/asm] x86/dumpstack: Allow preemption in show_stack_log_lvl() and dump_trace()
In reply to Josh Poimboeufx86/dumpstack: Allow preemption in show_stack_log_lvl() and dump_trace()
Commit-ID: cfeeed279dc2fa83a00fbe4856ebd231d56201ab Gitweb: http://git.kernel.org/tip/cfeeed279dc2fa83a00fbe4856ebd231d56201ab Author: Josh Poimboeuf <[email protected]> AuthorDate: Thu, 8 Sep 2016 16:49:20 -0500 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 14 Sep 2016 17:23:30 +0200
x86/dumpstack: Allow preemption in show_stack_log_lvl() and dump_trace()
show_stack_log_lvl() and dump_trace() are already preemption safe:
- If they're running in irq or exception context, preemption is already disabled and the percpu stack pointers can be trusted.
- If they're running with preemption enabled, they must be running on the task stack anyway, so it doesn't matter if they're comparing the stack pointer against a percpu stack pointer from this CPU or another
one: either way it won't match.
--- arch/x86/kernel/dumpstack_32.c | 14 ++++++-------- arch/x86/kernel/dumpstack_64.c | 26 +++++++++----------------- 2 files changed, 15 insertions(+), 25 deletions(-)
arch/x86/kernel/dumpstack_32.c +6 -8
@@ -24,16 +24,16 @@ static void *is_irq_stack(void *p, void *irq) } -static void *is_hardirq_stack (unsigned long *stack,int cpu) +static void *is_hardirq_stack (unsigned long *stack) { -void *irq =per_cpu (hardirq_stack, cpu); +void *irq =this_cpu_read (hardirq_stack);return is_irq_stack (stack, irq); } -static void *is_softirq_stack (unsigned long *stack,int cpu) +static void *is_softirq_stack (unsigned long *stack) { -void *irq =per_cpu (softirq_stack, cpu); +void *irq =this_cpu_read (softirq_stack);return is_irq_stack (stack, irq); }
@@ -42,7 +42,6 @@ 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(); int graph = 0;u32 *prev_esp;
@@ -53,9 +52,9 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,for (;;) {void *end_stack; - end_stack =is_hardirq_stack (stack, cpu); + end_stack =is_hardirq_stack (stack);if (!end_stack) - end_stack =is_softirq_stack (stack, cpu); + end_stack =is_softirq_stack (stack); bp = ops->walk_stack (task, stack, bp, ops, data, end_stack, &graph);
@@ -74,7 +73,6 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,break ;touch_nmi_watchdog (); } -put_cpu (); }EXPORT_SYMBOL (dump_trace);
arch/x86/kernel/dumpstack_64.c +9 -17
@@ -31,8 +31,8 @@ static char x86_stack_ids[][8] = {#endif }; -static unsigned long *in_exception_stack (unsigned cpu,unsigned long stack, -unsigned *usedp,char **idp) +static unsigned long *in_exception_stack (unsigned long stack,unsigned *usedp, +char **idp) {unsigned k;
@@ -41,7 +41,7 @@ static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,* 'stack' is in one of them: */ for (k =0 ; k <N_EXCEPTION_STACKS ; k++) { -unsigned long end =per_cpu (orig_ist, cpu).ist[k]; +unsigned long end =raw_cpu_ptr (&orig_ist)->ist[k];/* * Is 'stack' above this exception frame's end? * If yes then skip to the next frame.
@@ -111,7 +111,7 @@ enum stack_type { };static enum stack_type -analyze_stack (int cpu,struct task_struct *task,unsigned long *stack, +analyze_stack (struct task_struct *task,unsigned long *stack,unsigned long **stack_end,unsigned long *irq_stack,unsigned *used,char **id) {
@@ -121,8 +121,7 @@ analyze_stack(int cpu, struct task_struct *task, unsigned long *stack,if ((unsigned long )task_stack_page (task) == addr)return STACK_IS_NORMAL ; - *stack_end =in_exception_stack (cpu, (unsigned long )stack, - used, id); + *stack_end =in_exception_stack ((unsigned long )stack, used, id);if (*stack_end)return STACK_IS_EXCEPTION ;
@@ -149,8 +148,7 @@ 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 = (unsigned long *)per_cpu (irq_stack_ptr, cpu); + unsigned long *irq_stack = (unsigned long *)this_cpu_read(irq_stack_ptr);unsigned used =0 ;int graph =0 ;int done =0 ;
@@ -169,8 +167,8 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,enum stack_type stype;char *id; - stype =analyze_stack (cpu, task, stack, &stack_end, - irq_stack, &used, &id); + stype =analyze_stack (task, stack, &stack_end, irq_stack, &used, + &id);/* Default finish unless specified to continue */ done =1 ;
@@ -225,7 +223,6 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,* This handles the process stack: */ bp = ops->walk_stack (task, stack, bp, ops, data,NULL , &graph); -put_cpu (); }EXPORT_SYMBOL (dump_trace);
@@ -236,13 +233,9 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,unsigned long *irq_stack_end;unsigned long *irq_stack;unsigned long *stack; -int cpu;int i; -preempt_disable (); - cpu =smp_processor_id (); - - irq_stack_end = (unsigned long *)(per_cpu (irq_stack_ptr, cpu)); + irq_stack_end = (unsigned long *)this_cpu_read (irq_stack_ptr); irq_stack = irq_stack_end - (IRQ_STACK_SIZE /sizeof (long )); sp = sp ? :get_stack_pointer (task, regs);
@@ -274,7 +267,6 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, stack++;touch_nmi_watchdog (); } -preempt_enable ();pr_cont ("\n" );show_trace_log_lvl (task, regs, sp, bp, log_lvl);