On Fri, Jul 10, 2020 at 07:08:25PM -0400, Steven Rostedt wrote:
> On Fri, 10 Jul 2020 15:38:44 +0200
> Peter Zijlstra <[email protected]> wrote:
> 
> > +static void __static_call_transform(void *insn, enum insn_type type, void 
> > *func)
> >  {
> > -   const void *code = text_gen_insn(opcode, insn, func);
> > +   int size = CALL_INSN_SIZE;
> > +   const void *code;
> >  
> > -   if (WARN_ONCE(*(u8 *)insn != opcode,
> > -                 "unexpected static call insn opcode 0x%x at %pS\n",
> > -                 opcode, insn))
> 
> I would still feel better if we did some sort of sanity check before
> just writing to the text. Confirm this is a jmp, call, ret or nop?

Something like so (on top of the next patch) ?

I'm not convinced it actually helps much, but if it makes you feel
better :-)


--- a/arch/x86/kernel/static_call.c
+++ b/arch/x86/kernel/static_call.c
@@ -56,15 +56,36 @@ static inline enum insn_type __sc_insn(b
        return 2*tail + null;
 }
 
+static void __static_call_validate(void *insn, bool tail)
+{
+       u8 opcode = *(u8 *)insn;
+
+       if (tail) {
+               if (opcode == JMP32_INSN_OPCODE ||
+                   opcode == RET_INSN_OPCODE)
+                       return;
+       } else {
+               if (opcode == CALL_INSN_OPCODE ||
+                   !memcmp(insn, ideal_nops[NOP_ATOMIC5], 5))
+                       return;
+       }
+
+       WARN_ONCE(1, "unexpected static_call insn opcode 0x%x at %pS\n", 
opcode, insn);
+}
+
 void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
 {
        mutex_lock(&text_mutex);
 
-       if (tramp)
+       if (tramp) {
+               __static_call_validate(tramp, true);
                __static_call_transform(tramp, __sc_insn(!func, true), func);
+       }
 
-       if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE) && site)
+       if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE) && site) {
+               __static_call_validate(site, tail);
                __static_call_transform(site, __sc_insn(!func, tail), func);
+       }
 
        mutex_unlock(&text_mutex);
 }

Reply via email to