Skip to content

x86/dumpstack: rewrite x86 stack dump code

Josh Poimboeuf lkml 4 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest57 patches 22 repliesIn review
  2. v351 patches 47 repliesSuperseded
  3. v244 patches 21 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v119 patches71 repliesSuperseded

Changes in v4 from the cover letter

- complete rewrite of arch_within_stack_frames() for hardened usercopy
- handle empty stacks better:
- change in_*_stack() functions to consider the end to be part of the stack
- add loop in update_stack_state() to handle empty stacks more gracefully
- prevent false positive warnings when unwinding interrupts in entry code
- fix misplaced parentheses bug in __unwind_start()
- move 32-bit ret_from_fork change to a separate commit: "fix the end of the stack for newly forked tasks"
- add infinite loop after call to initial_code
- print orig_ax in __show_regs()
- fix duplicate RIP address display in __show_regs()
- rename "next_sp" to "next_frame" in unwind_next_frame() and "first_sp" to "first_frame" in unwind_start() to improve readability
- improve a few patch header descriptions

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/57x86/dumpstack: remove show_trace()arch/x86/include/asm/kdebug.h arch/x86/kernel/dumpstack.c+0 -80 replies
2/57x86/asm/head: remove unused init_rsp variable externarch/x86/include/asm/realmode.h+0 -11 reply
3/57x86/asm/head: rename 'stack_start' -> 'initial_stack'arch/x86/include/asm/realmode.h arch/x86/include/asm/smp.h arch/x86/kernel/acpi/sleep.c +3 more+12 -150 replies
4/57x86/asm/head: use a common function for starting CPUsarch/x86/kernel/head_64.S+11 -110 replies
5/57x86/dumpstack: make printk_stack_address() more generally usefularch/x86/kernel/dumpstack.c+3 -30 replies
6/57x86/dumpstack: add IRQ_USABLE_STACK_SIZE definearch/x86/include/asm/page_64_types.h arch/x86/kernel/cpu/common.c arch/x86/kernel/dumpstack_64.c +1 more+14 -140 replies
7/57x86/dumpstack: remove extra brackets around "<EOE>"arch/x86/kernel/dumpstack_64.c+1 -10 replies
8/57x86/dumpstack: fix irq stack bounds calculation in show_stack_log_lvl()arch/x86/kernel/dumpstack_64.c+2 -20 replies
9/57x86/dumpstack: fix x86_32 kernel_stack_pointer() previous stack accessarch/x86/kernel/ptrace.c+2 -20 replies
10/57x86/dumpstack: add get_stack_pointer() and get_frame_pointer()arch/x86/include/asm/stacktrace.h arch/x86/kernel/dumpstack.c arch/x86/kernel/dumpstack_32.c +1 more+33 -680 replies
11/57x86/dumpstack: remove unnecessary stack pointer argumentsarch/x86/kernel/dumpstack.c arch/x86/kernel/dumpstack_32.c arch/x86/kernel/dumpstack_64.c +1 more+4 -90 replies
12/57x86: move _stext marker to before head codearch/x86/kernel/vmlinux.lds.S+1 -10 replies
13/57x86/head: remove useless zeroed wordarch/x86/kernel/head_64.S+0 -10 replies
14/57x86/head: put real return address on idle task stackarch/x86/kernel/head_64.S+3 -20 replies
15/57x86/head: fix the end of the stack for idle tasksarch/x86/kernel/head_32.S arch/x86/kernel/head_64.S+15 -90 replies
16/57x86/entry/32: fix the end of the stack for newly forked tasksarch/x86/entry/entry_32.S+20 -40 replies
17/57x86/head/32: fix the end of the stack for idle tasksarch/x86/kernel/head_32.S+5 -30 replies
18/57x86/smp: fix initial idle stack location on 32-bitarch/x86/kernel/smpboot.c+1 -30 replies
19/57x86/entry/head/32: use local labelsarch/x86/entry/entry_32.S arch/x86/kernel/head_32.S+45 -440 replies
20/57x86/entry/32: rename 'error_code' to 'common_exception'arch/x86/entry/entry_32.S+23 -200 replies
21/57perf/x86: check perf_callchain_store() errorarch/x86/events/core.c+2 -10 replies
22/57oprofile/x86: add regs->ip to oprofile tracearch/x86/oprofile/backtrace.c+8 -20 replies
23/57proc: fix return address printk conversion specifer in /proc/<pid>/stackfs/proc/base.c+1 -10 replies
24/57ftrace: remove CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST from configarch/arm64/kernel/entry-ftrace.S arch/blackfin/kernel/ftrace-entry.S arch/sparc/Kconfig +5 more+9 -110 replies
25/57ftrace: only allocate the ret_stack 'fp' field when neededinclude/linux/ftrace.h kernel/trace/trace_functions_graph.c+4 -00 replies
26/57ftrace: add return address pointer to ftrace_ret_stackDocumentation/trace/ftrace-design.txt arch/arm/kernel/ftrace.c arch/arm64/kernel/ftrace.c +12 more+34 -150 replies
27/57ftrace: add ftrace_graph_ret_addr() stack unwinding helpersinclude/linux/ftrace.h kernel/trace/trace_functions_graph.c+68 -00 replies
28/57x86/dumpstack/ftrace: convert dump_trace() callbacks to use ftrace_graph_ret_addr()arch/x86/kernel/dumpstack.c+22 -430 replies
29/57ftrace/x86: implement HAVE_FUNCTION_GRAPH_RET_ADDR_PTRarch/x86/include/asm/ftrace.h arch/x86/kernel/ftrace.c+3 -10 replies
30/57x86/dumpstack/ftrace: mark function graph handler function as unreliablearch/x86/kernel/dumpstack.c+16 -80 replies
31/57x86/dumpstack/ftrace: don't print unreliable addresses in print_context_stack_bp()arch/x86/kernel/dumpstack.c+0 -20 replies
32/57x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace()arch/x86/kernel/dumpstack_32.c arch/x86/kernel/dumpstack_64.c+15 -250 replies
33/57x86/dumpstack: simplify in_exception_stack()arch/x86/kernel/dumpstack_64.c+26 -630 replies
34/57x86/dumpstack: add get_stack_info() interfacearch/x86/events/core.c arch/x86/include/asm/stacktrace.h arch/x86/kernel/dumpstack.c +4 more+232 -1280 replies
35/57x86/dumpstack: add recursion checking for all stacksarch/x86/kernel/dumpstack_32.c arch/x86/kernel/dumpstack_64.c+38 -180 replies
36/57x86/unwind: add new unwind interface and implementationsarch/x86/include/asm/unwind.h arch/x86/kernel/Makefile arch/x86/kernel/unwind_frame.c +1 more+235 -00 replies
37/57perf/x86: convert perf_callchain_kernel() to use the new unwinderarch/x86/events/core.c+10 -230 replies
38/57x86/stacktrace: convert save_stack_trace_*() to use the new unwinderarch/x86/kernel/stacktrace.c+29 -450 replies
39/57oprofile/x86: convert x86_backtrace() to use the new unwinderarch/x86/oprofile/backtrace.c+17 -220 replies
40/57x86/dumpstack: convert show_trace_log_lvl() to use the new unwinderarch/x86/include/asm/stacktrace.h arch/x86/kernel/dumpstack.c arch/x86/kernel/dumpstack_32.c +1 more+111 -450 replies
41/57x86/dumpstack: remove dump_trace() and related callbacksarch/x86/include/asm/stacktrace.h arch/x86/kernel/dumpstack.c arch/x86/kernel/dumpstack_32.c +1 more+0 -2260 replies
42/57x86/entry/unwind: create stack frames for saved interrupt registersarch/x86/entry/calling.h arch/x86/entry/entry_32.S arch/x86/entry/entry_64.S +2 more+146 -170 replies
43/57x86/unwind: create stack frames for saved syscall registersarch/x86/kernel/unwind_frame.c+35 -00 replies
44/57x86/dumpstack: print stack identifier on its own linearch/x86/kernel/dumpstack.c+4 -40 replies
45/57x86/dumpstack: print any pt_regs found on the stackarch/x86/kernel/dumpstack.c+15 -10 replies
46/57x86/dumpstack: fix duplicate RIP address display in __show_regs()arch/x86/kernel/process_64.c+2 -20 replies
47/57x86/dumpstack: print orig_ax in __show_regs()arch/x86/kernel/process_64.c+6 -10 replies
48/57x86: remove 64-byte gap at end of irq stackarch/x86/include/asm/page_64_types.h arch/x86/kernel/cpu/common.c arch/x86/kernel/dumpstack_64.c +1 more+4 -70 replies
49/57x86/unwind: warn on kernel stack corruptionarch/x86/kernel/unwind_frame.c+22 -20 replies
50/57x86/unwind: warn on bad stack return addressarch/x86/kernel/unwind_frame.c+9 -10 replies
51/57x86/unwind: warn if stack grows uparch/x86/kernel/unwind_frame.c+24 -20 replies
52/57x86/dumpstack: warn on stack recursionarch/x86/kernel/dumpstack_32.c arch/x86/kernel/dumpstack_64.c+8 -20 replies
53/57x86/mm: move arch_within_stack_frames() to usercopy.carch/Kconfig arch/x86/include/asm/thread_info.h arch/x86/lib/usercopy.c+54 -390 replies
54/57x86/mm: convert arch_within_stack_frames() to use the new unwinderarch/x86/lib/usercopy.c+28 -1642 replies
55/57x86/mm: simplify starting frame logic for hardened usercopyarch/x86/include/asm/thread_info.h arch/x86/lib/usercopy.c include/linux/thread_info.h +1 more+17 -130 replies
56/57x86/mm: removed unused arch_within_stack_frames() argumentsarch/x86/include/asm/thread_info.h arch/x86/lib/usercopy.c include/linux/thread_info.h +1 more+5 -140 replies
57/57mm: re-enable gcc frame address warningmm/Makefile+0 -30 replies

Reviews trailers per patch

6 of 57 patches have a Reviewed-by or Acked-by.

PatchAndy Lutomirski
1/57R-b
2/57
3/57
4/57
5/57
6/57
7/57
8/57
9/57
10/57R-b
11/57R-b
12/57
13/57
14/57R-b
15/57
16/57
17/57
18/57R-b
19/57
20/57
21/57
22/57
23/57
24/57
25/57
26/57
27/57
28/57
29/57
30/57
31/57
32/57
33/57
34/57
35/57
36/57
37/57
38/57
39/57
40/57
41/57
42/57A-b
43/57
44/57
45/57
46/57
47/57
48/57
49/57
50/57
51/57
52/57
53/57
54/57
55/57
56/57
57/57

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v4 thread

Cover letter [PATCH v4 00/57] x86/dumpstack: rewrite x86 stack dump code

Mostly minor changes this time. See below for the full list of changes.

A git branch is available at:

https://github.com/jpoimboe/linux unwind-v4

Based on tip/master.

v4:
- complete rewrite of arch_within_stack_frames() for hardened usercopy
- handle empty stacks better:
- change in_*_stack() functions to consider the end to be part of the stack
- add loop in update_stack_state() to handle empty stacks more gracefully
- prevent false positive warnings when unwinding interrupts in entry code
- fix misplaced parentheses bug in __unwind_start()
- move 32-bit ret_from_fork change to a separate commit: "fix the end of the stack for newly forked tasks"
- add infinite loop after call to initial_code
- print orig_ax in __show_regs()
- fix duplicate RIP address display in __show_regs()
- rename "next_sp" to "next_frame" in unwind_next_frame() and "first_sp" to "first_frame" in unwind_start() to improve readability
- improve a few patch header descriptions

v3:
- partial unwinder rewrite: each pt_regs gets its own frame
- add frame pointer encoding support for 32-bit
- several 32-bit fixes and cleanups for issues found by the new warnings
- convert CONFIG_HARDENED_USERCOPY arch_within_stack_frames()
- fix bug in unwinder when skipping stack frames (and add a comment)
- warn on stack recursion
- put start_cpu() in its own function
- export symbols in unwind_guess.c

v2:
- split up several of the patches and reorder them with lower-risk patches first
- add a lot more comments
- remove the 64-byte gap at the end of the irq stack
- fix some existing ftrace function graph unwinding issues
- fix an existing bug in kernel_stack_pointer()
- clarify the origins of the stack_info "next stack" pointers
- do visit_mask checking in get_stack_info() instead of in_*_stack()
- add some new unwinder warnings
- remove uses of test_and_set_bit()
- dont print regs->ip twice
- remove unwind_state.sp
- have unwind_get_return_address() validate the return address
- change /proc/pid/stack to use %pB
- several minor cleanups and fixes

----

The x86 stack dump code is a bit of a mess. dump_trace() uses callbacks, and each user of it seems to have slightly different requirements, so there are several slightly different callbacks floating around.

Also there are some upcoming features which will require more changes to the stack dump code: reliable stack detection for live patching, hardened user copy, and the DWARF unwinder. Each of those features would at least need more callbacks and/or callback interfaces, resulting in a much bigger mess than what we have today.

Before doing all that, we should try to clean things up and replace dump_trace() with something cleaner and more flexible.

The new unwinder is a simple state machine which was heavily inspired by a suggestion from Andy Lutomirski:

https://lkml.kernel.org/r/CALCETrUbNTqaM2LRyXGRx=kVLRPeY5A3Pc6k4TtQxF320rUT=w@mail.gmail.com

It's also similar to the libunwind API:

http://www.nongnu.org/libunwind/man/libunwind(3).html

Some if its advantages:

- simplicity: no more callback sprawl and less code duplication.

- flexibility: allows the caller to stop and inspect the stack state at each step in the unwinding process.

- modularity: the unwinder code, console stack dump code, and stack metadata analysis code are all better separated so that changing one of them shouldn't have much of an impact on any of the others.

----

Josh Poimboeuf (57):
x86/dumpstack: remove show_trace()
x86/asm/head: remove unused init_rsp variable extern x86/asm/head: rename 'stack_start' -> 'initial_stack' x86/asm/head: use a common function for starting CPUs x86/dumpstack: make printk_stack_address() more generally useful x86/dumpstack: add IRQ_USABLE_STACK_SIZE define x86/dumpstack: remove extra brackets around "<EOE>" x86/dumpstack: fix irq stack bounds calculation in
show_stack_log_lvl()
x86/dumpstack: fix x86_32 kernel_stack_pointer() previous stack access x86/dumpstack: add get_stack_pointer() and get_frame_pointer() x86/dumpstack: remove unnecessary stack pointer arguments
x86: move _stext marker to before head code
x86/head: remove useless zeroed word
x86/head: put real return address on idle task stack x86/head: fix the end of the stack for idle tasks x86/entry/32: fix the end of the stack for newly forked tasks x86/head/32: fix the end of the stack for idle tasks x86/smp: fix initial idle stack location on 32-bit x86/entry/head/32: use local labels
x86/entry/32: rename 'error_code' to 'common_exception' perf/x86: check perf_callchain_store() error
oprofile/x86: add regs->ip to oprofile trace
proc: fix return address printk conversion specifer in

    /proc/<pid>/stack

ftrace: remove CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST from config
ftrace: only allocate the ret_stack 'fp' field when needed
ftrace: add return address pointer to ftrace_ret_stack
ftrace: add ftrace_graph_ret_addr() stack unwinding helpers x86/dumpstack/ftrace: convert dump_trace() callbacks to use
ftrace_graph_ret_addr()
ftrace/x86: implement HAVE_FUNCTION_GRAPH_RET_ADDR_PTR x86/dumpstack/ftrace: mark function graph handler function as
unreliable
x86/dumpstack/ftrace: don't print unreliable addresses in
print_context_stack_bp()
x86/dumpstack: allow preemption in show_stack_log_lvl() and
dump_trace()
x86/dumpstack: simplify in_exception_stack() x86/dumpstack: add get_stack_info() interface x86/dumpstack: add recursion checking for all stacks x86/unwind: add new unwind interface and implementations perf/x86: convert perf_callchain_kernel() to use the new unwinder x86/stacktrace: convert save_stack_trace_*() to use the new unwinder oprofile/x86: convert x86_backtrace() to use the new unwinder x86/dumpstack: convert show_trace_log_lvl() to use the new unwinder x86/dumpstack: remove dump_trace() and related callbacks x86/entry/unwind: create stack frames for saved interrupt registers x86/unwind: create stack frames for saved syscall registers x86/dumpstack: print stack identifier on its own line x86/dumpstack: print any pt_regs found on the stack x86/dumpstack: fix duplicate RIP address display in __show_regs() x86/dumpstack: print orig_ax in __show_regs()
x86: remove 64-byte gap at end of irq stack
x86/unwind: warn on kernel stack corruption
x86/unwind: warn on bad stack return address
x86/unwind: warn if stack grows up
x86/dumpstack: warn on stack recursion
x86/mm: move arch_within_stack_frames() to usercopy.c x86/mm: convert arch_within_stack_frames() to use the new unwinder x86/mm: simplify starting frame logic for hardened usercopy x86/mm: removed unused arch_within_stack_frames() arguments
mm: re-enable gcc frame address warning

 Documentation/trace/ftrace-design.txt |  11 ++
 arch/Kconfig                          |   4 +-
 arch/arm/kernel/ftrace.c              |   2 +-
 arch/arm64/kernel/entry-ftrace.S      |   2 +-
 arch/arm64/kernel/ftrace.c            |   2 +-
 arch/blackfin/kernel/ftrace-entry.S   |   4 +-
 arch/blackfin/kernel/ftrace.c         |   2 +-
 arch/microblaze/kernel/ftrace.c       |   2 +-
 arch/mips/kernel/ftrace.c             |   4 +-
 arch/parisc/kernel/ftrace.c           |   2 +-
 arch/powerpc/kernel/ftrace.c          |   3 +-
 arch/s390/kernel/ftrace.c             |   3 +-
 arch/sh/kernel/ftrace.c               |   2 +-
 arch/sparc/Kconfig                    |   1 -
 arch/sparc/include/asm/ftrace.h       |   4 +
 arch/sparc/kernel/ftrace.c            |   2 +-
 arch/tile/kernel/ftrace.c             |   2 +-
 arch/x86/Kconfig                      |   1 -
 arch/x86/entry/calling.h              |  21 +++
 arch/x86/entry/entry_32.S             | 158 +++++++++++------
 arch/x86/entry/entry_64.S             |  10 +-
 arch/x86/events/core.c                |  36 ++--
 arch/x86/include/asm/ftrace.h         |   3 +
 arch/x86/include/asm/kdebug.h         |   2 -
 arch/x86/include/asm/page_64_types.h  |  16 +-
 arch/x86/include/asm/realmode.h       |   2 +-
 arch/x86/include/asm/smp.h            |   3 -
 arch/x86/include/asm/stacktrace.h     | 116 ++++++------
 arch/x86/include/asm/thread_info.h    |  48 +----
 arch/x86/include/asm/unwind.h         | 104 +++++++++++
 arch/x86/kernel/Makefile              |   6 +
 arch/x86/kernel/acpi/sleep.c          |   2 +-
 arch/x86/kernel/cpu/common.c          |   2 +-
 arch/x86/kernel/dumpstack.c           | 272 +++++++++++++----------------
 arch/x86/kernel/dumpstack_32.c        | 141 ++++++++-------
 arch/x86/kernel/dumpstack_64.c        | 320 +++++++++++-----------------------
 arch/x86/kernel/ftrace.c              |   2 +-
 arch/x86/kernel/head_32.S             |  57 +++---
 arch/x86/kernel/head_64.S             |  50 +++---
 arch/x86/kernel/process_64.c          |  11 +-
 arch/x86/kernel/ptrace.c              |   4 +-
 arch/x86/kernel/setup_percpu.c        |   2 +-
 arch/x86/kernel/smpboot.c             |   6 +-
 arch/x86/kernel/stacktrace.c          |  74 +++-----
 arch/x86/kernel/unwind_frame.c        | 245 ++++++++++++++++++++++++++
 arch/x86/kernel/unwind_guess.c        |  43 +++++
 arch/x86/kernel/vmlinux.lds.S         |   2 +-
 arch/x86/lib/usercopy.c               |  49 ++++++
 arch/x86/oprofile/backtrace.c         |  49 +++---
 fs/proc/base.c                        |   2 +-
 include/linux/ftrace.h                |  17 +-
 include/linux/thread_info.h           |   3 +-
 kernel/trace/Kconfig                  |   5 -
 kernel/trace/trace_functions_graph.c  |  67 ++++++-
 mm/Makefile                           |   3 -
 mm/usercopy.c                         |  14 +-
 56 files changed, 1225 insertions(+), 795 deletions(-)
 create mode 100644 arch/x86/include/asm/unwind.h
 create mode 100644 arch/x86/kernel/unwind_frame.c
 create mode 100644 arch/x86/kernel/unwind_guess.c
Signature · 1 line
2.7.4