From: "Madhavan T. Venkataraman" <redacted>
Make all stack walking functions use arch_stack_walk()
======================================================
Currently, there are multiple functions in ARM64 code that walk the
stack using start_backtrace() and unwind_frame(). Convert all of
them to use arch_stack_walk(). This makes maintenance easier.
Reorganize the unwinder code for better consistency and maintenance
===================================================================
Rename unwinder functions to unwind_*() similar to other architectures
for naming consistency.
Annotate all of the unwind_*() functions with notrace so they cannot be
ftraced and NOKPROBE_SYMBOL() so they cannot be kprobed. Ftrace and Kprobe
code can call the unwinder.
Redefine the unwinder loop and make it similar to other architectures.
Define the following:
unwind_start(&frame, task, fp, pc);
while (unwind_consume(&frame, consume_entry, cookie))
unwind_next(&frame);
return !unwind_failed(&frame);
unwind_start()
Same as the original start_backtrace().
unwind_consume()
This new function does two things:
- Calls consume_entry() to consume the return PC.
- Implements checks to determine whether the unwind should continue
or terminate.
unwind_next()
Same as the original unwind_frame() except:
- the stack trace termination check has been moved from here to
unwind_consume(). So, unwind_next() assumes that the fp is valid.
- unwind_frame() used to return an error value. This function only
sets internal state and does not return anything. The state is
retrieved via a helper. See next.
unwind_failed()
Return a boolean to indicate whether the stack trace completed
successfully or failed. arch_stack_walk() ignores the return
value. But arch_stack_walk_reliable() in the future will look
at the return value.
Unwind status
Introduce a new flag called "failed" in struct stackframe. Set this
flag when an error is encountered. If this flag is set, terminate
the unwind. Also, let the unwinder return the status to the caller.
Reliability checks
==================
There are some kernel features and conditions that make a stack trace
unreliable. Callers may require the unwinder to detect these cases.
E.g., livepatch.
Introduce a new function called unwind_is_reliable() that will detect
these cases and return a boolean.
Introduce a new argument to unwind() called "need_reliable" so a caller
can tell unwind() that it requires a reliable stack trace. For such a
caller, any unreliability in the stack trace must be treated as a fatal
error and the unwind must be aborted.
Call unwind_is_reliable() from unwind_consume() like this:
if (frame->need_reliable && !unwind_is_reliable(frame)) {
frame->failed = true;
return false;
}
arch_stack_walk() passes "false" for need_reliable because its callers
don't care about reliability. arch_stack_walk() is used for debug and
test purposes.
Introduce arch_stack_walk_reliable() for ARM64. This works like
arch_stack_walk() except for two things:
- It passes "true" for need_reliable.
- It returns -EINVAL if unwind() aborts.
Introduce the first reliability check in unwind_is_reliable() - If
a return PC is not a valid kernel text address, consider the stack
trace unreliable. It could be some generated code.
Other reliability checks will be added in the future. Until all of the
checks are in place, arch_stack_walk_reliable() may not be used by
livepatch. But it may be used by debug and test code.
SYM_CODE check
==============
SYM_CODE functions do not follow normal calling conventions. They cannot
be unwound reliably using the frame pointer. Collect the address ranges
of these functions in a special section called "sym_code_functions".
In unwind_is_reliable(), check the return PC against these ranges. If a
match is found, then consider the stack trace unreliable. This is the
second reliability check introduced by this work.
Last stack frame
----------------
If a SYM_CODE function occurs in the very last frame in the stack trace,
then the stack trace is not considered unreliable. This is because there
is no more unwinding to do. Examples:
- EL0 exception stack traces end in the top level EL0 exception
handlers.
- All kernel thread stack traces end in ret_from_fork().
---
Changelog:
v8:
From Mark Rutland:
- Make the unwinder loop similar to other architectures.
- Keep details to within the unwinder functions and return a simple
boolean to the caller.
- Convert some of the current code that contains unwinder logic to
simply use arch_stack_walk(). I have converted all of them.
- Do not copy sym_code_functions[]. Just place it in rodata for now.
- Have the main loop check for termination conditions rather than
having unwind_frame() check for them. In other words, let
unwind_frame() assume that the fp is valid.
- Replace the big comment for SYM_CODE functions with a shorter
comment.
/*
* As SYM_CODE functions don't follow the usual calling
* conventions, we assume by default that any SYM_CODE function
* cannot be unwound reliably.
*
* Note that this includes:
*
* - Exception handlers and entry assembly
* - Trampoline assembly (e.g., ftrace, kprobes)
* - Hypervisor-related assembly
* - Hibernation-related assembly
* - CPU start-stop, suspend-resume assembly
* - Kernel relocation assembly
*/
v7:
The Mailer screwed up the threading on this. So, I have resent this
same series as version 8 with proper threading to avoid confusion.
v6:
From Mark Rutland:
- The per-frame reliability concept and flag are acceptable. But more
work is needed to make the per-frame checks more accurate and more
complete. E.g., some code reorg is being worked on that will help.
I have now removed the frame->reliable flag and deleted the whole
concept of per-frame status. This is orthogonal to this patch series.
Instead, I have improved the unwinder to return proper return codes
so a caller can take appropriate action without needing per-frame
status.
- Remove the mention of PLTs and update the comment.
I have replaced the comment above the call to __kernel_text_address()
with the comment suggested by Mark Rutland.
Other comments:
- Other comments on the per-frame stuff are not relevant because
that approach is not there anymore.
v5:
From Keiya Nobuta:
- The term blacklist(ed) is not to be used anymore. I have changed it
to unreliable. So, the function unwinder_blacklisted() has been
changed to unwinder_is_unreliable().
From Mark Brown:
- Add a comment for the "reliable" flag in struct stackframe. The
reliability attribute is not complete until all the checks are
in place. Added a comment above struct stackframe.
- Include some of the comments in the cover letter in the actual
code so that we can compare it with the reliable stack trace
requirements document for completeness. I have added a comment:
- above unwinder_is_unreliable() that lists the requirements
that are addressed by the function.
- above the __kernel_text_address() call about all the cases
the call covers.
v4:
From Mark Brown:
- I was checking the return PC with __kernel_text_address() before
the Function Graph trace handling. Mark Brown felt that all the
reliability checks should be performed on the original return PC
once that is obtained. So, I have moved all the reliability checks
to after the Function Graph Trace handling code in the unwinder.
Basically, the unwinder should perform PC translations first (for
rhe return trampoline for Function Graph Tracing, Kretprobes, etc).
Then, the reliability checks should be applied to the resulting
PC.
- Mark said to improve the naming of the new functions so they don't
collide with existing ones. I have used a prefix "unwinder_" for
all the new functions.
From Josh Poimboeuf:
- In the error scenarios in the unwinder, the reliable flag in the
stack frame should be set. Implemented this.
- Some of the other comments are not relevant to the new code as
I have taken a different approach in the new code. That is why
I have not made those changes. E.g., Ard wanted me to add the
"const" keyword to the global section array. That array does not
exist in v4. Similarly, Mark Brown said to use ARRAY_SIZE() for
the same array in a for loop.
Other changes:
- Add a new definition for SYM_CODE_END() that adds the address
range of the function to a special section called
"sym_code_functions".
- Include the new section under initdata in vmlinux.lds.S.
- Define an early_initcall() to copy the contents of the
"sym_code_functions" section to an array by the same name.
- Define a function unwinder_blacklisted() that compares a return
PC against sym_code_sections[]. If there is a match, mark the
stack trace unreliable. Call this from unwind_frame().
v3:
- Implemented a sym_code_ranges[] array to contains sections bounds
for text sections that contain SYM_CODE_*() functions. The unwinder
checks each return PC against the sections. If it falls in any of
the sections, the stack trace is marked unreliable.
- Moved SYM_CODE functions from .text and .init.text into a new
text section called ".code.text". Added this section to
vmlinux.lds.S and sym_code_ranges[].
- Fixed the logic in the unwinder that handles Function Graph
Tracer return trampoline.
- Removed all the previous code that handles:
- ftrace entry code for traced function
- special_functions[] array that lists individual functions
- kretprobe_trampoline() special case
v2
- Removed the terminating entry { 0, 0 } in special_functions[]
and replaced it with the idiom { /* sentinel */ }.
- Change the ftrace trampoline entry ftrace_graph_call in
special_functions[] to ftrace_call + 4 and added explanatory
comments.
- Unnested #ifdefs in special_functions[] for FTRACE.
v1
- Define a bool field in struct stackframe. This will indicate if
a stack trace is reliable.
- Implement a special_functions[] array that will be populated
with special functions in which the stack trace is considered
unreliable.
- Using kallsyms_lookup(), get the address ranges for the special
functions and record them.
- Implement an is_reliable_function(pc). This function will check
if a given return PC falls in any of the special functions. If
it does, the stack trace is unreliable.
- Implement check_reliability() function that will check if a
stack frame is reliable. Call is_reliable_function() from
check_reliability().
- Before a return PC is checked against special_funtions[], it
must be validates as a proper kernel text address. Call
__kernel_text_address() from check_reliability().
- Finally, call check_reliability() from unwind_frame() for
each stack frame.
- Add EL1 exception handlers to special_functions[].
el1_sync();
el1_irq();
el1_error();
el1_sync_invalid();
el1_irq_invalid();
el1_fiq_invalid();
el1_error_invalid();
- The above functions are currently defined as LOCAL symbols.
Make them global so that they can be referenced from the
unwinder code.
- Add FTRACE trampolines to special_functions[]:
ftrace_graph_call()
ftrace_graph_caller()
return_to_handler()
- Add the kretprobe trampoline to special functions[]:
kretprobe_trampoline()
Previous versions and discussion
================================
v7: Mailer screwed up the threading. Sent the same as v8 with proper threading.
v6: https://lore.kernel.org/linux-arm-kernel/20210630223356.58714-1-madvenka@linux.microsoft.com/
v5: https://lore.kernel.org/linux-arm-kernel/20210526214917.20099-1-madvenka@linux.microsoft.com/
v4: https://lore.kernel.org/linux-arm-kernel/20210516040018.128105-1-madvenka@linux.microsoft.com/
v3: https://lore.kernel.org/linux-arm-kernel/20210503173615.21576-1-madvenka@linux.microsoft.com/
v2: https://lore.kernel.org/linux-arm-kernel/20210405204313.21346-1-madvenka@linux.microsoft.com/
v1: https://lore.kernel.org/linux-arm-kernel/20210330190955.13707-1-madvenka@linux.microsoft.com/
Madhavan T. Venkataraman (4):
arm64: Make all stack walking functions use arch_stack_walk()
arm64: Reorganize the unwinder code for better consistency and
maintenance
arm64: Introduce stack trace reliability checks in the unwinder
arm64: Create a list of SYM_CODE functions, check return PC against
list
arch/arm64/include/asm/linkage.h | 12 ++
arch/arm64/include/asm/sections.h | 1 +
arch/arm64/include/asm/stacktrace.h | 16 +-
arch/arm64/kernel/perf_callchain.c | 5 +-
arch/arm64/kernel/process.c | 39 ++--
arch/arm64/kernel/return_address.c | 6 +-
arch/arm64/kernel/stacktrace.c | 291 ++++++++++++++++++++--------
arch/arm64/kernel/time.c | 22 ++-
arch/arm64/kernel/vmlinux.lds.S | 10 +
9 files changed, 277 insertions(+), 125 deletions(-)
base-commit: 36a21d51725af2ce0700c6ebcb6b9594aac658a6
--
2.25.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: "Madhavan T. Venkataraman" <redacted>
Currently, there are multiple functions in ARM64 code that walk the
stack using start_backtrace() and unwind_frame(). Convert all of
them to use arch_stack_walk(). This makes maintenance easier.
Here is the list of functions:
perf_callchain_kernel()
get_wchan()
return_address()
dump_backtrace()
profile_pc()
Signed-off-by: Madhavan T. Venkataraman <redacted>
---
arch/arm64/include/asm/stacktrace.h | 3 ---
arch/arm64/kernel/perf_callchain.c | 5 +---
arch/arm64/kernel/process.c | 39 ++++++++++++++++++-----------
arch/arm64/kernel/return_address.c | 6 +----
arch/arm64/kernel/stacktrace.c | 38 +++-------------------------
arch/arm64/kernel/time.c | 22 +++++++++-------
6 files changed, 43 insertions(+), 70 deletions(-)
@@ -147,15 +147,12 @@ static bool callchain_trace(void *data, unsigned long pc)voidperf_callchain_kernel(structperf_callchain_entry_ctx*entry,structpt_regs*regs){-structstackframeframe;-if(perf_guest_cbs&&perf_guest_cbs->is_in_guest()){/* We don't support guest os callchain now */return;}-start_backtrace(&frame,regs->regs[29],regs->pc);-walk_stackframe(current,&frame,callchain_trace,entry);+arch_stack_walk(callchain_trace,entry,current,regs);}unsignedlongperf_instruction_pointer(structpt_regs*regs)
From: Mark Rutland <mark.rutland@arm.com> Date: 2021-08-24 13:13:53
On Thu, Aug 12, 2021 at 02:06:00PM -0500, madvenka@linux.microsoft.com wrote:
From: "Madhavan T. Venkataraman" <redacted>
Currently, there are multiple functions in ARM64 code that walk the
stack using start_backtrace() and unwind_frame(). Convert all of
them to use arch_stack_walk(). This makes maintenance easier.
It would be good to split this into a series of patches as Mark Brown
suggested in v7.
Here is the list of functions:
perf_callchain_kernel()
get_wchan()
return_address()
dump_backtrace()
profile_pc()
Note that arch_stack_walk() depends on CONFIG_STACKTRACE (which is not in
defconfig), so we'll need to reorganise things such that it's always defined,
or factor out the core of that function and add a wrapper such that we
can always use it.
@@ -147,15 +147,12 @@ static bool callchain_trace(void *data, unsigned long pc)voidperf_callchain_kernel(structperf_callchain_entry_ctx*entry,structpt_regs*regs){-structstackframeframe;-if(perf_guest_cbs&&perf_guest_cbs->is_in_guest()){/* We don't support guest os callchain now */return;}-start_backtrace(&frame,regs->regs[29],regs->pc);-walk_stackframe(current,&frame,callchain_trace,entry);+arch_stack_walk(callchain_trace,entry,current,regs);}
We can also update callchain_trace take the return value of
perf_callchain_store into acount, e.g.
| static bool callchain_trace(void *data, unsigned long pc)
| {
| struct perf_callchain_entry_ctx *entry = data;
| return perf_callchain_store(entry, pc) == 0;
| }
quoted hunk
unsigned long perf_instruction_pointer(struct pt_regs *regs)
This will terminate one entry earlier than the old logic since we used
to use a post-increment (testing the prior value), and now we're
effectively using a pre-decrement (testing the new value).
I don't think that matters all that much in practice, but it might be
best to keep the original logic, e.g. initialize `count` to 0 and here
do:
return wchan_info->count++ < 16;
quoted hunk
+
unsigned long get_wchan(struct task_struct *p)
{
- struct stackframe frame;
- unsigned long stack_page, ret = 0;
- int count = 0;
+ unsigned long stack_page;
+ struct wchan_info wchan_info;
+
if (!p || p == current || task_is_running(p))
return 0;
@@ -556,20 +573,12 @@ unsigned long get_wchan(struct task_struct *p) if (!stack_page) return 0;- start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p));+ wchan_info.pc = 0;+ wchan_info.count = 16;+ arch_stack_walk(get_wchan_cb, &wchan_info, p, NULL);- do {- if (unwind_frame(p, &frame))- goto out;- if (!in_sched_functions(frame.pc)) {- ret = frame.pc;- goto out;- }- } while (count++ < 16);--out: put_task_stack(p);- return ret;+ return wchan_info.pc; }
Other than the comment above, this looks good to me.
Nor that arch_stack_walk() will start with it's caller, so
return_address() will be included in the trace where it wasn't
previously, which implies we need to skip an additional level.
That said, I'm not entirely sure why we need to skip 2 levels today; it
might be worth checking that's correct.
We should also mark return_address() as noinline to avoid surprises with
LTO.
We can simplifiy this to:
if (regs && user_mode(regs))
return;
quoted hunk
if (!tsk)
@@ -176,36 +174,8 @@ void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, if (!try_get_task_stack(tsk)) return;- if (tsk == current) {- start_backtrace(&frame,- (unsigned long)__builtin_frame_address(0),- (unsigned long)dump_backtrace);- } else {- /*- * task blocked in __switch_to- */- start_backtrace(&frame,- thread_saved_fp(tsk),- thread_saved_pc(tsk));- }- printk("%sCall trace:\n", loglvl);- do {- /* skip until specified stack frame */- if (!skip) {- dump_backtrace_entry(frame.pc, loglvl);- } else if (frame.fp == regs->regs[29]) {- skip = 0;- /*- * Mostly, this is the case where this function is- * called in panic/abort. As exception handler's- * stack frame does not contain the corresponding pc- * at which an exception has taken place, use regs->pc- * instead.- */- dump_backtrace_entry(regs->pc, loglvl);- }- } while (!unwind_frame(tsk, &frame));+ arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
It turns out we currently need this skipping to get the balance the
ftrace call stack, and arch_stack_walk() doesn't currently do the right
thing when starting from regs. That balancing isn't quite right, and
will be wrong in some case when unwinding across exception boundaries;
we could implement HAVE_FUNCTION_GRAPH_RET_ADDR_PTR using the FP to
solve that.
From: Madhavan T. Venkataraman <hidden> Date: 2021-08-24 17:25:00
Thanks for the review. Responses inline...
On 8/24/21 8:13 AM, Mark Rutland wrote:
On Thu, Aug 12, 2021 at 02:06:00PM -0500, madvenka@linux.microsoft.com wrote:
quoted
From: "Madhavan T. Venkataraman" <redacted>
Currently, there are multiple functions in ARM64 code that walk the
stack using start_backtrace() and unwind_frame(). Convert all of
them to use arch_stack_walk(). This makes maintenance easier.
It would be good to split this into a series of patches as Mark Brown
suggested in v7.
Will do.
quoted
Here is the list of functions:
perf_callchain_kernel()
get_wchan()
return_address()
dump_backtrace()
profile_pc()
Note that arch_stack_walk() depends on CONFIG_STACKTRACE (which is not in
defconfig), so we'll need to reorganise things such that it's always defined,
or factor out the core of that function and add a wrapper such that we
can always use it.
I will include CONFIG_STACKTRACE in defconfig, if that is OK with you and
Mark Brown.
@@ -147,15 +147,12 @@ static bool callchain_trace(void *data, unsigned long pc)voidperf_callchain_kernel(structperf_callchain_entry_ctx*entry,structpt_regs*regs){-structstackframeframe;-if(perf_guest_cbs&&perf_guest_cbs->is_in_guest()){/* We don't support guest os callchain now */return;}-start_backtrace(&frame,regs->regs[29],regs->pc);-walk_stackframe(current,&frame,callchain_trace,entry);+arch_stack_walk(callchain_trace,entry,current,regs);}
We can also update callchain_trace take the return value of
perf_callchain_store into acount, e.g.
| static bool callchain_trace(void *data, unsigned long pc)
| {
| struct perf_callchain_entry_ctx *entry = data;
| return perf_callchain_store(entry, pc) == 0;
| }
OK.
quoted
unsigned long perf_instruction_pointer(struct pt_regs *regs)
This will terminate one entry earlier than the old logic since we used
to use a post-increment (testing the prior value), and now we're
effectively using a pre-decrement (testing the new value).
I don't think that matters all that much in practice, but it might be
best to keep the original logic, e.g. initialize `count` to 0 and here
do:
return wchan_info->count++ < 16;
The reason I did it this way is that with the old logic the actual limit
implemented is 17 instead of 16. That seemed odd. But I could do it the
way you have suggested.
quoted
+
unsigned long get_wchan(struct task_struct *p)
{
- struct stackframe frame;
- unsigned long stack_page, ret = 0;
- int count = 0;
+ unsigned long stack_page;
+ struct wchan_info wchan_info;
+
if (!p || p == current || task_is_running(p))
return 0;
@@ -556,20 +573,12 @@ unsigned long get_wchan(struct task_struct *p) if (!stack_page) return 0;- start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p));+ wchan_info.pc = 0;+ wchan_info.count = 16;+ arch_stack_walk(get_wchan_cb, &wchan_info, p, NULL);- do {- if (unwind_frame(p, &frame))- goto out;- if (!in_sched_functions(frame.pc)) {- ret = frame.pc;- goto out;- }- } while (count++ < 16);--out: put_task_stack(p);- return ret;+ return wchan_info.pc; }
Other than the comment above, this looks good to me.
Nor that arch_stack_walk() will start with it's caller, so
return_address() will be included in the trace where it wasn't
previously, which implies we need to skip an additional level.
You are correct. I will fix this. Thanks for catching this.
That said, I'm not entirely sure why we need to skip 2 levels today; it
might be worth checking that's correct.
AFAICT, return_address() acts like builtin_return_address(). That is, it
returns the address of the caller. If func() calls return_address(),
func() wants its caller's address. So, return_address() and func() need to
be skipped.
I will change it to skip 3 levels instead of 2.
We should also mark return_address() as noinline to avoid surprises with
LTO.
We can simplifiy this to:
if (regs && user_mode(regs))
return;
OK.
quoted
if (!tsk)
@@ -176,36 +174,8 @@ void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, if (!try_get_task_stack(tsk)) return;- if (tsk == current) {- start_backtrace(&frame,- (unsigned long)__builtin_frame_address(0),- (unsigned long)dump_backtrace);- } else {- /*- * task blocked in __switch_to- */- start_backtrace(&frame,- thread_saved_fp(tsk),- thread_saved_pc(tsk));- }- printk("%sCall trace:\n", loglvl);- do {- /* skip until specified stack frame */- if (!skip) {- dump_backtrace_entry(frame.pc, loglvl);- } else if (frame.fp == regs->regs[29]) {- skip = 0;- /*- * Mostly, this is the case where this function is- * called in panic/abort. As exception handler's- * stack frame does not contain the corresponding pc- * at which an exception has taken place, use regs->pc- * instead.- */- dump_backtrace_entry(regs->pc, loglvl);- }- } while (!unwind_frame(tsk, &frame));+ arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
It turns out we currently need this skipping to get the balance the
ftrace call stack, and arch_stack_walk() doesn't currently do the right
thing when starting from regs. That balancing isn't quite right, and
will be wrong in some case when unwinding across exception boundaries;
we could implement HAVE_FUNCTION_GRAPH_RET_ADDR_PTR using the FP to
solve that.
I am not sure that I completely understand. So, I will study this and get back
to you with any questions.
Nor that arch_stack_walk() will start with it's caller, so
return_address() will be included in the trace where it wasn't
previously, which implies we need to skip an additional level.
You are correct. I will fix this. Thanks for catching this.
quoted
That said, I'm not entirely sure why we need to skip 2 levels today; it
might be worth checking that's correct.
AFAICT, return_address() acts like builtin_return_address(). That is, it
returns the address of the caller. If func() calls return_address(),
func() wants its caller's address. So, return_address() and func() need to
be skipped.
I will change it to skip 3 levels instead of 2.
Actually, I take that back. I remember now. return_address() used to start
with PC=return_address(). That is, it used to start with itself. arch_stack_walk()
starts with its caller which, in this case, is return_address(). So, I don't need
to change anything.
Do you agree?
Madhavan
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mark Brown <broonie@kernel.org> Date: 2021-08-24 17:42:25
On Tue, Aug 24, 2021 at 12:21:28PM -0500, Madhavan T. Venkataraman wrote:
On 8/24/21 8:13 AM, Mark Rutland wrote:
quoted
On Thu, Aug 12, 2021 at 02:06:00PM -0500, madvenka@linux.microsoft.com wrote:
quoted
Note that arch_stack_walk() depends on CONFIG_STACKTRACE (which is not in
defconfig), so we'll need to reorganise things such that it's always defined,
or factor out the core of that function and add a wrapper such that we
can always use it.
I will include CONFIG_STACKTRACE in defconfig, if that is OK with you and
Mark Brown.
That might be separately useful but it doesn't address the issue, if
something is optional we need to handle the case where that option is
disabled. It'll need to be one of the two options Mark Rutland
mentioned above.
From: Madhavan T. Venkataraman <hidden> Date: 2021-08-24 17:42:57
On 8/24/21 12:38 PM, Mark Brown wrote:
On Tue, Aug 24, 2021 at 12:21:28PM -0500, Madhavan T. Venkataraman wrote:
quoted
On 8/24/21 8:13 AM, Mark Rutland wrote:
quoted
On Thu, Aug 12, 2021 at 02:06:00PM -0500, madvenka@linux.microsoft.com wrote:
quoted
quoted
Note that arch_stack_walk() depends on CONFIG_STACKTRACE (which is not in
defconfig), so we'll need to reorganise things such that it's always defined,
or factor out the core of that function and add a wrapper such that we
can always use it.
quoted
I will include CONFIG_STACKTRACE in defconfig, if that is OK with you and
Mark Brown.
That might be separately useful but it doesn't address the issue, if
something is optional we need to handle the case where that option is
disabled. It'll need to be one of the two options Mark Rutland
mentioned above.
From: Madhavan T. Venkataraman <hidden> Date: 2021-08-26 04:52:50
Hi Mark Rutland, Mark Brown,
Do you have any comments on the reliability part of the patch series?
Madhavan
On 8/24/21 8:13 AM, Mark Rutland wrote:
On Thu, Aug 12, 2021 at 02:06:00PM -0500, madvenka@linux.microsoft.com wrote:
quoted
From: "Madhavan T. Venkataraman" <redacted>
Currently, there are multiple functions in ARM64 code that walk the
stack using start_backtrace() and unwind_frame(). Convert all of
them to use arch_stack_walk(). This makes maintenance easier.
It would be good to split this into a series of patches as Mark Brown
suggested in v7.
quoted
Here is the list of functions:
perf_callchain_kernel()
get_wchan()
return_address()
dump_backtrace()
profile_pc()
Note that arch_stack_walk() depends on CONFIG_STACKTRACE (which is not in
defconfig), so we'll need to reorganise things such that it's always defined,
or factor out the core of that function and add a wrapper such that we
can always use it.
@@ -147,15 +147,12 @@ static bool callchain_trace(void *data, unsigned long pc)voidperf_callchain_kernel(structperf_callchain_entry_ctx*entry,structpt_regs*regs){-structstackframeframe;-if(perf_guest_cbs&&perf_guest_cbs->is_in_guest()){/* We don't support guest os callchain now */return;}-start_backtrace(&frame,regs->regs[29],regs->pc);-walk_stackframe(current,&frame,callchain_trace,entry);+arch_stack_walk(callchain_trace,entry,current,regs);}
We can also update callchain_trace take the return value of
perf_callchain_store into acount, e.g.
| static bool callchain_trace(void *data, unsigned long pc)
| {
| struct perf_callchain_entry_ctx *entry = data;
| return perf_callchain_store(entry, pc) == 0;
| }
quoted
unsigned long perf_instruction_pointer(struct pt_regs *regs)
This will terminate one entry earlier than the old logic since we used
to use a post-increment (testing the prior value), and now we're
effectively using a pre-decrement (testing the new value).
I don't think that matters all that much in practice, but it might be
best to keep the original logic, e.g. initialize `count` to 0 and here
do:
return wchan_info->count++ < 16;
quoted
+
unsigned long get_wchan(struct task_struct *p)
{
- struct stackframe frame;
- unsigned long stack_page, ret = 0;
- int count = 0;
+ unsigned long stack_page;
+ struct wchan_info wchan_info;
+
if (!p || p == current || task_is_running(p))
return 0;
@@ -556,20 +573,12 @@ unsigned long get_wchan(struct task_struct *p) if (!stack_page) return 0;- start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p));+ wchan_info.pc = 0;+ wchan_info.count = 16;+ arch_stack_walk(get_wchan_cb, &wchan_info, p, NULL);- do {- if (unwind_frame(p, &frame))- goto out;- if (!in_sched_functions(frame.pc)) {- ret = frame.pc;- goto out;- }- } while (count++ < 16);--out: put_task_stack(p);- return ret;+ return wchan_info.pc; }
Other than the comment above, this looks good to me.
Nor that arch_stack_walk() will start with it's caller, so
return_address() will be included in the trace where it wasn't
previously, which implies we need to skip an additional level.
That said, I'm not entirely sure why we need to skip 2 levels today; it
might be worth checking that's correct.
We should also mark return_address() as noinline to avoid surprises with
LTO.
We can simplifiy this to:
if (regs && user_mode(regs))
return;
quoted
if (!tsk)
@@ -176,36 +174,8 @@ void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, if (!try_get_task_stack(tsk)) return;- if (tsk == current) {- start_backtrace(&frame,- (unsigned long)__builtin_frame_address(0),- (unsigned long)dump_backtrace);- } else {- /*- * task blocked in __switch_to- */- start_backtrace(&frame,- thread_saved_fp(tsk),- thread_saved_pc(tsk));- }- printk("%sCall trace:\n", loglvl);- do {- /* skip until specified stack frame */- if (!skip) {- dump_backtrace_entry(frame.pc, loglvl);- } else if (frame.fp == regs->regs[29]) {- skip = 0;- /*- * Mostly, this is the case where this function is- * called in panic/abort. As exception handler's- * stack frame does not contain the corresponding pc- * at which an exception has taken place, use regs->pc- * instead.- */- dump_backtrace_entry(regs->pc, loglvl);- }- } while (!unwind_frame(tsk, &frame));+ arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
It turns out we currently need this skipping to get the balance the
ftrace call stack, and arch_stack_walk() doesn't currently do the right
thing when starting from regs. That balancing isn't quite right, and
will be wrong in some case when unwinding across exception boundaries;
we could implement HAVE_FUNCTION_GRAPH_RET_ADDR_PTR using the FP to
solve that.
From: Madhavan T. Venkataraman <hidden> Date: 2021-10-09 23:41:51
On 8/24/21 8:13 AM, Mark Rutland wrote:
On Thu, Aug 12, 2021 at 02:06:00PM -0500, madvenka@linux.microsoft.com wrote:
quoted
From: "Madhavan T. Venkataraman" <redacted>
Currently, there are multiple functions in ARM64 code that walk the
stack using start_backtrace() and unwind_frame(). Convert all of
them to use arch_stack_walk(). This makes maintenance easier.
It would be good to split this into a series of patches as Mark Brown
suggested in v7.
quoted
Here is the list of functions:
perf_callchain_kernel()
get_wchan()
return_address()
dump_backtrace()
profile_pc()
Note that arch_stack_walk() depends on CONFIG_STACKTRACE (which is not in
defconfig), so we'll need to reorganise things such that it's always defined,
or factor out the core of that function and add a wrapper such that we
can always use it.
@@ -147,15 +147,12 @@ static bool callchain_trace(void *data, unsigned long pc)voidperf_callchain_kernel(structperf_callchain_entry_ctx*entry,structpt_regs*regs){-structstackframeframe;-if(perf_guest_cbs&&perf_guest_cbs->is_in_guest()){/* We don't support guest os callchain now */return;}-start_backtrace(&frame,regs->regs[29],regs->pc);-walk_stackframe(current,&frame,callchain_trace,entry);+arch_stack_walk(callchain_trace,entry,current,regs);}
We can also update callchain_trace take the return value of
perf_callchain_store into acount, e.g.
| static bool callchain_trace(void *data, unsigned long pc)
| {
| struct perf_callchain_entry_ctx *entry = data;
| return perf_callchain_store(entry, pc) == 0;
| }
quoted
unsigned long perf_instruction_pointer(struct pt_regs *regs)
This will terminate one entry earlier than the old logic since we used
to use a post-increment (testing the prior value), and now we're
effectively using a pre-decrement (testing the new value).
I don't think that matters all that much in practice, but it might be
best to keep the original logic, e.g. initialize `count` to 0 and here
do:
return wchan_info->count++ < 16;
quoted
+
unsigned long get_wchan(struct task_struct *p)
{
- struct stackframe frame;
- unsigned long stack_page, ret = 0;
- int count = 0;
+ unsigned long stack_page;
+ struct wchan_info wchan_info;
+
if (!p || p == current || task_is_running(p))
return 0;
@@ -556,20 +573,12 @@ unsigned long get_wchan(struct task_struct *p) if (!stack_page) return 0;- start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p));+ wchan_info.pc = 0;+ wchan_info.count = 16;+ arch_stack_walk(get_wchan_cb, &wchan_info, p, NULL);- do {- if (unwind_frame(p, &frame))- goto out;- if (!in_sched_functions(frame.pc)) {- ret = frame.pc;- goto out;- }- } while (count++ < 16);--out: put_task_stack(p);- return ret;+ return wchan_info.pc; }
Other than the comment above, this looks good to me.
Nor that arch_stack_walk() will start with it's caller, so
return_address() will be included in the trace where it wasn't
previously, which implies we need to skip an additional level.
That said, I'm not entirely sure why we need to skip 2 levels today; it
might be worth checking that's correct.
We should also mark return_address() as noinline to avoid surprises with
LTO.
We can simplifiy this to:
if (regs && user_mode(regs))
return;
quoted
if (!tsk)
@@ -176,36 +174,8 @@ void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, if (!try_get_task_stack(tsk)) return;- if (tsk == current) {- start_backtrace(&frame,- (unsigned long)__builtin_frame_address(0),- (unsigned long)dump_backtrace);- } else {- /*- * task blocked in __switch_to- */- start_backtrace(&frame,- thread_saved_fp(tsk),- thread_saved_pc(tsk));- }- printk("%sCall trace:\n", loglvl);- do {- /* skip until specified stack frame */- if (!skip) {- dump_backtrace_entry(frame.pc, loglvl);- } else if (frame.fp == regs->regs[29]) {- skip = 0;- /*- * Mostly, this is the case where this function is- * called in panic/abort. As exception handler's- * stack frame does not contain the corresponding pc- * at which an exception has taken place, use regs->pc- * instead.- */- dump_backtrace_entry(regs->pc, loglvl);- }- } while (!unwind_frame(tsk, &frame));+ arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
It turns out we currently need this skipping to get the balance the
ftrace call stack, and arch_stack_walk() doesn't currently do the right
thing when starting from regs. That balancing isn't quite right, and
will be wrong in some case when unwinding across exception boundaries;
we could implement HAVE_FUNCTION_GRAPH_RET_ADDR_PTR using the FP to
solve that.
Hi Mark,
It seems that the behavior is the same in the old and new code. Do you
agree?
In the old code when regs is used, the stack trace starts from regs->regs[29]
and the first PC displayed is regs->pc. arch_stack_walk() does the same thing.
Can you elaborate what change you want me to make here?
Madhavan
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: "Madhavan T. Venkataraman" <redacted>
Renaming of unwinder functions
==============================
Rename unwinder functions to unwind_*() similar to other architectures
for naming consistency. More on this below.
unwind function attributes
==========================
Mark all of the unwind_*() functions with notrace so they cannot be ftraced
and NOKPROBE_SYMBOL() so they cannot be kprobed. Ftrace and Kprobe code
can call the unwinder.
start_backtrace()
=================
start_backtrace() is only called by arch_stack_walk(). Make it static.
Rename start_backtrace() to unwind_start() for naming consistency.
unwind_frame()
==============
Rename this to unwind_next() for naming consistency.
Replace walk_stackframe() with unwind()
=======================================
walk_stackframe() contains the unwinder loop that walks the stack
frames. Currently, start_backtrace() and walk_stackframe() are called
separately. They should be combined in the same function. Also, the
loop in walk_stackframe() should be simplified and should look like
the unwind loops in other architectures such as X86 and S390.
Remove walk_stackframe(). Define a new function called "unwind()" in
its place. Define the following unwinder loop:
unwind_start(&frame, task, fp, pc);
while (unwind_consume(&frame, consume_entry, cookie))
unwind_next(&frame);
return !unwind_failed(&frame);
unwind_start()
Same as the original start_backtrace().
unwind_consume()
This is a new function that calls the callback function to
consume the PC in a stackframe. Do it this way so that checks
can be performed before and after the callback to determine
whether the unwind should continue or terminate.
unwind_next()
Same as the original unwind_frame() except for two things:
- the stack trace termination check has been moved from
here to unwind_consume(). So, unwind_next() is always
called on a valid fp.
- unwind_frame() used to return an error value. This
function does not return anything.
unwind_failed()
Return a boolean to indicate if the stack trace completed
successfully or failed. arch_stack_walk() ignores the return
value. But arch_stack_walk_reliable() in the future will look
at the return value.
Unwind status
=============
Introduce a new flag called "failed" in struct stackframe. unwind_next()
and unwind_consume() will set this flag when an error is encountered and
unwind_consume() will check this flag. This is in keeping with other
architectures.
The failed flags is accessed via the helper unwind_failed().
Signed-off-by: Madhavan T. Venkataraman <redacted>
---
arch/arm64/include/asm/stacktrace.h | 9 +-
arch/arm64/kernel/stacktrace.c | 145 ++++++++++++++++++----------
2 files changed, 99 insertions(+), 55 deletions(-)
@@ -186,25 +178,74 @@ void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)barrier();}+staticboolnotraceunwind_consume(structstackframe*frame,+stack_trace_consume_fnconsume_entry,+void*cookie)+{+if(frame->failed){+/* PC is suspect. Cannot consume it. */+returnfalse;+}++if(!consume_entry(cookie,frame->pc)){+/* Caller terminated the unwind. */+frame->failed=true;+returnfalse;+}++if(frame->fp==(unsignedlong)task_pt_regs(frame->task)->stackframe){+/* Final frame; nothing to unwind */+returnfalse;+}+returntrue;+}++NOKPROBE_SYMBOL(unwind_consume);++staticinlineboolunwind_failed(structstackframe*frame)+{+returnframe->failed;+}++/* Core unwind function */+staticboolnotraceunwind(stack_trace_consume_fnconsume_entry,void*cookie,+structtask_struct*task,+unsignedlongfp,unsignedlongpc)+{+structstackframeframe;++unwind_start(&frame,task,fp,pc);+while(unwind_consume(&frame,consume_entry,cookie))+unwind_next(&frame);+return!unwind_failed(&frame);+}++NOKPROBE_SYMBOL(unwind);+#ifdef CONFIG_STACKTRACEnoinlinenotracevoidarch_stack_walk(stack_trace_consume_fnconsume_entry,void*cookie,structtask_struct*task,structpt_regs*regs){-structstackframeframe;+unsignedlongfp,pc;++if(!task)+task=current;-if(regs)-start_backtrace(&frame,regs->regs[29],regs->pc);-elseif(task==current)-start_backtrace(&frame,-(unsignedlong)__builtin_frame_address(1),-(unsignedlong)__builtin_return_address(0));-else-start_backtrace(&frame,thread_saved_fp(task),-thread_saved_pc(task));--walk_stackframe(task,&frame,consume_entry,cookie);+if(regs){+fp=regs->regs[29];+pc=regs->pc;+}elseif(task==current){+/* Skip arch_stack_walk() in the stack trace. */+fp=(unsignedlong)__builtin_frame_address(1);+pc=(unsignedlong)__builtin_return_address(0);+}else{+/* Caller guarantees that the task is not running. */+fp=thread_saved_fp(task);+pc=thread_saved_pc(task);+}+unwind(consume_entry,cookie,task,fp,pc);}#endif
--
2.25.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mark Brown <broonie@kernel.org> Date: 2021-08-26 16:24:45
On Thu, Aug 12, 2021 at 02:06:01PM -0500, madvenka@linux.microsoft.com wrote:
Renaming of unwinder functions
==============================
Rename unwinder functions to unwind_*() similar to other architectures
for naming consistency. More on this below.
This feels like it could probably do with splitting up a bit for
reviewability, several of these headers you've got in the commit
logs look like they could be separate commits. Splitting things
up does help with reviewability, having only one change to keep
in mind at once is a lot less cognative load.
Replace walk_stackframe() with unwind()
=======================================
walk_stackframe() contains the unwinder loop that walks the stack
frames. Currently, start_backtrace() and walk_stackframe() are called
separately. They should be combined in the same function. Also, the
loop in walk_stackframe() should be simplified and should look like
the unwind loops in other architectures such as X86 and S390.
From: Madhavan T. Venkataraman <hidden> Date: 2021-08-26 23:19:12
On 8/26/21 10:46 AM, Mark Brown wrote:
On Thu, Aug 12, 2021 at 02:06:01PM -0500, madvenka@linux.microsoft.com wrote:
quoted
Renaming of unwinder functions
==============================
quoted
Rename unwinder functions to unwind_*() similar to other architectures
for naming consistency. More on this below.
This feels like it could probably do with splitting up a bit for
reviewability, several of these headers you've got in the commit
logs look like they could be separate commits. Splitting things
up does help with reviewability, having only one change to keep
in mind at once is a lot less cognative load.
quoted
Replace walk_stackframe() with unwind()
=======================================
walk_stackframe() contains the unwinder loop that walks the stack
frames. Currently, start_backtrace() and walk_stackframe() are called
separately. They should be combined in the same function. Also, the
loop in walk_stackframe() should be simplified and should look like
the unwind loops in other architectures such as X86 and S390.
This definitely seems like a separate change.
OK. I will take a look at splitting the patch.
I am also requesting a review of the sym_code special section approach.
I know that you have already approved it. I wanted one more vote. Then,
I can remove the "RFC" word from the title and then it will be just a
code review of the patch series.
Mark Rutland,
Do you also approve the idea of placing unreliable functions (from an unwind
perspective) in a special section and using that in the unwinder for
reliable stack trace?
Thanks.
Madhavan
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Mark Brown <broonie@kernel.org> Date: 2021-09-01 16:20:40
On Thu, Aug 26, 2021 at 06:19:07PM -0500, Madhavan T. Venkataraman wrote:
Mark Rutland,
Do you also approve the idea of placing unreliable functions (from an unwind
perspective) in a special section and using that in the unwinder for
reliable stack trace?
Rutland is on vacation for a couple of weeks so he's unlikely to reply
before the merge window is over I'm afraid.
From: Madhavan T. Venkataraman <hidden> Date: 2021-09-02 07:10:07
On 9/1/21 11:20 AM, Mark Brown wrote:
On Thu, Aug 26, 2021 at 06:19:07PM -0500, Madhavan T. Venkataraman wrote:
quoted
Mark Rutland,
quoted
Do you also approve the idea of placing unreliable functions (from an unwind
perspective) in a special section and using that in the unwinder for
reliable stack trace?
Rutland is on vacation for a couple of weeks so he's unlikely to reply
before the merge window is over I'm afraid.
OK. I am pretty sure he is fine with the special sections idea. So, I will
send out version 8 with the changes you requested and without the "RFC".
Thanks.
Madhavan
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: "Madhavan T. Venkataraman" <redacted>
There are some kernel features and conditions that make a stack trace
unreliable. Callers may require the unwinder to detect these cases.
E.g., livepatch.
Introduce a new function called unwind_is_reliable() that will detect
these cases and return a boolean.
Introduce a new argument to unwind() called "need_reliable" so a caller
can tell unwind() that it requires a reliable stack trace. For such a
caller, any unreliability in the stack trace must be treated as a fatal
error and the unwind must be aborted.
Call unwind_is_reliable() from unwind_consume() like this:
if (frame->need_reliable && !unwind_is_reliable(frame)) {
frame->failed = true;
return false;
}
In other words, if the return PC in the stackframe falls in unreliable code,
then it cannot be unwound reliably.
arch_stack_walk() will pass "false" for need_reliable because its callers
don't care about reliability. arch_stack_walk() is used for debug and
test purposes.
Introduce arch_stack_walk_reliable() for ARM64. This works like
arch_stack_walk() except for two things:
- It passes "true" for need_reliable.
- It returns -EINVAL if unwind() says that the stack trace is
unreliable.
Introduce the first reliability check in unwind_is_reliable() - If
a return PC is not a valid kernel text address, consider the stack
trace unreliable. It could be some generated code.
Other reliability checks will be added in the future. Until all of the
checks are in place, arch_stack_walk_reliable() may not be used by
livepatch. But it may be used by debug and test code.
Signed-off-by: Madhavan T. Venkataraman <redacted>
---
arch/arm64/include/asm/stacktrace.h | 4 ++
arch/arm64/kernel/stacktrace.c | 63 +++++++++++++++++++++++++++--
2 files changed, 63 insertions(+), 4 deletions(-)
@@ -197,6 +216,12 @@ static bool notrace unwind_consume(struct stackframe *frame,/* Final frame; nothing to unwind */returnfalse;}++if(frame->need_reliable&&!unwind_is_reliable(frame)){+/* Cannot unwind to the next frame reliably. */+frame->failed=true;+returnfalse;+}returntrue;}
@@ -245,7 +271,36 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,fp=thread_saved_fp(task);pc=thread_saved_pc(task);}-unwind(consume_entry,cookie,task,fp,pc);+unwind(consume_entry,cookie,task,fp,pc,false);+}++/*+*arch_stack_walk_reliable()maynotbeusedforlivepatchuntilallof+*thereliabilitychecksareinplaceinunwind_consume().However,+*debugandtestcodecanchoosetouseitevenifallthechecksarenot+*inplace.+*/+noinlineintnotracearch_stack_walk_reliable(stack_trace_consume_fnconsume_fn,+void*cookie,+structtask_struct*task)+{+unsignedlongfp,pc;++if(!task)+task=current;++if(task==current){+/* Skip arch_stack_walk_reliable() in the stack trace. */+fp=(unsignedlong)__builtin_frame_address(1);+pc=(unsignedlong)__builtin_return_address(0);+}else{+/* Caller guarantees that the task is not running. */+fp=thread_saved_fp(task);+pc=thread_saved_pc(task);+}+if(unwind(consume_fn,cookie,task,fp,pc,true))+return0;+return-EINVAL;}#endif
--
2.25.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
@@ -245,7 +271,36 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, fp = thread_saved_fp(task); pc = thread_saved_pc(task); }- unwind(consume_entry, cookie, task, fp, pc);+ unwind(consume_entry, cookie, task, fp, pc, false);+}++/*+ * arch_stack_walk_reliable() may not be used for livepatch until all of+ * the reliability checks are in place in unwind_consume(). However,+ * debug and test code can choose to use it even if all the checks are not+ * in place.+ */
I'm glad to see the long-awaited function :)
Does the above comment mean that this comment will be removed by
another patch series that about live patch enablement, instead of [PATCH 4/4]?
It seems to take time... But I start thinking about test code.
Thanks,
Keiya
+noinline int notrace arch_stack_walk_reliable(stack_trace_consume_fn consume_fn,
+ void *cookie,
+ struct task_struct *task)
+{
+ unsigned long fp, pc;
+
+ if (!task)
+ task = current;
+
+ if (task == current) {
+ /* Skip arch_stack_walk_reliable() in the stack trace. */
+ fp = (unsigned long)__builtin_frame_address(1);
+ pc = (unsigned long)__builtin_return_address(0);
+ } else {
+ /* Caller guarantees that the task is not running. */
+ fp = thread_saved_fp(task);
+ pc = thread_saved_pc(task);
+ }
+ if (unwind(consume_fn, cookie, task, fp, pc, true))
+ return 0;
+ return -EINVAL;
}
#endif
--
2.25.1
From: Madhavan T. Venkataraman <hidden> Date: 2021-08-24 12:19:57
On 8/24/21 12:55 AM, nobuta.keiya@fujitsu.com wrote:
Hi Madhavan,
quoted
@@ -245,7 +271,36 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, fp = thread_saved_fp(task); pc = thread_saved_pc(task); }- unwind(consume_entry, cookie, task, fp, pc);+ unwind(consume_entry, cookie, task, fp, pc, false);+}++/*+ * arch_stack_walk_reliable() may not be used for livepatch until all of+ * the reliability checks are in place in unwind_consume(). However,+ * debug and test code can choose to use it even if all the checks are not+ * in place.+ */
I'm glad to see the long-awaited function :)
Does the above comment mean that this comment will be removed by
another patch series that about live patch enablement, instead of [PATCH 4/4]?
It seems to take time... But I start thinking about test code.
Yes. This comment will be removed when livepatch will be enabled eventually.
So, AFAICT, there are 4 pieces that are needed:
- Reliable stack trace in the kernel. I am trying to address that with my patch
series.
- Mark Rutland's work for making patching safe on ARM64.
- Objtool (or alternative method) for stack validation.
- Suraj Jitindar Singh's patch for miscellaneous things needed to enable live patch.
Once all of these pieces are in place, livepatch can be enabled.
That said, arch_stack_walk_reliable() can be used for test and debug purposes anytime
once this patch series gets accepted.
Thanks.
Madhavan
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
@@ -245,7 +271,36 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, fp = thread_saved_fp(task); pc = thread_saved_pc(task); }- unwind(consume_entry, cookie, task, fp, pc);+ unwind(consume_entry, cookie, task, fp, pc, false); }++/*+ * arch_stack_walk_reliable() may not be used for livepatch until+all of+ * the reliability checks are in place in unwind_consume(). However,+ * debug and test code can choose to use it even if all the checks+are not+ * in place.+ */
I'm glad to see the long-awaited function :)
Does the above comment mean that this comment will be removed by
another patch series that about live patch enablement, instead of [PATCH 4/4]?
It seems to take time... But I start thinking about test code.
Yes. This comment will be removed when livepatch will be enabled eventually.
So, AFAICT, there are 4 pieces that are needed:
- Reliable stack trace in the kernel. I am trying to address that with my patch
series.
- Mark Rutland's work for making patching safe on ARM64.
- Objtool (or alternative method) for stack validation.
- Suraj Jitindar Singh's patch for miscellaneous things needed to enable live patch.
Once all of these pieces are in place, livepatch can be enabled.
That said, arch_stack_walk_reliable() can be used for test and debug purposes anytime once this patch series gets accepted.
Thanks.
Madhavan
From: Mark Brown <broonie@kernel.org> Date: 2021-08-26 16:24:52
On Thu, Aug 12, 2021 at 02:06:02PM -0500, madvenka@linux.microsoft.com wrote:
+ if (frame->need_reliable && !unwind_is_reliable(frame)) {
+ /* Cannot unwind to the next frame reliably. */
+ frame->failed = true;
+ return false;
+ }
This means we only collect reliability information in the case
where we're specifically doing a reliable stacktrace. For
example when printing stack traces on the console it might be
useful to print a ? or something if the frame is unreliable as a
hint to the reader that the information might be misleading.
Could we therefore change the flag here to a reliability one and
our need_reliable check so that we always run
unwind_is_reliable()?
I'm not sure if we need to abandon the trace on first error when
doing a reliable trace but I can see it's a bit safer so perhaps
better to do so. If we don't abandon then we don't require the
need_reliable check at all.
From: Madhavan T. Venkataraman <hidden> Date: 2021-08-26 23:31:31
On 8/26/21 10:57 AM, Mark Brown wrote:
On Thu, Aug 12, 2021 at 02:06:02PM -0500, madvenka@linux.microsoft.com wrote:
quoted
+ if (frame->need_reliable && !unwind_is_reliable(frame)) {
+ /* Cannot unwind to the next frame reliably. */
+ frame->failed = true;
+ return false;
+ }
This means we only collect reliability information in the case
where we're specifically doing a reliable stacktrace. For
example when printing stack traces on the console it might be
useful to print a ? or something if the frame is unreliable as a
hint to the reader that the information might be misleading.
Could we therefore change the flag here to a reliability one and
our need_reliable check so that we always run
unwind_is_reliable()?
I'm not sure if we need to abandon the trace on first error when
doing a reliable trace but I can see it's a bit safer so perhaps
better to do so. If we don't abandon then we don't require the
need_reliable check at all.
I think that the caller should be able to specify that the stack trace
should be abandoned. Like Livepatch.
So, we could always do the reliability check. But keep need_reliable.
Thanks.
Madhavan
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: "Madhavan T. Venkataraman" <redacted>
SYM_CODE functions don't follow the usual calling conventions. Check if the
return PC in a stack frame falls in any of these. If it does, consider the
stack trace unreliable.
Define a special section for unreliable functions
=================================================
Define a SYM_CODE_END() macro for arm64 that adds the function address
range to a new section called "sym_code_functions".
Linker file
===========
Include the "sym_code_functions" section under read-only data in
vmlinux.lds.S.
Initialization
==============
Define an early_initcall() to create a sym_code_functions[] array from
the linker data.
Unwinder check
==============
Add a reliability check in unwind_is_reliable() that compares a return
PC with sym_code_functions[]. If there is a match, then return failure.
Signed-off-by: Madhavan T. Venkataraman <redacted>
---
arch/arm64/include/asm/linkage.h | 12 +++++++
arch/arm64/include/asm/sections.h | 1 +
arch/arm64/kernel/stacktrace.c | 53 +++++++++++++++++++++++++++++++
arch/arm64/kernel/vmlinux.lds.S | 10 ++++++
4 files changed, 76 insertions(+)
From: Madhavan T. Venkataraman <hidden> Date: 2021-08-12 19:17:48
OK. So, this time the threading is proper. Please review version 8.
It is identical to version 7 except for the version number and
threading.
Please disregard all emails sent as RFC PATCH v7 for this series. Again,
apologies for screwing the threading up.
Madhavan
On 8/12/21 2:05 PM, madvenka@linux.microsoft.com wrote:
From: "Madhavan T. Venkataraman" <redacted>
Make all stack walking functions use arch_stack_walk()
======================================================
Currently, there are multiple functions in ARM64 code that walk the
stack using start_backtrace() and unwind_frame(). Convert all of
them to use arch_stack_walk(). This makes maintenance easier.
Reorganize the unwinder code for better consistency and maintenance
===================================================================
Rename unwinder functions to unwind_*() similar to other architectures
for naming consistency.
Annotate all of the unwind_*() functions with notrace so they cannot be
ftraced and NOKPROBE_SYMBOL() so they cannot be kprobed. Ftrace and Kprobe
code can call the unwinder.
Redefine the unwinder loop and make it similar to other architectures.
Define the following:
unwind_start(&frame, task, fp, pc);
while (unwind_consume(&frame, consume_entry, cookie))
unwind_next(&frame);
return !unwind_failed(&frame);
unwind_start()
Same as the original start_backtrace().
unwind_consume()
This new function does two things:
- Calls consume_entry() to consume the return PC.
- Implements checks to determine whether the unwind should continue
or terminate.
unwind_next()
Same as the original unwind_frame() except:
- the stack trace termination check has been moved from here to
unwind_consume(). So, unwind_next() assumes that the fp is valid.
- unwind_frame() used to return an error value. This function only
sets internal state and does not return anything. The state is
retrieved via a helper. See next.
unwind_failed()
Return a boolean to indicate whether the stack trace completed
successfully or failed. arch_stack_walk() ignores the return
value. But arch_stack_walk_reliable() in the future will look
at the return value.
Unwind status
Introduce a new flag called "failed" in struct stackframe. Set this
flag when an error is encountered. If this flag is set, terminate
the unwind. Also, let the unwinder return the status to the caller.
Reliability checks
==================
There are some kernel features and conditions that make a stack trace
unreliable. Callers may require the unwinder to detect these cases.
E.g., livepatch.
Introduce a new function called unwind_is_reliable() that will detect
these cases and return a boolean.
Introduce a new argument to unwind() called "need_reliable" so a caller
can tell unwind() that it requires a reliable stack trace. For such a
caller, any unreliability in the stack trace must be treated as a fatal
error and the unwind must be aborted.
Call unwind_is_reliable() from unwind_consume() like this:
if (frame->need_reliable && !unwind_is_reliable(frame)) {
frame->failed = true;
return false;
}
arch_stack_walk() passes "false" for need_reliable because its callers
don't care about reliability. arch_stack_walk() is used for debug and
test purposes.
Introduce arch_stack_walk_reliable() for ARM64. This works like
arch_stack_walk() except for two things:
- It passes "true" for need_reliable.
- It returns -EINVAL if unwind() aborts.
Introduce the first reliability check in unwind_is_reliable() - If
a return PC is not a valid kernel text address, consider the stack
trace unreliable. It could be some generated code.
Other reliability checks will be added in the future. Until all of the
checks are in place, arch_stack_walk_reliable() may not be used by
livepatch. But it may be used by debug and test code.
SYM_CODE check
==============
SYM_CODE functions do not follow normal calling conventions. They cannot
be unwound reliably using the frame pointer. Collect the address ranges
of these functions in a special section called "sym_code_functions".
In unwind_is_reliable(), check the return PC against these ranges. If a
match is found, then consider the stack trace unreliable. This is the
second reliability check introduced by this work.
Last stack frame
----------------
If a SYM_CODE function occurs in the very last frame in the stack trace,
then the stack trace is not considered unreliable. This is because there
is no more unwinding to do. Examples:
- EL0 exception stack traces end in the top level EL0 exception
handlers.
- All kernel thread stack traces end in ret_from_fork().
---
Changelog:
v8:
From Mark Rutland:
- Make the unwinder loop similar to other architectures.
- Keep details to within the unwinder functions and return a simple
boolean to the caller.
- Convert some of the current code that contains unwinder logic to
simply use arch_stack_walk(). I have converted all of them.
- Do not copy sym_code_functions[]. Just place it in rodata for now.
- Have the main loop check for termination conditions rather than
having unwind_frame() check for them. In other words, let
unwind_frame() assume that the fp is valid.
- Replace the big comment for SYM_CODE functions with a shorter
comment.
/*
* As SYM_CODE functions don't follow the usual calling
* conventions, we assume by default that any SYM_CODE function
* cannot be unwound reliably.
*
* Note that this includes:
*
* - Exception handlers and entry assembly
* - Trampoline assembly (e.g., ftrace, kprobes)
* - Hypervisor-related assembly
* - Hibernation-related assembly
* - CPU start-stop, suspend-resume assembly
* - Kernel relocation assembly
*/
v7:
The Mailer screwed up the threading on this. So, I have resent this
same series as version 8 with proper threading to avoid confusion.
v6:
From Mark Rutland:
- The per-frame reliability concept and flag are acceptable. But more
work is needed to make the per-frame checks more accurate and more
complete. E.g., some code reorg is being worked on that will help.
I have now removed the frame->reliable flag and deleted the whole
concept of per-frame status. This is orthogonal to this patch series.
Instead, I have improved the unwinder to return proper return codes
so a caller can take appropriate action without needing per-frame
status.
- Remove the mention of PLTs and update the comment.
I have replaced the comment above the call to __kernel_text_address()
with the comment suggested by Mark Rutland.
Other comments:
- Other comments on the per-frame stuff are not relevant because
that approach is not there anymore.
v5:
From Keiya Nobuta:
- The term blacklist(ed) is not to be used anymore. I have changed it
to unreliable. So, the function unwinder_blacklisted() has been
changed to unwinder_is_unreliable().
From Mark Brown:
- Add a comment for the "reliable" flag in struct stackframe. The
reliability attribute is not complete until all the checks are
in place. Added a comment above struct stackframe.
- Include some of the comments in the cover letter in the actual
code so that we can compare it with the reliable stack trace
requirements document for completeness. I have added a comment:
- above unwinder_is_unreliable() that lists the requirements
that are addressed by the function.
- above the __kernel_text_address() call about all the cases
the call covers.
v4:
From Mark Brown:
- I was checking the return PC with __kernel_text_address() before
the Function Graph trace handling. Mark Brown felt that all the
reliability checks should be performed on the original return PC
once that is obtained. So, I have moved all the reliability checks
to after the Function Graph Trace handling code in the unwinder.
Basically, the unwinder should perform PC translations first (for
rhe return trampoline for Function Graph Tracing, Kretprobes, etc).
Then, the reliability checks should be applied to the resulting
PC.
- Mark said to improve the naming of the new functions so they don't
collide with existing ones. I have used a prefix "unwinder_" for
all the new functions.
From Josh Poimboeuf:
- In the error scenarios in the unwinder, the reliable flag in the
stack frame should be set. Implemented this.
- Some of the other comments are not relevant to the new code as
I have taken a different approach in the new code. That is why
I have not made those changes. E.g., Ard wanted me to add the
"const" keyword to the global section array. That array does not
exist in v4. Similarly, Mark Brown said to use ARRAY_SIZE() for
the same array in a for loop.
Other changes:
- Add a new definition for SYM_CODE_END() that adds the address
range of the function to a special section called
"sym_code_functions".
- Include the new section under initdata in vmlinux.lds.S.
- Define an early_initcall() to copy the contents of the
"sym_code_functions" section to an array by the same name.
- Define a function unwinder_blacklisted() that compares a return
PC against sym_code_sections[]. If there is a match, mark the
stack trace unreliable. Call this from unwind_frame().
v3:
- Implemented a sym_code_ranges[] array to contains sections bounds
for text sections that contain SYM_CODE_*() functions. The unwinder
checks each return PC against the sections. If it falls in any of
the sections, the stack trace is marked unreliable.
- Moved SYM_CODE functions from .text and .init.text into a new
text section called ".code.text". Added this section to
vmlinux.lds.S and sym_code_ranges[].
- Fixed the logic in the unwinder that handles Function Graph
Tracer return trampoline.
- Removed all the previous code that handles:
- ftrace entry code for traced function
- special_functions[] array that lists individual functions
- kretprobe_trampoline() special case
v2
- Removed the terminating entry { 0, 0 } in special_functions[]
and replaced it with the idiom { /* sentinel */ }.
- Change the ftrace trampoline entry ftrace_graph_call in
special_functions[] to ftrace_call + 4 and added explanatory
comments.
- Unnested #ifdefs in special_functions[] for FTRACE.
v1
- Define a bool field in struct stackframe. This will indicate if
a stack trace is reliable.
- Implement a special_functions[] array that will be populated
with special functions in which the stack trace is considered
unreliable.
- Using kallsyms_lookup(), get the address ranges for the special
functions and record them.
- Implement an is_reliable_function(pc). This function will check
if a given return PC falls in any of the special functions. If
it does, the stack trace is unreliable.
- Implement check_reliability() function that will check if a
stack frame is reliable. Call is_reliable_function() from
check_reliability().
- Before a return PC is checked against special_funtions[], it
must be validates as a proper kernel text address. Call
__kernel_text_address() from check_reliability().
- Finally, call check_reliability() from unwind_frame() for
each stack frame.
- Add EL1 exception handlers to special_functions[].
el1_sync();
el1_irq();
el1_error();
el1_sync_invalid();
el1_irq_invalid();
el1_fiq_invalid();
el1_error_invalid();
- The above functions are currently defined as LOCAL symbols.
Make them global so that they can be referenced from the
unwinder code.
- Add FTRACE trampolines to special_functions[]:
ftrace_graph_call()
ftrace_graph_caller()
return_to_handler()
- Add the kretprobe trampoline to special functions[]:
kretprobe_trampoline()
Previous versions and discussion
================================
v7: Mailer screwed up the threading. Sent the same as v8 with proper threading.
v6: https://lore.kernel.org/linux-arm-kernel/20210630223356.58714-1-madvenka@linux.microsoft.com/
v5: https://lore.kernel.org/linux-arm-kernel/20210526214917.20099-1-madvenka@linux.microsoft.com/
v4: https://lore.kernel.org/linux-arm-kernel/20210516040018.128105-1-madvenka@linux.microsoft.com/
v3: https://lore.kernel.org/linux-arm-kernel/20210503173615.21576-1-madvenka@linux.microsoft.com/
v2: https://lore.kernel.org/linux-arm-kernel/20210405204313.21346-1-madvenka@linux.microsoft.com/
v1: https://lore.kernel.org/linux-arm-kernel/20210330190955.13707-1-madvenka@linux.microsoft.com/
Madhavan T. Venkataraman (4):
arm64: Make all stack walking functions use arch_stack_walk()
arm64: Reorganize the unwinder code for better consistency and
maintenance
arm64: Introduce stack trace reliability checks in the unwinder
arm64: Create a list of SYM_CODE functions, check return PC against
list
arch/arm64/include/asm/linkage.h | 12 ++
arch/arm64/include/asm/sections.h | 1 +
arch/arm64/include/asm/stacktrace.h | 16 +-
arch/arm64/kernel/perf_callchain.c | 5 +-
arch/arm64/kernel/process.c | 39 ++--
arch/arm64/kernel/return_address.c | 6 +-
arch/arm64/kernel/stacktrace.c | 291 ++++++++++++++++++++--------
arch/arm64/kernel/time.c | 22 ++-
arch/arm64/kernel/vmlinux.lds.S | 10 +
9 files changed, 277 insertions(+), 125 deletions(-)
base-commit: 36a21d51725af2ce0700c6ebcb6b9594aac658a6