arm64: Implement arch_stack_walk_reliable
Subsystems (2)
Song Liu
maintainerRFC 1/2 arm64: Implement arch_stack_walk_reliable
Let do_kunwind() and kunwind_stack_walk() return the state of stack walk properly to the caller, and use them in arch_stack_walk_reliable(). This can be used to enable livepatching for arm64.
Signed-off-by Song Liu <[email protected]>
--- arch/arm64/Kconfig | 2 +- arch/arm64/kernel/stacktrace.c | 35 +++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-)
arch/arm64/Kconfig +1 -1
@@ -271,6 +271,7 @@ config ARM64 select HAVE_SOFTIRQ_ON_OWN_STACK select USER_STACKTRACE_SUPPORT select VDSO_GETRANDOM + select HAVE_RELIABLE_STACKTRACE help ARM 64-bit (AArch64) Linux support.
@@ -2495,4 +2496,3 @@ endmenu # "CPU Power Management" source "drivers/acpi/Kconfig" source "arch/arm64/kvm/Kconfig" -
arch/arm64/kernel/stacktrace.c +28 -7
@@ -277,22 +277,28 @@ kunwind_next(struct kunwind_state *state)typedef bool (*kunwind_consume_fn )(const struct kunwind_state *state,void *cookie); -static __always_inline void +static __always_inline intdo_kunwind (struct kunwind_state *state,kunwind_consume_fn consume_state,void *cookie) { +int ret; +if (kunwind_recover_return_address (state)) -return ; +return -EINVAL ;while (1 ) { -int ret; -if (!consume_state (state, cookie)) + ret =consume_state (state, cookie); +if (!ret)break ; ret =kunwind_next (state);if (ret <0 )break ; } +/* Unwind terminated successfully */ +if (ret == -ENOENT ) + ret =0 ; +return ret; }/*
@@ -324,7 +330,7 @@ do_kunwind(struct kunwind_state *state, kunwind_consume_fn consume_state, : stackinfo_get_unknown(); \ }) -static __always_inline void +static __always_inline intkunwind_stack_walk (kunwind_consume_fn consume_state,void *cookie,struct task_struct *task,struct pt_regs *regs)
@@ -352,7 +358,7 @@ kunwind_stack_walk(kunwind_consume_fn consume_state,if (regs) {if (task != current) -return ; +return -EINVAL ;kunwind_init_from_regs (&state, regs); }else if (task == current) {kunwind_init_from_caller (&state);
@@ -360,7 +366,7 @@ kunwind_stack_walk(kunwind_consume_fn consume_state,kunwind_init_from_task (&state, task); } -do_kunwind (&state, consume_state, cookie); +return do_kunwind (&state, consume_state, cookie); }struct kunwind_consume_entry_data {
@@ -387,6 +393,21 @@ noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry,kunwind_stack_walk (arch_kunwind_consume_entry, &data, task, regs); } +noinline noinstr int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, +void *cookie,struct task_struct *task) +{ +int ret; + +struct kunwind_consume_entry_data data = { + .consume_entry = consume_entry, + .cookie = cookie, + }; + + ret =kunwind_stack_walk (arch_kunwind_consume_entry, &data, task,NULL ); + +return ret; +} +struct bpf_unwind_consume_entry_data {bool (*consume_entry)(void *cookie,u64 ip,u64 sp,u64 fp);void *cookie;
Signature · 1 line
2.43.5
Song Liu
maintainerRFC 2/2 arm64: Implement HAVE_LIVEPATCH
This is largely based on [1] by Suraj Jitindar Singh.
Test coverage:
- Passed manual tests with samples/livepatch.
- Passed all but test-kprobe.sh in selftests/livepatch. test-kprobe.sh is expected to fail, because arm64 doesn't have KPROBES_ON_FTRACE.
- Passed tests with kpatch-build [2]. (This version includes commits that are not merged to upstream kpatch yet).
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/liu-song-6/kpatch/tree/fb-6.13
Cc Suraj Jitindar Singh <[email protected]>
Cc Torsten Duwe <[email protected]>
Signed-off-by Song Liu <[email protected]>
--- arch/arm64/Kconfig | 3 +++ arch/arm64/include/asm/thread_info.h | 4 +++- arch/arm64/kernel/entry-common.c | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +3 -0
@@ -272,6 +272,7 @@ config ARM64 select USER_STACKTRACE_SUPPORT select VDSO_GETRANDOM select HAVE_RELIABLE_STACKTRACE + select HAVE_LIVEPATCH help ARM 64-bit (AArch64) Linux support.
@@ -2496,3 +2497,5 @@ endmenu # "CPU Power Management" source "drivers/acpi/Kconfig" source "arch/arm64/kvm/Kconfig" + +source "kernel/livepatch/Kconfig"
arch/arm64/include/asm/thread_info.h +3 -1
@@ -69,6 +69,7 @@ void arch_setup_new_exec(void);#define TIF_SYSCALL_TRACEPOINT 10/* syscall tracepoint for ftrace */ #define TIF_SECCOMP 11/* syscall secure computing */ #define TIF_SYSCALL_EMU 12/* syscall emulation active */ +#define TIF_PATCH_PENDING 13/* pending live patching update */ #define TIF_MEMDIE 18/* is terminating due to OOM killer */ #define TIF_FREEZE 19#define TIF_RESTORE_SIGMASK 20
@@ -92,6 +93,7 @@ void arch_setup_new_exec(void);#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)#define _TIF_SECCOMP (1 << TIF_SECCOMP)#define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU) +#define _TIF_PATCH_PENDING (1 << TIF_PATCH_PENDING)#define _TIF_UPROBE (1 << TIF_UPROBE)#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)#define _TIF_32BIT (1 << TIF_32BIT)
@@ -103,7 +105,7 @@ void arch_setup_new_exec(void);#define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \ _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \ _TIF_UPROBE | _TIF_MTE_ASYNC_FAULT | \ - _TIF_NOTIFY_SIGNAL) + _TIF_NOTIFY_SIGNAL | _TIF_PATCH_PENDING)#define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \ _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
arch/arm64/kernel/entry-common.c +4 -0
@@ -8,6 +8,7 @@#include <linux/context_tracking.h> #include <linux/kasan.h> #include <linux/linkage.h> +#include <linux/livepatch.h> #include <linux/lockdep.h> #include <linux/ptrace.h> #include <linux/resume_user_mode.h>
@@ -144,6 +145,9 @@ static void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags) (void __user *)NULL, current); } +if (thread_flags & _TIF_PATCH_PENDING) +klp_update_patch_state (current); +if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))do_signal (regs);
Signature · 1 line
2.43.5