Re: [PATCH v2] objtool,ftrace: Implement UNWIND_HINT_RET_OFFSET

From: Peter Zijlstra
Date: Wed Apr 01 2020 - 13:09:33 EST


On Wed, Apr 01, 2020 at 04:43:35PM +0100, Julien Thierry wrote:

> > +static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
> > {
> > + u8 ret_offset = insn->ret_offset;
> > int i;
> >
> > - if (state->cfa.base != initial_func_cfi.cfa.base ||
> > - state->cfa.offset != initial_func_cfi.cfa.offset ||
> > - state->stack_size != initial_func_cfi.cfa.offset ||
> > - state->drap)
> > + if (state->cfa.base != initial_func_cfi.cfa.base || state->drap)
> > + return true;
> > +
> > + if (state->cfa.offset != initial_func_cfi.cfa.offset &&
> > + !(ret_offset && state->cfa.offset == initial_func_cfi.cfa.offset + ret_offset))
>
> Isn't that the same thing as "state->cfa.offset !=
> initial_func_cfi.cfa.offset + ret_offset" ?

I'm confused on what cfa.offset is, sometimes it increase with
stack_size, sometimes it doesn't.

ISTR that for the ftrace case it was indeed cfa.offset + 8, but for the
IRET case below (where it is now not used anymore) it was cfa.offset
(not cfa.offset + 40, which I was expecting).

> > + return true;
> > +
> > + if (state->stack_size != initial_func_cfi.cfa.offset + ret_offset)
> > return true;
> >
> > - for (i = 0; i < CFI_NUM_REGS; i++)
> > + for (i = 0; i < CFI_NUM_REGS; i++) {
> > if (state->regs[i].base != initial_func_cfi.regs[i].base ||
> > state->regs[i].offset != initial_func_cfi.regs[i].offset)
> > return true;
> > + }
> >
> > return false;
> > }

> > @@ -2185,6 +2148,13 @@ static int validate_branch(struct objtoo
> >
> > break;
> >
> > + case INSN_EXCEPTION_RETURN:
> > + if (func) {
> > + state.stack_size -= arch_exception_frame_size;
> > + break;
>
> Why break instead of returning? Shouldn't an exception return mark the end
> of a branch (whether inside or outside a function) ?
>
> Here it seems it will continue to the next instruction which might have been
> unreachable.

The code in question (x86's sync_core()), is an exception return to
self. It pushes an exception frame that points to right after the
exception return instruction.

This is the only usage of IRET in STT_FUNC symbols.

So rather than teaching objtool how to interpret the whole
push;push;push;push;push;iret sequence, teach it how big the frame is
(arch_exception_frame_size) and let it continue.

All the other (real) IRETs are in STT_NOTYPE in the entry assembly.

> > + }
> > +
> > + /* fallthrough */
>
> What is the purpose of the fallthrough here? If the exception return was in
> a function, it carried on to the next instruction, so it won't use the
> WARN_FUNC(). So, if I'm looking at the right version of the code only the
> "return 0;" will be used. And, unless my previous comment is wrong, I'd
> argue that we should return both for func and !func.

That came from the fact that we split it out of INSN_CONTEXT_SWITCH.
You're right that it has now reduced to just return 0.

> > case INSN_CONTEXT_SWITCH:
> > if (func && (!next_insn || !next_insn->hint)) {
> > WARN_FUNC("unsupported instruction in callable function",