Skip to content

Commit e2ac83d

Browse files
jpoimboerostedt
authored andcommitted
x86/ftrace: Fix ORC unwinding from ftrace handlers
Steven Rostedt discovered that the ftrace stack tracer is broken when it's used with the ORC unwinder. The problem is that objtool is instructed by the Makefile to ignore the ftrace_64.S code, so it doesn't generate any ORC data for it. Fix it by making the asm code objtool-friendly: - Objtool doesn't like the fact that save_mcount_regs pushes RBP at the beginning, but it's never restored (directly, at least). So just skip the original RBP push, which is only needed for frame pointers anyway. - Annotate some functions as normal callable functions with ENTRY/ENDPROC. - Add an empty unwind hint to return_to_handler(). The return address isn't on the stack, so there's nothing ORC can do there. It will just punt in the unlikely case it tries to unwind from that code. With all that fixed, remove the OBJECT_FILES_NON_STANDARD Makefile annotation so objtool can read the file. Link: http://lkml.kernel.org/r/20180123040746.ih4ep3tk4pbjvg7c@treble Reported-by: Steven Rostedt <[email protected]> Signed-off-by: Josh Poimboeuf <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 0c5b9b5 commit e2ac83d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

arch/x86/kernel/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ KASAN_SANITIZE_stacktrace.o := n
2929
KASAN_SANITIZE_paravirt.o := n
3030

3131
OBJECT_FILES_NON_STANDARD_relocate_kernel_$(BITS).o := y
32-
OBJECT_FILES_NON_STANDARD_ftrace_$(BITS).o := y
3332
OBJECT_FILES_NON_STANDARD_test_nx.o := y
3433
OBJECT_FILES_NON_STANDARD_paravirt_patch_$(BITS).o := y
3534

35+
ifdef CONFIG_FRAME_POINTER
36+
OBJECT_FILES_NON_STANDARD_ftrace_$(BITS).o := y
37+
endif
38+
3639
# If instrumentation of this dir is enabled, boot hangs during first second.
3740
# Probably could be more selective here, but note that files related to irqs,
3841
# boot, dumpstack/stacktrace, etc are either non-interesting or can lead to

arch/x86/kernel/ftrace_64.S

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <asm/ftrace.h>
99
#include <asm/export.h>
1010
#include <asm/nospec-branch.h>
11+
#include <asm/unwind_hints.h>
1112

1213
.code64
1314
.section .entry.text, "ax"
@@ -20,7 +21,6 @@ EXPORT_SYMBOL(__fentry__)
2021
EXPORT_SYMBOL(mcount)
2122
#endif
2223

23-
/* All cases save the original rbp (8 bytes) */
2424
#ifdef CONFIG_FRAME_POINTER
2525
# ifdef CC_USING_FENTRY
2626
/* Save parent and function stack frames (rip and rbp) */
@@ -31,7 +31,7 @@ EXPORT_SYMBOL(mcount)
3131
# endif
3232
#else
3333
/* No need to save a stack frame */
34-
# define MCOUNT_FRAME_SIZE 8
34+
# define MCOUNT_FRAME_SIZE 0
3535
#endif /* CONFIG_FRAME_POINTER */
3636

3737
/* Size of stack used to save mcount regs in save_mcount_regs */
@@ -64,10 +64,10 @@ EXPORT_SYMBOL(mcount)
6464
*/
6565
.macro save_mcount_regs added=0
6666

67-
/* Always save the original rbp */
67+
#ifdef CONFIG_FRAME_POINTER
68+
/* Save the original rbp */
6869
pushq %rbp
6970

70-
#ifdef CONFIG_FRAME_POINTER
7171
/*
7272
* Stack traces will stop at the ftrace trampoline if the frame pointer
7373
* is not set up properly. If fentry is used, we need to save a frame
@@ -105,7 +105,11 @@ EXPORT_SYMBOL(mcount)
105105
* Save the original RBP. Even though the mcount ABI does not
106106
* require this, it helps out callers.
107107
*/
108+
#ifdef CONFIG_FRAME_POINTER
108109
movq MCOUNT_REG_SIZE-8(%rsp), %rdx
110+
#else
111+
movq %rbp, %rdx
112+
#endif
109113
movq %rdx, RBP(%rsp)
110114

111115
/* Copy the parent address into %rsi (second parameter) */
@@ -148,7 +152,7 @@ EXPORT_SYMBOL(mcount)
148152

149153
ENTRY(function_hook)
150154
retq
151-
END(function_hook)
155+
ENDPROC(function_hook)
152156

153157
ENTRY(ftrace_caller)
154158
/* save_mcount_regs fills in first two parameters */
@@ -184,7 +188,7 @@ GLOBAL(ftrace_graph_call)
184188
/* This is weak to keep gas from relaxing the jumps */
185189
WEAK(ftrace_stub)
186190
retq
187-
END(ftrace_caller)
191+
ENDPROC(ftrace_caller)
188192

189193
ENTRY(ftrace_regs_caller)
190194
/* Save the current flags before any operations that can change them */
@@ -255,7 +259,7 @@ GLOBAL(ftrace_regs_caller_end)
255259

256260
jmp ftrace_epilogue
257261

258-
END(ftrace_regs_caller)
262+
ENDPROC(ftrace_regs_caller)
259263

260264

261265
#else /* ! CONFIG_DYNAMIC_FTRACE */
@@ -313,9 +317,10 @@ ENTRY(ftrace_graph_caller)
313317
restore_mcount_regs
314318

315319
retq
316-
END(ftrace_graph_caller)
320+
ENDPROC(ftrace_graph_caller)
317321

318-
GLOBAL(return_to_handler)
322+
ENTRY(return_to_handler)
323+
UNWIND_HINT_EMPTY
319324
subq $24, %rsp
320325

321326
/* Save the return values */
@@ -330,4 +335,5 @@ GLOBAL(return_to_handler)
330335
movq (%rsp), %rax
331336
addq $24, %rsp
332337
JMP_NOSPEC %rdi
338+
END(return_to_handler)
333339
#endif

0 commit comments

Comments
 (0)