From: Song Liu <song@kernel.org>
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 v2 1/2] arm64: Implement arch_stack_walk_reliable
Date: Wed, 19 Mar 2025 14:37:06 -0700 [thread overview]
Message-ID: <20250319213707.1784775-2-song@kernel.org> (raw)
In-Reply-To: <20250319213707.1784775-1-song@kernel.org>
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 <song@kernel.org>
---
arch/arm64/Kconfig | 2 +-
arch/arm64/kernel/stacktrace.c | 70 ++++++++++++++++++++++++++--------
2 files changed, 55 insertions(+), 17 deletions(-)
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 940343beb3d4..ed4f7bf4a879 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -275,6 +275,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.
@@ -2499,4 +2500,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..b0489aad5897 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;
};
static __always_inline void
@@ -230,8 +231,26 @@ kunwind_next_frame_record(struct kunwind_state *state)
new_fp = READ_ONCE(record->fp);
new_pc = READ_ONCE(record->lr);
- if (!new_fp && !new_pc)
- return kunwind_next_frame_record_meta(state);
+ if (!new_fp && !new_pc) {
+ int ret;
+
+ ret = kunwind_next_frame_record_meta(state);
+ if (ret < 0) {
+ /*
+ * This covers two different conditions:
+ * 1. ret == -ENOENT, unwinding is done.
+ * 2. ret == -EINVAL, unwinding hit error.
+ */
+ return ret;
+ }
+ /*
+ * Searching across exception boundaries. The stack is now
+ * unreliable.
+ */
+ if (state->end_on_unreliable)
+ return -EINVAL;
+ return 0;
+ }
unwind_consume_stack(&state->common, info, fp, sizeof(*record));
@@ -277,21 +296,24 @@ 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)
{
- if (kunwind_recover_return_address(state))
- return;
+ int ret;
- while (1) {
- int ret;
+ ret = kunwind_recover_return_address(state);
+ if (ret)
+ return ret;
+ while (1) {
if (!consume_state(state, cookie))
- break;
+ return -EINVAL;
ret = kunwind_next(state);
+ if (ret == -ENOENT)
+ return 0;
if (ret < 0)
- break;
+ return ret;
}
}
@@ -324,10 +346,10 @@ 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)
+ struct pt_regs *regs, bool end_on_unreliable)
{
struct stack_info stacks[] = {
stackinfo_get_task(task),
@@ -348,11 +370,12 @@ kunwind_stack_walk(kunwind_consume_fn consume_state,
.stacks = stacks,
.nr_stacks = ARRAY_SIZE(stacks),
},
+ .end_on_unreliable = end_on_unreliable,
};
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 +383,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 {
@@ -384,7 +407,22 @@ noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry,
.cookie = cookie,
};
- 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 consume_entry,
+ void *cookie, struct task_struct *task)
+{
+ struct kunwind_consume_entry_data data = {
+ .consume_entry = consume_entry,
+ .cookie = cookie,
+ };
+ int ret;
+
+ ret = kunwind_stack_walk(arch_kunwind_consume_entry, &data, task, NULL, true);
+ if (ret == -ENOENT)
+ ret = 0;
+ return ret;
}
struct bpf_unwind_consume_entry_data {
@@ -409,7 +447,7 @@ noinline noinstr void arch_bpf_stack_walk(bool (*consume_entry)(void *cookie, u6
.cookie = cookie,
};
- kunwind_stack_walk(arch_bpf_unwind_consume_entry, &data, current, NULL);
+ kunwind_stack_walk(arch_bpf_unwind_consume_entry, &data, current, NULL, false);
}
static const char *state_source_string(const struct kunwind_state *state)
@@ -456,7 +494,7 @@ void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
return;
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);
put_task_stack(tsk);
}
--
2.47.1
next prev parent reply other threads:[~2025-03-19 21:41 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-19 21:37 [PATCH v2 0/2] arm64: livepatch: Enable livepatch without sframe Song Liu
2025-03-19 21:37 ` Song Liu [this message]
2025-03-19 22:35 ` [PATCH v2 1/2] arm64: Implement arch_stack_walk_reliable Josh Poimboeuf
2025-03-20 2:20 ` Song Liu
2025-03-19 21:37 ` [PATCH v2 2/2] arm64: Implement HAVE_LIVEPATCH Song Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250319213707.1784775-2-song@kernel.org \
--to=song@kernel.org \
--cc=indu.bhagat@oracle.com \
--cc=irogers@google.com \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=kernel-team@meta.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-toolchains@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=peterz@infradead.org \
--cc=puranjay@kernel.org \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=will@kernel.org \
--cc=wnliu@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.