From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965492AbcAUTEF (ORCPT ); Thu, 21 Jan 2016 14:04:05 -0500 Received: from terminus.zytor.com ([198.137.202.10]:57476 "EHLO mail.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965418AbcAUTEC (ORCPT ); Thu, 21 Jan 2016 14:04:02 -0500 Subject: Re: [PATCH 5/5] x86/alternatives: Discard dynamic check after init To: Borislav Petkov References: <1453401286-26966-1-git-send-email-bp@alien8.de> <1453401286-26966-6-git-send-email-bp@alien8.de> Cc: Andy Lutomirski , Brian Gerst , X86 ML , LKML From: "H. Peter Anvin" Message-ID: <56A12B85.6030401@zytor.com> Date: Thu, 21 Jan 2016 11:03:33 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 MIME-Version: 1.0 In-Reply-To: <1453401286-26966-6-git-send-email-bp@alien8.de> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 01/21/16 10:34, Borislav Petkov wrote: > From: Brian Gerst > > Move the code to do the dynamic check to the altinstr_aux section so > that it is discarded after alternatives have run and a static branch has > been chosen. > > Boris: change it to do TESTB, as hpa suggests. [Also document that this changes it from C code to assembly, which makes it *substantially* smaller while avoiding a completely unnecessary call to an out of line function.] > * Static testing of CPU features. Used the same as boot_cpu_has(). > @@ -141,7 +140,7 @@ extern bool __static_cpu_has(u16 bit); > static __always_inline __pure bool _static_cpu_has(u16 bit) > { > #ifdef CC_HAVE_ASM_GOTO > - asm_volatile_goto("1: jmp %l[t_dynamic]\n" > + asm_volatile_goto("1: jmp 6f\n" > "2:\n" > ".skip -(((5f-4f) - (2b-1b)) > 0) * " > "((5f-4f) - (2b-1b)),0x90\n" > @@ -166,13 +165,19 @@ static __always_inline __pure bool _static_cpu_has(u16 bit) > " .byte 0\n" /* repl len */ > " .byte 0\n" /* pad len */ > ".previous\n" > - : : "i" (bit), "i" (X86_FEATURE_ALWAYS) > - : : t_dynamic, t_no); > + ".section .altinstr_aux,\"ax\"\n" > + "6: testb %[bitnum],%[cap_word]\n" > + " jnz %l[t_yes]\n" > + " jmp %l[t_no]\n" > + ".previous\n" > + : : "i" (bit), "i" (X86_FEATURE_ALWAYS), > + [bitnum] "i" (1 << (bit & 7)), > + [cap_word] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3]) Change to [cap_byte] as we normally refer to this array by 32-bit words. > + : : t_yes, t_no); > + t_yes: > return true; > t_no: > return false; > - t_dynamic: > - return __static_cpu_has(bit); > #else > return boot_cpu_has(bit); > #endif /* CC_HAVE_ASM_GOTO */ > diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c > index a57ec0df9cc3..897c65bd3faa 100644 > --- a/arch/x86/kernel/cpu/common.c > +++ b/arch/x86/kernel/cpu/common.c > @@ -1483,12 +1483,6 @@ void warn_pre_alternatives(void) > EXPORT_SYMBOL_GPL(warn_pre_alternatives); > #endif > > -inline bool __static_cpu_has(u16 bit) > -{ > - return boot_cpu_has(bit); > -} > -EXPORT_SYMBOL_GPL(__static_cpu_has); > - > static void bsp_resume(void) > { > if (this_cpu->c_bsp_resume) >