Skip to contentSkip to conversation

L1TF KVM ARCH_CAPABILITIES #0

PATCH In review Paolo Bonzini 2 participants 6 messages Aug 5, 2018

Subsystems (4)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

v3 cover L1TF KVM ARCH_CAPABILITIES #0

Support for ARCH_CAPABILITIES bit 3, which can already be used to disable the mitigations on a nested hypervisor. Patch 1 is already in Linus's tree.

Paolo Bonzini (4):
KVM: VMX: support MSR_IA32_ARCH_CAPABILITIES as a feature MSR
x86: simplify sysfs report of VMX L1TF vulnerability
x86: use ARCH_CAPABILITIES to skip L1D flush on vmentry
KVM: VMX: tell nested hypervisor to skip L1D flush on vmentry

 Documentation/admin-guide/l1tf.rst | 21 +++++++++++++++++++++
 arch/x86/include/asm/kvm_host.h    |  1 +
 arch/x86/include/asm/msr-index.h   |  1 +
 arch/x86/include/asm/vmx.h         |  1 +
 arch/x86/kernel/cpu/bugs.c         | 15 +++++++++++----
 arch/x86/kvm/vmx.c                 | 13 +++++++++++--
 arch/x86/kvm/x86.c                 | 28 +++++++++++++++++++++++++++-
 7 files changed, 73 insertions(+), 7 deletions(-)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v3 1/4 L1TF KVM ARCH_CAPABILITIES #1

This lets userspace read the MSR_IA32_ARCH_CAPABILITIES and check that all requested features are available on the host.

Message-Id: <[email protected]>

---
 arch/x86/kvm/x86.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +3 -1
index 902d535dff8f..79c8ca2c2ad9 100644
@@ -1098,6 +1098,7 @@ static u32 msr_based_features[] = {
 
 	MSR_F10H_DECFG,
 	MSR_IA32_UCODE_REV,
+	MSR_IA32_ARCH_CAPABILITIES,
 };
 
 static unsigned int num_msr_based_features;
@@ -1106,7 +1107,8 @@ static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
 {
 	switch (msr->index) {
 	case MSR_IA32_UCODE_REV:
-		rdmsrl(msr->index, msr->data);
+	case MSR_IA32_ARCH_CAPABILITIES:
+		rdmsrl_safe(msr->index, &msr->data);
 		break;
 	default:
 		if (kvm_x86_ops->get_msr_feature(msr))
Signature · 1 line
2.17.1
author maintainer
#2 lore raw

PATCH v3 2/4 L1TF KVM ARCH_CAPABILITIES #2

In reply to Paolo Bonzini

Three changes to the content of the sysfs file:

- If EPT is disabled, L1TF cannot be exploited even across threads on the same core, and SMT is irrelevant.

- If mitigation is completely disabled, and SMT is enabled, print "vulnerable" instead of "vulnerable, SMT vulnerable"

- reorder the two parts so that the main vulnerability state comes first and the detail on SMT is second.

---
 arch/x86/kernel/cpu/bugs.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/bugs.c +9 -3
index d63cb1501784..674db36a9f44 100644
@@ -753,9 +753,15 @@ static ssize_t l1tf_show_state(char *buf)
 	if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
 		return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
 
-	return sprintf(buf, "%s; VMX: SMT %s, L1D %s\n", L1TF_DEFAULT_MSG,
-		       cpu_smt_control == CPU_SMT_ENABLED ? "vulnerable" : "disabled",
-		       l1tf_vmx_states[l1tf_vmx_mitigation]);
+	if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
+	    (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
+	     cpu_smt_control == CPU_SMT_ENABLED))
+		return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
+			       l1tf_vmx_states[l1tf_vmx_mitigation]);
+
+	return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
+		       l1tf_vmx_states[l1tf_vmx_mitigation],
+		       cpu_smt_control == CPU_SMT_ENABLED ? "vulnerable" : "disabled");
 }
 #else
 static ssize_t l1tf_show_state(char *buf)
Signature · 1 line
2.17.1
author maintainer
#3 lore raw

PATCH v3 3/4 L1TF KVM ARCH_CAPABILITIES #3

In reply to Paolo Bonzini

Bit 3 of ARCH_CAPABILITIES tells a hypervisor that L1D flush on vmentry is not needed. Add a new value to enum vmx_l1d_flush_state, which is used either if there is no L1TF bug at all, or if bit 3 is set in ARCH_CAPABILITIES.

---
 arch/x86/include/asm/msr-index.h |  1 +
 arch/x86/include/asm/vmx.h       |  1 +
 arch/x86/kernel/cpu/bugs.c       |  3 ++-
 arch/x86/kvm/vmx.c               | 10 ++++++++++
 4 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +1 -0
index 0e7517089b80..4731f0cf97c5 100644
@@ -70,6 +70,7 @@
 #define MSR_IA32_ARCH_CAPABILITIES	0x0000010a
 #define ARCH_CAP_RDCL_NO		(1 << 0)   /* Not susceptible to Meltdown */
 #define ARCH_CAP_IBRS_ALL		(1 << 1)   /* Enhanced IBRS support */
+#define ARCH_CAP_SKIP_VMENTRY_L1DFLUSH	(1 << 3)   /* Skip L1D flush on vmentry */
 #define ARCH_CAP_SSB_NO			(1 << 4)   /*
 						    * Not susceptible to Speculative Store Bypass
 						    * attack, so no Speculative Store Bypass
arch/x86/include/asm/vmx.h +1 -0
index 94a8547d915b..ae262f55309f 100644
@@ -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_REQUIRED,
 };
 
 extern enum vmx_l1d_flush_state l1tf_vmx_mitigation;
arch/x86/kernel/cpu/bugs.c +2 -1
index 674db36a9f44..5ef80a3790c4 100644
@@ -745,7 +745,8 @@ static const char *l1tf_vmx_states[] = {
 	[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_EPT_DISABLED]= "EPT disabled",
+	[VMENTER_L1D_FLUSH_NOT_REQUIRED]= "flush not necessary"
 };
 
 static ssize_t l1tf_show_state(char *buf)
arch/x86/kvm/vmx.c +10 -0
index c5c0118b126d..d70c7de84a9a 100644
@@ -217,6 +217,16 @@ static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
 		return 0;
 	}
 
+       if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
+	       u64 msr;
+
+	       rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
+	       if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
+		       l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
+		       return 0;
+	       }
+       }
+
 	/* If set to auto use the default l1tf mitigation method */
 	if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
 		switch (l1tf_mitigation) {
Signature · 1 line
2.17.1
author maintainer
#4 lore raw

PATCH v3 4/4 L1TF KVM ARCH_CAPABILITIES #4

In reply to Paolo Bonzini

When nested virtualization is in use, VMENTER operations from the nested hypervisor into the nested guest will always be processed by the bare metal hypervisor, and KVM's "conditional cache flushes" mode in particular does a flush on nested vmentry. Therefore, include the "skip L1D flush on vmentry" bit in KVM's suggested ARCH_CAPABILITIES setting.

---
 Documentation/admin-guide/l1tf.rst | 21 +++++++++++++++++++++
 arch/x86/include/asm/kvm_host.h    |  1 +
 arch/x86/kvm/vmx.c                 |  3 +--
 arch/x86/kvm/x86.c                 | 26 +++++++++++++++++++++++++-
 4 files changed, 48 insertions(+), 3 deletions(-)
Documentation/admin-guide/l1tf.rst +21 -0
index 5adf7d7c2b4e..46765abba70c 100644
@@ -528,6 +528,27 @@ available:
     EPT can be disabled in the hypervisor via the 'kvm-intel.ept'
     parameter.
 
+3.4. Nested virtual machines
+""""""""""""""""""""""""""""
+
+When nested virtualization is in use, three operating systems are involved:
+the bare metal hypervisor, the nested hypervisor, and the nested virtual
+machine.  VMENTER operations from the nested hypervisor into the nested
+guest will always be processed by the bare metal hypervisor.  Therefore:
+
+When running as a bare metal hypervisor, instead, KVM will:
+
+ - flush the L1D cache on every switch from nested hypervisor to
+   nested virtual machine, so that the nested hypervisor's secrets
+   are not exposed to the nested virtual machine;
+
+ - flush the L1D cache on every switch from nested virtual machine to
+   nested hypervisor; this is a complex operation, and flushing the L1D
+   cache avoids that the bare metal hypervisor's secrets be exposed
+   to the nested virtual machine;
+
+ - instruct the nested hypervisor to not perform any L1D cache flush.
+
 
 .. _default_mitigations:
 
arch/x86/include/asm/kvm_host.h +1 -0
index 57d418061c55..e375011108d1 100644
@@ -1417,6 +1417,7 @@ int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
 void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);
 void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu);
 
+u64 kvm_get_arch_capabilities(void);
 void kvm_define_shared_msr(unsigned index, u32 msr);
 int kvm_set_shared_msr(unsigned index, u64 val, u64 mask);
 
arch/x86/kvm/vmx.c +1 -2
index d70c7de84a9a..87394fc21263 100644
@@ -6419,8 +6419,7 @@ static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
 		++vmx->nmsrs;
 	}
 
-	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
-		rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
+	vmx->arch_capabilities = kvm_get_arch_capabilities();
 
 	vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
 
arch/x86/kvm/x86.c +25 -1
index 79c8ca2c2ad9..a5caa5e5480c 100644
@@ -1103,11 +1103,35 @@ static u32 msr_based_features[] = {
 
 static unsigned int num_msr_based_features;
 
+u64 kvm_get_arch_capabilities(void)
+{
+	u64 data;
+
+	rdmsrl_safe(MSR_IA32_ARCH_CAPABILITIES, &data);
+
+	/*
+	 * If we're doing cache flushes (either "always" or "cond")
+	 * we will do one whenever the guest does a vmlaunch/vmresume.
+	 * If an outer hypervisor is doing the cache flush for us
+	 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
+	 * capability to the guest too, and if EPT is disabled we're not
+	 * vulnerable.  Overall, only VMENTER_L1D_FLUSH_NEVER will
+	 * require a nested hypervisor to do a flush of its own.
+	 */
+	if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
+		data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
+
+	return data;
+}
+EXPORT_SYMBOL_GPL(kvm_get_arch_capabilities);
+
 static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
 {
 	switch (msr->index) {
-	case MSR_IA32_UCODE_REV:
 	case MSR_IA32_ARCH_CAPABILITIES:
+		msr->data = kvm_get_arch_capabilities();
+		break;
+	case MSR_IA32_UCODE_REV:
 		rdmsrl_safe(msr->index, &msr->data);
 		break;
 	default:
Signature · 1 line
2.17.1
#5 lore raw
In reply to Paolo Bonzini

Paolo,

Quoted 3 lines · speck for Paolo Bonzini

Support for ARCH_CAPABILITIES bit 3, which can already be used to disable the mitigations on a nested hypervisor. Patch 1 is already in Linus's tree.

It's also in 4.17 stable, but 4.14 stable lacks it. IIRC you wanted to post a list of upstream commits which are prerequisite for bringing that in. I worked my way through it and picked out the following commits:

518e7b94817a ("KVM: X86: Allow userspace to define the microcode version") 66421c1ec340 ("KVM: X86: Introduce kvm_get_msr_feature()") d1d93fa90f1a ("KVM: SVM: Add MSR-based feature support for serializing LFENCE") 801e459a6f3a ("KVM: x86: Add a framework for supporting MSR-based features")

The LFENCE and UCODE one are not really required, but I thought they are useful in context of this mess.

Thanks,

tglx