Re: [PATCH v4 14/18] static_call: Add static_cond_call()

Josh Poimboeuf
2020-05-06T18:09:44Z

On Wed, May 06, 2020 at 07:58:52PM +0200, Peter Zijlstra wrote:
> On Wed, May 06, 2020 at 12:24:55PM -0500, Josh Poimboeuf wrote:
> > On Fri, May 01, 2020 at 10:29:03PM +0200, Peter Zijlstra wrote:
> > > +++ b/arch/x86/include/asm/static_call.h
> > > @@ -30,4 +30,14 @@
> > > ".size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(name) " \n" \
> > > ".popsection \n")
> > >
> > > +#define ARCH_DEFINE_STATIC_CALL_RETTRAMP(name) \
> > > + asm(".pushsection .static_call.text, \"ax\" \n" \
> > > + ".align 4 \n" \
> > > + ".globl " STATIC_CALL_TRAMP_STR(name) " \n" \
> > > + STATIC_CALL_TRAMP_STR(name) ": \n" \
> > > + " ret; nop; nop; nop; nop; \n" \
> > > + ".type " STATIC_CALL_TRAMP_STR(name) ", @function \n" \
> > > + ".size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(name) " \n" \
> > > + ".popsection \n")
> > > +
> >
> > The boilerplate in these two trampoline macros is identical except for
> > the actual instructions, maybe there could be a shared
> > __ARCH_DEFINE_STATIC_CALL_TRAMP(name, insns) macro which does most of
> > the dirty work.
>
> I'm afraid that'll just make it less readable :/

#define __ARCH_DEFINE_STATIC_CALL_TRAMP(name, insns) \
asm(".pushsection .static_call.text, \"ax\" \n" \
".align 4 \n" \
".globl " STATIC_CALL_TRAMP_STR(name) " \n" \
STATIC_CALL_TRAMP_STR(name) ": \n" \
insns " \n" \
".type " STATIC_CALL_TRAMP_STR(name) ", @function \n" \
".size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(name) " \n" \
".popsection \n")

#define ARCH_DEFINE_STATIC_CALL_TRAMP(name, func) \
__ARCH_DEFINE_STATIC_CALL_TRAMP(name, "jmp.d32 " # func)

#define ARCH_DEFINE_STATIC_CALL_RETTRAMP(name, func) \
__ARCH_DEFINE_STATIC_CALL_TRAMP(name, "ret; nop; nop; nop; nop")

I like it. Makes it easy to see the differences between the tramps.

> > > #endif /* _ASM_STATIC_CALL_H */
> > > --- a/arch/x86/kernel/static_call.c
> > > +++ b/arch/x86/kernel/static_call.c
> > > @@ -4,19 +4,41 @@
> > > #include <linux/bug.h>
> > > #include <asm/text-patching.h>
> > >
> > > -static void __static_call_transform(void *insn, u8 opcode, void *func)
> > > +enum insn_type {
> > > + call = 0, /* site call */
> > > + nop = 1, /* site cond-call */
> > > + jmp = 2, /* tramp / site tail-call */
> > > + ret = 3, /* tramp / site cond-tail-call */
> > > +};
> >
> > The lowercase enums threw me for a loop, I thought they were variables a
> > few times. Starting a new enum trend? :-)
>
> I can UPPERCASE them I suppose, not sure where this came from.

Just thought UPPERCASE was the standard... lowercase looks confusingly
like variables when referenced.

--
Josh