Re: [PATCH] On ppc64le we HAVE_RELIABLE_STACKTRACE

From: Nicholas Piggin
Date: Mon Dec 18 2017 - 21:46:53 EST


On Mon, 18 Dec 2017 12:56:22 -0600
Josh Poimboeuf <jpoimboe@xxxxxxxxxx> wrote:

> On Mon, Dec 18, 2017 at 03:33:34PM +1000, Nicholas Piggin wrote:
> > On Sun, 17 Dec 2017 20:58:54 -0600
> > Josh Poimboeuf <jpoimboe@xxxxxxxxxx> wrote:
> >
> > > On Fri, Dec 15, 2017 at 07:40:09PM +1000, Nicholas Piggin wrote:
> > > > On Tue, 12 Dec 2017 08:05:01 -0600
> > > > Josh Poimboeuf <jpoimboe@xxxxxxxxxx> wrote:
> > > >
> > > > > On Tue, Dec 12, 2017 at 12:39:12PM +0100, Torsten Duwe wrote:
> > > > > > Hi all,
> > > > > >
> > > > > > The "Power Architecture 64-Bit ELF V2 ABI" says in section 2.3.2.3:
> > > > > >
> > > > > > [...] There are several rules that must be adhered to in order to ensure
> > > > > > reliable and consistent call chain backtracing:
> > > > > >
> > > > > > * Before a function calls any other function, it shall establish its
> > > > > > own stack frame, whose size shall be a multiple of 16 bytes.
> > > > >
> > > > > What about leaf functions? If a leaf function doesn't establish a stack
> > > > > frame, and it has inline asm which contains a blr to another function,
> > > > > this ABI is broken.
> > >
> > > Oops, I meant to say "bl" instead of "blr".
> > >
> > > > > Also, even for non-leaf functions, is it possible for GCC to insert the
> > > > > inline asm before it sets up the stack frame? (This is an occasional
> > > > > problem on x86.)
> > > >
> > > > Inline asm must not have control transfer out of the statement unless
> > > > it is asm goto.
> > >
> > > Can inline asm have calls to other functions?
> >
> > I don't believe so.
>
> It's allowed on x86, I don't see why it wouldn't be allowed on powerpc.

It's not allowed in general, but there's a lot of architecture specific
differences there, so maybe x86 has an exception. I don't think such an
exception exists for powerpc.... If you know exactly how the code
generation works then you could write inline asm works.

> As you mentioned, GCC doesn't pay attention to what's inside asm("").

And as you mentioned, it doesn't set up the stack frame properly for
leaf functions that call others in asm. There's other concerns too like
different ABI versions (v1/v2) have different stack and calling
conventions.

> > > > > Also, what about hand-coded asm?
> > > >
> > > > Should follow the same rules if it uses the stack.
> > >
> > > How is that enforced?
> >
> > It's not, AFAIK. Gcc doesn't understand what's inside asm("").
>
> Here I was talking about .S files.

Not enforced, but you can assume hand coded asm will not set up a
non-standard stack frame and calling convention, that would be a
bug.

>
> > > > > > To me this sounds like the equivalent of HAVE_RELIABLE_STACKTRACE.
> > > > > > This patch may be unneccessarily limited to ppc64le, but OTOH the only
> > > > > > user of this flag so far is livepatching, which is only implemented on
> > > > > > PPCs with 64-LE, a.k.a. ELF ABI v2.
> > > > >
> > > > > In addition to fixing the above issues, the unwinder also needs to
> > > > > detect interrupts (i.e., preemption) and page faults on the stack of a
> > > > > blocked task. If a function were preempted before it created a stack
> > > > > frame, or if a leaf function blocked on a page fault, the stack trace
> > > > > will skip the function's caller, so such a trace will need to be
> > > > > reported to livepatch as unreliable.
> > > >
> > > > I don't think there is much problem there for powerpc. Stack frame
> > > > creation and function call with return pointer are each atomic.
> > >
> > > What if the function is interrupted before it creates the stack frame?
> > >
> >
> > Then there will be no stack frame, but you still get the caller address
> > because it's saved in LR register as part of the function call. Then
> > you get the caller's caller in its stack frame.
>
> Ok. So what about the interrupted function itself? Looking at the
> powerpc version of save_context_stack(), it doesn't do anything special
> for exception frames like checking regs->nip.
>
> Though it looks like that should be possible since show_stack() has a
> way to identify exception frames.
>

Yes, the low level interrupt code stores a marker in the stack frame
which identifies where an exception occurs.

Thanks,
Nick