kernel: backtrace unwind support
cover kernel: backtrace unwind support
hi,
I was recently dealing with libunwind and wanted to try out the dwarf backtrace unwind in kernel space.
The attached patchset implements dwarf backtrace unwind based on the exception header frames (.eh_frame_hdr and .eh_frame ELF sections). The code is mostly stolen from libunwind (git://git.sv.gnu.org/libunwind.git).
I'm not sure how much of usage this can be given that we already have quite reliable stack backtrace, and given the complexity of the dwarf unwind. But I might be overlooking something and this could be of use for someone else.
Also it needs to be said, that the state of this patchset is far from being done. It's in state 'working for me' on x86_64 and seems to provide reliable backtrace.
attached patches:
- 1/5 unwind, kconfig: Adding UNWIND* options
- 2/5 unwind, x86: Generate exception frames data for UNWIND_EH_FRAME option
- 3/5 unwind, dwarf: Add dwarf unwind support
- 4/5 unwind, api: Add unwind interface and implementation for x86_64
- 5/5 unwind, test: Add backtrace unwind test code
The test code could be triggered via unwind_test debugfs file with following output:
# echo > ./unwind_test
Testing unwind from process context.
unwind backtrace:
[0xffffffff810ef759] unw_backtrace+0x29/0x80
[0xffffffff810ef7d4] test_write+0x24/0x90
[0xffffffff81138940] vfs_write+0xd0/0x1a0
[0xffffffff81138b14] sys_write+0x54/0xa0
[0xffffffff814d7352] system_call_fastpath+0x16/0x1b
Testing a unwind from irq context.
unwind backtrace:
[0xffffffff810ef759] unw_backtrace+0x29/0x80
[0xffffffff810ef84e] unwind_test_irq_callback+0xe/0x20
[0xffffffff8103d1c3] tasklet_action+0x143/0x150
[0xffffffff8103d9bd] __do_softirq+0xdd/0x250
[0xffffffff8103dc18] run_ksoftirqd+0xe8/0x200
[0xffffffff8105a1b6] kthread+0xc6/0xd0
[0xffffffff814d8564] kernel_thread_helper+0x4/0x10thanks for comments,
jirka
--- arch/x86/Kconfig.debug | 2 + arch/x86/Makefile | 8 +- arch/x86/include/asm/dwarf.h | 89 +++++ arch/x86/include/asm/unwind.h | 14 + arch/x86/kernel/Makefile | 2 + arch/x86/kernel/dwarf.c | 101 ++++++ arch/x86/kernel/unwind_init_64.S | 31 ++ arch/x86/kernel/vmlinux.lds.S | 5 + include/asm-generic/vmlinux.lds.h | 12 + include/linux/dwarf.h | 161 +++++++++ include/linux/unwind.h | 23 ++ kernel/Kconfig.unwind | 26 ++ kernel/Makefile | 6 + kernel/dwarf-cfi.c | 337 ++++++++++++++++++ kernel/dwarf-expression.c | 694 +++++++++++++++++++++++++++++++++++++ kernel/dwarf-fde.c | 349 +++++++++++++++++++ kernel/dwarf-read.c | 227 ++++++++++++ kernel/dwarf.c | 7 + kernel/unwind.c | 224 ++++++++++++ 19 files changed, 2317 insertions(+), 1 deletions(-)
PATCH 1/5 unwind, kconfig: Adding UNWIND* options
Adding following config options:
CONFIG_UNWIND
- governs wether the unwind code is compiled in
CONFIG_UNWIND_EH_FRAME
- source of unwind data - eh_frame_hdr/eh_frame
CONFIG_UNWIND_DEBUG_FRAME
- source of unwind data - .debug.frame
--- arch/x86/Kconfig.debug | 2 ++ kernel/Kconfig.unwind | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 0 deletions(-) create mode 100644 kernel/Kconfig.unwind
arch/x86/Kconfig.debug +2 -0
@@ -299,4 +299,6 @@ config DEBUG_NMI_SELFTEST If unsure, say N. +source "kernel/Kconfig.unwind" + endmenu
kernel/Kconfig.unwind new file +26 -0
@@ -0,0 +1,26 @@ + +config UNWIND + bool "Use compiler information to display backtrace dump" + ---help--- + Adding code allowing to use compiled debug information + for stack unwinding (results in MUCH bigger kernel + and many more panics). + +choice + prompt "Unwind information source" + default UNWIND_EH_FRAME + depends on UNWIND + ---help--- + source of unwind information + +config UNWIND_EH_FRAME + bool "exception frame section" + ---help--- + eh_frame section + +config UNWIND_DEBUG_FRAME + bool "NOT IMPLEMENTED debug frame section" + ---help--- + debug_frame section + +endchoice
Signature · 1 line
1.7.1
PATCH 2/5 unwind, x86: Generate exception frames data for UNWIND_EH_FRAME option
In reply to Jiri OlsaAdding support to compile in eh_frame_hdr/eh_frame unwind data. Plus several symbols to properly access this data.
__eh_frame_hdr_start __eh_frame_hdr_end __eh_frame_start __eh_frame_end
Adding this for x86 arch, tested for x86_64 only.
--- arch/x86/Makefile | 8 +++++++- arch/x86/kernel/vmlinux.lds.S | 5 +++++ include/asm-generic/vmlinux.lds.h | 12 ++++++++++++ 3 files changed, 24 insertions(+), 1 deletions(-)
arch/x86/Makefile +7 -1
@@ -109,8 +109,14 @@ LDFLAGS := -m elf_$(UTS_MACHINE) KBUILD_CFLAGS += -pipe # Workaround for a gcc prelease that unfortunately was shipped in a suse release KBUILD_CFLAGS += -Wno-sign-compare -# + +ifdef CONFIG_UNWIND_EH_FRAME +LDFLAGS_vmlinux += --eh-frame-hdr +else +# do not generate ef_frame section by default KBUILD_CFLAGS += -fno-asynchronous-unwind-tables +endif + # prevent gcc from generating any FP code by mistake KBUILD_CFLAGS += $(call cc-option,-mno-sse -mno-mmx -mno-sse2 -mno-3dnow,)
arch/x86/kernel/vmlinux.lds.S +5 -0
@@ -114,6 +114,9 @@ SECTIONS NOTES :text :note + /* Exception (header) frames */ + EH_FRAME + EXCEPTION_TABLE(16) :text = 0x9090 #if defined(CONFIG_DEBUG_RODATA)
@@ -335,7 +338,9 @@ SECTIONS /* Sections to be discarded */ DISCARDS +#ifndef CONFIG_UNWIND_EH_FRAME /DISCARD/ : { *(.eh_frame) } +#endif }
include/asm-generic/vmlinux.lds.h +12 -0
@@ -602,6 +602,18 @@#define TRACEDATA #endif +#ifdef CONFIG_UNWIND_EH_FRAME +#define EH_FRAME \ + VMLINUX_SYMBOL(__eh_frame_hdr_start) = .; \ + .eh_frame_hdr : { *(.eh_frame_hdr) } \ + VMLINUX_SYMBOL(__eh_frame_hdr_end) = .; \ + VMLINUX_SYMBOL(__eh_frame_start) = .; \ + .eh_frame : { *(.eh_frame) } \ + VMLINUX_SYMBOL(__eh_frame_end) = .; +#else +#define EH_FRAME +#endif +#define NOTES \ .notes : AT(ADDR(.notes) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start_notes) = .; \
Signature · 1 line
1.7.1
PATCH 3/5 unwind, dwarf: Add dwarf unwind support
In reply to Jiri OlsaAdding dwarf object to handle unwind processing, mainly:
dwarf-cfi.c
- handles the cfi processing for FDE/CIE instructions
dwarf-expression.c
- handles the expression processing for CFI instructions:
DW_CFA_def_cfa_expression/DW_CFA_expression
dwarf-fde.c
- handles reading/processing FDE/CIE records
- governs the CFI intruction processing
dwarf-read.c
- data reading functions
--- arch/x86/include/asm/dwarf.h | 89 ++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/dwarf.c | 101 ++++++ include/linux/dwarf.h | 161 ++++++++++ kernel/Makefile | 5 + kernel/dwarf-cfi.c | 337 ++++++++++++++++++++ kernel/dwarf-expression.c | 694 ++++++++++++++++++++++++++++++++++++++++++ kernel/dwarf-fde.c | 349 +++++++++++++++++++++ kernel/dwarf-read.c | 227 ++++++++++++++ kernel/dwarf.c | 7 + 10 files changed, 1971 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/dwarf.h create mode 100644 arch/x86/kernel/dwarf.c create mode 100644 include/linux/dwarf.h create mode 100644 kernel/dwarf-cfi.c create mode 100644 kernel/dwarf-expression.c create mode 100644 kernel/dwarf-fde.c create mode 100644 kernel/dwarf-read.c create mode 100644 kernel/dwarf.c
arch/x86/include/asm/dwarf.h new file +89 -0
@@ -0,0 +1,89 @@ +/* +* Code mostly taken from libunwind (git://git.sv.gnu.org/libunwind.git) +* Adding copyright notice as requested: +* +* Copyright (c) 2002 Hewlett-Packard Co. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +*/ + +#ifndef _ARCH_X86_KERNEL_DWARF_H +#define _ARCH_X86_KERNEL_DWARF_H + +#include <linux/types.h> + +#ifdef __i386__ +typedef uint32_t dwarf_word_t ; +typedef int32_t dwarf_sword_t ; + +enum { +/* Standard x86 registers. */ +DWARF_X86_EAX , +DWARF_X86_ECX , +DWARF_X86_EDX , +DWARF_X86_EBX , +DWARF_X86_ESP , +DWARF_X86_EBP , +DWARF_X86_ESI , +DWARF_X86_EDI , +DWARF_X86_EIP , +DWARF_X86_EFLAGS , +DWARF_X86_TRAPNO , +DWARF_X86_ST0 , +DWARF_X86_ST1 , +DWARF_X86_ST2 , +DWARF_X86_ST3 , +DWARF_X86_ST4 , +DWARF_X86_ST5 , +DWARF_X86_ST6 , +DWARF_X86_ST7 + + /* Trating CFA as special register. */ +DWARF_CFA_REG_COLUMN , +DWARF_CFA_OFF_COLUMN , + +DWARF_REGS_NUM , +DWARF_SP =DWARF_X86_ESP , +}; +#else +typedef uint64_t dwarf_word_t ; +typedef int64_t dwarf_sword_t ; + +enum { +/* Standard x86_64 registers. */ +DWARF_X86_64_RAX , +DWARF_X86_64_RDX , +DWARF_X86_64_RCX , +DWARF_X86_64_RBX , +DWARF_X86_64_RSI , +DWARF_X86_64_RDI , +DWARF_X86_64_RBP , +DWARF_X86_64_RSP , +DWARF_X86_64_R8 , +DWARF_X86_64_R9 , +DWARF_X86_64_R10 , +DWARF_X86_64_R11 , +DWARF_X86_64_R12 , +DWARF_X86_64_R13 , +DWARF_X86_64_R14 , +DWARF_X86_64_R15 , +DWARF_X86_64_RIP , + +/* Trating CFA as special register. */ +DWARF_CFA_REG_COLUMN , +DWARF_CFA_OFF_COLUMN , + +DWARF_REGS_NUM , +DWARF_SP =DWARF_X86_64_RSP , +}; +#endif /* __i386__ */ +#endif /* _ARCH_X86_KERNEL_UNWIND_H */
arch/x86/kernel/Makefile +1 -0
@@ -100,6 +100,7 @@ obj-$(CONFIG_X86_CHECK_BIOS_CORRUPTION) += check.o obj-$(CONFIG_SWIOTLB) += pci-swiotlb.o obj-$(CONFIG_OF) += devicetree.o +obj-$(CONFIG_UNWIND) += dwarf.o ### # 64 bit specific files
arch/x86/kernel/dwarf.c new file +101 -0
101 lines not on this page · raw message
include/linux/dwarf.h new file +161 -0
161 lines not on this page · raw message
kernel/Makefile +5 -0
@@ -107,6 +107,11 @@ obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o obj-$(CONFIG_PADATA) += padata.o obj-$(CONFIG_CRASH_DUMP) += crash_dump.o obj-$(CONFIG_JUMP_LABEL) += jump_label.o +obj-$(CONFIG_UNWIND) += dwarf.o +obj-$(CONFIG_UNWIND) += dwarf-read.o +obj-$(CONFIG_UNWIND) += dwarf-cfi.o +obj-$(CONFIG_UNWIND) += dwarf-expression.o +obj-$(CONFIG_UNWIND) += dwarf-fde.o $(obj)/configs.o: $(obj)/config_data.h
kernel/dwarf-cfi.c new file +337 -0
337 lines not on this page · raw message
kernel/dwarf-expression.c new file +694 -0
694 lines not on this page · raw message
kernel/dwarf-fde.c new file +349 -0
349 lines not on this page · raw message
kernel/dwarf-read.c new file +227 -0
227 lines not on this page · raw message
kernel/dwarf.c new file +7 -0
@@ -0,0 +1,7 @@ + +#include <linux/kernel.h> +#include <linux/module.h> + +int dwarf_debug =0 ; +module_param (dwarf_debug, int,0644 ); +MODULE_PARM_DESC (dwarf_debug,"Turns on debug for dwarf code." );
Signature · 1 line
1.7.1
PATCH 4/5 unwind, api: Add unwind interface and implementation for x86_64
In reply to Jiri OlsaAdding unwind interface with x86_64 implementation. The interface consists of following functions:
struct unw_t;
- single backtrace handle
void unw_init(struct unw_t *u);
- initialize the handle
void unw_regs(struct unw_t *u, struct pt_regs *regs);
- returns current struct pt_regs registers data
int unw_step(struct unw_t *u);
- makes single backtrace step
void unw_backtrace(void);
- runs the backtrace unwind and printk it out
The example usage is shown in unw_backtrace function.
--- arch/x86/include/asm/unwind.h | 14 +++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/unwind_init_64.S | 31 +++++++ include/linux/unwind.h | 23 +++++ kernel/Makefile | 1 + kernel/unwind.c | 180 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 250 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/unwind.h create mode 100644 arch/x86/kernel/unwind_init_64.S create mode 100644 include/linux/unwind.h create mode 100644 kernel/unwind.c
arch/x86/include/asm/unwind.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef _ARCH_X86_KERNEL_UNWIND_H +#define _ARCH_X86_KERNEL_UNWIND_H + +#include <asm/ptrace-abi.h> + +#ifdef __ASSEMBLY__ +#ifdef __i386__ +#define UNW_X86_CFA_OFF (FRAME_SIZE + 0x0) +#else +#define UNW_X86_64_CFA_OFF (FRAME_SIZE + 0x0) +#endif /* __i386__ */ +#endif /* __ASSEMBLY__ */ + +#endif /* _ARCH_X86_KERNEL_UNWIND_H */
arch/x86/kernel/Makefile +1 -0
@@ -101,6 +101,7 @@ obj-$(CONFIG_X86_CHECK_BIOS_CORRUPTION) += check.o obj-$(CONFIG_SWIOTLB) += pci-swiotlb.o obj-$(CONFIG_OF) += devicetree.o obj-$(CONFIG_UNWIND) += dwarf.o +obj-$(CONFIG_UNWIND) += unwind_init_$(BITS).o ### # 64 bit specific files
arch/x86/kernel/unwind_init_64.S new file +31 -0
@@ -0,0 +1,31 @@ + +#include <linux/linkage.h> +#include <asm/ptrace-abi.h> +#include <asm/unwind.h> + + .code64 +ENTRY(unw_init) + /* Callee saved: RBX, RBP, R12-R15 */ + movq %r12, R12(%rdi) + movq %r13, R13(%rdi) + movq %r14, R14(%rdi) + movq %r15, R15(%rdi) + movq %rbp, RBP(%rdi) + movq %rbx, RBX(%rdi) + + movq %r8, R8(%rdi) + movq %r9, R9(%rdi) + movq %rdi, RDI(%rdi) + movq %rsi, RSI(%rdi) + movq %rdx, RDX(%rdi) + movq %rax, RAX(%rdi) + movq %rcx, RCX(%rdi) + + leaq 8(%rsp), %rax /* exclude this call. */ + movq %rax, UNW_X86_64_CFA_OFF(%rdi) + movq 0(%rsp), %rax + movq %rax, RIP(%rdi) + + xorq %rax, %rax + retq +END(unw_init)
include/linux/unwind.h new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef UNWIND_H +#define UNWIND_H + +#include <linux/ptrace.h> +#include <asm/unwind.h> +#include <linux/dwarf.h> + +struct unw_t { +struct pt_regs regs; +dwarf_word_t cfa; + +/* +* First 2 items are touched by assembly code, +* do not move them. +*/ +}; + +void unw_init (struct unw_t *u); +void unw_regs (struct unw_t *u,struct pt_regs *regs); +int unw_step (struct unw_t *u); +void unw_backtrace (void ); + +#endif /* UNWIND_H */
kernel/Makefile +1 -0
@@ -112,6 +112,7 @@ obj-$(CONFIG_UNWIND) += dwarf-read.o obj-$(CONFIG_UNWIND) += dwarf-cfi.o obj-$(CONFIG_UNWIND) += dwarf-expression.o obj-$(CONFIG_UNWIND) += dwarf-fde.o +obj-$(CONFIG_UNWIND) += unwind.o $(obj)/configs.o: $(obj)/config_data.h
kernel/unwind.c new file +180 -0
180 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH 5/5 unwind, test: Add backtrace unwind test code
In reply to Jiri OlsaAdding test code for backtrace unwinding. The debugfs file 'unwind_test' is created in debugfs root. Writing to this file triggers backtrace from process and irq context.
Mostly stolen from kernel/backtracetest.c. Getting following output in dmesg:
# echo > ./unwind_test
Testing unwind from process context.
unwind backtrace:
[0xffffffff810ef759] unw_backtrace+0x29/0x80
[0xffffffff810ef7d4] test_write+0x24/0x90
[0xffffffff81138940] vfs_write+0xd0/0x1a0
[0xffffffff81138b14] sys_write+0x54/0xa0
[0xffffffff814d7352] system_call_fastpath+0x16/0x1b
Testing a unwind from irq context.
unwind backtrace:
[0xffffffff810ef759] unw_backtrace+0x29/0x80
[0xffffffff810ef84e] unwind_test_irq_callback+0xe/0x20
[0xffffffff8103d1c3] tasklet_action+0x143/0x150
[0xffffffff8103d9bd] __do_softirq+0xdd/0x250
[0xffffffff8103dc18] run_ksoftirqd+0xe8/0x200
[0xffffffff8105a1b6] kthread+0xc6/0xd0
[0xffffffff814d8564] kernel_thread_helper+0x4/0x10
---
kernel/unwind.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 44 insertions(+), 0 deletions(-)kernel/unwind.c +44 -0
@@ -178,3 +178,47 @@ void unw_backtrace(void) }while (!unw_step (&unw)); } + +static DECLARE_COMPLETION (unwind_work); + +static void unwind_test_irq_callback (unsigned long data) +{ +unw_backtrace (); +complete (&unwind_work); +} + +static DECLARE_TASKLET(unwind_tasklet, &unwind_test_irq_callback, 0); + +static void unw_test_irq (void ) +{ +printk ("Testing a unwind from irq context.\n" ); + +init_completion (&unwind_work); +tasklet_schedule (&unwind_tasklet); +wait_for_completion (&unwind_work); +} + +static ssize_t +test_write (struct file *filp,const char __user *ubuf, +size_t cnt,loff_t *ppos) +{ +printk ("Testing unwind from process context.\n" ); +unw_backtrace (); +unw_test_irq (); +return cnt; +} + +static const struct file_operations test_fops = { + .write = test_write, +}; + +static int __init unwind_init_test (void ) +{ +if (!debugfs_create_file ("unwind_test" ,0644 ,NULL ,NULL , + &test_fops)) +return -ENOMEM ; + +return 0 ; +} + +late_initcall (unwind_init_test);
Signature · 1 line
1.7.1
Quoted 18 lines · Jiri Olsa
I was recently dealing with libunwind and wanted to try out the dwarf backtrace unwind in kernel space.
The attached patchset implements dwarf backtrace unwind based on the exception header frames (.eh_frame_hdr and .eh_frame ELF sections). The code is mostly stolen from libunwind (
git://git.sv.gnu.org/libunwind.git).I'm not sure how much of usage this can be given that we already have quite reliable stack backtrace, and given the complexity of the dwarf unwind. But I might be overlooking something and this could be of use for someone else.
Also it needs to be said, that the state of this patchset is far from being done. It's in state 'working for me' on
x86_64and seems to provide reliable backtrace.
Quoted 6 lines
attached patches:
- 1/5 unwind, kconfig: Adding UNWIND* options
- 2/5 unwind, x86: Generate exception frames data forUNWIND_EH_FRAMEoption
- 3/5 unwind, dwarf: Add dwarf unwind support
- 4/5 unwind, api: Add unwind interface and implementation forx86_64
- 5/5 unwind, test: Add backtrace unwind test code
Right, so last time someone did a x86 dwarf unwinder there was a bit of a 'discussion' and Linus basically told people to go away. That said, there are a number of literate dwarfs in various architectures because that simply the only way to get a backtrace on them.
So I CC'ed Linus who has a strong here, jejb since he's the one that told me several time there's a number of literate dwarfs already in the kernel and Jan because I think it was him that tried last on x86.
Quoted 4 lines · Peter Zijlstra
So I CC'ed Linus who has a strong here, jejb since he's the one that told me several time there's a number of literate dwarfs already in the kernel and Jan because I think it was him that tried last on x86.
I never *ever* want to see this code ever again.
Sorry, but last time was too f*cking painful. The whole (and *only*) point of unwinders is to make debugging easy when a bug occurs. But the f*cking dwarf unwinder had bugs itself, or our dwarf information had bugs, and in either case it actually turned several "trivial" bugs into a total undebuggable hell.
It was made doubly painful by the developers involved then several times ignoring the problem, and claiming the code was bug-free when it clearly wasn't, or trying to claim that the problem was that we set up some random dwarf information wrong, when THAT GOES WITHOUT SAYING (since dwarf is a complex mess that never gets any actual testing except when things go wrong - at which point the code had better work regardless of whether the dwarf info was correct or not).
So no. An unwinder that is several hundred lines long is simply not even *remotely* interesting to me.
If you can mathematically prove that the unwinder is correct - even in the presence of bogus and actively incorrect unwinding information - and never ever follows a bad pointer, I'll reconsider.
In the absence of that, just follow the damn chain on the stack *without* the "smarts" of an inevitably buggy piece of crap.
Linus
Em Fri, Feb 10, 2012 at 10:59:51AM -0800, Linus Torvalds escreveu:
Quoted 25 lines · Peter Zijlstra
Quoted 4 lines
So I CC'ed Linus who has a strong here, jejb since he's the one that told me several time there's a number of literate dwarfs already in the kernel and Jan because I think it was him that tried last on x86.
I never *ever* want to see this code ever again.
Sorry, but last time was too f*cking painful. The whole (and *only*) point of unwinders is to make debugging easy when a bug occurs. But the f*cking dwarf unwinder had bugs itself, or our dwarf information had bugs, and in either case it actually turned several "trivial" bugs into a total undebuggable hell.
It was made doubly painful by the developers involved then several times ignoring the problem, and claiming the code was bug-free when it clearly wasn't, or trying to claim that the problem was that we set up some random dwarf information wrong, when THAT GOES WITHOUT SAYING (since dwarf is a complex mess that never gets any actual testing except when things go wrong - at which point the code had better work regardless of whether the dwarf info was correct or not).
So no. An unwinder that is several hundred lines long is simply not even *remotely* interesting to me.
Quoted 6 lines
If you can mathematically prove that the unwinder is correct - even in the presence of bogus and actively incorrect unwinding information - and never ever follows a bad pointer, I'll reconsider.
In the absence of that, just follow the damn chain on the stack *without* the "smarts" of an inevitably buggy piece of crap.
"Vote for --fno-omit-frame-pointer! One register is a cheap price to pay for not going insane!"
/me goes back to non political things.
- Arnaldo
Quoted 5 lines · Arnaldo Carvalho de Melo
"Vote for --fno-omit-frame-pointer! One register is a cheap price to pay for not going insane!"
/me goes back to non political things.
Even with -fomit-frame-pointer (which seems to be a big deal on Atom in particular), the call frames really don't look that horrible even when we guess. And seeing the occasional stale pointer can often give hints about what the thing was doing before, so it's not even horrible.
The biggest problem actually seems to often be some gcc versions that allocate a *lot* of stack space for some functions and then never really use it. That ends up then letting *tons* of really old stale code pointers "shine through".
Sometimes it's our code that just has horrible stack usage with crazy worst-case allocations or something. We've fixed a few of them, it seems to be getting better.
Linus
Em Fri, Feb 10, 2012 at 11:32:42AM -0800, Linus Torvalds escreveu:
Quoted 8 lines
On Fri, Feb 10, 2012 at 11:27 AM, Arnaldo Carvalho de Melo
Quoted 4 lines
"Vote for --fno-omit-frame-pointer! One register is a cheap price to pay for not going insane!"
/me goes back to non political things.
Even with -fomit-frame-pointer (which seems to be a big deal on Atom in particular)
Nah, on such register starved arches we just do what we do today: try to figure out bogus addresses and bail out.
- Arnaldo
Em Fri, Feb 10, 2012 at 05:39:32PM -0200, Arnaldo Carvalho de Melo escreveu:
Quoted 5 lines
Em Fri, Feb 10, 2012 at 11:32:42AM -0800, Linus Torvalds escreveu:
Quoted 3 lines
On Fri, Feb 10, 2012 at 11:27 AM, Arnaldo Carvalho de Melo
Quoted 2 lines
"Vote for --fno-omit-frame-pointer! One register is a cheap price to pay for not going insane!"
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
/me goes back to non political things.
Even with -fomit-frame-pointer (which seems to be a big deal on Atom in particular)
Nah, on such register starved arches we just do what we do today: try to figure out bogus addresses and bail out.
And the "interesting" thing is that at least on:
[acme@aninha linux]$ cat /etc/fedora-release
Fedora release 14 (Laughlin)
[acme@aninha linux]$ uname -p
i686
I have -fno-omit-frame-pointer and get nice callchains, but if I try it on fedora x86_64... b00m. Go figure.
- Arnaldo
Quoted 30 lines · Arnaldo Carvalho de Melo
Em Fri, Feb 10, 2012 at 10:59:51AM -0800, Linus Torvalds escreveu:
Quoted 29 lines · Peter Zijlstra
Quoted 4 lines
So I CC'ed Linus who has a strong here, jejb since he's the one that told me several time there's a number of literate dwarfs already in the kernel and Jan because I think it was him that tried last on x86.
I never *ever* want to see this code ever again.
Sorry, but last time was too f*cking painful. The whole (and *only*) point of unwinders is to make debugging easy when a bug occurs. But the f*cking dwarf unwinder had bugs itself, or our dwarf information had bugs, and in either case it actually turned several "trivial" bugs into a total undebuggable hell.
It was made doubly painful by the developers involved then several times ignoring the problem, and claiming the code was bug-free when it clearly wasn't, or trying to claim that the problem was that we set up some random dwarf information wrong, when THAT GOES WITHOUT SAYING (since dwarf is a complex mess that never gets any actual testing except when things go wrong - at which point the code had better work regardless of whether the dwarf info was correct or not).
So no. An unwinder that is several hundred lines long is simply not even *remotely* interesting to me.
If you can mathematically prove that the unwinder is correct - even in the presence of bogus and actively incorrect unwinding information - and never ever follows a bad pointer, I'll reconsider.
Quoted 7 lines
Quoted 2 lines
In the absence of that, just follow the damn chain on the stack *without* the "smarts" of an inevitably buggy piece of crap.
"Vote for --fno-omit-frame-pointer! One register is a cheap price to pay for not going insane!"
/me goes back to non political things.
Well, instead of dropping it we could try to meet Linus's challenge, at least to a fair degree.
Also lets fundamentally treat GCC provided data as untrusted, hostile data and lets put lockdep-alike redundancy and resilence around it.
As a first step lets try input randomization unit tests. A lot of the broken unwind code was really just sloppy about boundary conditions.
I had a quick peek and I don't think it's constructed in a resilent enough form right now. For example there's no clear separation and checking of what comes from GCC and what not.
It *can* be done: lockdep is not hundreds but thousands of lines of highly complex code (with non-trivial algorithms such as graph walks), and still it has a very good track record - so it's possible.
Once that is done I'd like to try it myself in practice, without offering it as a pull to Linus. I see a *lot* of weird oopses all day in and out, often in impossible contexts, and the old dwarf unwinder was crap.
I'd also love to see perf callchains work on all kernels and extend into user-space as well, if that's possible in a sane fashion. 90% of the interesting apps out there are build with framepointers off, and the context of overhead is often rather obscure. Looking at good callchains is a good learning experience all around.
So it's not *entirely* crazy IMO, lets iterate this please. Jiri, are you still interested in it?
Thanks,
Ingo
Quoted 44 lines · Ingo Molnar
Quoted 30 lines · Arnaldo Carvalho de Melo
Em Fri, Feb 10, 2012 at 10:59:51AM -0800, Linus Torvalds escreveu:
Quoted 29 lines · Peter Zijlstra
Quoted 4 lines
So I CC'ed Linus who has a strong here, jejb since he's the one that told me several time there's a number of literate dwarfs already in the kernel and Jan because I think it was him that tried last on x86.
I never *ever* want to see this code ever again.
Sorry, but last time was too f*cking painful. The whole (and *only*) point of unwinders is to make debugging easy when a bug occurs. But the f*cking dwarf unwinder had bugs itself, or our dwarf information had bugs, and in either case it actually turned several "trivial" bugs into a total undebuggable hell.
It was made doubly painful by the developers involved then several times ignoring the problem, and claiming the code was bug-free when it clearly wasn't, or trying to claim that the problem was that we set up some random dwarf information wrong, when THAT GOES WITHOUT SAYING (since dwarf is a complex mess that never gets any actual testing except when things go wrong - at which point the code had better work regardless of whether the dwarf info was correct or not).
So no. An unwinder that is several hundred lines long is simply not even *remotely* interesting to me.
If you can mathematically prove that the unwinder is correct - even in the presence of bogus and actively incorrect unwinding information - and never ever follows a bad pointer, I'll reconsider.
Quoted 7 lines
Quoted 2 lines
In the absence of that, just follow the damn chain on the stack *without* the "smarts" of an inevitably buggy piece of crap.
"Vote for --fno-omit-frame-pointer! One register is a cheap price to pay for not going insane!"
/me goes back to non political things.
Well, instead of dropping it we could try to meet Linus's challenge, at least to a fair degree.
Quoted 7 lines
Also lets fundamentally treat GCC provided data as untrusted, hostile data and lets put lockdep-alike redundancy and resilence around it.
As a first step lets try input randomization unit tests. A lot of the broken unwind code was really just sloppy about boundary conditions.
right, looks like crucial part.. :)
Quoted 4 lines
I had a quick peek and I don't think it's constructed in a resilent enough form right now. For example there's no clear separation and checking of what comes from GCC and what not.
yes, there's nothing like this in now,
I'll see what can be done about that..
Quoted 18 lines
It *can* be done: lockdep is not hundreds but thousands of lines of highly complex code (with non-trivial algorithms such as graph walks), and still it has a very good track record - so it's possible.
Once that is done I'd like to try it myself in practice, without offering it as a pull to Linus. I see a *lot* of weird oopses all day in and out, often in impossible contexts, and the old dwarf unwinder was crap.
I'd also love to see perf callchains work on all kernels and extend into user-space as well, if that's possible in a sane fashion. 90% of the interesting apps out there are build with framepointers off, and the context of overhead is often rather obscure. Looking at good callchains is a good learning experience all around.
Quoted 2 lines
So it's not *entirely* crazy IMO, lets iterate this please. Jiri, are you still interested in it?
yep, looks interesting.. not sure about the mathematical proof though ;)
jirka
Quoted 2 lines · Jiri Olsa
yep, looks interesting.. not sure about the mathematical proof though ;)
Well, I can already say what the old code did horribly horribly wrong:
- *all* stack accesses need to go through a validation function, they
can never *ever* just try to access the stack.
The validation function really needs to really check the full
range of the stack area, not something random.
- all dwarf information accesses need to similarly validate the
access, and accept that sometimes the dwarf info is simply missing or actively wrong.
Basically, by the time a fault happens, EVERY SINGLE PIECE OF DATA YOU ACCESS needs to be thought of as untrusted. Because it really is. We took some kind of kernel fault, there clearly is a bug somewhere. The old unwinder thing just had the approach that "my code is perfect, and I can trust the unwind data", which was fundamentally incorrect and wrong.
The code needs to be really *obviously* correct and really crazy anal at the same time. And it's *hard* to write careful code in a way that makes it obvious too.
It's triply hard to do it when you start off from code from user space written by people who simply don't care and don't even have the same kinds of issues we do in the kernel.
The code we have now isn't exactly pretty either, but it has two *huge* advantages:
- it's tested in real life and thus largely trusted
- it's pretty simple, and it does have a lot of stack validation.
Linus
Quoted 9 lines · Linus Torvalds
Quoted 2 lines · Jiri Olsa
yep, looks interesting.. not sure about the mathematical proof though ;)
Well, I can already say what the old code did horribly horribly wrong:
- *all* stack accesses need to go through a validation function, they
can never *ever* just try to access the stack.
Quoted 6 lines
The validation function really needs to really check the full
range of the stack area, not something random.- all dwarf information accesses need to similarly validate the
access, and accept that sometimes the dwarf info is simply missing or actively wrong.
In addition it should all go through something like copy_from_user_inatomic(), ie, all access to that stuff should have built-in fault recovery without triggering high level page faults, stack expansion etc...
Cheers,
Ben.
Quoted 6 lines · Jiri Olsa
Quoted 3 lines
I had a quick peek and I don't think it's constructed in a resilent enough form right now. For example there's no clear separation and checking of what comes from GCC and what not.
yes, there's nothing like this in now, I'll see what can be done about that..
Another resilience feature of lockdep is the 'one strike and you are out!' aspect: the first error or unexpected condition we detect results in the very quick shutting down of all things lockdep. It prints exactly one error message, then it deactivates and never ever runs again.
The equivalent of this in the scope of your dwarf unwind kernel feature would be to fall back to the regular guess and framepointer based stack backtrace method the moment any error is detected.
Maybe print a single line that indicates that the fallback has been activated, and after that the dwarf code should never run again. Make sure nobody comes away a "oh, no, the dwarf unwind messed up things!' impression, even if it *does* run into some trouble (such as unexpected debuginfo generated by GCC - or debuginfo *corrupted* by a kernel bug [a very real possibility]).
What is totally unacceptable is for the dwarf code to *cause* crashes, or to destroy stack trace information.
Quoted 2 lines
yep, looks interesting.. not sure about the mathematical proof though ;)
In the physical sense even mathematics is always and unavoidably probability based (or brain and all our senses are probabilistic), so you can probably replace 'mathematical proof' with 'very robust design and a very, very good track record', before bothering Linus with it next time around ;-)
And we might as well conclude "it's simply not worth it", at some point down he road. I *do* think that it's worth it though, and I do think it can be designed and implemented robustly, so I'd be willing to try out these patches in -tip for a kernel release or two, without pushing it to Linus.
Thanks,
Ingo
Quoted 21 lines · Ingo Molnar
Quoted 6 lines · Jiri Olsa
Quoted 3 lines
I had a quick peek and I don't think it's constructed in a resilent enough form right now. For example there's no clear separation and checking of what comes from GCC and what not.
yes, there's nothing like this in now, I'll see what can be done about that..
Another resilience feature of lockdep is the 'one strike and you are out!' aspect: the first error or unexpected condition we detect results in the very quick shutting down of all things lockdep. It prints exactly one error message, then it deactivates and never ever runs again.
The equivalent of this in the scope of your dwarf unwind kernel feature would be to fall back to the regular guess and framepointer based stack backtrace method the moment any error is detected.
Quoted 7 lines
Maybe print a single line that indicates that the fallback has been activated, and after that the dwarf code should never run again. Make sure nobody comes away a "oh, no, the dwarf unwind messed up things!' impression, even if it *does* run into some trouble (such as unexpected debuginfo generated by GCC - or debuginfo *corrupted* by a kernel bug [a very real possibility]).
right, such fallback seems necessary
Quoted 4 lines
What is totally unacceptable is for the dwarf code to *cause* crashes, or to destroy stack trace information.
Quoted 8 lines
Quoted 2 lines
yep, looks interesting.. not sure about the mathematical proof though ;)
In the physical sense even mathematics is always and unavoidably probability based (or brain and all our senses are probabilistic), so you can probably replace 'mathematical proof' with 'very robust design and a very, very good track record', before bothering Linus with it next time around ;-)
I wasn't aware of such kernel unwind history ;) was just curious, if anyone is interested, before spending more time on that..
Quoted 6 lines
And we might as well conclude "it's simply not worth it", at some point down he road. I *do* think that it's worth it though, and I do think it can be designed and implemented robustly, so I'd be willing to try out these patches in -tip for a kernel release or two, without pushing it to Linus.
thanks a lot for your ideas, I'll start working on that
jirka
Quoted 66 lines · Ingo Molnar
Quoted 30 lines · Arnaldo Carvalho de Melo
Em Fri, Feb 10, 2012 at 10:59:51AM -0800, Linus Torvalds escreveu:
Quoted 29 lines · Peter Zijlstra
Quoted 4 lines
So I CC'ed Linus who has a strong here, jejb since he's the one that told me several time there's a number of literate dwarfs already in the kernel and Jan because I think it was him that tried last on x86.
I never *ever* want to see this code ever again.
Sorry, but last time was too f*cking painful. The whole (and *only*) point of unwinders is to make debugging easy when a bug occurs. But the f*cking dwarf unwinder had bugs itself, or our dwarf information had bugs, and in either case it actually turned several "trivial" bugs into a total undebuggable hell.
It was made doubly painful by the developers involved then several times ignoring the problem, and claiming the code was bug-free when it clearly wasn't, or trying to claim that the problem was that we set up some random dwarf information wrong, when THAT GOES WITHOUT SAYING (since dwarf is a complex mess that never gets any actual testing except when things go wrong - at which point the code had better work regardless of whether the dwarf info was correct or not).
So no. An unwinder that is several hundred lines long is simply not even *remotely* interesting to me.
If you can mathematically prove that the unwinder is correct - even in the presence of bogus and actively incorrect unwinding information - and never ever follows a bad pointer, I'll reconsider.
Quoted 7 lines
Quoted 2 lines
In the absence of that, just follow the damn chain on the stack *without* the "smarts" of an inevitably buggy piece of crap.
"Vote for --fno-omit-frame-pointer! One register is a cheap price to pay for not going insane!"
/me goes back to non political things.
Well, instead of dropping it we could try to meet Linus's challenge, at least to a fair degree.
Also lets fundamentally treat GCC provided data as untrusted, hostile data and lets put lockdep-alike redundancy and resilence around it.
As a first step lets try input randomization unit tests. A lot of the broken unwind code was really just sloppy about boundary conditions.
I had a quick peek and I don't think it's constructed in a resilent enough form right now. For example there's no clear separation and checking of what comes from GCC and what not.
It *can* be done: lockdep is not hundreds but thousands of lines of highly complex code (with non-trivial algorithms such as graph walks), and still it has a very good track record - so it's possible.
Once that is done I'd like to try it myself in practice, without offering it as a pull to Linus. I see a *lot* of weird oopses all day in and out, often in impossible contexts, and the old dwarf unwinder was crap.
Quoted 6 lines
I'd also love to see perf callchains work on all kernels and extend into user-space as well, if that's possible in a sane fashion. 90% of the interesting apps out there are build with framepointers off, and the context of overhead is often rather obscure. Looking at good callchains is a good learning experience all around.
My thinking is we can have two kinds of unwinding co-existing in the kernel:
- the heavy one that we use today that walks the entire stack for addresses, which validates addresses with frame pointer but which report also those that are considered unreliable. This one can stay the debug unwinder, used in warnings, crashes, etc... as it's proven solid and it's simple.
- a dwarf based one for tools like perf and ftrace that don't require the same degree of ultimate robustness. Besides, perf is a good usecase to debug an unwinder because it can take snapshots of various scenario of context stacking.
In fact, today in x86 we already have two distinct unwinder for debugging (print_context_stack() does the full stack walk + fp validation) and for perf (print_context_stack_bp() does only walk fp). The second is less robust as it relies on fp to be always reliable and we miss entries considered as unreliable but these can be useful.
Plugging a new unwinder for perf/ftrace should be fine as long as we really control what we dereference. But this doesn't need to be proven mathematically if it's only use by our profiling/tracing tools and not for real debuggging.
Now for userspace dwarf unwinding in perf, I guess we shouldn't do that from the kernel. Dumping regs and chunks of stacks on the record stream and let userspace play with that post mortem is probably wiser.