2021-03-04 17:51:45

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v1] powerpc: Include running function as first entry in save_stack_trace() and friends

[adding Mark Brown]

On Wed, Mar 03, 2021 at 04:20:43PM +0100, Marco Elver wrote:
> On Wed, Mar 03, 2021 at 03:52PM +0100, Christophe Leroy wrote:
> > Le 03/03/2021 � 15:38, Marco Elver a �crit�:
> > > On Wed, 3 Mar 2021 at 15:09, Christophe Leroy
> > > <[email protected]> wrote:
> > > >
> > > > It seems like all other sane architectures, namely x86 and arm64
> > > > at least, include the running function as top entry when saving
> > > > stack trace.
> > > >
> > > > Functionnalities like KFENCE expect it.
> > > >
> > > > Do the same on powerpc, it allows KFENCE to properly identify the faulting
> > > > function as depicted below. Before the patch KFENCE was identifying
> > > > finish_task_switch.isra as the faulting function.
> > > >
> > > > [ 14.937370] ==================================================================
> > > > [ 14.948692] BUG: KFENCE: invalid read in test_invalid_access+0x54/0x108
> > > > [ 14.948692]
> > > > [ 14.956814] Invalid read at 0xdf98800a:
> > > > [ 14.960664] test_invalid_access+0x54/0x108
> > > > [ 14.964876] finish_task_switch.isra.0+0x54/0x23c
> > > > [ 14.969606] kunit_try_run_case+0x5c/0xd0
> > > > [ 14.973658] kunit_generic_run_threadfn_adapter+0x24/0x30
> > > > [ 14.979079] kthread+0x15c/0x174
> > > > [ 14.982342] ret_from_kernel_thread+0x14/0x1c
> > > > [ 14.986731]
> > > > [ 14.988236] CPU: 0 PID: 111 Comm: kunit_try_catch Tainted: G B 5.12.0-rc1-01537-g95f6e2088d7e-dirty #4682
> > > > [ 14.999795] NIP: c016ec2c LR: c02f517c CTR: c016ebd8
> > > > [ 15.004851] REGS: e2449d90 TRAP: 0301 Tainted: G B (5.12.0-rc1-01537-g95f6e2088d7e-dirty)
> > > > [ 15.015274] MSR: 00009032 <EE,ME,IR,DR,RI> CR: 22000004 XER: 00000000
> > > > [ 15.022043] DAR: df98800a DSISR: 20000000
> > > > [ 15.022043] GPR00: c02f517c e2449e50 c1142080 e100dd24 c084b13c 00000008 c084b32b c016ebd8
> > > > [ 15.022043] GPR08: c0850000 df988000 c0d10000 e2449eb0 22000288
> > > > [ 15.040581] NIP [c016ec2c] test_invalid_access+0x54/0x108
> > > > [ 15.046010] LR [c02f517c] kunit_try_run_case+0x5c/0xd0
> > > > [ 15.051181] Call Trace:
> > > > [ 15.053637] [e2449e50] [c005a68c] finish_task_switch.isra.0+0x54/0x23c (unreliable)
> > > > [ 15.061338] [e2449eb0] [c02f517c] kunit_try_run_case+0x5c/0xd0
> > > > [ 15.067215] [e2449ed0] [c02f648c] kunit_generic_run_threadfn_adapter+0x24/0x30
> > > > [ 15.074472] [e2449ef0] [c004e7b0] kthread+0x15c/0x174
> > > > [ 15.079571] [e2449f30] [c001317c] ret_from_kernel_thread+0x14/0x1c
> > > > [ 15.085798] Instruction dump:
> > > > [ 15.088784] 8129d608 38e7ebd8 81020280 911f004c 39000000 995f0024 907f0028 90ff001c
> > > > [ 15.096613] 3949000a 915f0020 3d40c0d1 3d00c085 <8929000a> 3908adb0 812a4b98 3d40c02f
> > > > [ 15.104612] ==================================================================
> > > >
> > > > Signed-off-by: Christophe Leroy <[email protected]>
> > >
> > > Acked-by: Marco Elver <[email protected]>
> > >
> > > Thank you, I think this looks like the right solution. Just a question below:
> > >
> > ...
> >
> > > > @@ -59,23 +70,26 @@ void save_stack_trace(struct stack_trace *trace)
> > > >
> > > > sp = current_stack_frame();
> > > >
> > > > - save_context_stack(trace, sp, current, 1);
> > > > + save_context_stack(trace, sp, (unsigned long)save_stack_trace, current, 1);
> > >
> > > This causes ip == save_stack_trace and also below for
> > > save_stack_trace_tsk. Does this mean save_stack_trace() is included in
> > > the trace? Looking at kernel/stacktrace.c, I think the library wants
> > > to exclude itself from the trace, as it does '.skip = skipnr + 1' (and
> > > '.skip = skipnr + (current == tsk)' for the _tsk variant).
> > >
> > > If the arch-helper here is included, should this use _RET_IP_ instead?
> > >
> >
> > Don't really know, I was inspired by arm64 which has:
> >
> > void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
> > struct task_struct *task, struct pt_regs *regs)
> > {
> > struct stackframe frame;
> >
> > if (regs)
> > start_backtrace(&frame, regs->regs[29], regs->pc);
> > else if (task == current)
> > start_backtrace(&frame,
> > (unsigned long)__builtin_frame_address(0),
> > (unsigned long)arch_stack_walk);
> > else
> > start_backtrace(&frame, thread_saved_fp(task),
> > thread_saved_pc(task));
> >
> > walk_stackframe(task, &frame, consume_entry, cookie);
> > }
> >
> > But looking at x86 you may be right, so what should be done really ?
>
> x86:
>
> [ 2.843292] calling stack_trace_save:
> [ 2.843705] test_func+0x6c/0x118
> [ 2.844184] do_one_initcall+0x58/0x270
> [ 2.844618] kernel_init_freeable+0x1da/0x23a
> [ 2.845110] kernel_init+0xc/0x166
> [ 2.845494] ret_from_fork+0x22/0x30
>
> [ 2.867525] calling stack_trace_save_tsk:
> [ 2.868017] test_func+0xa9/0x118
> [ 2.868530] do_one_initcall+0x58/0x270
> [ 2.869003] kernel_init_freeable+0x1da/0x23a
> [ 2.869535] kernel_init+0xc/0x166
> [ 2.869957] ret_from_fork+0x22/0x30
>
> arm64:
>
> [ 3.786911] calling stack_trace_save:
> [ 3.787147] stack_trace_save+0x50/0x78
> [ 3.787443] test_func+0x84/0x13c
> [ 3.787738] do_one_initcall+0x5c/0x310
> [ 3.788099] kernel_init_freeable+0x214/0x294
> [ 3.788363] kernel_init+0x18/0x164
> [ 3.788585] ret_from_fork+0x10/0x30
>
> [ 3.803615] calling stack_trace_save_tsk:
> [ 3.804266] stack_trace_save_tsk+0x9c/0x100
> [ 3.804541] test_func+0xc4/0x13c
> [ 3.804803] do_one_initcall+0x5c/0x310
> [ 3.805031] kernel_init_freeable+0x214/0x294
> [ 3.805284] kernel_init+0x18/0x164
> [ 3.805505] ret_from_fork+0x10/0x30
>
> +Cc arm64 folks.
>
> So I think the arm64 version also has a bug, because I think a user of
> <linux/stacktrace.h> really doesn't care about the library function
> itself. And from reading kernel/stacktrace.c I think it wants to exclude
> itself entirely.
>
> It's a shame that <linux/stacktrace.h> isn't better documented, but I'm
> pretty sure that including the library functions in the trace is not
> useful.

I agree this behaviour isn't desireable, and that the lack of
documentation is unfortunate.

It looks like GCC is happy to give us the function-entry-time FP if we use
__builtin_frame_address(1), and assuming clang is similarly happy we can do:

| diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c
| index ad20981dfda4..5dfbf915eb7f 100644
| --- a/arch/arm64/kernel/stacktrace.c
| +++ b/arch/arm64/kernel/stacktrace.c
| @@ -203,8 +203,8 @@ void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
| start_backtrace(&frame, regs->regs[29], regs->pc);
| else if (task == current)
| start_backtrace(&frame,
| - (unsigned long)__builtin_frame_address(0),
| - (unsigned long)arch_stack_walk);
| + (unsigned long)__builtin_frame_address(1),
| + (unsigned long)__builtin_return_address(0));
| else
| start_backtrace(&frame, thread_saved_fp(task),
| thread_saved_pc(task));

... such that arch_stack_walk() will try to avoid including itself in a
trace, and so the existing skipping should (w/ caveats below) skip
stack_trace_save() or stack_trace_save_tsk().

If that works for you, I can spin that as a patch, though we'll need to
check that doesn't introduce a new fencepost error elsewhere.

The bigger problem here is that skipping is dodgy to begin with, and
this is still liable to break in some cases. One big concern is that
(especially with LTO) we cannot guarantee the compiler will not inline
or outline functions, causing the skipp value to be too large or too
small. That's liable to happen to callers, and in theory (though
unlikely in practice), portions of arch_stack_walk() or
stack_trace_save() could get outlined too.

Unless we can get some strong guarantees from compiler folk such that we
can guarantee a specific function acts boundary for unwinding (and
doesn't itself get split, etc), the only reliable way I can think to
solve this requires an assembly trampoline. Whatever we do is liable to
need some invasive rework.

Thanks,
Mark.


2021-03-05 00:16:04

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH v1] powerpc: Include running function as first entry in save_stack_trace() and friends

On Thu, 4 Mar 2021 at 15:57, Mark Rutland <[email protected]> wrote:
> [adding Mark Brown]
>
> On Wed, Mar 03, 2021 at 04:20:43PM +0100, Marco Elver wrote:
> > On Wed, Mar 03, 2021 at 03:52PM +0100, Christophe Leroy wrote:
> > > Le 03/03/2021 � 15:38, Marco Elver a �crit�:
> > > > On Wed, 3 Mar 2021 at 15:09, Christophe Leroy
> > > > <[email protected]> wrote:
> > > > >
> > > > > It seems like all other sane architectures, namely x86 and arm64
> > > > > at least, include the running function as top entry when saving
> > > > > stack trace.
> > > > >
> > > > > Functionnalities like KFENCE expect it.
> > > > >
> > > > > Do the same on powerpc, it allows KFENCE to properly identify the faulting
> > > > > function as depicted below. Before the patch KFENCE was identifying
> > > > > finish_task_switch.isra as the faulting function.
> > > > >
> > > > > [ 14.937370] ==================================================================
> > > > > [ 14.948692] BUG: KFENCE: invalid read in test_invalid_access+0x54/0x108
> > > > > [ 14.948692]
> > > > > [ 14.956814] Invalid read at 0xdf98800a:
> > > > > [ 14.960664] test_invalid_access+0x54/0x108
> > > > > [ 14.964876] finish_task_switch.isra.0+0x54/0x23c
> > > > > [ 14.969606] kunit_try_run_case+0x5c/0xd0
> > > > > [ 14.973658] kunit_generic_run_threadfn_adapter+0x24/0x30
> > > > > [ 14.979079] kthread+0x15c/0x174
> > > > > [ 14.982342] ret_from_kernel_thread+0x14/0x1c
> > > > > [ 14.986731]
> > > > > [ 14.988236] CPU: 0 PID: 111 Comm: kunit_try_catch Tainted: G B 5.12.0-rc1-01537-g95f6e2088d7e-dirty #4682
> > > > > [ 14.999795] NIP: c016ec2c LR: c02f517c CTR: c016ebd8
> > > > > [ 15.004851] REGS: e2449d90 TRAP: 0301 Tainted: G B (5.12.0-rc1-01537-g95f6e2088d7e-dirty)
> > > > > [ 15.015274] MSR: 00009032 <EE,ME,IR,DR,RI> CR: 22000004 XER: 00000000
> > > > > [ 15.022043] DAR: df98800a DSISR: 20000000
> > > > > [ 15.022043] GPR00: c02f517c e2449e50 c1142080 e100dd24 c084b13c 00000008 c084b32b c016ebd8
> > > > > [ 15.022043] GPR08: c0850000 df988000 c0d10000 e2449eb0 22000288
> > > > > [ 15.040581] NIP [c016ec2c] test_invalid_access+0x54/0x108
> > > > > [ 15.046010] LR [c02f517c] kunit_try_run_case+0x5c/0xd0
> > > > > [ 15.051181] Call Trace:
> > > > > [ 15.053637] [e2449e50] [c005a68c] finish_task_switch.isra.0+0x54/0x23c (unreliable)
> > > > > [ 15.061338] [e2449eb0] [c02f517c] kunit_try_run_case+0x5c/0xd0
> > > > > [ 15.067215] [e2449ed0] [c02f648c] kunit_generic_run_threadfn_adapter+0x24/0x30
> > > > > [ 15.074472] [e2449ef0] [c004e7b0] kthread+0x15c/0x174
> > > > > [ 15.079571] [e2449f30] [c001317c] ret_from_kernel_thread+0x14/0x1c
> > > > > [ 15.085798] Instruction dump:
> > > > > [ 15.088784] 8129d608 38e7ebd8 81020280 911f004c 39000000 995f0024 907f0028 90ff001c
> > > > > [ 15.096613] 3949000a 915f0020 3d40c0d1 3d00c085 <8929000a> 3908adb0 812a4b98 3d40c02f
> > > > > [ 15.104612] ==================================================================
> > > > >
> > > > > Signed-off-by: Christophe Leroy <[email protected]>
> > > >
> > > > Acked-by: Marco Elver <[email protected]>
> > > >
> > > > Thank you, I think this looks like the right solution. Just a question below:
> > > >
> > > ...
> > >
> > > > > @@ -59,23 +70,26 @@ void save_stack_trace(struct stack_trace *trace)
> > > > >
> > > > > sp = current_stack_frame();
> > > > >
> > > > > - save_context_stack(trace, sp, current, 1);
> > > > > + save_context_stack(trace, sp, (unsigned long)save_stack_trace, current, 1);
> > > >
> > > > This causes ip == save_stack_trace and also below for
> > > > save_stack_trace_tsk. Does this mean save_stack_trace() is included in
> > > > the trace? Looking at kernel/stacktrace.c, I think the library wants
> > > > to exclude itself from the trace, as it does '.skip = skipnr + 1' (and
> > > > '.skip = skipnr + (current == tsk)' for the _tsk variant).
> > > >
> > > > If the arch-helper here is included, should this use _RET_IP_ instead?
> > > >
> > >
> > > Don't really know, I was inspired by arm64 which has:
> > >
> > > void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
> > > struct task_struct *task, struct pt_regs *regs)
> > > {
> > > struct stackframe frame;
> > >
> > > if (regs)
> > > start_backtrace(&frame, regs->regs[29], regs->pc);
> > > else if (task == current)
> > > start_backtrace(&frame,
> > > (unsigned long)__builtin_frame_address(0),
> > > (unsigned long)arch_stack_walk);
> > > else
> > > start_backtrace(&frame, thread_saved_fp(task),
> > > thread_saved_pc(task));
> > >
> > > walk_stackframe(task, &frame, consume_entry, cookie);
> > > }
> > >
> > > But looking at x86 you may be right, so what should be done really ?
> >
> > x86:
> >
> > [ 2.843292] calling stack_trace_save:
> > [ 2.843705] test_func+0x6c/0x118
> > [ 2.844184] do_one_initcall+0x58/0x270
> > [ 2.844618] kernel_init_freeable+0x1da/0x23a
> > [ 2.845110] kernel_init+0xc/0x166
> > [ 2.845494] ret_from_fork+0x22/0x30
> >
> > [ 2.867525] calling stack_trace_save_tsk:
> > [ 2.868017] test_func+0xa9/0x118
> > [ 2.868530] do_one_initcall+0x58/0x270
> > [ 2.869003] kernel_init_freeable+0x1da/0x23a
> > [ 2.869535] kernel_init+0xc/0x166
> > [ 2.869957] ret_from_fork+0x22/0x30
> >
> > arm64:
> >
> > [ 3.786911] calling stack_trace_save:
> > [ 3.787147] stack_trace_save+0x50/0x78
> > [ 3.787443] test_func+0x84/0x13c
> > [ 3.787738] do_one_initcall+0x5c/0x310
> > [ 3.788099] kernel_init_freeable+0x214/0x294
> > [ 3.788363] kernel_init+0x18/0x164
> > [ 3.788585] ret_from_fork+0x10/0x30
> >
> > [ 3.803615] calling stack_trace_save_tsk:
> > [ 3.804266] stack_trace_save_tsk+0x9c/0x100
> > [ 3.804541] test_func+0xc4/0x13c
> > [ 3.804803] do_one_initcall+0x5c/0x310
> > [ 3.805031] kernel_init_freeable+0x214/0x294
> > [ 3.805284] kernel_init+0x18/0x164
> > [ 3.805505] ret_from_fork+0x10/0x30
> >
> > +Cc arm64 folks.
> >
> > So I think the arm64 version also has a bug, because I think a user of
> > <linux/stacktrace.h> really doesn't care about the library function
> > itself. And from reading kernel/stacktrace.c I think it wants to exclude
> > itself entirely.
> >
> > It's a shame that <linux/stacktrace.h> isn't better documented, but I'm
> > pretty sure that including the library functions in the trace is not
> > useful.
>
> I agree this behaviour isn't desireable, and that the lack of
> documentation is unfortunate.
>
> It looks like GCC is happy to give us the function-entry-time FP if we use
> __builtin_frame_address(1), and assuming clang is similarly happy we can do:
>
> | diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c
> | index ad20981dfda4..5dfbf915eb7f 100644
> | --- a/arch/arm64/kernel/stacktrace.c
> | +++ b/arch/arm64/kernel/stacktrace.c
> | @@ -203,8 +203,8 @@ void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
> | start_backtrace(&frame, regs->regs[29], regs->pc);
> | else if (task == current)
> | start_backtrace(&frame,
> | - (unsigned long)__builtin_frame_address(0),
> | - (unsigned long)arch_stack_walk);
> | + (unsigned long)__builtin_frame_address(1),
> | + (unsigned long)__builtin_return_address(0));
> | else
> | start_backtrace(&frame, thread_saved_fp(task),
> | thread_saved_pc(task));
>
> ... such that arch_stack_walk() will try to avoid including itself in a
> trace, and so the existing skipping should (w/ caveats below) skip
> stack_trace_save() or stack_trace_save_tsk().

Thank you! Yes, that works.

> If that works for you, I can spin that as a patch, though we'll need to
> check that doesn't introduce a new fencepost error elsewhere.
>
> The bigger problem here is that skipping is dodgy to begin with, and
> this is still liable to break in some cases. One big concern is that
> (especially with LTO) we cannot guarantee the compiler will not inline
> or outline functions, causing the skipp value to be too large or too
> small. That's liable to happen to callers, and in theory (though
> unlikely in practice), portions of arch_stack_walk() or
> stack_trace_save() could get outlined too.
>
> Unless we can get some strong guarantees from compiler folk such that we
> can guarantee a specific function acts boundary for unwinding (and
> doesn't itself get split, etc), the only reliable way I can think to
> solve this requires an assembly trampoline. Whatever we do is liable to
> need some invasive rework.

Will LTO and friends respect 'noinline'? One thing I also noticed is
that tail calls would also cause the stack trace to appear somewhat
incomplete (for some of my tests I've disabled tail call
optimizations). Is there a way to also mark a function
non-tail-callable? But I'm also not sure if with all that we'd be
guaranteed the code we want, even though in practice it might.

Thanks,
-- Marco

2021-03-05 01:00:16

by Segher Boessenkool

[permalink] [raw]
Subject: Re: [PATCH v1] powerpc: Include running function as first entry in save_stack_trace() and friends

Hi!

On Thu, Mar 04, 2021 at 02:57:30PM +0000, Mark Rutland wrote:
> It looks like GCC is happy to give us the function-entry-time FP if we use
> __builtin_frame_address(1),

From the GCC manual:
Calling this function with a nonzero argument can have
unpredictable effects, including crashing the calling program. As
a result, calls that are considered unsafe are diagnosed when the
'-Wframe-address' option is in effect. Such calls should only be
made in debugging situations.

It *does* warn (the warning is in -Wall btw), on both powerpc and
aarch64. Furthermore, using this builtin causes lousy code (it forces
the use of a frame pointer, which we normally try very hard to optimise
away, for good reason).

And, that warning is not an idle warning. Non-zero arguments to
__builtin_frame_address can crash the program. It won't on simpler
functions, but there is no real definition of what a simpler function
*is*. It is meant for debugging, not for production use (this is also
why no one has bothered to make it faster).

On Power it should work, but on pretty much any other arch it won't.

> Unless we can get some strong guarantees from compiler folk such that we
> can guarantee a specific function acts boundary for unwinding (and
> doesn't itself get split, etc), the only reliable way I can think to
> solve this requires an assembly trampoline. Whatever we do is liable to
> need some invasive rework.

You cannot get such a guarantee, other than not letting the compiler
see into the routine at all, like with assembler code (not inline asm,
real assembler code).

The real way forward is to bite the bullet and to no longer pretend you
can do a full backtrace from just the stack contents. You cannot.


Segher

2021-03-09 16:08:08

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v1] powerpc: Include running function as first entry in save_stack_trace() and friends

On Thu, Mar 04, 2021 at 03:54:48PM -0600, Segher Boessenkool wrote:
> Hi!

Hi Segher,

> On Thu, Mar 04, 2021 at 02:57:30PM +0000, Mark Rutland wrote:
> > It looks like GCC is happy to give us the function-entry-time FP if we use
> > __builtin_frame_address(1),
>
> From the GCC manual:
> Calling this function with a nonzero argument can have
> unpredictable effects, including crashing the calling program. As
> a result, calls that are considered unsafe are diagnosed when the
> '-Wframe-address' option is in effect. Such calls should only be
> made in debugging situations.
>
> It *does* warn (the warning is in -Wall btw), on both powerpc and
> aarch64. Furthermore, using this builtin causes lousy code (it forces
> the use of a frame pointer, which we normally try very hard to optimise
> away, for good reason).
>
> And, that warning is not an idle warning. Non-zero arguments to
> __builtin_frame_address can crash the program. It won't on simpler
> functions, but there is no real definition of what a simpler function
> *is*. It is meant for debugging, not for production use (this is also
> why no one has bothered to make it faster).
>
> On Power it should work, but on pretty much any other arch it won't.

I understand this is true generally, and cannot be relied upon in
portable code. However as you hint here for Power, I believe that on
arm64 __builtin_frame_address(1) shouldn't crash the program due to the
way frame records work on arm64, but I'll go check with some local
compiler folk. I agree that __builtin_frame_address(2) and beyond
certainly can, e.g. by NULL dereference and similar.

For context, why do you think this would work on power specifically? I
wonder if our rationale is similar.

Are you aware of anything in particular that breaks using
__builtin_frame_address(1) in non-portable code, or is this just a
general sentiment of this not being a supported use-case?

> > Unless we can get some strong guarantees from compiler folk such that we
> > can guarantee a specific function acts boundary for unwinding (and
> > doesn't itself get split, etc), the only reliable way I can think to
> > solve this requires an assembly trampoline. Whatever we do is liable to
> > need some invasive rework.
>
> You cannot get such a guarantee, other than not letting the compiler
> see into the routine at all, like with assembler code (not inline asm,
> real assembler code).

If we cannot reliably ensure this then I'm happy to go write an assembly
trampoline to snapshot the state at a function call boundary (where our
procedure call standard mandates the state of the LR, FP, and frame
records pointed to by the FP). This'll require reworking a reasonable
amount of code cross-architecture, so I'll need to get some more
concrete justification (e.g. examples of things that can go wrong in
practice).

> The real way forward is to bite the bullet and to no longer pretend you
> can do a full backtrace from just the stack contents. You cannot.

I think what you mean here is that there's no reliable way to handle the
current/leaf function, right? If so I do agree.

Beyond that I believe that arm64's frame records should be sufficient.

Thanks,
Mark.

2021-03-09 22:14:45

by Segher Boessenkool

[permalink] [raw]
Subject: Re: [PATCH v1] powerpc: Include running function as first entry in save_stack_trace() and friends

Hi!

On Tue, Mar 09, 2021 at 04:05:23PM +0000, Mark Rutland wrote:
> On Thu, Mar 04, 2021 at 03:54:48PM -0600, Segher Boessenkool wrote:
> > On Thu, Mar 04, 2021 at 02:57:30PM +0000, Mark Rutland wrote:
> > > It looks like GCC is happy to give us the function-entry-time FP if we use
> > > __builtin_frame_address(1),
> >
> > From the GCC manual:
> > Calling this function with a nonzero argument can have
> > unpredictable effects, including crashing the calling program. As
> > a result, calls that are considered unsafe are diagnosed when the
> > '-Wframe-address' option is in effect. Such calls should only be
> > made in debugging situations.
> >
> > It *does* warn (the warning is in -Wall btw), on both powerpc and
> > aarch64. Furthermore, using this builtin causes lousy code (it forces
> > the use of a frame pointer, which we normally try very hard to optimise
> > away, for good reason).
> >
> > And, that warning is not an idle warning. Non-zero arguments to
> > __builtin_frame_address can crash the program. It won't on simpler
> > functions, but there is no real definition of what a simpler function
> > *is*. It is meant for debugging, not for production use (this is also
> > why no one has bothered to make it faster).
> >
> > On Power it should work, but on pretty much any other arch it won't.
>
> I understand this is true generally, and cannot be relied upon in
> portable code. However as you hint here for Power, I believe that on
> arm64 __builtin_frame_address(1) shouldn't crash the program due to the
> way frame records work on arm64, but I'll go check with some local
> compiler folk. I agree that __builtin_frame_address(2) and beyond
> certainly can, e.g. by NULL dereference and similar.

I still do not know the aarch64 ABI well enough. If only I had time!

> For context, why do you think this would work on power specifically? I
> wonder if our rationale is similar.

On most 64-bit Power ABIs all stack frames are connected together as a
linked list (which is updated atomically, importantly). This makes it
possible to always find all previous stack frames.

> Are you aware of anything in particular that breaks using
> __builtin_frame_address(1) in non-portable code, or is this just a
> general sentiment of this not being a supported use-case?

It is not supported, and trying to do it anyway can crash: it can use
random stack contents as pointer! Not really "random" of course, but
where it thinks to find a pointer into the previous frame, which is not
something it can rely on (unless the ABI guarantees it somehow).

See gcc.gnu.org/PR60109 for example.

> > > Unless we can get some strong guarantees from compiler folk such that we
> > > can guarantee a specific function acts boundary for unwinding (and
> > > doesn't itself get split, etc), the only reliable way I can think to
> > > solve this requires an assembly trampoline. Whatever we do is liable to
> > > need some invasive rework.
> >
> > You cannot get such a guarantee, other than not letting the compiler
> > see into the routine at all, like with assembler code (not inline asm,
> > real assembler code).
>
> If we cannot reliably ensure this then I'm happy to go write an assembly
> trampoline to snapshot the state at a function call boundary (where our
> procedure call standard mandates the state of the LR, FP, and frame
> records pointed to by the FP).

Is the frame pointer required?!

> This'll require reworking a reasonable
> amount of code cross-architecture, so I'll need to get some more
> concrete justification (e.g. examples of things that can go wrong in
> practice).

Say you have a function that does dynamic stack allocation, then there
is usually no way to find the previous stack frame (without function-
specific knowledge). So __builtin_frame_address cannot work (it knows
nothing about frames further up).

Dynamic stack allocation (alloca, or variable length automatic arrays)
is just the most common and most convenient example; it is not the only
case you have problems here.

> > The real way forward is to bite the bullet and to no longer pretend you
> > can do a full backtrace from just the stack contents. You cannot.
>
> I think what you mean here is that there's no reliable way to handle the
> current/leaf function, right? If so I do agree.

No, I meant what I said.

There is the separate issue that you do not know where the return
address (etc.) is stored in a function that has not yet done a call
itself, sure. You cannot assume anything the ABI does not tell you you
can depend on.

> Beyond that I believe that arm64's frame records should be sufficient.

Do you have a simple linked list connecting all frames? The aarch64 GCC
port does not define anything special here (DYNAMIC_CHAIN_ADDRESS), so
the default will be used: every frame pointer has to point to the
previous one, no exceptions whatsoever.


Segher

2021-03-10 11:35:47

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v1] powerpc: Include running function as first entry in save_stack_trace() and friends

On Tue, Mar 09, 2021 at 04:05:32PM -0600, Segher Boessenkool wrote:
> Hi!
>
> On Tue, Mar 09, 2021 at 04:05:23PM +0000, Mark Rutland wrote:
> > On Thu, Mar 04, 2021 at 03:54:48PM -0600, Segher Boessenkool wrote:
> > > On Thu, Mar 04, 2021 at 02:57:30PM +0000, Mark Rutland wrote:
> > > > It looks like GCC is happy to give us the function-entry-time FP if we use
> > > > __builtin_frame_address(1),
> > >
> > > From the GCC manual:
> > > Calling this function with a nonzero argument can have
> > > unpredictable effects, including crashing the calling program. As
> > > a result, calls that are considered unsafe are diagnosed when the
> > > '-Wframe-address' option is in effect. Such calls should only be
> > > made in debugging situations.
> > >
> > > It *does* warn (the warning is in -Wall btw), on both powerpc and
> > > aarch64. Furthermore, using this builtin causes lousy code (it forces
> > > the use of a frame pointer, which we normally try very hard to optimise
> > > away, for good reason).
> > >
> > > And, that warning is not an idle warning. Non-zero arguments to
> > > __builtin_frame_address can crash the program. It won't on simpler
> > > functions, but there is no real definition of what a simpler function
> > > *is*. It is meant for debugging, not for production use (this is also
> > > why no one has bothered to make it faster).
> > >
> > > On Power it should work, but on pretty much any other arch it won't.
> >
> > I understand this is true generally, and cannot be relied upon in
> > portable code. However as you hint here for Power, I believe that on
> > arm64 __builtin_frame_address(1) shouldn't crash the program due to the
> > way frame records work on arm64, but I'll go check with some local
> > compiler folk. I agree that __builtin_frame_address(2) and beyond
> > certainly can, e.g. by NULL dereference and similar.
>
> I still do not know the aarch64 ABI well enough. If only I had time!
>
> > For context, why do you think this would work on power specifically? I
> > wonder if our rationale is similar.
>
> On most 64-bit Power ABIs all stack frames are connected together as a
> linked list (which is updated atomically, importantly). This makes it
> possible to always find all previous stack frames.

We have something similar on arm64, where the kernel depends on being
built with a frame pointer following the AAPCS frame pointer rules.

Every stack frame contains a "frame record" *somewhere* within that
stack frame, and the frame records are chained together as a linked
list. The frame pointer points at the most recent frame record (and this
is what __builtin_frame_address(0) returns).

The records themselves are basically:

struct record {
struct record *next;
unsigned long ret_addr;
};

At function call boundaries, we know that the FP is the caller's record
(or NULL for the first function), and the LR is the address the current
function should return to. Within a function with a stack frame, we can
access that function's record and the `next` field (equivalent to the FP
at the time of entry to the function) is what __builtin_frame_address(1)
should return.

> > Are you aware of anything in particular that breaks using
> > __builtin_frame_address(1) in non-portable code, or is this just a
> > general sentiment of this not being a supported use-case?
>
> It is not supported, and trying to do it anyway can crash: it can use
> random stack contents as pointer! Not really "random" of course, but
> where it thinks to find a pointer into the previous frame, which is not
> something it can rely on (unless the ABI guarantees it somehow).
>
> See gcc.gnu.org/PR60109 for example.

Sure; I see that being true generally (and Ramana noted that on 32-bit
arm a frame pointer wasn't mandated), but I think in this case we have a
stronger target (and configuration) specific guarantee.

> > > > Unless we can get some strong guarantees from compiler folk such that we
> > > > can guarantee a specific function acts boundary for unwinding (and
> > > > doesn't itself get split, etc), the only reliable way I can think to
> > > > solve this requires an assembly trampoline. Whatever we do is liable to
> > > > need some invasive rework.
> > >
> > > You cannot get such a guarantee, other than not letting the compiler
> > > see into the routine at all, like with assembler code (not inline asm,
> > > real assembler code).
> >
> > If we cannot reliably ensure this then I'm happy to go write an assembly
> > trampoline to snapshot the state at a function call boundary (where our
> > procedure call standard mandates the state of the LR, FP, and frame
> > records pointed to by the FP).
>
> Is the frame pointer required?!

The arm64 Linux port mandates frame pointers for kernel code. It is
generally possible to build code without frame pointers (e.g. userspace),
but doing that for kernel code would be a bug.

> > This'll require reworking a reasonable
> > amount of code cross-architecture, so I'll need to get some more
> > concrete justification (e.g. examples of things that can go wrong in
> > practice).
>
> Say you have a function that does dynamic stack allocation, then there
> is usually no way to find the previous stack frame (without function-
> specific knowledge). So __builtin_frame_address cannot work (it knows
> nothing about frames further up).
>
> Dynamic stack allocation (alloca, or variable length automatic arrays)
> is just the most common and most convenient example; it is not the only
> case you have problems here.

I agree with those as general concerns, but I don't think that affects
arm64's frame records, since their location within a stack frame is
immaterial given the chaining.

> > > The real way forward is to bite the bullet and to no longer pretend you
> > > can do a full backtrace from just the stack contents. You cannot.
> >
> > I think what you mean here is that there's no reliable way to handle the
> > current/leaf function, right? If so I do agree.
>
> No, I meant what I said.
>
> There is the separate issue that you do not know where the return
> address (etc.) is stored in a function that has not yet done a call
> itself, sure. You cannot assume anything the ABI does not tell you you
> can depend on.

This is in the frame record per the AAPCS.

> > Beyond that I believe that arm64's frame records should be sufficient.
>
> Do you have a simple linked list connecting all frames?

Yes.

Thanks,
Mark.

2021-03-10 17:46:22

by Segher Boessenkool

[permalink] [raw]
Subject: Re: [PATCH v1] powerpc: Include running function as first entry in save_stack_trace() and friends

Hi!

On Wed, Mar 10, 2021 at 11:32:20AM +0000, Mark Rutland wrote:
> On Tue, Mar 09, 2021 at 04:05:32PM -0600, Segher Boessenkool wrote:
> > On Tue, Mar 09, 2021 at 04:05:23PM +0000, Mark Rutland wrote:
> > > On Thu, Mar 04, 2021 at 03:54:48PM -0600, Segher Boessenkool wrote:
> > > > On Thu, Mar 04, 2021 at 02:57:30PM +0000, Mark Rutland wrote:
> > > > > It looks like GCC is happy to give us the function-entry-time FP if we use
> > > > > __builtin_frame_address(1),
> > > >
> > > > From the GCC manual:
> > > > Calling this function with a nonzero argument can have
> > > > unpredictable effects, including crashing the calling program. As
> > > > a result, calls that are considered unsafe are diagnosed when the
> > > > '-Wframe-address' option is in effect. Such calls should only be
> > > > made in debugging situations.
> > > >
> > > > It *does* warn (the warning is in -Wall btw), on both powerpc and
> > > > aarch64. Furthermore, using this builtin causes lousy code (it forces
> > > > the use of a frame pointer, which we normally try very hard to optimise
> > > > away, for good reason).
> > > >
> > > > And, that warning is not an idle warning. Non-zero arguments to
> > > > __builtin_frame_address can crash the program. It won't on simpler
> > > > functions, but there is no real definition of what a simpler function
> > > > *is*. It is meant for debugging, not for production use (this is also
> > > > why no one has bothered to make it faster).
> > > >
> > > > On Power it should work, but on pretty much any other arch it won't.
> > >
> > > I understand this is true generally, and cannot be relied upon in
> > > portable code. However as you hint here for Power, I believe that on
> > > arm64 __builtin_frame_address(1) shouldn't crash the program due to the
> > > way frame records work on arm64, but I'll go check with some local
> > > compiler folk. I agree that __builtin_frame_address(2) and beyond
> > > certainly can, e.g. by NULL dereference and similar.
> >
> > I still do not know the aarch64 ABI well enough. If only I had time!
> >
> > > For context, why do you think this would work on power specifically? I
> > > wonder if our rationale is similar.
> >
> > On most 64-bit Power ABIs all stack frames are connected together as a
> > linked list (which is updated atomically, importantly). This makes it
> > possible to always find all previous stack frames.
>
> We have something similar on arm64, where the kernel depends on being
> built with a frame pointer following the AAPCS frame pointer rules.

The huge difference is on Power this is about the stack itself: you do
not need a frame pointer at all for it (there is no specific register
named as frame pointer, even).

> Every stack frame contains a "frame record" *somewhere* within that
> stack frame, and the frame records are chained together as a linked
> list. The frame pointer points at the most recent frame record (and this
> is what __builtin_frame_address(0) returns).

> > See gcc.gnu.org/PR60109 for example.
>
> Sure; I see that being true generally (and Ramana noted that on 32-bit
> arm a frame pointer wasn't mandated), but I think in this case we have a
> stronger target (and configuration) specific guarantee.

It sounds like it, yes. You need to have a frame pointer in the ABI,
with pretty strong rules, and have everything follow those rules.

> > Is the frame pointer required?!
>
> The arm64 Linux port mandates frame pointers for kernel code. It is
> generally possible to build code without frame pointers (e.g. userspace),
> but doing that for kernel code would be a bug.

I see. And it even is less expensive to do this than on most machines,
because of register pair load/store instructions :-)

> > > > The real way forward is to bite the bullet and to no longer pretend you
> > > > can do a full backtrace from just the stack contents. You cannot.
> > >
> > > I think what you mean here is that there's no reliable way to handle the
> > > current/leaf function, right? If so I do agree.
> >
> > No, I meant what I said.
> >
> > There is the separate issue that you do not know where the return
> > address (etc.) is stored in a function that has not yet done a call
> > itself, sure. You cannot assume anything the ABI does not tell you you
> > can depend on.
>
> This is in the frame record per the AAPCS.

But you do not know where in the function it will store that. It often
can be optimised by the compiler to only store the LR and FP on paths
where a call will happen later, and there is no way (without DWARF info
or similar) to know whether that has happened yet or not.

This is a well-known problem of course. For the current function you
cannot know in general if there is an activation frame yet or not.


Segher