Skip to content

KVM: x86: SVM INVPCID fix, and cleanups

Sean Christopherson kvm lkml 1 version Applied

Status from this message: Queued, thanks.

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest3 patches 4 repliesApplied

Patches in v1 Applied

#Subject and filesSizeRepliesApplied
1/3KVM: SVM: Intercept INVPCID when it's disabled to inject #UDarch/x86/kvm/svm/svm.c+4 -41 reply
2/3KVM: x86: Advertise INVPCID by defaultarch/x86/kvm/cpuid.c arch/x86/kvm/svm/svm.c arch/x86/kvm/vmx/vmx.c+3 -61 reply
3/3KVM: VMX: Allow INVPCID in guest without PCIDarch/x86/kvm/vmx/vmx.c+0 -101 reply

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by.

PatchJim Mattson
1/3R-b
2/3R-b
3/3R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 0/3] KVM: x86: SVM INVPCID fix, and cleanups

Fix an INVPCID bug on SVM where it fails to injected a #UD when INVPCID is supported but not exposed to the guest. Do a bit of cleanup in patch 02 now that both VMX and SVM support PCID/INVPCID.

Patch 03 address KVM behavior that has long confused the heck out of me. KVM currently allows enabling INVPCID if and only if PCID is also enabled for the guest, the justification being that the guest will see incorrect fault behavior (#UD instead of #GP) due to the way the VMCS control works.

But that makes no sense, because nothing is forcing KVM to disable INVCPID in the VMCS when PCID is disabled. AFACIT, the myth was the result of a bug in the original _submission_, not even the original _commit_ was buggy.

Digging back, the very original submission had this code, where vmx_pcid_supported() was further conditioned on EPT being enabled. This would lead to the buggy scenario of unexpected #UD, as a host with PCID and INVCPID would fail to enable INVPCID if EPT was disabled.

Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+	if (vmx_pcid_supported()) {
+		exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
+		if (exec_control & SECONDARY_EXEC_ENABLE_INVPCID) {
+			best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
+			if (best && (best->ecx & bit(X86_FEATURE_PCID)))
+				vmx->invpcid_enabled = true;
+			else {
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+				exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
+				vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
+						exec_control);
+				best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
+				best->ecx &= ~bit(X86_FEATURE_INVPCID);
+			}
+		}
+	}

The incorrect behavior is especially problematic now that SVM also supports INVCPID, as KVM allows !PCID && INVPCID on SVM but not on VMX.

Patches to fix kvm-unit-tests are also incoming...

Sean Christopherson (3):
KVM: SVM: Intercept INVPCID when it's disabled to inject #UD
KVM: x86: Advertise INVPCID by default
KVM: VMX: Allow INVPCID in guest without PCID

 arch/x86/kvm/cpuid.c   |  2 +-
 arch/x86/kvm/svm/svm.c | 11 ++++-------
 arch/x86/kvm/vmx/vmx.c | 14 ++------------
 3 files changed, 7 insertions(+), 20 deletions(-)
Signature · 1 line
2.30.0.478.g8a0d178c01-goog