Skip to contentSkip to conversation

KVM: nSVM: Optimize nSVM TLB flushes

PATCH Superseded Yosry Ahmed 2 participants 45 messages Jul 28 – Aug 5

Subsystems (7)
Stable branch Documentation x86 KVM +3 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover KVM: nSVM: Optimize nSVM TLB flushes

This series optimizes TLB flushes for nested SVM by using and maintaining a separate ASID for L2 for each vCPU (similar to VMX's handling of VPIDs), rather than sharing the same ASID and flushing everything on every nested transition.

The series also drops the dynamic ASID allocation scheme and uses a static ASID per vCPU (or two if using nested), bringing it closer to how VMX handles VPIDs. Modern CPUs (Rome, Milan, Genoa, Turin) advertise 32K ASIDs, so there is no need for dynamic allocation.

The SEV and Hyper-V bits only got build tested.

Otherwise, this series was tested in two ways:
1. A new selftest:
- Tests most basic scenarios where L1 flushes the TLB for L2, as well as when KVM triggers a TLB flush for both L1 and L2.
- Fails reliably when injecting manual TLB bugs (e.g. commenting out a

    flush).

- Caught an actual bug when I was rebasing the series on top of Paolo's MMU structures rework.
- Works for both SVM and VMX.
- Was used to repro a separate nVMX TLB bug [1].

2. An AI generated script (800 lines of bash that I don't dare to read):
- Runs 10 L1 VMs, each with 4 L2 VMs inside.
- Each L2 VM runs a workload that writes to all memory and then validates its content.
- L2 VMs overcommit both memory and CPU in L1 (exercise L1-induced TLB flushes as well as L2 ASID switching).
- L0 periodically reclaims memory from L1 VMs proactively (exercise L0-induced TLB flushes).
- Fails semi-reliably when injecting manual TLB bugs.
- Caugh an actual bug when testing the fallback ASID logic with synthetically reduced number of ASIDs.

Overall, I think I am happy with the testing I put this series through, hence dropping the RFC tag at last.

Breakdown:
- Patch 1 is a bug fix that can be taken separately.
- Patches 2-11 implement the change to use static ASIDs per vCPU for SVM (including SEV-specific handling and fallback ASID optimization), and can also be taken separately if needed.
- Patches 12-27 add the L2 ASID and proper handling for it, leading to dropping the unconditional flushes on nested transitions.
- Patch 28 adds the nested TLB flush selftest.

RFC v2 -> v1:
- Dropped the RFC tag.
- Document KVM's handling of number of ASIDs in CPUID rather than increase the number of advertised ASIDs [Jim, Sean].
- Explicitly support reserved TLB tags rather than a minuimum TLB tag [Sean].
- Set an upper bound on the number of TLB tags [Sean].
- Allow reserving all ASIDs to allow allocating all ASIDs to SEV (if at all possible). [Sashiko].
- Actually handle TLB flushes for nested on SEV [Sean, Sashiko].
- Optimize fallback ASID flushes by tracking the last vCPU using it on each pCPU instead of flushing it on every VMRUN [Sean].
- Drop svm->nested.initialized and properly NULLify the vmcb02 pointer instead (and use it).
- Make sure servicing local TLB flushes is done when both the VMCB and guest_mode are consistent [Sashiko].
- Always initialize last_asid to 0 to flush the TLB on the first use of L2's ASID [Sashiko].
- Open-code TLB flush handling on nested VM-Enter and VM-Exit instead of burying it in svm_switch_vmcb() [Sean].
- Always flush the hardware ASID on nested transitions if L1 and L2 share it (e.g. when using the fallback ASID, or for SEV guests) to prevent TLB entry leakage between L1 and L2.
- Rename __kvm_mmu_invalidate_addr() to kvm_mmu_sync_addr() [Sean].

RFC v1: https://lore.kernel.org/lkml/[email protected]/
RFC v2: https://lore.kernel.org/kvm/[email protected]/

[1]https://lore.kernel.org/kvm/[email protected]/

Yosry Ahmed (28):
KVM: nSVM: Flush the TLB after forcefully leaving nested
KVM: SVM: Document number of ASIDs CPUID setting
KVM: VMX: Generalize VPID allocation to be vendor-neutral
KVM: x86/mmu: Support specifying reserved TLB tags
KVM: SVM: Add helpers to set/clear ASID flush in VMCB
KVM: SVM: Fallback to flush everything if FLUSHBYASID is not available
KVM: SVM: Duplicate pre-run ASID check for SEV and non-SEV guests
KVM: SEV: Do ASID initialization at VMCB initialization
KVM: SEV: Expose sev_get_asid() outside of sev.c
KVM: SVM: Use a static ASID per vCPU
KVM: SVM: Only flush the fallback ASID when used by a different vCPU
KVM: nSVM: Add a placeholder ASID for L2
KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb()
KVM: x86: hyper-v: Allow puring all TLB flush FIFOs
KVM: nSVM: Drop svm->nested.initialized
KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH
KVM: nSVM: Always switch VMCB before leaving guest mode
KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fns
KVM: nSVM: Service local TLB flushes before nested transitions
KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROL
KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12
KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enter
KVM: x86/mmu: Rename __kvm_mmu_invalidate_addr() to

    kvm_mmu_sync_addr()

KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the GVA
flush
KVM: nSVM: Flush L2's ASID when emulating INVLPGA
KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2
KVM: nSVM: Use different ASIDs for L1 and L2
KVM: selftests: Add a test for nested TLB flushes

 Documentation/virt/kvm/api.rst                |  20 +
 arch/x86/kvm/cpuid.c                          |   4 +-
 arch/x86/kvm/hyperv.h                         |  33 +-
 arch/x86/kvm/mmu.h                            |  11 +
 arch/x86/kvm/mmu/mmu.c                        | 126 ++++-
 arch/x86/kvm/svm/nested.c                     | 142 ++++--
 arch/x86/kvm/svm/sev.c                        |  16 +-
 arch/x86/kvm/svm/svm.c                        | 133 ++++--
 arch/x86/kvm/svm/svm.h                        |  54 ++-
 arch/x86/kvm/vmx/vmx.c                        |  40 +-
 arch/x86/kvm/vmx/vmx.h                        |  28 +-
 arch/x86/kvm/x86.c                            |   2 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/include/x86/svm.h |   5 +
 tools/testing/selftests/kvm/include/x86/vmx.h |  21 +
 .../selftests/kvm/x86/nested_tlb_flush_test.c | 432 ++++++++++++++++++
 16 files changed, 916 insertions(+), 152 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c

base-commit: 271255273d5ff348fe29d89fe4712b2f7f7907c3

Signature · 1 line
2.55.0.229.g6434b31f56-goog
#1 lore raw

PATCH 1/28 KVM: nSVM: Flush the TLB after forcefully leaving nested

KVM flushes the TLB on nested VM-Enter and nested VM-Exit, but not when forcefully leaving nested. In this case, L2 TLB entries can leak into L1. Flush the TLB after forcefully exiting L2, similar to nested VM-Exits.

Note that vmx_leave_nested() handles this correctly, as it reuses nested_vmx_vmexit(), which handles the necessary TLB flushes on an L2 -> L1 transition.

---
 arch/x86/kvm/svm/nested.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/nested.c +2 -0
index 5e3e280cd483e..75378feddb349 100644
@@ -1553,6 +1553,8 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
 
 		svm_switch_vmcb(svm, &svm->vmcb01);
 
+		nested_svm_transition_tlb_flush(vcpu);
+
 		nested_svm_uninit_mmu_context(vcpu);
 		vmcb_mark_all_dirty(svm->vmcb);
 
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#2 lore raw

PATCH 2/28 KVM: SVM: Document number of ASIDs CPUID setting

In reply to Yosry Ahmed

KVM arbitrarly advertises 8 ASIDs in KVM_GET_SUPPORTED_CPUID for historical reasons. Advertising a small number of ASIDs to L1 is currently harmless, as KVM (L0) uses the same ASID for both L1 and L2, and flushes that ASID on nested transitions.

However, following changes will add proper ASID emulation and a separate ASID for L2, minimizing the TLB flushes on nested transitions. At that point, a full TLB flush from a KVM guest (L1) would flush both L1 and L2 ASIDs, so should be avoided as much as possible by advertising more ASIDs to L1.

Increasing the value advertised by KVM is tricky, because a VMM copying the number of ASIDs from KVM_GET_SUPPORTED_CPUID may perceive a new VM with a higher number of ASIDs as incompatible with older KVM (which is not true).

Avoid changing the value advertised by KVM, but document that it is essentially meaningless and does not represent a limit or a hint. Also, more-or-less encourage userspace to advertise a higher value to avoid potential unnecessary TLB flushes from an L1 hypervisor.

---
 Documentation/virt/kvm/api.rst | 20 ++++++++++++++++++++
 arch/x86/kvm/cpuid.c           |  4 ++--
 2 files changed, 22 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +20 -0
index a5f9ee92f43e8..2d24bf708d77e 100644
@@ -9542,6 +9542,26 @@ the APIC ID is found in EDX for all subleaves of 0x0b and 0x1f, and in EAX
 for 0x8000001e; the latter also encodes the core id and node id in bits
 7:0 of EBX and ECX respectively.
 
+Number of ASIDs (nested SVM)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+CPUID[0x8000000A]:EBX reports the number of ASIDs supported by the processor
+on AMD CPUs. KVM's ``KVM_GET_SUPPORTED_CPUID`` hardcodes this value to 8 for
+historical reasons. This is value is arbitrary and does not represent a
+limit on the supported number of ASIDs, or even a reasonable default value.
+
+KVM allows userspace to set any value (up to 0xFFFFFFFF), and emulates those
+ASIDs by multiplexing them onto hardware ASIDs. AMD CPUs have only ever
+supported 3 values: 0x40, 0x8000, and 0x1000 -- with modern CPUs advertising
+0x8000.
+
+Advertising a small number of ASIDs could lead an L1 hypervisor to perform
+unnecessary TLB flushes (e.g. older KVM would flush the entire TLB when running
+out of ASIDs). Userspace should probably set the number of ASIDs to a
+reasonably high power of 2 value to remain consistent with hardware and avoid
+any misbehavior by L1 hypervisors, ideally one of the values advertised by
+actual hardware.
+
 Obsolete ioctls and capabilities
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
arch/x86/kvm/cpuid.c +2 -2
index 9e9cf6538a96e..8187a58fe6736 100644
@@ -1839,8 +1839,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 			break;
 		}
 		entry->eax = 1; /* SVM revision 1 */
-		entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
-				   ASID emulation to nested SVM */
+		entry->ebx = 8; /* Arbitrary value for historical reasons. KVM
+				   practically allows and ignores all values. */
 		entry->ecx = 0; /* Reserved */
 		cpuid_entry_override(entry, CPUID_8000_000A_EDX);
 		break;
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#4 lore raw

PATCH 3/28 KVM: VMX: Generalize VPID allocation to be vendor-neutral

In reply to Yosry Ahmed

In preparation for sharing with SVM, generalize the VMX VPID allocation code and move it to common code as a TLB tags allocator. Parameterize the TLB tags allocator by the number of tags, and allocate the bitmap dynamically. Opportunisitcally use guards to acquire the lock instead of spin_{lock/unlock}().

Keep the number of allowed tags capped at VMX's hardware cap, to avoid allocating a huge bitmap if L0 advertises a huge number of ASIDs to an L1 KVM. Realistically, the number of actual hardware ASIDs wouldn't be that large so there is no benefit.

Initialize the TLB tags allocator during hardware setup/unsetup, and reserve tag=0 during initialziation, similar to how VPID=0 is currently reserved in the VMX-specific bitmap during hardware setup.

Allow nr=0 to allow allocating all ASIDs to SEV on AMD without failing hardware setup, if at all possible, in which case any tag allocation fails (SEV won't use the tag allocator). The number of tags includes tag=0, which is not usable. The interface is a little confusing in that regard, but this will be changed soon when reserved tags are explicitly introduced.

Keep allocate_vpid() and free_vpid() as wrapper that check enable_vpid to avoid checking at all callsites, and add init_vpids() and destroy_vpids() to wrap init/destroy calls as well.

No functional change intended.

---
 arch/x86/kvm/mmu.h     |  8 +++++
 arch/x86/kvm/mmu/mmu.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c | 40 +++++-----------------
 arch/x86/kvm/vmx/vmx.h | 28 ++++++++++++---
 4 files changed, 119 insertions(+), 35 deletions(-)
arch/x86/kvm/mmu.h +8 -0
index 2ae7f9ed4cf86..de79e002edf8f 100644
@@ -410,4 +410,12 @@ static inline bool kvm_is_gfn_alias(struct kvm *kvm, gfn_t gfn)
 {
 	return gfn & kvm_gfn_direct_bits(kvm);
 }
+
+typedef unsigned int kvm_tlb_tag_t;
+
+int kvm_init_tlb_tags(unsigned int nr);
+void kvm_destroy_tlb_tags(void);
+kvm_tlb_tag_t kvm_alloc_tlb_tag(void);
+void kvm_free_tlb_tag(kvm_tlb_tag_t tag);
+
 #endif
arch/x86/kvm/mmu/mmu.c +78 -0

84 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.c +9 -31
index e4b9ac7fed9f0..ec28da31dd96b 100644
@@ -595,9 +595,6 @@ DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  */
 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
 
-static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
-static DEFINE_SPINLOCK(vmx_vpid_lock);
-
 struct vmcs_config vmcs_config __ro_after_init;
 struct vmx_capability vmx_capability __ro_after_init;
 
@@ -4077,31 +4074,6 @@ static void seg_setup(int seg)
 	vmcs_write32(sf->ar_bytes, ar);
 }
 
-int allocate_vpid(void)
-{
-	int vpid;
-
-	if (!enable_vpid)
-		return 0;
-	spin_lock(&vmx_vpid_lock);
-	vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
-	if (vpid < VMX_NR_VPIDS)
-		__set_bit(vpid, vmx_vpid_bitmap);
-	else
-		vpid = 0;
-	spin_unlock(&vmx_vpid_lock);
-	return vpid;
-}
-
-void free_vpid(int vpid)
-{
-	if (!enable_vpid || vpid == 0)
-		return;
-	spin_lock(&vmx_vpid_lock);
-	__clear_bit(vpid, vmx_vpid_bitmap);
-	spin_unlock(&vmx_vpid_lock);
-}
-
 static void vmx_msr_bitmap_l01_changed(struct vcpu_vmx *vmx)
 {
 	/*
@@ -8486,6 +8458,8 @@ void vmx_hardware_unsetup(void)
 
 	if (nested)
 		nested_vmx_hardware_unsetup();
+
+	destroy_vpids();
 }
 
 void vmx_vm_destroy(struct kvm *kvm)
@@ -8711,8 +8685,6 @@ __init int vmx_hardware_setup(void)
 	kvm_caps.has_bus_lock_exit = cpu_has_vmx_bus_lock_detection();
 	kvm_caps.has_notify_vmexit = cpu_has_notify_vmexit();
 
-	set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
-
 	if (enable_ept)
 		kvm_mmu_set_ept_masks(enable_ept_ad_bits);
 	else
@@ -8777,6 +8749,10 @@ __init int vmx_hardware_setup(void)
 
 	vmx_set_cpu_caps();
 
+	r = init_vpids();
+	if (r)
+		return r;
+
 	/*
 	 * Configure nested capabilities after core CPU capabilities so that
 	 * nested support can be conditional on base support, e.g. so that KVM
@@ -8784,8 +8760,10 @@ __init int vmx_hardware_setup(void)
 	 */
 	if (nested) {
 		r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
-		if (r)
+		if (r) {
+			destroy_vpids();
 			return r;
+		}
 	}
 	vmx_nested_ops.enabled = nested;
 
arch/x86/kvm/vmx/vmx.h +24 -4
index dc8517f15bc46..3de3ee53ccbdc 100644
@@ -182,7 +182,7 @@ struct nested_vmx {
 	u64 pre_vmenter_ssp;
 	u64 pre_vmenter_ssp_tbl;
 
-	u16 vpid02;
+	kvm_tlb_tag_t vpid02;
 	u16 last_vpid;
 
 	int tsc_autostore_slot;
@@ -256,7 +256,7 @@ struct vcpu_vmx {
 			u32 ar;
 		} seg[8];
 	} segment_cache;
-	int vpid;
+	kvm_tlb_tag_t vpid;
 
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
@@ -341,9 +341,29 @@ static __always_inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu)
 	return vt->exit_intr_info;
 }
 
+static __always_inline int init_vpids(void)
+{
+	return enable_vpid ? kvm_init_tlb_tags(VMX_NR_VPIDS) : 0;
+}
+
+static __always_inline void destroy_vpids(void)
+{
+	if (enable_vpid)
+		kvm_destroy_tlb_tags();
+}
+
+static __always_inline kvm_tlb_tag_t allocate_vpid(void)
+{
+	return enable_vpid ? kvm_alloc_tlb_tag() : 0;
+}
+
+static __always_inline void free_vpid(kvm_tlb_tag_t vpid)
+{
+	if (enable_vpid)
+		kvm_free_tlb_tag(vpid);
+}
+
 void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu);
-int allocate_vpid(void);
-void free_vpid(int vpid);
 void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
 void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
 void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#6 lore raw

PATCH 4/28 KVM: x86/mmu: Support specifying reserved TLB tags

In reply to Yosry Ahmed

In preparation for using the TLB tags allocator for SVM, which has a range of ASIDs allocated for SEV/SNP, pass in the number of reserved TLB tags when initializing the TLB tags allocator. The bitmap is conceptually shifted such that bit=0 corresponds to tag=nr_reserved.

Specifying the minimum value during initialization also makes the API clearer, as tag=0 is now explicitly reserved by the caller.

No functional change intended for VMX as VPID=0 was already implicitly reserved.

---
 arch/x86/kvm/mmu.h     |  2 +-
 arch/x86/kvm/mmu/mmu.c | 42 ++++++++++++++++++++++++++++--------------
 arch/x86/kvm/vmx/vmx.h |  2 +-
 3 files changed, 30 insertions(+), 16 deletions(-)
arch/x86/kvm/mmu.h +1 -1
index de79e002edf8f..b443a5083bfb0 100644
@@ -413,7 +413,7 @@ static inline bool kvm_is_gfn_alias(struct kvm *kvm, gfn_t gfn)
 
 typedef unsigned int kvm_tlb_tag_t;
 
-int kvm_init_tlb_tags(unsigned int nr);
+int kvm_init_tlb_tags(unsigned int nr_total, unsigned int nr_reserved);
 void kvm_destroy_tlb_tags(void);
 kvm_tlb_tag_t kvm_alloc_tlb_tag(void);
 void kvm_free_tlb_tag(kvm_tlb_tag_t tag);
arch/x86/kvm/mmu/mmu.c +28 -14

87 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.h +1 -1
index 3de3ee53ccbdc..7b7e57fd549c4 100644
@@ -343,7 +343,7 @@ static __always_inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu)
 
 static __always_inline int init_vpids(void)
 {
-	return enable_vpid ? kvm_init_tlb_tags(VMX_NR_VPIDS) : 0;
+	return enable_vpid ? kvm_init_tlb_tags(VMX_NR_VPIDS, 1) : 0;
 }
 
 static __always_inline void destroy_vpids(void)
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#7 lore raw

PATCH 5/28 KVM: SVM: Add helpers to set/clear ASID flush in VMCB

In reply to Yosry Ahmed

Introduce set/clear helpers to set tlb_ctl to TLB_CONTROL_FLUSH_ASID or TLB_CONTROL_DO_NOTHING. Incoming changes will eliminate the use of TLB_CONTROL_FLUSH_ALL_ASID except as a fallback when FLUSHBYASID is not available, so the helpers will create a common path for this.

---
 arch/x86/kvm/svm/nested.c |  2 +-
 arch/x86/kvm/svm/sev.c    |  2 +-
 arch/x86/kvm/svm/svm.c    |  4 ++--
 arch/x86/kvm/svm/svm.h    | 10 ++++++++++
 4 files changed, 14 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +1 -1
index 75378feddb349..ef0326932c446 100644
@@ -931,7 +931,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	/* Done at vmrun: asid.  */
 
 	/* Also overwritten later if necessary.  */
-	vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+	vmcb_clr_flush_asid(vmcb02);
 
 	/* Use vmcb01 MMU and format if guest does not use nNPT */
 	if (nested_npt_enabled(svm)) {
arch/x86/kvm/svm/sev.c +1 -1
index 90a08d36d843d..d1f0e42c72b27 100644
@@ -3594,7 +3594,7 @@ int pre_sev_run(struct vcpu_svm *svm, int cpu)
 		return 0;
 
 	sd->sev_vmcbs[asid] = svm->vmcb;
-	svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
+	vmcb_set_flush_asid(svm->vmcb);
 	vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
 	return 0;
 }
arch/x86/kvm/svm/svm.c +2 -2
index d68cba12c772b..de90320a2e669 100644
@@ -4199,7 +4199,7 @@ static void svm_flush_tlb_asid(struct kvm_vcpu *vcpu)
 	 * VM-Exit (via kvm_mmu_reset_context()).
 	 */
 	if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
-		svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
+		vmcb_set_flush_asid(svm->vmcb);
 	else
 		svm->current_vmcb->asid_generation--;
 }
@@ -4612,7 +4612,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
 		vcpu->arch.nested_run_pending = 0;
 	}
 
-	svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+	vmcb_clr_flush_asid(svm->vmcb);
 
 	/*
 	 * Unconditionally mask off the CLEAR_RAP bit, the AND is just as cheap
arch/x86/kvm/svm/svm.h +10 -0
index a327bf751ecd5..e3cce839573d9 100644
@@ -477,6 +477,16 @@ static inline void vmcb_set_gpat(struct vmcb *vmcb, u64 data)
 	vmcb_mark_dirty(vmcb, VMCB_NPT);
 }
 
+static inline void vmcb_set_flush_asid(struct vmcb *vmcb)
+{
+	vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
+}
+
+static inline void vmcb_clr_flush_asid(struct vmcb *vmcb)
+{
+	vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+}
+
 static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
 {
 	return container_of(vcpu, struct vcpu_svm, vcpu);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#8 lore raw

PATCH 6/28 KVM: SVM: Fallback to flush everything if FLUSHBYASID is not available

In reply to Yosry Ahmed

Currently, if FLUSHBYASID is not available when performing a TLB flush, the fallback is decrementing the ASID generation to trigger allocating a new ASID. In preparation for using a static ASID per vCPU (like VMX), just fallback to flushing everything if FLUSHBYASID is not available. This is probably worse from a performance perspective, but FLUSHBYASID has been around for ~15 years and it's not worth carrying the complexity.

Move the fallback logic into vmcb_set_flush_asid(), as more callers will be added and will need the fallback as well. The only other current caller is from SEV code, and no CPUs support SEV but not FLUSHBYASID (at least according to AI).

---
 arch/x86/kvm/svm/svm.c | 5 +----
 arch/x86/kvm/svm/svm.h | 5 ++++-
 2 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/svm.c +1 -4
index de90320a2e669..aff1aae5a5a32 100644
@@ -4198,10 +4198,7 @@ static void svm_flush_tlb_asid(struct kvm_vcpu *vcpu)
 	 * unconditionally does a TLB flush on both nested VM-Enter and nested
 	 * VM-Exit (via kvm_mmu_reset_context()).
 	 */
-	if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
-		vmcb_set_flush_asid(svm->vmcb);
-	else
-		svm->current_vmcb->asid_generation--;
+	vmcb_set_flush_asid(svm->vmcb);
 }
 
 static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +4 -1
index e3cce839573d9..2f18dff023bc5 100644
@@ -479,7 +479,10 @@ static inline void vmcb_set_gpat(struct vmcb *vmcb, u64 data)
 
 static inline void vmcb_set_flush_asid(struct vmcb *vmcb)
 {
-	vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
+	if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
+		vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
+	else
+		vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
 }
 
 static inline void vmcb_clr_flush_asid(struct vmcb *vmcb)
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#9 lore raw

PATCH 7/28 KVM: SVM: Duplicate pre-run ASID check for SEV and non-SEV guests

In reply to Yosry Ahmed

In preparation for dropping the check separately for SEV and non-SEV VMs, duplicate the check between pre_sev_run() and pre_svm_run() (for the non-SEV code path).

Opportunistically drop the unconditional dirtying of VMCB_ASID, as it's only needed when the ASID is actually updated in the VMCB, not when TLB_CONTROL is updated (as the code currently reads).

No functional change intended.

---
 arch/x86/kvm/svm/sev.c | 6 +++++-
 arch/x86/kvm/svm/svm.c | 9 +++++----
 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/sev.c +5 -1
index d1f0e42c72b27..0e5046a12a970 100644
@@ -3583,6 +3583,11 @@ int pre_sev_run(struct vcpu_svm *svm, int cpu)
 	/* Assign the asid allocated with this SEV guest */
 	svm->asid = asid;
 
+	if (unlikely(svm->asid != svm->vmcb->control.asid)) {
+		svm->vmcb->control.asid = asid;
+		vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
+	}
+
 	/*
 	 * Flush guest TLB:
 	 *
@@ -3595,7 +3600,6 @@ int pre_sev_run(struct vcpu_svm *svm, int cpu)
 
 	sd->sev_vmcbs[asid] = svm->vmcb;
 	vmcb_set_flush_asid(svm->vmcb);
-	vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
 	return 0;
 }
 
arch/x86/kvm/svm/svm.c +5 -4
index aff1aae5a5a32..b8f3f71f70673 100644
@@ -3779,6 +3779,11 @@ static int pre_svm_run(struct kvm_vcpu *vcpu)
 	if (svm->current_vmcb->asid_generation != sd->asid_generation)
 		new_asid(svm, sd);
 
+	if (unlikely(svm->asid != svm->vmcb->control.asid)) {
+		svm->vmcb->control.asid = svm->asid;
+		vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
+	}
+
 	return 0;
 }
 
@@ -4515,10 +4520,6 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
 
 	sync_lapic_to_cr8(vcpu);
 
-	if (unlikely(svm->asid != svm->vmcb->control.asid)) {
-		svm->vmcb->control.asid = svm->asid;
-		vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
-	}
 	svm->vmcb->save.cr2 = vcpu->arch.cr2;
 
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS) &&
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#10 lore raw

PATCH 8/28 KVM: SEV: Do ASID initialization at VMCB initialization

In reply to Yosry Ahmed

The per-vCPU ASID does not change for SEV guests, so move the per-vCPU (and per-VMCB) ASID initialization to sev_init_vmcb() to do it once, instead of checking before every VMRUN.

No functional change intended.

---
 arch/x86/kvm/svm/sev.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/kvm/svm/sev.c +4 -8
index 0e5046a12a970..8717da0e9fe8a 100644
@@ -3580,14 +3580,6 @@ int pre_sev_run(struct vcpu_svm *svm, int cpu)
 	if (!cpumask_test_cpu(cpu, to_kvm_sev_info(kvm)->have_run_cpus))
 		cpumask_set_cpu(cpu, to_kvm_sev_info(kvm)->have_run_cpus);
 
-	/* Assign the asid allocated with this SEV guest */
-	svm->asid = asid;
-
-	if (unlikely(svm->asid != svm->vmcb->control.asid)) {
-		svm->vmcb->control.asid = asid;
-		vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
-	}
-
 	/*
 	 * Flush guest TLB:
 	 *
@@ -4829,6 +4821,10 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)
 	svm->vmcb->control.misc_ctl |= SVM_MISC_ENABLE_SEV;
 	clr_exception_intercept(svm, UD_VECTOR);
 
+	svm->asid = sev_get_asid(vcpu->kvm);
+	svm->vmcb->control.asid = svm->asid;
+	vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
+
 	/*
 	 * Don't intercept #GP for SEV guests, e.g. for the VMware backdoor, as
 	 * KVM can't decrypt guest memory to decode the faulting instruction.
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#12 lore raw

PATCH 9/28 KVM: SEV: Expose sev_get_asid() outside of sev.c

In reply to Yosry Ahmed

Move the function definition to svm.h in preparation for using it in more SVM code.

No functional change intended.

---
 arch/x86/kvm/svm/sev.c |  5 -----
 arch/x86/kvm/svm/svm.h | 10 ++++++++++
 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/sev.c +0 -5
index 8717da0e9fe8a..3aac702f45b2b 100644
@@ -313,11 +313,6 @@ static int sev_asid_new(struct kvm_sev_info *sev, unsigned long vm_type)
 	return ret;
 }
 
-static unsigned int sev_get_asid(struct kvm *kvm)
-{
-	return to_kvm_sev_info(kvm)->asid;
-}
-
 static void sev_asid_free(struct kvm_sev_info *sev)
 {
 	struct svm_cpu_data *sd;
arch/x86/kvm/svm/svm.h +10 -0
index 2f18dff023bc5..d41d189ecf4e1 100644
@@ -429,6 +429,11 @@ static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
 {
 	return ____sev_snp_guest(vcpu->kvm);
 }
+
+static __always_inline unsigned int sev_get_asid(struct kvm *kvm)
+{
+	return to_kvm_sev_info(kvm)->asid;
+}
 #else
 static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu)
 {
@@ -443,6 +448,11 @@ static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
 {
 	return false;
 }
+
+static __always_inline unsigned int sev_get_asid(struct kvm *kvm)
+{
+	return 0;
+}
 #endif
 
 static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val)
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#13 lore raw

PATCH 10/28 KVM: SVM: Use a static ASID per vCPU

In reply to Yosry Ahmed

Switch from dynamic ASID allocation to a static per-vCPU ASID. The ASID dynamic ASID allocation logic is now only effectively used when running out of ASIDs on a CPU (uncommon on modern hardware), or switching CPUs.

The per-CPU ASID generation is initialized to 1, and the per-VMCB ASID generation is initialized to 0. This leads to a TLB flush of the ASID on the first VMRUN, allocating a new ASID, and pumping the per-VMCB generation to 1 (matching the per-CPU generation). The ASID remains static until either:
- The vCPU runs on a new CPU, in which case KVM resets the per-VMCB generation to allocate a new ASID on the new CPU.
- KVM hits the maximum ASID on a CPU and increments the per-CPU ASID generation, at which point all vCPUs on this CPU will allocate a new ASID.

Drop the complexity and make the ASID static for each vCPU. This makes SVM's handling of ASIDs closer to VMX's handling of VPIDs, and simplifies the code. It also completely avoids full TLB flushes (i.e. TLB_CONTROL_FLUSH_ALL_ASID) on systems with FlushByAsid. Full flushes previously happened when updating the generation, so not so common, but should generally be avoided as they cause a VMRUN to invalidate the TLB entries for other VMs as well as the host.

When a vCPU is migrated to a new physical CPU, flush the (now static) ASID instead of allocating a new one. This might cause extra TLB flushes when switching CPUs (compared to just using a new ASID), but odds are that the TLB is cold on the new CPU anyway.

As using ASIDs cannot be disabled like VPIDs, allocate a fallback ASID to be shared by all vCPUs after running out of ASIDs (and flushed on every vCPU run, for now). If a fallback ASID cannot be allocated, fail vCPU creation (for non-SEV guests), but do not fail SVM initialization, to allow for allocating all ASIDs for SEV (if at all possible).

For SEV, the same ASID continues to be used by all vCPUs in a VM. Move svm->asid initialization from SEV-specific VMCB initialization to vCPU creation, such that svm->asid is always initialized on vCPU creation for both SEV and non-SEV vCPUs. This makes the logic simpler to follow, and makes it easier to hide it in allocate_asid() and free_asid() helpers.

Carefully handle SEV VM migration by copying svm->asid before the SEV-specific VMCB initialization for the destination, such that the ASID ends up being correctly initialized in the VMCB. Also, keep the ASID initialization in sev_init_vmcb(), even though it's already done in init_vmcb(), to specifically handle the SEV VM migration case where the ASID in the VMCB needs to be updated but init_vmcb() is not called again.

Note: A nice side-effect is reading the min/max ASIDs once during initialization, instead of once per-CPU.

---
 arch/x86/kvm/svm/nested.c |  4 +--
 arch/x86/kvm/svm/sev.c    |  3 +-
 arch/x86/kvm/svm/svm.c    | 63 +++++++++++++++++++--------------------
 arch/x86/kvm/svm/svm.h    | 24 ++++++++++-----
 4 files changed, 52 insertions(+), 42 deletions(-)
arch/x86/kvm/svm/nested.c +2 -2
index ef0326932c446..ed4af5a08f794 100644
@@ -928,9 +928,9 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	else
 		vmcb02->control.bus_lock_counter = 0;
 
-	/* Done at vmrun: asid.  */
+	vmcb02->control.asid = vmcb01->control.asid;
 
-	/* Also overwritten later if necessary.  */
+	/* Overwritten later if necessary.  */
 	vmcb_clr_flush_asid(vmcb02);
 
 	/* Use vmcb01 MMU and format if guest does not use nNPT */
arch/x86/kvm/svm/sev.c +2 -1
index 3aac702f45b2b..087d18a5eda69 100644
@@ -2071,6 +2071,7 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
 	kvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {
 		dst_svm = to_svm(dst_vcpu);
 
+		dst_svm->asid = dst->asid;
 		sev_init_vmcb(dst_svm, false);
 
 		if (!dst->es_active)
@@ -4816,7 +4817,7 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)
 	svm->vmcb->control.misc_ctl |= SVM_MISC_ENABLE_SEV;
 	clr_exception_intercept(svm, UD_VECTOR);
 
-	svm->asid = sev_get_asid(vcpu->kvm);
+	WARN_ON_ONCE(svm->asid != sev_get_asid(vcpu->kvm));
 	svm->vmcb->control.asid = svm->asid;
 	vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
 
arch/x86/kvm/svm/svm.c +31 -32

140 lines not on this page · raw message

arch/x86/kvm/svm/svm.h +17 -7
index d41d189ecf4e1..a75862e693134 100644
@@ -26,6 +26,7 @@
 #include "regs.h"
 #include "x86.h"
 #include "pmu.h"
+#include "mmu.h"
 
 /*
  * Helpers to convert to/from physical addresses for pages whose address is
@@ -143,7 +144,6 @@ struct kvm_vmcb_info {
 	struct vmcb *ptr;
 	unsigned long pa;
 	int cpu;
-	uint64_t asid_generation;
 };
 
 struct vmcb_save_area_cached {
@@ -283,7 +283,7 @@ struct vcpu_svm {
 	struct vmcb *vmcb;
 	struct kvm_vmcb_info vmcb01;
 	struct kvm_vmcb_info *current_vmcb;
-	u32 asid;
+	kvm_tlb_tag_t asid;
 	u32 sysenter_esp_hi;
 	u32 sysenter_eip_hi;
 	uint64_t tsc_aux;
@@ -370,11 +370,6 @@ struct vcpu_svm {
 };
 
 struct svm_cpu_data {
-	u64 asid_generation;
-	u32 max_asid;
-	u32 next_asid;
-	u32 min_asid;
-
 	bool bp_spec_reduce_set;
 
 	struct vmcb *save_area;
@@ -487,6 +482,21 @@ static inline void vmcb_set_gpat(struct vmcb *vmcb, u64 data)
 	vmcb_mark_dirty(vmcb, VMCB_NPT);
 }
 
+extern kvm_tlb_tag_t fallback_asid;
+
+static inline kvm_tlb_tag_t allocate_asid(struct kvm_vcpu *vcpu)
+{
+	if (is_sev_guest(vcpu))
+		return sev_get_asid(vcpu->kvm);
+	return kvm_alloc_tlb_tag() ?: fallback_asid;
+}
+
+static inline void free_asid(struct kvm_vcpu *vcpu, kvm_tlb_tag_t asid)
+{
+	if (!is_sev_guest(vcpu) && asid != fallback_asid)
+		kvm_free_tlb_tag(asid);
+}
+
 static inline void vmcb_set_flush_asid(struct vmcb *vmcb)
 {
 	if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#16 lore raw

PATCH 11/28 KVM: SVM: Only flush the fallback ASID when used by a different vCPU

In reply to Yosry Ahmed

Instead of always flushing the fallback ASID on every vCPU run, only flush it if the last vCPU that used it on the pCPU changes. This avoids constant TLB flushing on a vCPU using the fallback ASID if no other vCPUs that use the same ASID ran on the same pCPU.

Note that checking (and flushing) the current ASID on vCPU load is not sufficient, as it is possible that a vCPU is using the fallback ASID only for L1 or only for L2. To do this correctly on vCPU load, both L1 and L2 ASIDs would need to be checked, and the respective VMCB would need to be flushed. This would not be much simpler, and would perform unnecessary flushes in some cases (e.g. loading a vCPU that uses the fallback ASID for L2, but L2 doesn't actually run until the vCPU is unloaded).

Another alternative is always flushing on vCPU load if the current ASID is the fallback ASID, and flushing on nested transitions if the new ASID is the fallback ASID. Again, this is not much simpler and also incurs unnecessary flushes in some cases.

Do the logical thing and track the vCPU using the fallback ASID instead.

---
 arch/x86/kvm/svm/svm.c | 13 ++++++++++++-
 arch/x86/kvm/svm/svm.h |  3 +++
 2 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +12 -1
index 31194c773af7c..30a295785c68a 100644
@@ -1365,6 +1365,8 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
 static void svm_vcpu_free(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
+	struct svm_cpu_data *sd;
+	int cpu;
 
 	WARN_ON_ONCE(!list_empty(&svm->ir_list));
 
@@ -1377,6 +1379,11 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)
 	svm_vcpu_free_msrpm(svm->msrpm);
 
 	free_asid(vcpu, svm->asid);
+
+	for_each_possible_cpu(cpu) {
+		sd = per_cpu_ptr(&svm_data, cpu);
+		cmpxchg(&sd->fallback_asid_vcpu, vcpu, NULL);
+	}
 }
 
 #ifdef CONFIG_CPU_MITIGATIONS
@@ -3755,6 +3762,7 @@ static void svm_set_nested_run_soft_int_state(struct kvm_vcpu *vcpu)
 
 static int pre_svm_run(struct kvm_vcpu *vcpu)
 {
+	struct svm_cpu_data *sd = this_cpu_ptr(&svm_data);
 	struct vcpu_svm *svm = to_svm(vcpu);
 
 	/*
@@ -3771,8 +3779,11 @@ static int pre_svm_run(struct kvm_vcpu *vcpu)
 	if (is_sev_guest(vcpu))
 		return pre_sev_run(svm, vcpu->cpu);
 
-	if (unlikely(svm->vmcb->control.asid == fallback_asid))
+	if (unlikely(svm->vmcb->control.asid == fallback_asid &&
+		     sd->fallback_asid_vcpu != vcpu)) {
 		vmcb_set_flush_asid(svm->vmcb);
+		sd->fallback_asid_vcpu = vcpu;
+	}
 
 	return 0;
 }
arch/x86/kvm/svm/svm.h +3 -0
index a75862e693134..79339ee8a3d56 100644
@@ -375,6 +375,9 @@ struct svm_cpu_data {
 	struct vmcb *save_area;
 	unsigned long save_area_pa;
 
+	/* Last vCPU to use fallback_asid on this CPU */
+	struct kvm_vcpu *fallback_asid_vcpu;
+
 	/* index = sev_asid, value = vmcb pointer */
 	struct vmcb **sev_vmcbs;
 };
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#17 lore raw

PATCH 12/28 KVM: nSVM: Add a placeholder ASID for L2

In reply to Yosry Ahmed

In preparation for introducing a separate ASID for L2, introduce a 'placeholder' ASID that is still the same as L1's ASID. This will facilitate future changes that need to distinguish L1 and L2's ASIDs, before actually using a different ASID for L1 and L2.

No functional change intended.

---
 arch/x86/kvm/svm/nested.c | 5 +++--
 arch/x86/kvm/svm/svm.h    | 2 ++
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/nested.c +3 -2
index ed4af5a08f794..eb60d6b959d86 100644
@@ -698,7 +698,6 @@ static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
 	 *  - Honor L1's request to flush an ASID on nested VMRUN
 	 *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
 	 *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
-	 *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
 	 *
 	 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
 	 *     NPT guest-physical mappings on VMRUN.
@@ -928,7 +927,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	else
 		vmcb02->control.bus_lock_counter = 0;
 
-	vmcb02->control.asid = vmcb01->control.asid;
+	vmcb02->control.asid = svm->nested.asid02;
 
 	/* Overwritten later if necessary.  */
 	vmcb_clr_flush_asid(vmcb02);
@@ -1498,6 +1497,8 @@ int svm_allocate_nested(struct vcpu_svm *svm)
 	if (!svm->nested.msrpm)
 		goto err_free_vmcb02;
 
+	svm->nested.asid02 = svm->asid;
+
 	svm->nested.initialized = true;
 	return 0;
 
arch/x86/kvm/svm/svm.h +2 -0
index 79339ee8a3d56..196fb39ab0ae9 100644
@@ -232,6 +232,8 @@ struct svm_nested_state {
 	 */
 	struct vmcb_save_area_cached save;
 
+	kvm_tlb_tag_t asid02;
+
 	bool initialized;
 
 	/*
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#21 lore raw

PATCH 13/28 KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb()

In reply to Yosry Ahmed

Rename kvm_hv_vcpu_purge_flush_tlb() to kvm_hv_purge_tlb_flush_fifo() to clarify that it purges the TLB flush FIFO, not purge *and* flush the TLB.

No functional change intended.

---
 arch/x86/kvm/hyperv.h  | 4 ++--
 arch/x86/kvm/svm/svm.c | 2 +-
 arch/x86/kvm/x86.c     | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kvm/hyperv.h +2 -2
index 622a6553e9ac2..03cc65f8cc52d 100644
@@ -312,7 +312,7 @@ static inline struct kvm_vcpu_hv_tlb_flush_fifo *kvm_hv_get_tlb_flush_fifo(struc
 	return &hv_vcpu->tlb_flush_fifo[i];
 }
 
-static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu)
+static inline void kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu)
 {
 	struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
 
@@ -396,7 +396,7 @@ static inline int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 {
 	return HV_STATUS_ACCESS_DENIED;
 }
-static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu) {}
+static inline void kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu) {}
 static inline bool kvm_hv_synic_has_vector(struct kvm_vcpu *vcpu, int vector)
 {
 	return false;
arch/x86/kvm/svm/svm.c +1 -1
index 30a295785c68a..991171df83486 100644
@@ -4195,7 +4195,7 @@ static void svm_flush_tlb_asid(struct kvm_vcpu *vcpu)
 	 * A TLB flush for the current ASID flushes both "host" and "guest" TLB
 	 * entries, and thus is a superset of Hyper-V's fine grained flushing.
 	 */
-	kvm_hv_vcpu_purge_flush_tlb(vcpu);
+	kvm_hv_purge_tlb_flush_fifo(vcpu);
 
 	/*
 	 * Flush only the current ASID even if the TLB flush was invoked via
arch/x86/kvm/x86.c +1 -1
index 0f1a829032c06..6de69ef21d9be 100644
@@ -2019,7 +2019,7 @@ static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
 	 * Flushing all "guest" TLB is always a superset of Hyper-V's fine
 	 * grained flushing.
 	 */
-	kvm_hv_vcpu_purge_flush_tlb(vcpu);
+	kvm_hv_purge_tlb_flush_fifo(vcpu);
 }
 
 
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#22 lore raw

PATCH 14/28 KVM: x86: hyper-v: Allow puring all TLB flush FIFOs

In reply to Yosry Ahmed

Refactor kvm_hv_purge_tlb_flush_fifo() to introduce an inner helper, __kvm_hv_purge_tlb_flush_fifo(), parameterized by an optional FIFO. If a FIFO is not passed in, all FIFOs are purged.

Note that KVM_REQ_HV_TLB_FLUSH is consumed by __kvm_hv_purge_tlb_flush_fifo(), so a selective purge on one FIFO will render any subsequent non-selective purge useless. This is not a problem because purging TLB flush FIFOs is an optimization (and there are currently no users of the non-selective purges).

No functional change intended as a FIFO is always being passed in now.

---
 arch/x86/kvm/hyperv.h | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
arch/x86/kvm/hyperv.h +18 -7
index 03cc65f8cc52d..a23ef05c1075c 100644
@@ -312,18 +312,29 @@ static inline struct kvm_vcpu_hv_tlb_flush_fifo *kvm_hv_get_tlb_flush_fifo(struc
 	return &hv_vcpu->tlb_flush_fifo[i];
 }
 
-static inline void kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu)
+/* Purge pending TLB flushes in @fifo, or in all FIFOs if @fifo is NULL */
+static inline void __kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu,
+						 struct kvm_vcpu_hv_tlb_flush_fifo *fifo)
 {
-	struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
+	struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
+	int i;
 
-	if (!kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
+	if (!hv_vcpu || !kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
 		return;
 
-	tlb_flush_fifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu));
-	if (!tlb_flush_fifo)
-		return;
+	for (i = 0; i < ARRAY_SIZE(hv_vcpu->tlb_flush_fifo); i++) {
+		if (!fifo || fifo == &hv_vcpu->tlb_flush_fifo[i])
+			kfifo_reset_out(&hv_vcpu->tlb_flush_fifo[i].entries);
+	}
+}
+
+static inline void kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vcpu_hv_tlb_flush_fifo *fifo;
 
-	kfifo_reset_out(&tlb_flush_fifo->entries);
+	fifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu));
+	if (fifo)
+		__kvm_hv_purge_tlb_flush_fifo(vcpu, fifo);
 }
 
 static inline bool guest_hv_cpuid_has_l2_tlb_flush(struct kvm_vcpu *vcpu)
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#23 lore raw

PATCH 15/28 KVM: nSVM: Drop svm->nested.initialized

In reply to Yosry Ahmed

Do NULL checks on svm->nested.vmcb02.ptr instead of using a dedicated proxy 'initialized' field. Make sure svm->nested.vmcb02.ptr is correctly set to NULL on initialization failures, as currently it is currently left as a dangling pointer.

This makes it possible to directly do a NULL check on svm->nested.vmcb02.ptr before using it, instead of checking a different field, which is less error-prone.

---
 arch/x86/kvm/svm/nested.c | 13 +++++--------
 arch/x86/kvm/svm/svm.h    |  2 --
 2 files changed, 5 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/nested.c +5 -8
index eb60d6b959d86..a8bf847209618 100644
@@ -1134,7 +1134,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)
 		return ret;
 	}
 
-	if (WARN_ON_ONCE(!svm->nested.initialized))
+	if (WARN_ON_ONCE(!svm->nested.vmcb02.ptr))
 		return -EINVAL;
 
 	vmcb12_gpa = kvm_rax_read(vcpu);
@@ -1484,22 +1484,21 @@ int svm_allocate_nested(struct vcpu_svm *svm)
 {
 	struct page *vmcb02_page;
 
-	if (svm->nested.initialized)
+	if (svm->nested.vmcb02.ptr)
 		return 0;
 
 	vmcb02_page = snp_safe_alloc_page();
 	if (!vmcb02_page)
 		return -ENOMEM;
-	svm->nested.vmcb02.ptr = page_address(vmcb02_page);
-	svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
 
 	svm->nested.msrpm = svm_vcpu_alloc_msrpm();
 	if (!svm->nested.msrpm)
 		goto err_free_vmcb02;
 
+	svm->nested.vmcb02.ptr = page_address(vmcb02_page);
+	svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
 	svm->nested.asid02 = svm->asid;
 
-	svm->nested.initialized = true;
 	return 0;
 
 err_free_vmcb02:
@@ -1509,7 +1508,7 @@ int svm_allocate_nested(struct vcpu_svm *svm)
 
 void svm_free_nested(struct vcpu_svm *svm)
 {
-	if (!svm->nested.initialized)
+	if (!svm->nested.vmcb02.ptr)
 		return;
 
 	if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
@@ -1529,8 +1528,6 @@ void svm_free_nested(struct vcpu_svm *svm)
 	 * When the vmcb02 is freed, this optimization becomes invalid.
 	 */
 	svm->nested.last_vmcb12_gpa = INVALID_GPA;
-
-	svm->nested.initialized = false;
 }
 
 void svm_leave_nested(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +0 -2
index 196fb39ab0ae9..2e0c9c469ec63 100644
@@ -234,8 +234,6 @@ struct svm_nested_state {
 
 	kvm_tlb_tag_t asid02;
 
-	bool initialized;
-
 	/*
 	 * Indicates whether MSR bitmap for L2 needs to be rebuilt due to
 	 * changes in MSR bitmap for L1 or switching to a different L2. Note,
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#24 lore raw

PATCH 16/28 KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH

In reply to Yosry Ahmed

Flush both L1 and L2 ASIDs in svm_flush_tlb_all() to appropriately handle KVM_REQ_TLB_FLUSH by flushing all TLB entries in all contexts (e.g. for kvm_flush_remote_tlbs()). Since both L1 and L2 currently share an ASID, this is effectively a noop, but it won't be once L2 has a separate ASID.

Purge all Hyper-V TLB FIFOs (for both L1 and L2), since both ASIDs are flushed, and an ASID flush is a superset of Hyper-V's fine-grained flushing (see comment in svm_flush_tlb_asid()).

Note that if one TLB flush FIFO is purged (e.g. as a result of KVM_REQ_TLB_FLUSH_CURRENT), it will consume KVM_REQ_HV_TLB_FLUSH, and a subsequent KVM_REQ_TLB_FLUSH will not flush the other FIFO. This is alright as flushing both FIFOs is a (newly introduced) optimization anyway. The other FIFO will be checked after a nested transition, as KVM_REQ_HV_TLB_FLUSH is always set on nested transitions.

---
 arch/x86/kvm/hyperv.h     | 6 ++++++
 arch/x86/kvm/svm/nested.c | 1 -
 arch/x86/kvm/svm/svm.c    | 8 +++++++-
 3 files changed, 13 insertions(+), 2 deletions(-)
arch/x86/kvm/hyperv.h +6 -0
index a23ef05c1075c..b82f70e6fcea1 100644
@@ -337,6 +337,11 @@ static inline void kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu)
 		__kvm_hv_purge_tlb_flush_fifo(vcpu, fifo);
 }
 
+static inline void kvm_hv_purge_all_tlb_flush_fifos(struct kvm_vcpu *vcpu)
+{
+	__kvm_hv_purge_tlb_flush_fifo(vcpu, NULL);
+}
+
 static inline bool guest_hv_cpuid_has_l2_tlb_flush(struct kvm_vcpu *vcpu)
 {
 	struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
@@ -408,6 +413,7 @@ static inline int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 	return HV_STATUS_ACCESS_DENIED;
 }
 static inline void kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu) {}
+static inline void kvm_hv_purge_all_tlb_flush_fifos(struct kvm_vcpu *vcpu) {}
 static inline bool kvm_hv_synic_has_vector(struct kvm_vcpu *vcpu, int vector)
 {
 	return false;
arch/x86/kvm/svm/nested.c +0 -1
index a8bf847209618..a6a49a5e0d90a 100644
@@ -694,7 +694,6 @@ static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
 	 * TODO: optimize unconditional TLB flush/MMU sync.  A partial list of
 	 * things to fix before this can be conditional:
 	 *
-	 *  - Flush TLBs for both L1 and L2 remote TLB flush
 	 *  - Honor L1's request to flush an ASID on nested VMRUN
 	 *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
 	 *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
arch/x86/kvm/svm/svm.c +7 -1
index 991171df83486..1a8dae05f4ab5 100644
@@ -4225,6 +4225,8 @@ static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)
 
 static void svm_flush_tlb_all(struct kvm_vcpu *vcpu)
 {
+	struct vcpu_svm *svm = to_svm(vcpu);
+
 	/*
 	 * When running on Hyper-V with EnlightenedNptTlb enabled, remote TLB
 	 * flushes should be routed to hv_flush_remote_tlbs() without requesting
@@ -4235,7 +4237,11 @@ static void svm_flush_tlb_all(struct kvm_vcpu *vcpu)
 	if (WARN_ON_ONCE(svm_hv_is_enlightened_tlb_enabled(vcpu)))
 		hv_flush_remote_tlbs(vcpu->kvm);
 
-	svm_flush_tlb_asid(vcpu);
+	kvm_hv_purge_all_tlb_flush_fifos(vcpu);
+
+	vmcb_set_flush_asid(svm->vmcb01.ptr);
+	if (svm->nested.vmcb02.ptr)
+		vmcb_set_flush_asid(svm->nested.vmcb02.ptr);
 }
 
 static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#25 lore raw

PATCH 17/28 KVM: nSVM: Always switch VMCB before leaving guest mode

In reply to Yosry Ahmed

Move svm_switch_vmcb() calls ahead of leave_guest_mode(), as nothing between the calls depends on svm->vmcb being set to vmcb01. This makes the nested VM-Exit path consistent with the nested VM-Enter path, and having the calls to svm_switch_vmcb() and leave_guest_mode() close makes it easier to reason about incoming changes that depend on both VMCB and guest_mode context.

No functional change intended.

---
 arch/x86/kvm/svm/nested.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/nested.c +2 -5
index a6a49a5e0d90a..410d0f1aaa63c 100644
@@ -1325,7 +1325,7 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
 	if (nested_svm_vmexit_update_vmcb12(vcpu))
 		kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
 
-	/* Exit Guest-Mode */
+	svm_switch_vmcb(svm, &svm->vmcb01);
 	leave_guest_mode(vcpu);
 	svm_pmu_handle_nested_transition(svm);
 
@@ -1352,8 +1352,6 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS))
 		vmcb01->control.erap_ctl |= ERAP_CONTROL_CLEAR_RAP;
 
-	svm_switch_vmcb(svm, &svm->vmcb01);
-
 	/*
 	 * Rules for synchronizing int_ctl bits from vmcb02 to vmcb01:
 	 *
@@ -1537,6 +1535,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
 		vcpu->arch.nested_run_pending = 0;
 		svm->nested.vmcb12_gpa = INVALID_GPA;
 
+		svm_switch_vmcb(svm, &svm->vmcb01);
 		leave_guest_mode(vcpu);
 
 		/*
@@ -1548,8 +1547,6 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
 		 */
 		__svm_pmu_handle_nested_transition(svm, true);
 
-		svm_switch_vmcb(svm, &svm->vmcb01);
-
 		nested_svm_transition_tlb_flush(vcpu);
 
 		nested_svm_uninit_mmu_context(vcpu);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#26 lore raw

PATCH 18/28 KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fns

In reply to Yosry Ahmed

The handling for the entry and exit TLB flushes will diverge significantly in the following changes. Instead of adding an 'is_vmenter' argument like nested_vmx_transition_tlb_flush(), just split the function into two variants for 'entry' and 'exit'.

No functional change intended.

---
 arch/x86/kvm/svm/nested.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +12 -4
index 410d0f1aaa63c..57752ad37c2a2 100644
@@ -685,7 +685,7 @@ static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
 	vmcb12->control.exit_int_info = exit_int_info;
 }
 
-static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
+static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
 {
 	/* Handle pending Hyper-V TLB flush requests */
 	kvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);
@@ -705,6 +705,14 @@ static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
 	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
 }
 
+static void nested_svm_exit_tlb_flush(struct kvm_vcpu *vcpu)
+{
+	kvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);
+
+	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
+	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+}
+
 /*
  * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
  * if we are emulating VM-Entry into a guest with NPT enabled.
@@ -857,7 +865,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	struct vmcb *vmcb01 = svm->vmcb01.ptr;
 	struct kvm_vcpu *vcpu = &svm->vcpu;
 
-	nested_svm_transition_tlb_flush(vcpu);
+	nested_svm_entry_tlb_flush(vcpu);
 
 	/* Enter Guest-Mode */
 	enter_guest_mode(vcpu);
@@ -1430,7 +1438,7 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
 	svm->vcpu.arch.dr7 = DR7_FIXED_1;
 	kvm_update_dr7(&svm->vcpu);
 
-	nested_svm_transition_tlb_flush(vcpu);
+	nested_svm_exit_tlb_flush(vcpu);
 
 	nested_svm_uninit_mmu_context(vcpu);
 
@@ -1547,7 +1555,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
 		 */
 		__svm_pmu_handle_nested_transition(svm, true);
 
-		nested_svm_transition_tlb_flush(vcpu);
+		nested_svm_exit_tlb_flush(vcpu);
 
 		nested_svm_uninit_mmu_context(vcpu);
 		vmcb_mark_all_dirty(svm->vmcb);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#27 lore raw

PATCH 19/28 KVM: nSVM: Service local TLB flushes before nested transitions

In reply to Yosry Ahmed

Service pending TLB flushes for a specific context (L1 vs. L2) before entering/leaving guest mode, as KVM does not track TLB flush requests for L1 vs. L2. This is not currently required as KVM always flushes the TLB on nested transitions, but it is a requirement to drop the unconditional flushes.

TLB flushes are performed through the VMCB's TLB_CONTROL field, so service the flushes before every VMCB switch (except the precautionary switch in svm_free_nested()). Servicing flushes may end up purging the relevant HV TLB flush FIFO, which is based on guest_mode, so make sure local TLB flushes are handled before calling {enter/leave}_guest_mode().

This is conceptually similar to how nVMX calls kvm_service_local_tlb_flush_requests(), except that the VMX calls only have ordering requirements on {enter/leave}_guest_mode(), and not switching the VMCS, because VPID used for TLB flushes depends on guest_mode, and flushes are not VMCS-based.

Note that nested_svm_{entry/exit}_tlb_flush() must be called after kvm_service_local_tlb_flush_requests(), otherwise the TLB flushes will be immediately applied on the outgoing VMCB rather than the incoming one.

---
 arch/x86/kvm/svm/nested.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/x86/kvm/svm/nested.c +22 -0
index 57752ad37c2a2..c932cad1520ea 100644
@@ -1036,6 +1036,20 @@ static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to
 	to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
 }
 
+static void nested_svm_service_local_tlb_flushes(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	/*
+	 * TLB flushes are applied to the VMCB, so apply any pending TLB flushes
+	 * on the outgoing VMCB before switching to a new one. A TLB flush could
+	 * purge the relevant HV TLB flush FIFO depending on guest_mode, so make
+	 * sure the VMCB and guest_mode context is consistent.
+	 */
+	WARN_ON_ONCE(is_guest_mode(vcpu) != (svm->vmcb == svm->nested.vmcb02.ptr));
+	kvm_service_local_tlb_flush_requests(vcpu);
+}
+
 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -1067,6 +1081,8 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)
 
 	nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
 
+	nested_svm_service_local_tlb_flushes(vcpu);
+
 	svm_switch_vmcb(svm, &svm->nested.vmcb02);
 	nested_vmcb02_prepare_control(svm);
 	nested_vmcb02_prepare_save(svm);
@@ -1333,6 +1349,8 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
 	if (nested_svm_vmexit_update_vmcb12(vcpu))
 		kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
 
+	nested_svm_service_local_tlb_flushes(vcpu);
+
 	svm_switch_vmcb(svm, &svm->vmcb01);
 	leave_guest_mode(vcpu);
 	svm_pmu_handle_nested_transition(svm);
@@ -1543,6 +1561,8 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
 		vcpu->arch.nested_run_pending = 0;
 		svm->nested.vmcb12_gpa = INVALID_GPA;
 
+		nested_svm_service_local_tlb_flushes(vcpu);
+
 		svm_switch_vmcb(svm, &svm->vmcb01);
 		leave_guest_mode(vcpu);
 
@@ -2075,6 +2095,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 	svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
 	nested_copy_vmcb_control_to_cache(svm, ctl);
 
+	nested_svm_service_local_tlb_flushes(vcpu);
+
 	svm_switch_vmcb(svm, &svm->nested.vmcb02);
 
 	if (use_separate_l2_pat)
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#28 lore raw

PATCH 20/28 KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROL

In reply to Yosry Ahmed

Handle L1's requests to flush L2's TLB through the TLB_CONTROL field of VMCB12.

On nested VM-Enter, flush L2's ASID if any flush is specified in TLB_CONTROL. This handles TLB_CONTROL_FLUSH_ASID and TLB_CONTROL_FLUSH_ASID_LOCAL equally for simplicity.

On nested VM-Exit, flush L1's own ASID if L1 requested a *full* TLB flush (i.e. TLB_CONTROL_FLUSH_ALL_ASID).

Essentially, TLB_CONTROL_FLUSH_ASID[_LOCAL] cause a TLB flush on nested VM-Enter only, while TLB_CONTROL_FLUSH_ALL_ASID causes a TLB flush on both nested VM-Enter and nested VM-Exit.

Additionally, sync the nested NPTs on nested VM-Enter on an ASID flush, as ASID flushes also invalidate guest-physical translations on SVM (unlike VMX, which has separate VPID-based and EPT-based flushing).

All TLB_CONTROL values can be handled by KVM regardless of FLUSHBYASID support on the underlying CPU, so keep advertising FLUSHBYASID to the guest unconditionally.

Note, TLB_CONTROL_FLUSH_ALL_ASID is never propagated from the vmcb12 to the vmcb02 (unless FLUSHBYASID is not available), as this gives the guest the power to flush the entire physical TLB (including translations for the host and other VMs).

---
 arch/x86/kvm/svm/nested.c | 30 +++++++++++++++++++++++++-----
 arch/x86/kvm/svm/svm.c    |  5 ++---
 2 files changed, 27 insertions(+), 8 deletions(-)
arch/x86/kvm/svm/nested.c +25 -5
index c932cad1520ea..073368ddd207a 100644
@@ -687,19 +687,33 @@ static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
 
 static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
 {
+	struct vcpu_svm *svm = to_svm(vcpu);
+
 	/* Handle pending Hyper-V TLB flush requests */
 	kvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);
 
+	/*
+	 * If L1 requested a TLB flush for L2, flush L2's TLB on nested entry
+	 * and sync the nested NPT MMU, as TLB_CONTROL also flushes NPT
+	 * guest-physical mappings.
+	 *
+	 * If L1 requested a full TLB flush for all ASIDs (including its own),
+	 * L1's own ASID is also flushed on nested VM-Exit, before running L1.
+	 *
+	 * TLB_CONTROL_FLUSH_ASID and TLB_CONTROL_FLUSH_ASID_LOCAL are handled
+	 * equally for simplicity.
+	 */
+	if (svm->nested.ctl.tlb_ctl != TLB_CONTROL_DO_NOTHING) {
+		if (nested_npt_enabled(svm))
+			kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
+		kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
+	}
+
 	/*
 	 * TODO: optimize unconditional TLB flush/MMU sync.  A partial list of
 	 * things to fix before this can be conditional:
 	 *
-	 *  - Honor L1's request to flush an ASID on nested VMRUN
-	 *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
 	 *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
-	 *
-	 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
-	 *     NPT guest-physical mappings on VMRUN.
 	 */
 	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
@@ -707,8 +721,14 @@ static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
 
 static void nested_svm_exit_tlb_flush(struct kvm_vcpu *vcpu)
 {
+	struct vcpu_svm *svm = to_svm(vcpu);
+
 	kvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);
 
+	/* Flush L1's own ASID if it request a *full* TLB flush on VMRUN */
+	if (svm->nested.ctl.tlb_ctl == TLB_CONTROL_FLUSH_ALL_ASID)
+		kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
+
 	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
 }
arch/x86/kvm/svm/svm.c +2 -3
index 1a8dae05f4ab5..e4bda43238654 100644
@@ -5533,9 +5533,8 @@ static __init void svm_set_cpu_caps(void)
 		kvm_cpu_cap_set(X86_FEATURE_VMCBCLEAN);
 
 		/*
-		 * KVM currently flushes TLBs on *every* nested SVM transition,
-		 * and so for all intents and purposes KVM supports flushing by
-		 * ASID, i.e. KVM is guaranteed to honor every L1 ASID flush.
+		 * KVM handles all TLB_CONTROL values set by L1, even if the
+		 * underlying CPU does not.
 		 */
 		kvm_cpu_cap_set(X86_FEATURE_FLUSHBYASID);
 
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#29 lore raw

PATCH 21/28 KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12

In reply to Yosry Ahmed

KVM uses a single ASID for L2 guests per-vCPU. Hence, when L1 changes L2's ASID in vmcb12 (e.g. due to switching a different L2 vCPU or simply to avoid flushing an existing ASID), KVM needs to flush the L2 ASID to correctly emulate different ASIDs as different TLB domains.

Additionally, the nested NPT needs to be resync'd, as the MMU context is not tagged by ASID, and KVM cannot reuse nested NPT entries across different L2 ASIDs.

Essentially, L1 switching L2's ASID is treated exactly the same as L1 flushing L2's ASID, which is consistent with the APM:

Software may effectively flush the guest's TLB entries by allocating a new ASID for the guest and not reusing the old ASID until the entire TLB has been flushed at least once.

This is similar to nVMX's handling of last_vpid, except that when L1 changes VPID12, KVM does *not* need to resync the nested EPT, because VMX VPIDs , unlike SVM ASIDs, do not tag guest-physical translations (i.e. nGPA to GPA translations).

Drop the commentary about vmcb12's ASID being copied around only for the consistency checks, as they no longer apply, and opportunistically fix whitespace alignment.

This is currently functionally a noop, as a full flush and sync is triggered on every nested transition, but is a step toward eliminating that.

---
 arch/x86/kvm/svm/nested.c | 22 ++++++++++++++++------
 arch/x86/kvm/svm/svm.h    |  2 ++
 2 files changed, 18 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/nested.c +16 -6
index 073368ddd207a..f50e21fd363fc 100644
@@ -553,10 +553,8 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
 	to->misc_ctl2		= from->misc_ctl2;
 	to->pause_filter_count  = from->pause_filter_count;
 	to->pause_filter_thresh = from->pause_filter_thresh;
-
-	/* Copy asid here because nested_vmcb_check_controls() will check it */
-	to->asid           = from->asid;
-	to->clean = from->clean;
+	to->asid		= from->asid;
+	to->clean		= from->clean;
 
 #ifdef CONFIG_KVM_HYPERV
 	/* Hyper-V extensions (Enlightened VMCB) */
@@ -688,22 +686,34 @@ static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
 static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
+	bool new_asid = false;
 
 	/* Handle pending Hyper-V TLB flush requests */
 	kvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);
 
+	if (svm->nested.ctl.asid != svm->nested.last_asid) {
+		svm->nested.last_asid = svm->nested.ctl.asid;
+		new_asid = true;
+	}
+
 	/*
 	 * If L1 requested a TLB flush for L2, flush L2's TLB on nested entry
 	 * and sync the nested NPT MMU, as TLB_CONTROL also flushes NPT
 	 * guest-physical mappings.
 	 *
+	 * Handle L1 changing L2's ASID12 similarly, as KVM only uses one ASID
+	 * for L2 in hardware (per vCPU), so it must start fresh when L1 changes
+	 * ASID12 to emulate different ASIDs correctly.  Additionally, the MMU
+	 * context is not tagged by the ASID, so the shadow NPTs cannot be
+	 * reused across different L2 ASIDs.
+	 *
 	 * If L1 requested a full TLB flush for all ASIDs (including its own),
 	 * L1's own ASID is also flushed on nested VM-Exit, before running L1.
 	 *
 	 * TLB_CONTROL_FLUSH_ASID and TLB_CONTROL_FLUSH_ASID_LOCAL are handled
 	 * equally for simplicity.
 	 */
-	if (svm->nested.ctl.tlb_ctl != TLB_CONTROL_DO_NOTHING) {
+	if (new_asid || (svm->nested.ctl.tlb_ctl != TLB_CONTROL_DO_NOTHING)) {
 		if (nested_npt_enabled(svm))
 			kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 		kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
@@ -1885,7 +1895,7 @@ void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
 	svm_write_tsc_multiplier(vcpu);
 }
 
-/* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
+/* Inverse operation of nested_copy_vmcb_control_to_cache() */
 static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
 					      struct vmcb_ctrl_area_cached *from)
 {
arch/x86/kvm/svm/svm.h +2 -0
index 2e0c9c469ec63..80a6b08fc4003 100644
@@ -242,6 +242,8 @@ struct svm_nested_state {
 	 * on its side.
 	 */
 	bool force_msr_bitmap_recalc;
+
+	u32 last_asid;
 };
 
 struct vcpu_sev_es_state {
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#30 lore raw

PATCH 22/28 KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enter

In reply to Yosry Ahmed

Stop clearing TLB_CONTROL when preparing the control area of vmcb02, as this potentially undos pending TLB flushes for L2 (e.g. through KVM_REQ_TLB_FLUSH while L1 is running), and remove the associated TODO comment.

This is currently harmless, because nested_svm_entry_tlb_flush() always requests KVM_REQ_TLB_FLUSH_CURRENT on nested VM-Enter, which sets TLB_CONTROL again before L2 is actually run. However, always flushing will soon go away with proper TLB handling for L2, at which point always clearing TLB_CONTROL would be a bug.

Clearing TLB_CONTROL on nested VM-Enter was probably done because TLB_CONTROL is not cleared by the CPU on VM-Exit. However, KVM always clears TLB_CONTROL in the active VMCB after VMRUN. Hence, at nested VM-Enter, TLB_CONTROL in vmcb02 can only be non-zero if a TLB flush is queued for L2 while L1 is running (i.e. KVM_REQ_TLB_FLUSH), and that should never be ignored.

---
 arch/x86/kvm/svm/nested.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
arch/x86/kvm/svm/nested.c +1 -9
index f50e21fd363fc..58e7d099c559e 100644
@@ -719,12 +719,7 @@ static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
 		kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
 	}
 
-	/*
-	 * TODO: optimize unconditional TLB flush/MMU sync.  A partial list of
-	 * things to fix before this can be conditional:
-	 *
-	 *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
-	 */
+	/* TODO: optimize unconditional TLB flush/MMU sync */
 	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
 }
@@ -966,9 +961,6 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 
 	vmcb02->control.asid = svm->nested.asid02;
 
-	/* Overwritten later if necessary.  */
-	vmcb_clr_flush_asid(vmcb02);
-
 	/* Use vmcb01 MMU and format if guest does not use nNPT */
 	if (nested_npt_enabled(svm)) {
 		vmcb02->control.misc_ctl &= ~SVM_MISC_ENABLE_GMET;
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#31 lore raw

PATCH 23/28 KVM: x86/mmu: Rename __kvm_mmu_invalidate_addr() to kvm_mmu_sync_addr()

In reply to Yosry Ahmed

In preparation for creating another helper for kvm_mmu_invalidate_addr(), rename __kvm_mmu_invalidate_addr() to kvm_mmu_sync_addr(), which is arguably a more descriptive name.

No functional change intended.

---
 arch/x86/kvm/mmu/mmu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/mmu.c +4 -4
index 05d2ba2598e1b..1889413759d2e 100644
@@ -6627,8 +6627,8 @@ void kvm_mmu_print_sptes(struct kvm_vcpu *vcpu, gpa_t gpa, const char *msg)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_print_sptes);
 
-static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
-				      u64 addr, hpa_t root_hpa)
+static void kvm_mmu_sync_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
+			      u64 addr, hpa_t root_hpa)
 {
 	struct kvm_shadow_walk_iterator iterator;
 
@@ -6693,11 +6693,11 @@ void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 		return;
 
 	if (roots & KVM_MMU_ROOT_CURRENT)
-		__kvm_mmu_invalidate_addr(vcpu, mmu, addr, mmu->root.hpa);
+		kvm_mmu_sync_addr(vcpu, mmu, addr, mmu->root.hpa);
 
 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
 		if (roots & KVM_MMU_ROOT_PREVIOUS(i))
-			__kvm_mmu_invalidate_addr(vcpu, mmu, addr, mmu->prev_roots[i].hpa);
+			kvm_mmu_sync_addr(vcpu, mmu, addr, mmu->prev_roots[i].hpa);
 	}
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invalidate_addr);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#32 lore raw

PATCH 24/28 KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the GVA flush

In reply to Yosry Ahmed

Refactor helpers out of kvm_mmu_invalidate_addr() and kvm_mmu_invlpg() that take in an extra argument to skip the GVA flush.

This will be used when invalidating GVAs in a different context than the current one (i.e. invalidating an L2 GVA from L1), so flushing the current context would flush the wrong TLB entries.

No functional change intended.

---
 arch/x86/kvm/mmu/mmu.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu/mmu.c +18 -6
index 1889413759d2e..0d3e3b351b31c 100644
@@ -6664,8 +6664,8 @@ static void kvm_mmu_sync_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 	write_unlock(&vcpu->kvm->mmu_lock);
 }
 
-void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
-			     u64 addr, unsigned long roots)
+static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
+				      u64 addr, unsigned long roots, bool flush_gva)
 {
 	struct kvm_mmu *mmu;
 	int i;
@@ -6678,7 +6678,8 @@ void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 		if (is_noncanonical_invlpg_address(addr, vcpu))
 			return;
 
-		kvm_x86_call(flush_tlb_gva)(vcpu, addr);
+		if (flush_gva)
+			kvm_x86_call(flush_tlb_gva)(vcpu, addr);
 
 		if (tdp_enabled)
 			return;
@@ -6700,9 +6701,15 @@ void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 			kvm_mmu_sync_addr(vcpu, mmu, addr, mmu->prev_roots[i].hpa);
 	}
 }
+
+void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
+			     u64 addr, unsigned long roots)
+{
+	__kvm_mmu_invalidate_addr(vcpu, w, addr, roots, true);
+}
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invalidate_addr);
 
-void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
+static void __kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva, bool flush_gva)
 {
 	/*
 	 * INVLPG is required to invalidate any global mappings for the VA,
@@ -6714,11 +6721,16 @@ void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
 	 * be synced when switching to that new cr3, so nothing needs to be
 	 * done here for them.
 	 */
-	kvm_mmu_invalidate_addr(vcpu, &vcpu->arch.gva_walk, gva, KVM_MMU_ROOTS_ALL);
+	__kvm_mmu_invalidate_addr(vcpu, &vcpu->arch.gva_walk, gva,
+				  KVM_MMU_ROOTS_ALL, flush_gva);
 	++vcpu->stat.invlpg;
 }
-EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invlpg);
 
+void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
+{
+	__kvm_mmu_invlpg(vcpu, gva, true);
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invlpg);
 
 void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid)
 {
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#33 lore raw

PATCH 25/28 KVM: nSVM: Flush L2's ASID when emulating INVLPGA

In reply to Yosry Ahmed

KVM currently handles INVLPGA in the same way as INVLPG, flushing L1's own ASID. This is currently correct because L1 and L2 share an ASID, but it doesn't work once they have separate ASIDs.

If L1 is flushing a different L2 ASID than the one KVM is tracking, do nothing, as KVM will flush L2's ASID in hardware (and sync the MMU if needed) when L1 switches to the target ASID.

If L1 is flushing its own ASID, handle the flush the same as INVLPG. Otherwise, skip the GVA flush for the current context (L1's ASID), and flush the L2 ASID in hardware using INVLPGA if running on the same CPU as L2. If not, fallback to a VMCB-based ASID flush. Note that if L2 then runs on a different CPU KVM will flush the ASID anyway.

Either way, sync the MMU if NPT is disabled, which is handled by __kvm_mmu_invlpg(). Note that all MMU roots are sync'd when NPT is disabled, which can be optimized by keying off guest_mode to only sync the appropriate context (L1 vs. L2).

---
 arch/x86/kvm/mmu.h     |  3 +++
 arch/x86/kvm/mmu/mmu.c |  8 +++++---
 arch/x86/kvm/svm/svm.c | 38 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 44 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu.h +3 -0
index b443a5083bfb0..34f669e6650f2 100644
@@ -150,7 +150,10 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
 		       void *insn, int insn_len);
 void kvm_mmu_print_sptes(struct kvm_vcpu *vcpu, gpa_t gpa, const char *msg);
+void __kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva, bool flush_gva);
 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
+void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
+			       u64 addr, unsigned long roots, bool flush_gva);
 void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 			     u64 addr, unsigned long roots);
 void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid);
arch/x86/kvm/mmu/mmu.c +5 -3
index 0d3e3b351b31c..4175bccad166c 100644
@@ -6664,8 +6664,8 @@ static void kvm_mmu_sync_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 	write_unlock(&vcpu->kvm->mmu_lock);
 }
 
-static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
-				      u64 addr, unsigned long roots, bool flush_gva)
+void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
+			       u64 addr, unsigned long roots, bool flush_gva)
 {
 	struct kvm_mmu *mmu;
 	int i;
@@ -6701,6 +6701,7 @@ static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk
 			kvm_mmu_sync_addr(vcpu, mmu, addr, mmu->prev_roots[i].hpa);
 	}
 }
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(__kvm_mmu_invalidate_addr);
 
 void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 			     u64 addr, unsigned long roots)
@@ -6709,7 +6710,7 @@ void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invalidate_addr);
 
-static void __kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva, bool flush_gva)
+void __kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva, bool flush_gva)
 {
 	/*
 	 * INVLPG is required to invalidate any global mappings for the VA,
@@ -6725,6 +6726,7 @@ static void __kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva, bool flush_gva)
 				  KVM_MMU_ROOTS_ALL, flush_gva);
 	++vcpu->stat.invlpg;
 }
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(__kvm_mmu_invlpg);
 
 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
 {
arch/x86/kvm/svm/svm.c +36 -2
index e4bda43238654..d7b20941b1fee 100644
@@ -2425,17 +2425,51 @@ static int clgi_interception(struct kvm_vcpu *vcpu)
 
 static int invlpga_interception(struct kvm_vcpu *vcpu)
 {
+	struct vcpu_svm *svm = to_svm(vcpu);
 	/* FIXME: Handle an address size prefix. */
 	gva_t gva = kvm_rax_read(vcpu);
 	u32 asid = kvm_ecx_read(vcpu);
+	int cpu;
 
 	if (nested_svm_check_permissions(vcpu))
 		return 1;
 
 	trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
 
-	/* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
-	kvm_mmu_invlpg(vcpu, gva);
+	/*
+	 * INVLPG on a non-canonical address is a NOP according to the SDM,
+	 * assumethe same behavior from INVLPGA since the APM doesn't specify.
+	 */
+	if (is_noncanonical_invlpg_address(gva, vcpu))
+		return kvm_skip_emulated_instruction(vcpu);
+
+	/*
+	 * Do nothing if L1 is flushing a different L2 ASID than the one KVM is
+	 * currently tracking.  KVM tracks a single L2 ASID, and performs a TLB
+	 * flush (and MMU resync if needed) when L1 switches ASIDs anyway.
+	 */
+	if (asid && asid != svm->nested.last_asid)
+		return kvm_skip_emulated_instruction(vcpu);
+
+	/*
+	 * Handle INVLPGA similar to INVLPG, with one caveat. If the specified
+	 * ASID is non-zero (i.e. L1 is not flushing it's own ASID), skip
+	 * flushing the TLB for the current context (L1's), and use INVLPGA to
+	 * flush L2's ASID in hardware if running on the same CPU (otherwise
+	 * fallback to a full ASID flush).
+	 *
+	 * Note, if NPT is disabled, this will sync all the shadow page tables.
+	 * This can be optimized by keying off guest_mode.
+	 */
+	__kvm_mmu_invlpg(vcpu, gva, !asid);
+	if (asid) {
+		cpu = get_cpu();
+		if (cpu == svm->nested.vmcb02.cpu)
+			invlpga(gva, svm->nested.asid02);
+		else
+			vmcb_set_flush_asid(svm->nested.vmcb02.ptr);
+		put_cpu();
+	}
 
 	return kvm_skip_emulated_instruction(vcpu);
 }
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#37 lore raw

PATCH 26/28 KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2

In reply to Yosry Ahmed

If L1 and L2 use the same ASID in hardware, always flush it on nested transitions to avoid using L1 TLB entries for L2 or vice versa, since from L1's perspective they are two different TLB domains.

This can happen in two cases:
- KVM runs out of ASIDs and uses the fallback ASID for both L1 and L2.
- SEV VMs always use the same ASID for SEV-specific requirements.

Note that KVM_REQ_TLB_FLUSH_CURRENT is required here, not KVM_REQ_TLB_FLUSH_GUEST (used for L1 TLB flush requests). This is because KVM could be switching between different NPT roots on the same ASID. While this generally requires an ASID flush in the VMCB, which is done by both KVM_REQ_TLB_FLUSH_CURRENT and KVM_REQ_TLB_FLUSH_GUEST, it also requires a hypercall to specifically flush NPT mappings when running on Hyper-V, which is only done with KVM_REQ_TLB_FLUSH_CURRENT (as KVM_REQ_TLB_FLUSH_GUEST is only meant to flush translations created by the guest).

Note that this is currently a noop as KVM requests KVM_REQ_TLB_FLUSH_CURRENT unconditionally on nested transitions, but this will soon be removed in favor of conditional flushes.

---
 arch/x86/kvm/svm/nested.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/x86/kvm/svm/nested.c +12 -0
index 58e7d099c559e..9ea8f21be9940 100644
@@ -719,6 +719,15 @@ static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
 		kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
 	}
 
+	/*
+	 * If L1 and L2 share the same ASID in hardware (when using the fallback
+	 * ASID for both, or for SEV guests), flush it on nested transitions.
+	 */
+	if (svm->asid == svm->nested.asid02) {
+		WARN_ON_ONCE(svm->asid != fallback_asid && !is_sev_guest(vcpu));
+		kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+	}
+
 	/* TODO: optimize unconditional TLB flush/MMU sync */
 	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
@@ -734,6 +743,9 @@ static void nested_svm_exit_tlb_flush(struct kvm_vcpu *vcpu)
 	if (svm->nested.ctl.tlb_ctl == TLB_CONTROL_FLUSH_ALL_ASID)
 		kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
 
+	if (svm->asid == svm->nested.asid02)
+		kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+
 	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
 }
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#39 lore raw

PATCH 27/28 KVM: nSVM: Use different ASIDs for L1 and L2

In reply to Yosry Ahmed

Now that TLB flushes are properly handled and tracked for L1 vs L2 ASIDs, allocate a separate new ASID for L2 for each vCPU, similar to how VMX handles VPIDs. Drop the unconditional flushes and syncs on nested transitions.

Initialize last_asid to 0, such that the L2 ASID is always flushed on first nested VM-Enter after it's allocated.

On a Turin CPU, this results in 8-15% performance boost in CPUID rate microbenchmark [1] and netperf TCP_RR latency/throughput.

[1]https://lore.kernel.org/kvm/[email protected]/

---
 arch/x86/kvm/svm/nested.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
arch/x86/kvm/svm/nested.c +20 -8
index 9ea8f21be9940..659e9da43945a 100644
@@ -707,6 +707,10 @@ static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
 	 * context is not tagged by the ASID, so the shadow NPTs cannot be
 	 * reused across different L2 ASIDs.
 	 *
+	 * Note, last_asid is initialized as 0, so the first nested VM-Enter
+	 * after setting EFER.SVME will always flush the TLB to avoid using
+	 * stale entries.
+	 *
 	 * If L1 requested a full TLB flush for all ASIDs (including its own),
 	 * L1's own ASID is also flushed on nested VM-Exit, before running L1.
 	 *
@@ -727,10 +731,6 @@ static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
 		WARN_ON_ONCE(svm->asid != fallback_asid && !is_sev_guest(vcpu));
 		kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
 	}
-
-	/* TODO: optimize unconditional TLB flush/MMU sync */
-	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
-	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
 }
 
 static void nested_svm_exit_tlb_flush(struct kvm_vcpu *vcpu)
@@ -745,9 +745,6 @@ static void nested_svm_exit_tlb_flush(struct kvm_vcpu *vcpu)
 
 	if (svm->asid == svm->nested.asid02)
 		kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
-
-	kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
-	kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
 }
 
 /*
@@ -1554,7 +1551,20 @@ int svm_allocate_nested(struct vcpu_svm *svm)
 
 	svm->nested.vmcb02.ptr = page_address(vmcb02_page);
 	svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
-	svm->nested.asid02 = svm->asid;
+	svm->nested.asid02 = allocate_asid(&svm->vcpu);
+
+	/*
+	 * KVM uses a fallback ASID when out of ASIDs, and fails vCPU creation
+	 * if there's no fallback ASID. ASID allocation must succeed here.
+	 */
+	KVM_BUG_ON(!svm->nested.asid02, svm->vcpu.kvm);
+
+	/*
+	 * Clear last_asid to ensure that the ASID is flushed on the first
+	 * nested VM-Enter. Otherwise, stale TLB entries from a previous life of
+	 * the VPID (e.g. different vCPU or even different VM) could be used.
+	 */
+	svm->nested.last_asid = 0;
 
 	return 0;
 
@@ -1571,6 +1581,8 @@ void svm_free_nested(struct vcpu_svm *svm)
 	if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
 		svm_switch_vmcb(svm, &svm->vmcb01);
 
+	free_asid(&svm->vcpu, svm->nested.asid02);
+
 	svm_vcpu_free_msrpm(svm->nested.msrpm);
 	svm->nested.msrpm = NULL;
 
Signature · 1 line
2.55.0.229.g6434b31f56-goog
#40 lore raw

PATCH 28/28 KVM: selftests: Add a test for nested TLB flushes

In reply to Yosry Ahmed

Add a test that exercises most common scenarios for TLB flushes. The test is split into two testing scenarios:
1. Guest-triggered TLB flushes
2. KVM-triggered TLB flushes

For (1), the test runs an L1 guest that keeps switching an L2 PTE (or TDP PTE) between two GPAs, flushes the TLB, and runs L2 to check that accessing that address access the correct page. A missed TLB flush causes L2 to access the wrong GPA through a stale TLB entry. The test exercises multiple ways for L1 to flush the TLB, including changing L2's ASID/VPID, flushing L2's entire ASID/VPID/EPTP, or flushing an individual L2 GVA (using INVLPGA/INVVPID) when L1 does not enable TDP.

For (2), the test creates a memslot with 2 pages, and keeps moving the base GPA of the memslot between a TEST_GPA and TEST_GPA - PAGE_SIZE, essentially resulting in the TEST_GPA being switched between two underlying HPAs. KVM should flush the TLB for both L1 and L2 contexts when updating a GPA -> HPA mapping.

The test runs an L1 guest that verifies access to page 1, triggers the memslot move, and checks that both L1 and L2 now (correctly) access page 2. This verifies that a TLB flush in L1's context flushes the TLB for both L1 and L2. After that, the test does the opposite and triggers the memslot move from L2, to double check that a TLB flush in L2's context flushes the TLB for both L1 and L2.

The test runs various test cases with TDP on/off in L1, and actually catches multiple injected bugs (in SVM), including:
- Missing the TLB flush on nested VM-Enter if ASID12 changes.
- Missing the TLB flush on nested VM-Enter if L1 uses TLB_CONTROL_FLUSH_ASID.
- Missing the nested NPT resync on nested VM-Enter if L1 uses TLB_CONTROL_FLUSH_ASID with nested NPT enabled.
- Missing the TLB flush when emulating INVLPGA.
- Missing flushing both L1 and L2 ASIDs when handling KVM_REQ_TLB_FLUSH.

Assisted-by Gemini:gemini-3.5-pro
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/include/x86/svm.h |   5 +
 tools/testing/selftests/kvm/include/x86/vmx.h |  21 +
 .../selftests/kvm/x86/nested_tlb_flush_test.c | 436 ++++++++++++++++++
 4 files changed, 463 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 88c6c8046ddec..8bea9fbaca961 100644
@@ -120,6 +120,7 @@ TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_vmcb12_gpa
 TEST_GEN_PROGS_x86 += x86/svm_nested_pat_test
+TEST_GEN_PROGS_x86 += x86/nested_tlb_flush_test
 TEST_GEN_PROGS_x86 += x86/svm_lbr_nested_state
 TEST_GEN_PROGS_x86 += x86/svm_pmu_host_guest_test
 TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync
tools/testing/selftests/kvm/include/x86/svm.h +5 -0
index c8539166270ea..7644e37c7042c 100644
@@ -316,4 +316,9 @@ struct __attribute__ ((__packed__)) vmcb {
 
 #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
 
+static inline void invlpga(unsigned long addr, u32 asid)
+{
+	asm volatile("invlpga %0, %1" : : "a"(addr), "c"(asid));
+}
+
 #endif /* SELFTEST_KVM_SVM_H */
tools/testing/selftests/kvm/include/x86/vmx.h +21 -0
index 4bcfd60e3aecb..dcd43dff4ad11 100644
@@ -563,4 +563,25 @@ bool kvm_cpu_has_ept(void);
 void vm_enable_ept(struct kvm_vm *vm);
 void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm);
 
+static inline void invvpid(unsigned long ext, u16 vpid, gva_t gva)
+{
+	struct {
+		u64 vpid : 16;
+		u64 rsvd : 48;
+		u64 gva;
+	} operand = { vpid, 0, gva };
+
+	asm volatile("invvpid %0, %1" : : "m"(operand), "r"(ext) : "memory");
+}
+
+static inline void invept(unsigned long ext, u64 eptp)
+{
+	struct {
+		u64 eptp;
+		u64 rsvd;
+	} operand = { eptp, 0 };
+
+	asm volatile("invept %0, %1" : : "m"(operand), "r"(ext) : "memory");
+}
+
 #endif /* SELFTEST_KVM_VMX_H */
tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c new file +436 -0

436 lines not on this page · raw message

Signature · 1 line
2.55.0.229.g6434b31f56-goog
#41 lore raw
In reply to Yosry Ahmed
Quoted 24 lines

Yosry Ahmed (28):
KVM: nSVM: Flush the TLB after forcefully leaving nested
KVM: SVM: Document number of ASIDs CPUID setting
KVM: VMX: Generalize VPID allocation to be vendor-neutral
KVM: x86/mmu: Support specifying reserved TLB tags
KVM: SVM: Add helpers to set/clear ASID flush in VMCB
KVM: SVM: Fallback to flush everything if FLUSHBYASID is not available
KVM: SVM: Duplicate pre-run ASID check for SEV and non-SEV guests
KVM: SEV: Do ASID initialization at VMCB initialization
KVM: SEV: Expose sev_get_asid() outside of sev.c
KVM: SVM: Use a static ASID per vCPU
KVM: SVM: Only flush the fallback ASID when used by a different vCPU
KVM: nSVM: Add a placeholder ASID for L2
KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb()
KVM: x86: hyper-v: Allow puring all TLB flush FIFOs
KVM: nSVM: Drop svm->nested.initialized
KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH
KVM: nSVM: Always switch VMCB before leaving guest mode
KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fns
KVM: nSVM: Service local TLB flushes before nested transitions
KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROL
KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12
KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enter
KVM: x86/mmu: Rename __kvm_mmu_invalidate_addr() to

Quoted 7 lines
    kvm_mmu_sync_addr()

KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the GVA
flush
KVM: nSVM: Flush L2's ASID when emulating INVLPGA
KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2
KVM: nSVM: Use different ASIDs for L1 and L2
KVM: selftests: Add a test for nested TLB flushes

Sashiko wasn't able to apply the patches:
https://sashiko.dev/#/patchset/20260728003557.1136583-1-yosry%40kernel.org.

For some reason, it failed to apply on the baseline commit, 271255273d5ff348fe29d89fe4712b2f7f7907c3.

This is the top of kvm-x86/next tho, so I am not sure what went wrong:

$ git show upstream/kvm-x86/next
commit 271255273d5ff348fe29d89fe4712b2f7f7907c3 (tag:
kvm-x86-next-2026.07.27, upstream/kvm-x86/next, upstream/kvm-x86/HEAD)
Merge: a204badd8432f 2abcdf03fda13 9c66085e6dcfb db3a46e200df2
2708ecca4dbdb 710b3a30f407e e428f9779a437 653857a5af462 ec9a16c6aeba8
05a0b701d1089
Author: Sean Christopherson <[email protected]>
Date:   Mon Jul 27 12:44:28 2026 -0700
...

The commit was from earlier today, maybe Sashiko only fetches the trees every once in a while?

kvm-x86/next is not among the list of branches it tried. I don't see the tree in MAINTAINERS, so perhaps this patch was never picked up:
https://lore.kernel.org/kvm/[email protected]/.

Adding Roman here just in case. Sean, let me know if you want me to resend this series after we figure this out.

#42 lore raw
Quoted 50 lines · Yosry Ahmed
Quoted 24 lines

Yosry Ahmed (28):
KVM: nSVM: Flush the TLB after forcefully leaving nested
KVM: SVM: Document number of ASIDs CPUID setting
KVM: VMX: Generalize VPID allocation to be vendor-neutral
KVM: x86/mmu: Support specifying reserved TLB tags
KVM: SVM: Add helpers to set/clear ASID flush in VMCB
KVM: SVM: Fallback to flush everything if FLUSHBYASID is not available
KVM: SVM: Duplicate pre-run ASID check for SEV and non-SEV guests
KVM: SEV: Do ASID initialization at VMCB initialization
KVM: SEV: Expose sev_get_asid() outside of sev.c
KVM: SVM: Use a static ASID per vCPU
KVM: SVM: Only flush the fallback ASID when used by a different vCPU
KVM: nSVM: Add a placeholder ASID for L2
KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb()
KVM: x86: hyper-v: Allow puring all TLB flush FIFOs
KVM: nSVM: Drop svm->nested.initialized
KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH
KVM: nSVM: Always switch VMCB before leaving guest mode
KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fns
KVM: nSVM: Service local TLB flushes before nested transitions
KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROL
KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12
KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enter
KVM: x86/mmu: Rename __kvm_mmu_invalidate_addr() to

Quoted 7 lines
    kvm_mmu_sync_addr()

KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the GVA
flush
KVM: nSVM: Flush L2's ASID when emulating INVLPGA
KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2
KVM: nSVM: Use different ASIDs for L1 and L2
KVM: selftests: Add a test for nested TLB flushes

Sashiko wasn't able to apply the patches:
https://sashiko.dev/#/patchset/20260728003557.1136583-1-yosry%40kernel.org.

For some reason, it failed to apply on the baseline commit, 271255273d5ff348fe29d89fe4712b2f7f7907c3.

This is the top of kvm-x86/next tho, so I am not sure what went wrong:

$ git show upstream/kvm-x86/next
commit 271255273d5ff348fe29d89fe4712b2f7f7907c3 (tag:
kvm-x86-next-2026.07.27, upstream/kvm-x86/next, upstream/kvm-x86/HEAD)
Merge: a204badd8432f 2abcdf03fda13 9c66085e6dcfb db3a46e200df2
2708ecca4dbdb 710b3a30f407e e428f9779a437 653857a5af462 ec9a16c6aeba8
05a0b701d1089
Author: Sean Christopherson <[email protected]>
Date:   Mon Jul 27 12:44:28 2026 -0700
...
Quoted 6 lines

The commit was from earlier today, maybe Sashiko only fetches the trees every once in a while?

kvm-x86/next is not among the list of branches it tried. I don't see the tree in MAINTAINERS, so perhaps this patch was never picked up:
https://lore.kernel.org/kvm/[email protected]/.

Yeah, I need to get Paolo's eyeballs on those changes.

Quoted 2 lines

Adding Roman here just in case. Sean, let me know if you want me to resend this series after we figure this out.

This is probably my fault? I force-pushed to kvm-x86/next this morning to fix a stupid goof I made, maybe Sashiko was confused by the exact objects not being in in linux-next? I would say do nothing for now, we can always do a resend if us humans don't find anything in this version to necessitate a v2.

#43 lore raw
Quoted 52 lines · Sean Christopherson
Quoted 50 lines · Yosry Ahmed
Quoted 24 lines

Yosry Ahmed (28):
KVM: nSVM: Flush the TLB after forcefully leaving nested
KVM: SVM: Document number of ASIDs CPUID setting
KVM: VMX: Generalize VPID allocation to be vendor-neutral
KVM: x86/mmu: Support specifying reserved TLB tags
KVM: SVM: Add helpers to set/clear ASID flush in VMCB
KVM: SVM: Fallback to flush everything if FLUSHBYASID is not available
KVM: SVM: Duplicate pre-run ASID check for SEV and non-SEV guests
KVM: SEV: Do ASID initialization at VMCB initialization
KVM: SEV: Expose sev_get_asid() outside of sev.c
KVM: SVM: Use a static ASID per vCPU
KVM: SVM: Only flush the fallback ASID when used by a different vCPU
KVM: nSVM: Add a placeholder ASID for L2
KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb()
KVM: x86: hyper-v: Allow puring all TLB flush FIFOs
KVM: nSVM: Drop svm->nested.initialized
KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH
KVM: nSVM: Always switch VMCB before leaving guest mode
KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fns
KVM: nSVM: Service local TLB flushes before nested transitions
KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROL
KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12
KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enter
KVM: x86/mmu: Rename __kvm_mmu_invalidate_addr() to

Quoted 7 lines
    kvm_mmu_sync_addr()

KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the GVA
flush
KVM: nSVM: Flush L2's ASID when emulating INVLPGA
KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2
KVM: nSVM: Use different ASIDs for L1 and L2
KVM: selftests: Add a test for nested TLB flushes

Sashiko wasn't able to apply the patches:
https://sashiko.dev/#/patchset/20260728003557.1136583-1-yosry%40kernel.org.

For some reason, it failed to apply on the baseline commit, 271255273d5ff348fe29d89fe4712b2f7f7907c3.

This is the top of kvm-x86/next tho, so I am not sure what went wrong:

$ git show upstream/kvm-x86/next
commit 271255273d5ff348fe29d89fe4712b2f7f7907c3 (tag:
kvm-x86-next-2026.07.27, upstream/kvm-x86/next, upstream/kvm-x86/HEAD)
Merge: a204badd8432f 2abcdf03fda13 9c66085e6dcfb db3a46e200df2
2708ecca4dbdb 710b3a30f407e e428f9779a437 653857a5af462 ec9a16c6aeba8
05a0b701d1089
Author: Sean Christopherson <[email protected]>
Date:   Mon Jul 27 12:44:28 2026 -0700
...
Quoted 8 lines
Quoted 6 lines

The commit was from earlier today, maybe Sashiko only fetches the trees every once in a while?

kvm-x86/next is not among the list of branches it tried. I don't see the tree in MAINTAINERS, so perhaps this patch was never picked up:
https://lore.kernel.org/kvm/[email protected]/.

Yeah, I need to get Paolo's eyeballs on those changes.

It's really none of my business, but what about just getting patch 1 merged for now? I think this is everything Sashiko needs and the tree is already documented in the maintainer profile (so it's nothing new)?

#3 lore raw

Re: [PATCH v1 02/28] KVM: SVM: Document number of ASIDs CPUID setting

In reply to Yosry Ahmed
Quoted 34 lines · Yosry Ahmed

KVM arbitrarly advertises 8 ASIDs in KVM_GET_SUPPORTED_CPUID for historical reasons. Advertising a small number of ASIDs to L1 is currently harmless, as KVM (L0) uses the same ASID for both L1 and L2, and flushes that ASID on nested transitions.

However, following changes will add proper ASID emulation and a separate ASID for L2, minimizing the TLB flushes on nested transitions. At that point, a full TLB flush from a KVM guest (L1) would flush both L1 and L2 ASIDs, so should be avoided as much as possible by advertising more ASIDs to L1.

Increasing the value advertised by KVM is tricky, because a VMM copying the number of ASIDs from KVM_GET_SUPPORTED_CPUID may perceive a new VM with a higher number of ASIDs as incompatible with older KVM (which is not true).

Avoid changing the value advertised by KVM, but document that it is essentially meaningless and does not represent a limit or a hint. Also, more-or-less encourage userspace to advertise a higher value to avoid potential unnecessary TLB flushes from an L1 hypervisor.

---
 Documentation/virt/kvm/api.rst | 20 ++++++++++++++++++++
 arch/x86/kvm/cpuid.c           |  4 ++--
 2 files changed, 22 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +0 -0
index a5f9ee92f43e8..2d24bf708d77e 100644

Documentation/virt/kvm/api.rst the APIC ID is found in EDX for all subleaves of 0x0b and 0x1f, and in EAX · line 9550

Quoted 9 lines
Documentation/virt/kvm/api.rst +6 -0
@@ -9542,6 +9542,26 @@ the APIC ID is found in EDX for all subleaves of 0x0b and 0x1f, and in EAX
 for 0x8000001e; the latter also encodes the core id and node id in bits
 7:0 of EBX and ECX respectively.
 
+Number of ASIDs (nested SVM)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+CPUID[0x8000000A]:EBX reports the number of ASIDs supported by the processor
+on AMD CPUs. KVM's ``KVM_GET_SUPPORTED_CPUID`` hardcodes this value to 8 for
+historical reasons. This is value is arbitrary and does not represent a

This value*

#5 lore raw

Re: [PATCH v1 03/28] KVM: VMX: Generalize VPID allocation to be vendor-neutral

In reply to Yosry Ahmed
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +0 -2
@@ -8711,8 +8685,6 @@ __init int vmx_hardware_setup(void)
        kvm_caps.has_bus_lock_exit = cpu_has_vmx_bus_lock_detection();
        kvm_caps.has_notify_vmexit = cpu_has_notify_vmexit();
 
-       set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
-
        if (enable_ept)
                kvm_mmu_set_ept_masks(enable_ept_ad_bits);
        else

arch/x86/kvm/vmx/vmx.c __init int vmx_hardware_setup(void) · line 8755

Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -8777,6 +8749,10 @@ __init int vmx_hardware_setup(void)
 
        vmx_set_cpu_caps();
 
+       r = init_vpids();
+       if (r)
+               return r;
+

From internal Sashiko:
---
If tdx_hardware_setup() fails after vmx_hardware_setup() succeeds, will this leak the dynamically allocated tlb_tags.bitmap?

Looking at vt_hardware_setup() in arch/x86/kvm/vmx/main.c:

arch/x86/kvm/vmx/main.c:vt_hardware_setup() {
        ...
        ret = vmx_hardware_setup();
        if (ret)
                return ret;
        return enable_tdx ? tdx_hardware_setup() : 0;
}

When tdx_hardware_setup() fails, it returns the error code directly without calling vmx_hardware_unsetup(). KVM's core setup (kvm_x86_vendor_init) expects vendor routines to handle their own partial initialization failures and skips hardware_unsetup() if hardware_setup() fails.

Since this patch changes the VPID bitmap from a static array to a dynamic allocation in init_vpids(), this error path will now leave tlb_tags.bitmap permanently leaked on module initialization failure.

Does vt_hardware_setup() need to call vmx_hardware_unsetup() when TDX setup fails to ensure the new dynamic allocations are freed?
---

I think this is a pre-existing issue. The allocations made by nested_vmx_hardware_setup() are also not properly cleaned up.

Quoted 15 lines
arch/x86/kvm/vmx/vmx.c +3 -1
        /*
         * Configure nested capabilities after core CPU capabilities so that
         * nested support can be conditional on base support, e.g. so that KVM
@@ -8784,8 +8760,10 @@ __init int vmx_hardware_setup(void)
         */
        if (nested) {
                r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
-               if (r)
+               if (r) {
+                       destroy_vpids();
                        return r;
+               }
        }
        vmx_nested_ops.enabled = nested;
 
#11 lore raw

Re: [PATCH v1 08/28] KVM: SEV: Do ASID initialization at VMCB initialization

In reply to Yosry Ahmed
Quoted 18 lines · Yosry Ahmed

The per-vCPU ASID does not change for SEV guests, so move the per-vCPU (and per-VMCB) ASID initialization to sev_init_vmcb() to do it once, instead of checking before every VMRUN.

No functional change intended.

---
 arch/x86/kvm/svm/sev.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/kvm/svm/sev.c +0 -0
index 0e5046a12a970..8717da0e9fe8a 100644
@@ -3580,14 +3580,6 @@ int pre_sev_run(struct vcpu_svm *svm, int cpu)
        if (!cpumask_test_cpu(cpu, to_kvm_sev_info(kvm)->have_run_cpus))

arch/x86/kvm/svm/sev.c int pre_sev_run(struct vcpu_svm *svm, int cpu) · line 3590

Quoted 10 lines
arch/x86/kvm/svm/sev.c +0 -8
                cpumask_set_cpu(cpu, to_kvm_sev_info(kvm)->have_run_cpus);
 
-       /* Assign the asid allocated with this SEV guest */
-       svm->asid = asid;
-
-       if (unlikely(svm->asid != svm->vmcb->control.asid)) {
-               svm->vmcb->control.asid = asid;
-               vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
-       }
-

From internal Sashiko:
---
Does removing this dynamic ASID assignment break nested virtualization for SEV guests?

If a guest is configured with both SEV and nested virtualization, KVM switches svm->vmcb to the dynamically allocated vmcb02 when intercepting VMRUN.

Since vmcb02 is zero-allocated during svm_allocate_nested(), and sev_init_vmcb() is only called for vmcb01 during initial vCPU creation, won't vmcb02->control.asid remain 0?

When KVM then executes VMRUN to enter the nested guest, it seems the hardware would reject the invalid ASID (0) and immediately crash the guest via a hypervisor entry failure (SVM_EXIT_ERR).

---

Yeah I think this is correct, but it gets fixed in patch 12 when we explicitly initialize the ASID in vmcb02. I can fix it if I re-spin, but I am not sure how much we care about breaking bisection for nested on SEV.

Quoted 17 lines
arch/x86/kvm/svm/sev.c +4 -1
        /*
         * Flush guest TLB:
         *
@@ -4829,6 +4821,10 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)
        svm->vmcb->control.misc_ctl |= SVM_MISC_ENABLE_SEV;
        clr_exception_intercept(svm, UD_VECTOR);
 
+       svm->asid = sev_get_asid(vcpu->kvm);
+       svm->vmcb->control.asid = svm->asid;
+       vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
+
        /*
         * Don't intercept #GP for SEV guests, e.g. for the VMware backdoor, as
         * KVM can't decrypt guest memory to decode the faulting instruction.
--

2.55.0.229.g6434b31f56-goog

#14 lore raw

Re: [PATCH v1 10/28] KVM: SVM: Use a static ASID per vCPU

In reply to Yosry Ahmed

arch/x86/kvm/svm/sev.c static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm) · line 2074

Quoted 9 lines
arch/x86/kvm/svm/sev.c +1 -0
index 3aac702f45b2b..087d18a5eda69 100644
@@ -2071,6 +2071,7 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
        kvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {
                dst_svm = to_svm(dst_vcpu);
 
+               dst_svm->asid = dst->asid;

From internal Sashiko:
---
Does this overwrite the destination vCPU's initial ASID without freeing it?

During vCPU creation for the destination VM, before it is marked as an SEV guest, svm_vcpu_create() allocates a normal TLB tag. When this migration overwrites dst_svm->asid with the source SEV ASID, the original tag appears to be leaked.

When the destination VM is eventually destroyed, is_sev_guest(vcpu) will evaluate to true, preventing the leaked tag from being recovered in free_asid(). Could this be abused to exhaust the global non-SEV TLB tag pool?
---

Yeah I think this is correct. Looking at the SEV migration selftest, it seems like the destination VM is not initialized as an SEV VM. We should probably free the non-SEV ASID before copying the ASID here?

Quoted 11 lines
arch/x86/kvm/svm/sev.c +1 -1
                sev_init_vmcb(dst_svm, false);
 
                if (!dst->es_active)
@@ -4816,7 +4817,7 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)
        svm->vmcb->control.misc_ctl |= SVM_MISC_ENABLE_SEV;
        clr_exception_intercept(svm, UD_VECTOR);
 
-       svm->asid = sev_get_asid(vcpu->kvm);
+       WARN_ON_ONCE(svm->asid != sev_get_asid(vcpu->kvm));
        svm->vmcb->control.asid = svm->asid;
        vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
#15 lore raw

Re: [PATCH v1 10/28] KVM: SVM: Use a static ASID per vCPU

In reply to Yosry Ahmed
Quoted 9 lines
arch/x86/kvm/svm/svm.h +5 -0
@@ -487,6 +482,21 @@ static inline void vmcb_set_gpat(struct vmcb *vmcb, u64 data)
        vmcb_mark_dirty(vmcb, VMCB_NPT);
 }
 
+extern kvm_tlb_tag_t fallback_asid;
+
+static inline kvm_tlb_tag_t allocate_asid(struct kvm_vcpu *vcpu)
+{
+       if (is_sev_guest(vcpu))

arch/x86/kvm/svm/svm.h static inline void vmcb_set_gpat(struct vmcb *vmcb, u64 data) · line 499

Quoted 10 lines
arch/x86/kvm/svm/svm.h +10 -0
+               return sev_get_asid(vcpu->kvm);
+       return kvm_alloc_tlb_tag() ?: fallback_asid;
+}
+
+static inline void free_asid(struct kvm_vcpu *vcpu, kvm_tlb_tag_t asid)
+{
+       if (!is_sev_guest(vcpu) && asid != fallback_asid)
+               kvm_free_tlb_tag(asid);
+}
+

From internal Sashiko:
---
Can this trigger a kernel warning during SEV VM destruction?

In sev_migrate_from(), the source VM's active flag is cleared via src->active = false. However, the source VM's vCPUs still retain their SEV ASIDs.

When the source VM is subsequently destroyed, free_asid() is called. Since is_sev_guest(vcpu) now returns false for the source VM, the SEV ASID is passed to the standard allocator:

kvm_free_tlb_tag(asid)
WARN_ON_ONCE(tag < tlb_tags.start)

Because SEV ASIDs are numerically lower than the normal tag pool start, this will trigger the warning. On production systems with panic_on_warn, could an unprivileged user exploit this by migrating and destroying an SEV VM?
---

This is similar-ish to the other issue in this patch, and more annoying. The SEV ASID is only freed when the VM is freed, which I assume only happens after all vCPUs are destroyed and free_asid() is called. I am not sure how important it is to actually clear the SEV active flag on the source of the migration, but I'd rather not try and find out.

We can simply drop the WARN here and ignore attempts to free a reserved ASID. Another option is clearing src_svm->asid in sev_migration_from().

#18 lore raw

Re: [PATCH v1 12/28] KVM: nSVM: Add a placeholder ASID for L2

In reply to Yosry Ahmed
Quoted 36 lines · Yosry Ahmed

In preparation for introducing a separate ASID for L2, introduce a 'placeholder' ASID that is still the same as L1's ASID. This will facilitate future changes that need to distinguish L1 and L2's ASIDs, before actually using a different ASID for L1 and L2.

No functional change intended.

---
 arch/x86/kvm/svm/nested.c | 5 +++--
 arch/x86/kvm/svm/svm.h    | 2 ++
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/nested.c +1 -2
index ed4af5a08f794..eb60d6b959d86 100644
@@ -698,7 +698,6 @@ static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
         *  - Honor L1's request to flush an ASID on nested VMRUN
         *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
         *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
-        *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
         *
         * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
         *     NPT guest-physical mappings on VMRUN.
@@ -928,7 +927,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
        else
                vmcb02->control.bus_lock_counter = 0;
 
-       vmcb02->control.asid = vmcb01->control.asid;
+       vmcb02->control.asid = svm->nested.asid02;
 
        /* Overwritten later if necessary.  */
        vmcb_clr_flush_asid(vmcb02);

arch/x86/kvm/svm/nested.c int svm_allocate_nested(struct vcpu_svm *svm) · line 1501

Quoted 5 lines
arch/x86/kvm/svm/nested.c +2 -0
@@ -1498,6 +1497,8 @@ int svm_allocate_nested(struct vcpu_svm *svm)
        if (!svm->nested.msrpm)
                goto err_free_vmcb02;
 
+       svm->nested.asid02 = svm->asid;
+

From internal Sashiko:
---
Does caching the ASID here risk leaving nested.asid02 stale during an intra-host SEV VM migration?

If userspace restores EFER.SVME on a destination vCPU before completing the migration, svm_allocate_nested() is called and nested.asid02 is initialized with a normal non-SEV TLB tag.

Later, when KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM completes the migration, sev_migrate_from() updates the primary ASID but misses this new nested.asid02 field:

arch/x86/kvm/svm/sev.c:sev_migrate_from() {
        ...
        dst_svm->asid = dst->asid;
        sev_init_vmcb(dst_svm, false);
        ...
}

When L1 subsequently executes VMRUN to enter L2, nested_vmcb02_prepare_control() will assign this stale non-SEV nested.asid02 to vmcb02->control.asid.

Does this mismatch cause L2 to run with an invalid non-SEV ASID while the VMCB's SEV bit is set, resulting in an unconditional #VMEXIT_INVALID hardware fault?
---

I am starting to hate SEV..

This would also be correct if the vCPUs are created on the destination VM before it's marked as an SEV VM. This is a recurring theme at this point.

Quoted 2 lines
arch/x86/kvm/svm/nested.c +0 -0
        svm->nested.initialized = true;
        return 0;
#38 lore raw

Re: [PATCH v1 26/28] KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2

In reply to Yosry Ahmed
Quoted 5 lines
arch/x86/kvm/svm/nested.c +0 -0
index 58e7d099c559e..9ea8f21be9940 100644

arch/x86/kvm/svm/nested.c static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu) · line 727

Quoted 9 lines
arch/x86/kvm/svm/nested.c +6 -0
@@ -719,6 +719,15 @@ static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
                kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
        }
 
+       /*
+        * If L1 and L2 share the same ASID in hardware (when using the fallback
+        * ASID for both, or for SEV guests), flush it on nested transitions.
+        */
+       if (svm->asid == svm->nested.asid02) {
+               WARN_ON_ONCE(svm->asid != fallback_asid && !is_sev_guest(vcpu));

From internal Sashiko:
---
Will this WARN_ON_ONCE fire deterministically on the first nested VM-Enter for normal nested SVM guests?

Because L1 and L2 ASIDs are not properly separated until a later commit in this series (commit 0fcd2c53996cc885d1e9898202bcd67438d2a97a, "KVM: nSVM:
Use different ASIDs for L1 and L2"), svm->asid and svm->nested.asid02 are unconditionally equal at this point in the git history.

For a normal nested SVM guest, both svm->asid != fallback_asid and !is_sev_guest(vcpu) will evaluate to true.

Could an untrusted L1 guest executing VMRUN trigger this warning, potentially causing a host Denial of Service if panic_on_warn is set? Even though this is resolved in a later commit, should the WARN_ON_ONCE be deferred until the ASID separation is actually implemented so that bisectability is preserved and intermediate commits remain fully self-contained?
---

I don't think a DoS is a risk here, but yeah the warning will fire at this patch. It should probably be moved till the next patch or a standalone patch.

#44 lore raw
In reply to Sean Christopherson
Quoted 4 lines

This is probably my fault? I force-pushed to kvm-x86/next this morning to fix a stupid goof I made, maybe Sashiko was confused by the exact objects not being in in linux-next? I would say do nothing for now, we can always do a resend if us humans don't find anything in this version to necessitate a v2.

I ran an internal version of Sashiko on the series and replied to individual patches with the non-bogus findings.

TL;DR mostly problems with SEV. Unfortunately, some of them are due to the first half of the series (static ASID per vCPU), not just nested support.

#34 lore raw

Re: [PATCH v1 25/28] KVM: nSVM: Flush L2's ASID when emulating INVLPGA

In reply to Yosry Ahmed
Quoted 17 lines
arch/x86/kvm/svm/svm.c +2 -0
index e4bda43238654..d7b20941b1fee 100644
@@ -2425,17 +2425,51 @@ static int clgi_interception(struct kvm_vcpu *vcpu)
 
 static int invlpga_interception(struct kvm_vcpu *vcpu)
 {
+       struct vcpu_svm *svm = to_svm(vcpu);
        /* FIXME: Handle an address size prefix. */
        gva_t gva = kvm_rax_read(vcpu);
        u32 asid = kvm_ecx_read(vcpu);
+       int cpu;
 
        if (nested_svm_check_permissions(vcpu))
                return 1;
 

arch/x86/kvm/svm/svm.c static int clgi_interception(struct kvm_vcpu *vcpu) · line 2444

Quoted 10 lines
arch/x86/kvm/svm/svm.c +6 -2
        trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
 
-       /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
-       kvm_mmu_invlpg(vcpu, gva);
+       /*
+        * INVLPG on a non-canonical address is a NOP according to the SDM,
+        * assumethe same behavior from INVLPGA since the APM doesn't specify.
+        */
+       if (is_noncanonical_invlpg_address(gva, vcpu))
+               return kvm_skip_emulated_instruction(vcpu);

From internal Sashiko:
---
This is a pre-existing issue and was not introduced by the patch under review, but if L1 is configured for 4-level paging and L2 is configured for 5-level paging, could this drop valid INVLPGA TLB flushes for L2?

Since vcpu represents L1, the check is_noncanonical_invlpg_address(gva, vcpu) uses L1's canonical boundaries. If L1 executes INVLPGA to flush a valid 57-bit virtual address for the L2 guest, will KVM incorrectly deem the L2 address as non-canonical and silently skip emulation?

Bypassing both the hardware TLB flush and shadow page table synchronization could allow L2 to continue accessing memory through old translations, potentially leading to memory corruption or security boundary bypass inside L2.
---

I need to take a closer look here, but if this is indeed an issue, even if pre-existing, it would only matter with this series. Before this series, even if INVLPGA is ignored we flush everything before running L2 anyway.

#35 lore raw

Re: [PATCH v1 25/28] KVM: nSVM: Flush L2's ASID when emulating INVLPGA

Quoted 46 lines · Yosry Ahmed
Quoted 18 lines
arch/x86/kvm/svm/svm.c +2 -0
index e4bda43238654..d7b20941b1fee 100644
@@ -2425,17 +2425,51 @@ static int clgi_interception(struct kvm_vcpu *vcpu)
 
 static int invlpga_interception(struct kvm_vcpu *vcpu)
 {
+       struct vcpu_svm *svm = to_svm(vcpu);
        /* FIXME: Handle an address size prefix. */
        gva_t gva = kvm_rax_read(vcpu);
        u32 asid = kvm_ecx_read(vcpu);
+       int cpu;
 
        if (nested_svm_check_permissions(vcpu))
                return 1;
 

arch/x86/kvm/svm/svm.c static int clgi_interception(struct kvm_vcpu *vcpu) · line 2444

Quoted 10 lines
arch/x86/kvm/svm/svm.c +6 -2
        trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
 
-       /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
-       kvm_mmu_invlpg(vcpu, gva);
+       /*
+        * INVLPG on a non-canonical address is a NOP according to the SDM,
+        * assumethe same behavior from INVLPGA since the APM doesn't specify.
+        */
+       if (is_noncanonical_invlpg_address(gva, vcpu))
+               return kvm_skip_emulated_instruction(vcpu);

From internal Sashiko:
---
This is a pre-existing issue and was not introduced by the patch under review, but if L1 is configured for 4-level paging and L2 is configured for 5-level paging, could this drop valid INVLPGA TLB flushes for L2?

Since vcpu represents L1, the check is_noncanonical_invlpg_address(gva, vcpu) uses L1's canonical boundaries. If L1 executes INVLPGA to flush a valid 57-bit virtual address for the L2 guest, will KVM incorrectly deem the L2 address as non-canonical and silently skip emulation?

Bypassing both the hardware TLB flush and shadow page table synchronization could allow L2 to continue accessing memory through old translations, potentially leading to memory corruption or security boundary bypass inside L2.
---

Quoted 4 lines

I need to take a closer look here, but if this is indeed an issue, even if pre-existing, it would only matter with this series. Before this series, even if INVLPGA is ignored we flush everything before running L2 anyway.

I don't think this is an issue. is_noncanonical_invlpg_address() doesn't use "L1's canonical boundaries". It calls is_noncanonical_address(.., X86EMUL_F_INVLPG), which calls __is_canonical_address(.., max_host_virt_addr_bits()). So the canonicality checks depend on the host support, not L1's CPUID or configuration.

The only potentially interesting case is if hardware
only supports 48-bit addresses and L1 decides to emulate 57-bit addresses (e.g. emulate 5-level paging on HW with only 4-level paging support). In this case (if it all possible to begin with), I assume it's L1's responsibility to make sure this is emulated correctly (e.g. intercept L2's page faults with higher bits set).

#36 lore raw

Re: [PATCH v1 25/28] KVM: nSVM: Flush L2's ASID when emulating INVLPGA

Quoted 52 lines · Yosry Ahmed
Quoted 46 lines · Yosry Ahmed
Quoted 18 lines
arch/x86/kvm/svm/svm.c +2 -0
index e4bda43238654..d7b20941b1fee 100644
@@ -2425,17 +2425,51 @@ static int clgi_interception(struct kvm_vcpu *vcpu)
 
 static int invlpga_interception(struct kvm_vcpu *vcpu)
 {
+       struct vcpu_svm *svm = to_svm(vcpu);
        /* FIXME: Handle an address size prefix. */
        gva_t gva = kvm_rax_read(vcpu);
        u32 asid = kvm_ecx_read(vcpu);
+       int cpu;
 
        if (nested_svm_check_permissions(vcpu))
                return 1;
 

arch/x86/kvm/svm/svm.c static int clgi_interception(struct kvm_vcpu *vcpu) · line 2444

Quoted 10 lines
arch/x86/kvm/svm/svm.c +6 -2
        trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
 
-       /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
-       kvm_mmu_invlpg(vcpu, gva);
+       /*
+        * INVLPG on a non-canonical address is a NOP according to the SDM,
+        * assumethe same behavior from INVLPGA since the APM doesn't specify.
+        */
+       if (is_noncanonical_invlpg_address(gva, vcpu))
+               return kvm_skip_emulated_instruction(vcpu);

From internal Sashiko:
---
This is a pre-existing issue and was not introduced by the patch under review, but if L1 is configured for 4-level paging and L2 is configured for 5-level paging, could this drop valid INVLPGA TLB flushes for L2?

Since vcpu represents L1, the check is_noncanonical_invlpg_address(gva, vcpu) uses L1's canonical boundaries. If L1 executes INVLPGA to flush a valid 57-bit virtual address for the L2 guest, will KVM incorrectly deem the L2 address as non-canonical and silently skip emulation?

Bypassing both the hardware TLB flush and shadow page table synchronization could allow L2 to continue accessing memory through old translations, potentially leading to memory corruption or security boundary bypass inside L2.
---

Quoted 4 lines

I need to take a closer look here, but if this is indeed an issue, even if pre-existing, it would only matter with this series. Before this series, even if INVLPGA is ignored we flush everything before running L2 anyway.

Quoted 6 lines

I don't think this is an issue. is_noncanonical_invlpg_address() doesn't use "L1's canonical boundaries". It calls is_noncanonical_address(.., X86EMUL_F_INVLPG), which calls __is_canonical_address(.., max_host_virt_addr_bits()). So the canonicality checks depend on the host support, not L1's CPUID or configuration.

Yep, this is a complete non-issue.

Quoted 6 lines

The only potentially interesting case is if hardware
only supports 48-bit addresses and L1 decides to emulate 57-bit addresses (e.g. emulate 5-level paging on HW with only 4-level paging support). In this case (if it all possible to begin with), I assume it's L1's responsibility to make sure this is emulated correctly (e.g. intercept L2's page faults with higher bits set).

Yep.

#19 lore raw

Re: [PATCH v1 12/28] KVM: nSVM: Add a placeholder ASID for L2

In reply to Yosry Ahmed
Quoted 72 lines · Yosry Ahmed
Quoted 36 lines · Yosry Ahmed

In preparation for introducing a separate ASID for L2, introduce a 'placeholder' ASID that is still the same as L1's ASID. This will facilitate future changes that need to distinguish L1 and L2's ASIDs, before actually using a different ASID for L1 and L2.

No functional change intended.

---
 arch/x86/kvm/svm/nested.c | 5 +++--
 arch/x86/kvm/svm/svm.h    | 2 ++
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/nested.c +1 -2
index ed4af5a08f794..eb60d6b959d86 100644
@@ -698,7 +698,6 @@ static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
         *  - Honor L1's request to flush an ASID on nested VMRUN
         *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
         *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
-        *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
         *
         * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
         *     NPT guest-physical mappings on VMRUN.
@@ -928,7 +927,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
        else
                vmcb02->control.bus_lock_counter = 0;
 
-       vmcb02->control.asid = vmcb01->control.asid;
+       vmcb02->control.asid = svm->nested.asid02;
 
        /* Overwritten later if necessary.  */
        vmcb_clr_flush_asid(vmcb02);

arch/x86/kvm/svm/nested.c int svm_allocate_nested(struct vcpu_svm *svm) · line 1501

Quoted 5 lines
arch/x86/kvm/svm/nested.c +2 -0
@@ -1498,6 +1497,8 @@ int svm_allocate_nested(struct vcpu_svm *svm)
        if (!svm->nested.msrpm)
                goto err_free_vmcb02;
 
+       svm->nested.asid02 = svm->asid;
+

From internal Sashiko:
---
Does caching the ASID here risk leaving nested.asid02 stale during an intra-host SEV VM migration?

If userspace restores EFER.SVME on a destination vCPU before completing the migration, svm_allocate_nested() is called and nested.asid02 is initialized with a normal non-SEV TLB tag.

Later, when KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM completes the migration, sev_migrate_from() updates the primary ASID but misses this new nested.asid02 field:

arch/x86/kvm/svm/sev.c:sev_migrate_from() {
        ...
        dst_svm->asid = dst->asid;
        sev_init_vmcb(dst_svm, false);
        ...
}

When L1 subsequently executes VMRUN to enter L2, nested_vmcb02_prepare_control() will assign this stale non-SEV nested.asid02 to vmcb02->control.asid.

Does this mismatch cause L2 to run with an invalid non-SEV ASID while the VMCB's SEV bit is set, resulting in an unconditional #VMEXIT_INVALID hardware fault?
---

Quoted 5 lines

I am starting to hate SEV..

This would also be correct if the vCPUs are created on the destination VM before it's marked as an SEV VM. This is a recurring theme at this point.

So for this problem and the other two SEV migration problems reported in patch #10, I am thinking we end up with something like this over both patches (untested) -- any feedback would be appreciated as I have ~0 knowledge of SEV and ~0 testing capabilities:

arch/x86/kvm/svm/sev.c +29 -6
index 087d18a5eda69..09a0572996352 100644
@@ -2019,6 +2019,29 @@ static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
        atomic_set_release(&src_sev->migration_in_progress, 0);
 }
 
+static void sev_vcpu_migrate_asid(struct vcpu_svm *dst_svm, struct vcpu_svm *src_svm,
+                                 unsigned int asid)
+{
+       /*
+        * Free the (potentially non-SEV) ASID on the destination vCPU before
+        * setting the new SEV ASID. Clear the ASID on the source vCPU to avoid
+        * mistakenly attempting to free an SEV ASID after the vCPU is no longer
+        * an SEV vCPU.
+        */
+       free_asid(dst_svm->asid);
+       dst_svm->asid = asid;
+       src_svm->asid = 0;
+
+       /*
+        * If nested is already initialized on the destination vCPU, update the
+        * nested ASID as well to match the new SEV ASID.
+        */
+       if (dst_svm->nested.vmcb02.ptr) {
+               dst_svm->nested.asid02 = asid;
+               dst_svm->nested.vmcb02.ptr->control.asid = asid;
+       }
+}
+
 static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
 {
        struct kvm_sev_info *dst = to_kvm_sev_info(dst_kvm);
@@ -2071,12 +2094,6 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
        kvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {
                dst_svm = to_svm(dst_vcpu);
 
-               dst_svm->asid = dst->asid;
-               sev_init_vmcb(dst_svm, false);
-
-               if (!dst->es_active)
-                       continue;
-
                /*
                 * Note, the source is not required to have the same number of
                 * vCPUs as the destination when migrating a vanilla SEV VM.
@@ -2084,6 +2101,12 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
                src_vcpu = kvm_get_vcpu(src_kvm, i);
                src_svm = to_svm(src_vcpu);
 
+               sev_vcpu_migrate_asid(dst_svm, src_svm, dst->asid);
+               sev_init_vmcb(dst_svm, false);
+
+               if (!dst->es_active)
+                       continue;
+
                /*
                 * Transfer VMSA and GHCB state to the destination.  Nullify and
                 * clear source fields as appropriate, the state now belongs to
#20 lore raw

Re: [PATCH v1 12/28] KVM: nSVM: Add a placeholder ASID for L2

Quoted 92 lines · Yosry Ahmed
Quoted 72 lines · Yosry Ahmed
Quoted 36 lines · Yosry Ahmed

In preparation for introducing a separate ASID for L2, introduce a 'placeholder' ASID that is still the same as L1's ASID. This will facilitate future changes that need to distinguish L1 and L2's ASIDs, before actually using a different ASID for L1 and L2.

No functional change intended.

---
 arch/x86/kvm/svm/nested.c | 5 +++--
 arch/x86/kvm/svm/svm.h    | 2 ++
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/nested.c +1 -2
index ed4af5a08f794..eb60d6b959d86 100644
@@ -698,7 +698,6 @@ static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
         *  - Honor L1's request to flush an ASID on nested VMRUN
         *  - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
         *  - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
-        *  - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
         *
         * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
         *     NPT guest-physical mappings on VMRUN.
@@ -928,7 +927,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
        else
                vmcb02->control.bus_lock_counter = 0;
 
-       vmcb02->control.asid = vmcb01->control.asid;
+       vmcb02->control.asid = svm->nested.asid02;
 
        /* Overwritten later if necessary.  */
        vmcb_clr_flush_asid(vmcb02);

arch/x86/kvm/svm/nested.c int svm_allocate_nested(struct vcpu_svm *svm) · line 1501

Quoted 5 lines
arch/x86/kvm/svm/nested.c +2 -0
@@ -1498,6 +1497,8 @@ int svm_allocate_nested(struct vcpu_svm *svm)
        if (!svm->nested.msrpm)
                goto err_free_vmcb02;
 
+       svm->nested.asid02 = svm->asid;
+

From internal Sashiko:
---
Does caching the ASID here risk leaving nested.asid02 stale during an intra-host SEV VM migration?

If userspace restores EFER.SVME on a destination vCPU before completing the migration, svm_allocate_nested() is called and nested.asid02 is initialized with a normal non-SEV TLB tag.

Later, when KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM completes the migration, sev_migrate_from() updates the primary ASID but misses this new nested.asid02 field:

arch/x86/kvm/svm/sev.c:sev_migrate_from() {
        ...
        dst_svm->asid = dst->asid;
        sev_init_vmcb(dst_svm, false);
        ...
}

When L1 subsequently executes VMRUN to enter L2, nested_vmcb02_prepare_control() will assign this stale non-SEV nested.asid02 to vmcb02->control.asid.

Does this mismatch cause L2 to run with an invalid non-SEV ASID while the VMCB's SEV bit is set, resulting in an unconditional #VMEXIT_INVALID hardware fault?
---

Quoted 5 lines

I am starting to hate SEV..

This would also be correct if the vCPUs are created on the destination VM before it's marked as an SEV VM. This is a recurring theme at this point.

So for this problem and the other two SEV migration problems reported in patch #10, I am thinking we end up with something like this over both patches (untested) -- any feedback would be appreciated as I have ~0 knowledge of SEV and ~0 testing capabilities:

arch/x86/kvm/svm/sev.c +0 -0
index 087d18a5eda69..09a0572996352 100644
@@ -2019,6 +2019,29 @@ static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)
        atomic_set_release(&src_sev->migration_in_progress, 0);
 }
 

arch/x86/kvm/svm/sev.c static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) · line 2031

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+static void sev_vcpu_migrate_asid(struct vcpu_svm *dst_svm, struct vcpu_svm *src_svm,
+                                 unsigned int asid)
+{
+       /*
+        * Free the (potentially non-SEV) ASID on the destination vCPU before
+        * setting the new SEV ASID. Clear the ASID on the source vCPU to avoid
+        * mistakenly attempting to free an SEV ASID after the vCPU is no longer
+        * an SEV vCPU.
+        */
+       free_asid(dst_svm->asid);

Actually this doesn't solve the problem of leaking the non-SEV ASID, because is_sev_guest() will return true for the destination vCPU at this point AFAICT.

It's probably best to have free_asid() not key off is_sev_guest() at all, and explicitly check if the ASID is reserved. Something like:

static inline void free_asid(kvm_tlb_tag_t asid)

{
	if (asid >= svm_nr_reserved_asids && asid != fallback_asid)
                kvm_free_tlb_tag(asid);
}

, or even better, we can have fallback_asid be a reserved ASID as well (if all ASIDs are not allocated to SEV), then we can just check reserved ASIDs and we don't have to worry about freeing the fallback_asid.

arch/x86/kvm/svm/sev.c static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) · line 2033

Quoted 2 lines
arch/x86/kvm/svm/sev.c +2 -0
+       dst_svm->asid = asid;
+       src_svm->asid = 0;

..and the above would also remove te need to clear src_svm->asid here.

Quoted 42 lines
arch/x86/kvm/svm/sev.c +17 -6
+
+       /*
+        * If nested is already initialized on the destination vCPU, update the
+        * nested ASID as well to match the new SEV ASID.
+        */
+       if (dst_svm->nested.vmcb02.ptr) {
+               dst_svm->nested.asid02 = asid;
+               dst_svm->nested.vmcb02.ptr->control.asid = asid;
+       }
+}
+
 static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
 {
        struct kvm_sev_info *dst = to_kvm_sev_info(dst_kvm);
@@ -2071,12 +2094,6 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
        kvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {
                dst_svm = to_svm(dst_vcpu);
 
-               dst_svm->asid = dst->asid;
-               sev_init_vmcb(dst_svm, false);
-
-               if (!dst->es_active)
-                       continue;
-
                /*
                 * Note, the source is not required to have the same number of
                 * vCPUs as the destination when migrating a vanilla SEV VM.
@@ -2084,6 +2101,12 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
                src_vcpu = kvm_get_vcpu(src_kvm, i);
                src_svm = to_svm(src_vcpu);
 
+               sev_vcpu_migrate_asid(dst_svm, src_svm, dst->asid);
+               sev_init_vmcb(dst_svm, false);
+
+               if (!dst->es_active)
+                       continue;
+
                /*
                 * Transfer VMSA and GHCB state to the destination.  Nullify and
                 * clear source fields as appropriate, the state now belongs to