kprobes: Make KUnit and add stacktrace on kretprobe tests
Subsystems (6)
Masami Hiramatsu
maintainercover kprobes: Make KUnit and add stacktrace on kretprobe tests
Hi,
Here is the 2nd version of the series to change the kprobes selftest to KUnit and add testcases for stacktrace on kretprobes, which has been fixed recently on x86. The previous version is here;
https://lore.kernel.org/all/163369609308.636038.15295764725220907794.stgit@devnote2/
In this version, I fixed some typos and coding issues according to Will and Mark's comments. Thanks!
And I added 1 RFC patch, which will detect the unwinding error on arm64 (just for testing) according to Mark's comment. But since I'm not sure how to handle that error correctly in the unwinder code. So this is just for testing.
Mark, can you tell me how can I handle it? Just asserted by WARN_ON_ONCE() is OK? Or print out more error information? For the debugging, we need more information, so I printed out the error code.
Thank you,
---
Masami Hiramatsu (9):
kprobes: Add a test case for stacktrace from kretprobe handler x86/unwind: Compile kretprobe fixup code only if CONFIG_KRETPROBES=y
arm64: kprobes: Record frame pointer with kretprobe instance
arm64: kprobes: Make a frame pointer on __kretprobe_trampoline
arm64: Recover kretprobe modified return address in stacktrace
ARM: clang: Do not rely on lr register for stacktrace
ARM: kprobes: Make a frame pointer on __kretprobe_trampoline
ARM: Recover kretprobe modified return address in stacktrace [RFC] arm64: kprobes: Detect error of kretprobe return address fixup
Sven Schnelle (1):
kprobes: convert tests to kunit
arch/Kconfig | 8 + arch/arm/Kconfig | 1 arch/arm/include/asm/stacktrace.h | 9 + arch/arm/kernel/return_address.c | 4 arch/arm/kernel/stacktrace.c | 17 + arch/arm/probes/kprobes/core.c | 29 ++ arch/arm64/Kconfig | 1 arch/arm64/include/asm/stacktrace.h | 4 arch/arm64/kernel/probes/kprobes.c | 4 arch/arm64/kernel/probes/kprobes_trampoline.S | 4 arch/arm64/kernel/stacktrace.c | 13 + arch/x86/Kconfig | 1 arch/x86/include/asm/unwind.h | 6 include/linux/kprobes.h | 2 kernel/kprobes.c | 52 +++ kernel/test_kprobes.c | 374 ++++++++++++++----------- lib/Kconfig.debug | 3 17 files changed, 359 insertions(+), 173 deletions(-)
--
Masami Hiramatsu (Linaro) <[email protected]>
Masami Hiramatsu
maintainerPATCH 1/10 kprobes: convert tests to kunit
From: Sven Schnelle <[email protected]>
This converts the kprobes testcases to use the kunit framework. It adds a dependency on CONFIG_KUNIT, and the output will change to TAP:
TAP version 14
1..1
# Subtest: kprobes_test
1..4
random: crng init done
ok 1 - test_kprobe
ok 2 - test_kprobes
ok 3 - test_kretprobe
ok 4 - test_kretprobes
ok 1 - kprobes_test
Note that the kprobes testcases are no longer run immediately after kprobes initialization, but as a late initcall when kunit is initialized. kprobes itself is initialized with an early initcall, so the order is still correct.
--- kernel/kprobes.c | 3 - kernel/test_kprobes.c | 222 +++++++++++++------------------------------------ lib/Kconfig.debug | 3 - 3 files changed, 61 insertions(+), 167 deletions(-)
kernel/kprobes.c +0 -3
@@ -2581,9 +2581,6 @@ static int __init init_kprobes(void) err =register_module_notifier (&kprobe_module_nb); kprobes_initialized = (err ==0 ); - -if (!err) -init_test_probes ();return err; }early_initcall (init_kprobes);
kernel/test_kprobes.c +59 -163
338 lines not on this page · raw message
lib/Kconfig.debug +2 -1
@@ -2080,9 +2080,10 @@ config TEST_DIV64 If unsure, say N. config KPROBES_SANITY_TEST - bool "Kprobes sanity tests" + tristate "Kprobes sanity tests" depends on DEBUG_KERNEL depends on KPROBES + depends on KUNIT help This option provides for testing basic kprobes functionality on boot. Samples of kprobe and kretprobe are inserted and
Masami Hiramatsu
maintainerPATCH 3/10 x86/unwind: Compile kretprobe fixup code only if CONFIG_KRETPROBES=y
In reply to Masami HiramatsuCompile kretprobe related stacktrace entry recovery code and unwind_state::kr_cur field only when CONFIG_KRETPROBES=y.
--- arch/x86/include/asm/unwind.h | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/include/asm/unwind.h +6 -0
@@ -16,7 +16,9 @@ struct unwind_state {unsigned long stack_mask;struct task_struct *task;int graph_idx; +#ifdef CONFIG_KRETPROBES struct llist_node *kr_cur; +#endif bool error;#if defined(CONFIG_UNWINDER_ORC )bool signal, full_regs;
@@ -105,9 +107,13 @@ static inlineunsigned long unwind_recover_kretprobe (struct unwind_state *state,unsigned long addr,unsigned long *addr_p) { +#ifdef CONFIG_KRETPROBES return is_kretprobe_trampoline (addr) ?kretprobe_find_ret_addr (state->task, addr_p, &state->kr_cur) : addr; +#else +return addr; +#endif }/* Recover the return address modified by kretprobe and ftrace_graph. */
Masami Hiramatsu
maintainerPATCH 4/10 arm64: kprobes: Record frame pointer with kretprobe instance
In reply to Masami HiramatsuRecord the frame pointer instead of stack address with kretprobe instance as the identifier on the instance list.
Since arm64 always enable CONFIG_FRAME_POINTER, we can use the actual frame pointer (x29).
This will allow the stacktrace code to find the original return address from the FP alone.
---
Changes in v2:
- Update changelog according to Mark's comment.
--- arch/arm64/kernel/probes/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kernel/probes/kprobes.c +2 -2
@@ -401,14 +401,14 @@ int __init arch_populate_kprobe_blacklist(void)void __kprobes __used *trampoline_probe_handler (struct pt_regs *regs) { -return (void *)kretprobe_trampoline_handler (regs, (void *)kernel_stack_pointer (regs)); +return (void *)kretprobe_trampoline_handler (regs, (void *)regs->regs[29 ]); }void __kprobes arch_prepare_kretprobe (struct kretprobe_instance *ri,struct pt_regs *regs) { ri->ret_addr = (kprobe_opcode_t *)regs->regs[30 ]; - ri->fp = (void *)kernel_stack_pointer (regs); + ri->fp = (void *)regs->regs[29 ];/* replace return addr (x30) with trampoline */ regs->regs[30 ] = (long )&__kretprobe_trampoline;
Masami Hiramatsu
maintainerPATCH 5/10 arm64: kprobes: Make a frame pointer on __kretprobe_trampoline
In reply to Masami HiramatsuMake a frame pointer (make the x29 register points the address of pt_regs->regs[29]) on __kretprobe_trampoline.
This frame pointer will be used by the stacktracer when it is called from the kretprobe handlers. In this case, the stack tracer will unwind stack to trampoline_probe_handler() and find the next frame pointer in the stack frame of the __kretprobe_trampoline().
--- arch/arm64/kernel/probes/kprobes_trampoline.S | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/kernel/probes/kprobes_trampoline.S +4 -0
@@ -66,6 +66,9 @@ SYM_CODE_START(__kretprobe_trampoline) save_all_base_regs + /* Setup a frame pointer. */ + add x29, sp, #S_FP + mov x0, sp bl trampoline_probe_handler /*
@@ -74,6 +77,7 @@ SYM_CODE_START(__kretprobe_trampoline) */ mov lr, x0 + /* The frame pointer (x29) is restored with other registers. */ restore_all_base_regs add sp, sp, #PT_REGS_SIZE
Masami Hiramatsu
maintainerPATCH 2/10 kprobes: Add a test case for stacktrace from kretprobe handler
In reply to Masami HiramatsuAdd a test case for stacktrace from kretprobe handler and nested kretprobe handlers.
This test checks both of stack trace inside kretprobe handler and stack trace from pt_regs. Those stack trace must include actual function return address instead of kretprobe trampoline. The nested kretprobe stacktrace test checks whether the unwinder can correctly unwind the call frame on the stack which has been modified by the kretprobe.
Since the stacktrace on kretprobe is correctly fixed only on x86, this introduces a meta kconfig ARCH_CORRECT_STACKTRACE_ON_KRETPROBE which tells user that the stacktrace on kretprobe is correct or not.
The test results will be shown like below;
TAP version 14
1..1
# Subtest: kprobes_test
1..6
ok 1 - test_kprobe
ok 2 - test_kprobes
ok 3 - test_kretprobe
ok 4 - test_kretprobes
ok 5 - test_stacktrace_on_kretprobe
ok 6 - test_stacktrace_on_nested_kretprobe
# kprobes_test: pass:6 fail:0 skip:0 total:6
# Totals: pass:6 fail:0 skip:0 total:6
ok 1 - kprobes_test
--- arch/Kconfig | 8 ++ arch/x86/Kconfig | 1 kernel/test_kprobes.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+)
arch/Kconfig +8 -0
@@ -191,6 +191,14 @@ config HAVE_OPTPROBES config HAVE_KPROBES_ON_FTRACE bool +config ARCH_CORRECT_STACKTRACE_ON_KRETPROBE + bool + help + Since kretprobes modifies return address on the stack, the + stacktrace may see the kretprobe trampoline address instead + of correct one. If the architecture stacktrace code and + unwinder can adjust such entries, select this configuration. + config HAVE_FUNCTION_ERROR_INJECTION bool
arch/x86/Kconfig +1 -0
@@ -61,6 +61,7 @@ config X86 select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI select ARCH_32BIT_OFF_T if X86_32 select ARCH_CLOCKSOURCE_INIT + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if X86_64 && HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG if X86_64 || (X86_32 && HIGHMEM) select ARCH_ENABLE_MEMORY_HOTREMOVE if MEMORY_HOTPLUG
kernel/test_kprobes.c +158 -0
188 lines not on this page · raw message
Masami Hiramatsu
maintainerPATCH 6/10 arm64: Recover kretprobe modified return address in stacktrace
In reply to Masami HiramatsuSince the kretprobe replaces the function return address with the kretprobe_trampoline on the stack, stack unwinder shows it instead of the correct return address.
This checks whether the next return address is the __kretprobe_trampoline(), and if so, try to find the correct return address from the kretprobe instance list. For this purpose this adds 'kr_cur' loop cursor to memorize the current kretprobe instance.
With this fix, now arm64 can enable CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE, and pass the kprobe self tests.
---
Changes in v2:
- Add comment for kr_cur.
- Make the kretprobe related code depends on CONFIG_KRETPROBES.
- Initialize "kr_cur" directly in start_backtrace() instead of clearing "frame" data structure by memset().
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/stacktrace.h | 4 ++++ arch/arm64/kernel/stacktrace.c | 7 +++++++ 3 files changed, 12 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -11,6 +11,7 @@ config ARM64 select ACPI_PPTT if ACPI select ARCH_HAS_DEBUG_WX select ARCH_BINFMT_ELF_STATE + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVE
arch/arm64/include/asm/stacktrace.h +4 -0
@@ -9,6 +9,7 @@#include <linux/sched.h> #include <linux/sched/task_stack.h> #include <linux/types.h> +#include <linux/llist.h> #include <asm/memory.h> #include <asm/ptrace.h>
@@ -59,6 +60,9 @@ struct stackframe {#ifdef CONFIG_FUNCTION_GRAPH_TRACER int graph;#endif +#ifdef CONFIG_KRETPROBES +struct llist_node *kr_cur; +#endif };extern int unwind_frame (struct task_struct *tsk,struct stackframe *frame);
arch/arm64/kernel/stacktrace.c +7 -0
@@ -41,6 +41,9 @@ void start_backtrace(struct stackframe *frame, unsigned long fp,#ifdef CONFIG_FUNCTION_GRAPH_TRACER frame->graph =0 ;#endif +#ifdef CONFIG_KRETPROBES + frame->kr_cur =NULL ; +#endif /* * Prime the first unwind.
@@ -129,6 +132,10 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) frame->pc = ret_stack->ret; }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +#ifdef CONFIG_KRETPROBES +if (is_kretprobe_trampoline (frame->pc)) + frame->pc =kretprobe_find_ret_addr (tsk, (void *)frame->fp, &frame->kr_cur); +#endif frame->pc =ptrauth_strip_insn_pac (frame->pc);
Masami Hiramatsu
maintainerPATCH 7/10 ARM: clang: Do not rely on lr register for stacktrace
In reply to Masami HiramatsuCurrently the stacktrace on clang compiled arm kernel uses the 'lr' register to find the first frame address from pt_regs. However, that is wrong after calling another function, because the 'lr' register is used by 'bl' instruction and never be recovered.
As same as gcc arm kernel, directly use the frame pointer (r11) of the pt_regs to find the first frame address.
Note that this fixes kretprobe stacktrace issue only with CONFIG_UNWINDER_FRAME_POINTER=y. For the CONFIG_UNWINDER_ARM, we need another fix.
---
Changes in v2:
- Fix typos in changelog.
--- arch/arm/kernel/stacktrace.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/kernel/stacktrace.c +1 -2
@@ -54,8 +54,7 @@ int notrace unwind_frame(struct stackframe *frame) frame->sp = frame->fp; frame->fp = *(unsigned long *)(fp); - frame->pc = frame->lr; - frame->lr = *(unsigned long *)(fp +4 ); + frame->pc = *(unsigned long *)(fp +4 );#else /* check current frame pointer is within bounds */ if (fp < low +12 || fp > high -4 )
Masami Hiramatsu
maintainerPATCH 8/10 ARM: kprobes: Make a frame pointer on __kretprobe_trampoline
In reply to Masami HiramatsuCurrently kretprobe on ARM just fills r0-r11 of pt_regs, but that is not enough for the stacktrace. Moreover, from the user kretprobe handler, stacktrace needs a frame pointer on the __kretprobe_trampoline.
This adds a frame pointer on __kretprobe_trampoline for both gcc and clang case. Those have different frame pointer so we need different but similar stack on pt_regs.
Gcc makes the frame pointer (fp) to point the 'pc' address of the {fp, ip (=sp), lr, pc}, this means {r11, r13, r14, r15}. Thus if we save the r11 (fp) on pt_regs->r12, we can make this set on the end of pt_regs.
On the other hand, Clang makes the frame pointer to point the 'fp' address of {fp, lr} on stack. Since the next to the pt_regs->lr is pt_regs->sp, I reused the pair of pt_regs->fp and pt_regs->ip.
So this stores the 'lr' on pt_regs->ip and make the fp to point pt_regs->fp.
For both cases, saves __kretprobe_trampoline address to pt_regs->lr, so that the stack tracer can identify this frame pointer has been made by the __kretprobe_trampoline.
Note that if the CONFIG_FRAME_POINTER is not set, this keeps fp as is.
--- arch/arm/probes/kprobes/core.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
arch/arm/probes/kprobes/core.c +24 -5
@@ -368,16 +368,35 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self,/* * When a retprobed function returns, trampoline_handler() is called, * calling the kretprobe's handler. We construct a struct pt_regs to -* give a view of registers r0-r11 to the user return-handler. This is -* not a complete pt_regs structure, but that should be plenty sufficient -* for kretprobe handlers which should normally be interested in r0 only -* anyway. +* give a view of registers r0-r11, sp, lr, and pc to the user +* return-handler. This is not a complete pt_regs structure, but that +* should be enough for stacktrace from the return handler with or +* without pt_regs. */ void __naked __kprobes __kretprobe_trampoline (void ) { __asm__ __volatile__ ( -"sub sp, sp, #16 \n\t" + "ldr lr, =__kretprobe_trampoline \n\t" + "stmdb sp!, {sp, lr, pc} \n\t" +#ifdef CONFIG_FRAME_POINTER +/* __kretprobe_trampoline makes a framepointer on pt_regs. */ +#ifdef CONFIG_CC_IS_CLANG +/* In clang case, pt_regs->ip = lr. */ +"stmdb sp!, {lr} \n\t" "stmdb sp!, {r0 - r11} \n\t" +/* fp points regs->r11 (fp) */ + "add fp, sp, #44 \n\t" +#else /* !CONFIG_CC_IS_CLANG */ +/* In gcc case, pt_regs->ip = fp. */ +"stmdb sp!, {fp} \n\t" +"stmdb sp!, {r0 - r11} \n\t" +/* fp points regs->r15 (pc) */ + "add fp, sp, #60 \n\t" +#endif /* CONFIG_CC_IS_CLANG */ +#else /* !CONFIG_FRAME_POINTER */ +"sub sp, sp, #4 \n\t" + "stmdb sp!, {r0 - r11} \n\t" +#endif /* CONFIG_FRAME_POINTER */ "mov r0, sp \n\t" "bl trampoline_handler \n\t" "mov lr, r0 \n\t"
Masami Hiramatsu
maintainerPATCH 9/10 ARM: Recover kretprobe modified return address in stacktrace
In reply to Masami HiramatsuSince the kretprobe replaces the function return address with the kretprobe_trampoline on the stack, arm unwinder shows it instead of the correct return address.
This finds the correct return address from the per-task kretprobe_instances list and verify it is in between the caller fp and callee fp.
Note that this supports both GCC and clang if CONFIG_FRAME_POINTER=y and CONFIG_ARM_UNWIND=n. For the ARM unwinder, this is still not working correctly.
---
Changes in v2:
- Compile this code only when CONFIG_KRETPROBES=y
--- arch/arm/Kconfig | 1 + arch/arm/include/asm/stacktrace.h | 9 +++++++++ arch/arm/kernel/return_address.c | 4 ++++ arch/arm/kernel/stacktrace.c | 14 ++++++++++++++ 4 files changed, 28 insertions(+)
arch/arm/Kconfig +1 -0
@@ -3,6 +3,7 @@ config ARM bool default y select ARCH_32BIT_OFF_T + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE if HAVE_KRETPROBES && FRAME_POINTER && !ARM_UNWIND select ARCH_HAS_BINFMT_FLAT select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DMA_WRITE_COMBINE if !ARM_DMA_MEM_BUFFERABLE
arch/arm/include/asm/stacktrace.h +9 -0
@@ -3,6 +3,7 @@#define __ASM_STACKTRACE_H#include <asm/ptrace.h> +#include <linux/llist.h> struct stackframe {/*
@@ -13,6 +14,10 @@ struct stackframe {unsigned long sp;unsigned long lr;unsigned long pc; +#ifdef CONFIG_KRETPROBES +struct llist_node *kr_cur; +struct task_struct *tsk; +#endif }; static __always_inline
@@ -22,6 +27,10 @@ void arm_get_current_stackframe(struct pt_regs *regs, struct stackframe *frame) frame->sp = regs->ARM_sp; frame->lr = regs->ARM_lr; frame->pc = regs->ARM_pc; +#ifdef CONFIG_KRETPROBES + frame->kr_cur =NULL ; + frame->tsk = current; +#endif }extern int unwind_frame (struct stackframe *frame);
arch/arm/kernel/return_address.c +4 -0
@@ -42,6 +42,10 @@ void *return_address(unsigned int level) frame.sp = current_stack_pointer; frame.lr = (unsigned long )__builtin_return_address (0 ); frame.pc = (unsigned long )return_address; +#ifdef CONFIG_KRETPROBES + frame.kr_cur =NULL ; + frame.tsk = current; +#endif walk_stackframe (&frame, save_return_addr, &data);
arch/arm/kernel/stacktrace.c +14 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/export.h> +#include <linux/kprobes.h> #include <linux/sched.h> #include <linux/sched/debug.h> #include <linux/stacktrace.h>
@@ -65,6 +66,11 @@ int notrace unwind_frame(struct stackframe *frame) frame->sp = *(unsigned long *)(fp -8 ); frame->pc = *(unsigned long *)(fp -4 );#endif +#ifdef CONFIG_KRETPROBES +if (is_kretprobe_trampoline (frame->pc)) + frame->pc =kretprobe_find_ret_addr (frame->tsk, + (void *)frame->fp, &frame->kr_cur); +#endif return 0 ; }
@@ -156,6 +162,10 @@ static noinline void __save_stack_trace(struct task_struct *tsk, frame.lr = (unsigned long )__builtin_return_address (0 ); frame.pc = (unsigned long )__save_stack_trace; } +#ifdef CONFIG_KRETPROBES + frame.kr_cur =NULL ; + frame.tsk = tsk; +#endif walk_stackframe (&frame, save_trace, &data); }
@@ -173,6 +183,10 @@ void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) frame.sp = regs->ARM_sp; frame.lr = regs->ARM_lr; frame.pc = regs->ARM_pc; +#ifdef CONFIG_KRETPROBES + frame.kr_cur =NULL ; + frame.tsk = current; +#endif walk_stackframe (&frame, save_trace, &data); }
Masami Hiramatsu
maintainerRFC PATCH 10/10 arm64: kprobes: Detect error of kretprobe return address fixup
In reply to Masami HiramatsuAdd kretprobe_next_ret_addr() which can detect errors in the given parameter or the kretprobe_instance list, and call it from arm64 stacktrace.
This kretprobe_next_ret_addr() will return following errors when it detects;
- -EINVAL if @cur is NULL (caller issue)
- -ENOENT if there is no next correct return address (either kprobes or caller issue)
- -EILSEQ if the next currect return address is there but doesn't match the framepointer (maybe caller issue)
Thus the caller must check the error and handle it. On arm64, this tries to handle the errors and show it on the log.
--- arch/arm64/kernel/stacktrace.c | 10 +++++++- include/linux/kprobes.h | 2 ++ kernel/kprobes.c | 49 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-)
arch/arm64/kernel/stacktrace.c +8 -2
@@ -133,8 +133,14 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ #ifdef CONFIG_KRETPROBES -if (is_kretprobe_trampoline (frame->pc)) - frame->pc =kretprobe_find_ret_addr (tsk, (void *)frame->fp, &frame->kr_cur); +if (is_kretprobe_trampoline (frame->pc)) { +void *ret =kretprobe_next_ret_addr (tsk, (void *)frame->fp, &frame->kr_cur); +/* There must be a bug in this unwinder or kretprobe. */ +if (WARN_ON_ONCE (IS_ERR (ret))) +pr_err ("Kretprobe_trampoline recovery failed (%d)\n" ,PTR_ERR (ret)); +else + frame->pc = (unsigned long )ret; + }#endif frame->pc =ptrauth_strip_insn_pac (frame->pc);
include/linux/kprobes.h +2 -0
@@ -516,6 +516,8 @@ static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)unsigned long kretprobe_find_ret_addr (struct task_struct *tsk,void *fp,struct llist_node **cur); +kprobe_opcode_t *kretprobe_next_ret_addr (struct task_struct *tsk,void *fp, +struct llist_node **cur);#else static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr) {
kernel/kprobes.c +49 -0
55 lines not on this page · raw message
Masami Hiramatsu
maintainerRe: [PATCH 03/10] x86/unwind: Compile kretprobe fixup code only if CONFIG_KRETPROBES=y
In reply to Masami HiramatsuQuoted 2 lines · On Fri, 15 Oct 2021 21:51:10 +0900 Masami Hiramatsu <[email protected]> wrote:
Compile kretprobe related stacktrace entry recovery code and
unwind_state::kr_curfield only whenCONFIG_KRETPROBES=y.
Oh, this is another new patch, and is a kind of cleanup. No functionality change, but a bit reducing memory usage when CONFIG_KRETPROBES=n.
Thank you,
Quoted 35 lines
Signed-off-by Masami Hiramatsu <[email protected]>--- arch/x86/include/asm/unwind.h | 6 ++++++ 1 file changed, 6 insertions(+)arch/x86/include/asm/unwind.h +6
-0@@ -16,7 +16,9 @@ struct unwind_state {unsigned long stack_mask;struct task_struct *task;int graph_idx; +#ifdef CONFIG_KRETPROBES struct llist_node *kr_cur; +#endif bool error;#if defined(CONFIG_UNWINDER_ORC )bool signal, full_regs;@@ -105,9 +107,13 @@ static inlineunsigned long unwind_recover_kretprobe (struct unwind_state *state,unsigned long addr,unsigned long *addr_p) { +#ifdef CONFIG_KRETPROBES return is_kretprobe_trampoline (addr) ?kretprobe_find_ret_addr (state->task, addr_p, &state->kr_cur) : addr; +#else +return addr; +#endif }/* Recover the return address modified by kretprobe and ftrace_graph. */
Signature · 1 line
Masami Hiramatsu <[email protected]>
kernel test robot
build botRe: [PATCH 08/10] ARM: kprobes: Make a frame pointer on __kretprobe_trampoline
In reply to Masami HiramatsuI love your patch! Yet something to improve:
Hi Masami,
I love your patch! Yet something to improve:
[auto build test ERROR on rostedt-trace/for-next]
[also build test ERROR on next-20211015]
[cannot apply to arm64/for-next/core tip/x86/core linus/master v5.15-rc5] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Masami-Hiramatsu/kprobes-Make-KUnit-and-add-stacktrace-on-kretprobe-tests/20211015-205329 base: https://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git for-next config: arm-randconfig-r016-20211015 (attached as .config) compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 6069a6a5049497a32a50a49661c2f4169078bdba)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/4039c4f80aba40806049567ad4f916bc4b9c1576
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Masami-Hiramatsu/kprobes-Make-KUnit-and-add-stacktrace-on-kretprobe-tests/20211015-205329 git checkout 4039c4f80aba40806049567ad4f916bc4b9c1576
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
arch/arm/probes/kprobes/core.c:236:16: warning: no previous prototype for function 'kprobe_handler' [-Wmissing-prototypes] void __kprobes kprobe_handler(struct pt_regs *regs)
^
arch/arm/probes/kprobes/core.c:236:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
void __kprobes kprobe_handler(struct pt_regs *regs)
^
static
>> arch/arm/probes/kprobes/core.c:379:38: error: writeback register not allowed in register list
"ldr lr, =__kretprobe_trampoline \n\t"
^
<inline asm>:2:13: note: instantiated into assembly here
stmdb sp!, {sp, lr, pc}
^1 warning and 1 error generated.
vim +379 arch/arm/probes/kprobes/core.c
367
368 /*
369 * When a retprobed function returns, trampoline_handler() is called,
370 * calling the kretprobe's handler. We construct a struct pt_regs to
371 * give a view of registers r0-r11, sp, lr, and pc to the user
372 * return-handler. This is not a complete pt_regs structure, but that
373 * should be enough for stacktrace from the return handler with or
374 * without pt_regs.
375 */
376 void __naked __kprobes __kretprobe_trampoline(void)
377 {
378 __asm__ __volatile__ (
> 379 "ldr lr, =__kretprobe_trampoline \n\t"
380 "stmdb sp!, {sp, lr, pc} \n\t"
381 #ifdef CONFIG_FRAME_POINTER
382 /* __kretprobe_trampoline makes a framepointer on pt_regs. */
383 #ifdef CONFIG_CC_IS_CLANG
384 /* In clang case, pt_regs->ip = lr. */
385 "stmdb sp!, {lr} \n\t"
386 "stmdb sp!, {r0 - r11} \n\t"
387 /* fp points regs->r11 (fp) */
388 "add fp, sp, #44 \n\t"
389 #else /* !CONFIG_CC_IS_CLANG */
390 /* In gcc case, pt_regs->ip = fp. */
391 "stmdb sp!, {fp} \n\t"
392 "stmdb sp!, {r0 - r11} \n\t"
393 /* fp points regs->r15 (pc) */
394 "add fp, sp, #60 \n\t"
395 #endif /* CONFIG_CC_IS_CLANG */
396 #else /* !CONFIG_FRAME_POINTER */
397 "sub sp, sp, #4 \n\t"
398 "stmdb sp!, {r0 - r11} \n\t"
399 #endif /* CONFIG_FRAME_POINTER */
400 "mov r0, sp \n\t"
401 "bl trampoline_handler \n\t"
402 "mov lr, r0 \n\t"
403 "ldmia sp!, {r0 - r11} \n\t"
404 "add sp, sp, #16 \n\t"
405 #ifdef CONFIG_THUMB2_KERNEL
406 "bx lr \n\t"
407 #else
408 "mov pc, lr \n\t"
409 #endif
410 : : : "memory");
411 }
412
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Russell King (Oracle)
maintainerRe: [PATCH 08/10] ARM: kprobes: Make a frame pointer on __kretprobe_trampoline
In reply to Masami HiramatsuQuoted 47 lines · Masami Hiramatsu
Currently kretprobe on ARM just fills r0-r11 of
pt_regs, but that is not enough for the stacktrace. Moreover, from the user kretprobe handler, stacktrace needs a frame pointer on the__kretprobe_trampoline.This adds a frame pointer on
__kretprobe_trampolinefor both gcc and clang case. Those have different frame pointer so we need different but similar stack onpt_regs.Gcc makes the frame pointer (fp) to point the 'pc' address of the {fp, ip (=sp), lr, pc}, this means {r11, r13, r14, r15}. Thus if we save the r11 (fp) on
pt_regs->r12, we can make this set on the end ofpt_regs.On the other hand, Clang makes the frame pointer to point the 'fp' address of {fp, lr} on stack. Since the next to the
pt_regs->lrispt_regs->sp, I reused the pair ofpt_regs->fpandpt_regs->ip.
So this stores the 'lr' onpt_regs->ipand make the fp to pointpt_regs->fp.For both cases, saves
__kretprobe_trampolineaddress topt_regs->lr, so that the stack tracer can identify this frame pointer has been made by the__kretprobe_trampoline.Note that if the
CONFIG_FRAME_POINTERis not set, this keeps fp as is.Signed-off-by Masami Hiramatsu <[email protected]>Reviewed-by Nick Desaulniers <[email protected]>--- arch/arm/probes/kprobes/core.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)arch/arm/probes/kprobes/core.c +1
-4@@ -368,16 +368,35 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self,/* * When a retprobed function returns, trampoline_handler() is called, * calling the kretprobe's handler. We construct a struct pt_regs to -* give a view of registers r0-r11 to the user return-handler. This is -* not a complete pt_regs structure, but that should be plenty sufficient -* for kretprobe handlers which should normally be interested in r0 only -* anyway. +* give a view of registers r0-r11, sp, lr, and pc to the user
arch/arm/probes/kprobes/core.c int __kprobes kprobe_exceptions_notify(struct notifier_block *self, · line 380
Quoted 10 lines
arch/arm/probes/kprobes/core.c +5
-1+* return-handler. This is not a complete pt_regs structure, but that +* should be enough for stacktrace from the return handler with or +* without pt_regs. */ void __naked __kprobes __kretprobe_trampoline (void ) { __asm__ __volatile__ ( - "sub sp, sp, #16 \n\t" +"ldr lr, =__kretprobe_trampoline \n\t" + "stmdb sp!, {sp, lr, pc} \n\t"
I think you really do not want to do that.
From DDI0406C:
"ARM deprecates the use of instructions with the base register in the list and ! specified. If the base register is not the lowest-numbered register in the list, such an instruction stores an UNKNOWN value for the base register."
However, it doesn't say what value is stored if the base register is the lowest-numbered register in the list. The pseudocode given shows that it is the original value. However, DDI0100E:
"Operand restrictions
If <Rn> is specified as <registers> and base register writeback is specified:
• If <Rn> is the lowest-numbered register specified in <register_list>, the original value of <Rn> is stored.
• Otherwise, the stored value of <Rn> is UNPREDICTABLE."
So I guess it might be okay... but it seems a bit dodgy to rely on this behaviour.
arch/arm/probes/kprobes/core.c int __kprobes kprobe_exceptions_notify(struct notifier_block *self, · line 386
Quoted 6 lines
arch/arm/probes/kprobes/core.c +5
-0+#ifdef CONFIG_FRAME_POINTER +/* __kretprobe_trampoline makes a framepointer on pt_regs. */ +#ifdef CONFIG_CC_IS_CLANG +/* In clang case, pt_regs->ip = lr. */ +"stmdb sp!, {lr} \n\t" "stmdb sp!, {r0 - r11} \n\t"
This can be simplified to:
"stmdb sp!, {r0 - r11, lr} \n\t"Also, note the value we store for "fp" is __kretprobe_trampoline.
arch/arm/probes/kprobes/core.c int __kprobes kprobe_exceptions_notify(struct notifier_block *self, · line 392
Quoted 6 lines
arch/arm/probes/kprobes/core.c +6
-0+/* fp points regs->r11 (fp) */ +"add fp, sp, #44 \n\t" +#else /* !CONFIG_CC_IS_CLANG */ +/* In gcc case, pt_regs->ip = fp. */ +"stmdb sp!, {fp} \n\t" +"stmdb sp!, {r0 - r11} \n\t"
This can be simplified to:
"stmdb sp!, {r0 - r12} \n\t"since fp is r12.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
Masami Hiramatsu
maintainerRe: [PATCH 08/10] ARM: kprobes: Make a frame pointer on __kretprobe_trampoline
Quoted 52 lines · On Sat, 16 Oct 2021 22:15:57 +0100 "Russell King (Oracle)"…
Quoted 51 lines · Masami Hiramatsu
Currently kretprobe on ARM just fills r0-r11 of
pt_regs, but that is not enough for the stacktrace. Moreover, from the user kretprobe handler, stacktrace needs a frame pointer on the__kretprobe_trampoline.This adds a frame pointer on
__kretprobe_trampolinefor both gcc and clang case. Those have different frame pointer so we need different but similar stack onpt_regs.Gcc makes the frame pointer (fp) to point the 'pc' address of the {fp, ip (=sp), lr, pc}, this means {r11, r13, r14, r15}. Thus if we save the r11 (fp) on
pt_regs->r12, we can make this set on the end ofpt_regs.On the other hand, Clang makes the frame pointer to point the 'fp' address of {fp, lr} on stack. Since the next to the
pt_regs->lrispt_regs->sp, I reused the pair ofpt_regs->fpandpt_regs->ip.
So this stores the 'lr' onpt_regs->ipand make the fp to pointpt_regs->fp.For both cases, saves
__kretprobe_trampolineaddress topt_regs->lr, so that the stack tracer can identify this frame pointer has been made by the__kretprobe_trampoline.Note that if the
CONFIG_FRAME_POINTERis not set, this keeps fp as is.Signed-off-by Masami Hiramatsu <[email protected]>Reviewed-by Nick Desaulniers <[email protected]>--- arch/arm/probes/kprobes/core.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)arch/arm/probes/kprobes/core.c +4
-4@@ -368,16 +368,35 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self,/* * When a retprobed function returns, trampoline_handler() is called, * calling the kretprobe's handler. We construct a struct pt_regs to -* give a view of registers r0-r11 to the user return-handler. This is -* not a complete pt_regs structure, but that should be plenty sufficient -* for kretprobe handlers which should normally be interested in r0 only -* anyway. +* give a view of registers r0-r11, sp, lr, and pc to the user +* return-handler. This is not a complete pt_regs structure, but that +* should be enough for stacktrace from the return handler with or +* without pt_regs. */
Quoted 8 lines
arch/arm/probes/kprobes/core.c int __kprobes kprobe_exceptions_notify(struct notifier_block *self, · line 380
Quoted 6 lines
arch/arm/probes/kprobes/core.c +2
-1void __naked __kprobes __kretprobe_trampoline (void ) { __asm__ __volatile__ ( - "sub sp, sp, #16 \n\t" +"ldr lr, =__kretprobe_trampoline \n\t" + "stmdb sp!, {sp, lr, pc} \n\t"I think you really do not want to do that.
Yes, I just wants to save the {sp, lr, pc} to mimic the framepointer.
Quoted 19 lines
From DDI0406C:
"ARM deprecates the use of instructions with the base register in the list and ! specified. If the base register is not the lowest-numbered register in the list, such an instruction stores an UNKNOWN value for the base register."
However, it doesn't say what value is stored if the base register is the lowest-numbered register in the list. The pseudocode given shows that it is the original value. However, DDI0100E:
"Operand restrictions
If <Rn> is specified as <registers> and base register writeback is specified:
• If <Rn> is the lowest-numbered register specified in <register_list>, the original value of <Rn> is stored.
• Otherwise, the stored value of <Rn> is UNPREDICTABLE."
Quoted 2 lines
So I guess it might be okay... but it seems a bit dodgy to rely on this behaviour.
Oh, OK. I just tested it on qemu-arm so maybe it was wrong.
Quoted 9 lines
arch/arm/probes/kprobes/core.c int __kprobes kprobe_exceptions_notify(struct notifier_block *self, · line 386
Quoted 7 lines
arch/arm/probes/kprobes/core.c +5
-0+#ifdef CONFIG_FRAME_POINTER +/* __kretprobe_trampoline makes a framepointer on pt_regs. */ +#ifdef CONFIG_CC_IS_CLANG +/* In clang case, pt_regs->ip = lr. */ +"stmdb sp!, {lr} \n\t" "stmdb sp!, {r0 - r11} \n\t" This can be simplified to:
Quoted 3 lines
"stmdb sp!, {r0 - r11, lr} \n\t"Also, note the value we store for "fp" is
__kretprobe_trampoline.
Oh, I thought 'r11' is 'fp' from arch/arm/include/uapi/asm/ptrace.h
...
#define ARM_ip uregs[12] #define ARM_fp uregs[11] #define ARM_r10 uregs[10]
...
Is that fp? or ip?
Quoted 9 lines
arch/arm/probes/kprobes/core.c int __kprobes kprobe_exceptions_notify(struct notifier_block *self, · line 392
Quoted 7 lines
arch/arm/probes/kprobes/core.c +6
-0+/* fp points regs->r11 (fp) */ +"add fp, sp, #44 \n\t" +#else /* !CONFIG_CC_IS_CLANG */ +/* In gcc case, pt_regs->ip = fp. */ +"stmdb sp!, {fp} \n\t" +"stmdb sp!, {r0 - r11} \n\t" This can be simplified to:
Quoted 3 lines
"stmdb sp!, {r0 - r12} \n\t"since fp is r12.
Ditto. The arch/arm/include/uapi/asm/ptrace.h seems to say 'r12' is 'ip'.
Thank you,
Quoted 4 lines
Signature · 3 lines
-- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
Signature · 1 line
Masami Hiramatsu <[email protected]>