From nobody Wed Dec 17 08:52:23 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 197182248BE; Thu, 20 Mar 2025 17:16:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742490976; cv=none; b=AHi29ookEvqvr8QxOb4/UkYEFhQ3aO9XQnDF0wMPkS6OJDTmacAh6HONDMqRjWsr4cy8KqAbgtwHSQvZYShKbbEokIIOrZg4mJ+ZN+SQamp+RTgyOsVeqKMrMCm+7gm1j1TfGtuCd0jBicdebbpFe0b3pBwVl1V2D8tv/LdSu8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742490976; c=relaxed/simple; bh=93BptoOqRVNyFCiKnHpDZo5E7NyEmw7OFNQMaCFMsOU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Wmj0eSaEqDTzOdZV2ng/tQMlrqF7a6N+uXycHxUwHizLYDuCanj3IZWsNK3c/di1v4J+YPWDggV18dhFjXwV89xd9980D0hOtgpRFhfEG1DTdfLyqIwzVtcxrLAsHuNgK3pgs5OOV1mkf0uvwR6yI2jqwM0X6BtIDCLvvHlEEb4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WdMz1U1s; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WdMz1U1s" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55640C4CEDD; Thu, 20 Mar 2025 17:16:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742490975; bh=93BptoOqRVNyFCiKnHpDZo5E7NyEmw7OFNQMaCFMsOU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WdMz1U1svm/4L8hrxLq7fu0fIWjgK7YNV0P0hmjtoZdidcxGlN+c8CWY9XwBIFLxW TubZI2sZROkpdT20h2nbq3zhTE8Fv9j5LsaD5JGbjpPsmZTb/G6eG8BestSS6MqDXN I1O9IcFpuZJC+3dz5aJaSL+FeJyCDiLFz+x49k/q6bKGapPjgOScxWi9Co5AVUVX4C KgG5k2bOVtz7cW5xH8GNN9pcxCx6u4Y3BrdvGhLISjcffthPg3pCbgjo7/q6k82HVW R9Vp8yIJ1r4uu5jsA0OG3h52QeioNlWgVWJXf20DHdxg/FtPkXKJDX5WjyalA1fCY5 0gHs6Lsz/5RMg== From: Song Liu To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-toolchains@vger.kernel.org, live-patching@vger.kernel.org Cc: indu.bhagat@oracle.com, puranjay@kernel.org, wnliu@google.com, irogers@google.com, joe.lawrence@redhat.com, jpoimboe@kernel.org, mark.rutland@arm.com, peterz@infradead.org, roman.gushchin@linux.dev, rostedt@goodmis.org, will@kernel.org, kernel-team@meta.com, song@kernel.org Subject: [PATCH v3 1/2] arm64: Implement arch_stack_walk_reliable Date: Thu, 20 Mar 2025 10:15:58 -0700 Message-ID: <20250320171559.3423224-2-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250320171559.3423224-1-song@kernel.org> References: <20250320171559.3423224-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With proper exception boundary detection, it is possible to implment arch_stack_walk_reliable without sframe. Note that, arch_stack_walk_reliable does not guarantee getting reliable stack in all scenarios. Instead, it can reliably detect when the stack trace is not reliable, which is enough to provide reliable livepatching. Signed-off-by: Song Liu Reviewed-by: Josh Poimboeuf Reviewed-by: Miroslav Benes Reviewed-by: Petr Mladek Tested-by: Andrea della Porta --- arch/arm64/Kconfig | 2 +- arch/arm64/kernel/stacktrace.c | 66 +++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 701d980ea921..31d5e1ee6089 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -276,6 +276,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. =20 @@ -2500,4 +2501,3 @@ endmenu # "CPU Power Management" source "drivers/acpi/Kconfig" =20 source "arch/arm64/kvm/Kconfig" - diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 1d9d51d7627f..7e07911d8694 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -56,6 +56,7 @@ struct kunwind_state { enum kunwind_source source; union unwind_flags flags; struct pt_regs *regs; + bool end_on_unreliable; }; =20 static __always_inline void @@ -230,8 +231,26 @@ kunwind_next_frame_record(struct kunwind_state *state) new_fp =3D READ_ONCE(record->fp); new_pc =3D READ_ONCE(record->lr); =20 - if (!new_fp && !new_pc) - return kunwind_next_frame_record_meta(state); + if (!new_fp && !new_pc) { + int ret; + + ret =3D kunwind_next_frame_record_meta(state); + if (ret < 0) { + /* + * This covers two different conditions: + * 1. ret =3D=3D -ENOENT, unwinding is done. + * 2. ret =3D=3D -EINVAL, unwinding hit error. + */ + return ret; + } + /* + * Searching across exception boundaries. The stack is now + * unreliable. + */ + if (state->end_on_unreliable) + return -EINVAL; + return 0; + } =20 unwind_consume_stack(&state->common, info, fp, sizeof(*record)); =20 @@ -277,21 +296,24 @@ kunwind_next(struct kunwind_state *state) =20 typedef bool (*kunwind_consume_fn)(const struct kunwind_state *state, void= *cookie); =20 -static __always_inline void +static __always_inline int do_kunwind(struct kunwind_state *state, kunwind_consume_fn consume_state, void *cookie) { - if (kunwind_recover_return_address(state)) - return; + int ret; =20 - while (1) { - int ret; + ret =3D kunwind_recover_return_address(state); + if (ret) + return ret; =20 + while (1) { if (!consume_state(state, cookie)) - break; + return -EINVAL; ret =3D kunwind_next(state); + if (ret =3D=3D -ENOENT) + return 0; if (ret < 0) - break; + return ret; } } =20 @@ -324,10 +346,10 @@ do_kunwind(struct kunwind_state *state, kunwind_consu= me_fn consume_state, : stackinfo_get_unknown(); \ }) =20 -static __always_inline void +static __always_inline int kunwind_stack_walk(kunwind_consume_fn consume_state, void *cookie, struct task_struct *task, - struct pt_regs *regs) + struct pt_regs *regs, bool end_on_unreliable) { struct stack_info stacks[] =3D { stackinfo_get_task(task), @@ -348,11 +370,12 @@ kunwind_stack_walk(kunwind_consume_fn consume_state, .stacks =3D stacks, .nr_stacks =3D ARRAY_SIZE(stacks), }, + .end_on_unreliable =3D end_on_unreliable, }; =20 if (regs) { if (task !=3D current) - return; + return -EINVAL; kunwind_init_from_regs(&state, regs); } else if (task =3D=3D current) { kunwind_init_from_caller(&state); @@ -360,7 +383,7 @@ kunwind_stack_walk(kunwind_consume_fn consume_state, kunwind_init_from_task(&state, task); } =20 - do_kunwind(&state, consume_state, cookie); + return do_kunwind(&state, consume_state, cookie); } =20 struct kunwind_consume_entry_data { @@ -384,7 +407,18 @@ noinline noinstr void arch_stack_walk(stack_trace_cons= ume_fn consume_entry, .cookie =3D cookie, }; =20 - kunwind_stack_walk(arch_kunwind_consume_entry, &data, task, regs); + kunwind_stack_walk(arch_kunwind_consume_entry, &data, task, regs, false); +} + +noinline noinstr int arch_stack_walk_reliable(stack_trace_consume_fn consu= me_entry, + void *cookie, struct task_struct *task) +{ + struct kunwind_consume_entry_data data =3D { + .consume_entry =3D consume_entry, + .cookie =3D cookie, + }; + + return kunwind_stack_walk(arch_kunwind_consume_entry, &data, task, NULL, = true); } =20 struct bpf_unwind_consume_entry_data { @@ -409,7 +443,7 @@ noinline noinstr void arch_bpf_stack_walk(bool (*consum= e_entry)(void *cookie, u6 .cookie =3D cookie, }; =20 - kunwind_stack_walk(arch_bpf_unwind_consume_entry, &data, current, NULL); + kunwind_stack_walk(arch_bpf_unwind_consume_entry, &data, current, NULL, f= alse); } =20 static const char *state_source_string(const struct kunwind_state *state) @@ -456,7 +490,7 @@ void dump_backtrace(struct pt_regs *regs, struct task_s= truct *tsk, return; =20 printk("%sCall trace:\n", loglvl); - kunwind_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs); + kunwind_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs, false= ); =20 put_task_stack(tsk); } --=20 2.47.1 From nobody Wed Dec 17 08:52:23 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25FF41E5B6D; Thu, 20 Mar 2025 17:16:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742490983; cv=none; b=QyRVCP6J4QQa4ZOiZ9g8MLHi8DnndQS9qmqlQUd/fHxT3JrG5OHN0MwNxViKfc2i6Vtqc9r2rUh1414qvuyRbCuyiGl/0LP9w71VDpxzmqSl1Ro6Uxi1ATvfR3pCK3aLT9bzQ5Zk79SBi71igNRomeYHMuzmAfPCt8Dq0fbbHaE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742490983; c=relaxed/simple; bh=+evCBeNGTvUjxQbku1JezmpWd8bBB4yvmqkVwISqS1w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ewgzl0zYoouq1bDl+rVOQ9/gu6NowPsSRzScr1qCwOiNEAU+fDUtQtYX4wCV1rfYzlVIz/Lqu72We4Ic6QKSn+C2IU+d6Xem5G8WEV4EQ82PEErRxzWxB0IxtHD8z21Mmd4qBFjmFiBHCZC/WSoX1V87yppr4pSXJ+1GrcG8npw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aZhkcZjh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="aZhkcZjh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4622C4CEDD; Thu, 20 Mar 2025 17:16:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742490983; bh=+evCBeNGTvUjxQbku1JezmpWd8bBB4yvmqkVwISqS1w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aZhkcZjhgwTBQhn5xLxRnF3a4qsa3u+33DaXQvkxOCmztDrjEduWoh9z6n8kqeTFf qAdmXarac8CfVE7puGIIgCCYAY0GtNUjG8G8gfUcZPpPJ07fx5PSBQUC9g488Gx/d5 c6TKxha++wh71G+QcFcyJa6NVvszcn/4y8Va3WfQLtoRrFPSdzrs9S2wsuPqjptnsH uld7Tp2ri8VeaM446rm7kl0yuZ/TOjgptCBqKroNZi5627Urk8CscO0RsHovIuGnTM DVWtGQPt8hXyrV7j4TIsGz1B7FAOv5N2GVjRGmmxPIiLP532gpL2Hl4m5wANOgyt0w RP8bMohr+O8GA== From: Song Liu To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-toolchains@vger.kernel.org, live-patching@vger.kernel.org Cc: indu.bhagat@oracle.com, puranjay@kernel.org, wnliu@google.com, irogers@google.com, joe.lawrence@redhat.com, jpoimboe@kernel.org, mark.rutland@arm.com, peterz@infradead.org, roman.gushchin@linux.dev, rostedt@goodmis.org, will@kernel.org, kernel-team@meta.com, song@kernel.org, Suraj Jitindar Singh , Torsten Duwe , Miroslav Benes , Breno Leitao Subject: [PATCH v3 2/2] arm64: Implement HAVE_LIVEPATCH Date: Thu, 20 Mar 2025 10:15:59 -0700 Message-ID: <20250320171559.3423224-3-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250320171559.3423224-1-song@kernel.org> References: <20250320171559.3423224-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/20210604235930.603-1-surajjs@amazon.com/ [2] https://github.com/liu-song-6/kpatch/tree/fb-6.13 Cc: Suraj Jitindar Singh Cc: Torsten Duwe Signed-off-by: Song Liu Acked-by: Miroslav Benes Tested-by: Breno Leitao Reviewed-by: Petr Mladek Tested-by: Andrea della Porta --- 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(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 31d5e1ee6089..dbd237b13b21 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -277,6 +277,7 @@ config ARM64 select USER_STACKTRACE_SUPPORT select VDSO_GETRANDOM select HAVE_RELIABLE_STACKTRACE + select HAVE_LIVEPATCH help ARM 64-bit (AArch64) Linux support. =20 @@ -2501,3 +2502,5 @@ endmenu # "CPU Power Management" source "drivers/acpi/Kconfig" =20 source "arch/arm64/kvm/Kconfig" + +source "kernel/livepatch/Kconfig" diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/= thread_info.h index 1114c1c3300a..4ac42e13032b 100644 --- a/arch/arm64/include/asm/thread_info.h +++ b/arch/arm64/include/asm/thread_info.h @@ -64,6 +64,7 @@ void arch_setup_new_exec(void); #define TIF_UPROBE 4 /* uprobe breakpoint or singlestep */ #define TIF_MTE_ASYNC_FAULT 5 /* MTE Asynchronous Tag Check Fault */ #define TIF_NOTIFY_SIGNAL 6 /* signal notifications exist */ +#define TIF_PATCH_PENDING 7 /* pending live patching update */ #define TIF_SYSCALL_TRACE 8 /* syscall trace active */ #define TIF_SYSCALL_AUDIT 9 /* syscall auditing */ #define TIF_SYSCALL_TRACEPOINT 10 /* syscall tracepoint for ftrace */ @@ -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) =20 #define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \ _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \ diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-com= mon.c index b260ddc4d3e9..b537af333b42 100644 --- a/arch/arm64/kernel/entry-common.c +++ b/arch/arm64/kernel/entry-common.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -144,6 +145,9 @@ static void do_notify_resume(struct pt_regs *regs, unsi= gned long thread_flags) (void __user *)NULL, current); } =20 + if (thread_flags & _TIF_PATCH_PENDING) + klp_update_patch_state(current); + if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) do_signal(regs); =20 --=20 2.47.1