Received: by 10.223.176.46 with SMTP id f43csp2052757wra; Sun, 21 Jan 2018 10:06:59 -0800 (PST) X-Google-Smtp-Source: AH8x224anX/9m/zuXINWx3S0EnK1WJY5X3On6xdg9X0s1h2tgrjfDztbT/JZf8faXUUOdH9tUUns X-Received: by 10.98.237.6 with SMTP id u6mr5957629pfh.190.1516558019324; Sun, 21 Jan 2018 10:06:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516558019; cv=none; d=google.com; s=arc-20160816; b=OtJ9hVQa7KrnD6IauxJ784RNdlPT1PjkQ948jhtt0FpTp/8xhcNhz7IFGQtuMzd/Le /p3uQa9Lyh81zi1Ra13vQKCcqzk9gNrhq7Ig2M5swzuLz58QHDljJn2cyH7Hv/5s3aJj t/seJp42Y6fpKLf3xlyOMYPoYA+8eRHqOxwXuzKTTjiHsgbsNbX50SU//aYcvdLr51Bc yHKenlTLuJVq7644+niGXBiegTnY3H8AwMh9dl+CFYHkSyse9c6BmP3oIXiCHwgl4JfP wx/PIlWym4WMnu2AkP9BJoQTxTivTfjCQG77KJRIsW6jyKi+3Rsy0PBTYoYA9EoZEkJ6 A6ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=if7Km25QOy8DHcDrybcIydJ1GnD0Mz9xr+ytn01t1J4=; b=Wq6XMS3aiCH9T198ngkdIDA+l+drvauDatKhDi+6qC+yoyrngjAYDMjeEcC8AY939A gW6GsdUI2fSbnQ4AuenVHIzxo9vL7RTLorLU9ejZHbqKts9dR7AC23f0U1o4Ivp1KHsr WVPm+GhVjBt9qNEwAJwqU5R/05vFIN5CfvRUEUvliHFdQ53UoGukt7wvOG47Uy7cAfSf IyTg7dE5+li5RDKn5XYVPSSzOGpxxSkUE1KMV3kzD+7FH/0+qV7eiEl3L63+6rQ4W69Y r0evXCNAuuWTFyRn9vxJBSW18U+bhWz6x1qCAZxVsGEquDTEB7tgzcJPDyZDWE8Lbs2r luug== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t10si7554962pfk.153.2018.01.21.10.06.45; Sun, 21 Jan 2018 10:06:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751349AbeAUSG0 (ORCPT + 99 others); Sun, 21 Jan 2018 13:06:26 -0500 Received: from mail.skyhub.de ([5.9.137.197]:49474 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750954AbeAUSGZ (ORCPT ); Sun, 21 Jan 2018 13:06:25 -0500 X-Virus-Scanned: Nedap ESD1 at mail.skyhub.de Received: from mail.skyhub.de ([127.0.0.1]) by localhost (blast.alien8.de [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 5iF1Qc-GENrg; Sun, 21 Jan 2018 19:06:24 +0100 (CET) Received: from pd.tnic (p200300EC2BEE1B001C757E499366AA02.dip0.t-ipconnect.de [IPv6:2003:ec:2bee:1b00:1c75:7e49:9366:aa02]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id E7B9E1EC02C4; Sun, 21 Jan 2018 19:06:23 +0100 (CET) Date: Sun, 21 Jan 2018 19:06:21 +0100 From: Borislav Petkov To: David Woodhouse Cc: arjan@linux.intel.com, tglx@linutronix.de, karahmed@amazon.de, x86@kernel.org, linux-kernel@vger.kernel.org, tim.c.chen@linux.intel.com, peterz@infradead.org, pbonzini@redhat.com, ak@linux.intel.com, torvalds@linux-foundation.org, gregkh@linux-foundation.org Subject: Re: [PATCH v2 5/8] x86/speculation: Add basic support for IBPB Message-ID: <20180121180621.ufmc5m7nr6v4tjvc@pd.tnic> References: <1516528149-9370-1-git-send-email-dwmw@amazon.co.uk> <1516528149-9370-6-git-send-email-dwmw@amazon.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1516528149-9370-6-git-send-email-dwmw@amazon.co.uk> User-Agent: NeoMutt/20170609 (1.8.3) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jan 21, 2018 at 09:49:06AM +0000, David Woodhouse wrote: > From: Thomas Gleixner > > Expose indirect_branch_prediction_barrier() for use in subsequent patches. > > [karahmed: remove the special-casing of skylake for using IBPB (wtf?), > switch to using ALTERNATIVES instead of static_cpu_has] > [dwmw2: set up ax/cx/dx in the asm too so it gets NOP'd out] > > Signed-off-by: Thomas Gleixner > Signed-off-by: KarimAllah Ahmed > Signed-off-by: David Woodhouse > --- > arch/x86/include/asm/cpufeatures.h | 1 + > arch/x86/include/asm/nospec-branch.h | 16 ++++++++++++++++ > arch/x86/kernel/cpu/bugs.c | 7 +++++++ > 3 files changed, 24 insertions(+) > > diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h > index 8c9e5c0..cf28399 100644 > --- a/arch/x86/include/asm/cpufeatures.h > +++ b/arch/x86/include/asm/cpufeatures.h > @@ -207,6 +207,7 @@ > #define X86_FEATURE_RETPOLINE_AMD ( 7*32+13) /* AMD Retpoline mitigation for Spectre variant 2 */ > #define X86_FEATURE_INTEL_PPIN ( 7*32+14) /* Intel Processor Inventory Number */ > > +#define X86_FEATURE_IBPB ( 7*32+16) /* Using Indirect Branch Prediction Barrier */ Right, and as AMD has a separate bit for this in CPUID_80000008_EBX[12], we probably don't really need the synthetic bit here but simply use the one at (13*32+12) - word 13. > #define X86_FEATURE_AMD_PRED_CMD ( 7*32+17) /* Prediction Command MSR (AMD) */ > #define X86_FEATURE_MBA ( 7*32+18) /* Memory Bandwidth Allocation */ > #define X86_FEATURE_RSB_CTXSW ( 7*32+19) /* Fill RSB on context switches */ > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > index 4ad4108..c333c95 100644 > --- a/arch/x86/include/asm/nospec-branch.h > +++ b/arch/x86/include/asm/nospec-branch.h > @@ -218,5 +218,21 @@ static inline void vmexit_fill_RSB(void) > #endif > } > > +static inline void indirect_branch_prediction_barrier(void) I like ibp_barrier() better. > +{ > + unsigned long ax, cx, dx; > + > + asm volatile(ALTERNATIVE("", > + "movl %[msr], %%ecx\n\t" > + "movl %[val], %%eax\n\t" > + "movl $0, %%edx\n\t" > + "wrmsr", > + X86_FEATURE_IBPB) > + : "=a" (ax), "=c" (cx), "=d" (dx) > + : [msr] "i" (MSR_IA32_PRED_CMD), > + [val] "i" (PRED_CMD_IBPB) > + : "memory"); > +} Btw, we can simplify this a bit by dropping the inputs and marking the 3 GPRs as clobbered: alternative_input("", "mov $0x49, %%ecx\n\t" "mov $1, %%eax\n\t" "xor %%edx, %%edx\n\t" "wrmsr\n\t", X86_FEATURE_IBPB, ASM_NO_INPUT_CLOBBER("eax", "ecx", "edx", "memory")); The "memory" clobber is probably not really needed but it wouldn't hurt... Also, above says: > switch to using ALTERNATIVES instead of static_cpu_has] Why? if (static_cpu_has(X86_FEATURE_IBPB)) wrmsr(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, 0); It can't get any more readable than this. Why even f*ck with alternatives? > + > #endif /* __ASSEMBLY__ */ > #endif /* __NOSPEC_BRANCH_H__ */ > diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c > index 390b3dc..96548ff 100644 > --- a/arch/x86/kernel/cpu/bugs.c > +++ b/arch/x86/kernel/cpu/bugs.c > @@ -249,6 +249,13 @@ static void __init spectre_v2_select_mitigation(void) > setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW); > pr_info("Filling RSB on context switch\n"); > } > + > + /* Initialize Indirect Branch Prediction Barrier if supported */ > + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL) || > + boot_cpu_has(X86_FEATURE_AMD_PRED_CMD)) { > + setup_force_cpu_cap(X86_FEATURE_IBPB); > + pr_info("Enabling Indirect Branch Prediction Barrier\n"); We don't really need the pr_info as "ibpb" will appear in /proc/cpuinfo. -- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.