From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753169AbbDDVJt (ORCPT ); Sat, 4 Apr 2015 17:09:49 -0400 Received: from mail.skyhub.de ([78.46.96.112]:52454 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752561AbbDDVJs (ORCPT ); Sat, 4 Apr 2015 17:09:48 -0400 From: Borislav Petkov To: Ingo Molnar Cc: X86 ML , LKML Subject: [PATCH] x86/alternatives: Guard NOPs optimization Date: Sat, 4 Apr 2015 23:07:42 +0200 Message-Id: <1428181662-18020-1-git-send-email-bp@alien8.de> X-Mailer: git-send-email 2.3.3 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Take a look at the first insn byte before optimizing the NOP - there might be something else 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 --- arch/x86/kernel/alternative.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 7c4ad005d7a0..aef653193160 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -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: ", -- 2.3.3