Re: [PATCH v2 4/4] x86/static_call: Add inline static call implementation for x86-64
From: Peter Zijlstra
Date: Mon Nov 26 2018 - 15:08:18 EST
On Mon, Nov 26, 2018 at 11:56:24AM -0600, Josh Poimboeuf wrote:
> diff --git a/arch/x86/kernel/static_call.c b/arch/x86/kernel/static_call.c
> index d3869295b88d..8fd6c8556750 100644
> --- a/arch/x86/kernel/static_call.c
> +++ b/arch/x86/kernel/static_call.c
> @@ -7,24 +7,19 @@
>
> #define CALL_INSN_SIZE 5
>
> +unsigned long bp_handler_call_return_addr;
>
> +static void static_call_bp_handler(struct pt_regs *regs)
> +{
> #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
> + /*
> + * Push the return address on the stack so the "called" function will
> + * return to immediately after the call site.
> + */
> + regs->sp -= sizeof(long);
> + *(unsigned long *)regs->sp = bp_handler_call_return_addr;
> #endif
> +}
>
> void arch_static_call_transform(void *site, void *tramp, void *func)
> {
> @@ -52,14 +47,12 @@ void arch_static_call_transform(void *site, void *tramp, void *func)
> opcodes[0] = insn_opcode;
> memcpy(&opcodes[1], &dest_relative, CALL_INSN_SIZE - 1);
>
> if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE))
> + bp_handler_call_return_addr = insn + CALL_INSN_SIZE;
>
> /* Patch the call site: */
> text_poke_bp((void *)insn, opcodes, CALL_INSN_SIZE,
> - static_call_bp_handler);
> + static_call_bp_handler, func);
>
> done:
> mutex_unlock(&text_mutex);
like maybe something along the lines of:
struct sc_data {
unsigned long ret;
unsigned long ip;
};
void sc_handler(struct pt_regs *regs, void *data)
{
struct sc_data *scd = data;
regs->sp -= sizeof(long);
*(unsigned long *)regs->sp = scd->ret;
regs->ip = scd->ip;
}
arch_static_call_transform()
{
...
scd = (struct sc_data){
.ret = insn + CALL_INSN_SIZE,
.ip = (unsigned long)func,
};
text_poke_bp((void *)insn, opcodes, CALL_INSN_SIZE,
sc_handler, (void *)&scd);
...
}