x86/alternatives: Guard NOPs optimization
authorBorislav Petkov <[email protected]>
Sat, 4 Apr 2015 21:07:42 +0000 (23:07 +0200)
committerIngo Molnar <[email protected]>
Mon, 6 Apr 2015 07:24:09 +0000 (09:24 +0200)
Take a look at the first instruction byte before optimizing the NOP -
there might be something else there already, like the ALTERNATIVE_2()
in rdtsc_barrier() which NOPs out on AMD even though we just
patched in an MFENCE.

This happens because the alternatives sees X86_FEATURE_MFENCE_RDTSC,
AMD CPUs set it, we patch in the MFENCE and right afterwards it sees
X86_FEATURE_LFENCE_RDTSC which AMD CPUs don't set and we blindly
optimize the NOP.

Checking whether at least the first byte is 0x90 prevents that.

Signed-off-by: Borislav Petkov <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Brian Gerst <[email protected]>
Cc: Denys Vlasenko <[email protected]>
Cc: H. Peter Anvin <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
arch/x86/kernel/alternative.c

index 7c4ad005d7a0a5467ac980a5ae0d89f2ebdc4678..aef65319316065eab845f35141682c3550f18a22 100644 (file)
@@ -325,6 +325,9 @@ done:
 
 static void __init_or_module optimize_nops(struct alt_instr *a, u8 *instr)
 {
+       if (instr[0] != 0x90)
+               return;
+
        add_nops(instr + (a->instrlen - a->padlen), a->padlen);
 
        DUMP_BYTES(instr, a->instrlen, "%p: [%d:%d) optimized NOPs: ",