Return-Path: Received: from mail.linutronix.de (146.0.238.70:993) by crypto-ml.lab.linutronix.de with IMAP4-SSL for ; 15 Jul 2018 13:59:16 -0000 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by Galois.linutronix.de with esmtps (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1fehWg-000480-0P for speck@linutronix.de; Sun, 15 Jul 2018 15:57:26 +0200 Received: by mail-wr1-x42c.google.com with SMTP id h9-v6so29348062wro.3 for ; Sun, 15 Jul 2018 06:57:26 -0700 (PDT) Received: from donizetti.redhat.com (dynamic-adsl-78-12-156-57.clienti.tiscali.it. [78.12.156.57]) by smtp.gmail.com with ESMTPSA id m10-v6sm29902662wrj.35.2018.07.15.06.57.19 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 15 Jul 2018 06:57:19 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Subject: [MODERATED] [PATCH] L1TF KVM ARCH_CAPABILITIES #1 Date: Sun, 15 Jul 2018 15:57:13 +0200 Message-Id: <20180715135716.1874-2-pbonzini@redhat.com> In-Reply-To: <20180715135716.1874-1-pbonzini@redhat.com> References: <20180715135716.1874-1-pbonzini@redhat.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit MIME-Version: 1.0 To: speck@linutronix.de List-ID: If bit 3 of ARCH_CAPABILITIES is set, the L1TF bug is present but the hypervisor does not need to flush the L1D cache on vmentry. To prepare for that, use l1tf_vmx_mitigation to test for the EPT variant of the bug in KVM. boot_cpu_has(X86_BUG_L1TF) is only used for the non-hypervisor case. Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/vmx.h | 1 + arch/x86/kernel/cpu/bugs.c | 18 ++++++++++++------ arch/x86/kvm/vmx.c | 33 +++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index 94a8547d915b..3beb2f6f1a94 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -579,6 +579,7 @@ enum vmx_l1d_flush_state { VMENTER_L1D_FLUSH_COND, VMENTER_L1D_FLUSH_ALWAYS, VMENTER_L1D_FLUSH_EPT_DISABLED, + VMENTER_L1D_FLUSH_NOT_VULNERABLE, }; extern enum vmx_l1d_flush_state l1tf_vmx_mitigation; diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index d63cb1501784..4e06eee987d4 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -672,8 +672,10 @@ static void __init l1tf_select_mitigation(void) { u64 half_pa; - if (!boot_cpu_has_bug(X86_BUG_L1TF)) + if (!boot_cpu_has_bug(X86_BUG_L1TF)) { + l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_VULNERABLE; return; + } switch (l1tf_mitigation) { case L1TF_MITIGATION_OFF: @@ -741,15 +743,19 @@ static int __init l1tf_cmdline(char *str) #if IS_ENABLED(CONFIG_KVM_INTEL) static const char *l1tf_vmx_states[] = { - [VMENTER_L1D_FLUSH_AUTO] = "auto", - [VMENTER_L1D_FLUSH_NEVER] = "vulnerable", - [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes", - [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes", - [VMENTER_L1D_FLUSH_EPT_DISABLED]= "EPT disabled" + [VMENTER_L1D_FLUSH_AUTO] = "auto", + [VMENTER_L1D_FLUSH_NEVER] = "vulnerable", + [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes", + [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes", + [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled", + [VMENTER_L1D_FLUSH_NOT_VULNERABLE] = "not vulnerable", }; static ssize_t l1tf_show_state(char *buf) { + if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NOT_VULNERABLE) + return sprintf(buf, "%s; VMX: not vulnerable\n", L1TF_DEFAULT_MSG); + if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG); diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index c5c0118b126d..bf0801399cb4 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -208,14 +208,17 @@ #define L1D_CACHE_ORDER 4 static void *vmx_l1d_flush_pages; -static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) +static bool vmx_l1tf_vulnerable(void) +{ + return l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NOT_VULNERABLE && + l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_EPT_DISABLED; +} + +static int vmx_update_l1d_flush(enum vmx_l1d_flush_state l1tf) { struct page *page; - if (!enable_ept) { - l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED; - return 0; - } + WARN_ON(!vmx_l1tf_vulnerable()); /* If set to auto use the default l1tf mitigation method */ if (l1tf == VMENTER_L1D_FLUSH_AUTO) { @@ -259,6 +262,16 @@ static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) return 0; } +static int vmx_setup_l1d_flush(void) +{ + if (!enable_ept) { + l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED; + return 0; + } + + return vmx_update_l1d_flush(vmentry_l1d_flush_param); +} + static int vmentry_l1d_flush_parse(const char *s) { unsigned int i; @@ -276,7 +289,7 @@ static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) { int l1tf, ret; - if (!boot_cpu_has(X86_BUG_L1TF)) + if (!vmx_l1tf_vulnerable()) return 0; l1tf = vmentry_l1d_flush_parse(s); @@ -295,7 +308,7 @@ static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) } mutex_lock(&vmx_l1d_flush_mutex); - ret = vmx_setup_l1d_flush(l1tf); + ret = vmx_update_l1d_flush(l1tf); mutex_unlock(&vmx_l1d_flush_mutex); return ret; } @@ -10592,7 +10605,7 @@ static int vmx_vm_init(struct kvm *kvm) if (!ple_gap) kvm->arch.pause_in_guest = true; - if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) { + if (vmx_l1tf_vulnerable()) { switch (l1tf_mitigation) { case L1TF_MITIGATION_OFF: case L1TF_MITIGATION_FLUSH_NOWARN: @@ -13383,8 +13396,8 @@ static int __init vmx_init(void) * contain 'auto' which will be turned into the default 'cond' * mitigation mode. */ - if (boot_cpu_has(X86_BUG_L1TF)) { - r = vmx_setup_l1d_flush(vmentry_l1d_flush_param); + if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NOT_VULNERABLE) { + r = vmx_setup_l1d_flush(); if (r) { vmx_exit(); return r; -- 1.8.3.1