From patchwork Wed Jan 29 23:29:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 13954242 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B4A0DC0218D for ; Wed, 29 Jan 2025 23:31:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=NaS0fs1sqDcgE/8Lvzs7S7HBVlJ2TLFKX7uIm1cAuNY=; b=kEXX6Vzs2aaH+4rCtaX4gRnqPM rwY/UV1Va1sDz7b/M/vr4gjqtj4MquGyR2rJKVaAPX0qWEJQqk2hf/mhoXc9Vf6aBDuS5kz3p4qx0 JEg/xoi5trffFE8oRAXO47ekXobAAUXzWuiCDeIdoPHEvOuSE6RK1hdu+KbCtHQWQqoIUl1xdsSs6 siSOzAOiwCe6R+TL4JHlYEelGWLnYsgCvQZaGxDx3h3w6qppkZ018YDd4opLdqzT8Qkuo1QQXvPEt hO1ubfLP+vQQYTpDWQpFBvknJK5EvL6icLkOo/CmXraAG3mlbprmMTM8M3ZdnXW/2rNHQtpmhxFDp 9oNPMSGw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tdHX6-00000007xiV-3O25; Wed, 29 Jan 2025 23:31:44 +0000 Received: from nyc.source.kernel.org ([2604:1380:45d1:ec00::3]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tdHVl-00000007xcI-0Pox for linux-arm-kernel@lists.infradead.org; Wed, 29 Jan 2025 23:30:22 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 1AE7AA41CAC; Wed, 29 Jan 2025 23:28:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 735BEC4CED1; Wed, 29 Jan 2025 23:30:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738193419; bh=wUAJ/v7od0Gk1yKWDiIdIZiAOmZif9syfzaEjk1XI98=; h=From:To:Cc:Subject:Date:From; b=sFKI3DC/0dh9WCpYNoY1rOI8OJq2KVdOEeiWXMUrstkGDrDmBf0vMC3KwtGEGyd/A 2NFu3zxaXhYyoK3hbcbS0zgI6aPGxQlN6QcOJBPN4CAN8+AqfDBYKi2sg79/GLNc/p 1OijCbNAD4tJmDVrgrbNaZSF3x+6oVuy30+Nqyw9j10ZqTqA4noOqz+9WjRZo30WyU glWg6lqBo+ysYEXeDfj97P03eYZBRDCGnLnQ7OUVXadbylJV0j5FKUDIqDTiC1dVX/ Pz5WNxapg7SZHo+qaiHZtV6dQgZd+nPqkHN09LFgpGNvSybSt4Av/6jhwwN4eFJJhL K/M3XfBR6YjBA== From: Song Liu To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, live-patching@vger.kernel.org Cc: catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com, surajjs@amazon.com, duwe@suse.de, song@kernel.org, kernel-team@meta.com Subject: [RFC 1/2] arm64: Implement arch_stack_walk_reliable Date: Wed, 29 Jan 2025 15:29:35 -0800 Message-ID: <20250129232936.1795412-1-song@kernel.org> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250129_153021_267770_6F7D57C9 X-CRM114-Status: GOOD ( 14.31 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- arch/arm64/Kconfig | 2 +- arch/arm64/kernel/stacktrace.c | 35 +++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 213f42d5ca27..f5af6faf9e2b 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -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" - diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 1d9d51d7627f..280dd6839a18 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -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 int do_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 int kunwind_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; From patchwork Wed Jan 29 23:29:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 13954243 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BD6F9C0218D for ; Wed, 29 Jan 2025 23:33:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=xKPH1ZCp5oYZHJkO29c+lc4W7mzocOtl3hUddCWj21Y=; b=Tm4mPfolV4m6GBMCs90/787IJp awppZ+a4Lkse3lvCngzvt+CMo44IjPAXAfcJjolIDAmp6f/RCNZOnsqzPUHE57hqUXFnHZGoUvaB9 m/ZQ1oDv4hLQdX7R4IYkG67HPtnqsIoTSFlDi0kLD9QYkvCIangQzGY8Ys82sw7eTPucA0KiRHxJX XF5mnEYog9oCGBxyGzgVAj/9nh+WL4/QnFEjz0mE2mzS5r/3N2Pi6vzmlk3xYAdp1n6WcMnod7V4w UAZRaNzW1qdQkJ/BzHy5lCnEsiX5oqJ2a1vnx/SRdEMA8lgVv1tUU9C3own585tvd1Tui/K90LW/C 8rrJGMAg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tdHYQ-00000007xra-29gd; Wed, 29 Jan 2025 23:33:06 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tdHVq-00000007xce-2nqg for linux-arm-kernel@lists.infradead.org; Wed, 29 Jan 2025 23:30:28 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id E7C895C6214; Wed, 29 Jan 2025 23:29:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C781C4CED3; Wed, 29 Jan 2025 23:30:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738193425; bh=0tXoxL2mW+4lVtn3q6KdWULDU6u828xfzxaAEmywKLc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TiSaKyd9q/yYLD7+iv+cXeYDYhPF45pWLYTpFUkwXbucW3n1XjF5bR25PjMzluXA5 DNHgkEozHLs3BnLE2OTK1u5KfAzXRe7rJsQlg1q00nrHqZ4lNIr9y2BQ9A4Jqpew7v 11APKHCxXulXGbIvUqxmDS4fA99cLEtSlISXamWaFGsNn+h0dhBMIobtfIqxGmlnUn ssD6SL2Qt/aKnzhsOcRBc9dGRvbHCNi/RLxBbZTpVQpTVI4Egu18D/onfrYIac6tFD rdx0DhEVYa2Xj72EW+8O/tnRn6YgvlnHCUXQOPJCaKFG8Lv0nydzR2iQ9JFbZWz90P EkFbZFOZ9fl1A== From: Song Liu To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, live-patching@vger.kernel.org Cc: catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com, surajjs@amazon.com, duwe@suse.de, song@kernel.org, kernel-team@meta.com Subject: [RFC 2/2] arm64: Implement HAVE_LIVEPATCH Date: Wed, 29 Jan 2025 15:29:36 -0800 Message-ID: <20250129232936.1795412-2-song@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250129232936.1795412-1-song@kernel.org> References: <20250129232936.1795412-1-song@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250129_153026_808676_A1CAC6DE X-CRM114-Status: GOOD ( 13.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- 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 f5af6faf9e2b..475caa57c94a 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -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" diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h index 1114c1c3300a..01623c471beb 100644 --- a/arch/arm64/include/asm/thread_info.h +++ b/arch/arm64/include/asm/thread_info.h @@ -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 | \ diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-common.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, 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);