Received: by 10.223.185.116 with SMTP id b49csp3283788wrg; Tue, 13 Feb 2018 00:03:57 -0800 (PST) X-Google-Smtp-Source: AH8x2269zE0bc4JH0nbsyHPaA1BDuV2xjgtAr6AAiWmrGhgw+82ZO1oCvra2e83z57v3Hk6fIabb X-Received: by 10.99.6.85 with SMTP id 82mr315837pgg.181.1518509036916; Tue, 13 Feb 2018 00:03:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518509036; cv=none; d=google.com; s=arc-20160816; b=EnxLnnvWx49nZI2zeFP3lugw/e4z5w6X5yjO5JDS5fhl+driuI6blMJ2IDtPqywDxq 2fLyQlYogvsoiJjxGKpjHPDTxqvltQLFwkrjcTOUJlQQdgDX8jWb5SloC8i8KykSNd66 YWTDqBWLQ8f15Tp3KyFM//2zrUaOm/CCsJC4up85hZI5lBcdHpXG4NVZxMJVjWQ1M6Bd usZiJ1Xy0sotPUPAtN20hxKBEA/aM3TIVRz1NTAiSKyBBunnpcKcia+2qd69lUs8s/jV ewGaCTO+VEMXt0nkR6JLMkP3L+ZNMSoLuvR6zOjb5jYS1c22tWiQzNRLr4fuy0f8tfjn wsHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject:arc-authentication-results; bh=DH8WblKYn8fzCXCPO0JPLzLgpkfcg5akTsIsFx/GfDY=; b=lo6UOSHtcRw21EPzFjGQFgbmqP5UgwC8VPGZuA2fRFJYMdKmtUzY33OwGciW5NgPFO k2LINqRQ6wiEyl6klhCOLH87X4Vqj9UOEBSqSv74JjArNejGOG8fdsXMg9OQ5XNGZg8m WNzAjnHzch/URtS1JpoEhUT2VVODiTT4ASH2fLTcYvVs+JSkIi2pNKz2cp60pOfvXfHx u8cMb0DVIoXWVz1ETCjx3XnpISCVwvuL6znPfY5/2ynVyGBCv+d5x1ASFA8VlPsx1XIP 8LbG66Fhzrx3c+FQ36uSEi7CfzRwRFiQmTyfbJLRql9AcsyZ6xe1FdRx+h8G0KMFPnoq 7H5w== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p10-v6si939491plo.810.2018.02.13.00.03.41; Tue, 13 Feb 2018 00:03:56 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933656AbeBMICd (ORCPT + 99 others); Tue, 13 Feb 2018 03:02:33 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48678 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933525AbeBMICb (ORCPT ); Tue, 13 Feb 2018 03:02:31 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2DC6C40FB62D; Tue, 13 Feb 2018 08:02:31 +0000 (UTC) Received: from [10.36.116.223] (ovpn-116-223.ams2.redhat.com [10.36.116.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 375E4AEDF6; Tue, 13 Feb 2018 08:02:28 +0000 (UTC) Subject: Re: [PATCH 2/2] x86/speculation: Support "Enhanced IBRS" on future CPUs To: David Woodhouse , tglx@linutronix.de, x86@kernel.org, kvm@vger.kernel.org, torvalds@linux-foundation.org, linux-kernel@vger.kernel.org, arjan.van.de.ven@intel.com, dave.hansen@intel.com References: <1518449255-2182-1-git-send-email-dwmw@amazon.co.uk> <1518449255-2182-2-git-send-email-dwmw@amazon.co.uk> From: Paolo Bonzini Message-ID: <7e2e5ad1-49b6-1fdb-4a62-8ad6aefc30a0@redhat.com> Date: Tue, 13 Feb 2018 09:02:25 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <1518449255-2182-2-git-send-email-dwmw@amazon.co.uk> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 13 Feb 2018 08:02:31 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 13 Feb 2018 08:02:31 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'pbonzini@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12/02/2018 16:27, David Woodhouse wrote: > The original IBRS hack in microcode is horribly slow. For the next > generation of CPUs, as a stopgap until we get a proper fix, Intel > promise an "Enhanced IBRS" which will be fast. > > The assumption is that predictions in the BTB/RSB will be tagged with > the VMX mode and ring that they were learned in, and thus the CPU will > avoid consuming unsafe predictions without a performance penalty. > > Intel's documentation says that it is still required to set the IBRS bit > in the SPEC_CTRL MSR and ensure that it remains set. > > Cope with this by trapping and emulating *all* access to SPEC_CTRL from > KVM guests when the IBRS_ALL feature is present, so it can never be > turned off. Guests who see IBRS_ALL should never do anything except > turn it on at boot anyway. And if they didn't know about IBRS_ALL and > they keep frobbing IBRS on every kernel entry/exit... well the vmexit > for a no-op is probably going to be faster than they were expecting > anyway, so they'll live. > > Signed-off-by: David Woodhouse > Acked-by: Arjan van de Ven > --- > arch/x86/include/asm/nospec-branch.h | 9 ++++++++- > arch/x86/kernel/cpu/bugs.c | 16 ++++++++++++++-- > arch/x86/kvm/vmx.c | 17 ++++++++++------- > 3 files changed, 32 insertions(+), 10 deletions(-) > > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > index 788c4da..524bb86 100644 > --- a/arch/x86/include/asm/nospec-branch.h > +++ b/arch/x86/include/asm/nospec-branch.h > @@ -140,9 +140,16 @@ enum spectre_v2_mitigation { > SPECTRE_V2_RETPOLINE_MINIMAL_AMD, > SPECTRE_V2_RETPOLINE_GENERIC, > SPECTRE_V2_RETPOLINE_AMD, > - SPECTRE_V2_IBRS, > + SPECTRE_V2_IBRS_ALL, > }; > > +extern enum spectre_v2_mitigation spectre_v2_enabled; > + > +static inline bool spectre_v2_ibrs_all(void) > +{ > + return spectre_v2_enabled == SPECTRE_V2_IBRS_ALL; > +} > + > extern char __indirect_thunk_start[]; > extern char __indirect_thunk_end[]; > > diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c > index debcdda..047538a 100644 > --- a/arch/x86/kernel/cpu/bugs.c > +++ b/arch/x86/kernel/cpu/bugs.c > @@ -88,12 +88,13 @@ static const char *spectre_v2_strings[] = { > [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline", > [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline", > [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline", > + [SPECTRE_V2_IBRS_ALL] = "Mitigation: Enhanced IBRS", > }; > > #undef pr_fmt > #define pr_fmt(fmt) "Spectre V2 : " fmt > > -static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE; > +enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE; > > #ifdef RETPOLINE > static bool spectre_v2_bad_module; > @@ -237,6 +238,16 @@ static void __init spectre_v2_select_mitigation(void) > > case SPECTRE_V2_CMD_FORCE: > case SPECTRE_V2_CMD_AUTO: > + if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) { > + u64 ia32_cap = 0; > + > + rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap); > + if (ia32_cap & ARCH_CAP_IBRS_ALL) { > + mode = SPECTRE_V2_IBRS_ALL; > + wrmsrl(MSR_IA32_SPEC_CTRL, SPEC_CTRL_IBRS); > + goto ibrs_all; > + } > + } > if (IS_ENABLED(CONFIG_RETPOLINE)) > goto retpoline_auto; > break; > @@ -274,6 +285,7 @@ static void __init spectre_v2_select_mitigation(void) > setup_force_cpu_cap(X86_FEATURE_RETPOLINE); > } > > + ibrs_all: > spectre_v2_enabled = mode; > pr_info("%s\n", spectre_v2_strings[mode]); > > @@ -306,7 +318,7 @@ static void __init spectre_v2_select_mitigation(void) > * branches. But we don't know whether the firmware is safe, so > * use IBRS to protect against that: > */ > - if (boot_cpu_has(X86_FEATURE_IBRS)) { > + if (mode != SPECTRE_V2_IBRS_ALL && boot_cpu_has(X86_FEATURE_IBRS)) { > setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW); > pr_info("Spectre mitigation: Restricting branch speculation (enabling IBRS) for firmware calls\n"); > } > diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c > index 91e3539..d99ba9b 100644 > --- a/arch/x86/kvm/vmx.c > +++ b/arch/x86/kvm/vmx.c > @@ -3419,13 +3419,14 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > > vmx->spec_ctrl = data; > > - if (!data) > + if (!data && !spectre_v2_ibrs_all()) > break; This should check the value of IBRS_ALL in the VM, not in the host. > /* > * For non-nested: > * When it's written (to non-zero) for the first time, pass > - * it through. > + * it through unless we have IBRS_ALL and it should just be > + * set for ever. > * > * For nested: > * The handling of the MSR bitmap for L2 guests is done in > @@ -9441,7 +9442,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) > * is no need to worry about the conditional branch over the wrmsr > * being speculatively taken. > */ > - if (vmx->spec_ctrl) > + if (vmx->spec_ctrl && !spectre_v2_ibrs_all()) > wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); Same here, and it should also be if (vmx->spec_ctrl != host_spec_ctrl()) where static inline int host_spec_ctrl() { return spectre_v2_enabled != SPECTRE_V2_NONE; } Likewise below. Paolo > vmx->__launched = vmx->loaded_vmcs->launched; > @@ -9577,11 +9578,13 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) > * If the L02 MSR bitmap does not intercept the MSR, then we need to > * save it. > */ > - if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)) > - rdmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); > + if (!spectre_v2_ibrs_all) { > + if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)) > + rdmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); > > - if (vmx->spec_ctrl) > - wrmsrl(MSR_IA32_SPEC_CTRL, 0); > + if (vmx->spec_ctrl) > + wrmsrl(MSR_IA32_SPEC_CTRL, 0); > + } > > /* Eliminate branch target predictions from guest mode */ > vmexit_fill_RSB(); >