| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/08 00:29 | flow | patch-triage |
5mArgs:null Results: {
"EnableConfigs": [
"KVM",
"KVM_AMD",
"KVM_INTEL",
"KVM_AMD_SEV",
"KVM_HYPERV"
],
"FocusSymbols": [
"nested_svm_vmrun",
"nested_svm_vmexit",
"enter_svm_guest_mode",
"svm_set_nested_state",
"invlpga_interception",
"svm_flush_tlb_asid",
"svm_flush_tlb_all",
"kvm_alloc_tlb_tag",
"kvm_free_tlb_tag",
"__kvm_mmu_invlpg",
"__kvm_mmu_invalidate_addr",
"svm_allocate_nested",
"svm_free_nested",
"sev_migrate_from"
],
"KMSANReasoning": "The patch series refactors KVM x86 TLB flushing, unifies TLB tag (VPID/ASID) allocation between VMX and SVM via bitmap_zalloc, improves nested SVM ASID tracking and TLB invalidation handling, and introduces KVM_REQ_MMU_SYNC_ALL_ROOTS.\n\nKMSAN vs KASAN applicability:\n1. No uninitialized memory usage:\n - The newly allocated bitmap in `kvm_init_tlb_tags` is allocated using `bitmap_zalloc()`, which zeroes all allocated memory.\n - New struct members in `vcpu_svm` and `svm_nested_state` (`asid02`, `last_asid`) belong to `vcpu_svm`, which is allocated via `kmem_cache_zalloc()`, ensuring they are zero-initialized.\n - All stack local variables in modified/added functions (such as `new_asid`, `bit`, `nr`, `fifo`) are explicitly initialized before being read or used in conditional logic.\n2. No kernel-to-user info-leaks:\n - The patch does not modify or introduce any data copied to userspace (no `copy_to_user`, `put_user`, or ioctl output alterations).\n3. Potential bug surfaces:\n - Potential errors in this series relate to TLB synchronization correctness, logic flaws in ASID reuse, concurrency, or locking, which are either architectural emulation correctness issues or covered by KASAN and LOCKDEP.\n\nBecause no uninitialized memory risks or info-leaks are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch series introduces a shared TLB tag management framework across x86 KVM, refactors VPID allocation on Intel VMX and ASID management on AMD SVM, reworks nested SVM TLB flushing and MMU synchronization (including a new KVM_REQ_MMU_SYNC_ALL_ROOTS request and dedicated asid02 allocation for nested guests), updates INVLPGA instruction interception handling, and introduces multiple new runtime assertions (WARN_ON_ONCE, KVM_BUG_ON). These changes modify reachable virtualization paths exposed via /dev/kvm ioctls and nested SVM execution, warranting fuzzing for potential state desynchronization, memory leaks, and assertion violations.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/08 00:29 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 15e9e385bcc0e8a4bbd6ce129c9a926c3d78c56c\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Thu Oct 8 00:29:28 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst\nindex ddc61ea36ac59..60f49b3675920 100644\n--- a/Documentation/virt/kvm/api.rst\n+++ b/Documentation/virt/kvm/api.rst\n@@ -9770,6 +9770,26 @@ the APIC ID is found in EDX for all subleaves of 0x0b and 0x1f, and in EAX\n for 0x8000001e; the latter also encodes the core id and node id in bits\n 7:0 of EBX and ECX respectively.\n \n+Number of ASIDs (nested SVM)\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+\n+CPUID[0x8000000A]:EBX reports the number of ASIDs supported by the processor\n+on AMD CPUs. KVM's ``KVM_GET_SUPPORTED_CPUID`` hardcodes this value to 8 for\n+historical reasons. This value is arbitrary and does not represent a limit\n+on the supported number of ASIDs, or even a reasonable default value.\n+\n+KVM allows userspace to set any value (up to 0xFFFFFFFF), and emulates those\n+ASIDs by multiplexing them onto hardware ASIDs. AMD CPUs have only ever\n+supported 3 values: 0x40, 0x8000, and 0x1000 -- with modern CPUs advertising\n+0x8000.\n+\n+Advertising a small number of ASIDs could lead an L1 hypervisor to perform\n+unnecessary TLB flushes (e.g. older KVM would flush the entire TLB when running\n+out of ASIDs). Userspace should probably set the number of ASIDs to a\n+reasonably high power of 2 value to remain consistent with hardware and avoid\n+any misbehavior by L1 hypervisors, ideally one of the values advertised by\n+actual hardware.\n+\n Obsolete ioctls and capabilities\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n \ndiff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h\nindex e2eef7057bd35..1d28bb7e92c7f 100644\n--- a/arch/x86/include/asm/kvm_host.h\n+++ b/arch/x86/include/asm/kvm_host.h\n@@ -127,6 +127,7 @@\n \tKVM_ARCH_REQ_FLAGS(33, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)\n #define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE \\\n \tKVM_ARCH_REQ_FLAGS(34, KVM_REQUEST_WAIT)\n+#define KVM_REQ_MMU_SYNC_ALL_ROOTS\tKVM_ARCH_REQ(35)\n \n #define INVALID_PAGE (~(hpa_t)0)\n #define VALID_PAGE(x) ((x) != INVALID_PAGE)\ndiff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c\nindex adafd6fa2c542..e45c01d6faeaf 100644\n--- a/arch/x86/kvm/cpuid.c\n+++ b/arch/x86/kvm/cpuid.c\n@@ -1846,8 +1846,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)\n \t\t\tbreak;\n \t\t}\n \t\tentry-\u003eeax = 1; /* SVM revision 1 */\n-\t\tentry-\u003eebx = 8; /* Lets support 8 ASIDs in case we add proper\n-\t\t\t\t ASID emulation to nested SVM */\n+\t\tentry-\u003eebx = 8; /* Arbitrary value for historical reasons. KVM\n+\t\t\t\t practically allows and ignores all values. */\n \t\tentry-\u003eecx = 0; /* Reserved */\n \t\tcpuid_entry_override(entry, CPUID_8000_000A_EDX);\n \t\tbreak;\ndiff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h\nindex 622a6553e9ac2..b82f70e6fcea1 100644\n--- a/arch/x86/kvm/hyperv.h\n+++ b/arch/x86/kvm/hyperv.h\n@@ -312,18 +312,34 @@ static inline struct kvm_vcpu_hv_tlb_flush_fifo *kvm_hv_get_tlb_flush_fifo(struc\n \treturn \u0026hv_vcpu-\u003etlb_flush_fifo[i];\n }\n \n-static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu)\n+/* Purge pending TLB flushes in @fifo, or in all FIFOs if @fifo is NULL */\n+static inline void __kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu,\n+\t\t\t\t\t\t struct kvm_vcpu_hv_tlb_flush_fifo *fifo)\n {\n-\tstruct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;\n+\tstruct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);\n+\tint i;\n \n-\tif (!kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))\n+\tif (!hv_vcpu || !kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))\n \t\treturn;\n \n-\ttlb_flush_fifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu));\n-\tif (!tlb_flush_fifo)\n-\t\treturn;\n+\tfor (i = 0; i \u003c ARRAY_SIZE(hv_vcpu-\u003etlb_flush_fifo); i++) {\n+\t\tif (!fifo || fifo == \u0026hv_vcpu-\u003etlb_flush_fifo[i])\n+\t\t\tkfifo_reset_out(\u0026hv_vcpu-\u003etlb_flush_fifo[i].entries);\n+\t}\n+}\n+\n+static inline void kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu)\n+{\n+\tstruct kvm_vcpu_hv_tlb_flush_fifo *fifo;\n+\n+\tfifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu));\n+\tif (fifo)\n+\t\t__kvm_hv_purge_tlb_flush_fifo(vcpu, fifo);\n+}\n \n-\tkfifo_reset_out(\u0026tlb_flush_fifo-\u003eentries);\n+static inline void kvm_hv_purge_all_tlb_flush_fifos(struct kvm_vcpu *vcpu)\n+{\n+\t__kvm_hv_purge_tlb_flush_fifo(vcpu, NULL);\n }\n \n static inline bool guest_hv_cpuid_has_l2_tlb_flush(struct kvm_vcpu *vcpu)\n@@ -396,7 +412,8 @@ static inline int kvm_hv_hypercall(struct kvm_vcpu *vcpu)\n {\n \treturn HV_STATUS_ACCESS_DENIED;\n }\n-static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu) {}\n+static inline void kvm_hv_purge_tlb_flush_fifo(struct kvm_vcpu *vcpu) {}\n+static inline void kvm_hv_purge_all_tlb_flush_fifos(struct kvm_vcpu *vcpu) {}\n static inline bool kvm_hv_synic_has_vector(struct kvm_vcpu *vcpu, int vector)\n {\n \treturn false;\ndiff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h\nindex 2ae7f9ed4cf86..b5100f31f6a35 100644\n--- a/arch/x86/kvm/mmu.h\n+++ b/arch/x86/kvm/mmu.h\n@@ -150,7 +150,10 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,\n int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,\n \t\t void *insn, int insn_len);\n void kvm_mmu_print_sptes(struct kvm_vcpu *vcpu, gpa_t gpa, const char *msg);\n+void __kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva, bool flush_gva);\n void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);\n+void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,\n+\t\t\t u64 addr, unsigned long roots, bool flush_gva);\n void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,\n \t\t\t u64 addr, unsigned long roots);\n void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid);\n@@ -170,6 +173,13 @@ void kvm_mmu_unload(struct kvm_vcpu *vcpu);\n void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu);\n void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);\n void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu);\n+\n+static inline void kvm_mmu_sync_all_roots(struct kvm_vcpu *vcpu)\n+{\n+\tkvm_mmu_sync_roots(vcpu);\n+\tkvm_mmu_sync_prev_roots(vcpu);\n+}\n+\n void kvm_mmu_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,\n \t\t\t int bytes);\n \n@@ -410,4 +420,12 @@ static inline bool kvm_is_gfn_alias(struct kvm *kvm, gfn_t gfn)\n {\n \treturn gfn \u0026 kvm_gfn_direct_bits(kvm);\n }\n+\n+typedef unsigned int kvm_tlb_tag_t;\n+\n+int kvm_init_tlb_tags(unsigned int nr_total, unsigned int nr_reserved);\n+void kvm_destroy_tlb_tags(void);\n+kvm_tlb_tag_t kvm_alloc_tlb_tag(void);\n+void kvm_free_tlb_tag(kvm_tlb_tag_t tag);\n+\n #endif\ndiff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c\nindex aadd5bdc2c97a..bfc3dd08bedb0 100644\n--- a/arch/x86/kvm/mmu/mmu.c\n+++ b/arch/x86/kvm/mmu/mmu.c\n@@ -6732,8 +6732,8 @@ void kvm_mmu_print_sptes(struct kvm_vcpu *vcpu, gpa_t gpa, const char *msg)\n }\n EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_print_sptes);\n \n-static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,\n-\t\t\t\t u64 addr, hpa_t root_hpa)\n+static void kvm_mmu_sync_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,\n+\t\t\t u64 addr, hpa_t root_hpa)\n {\n \tstruct kvm_shadow_walk_iterator iterator;\n \n@@ -6769,8 +6769,8 @@ static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu\n \twrite_unlock(\u0026vcpu-\u003ekvm-\u003emmu_lock);\n }\n \n-void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,\n-\t\t\t u64 addr, unsigned long roots)\n+void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,\n+\t\t\t u64 addr, unsigned long roots, bool flush_gva)\n {\n \tstruct kvm_mmu *mmu;\n \tint i;\n@@ -6783,7 +6783,8 @@ void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,\n \t\tif (is_noncanonical_invlpg_address(addr, vcpu))\n \t\t\treturn;\n \n-\t\tkvm_x86_call(flush_tlb_gva)(vcpu, addr);\n+\t\tif (flush_gva)\n+\t\t\tkvm_x86_call(flush_tlb_gva)(vcpu, addr);\n \n \t\tif (tdp_enabled)\n \t\t\treturn;\n@@ -6798,16 +6799,23 @@ void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,\n \t\treturn;\n \n \tif (roots \u0026 KVM_MMU_ROOT_CURRENT)\n-\t\t__kvm_mmu_invalidate_addr(vcpu, mmu, addr, mmu-\u003eroot.hpa);\n+\t\tkvm_mmu_sync_addr(vcpu, mmu, addr, mmu-\u003eroot.hpa);\n \n \tfor (i = 0; i \u003c KVM_MMU_NUM_PREV_ROOTS; i++) {\n \t\tif (roots \u0026 KVM_MMU_ROOT_PREVIOUS(i))\n-\t\t\t__kvm_mmu_invalidate_addr(vcpu, mmu, addr, mmu-\u003eprev_roots[i].hpa);\n+\t\t\tkvm_mmu_sync_addr(vcpu, mmu, addr, mmu-\u003eprev_roots[i].hpa);\n \t}\n }\n+EXPORT_SYMBOL_FOR_KVM_INTERNAL(__kvm_mmu_invalidate_addr);\n+\n+void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,\n+\t\t\t u64 addr, unsigned long roots)\n+{\n+\t__kvm_mmu_invalidate_addr(vcpu, w, addr, roots, true);\n+}\n EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invalidate_addr);\n \n-void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)\n+void __kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva, bool flush_gva)\n {\n \t/*\n \t * INVLPG is required to invalidate any global mappings for the VA,\n@@ -6819,11 +6827,17 @@ void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)\n \t * be synced when switching to that new cr3, so nothing needs to be\n \t * done here for them.\n \t */\n-\tkvm_mmu_invalidate_addr(vcpu, \u0026vcpu-\u003earch.gva_walk, gva, KVM_MMU_ROOTS_ALL);\n+\t__kvm_mmu_invalidate_addr(vcpu, \u0026vcpu-\u003earch.gva_walk, gva,\n+\t\t\t\t KVM_MMU_ROOTS_ALL, flush_gva);\n \t++vcpu-\u003estat.invlpg;\n }\n-EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invlpg);\n+EXPORT_SYMBOL_FOR_KVM_INTERNAL(__kvm_mmu_invlpg);\n \n+void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)\n+{\n+\t__kvm_mmu_invlpg(vcpu, gva, true);\n+}\n+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invlpg);\n \n void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid)\n {\n@@ -8178,6 +8192,98 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm)\n \t\tvhost_task_stop(kvm-\u003earch.nx_huge_page_recovery_thread);\n }\n \n+static struct {\n+\tspinlock_t\tlock;\n+\tunsigned long\t*bitmap;\n+\tunsigned int\tnr;\n+\tkvm_tlb_tag_t\tstart;\n+} tlb_tags;\n+\n+/*\n+ * Initialize @nr_total TLB tags, where @nr_reserved of those tags are reserved.\n+ * Reserved tags start at 0 and are contiguous. Hence, usable tags are\n+ * [nr_reserved, nr_total). At least one reserved tag is required, as 0 is the\n+ * host TLB tag on both VMX and AMD, and is the value returned on a failed tag\n+ * allocation.\n+ */\n+int kvm_init_tlb_tags(unsigned int nr_total, unsigned int nr_reserved)\n+{\n+\t/*\n+\t * Limit the number of TLB tags to VMX's hardcoded maximum of 0x10000\n+\t * to avoid wasting memory for the bitmap in the unlikely scenario the\n+\t * CPU supports an inordinate number of ASIDs (on AMD). If userspace\n+\t * wants to concurrently run tens of thousands of vCPUs, they'll likely\n+\t * need a solution that works for both Intel and AMD.\n+\t */\n+\tconst unsigned int MAX_NR_TLB_TAGS = VMX_NR_VPIDS;\n+\tunsigned int nr;\n+\n+\tif (WARN_ON_ONCE(!nr_reserved || nr_reserved \u003e nr_total))\n+\t\treturn -EINVAL;\n+\n+\tnr = nr_total - nr_reserved;\n+\tif (!nr)\n+\t\treturn 0;\n+\n+\tif (nr \u003e MAX_NR_TLB_TAGS) {\n+\t\tpr_warn_once(\"Number of TLB tags capped (%u instead of %u)\\n\",\n+\t\t\t MAX_NR_TLB_TAGS, nr);\n+\t\tnr = MAX_NR_TLB_TAGS;\n+\t}\n+\n+\ttlb_tags.bitmap = bitmap_zalloc(nr, GFP_KERNEL);\n+\tif (!tlb_tags.bitmap)\n+\t\treturn -ENOMEM;\n+\n+\ttlb_tags.nr = nr;\n+\ttlb_tags.start = nr_reserved;\n+\tspin_lock_init(\u0026tlb_tags.lock);\n+\treturn 0;\n+}\n+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_init_tlb_tags);\n+\n+void kvm_destroy_tlb_tags(void)\n+{\n+\tbitmap_free(tlb_tags.bitmap);\n+\ttlb_tags.bitmap = NULL;\n+}\n+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_destroy_tlb_tags);\n+\n+kvm_tlb_tag_t kvm_alloc_tlb_tag(void)\n+{\n+\tunsigned int bit;\n+\n+\tif (!tlb_tags.bitmap)\n+\t\treturn 0;\n+\n+\tguard(spinlock)(\u0026tlb_tags.lock);\n+\n+\tbit = find_first_zero_bit(tlb_tags.bitmap, tlb_tags.nr);\n+\tif (bit \u003e= tlb_tags.nr)\n+\t\treturn 0;\n+\n+\t__set_bit(bit, tlb_tags.bitmap);\n+\treturn tlb_tags.start + bit;\n+}\n+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_alloc_tlb_tag);\n+\n+void kvm_free_tlb_tag(kvm_tlb_tag_t tag)\n+{\n+\tunsigned int bit;\n+\n+\tif (!tag || WARN_ON_ONCE(tag \u003c tlb_tags.start))\n+\t\treturn;\n+\n+\tbit = tag - tlb_tags.start;\n+\tif (WARN_ON_ONCE(bit \u003e= tlb_tags.nr))\n+\t\treturn;\n+\n+\tguard(spinlock)(\u0026tlb_tags.lock);\n+\n+\t__clear_bit(bit, tlb_tags.bitmap);\n+}\n+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_free_tlb_tag);\n+\n #ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES\n static bool hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn,\n \t\t\t\tint level)\ndiff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c\nindex af0ef678433d7..6fe68f2ae282d 100644\n--- a/arch/x86/kvm/svm/nested.c\n+++ b/arch/x86/kvm/svm/nested.c\n@@ -556,10 +556,8 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,\n \tto-\u003enested_cr3 = from-\u003enested_cr3;\n \tto-\u003epause_filter_count = from-\u003epause_filter_count;\n \tto-\u003epause_filter_thresh = from-\u003epause_filter_thresh;\n-\n-\t/* Copy asid here because nested_vmcb_check_controls() will check it */\n-\tto-\u003easid = from-\u003easid;\n-\tto-\u003eclean = from-\u003eclean;\n+\tto-\u003easid\t\t= from-\u003easid;\n+\tto-\u003eclean\t\t= from-\u003eclean;\n \n #ifdef CONFIG_KVM_HYPERV\n \t/* Hyper-V extensions (Enlightened VMCB) */\n@@ -688,26 +686,69 @@ static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,\n \tvmcb12-\u003econtrol.exit_int_info = exit_int_info;\n }\n \n-static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)\n+static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)\n {\n+\tstruct vcpu_svm *svm = to_svm(vcpu);\n+\tbool new_asid = false;\n+\n \t/* Handle pending Hyper-V TLB flush requests */\n \tkvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);\n \n+\tif (svm-\u003enested.ctl.asid != svm-\u003enested.last_asid) {\n+\t\tsvm-\u003enested.last_asid = svm-\u003enested.ctl.asid;\n+\t\tnew_asid = true;\n+\t}\n+\n \t/*\n-\t * TODO: optimize unconditional TLB flush/MMU sync. A partial list of\n-\t * things to fix before this can be conditional:\n+\t * If L1 requested a TLB flush for L2, flush L2's TLB on nested entry\n+\t * and sync the nested NPT MMU, as TLB_CONTROL also flushes NPT\n+\t * guest-physical mappings. Sync all roots, since the TLB flush is not\n+\t * specific to the currently loaded nCR3.\n+\t *\n+\t * Handle L1 changing L2's ASID12 similarly, as KVM only uses one ASID\n+\t * for L2 in hardware (per vCPU), so it must start fresh when L1 changes\n+\t * ASID12 to emulate different ASIDs correctly. Additionally, the MMU\n+\t * context is not tagged by the ASID, so the shadow NPTs cannot be\n+\t * reused across different L2 ASIDs.\n+\t *\n+\t * Note, last_asid is initialized as 0, so the first nested VM-Enter\n+\t * after setting EFER.SVME will always flush the TLB to avoid using\n+\t * stale entries.\n \t *\n-\t * - Flush TLBs for both L1 and L2 remote TLB flush\n-\t * - Honor L1's request to flush an ASID on nested VMRUN\n-\t * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]\n-\t * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN\n-\t * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST\n+\t * If L1 requested a full TLB flush for all ASIDs (including its own),\n+\t * L1's own ASID is also flushed on nested VM-Exit, before running L1.\n \t *\n-\t * [*] Unlike nested EPT, SVM's ASID management can invalidate nested\n-\t * NPT guest-physical mappings on VMRUN.\n+\t * TLB_CONTROL_FLUSH_ASID and TLB_CONTROL_FLUSH_ASID_LOCAL are handled\n+\t * equally for simplicity.\n \t */\n-\tkvm_make_request(KVM_REQ_MMU_SYNC, vcpu);\n-\tkvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);\n+\tif (new_asid || (svm-\u003enested.ctl.tlb_ctl != TLB_CONTROL_DO_NOTHING)) {\n+\t\tif (nested_npt_enabled(svm))\n+\t\t\tkvm_make_request(KVM_REQ_MMU_SYNC_ALL_ROOTS, vcpu);\n+\t\tkvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);\n+\t}\n+\n+\t/*\n+\t * If L1 and L2 share the same ASID in hardware (when using the fallback\n+\t * ASID for both, or for SEV guests), flush it on nested transitions.\n+\t */\n+\tif (svm-\u003easid == svm-\u003enested.asid02) {\n+\t\tWARN_ON_ONCE(svm-\u003easid != fallback_asid \u0026\u0026 !is_sev_guest(vcpu));\n+\t\tkvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);\n+\t}\n+}\n+\n+static void nested_svm_exit_tlb_flush(struct kvm_vcpu *vcpu)\n+{\n+\tstruct vcpu_svm *svm = to_svm(vcpu);\n+\n+\tkvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);\n+\n+\t/* Flush L1's own ASID if it requested a *full* TLB flush on VMRUN */\n+\tif (svm-\u003enested.ctl.tlb_ctl == TLB_CONTROL_FLUSH_ALL_ASID)\n+\t\tkvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);\n+\n+\tif (svm-\u003easid == svm-\u003enested.asid02)\n+\t\tkvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);\n }\n \n /*\n@@ -860,7 +901,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)\n \tstruct vmcb *vmcb01 = svm-\u003evmcb01.ptr;\n \tstruct kvm_vcpu *vcpu = \u0026svm-\u003evcpu;\n \n-\tnested_svm_transition_tlb_flush(vcpu);\n+\tnested_svm_entry_tlb_flush(vcpu);\n \n \t/* Enter Guest-Mode */\n \tenter_guest_mode(vcpu);\n@@ -936,10 +977,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)\n \telse\n \t\tvmcb02-\u003econtrol.bus_lock_counter = 0;\n \n-\t/* Done at vmrun: asid. */\n-\n-\t/* Also overwritten later if necessary. */\n-\tvmcb02-\u003econtrol.tlb_ctl = TLB_CONTROL_DO_NOTHING;\n+\tvmcb02-\u003econtrol.asid = svm-\u003enested.asid02;\n \n \t/* Use vmcb01 MMU and format if guest does not use nNPT */\n \tif (nested_npt_enabled(svm)) {\n@@ -1038,6 +1076,20 @@ static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to\n \tto_vmcb-\u003esave.spec_ctrl = from_vmcb-\u003esave.spec_ctrl;\n }\n \n+static void nested_svm_service_local_tlb_flushes(struct kvm_vcpu *vcpu)\n+{\n+\tstruct vcpu_svm *svm = to_svm(vcpu);\n+\n+\t/*\n+\t * TLB flushes are applied to the VMCB, so apply any pending TLB flushes\n+\t * on the outgoing VMCB before switching to a new one. A TLB flush could\n+\t * purge the relevant HV TLB flush FIFO depending on guest_mode, so make\n+\t * sure the VMCB and guest_mode context is consistent.\n+\t */\n+\tWARN_ON_ONCE(is_guest_mode(vcpu) != (svm-\u003evmcb == svm-\u003enested.vmcb02.ptr));\n+\tkvm_service_local_tlb_flush_requests(vcpu);\n+}\n+\n int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)\n {\n \tstruct vcpu_svm *svm = to_svm(vcpu);\n@@ -1069,6 +1121,8 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)\n \n \tnested_svm_copy_common_state(svm-\u003evmcb01.ptr, svm-\u003enested.vmcb02.ptr);\n \n+\tnested_svm_service_local_tlb_flushes(vcpu);\n+\n \tsvm_switch_vmcb(svm, \u0026svm-\u003enested.vmcb02);\n \tnested_vmcb02_prepare_control(svm);\n \tnested_vmcb02_prepare_save(svm);\n@@ -1142,7 +1196,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)\n \t\treturn ret;\n \t}\n \n-\tif (WARN_ON_ONCE(!svm-\u003enested.initialized))\n+\tif (WARN_ON_ONCE(!svm-\u003enested.vmcb02.ptr))\n \t\treturn -EINVAL;\n \n \tvmcb12_gpa = kvm_rax_read(vcpu);\n@@ -1194,6 +1248,13 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)\n \t\tsvm-\u003evmcb-\u003econtrol.exit_info_1 = 0;\n \t\tsvm-\u003evmcb-\u003econtrol.exit_info_2 = 0;\n \n+\t\t/*\n+\t\t * Force a flush+sync on the next nested VMRUN. Otherwise,\n+\t\t * pending MMU syncs due to a changed ASID are lost, as\n+\t\t * last_asid was already updated.\n+\t\t */\n+\t\tsvm-\u003enested.last_asid = 0;\n+\n \t\tnested_svm_vmexit(svm);\n \t}\n \n@@ -1331,7 +1392,9 @@ void nested_svm_vmexit(struct vcpu_svm *svm)\n \tif (nested_svm_vmexit_update_vmcb12(vcpu))\n \t\tkvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);\n \n-\t/* Exit Guest-Mode */\n+\tnested_svm_service_local_tlb_flushes(vcpu);\n+\n+\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\n \tleave_guest_mode(vcpu);\n \tsvm_pmu_handle_nested_transition(svm);\n \n@@ -1358,8 +1421,6 @@ void nested_svm_vmexit(struct vcpu_svm *svm)\n \tif (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS))\n \t\tvmcb01-\u003econtrol.erap_ctl |= ERAP_CONTROL_CLEAR_RAP;\n \n-\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\n-\n \t/*\n \t * Rules for synchronizing int_ctl bits from vmcb02 to vmcb01:\n \t *\n@@ -1438,7 +1499,7 @@ void nested_svm_vmexit(struct vcpu_svm *svm)\n \tsvm-\u003evcpu.arch.dr7 = DR7_FIXED_1;\n \tkvm_update_dr7(\u0026svm-\u003evcpu);\n \n-\tnested_svm_transition_tlb_flush(vcpu);\n+\tnested_svm_exit_tlb_flush(vcpu);\n \n \tnested_svm_uninit_mmu_context(vcpu);\n \n@@ -1489,21 +1550,36 @@ int svm_allocate_nested(struct vcpu_svm *svm)\n {\n \tstruct page *vmcb02_page;\n \n-\tif (svm-\u003enested.initialized)\n+\tif (svm-\u003enested.vmcb02.ptr)\n \t\treturn 0;\n \n \tvmcb02_page = snp_safe_alloc_page();\n \tif (!vmcb02_page)\n \t\treturn -ENOMEM;\n-\tsvm-\u003enested.vmcb02.ptr = page_address(vmcb02_page);\n-\tsvm-\u003enested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) \u003c\u003c PAGE_SHIFT);\n \n \tsvm-\u003enested.msrpm = svm_vcpu_alloc_msrpm();\n \tif (!svm-\u003enested.msrpm)\n \t\tgoto err_free_vmcb02;\n \n \tsvm-\u003enested.vmcb02.cpu = -1;\n-\tsvm-\u003enested.initialized = true;\n+\tsvm-\u003enested.vmcb02.ptr = page_address(vmcb02_page);\n+\tsvm-\u003enested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) \u003c\u003c PAGE_SHIFT);\n+\n+\tsvm-\u003enested.asid02 = allocate_asid(\u0026svm-\u003evcpu);\n+\n+\t/*\n+\t * KVM uses a fallback ASID when out of ASIDs, and fails vCPU creation\n+\t * if there's no fallback ASID. ASID allocation must succeed here.\n+\t */\n+\tKVM_BUG_ON(!svm-\u003enested.asid02, svm-\u003evcpu.kvm);\n+\n+\t/*\n+\t * Clear last_asid to ensure that the ASID is flushed on the first\n+\t * nested VM-Enter. Otherwise, stale TLB entries from a previous life of\n+\t * the ASID (e.g. different vCPU or even different VM) could be used.\n+\t */\n+\tsvm-\u003enested.last_asid = 0;\n+\n \treturn 0;\n \n err_free_vmcb02:\n@@ -1513,12 +1589,14 @@ int svm_allocate_nested(struct vcpu_svm *svm)\n \n void svm_free_nested(struct vcpu_svm *svm)\n {\n-\tif (!svm-\u003enested.initialized)\n+\tif (!svm-\u003enested.vmcb02.ptr)\n \t\treturn;\n \n \tif (WARN_ON_ONCE(svm-\u003evmcb != svm-\u003evmcb01.ptr))\n \t\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\n \n+\tfree_asid(svm-\u003enested.asid02);\n+\n \tsvm_vcpu_free_msrpm(svm-\u003enested.msrpm);\n \tsvm-\u003enested.msrpm = NULL;\n \n@@ -1533,8 +1611,6 @@ void svm_free_nested(struct vcpu_svm *svm)\n \t * When the vmcb02 is freed, this optimization becomes invalid.\n \t */\n \tsvm-\u003enested.last_vmcb12_gpa = INVALID_GPA;\n-\n-\tsvm-\u003enested.initialized = false;\n }\n \n void svm_leave_nested(struct kvm_vcpu *vcpu)\n@@ -1545,6 +1621,9 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)\n \t\tvcpu-\u003earch.nested_run_pending = 0;\n \t\tsvm-\u003enested.vmcb12_gpa = INVALID_GPA;\n \n+\t\tnested_svm_service_local_tlb_flushes(vcpu);\n+\n+\t\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\n \t\tleave_guest_mode(vcpu);\n \n \t\t/*\n@@ -1556,7 +1635,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)\n \t\t */\n \t\t__svm_pmu_handle_nested_transition(svm, true);\n \n-\t\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\n+\t\tnested_svm_exit_tlb_flush(vcpu);\n \n \t\tnested_svm_uninit_mmu_context(vcpu);\n \t\tvmcb_mark_all_dirty(svm-\u003evmcb);\n@@ -1853,7 +1932,7 @@ void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)\n \tsvm_write_tsc_multiplier(vcpu);\n }\n \n-/* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */\n+/* Inverse operation of nested_copy_vmcb_control_to_cache() */\n static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,\n \t\t\t\t\t struct vmcb_ctrl_area_cached *from)\n {\n@@ -2084,6 +2163,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,\n \tsvm_copy_vmrun_state(vcpu, \u0026svm-\u003evmcb01.ptr-\u003esave, save);\n \tnested_copy_vmcb_control_to_cache(svm, ctl);\n \n+\tnested_svm_service_local_tlb_flushes(vcpu);\n+\n \tsvm_switch_vmcb(svm, \u0026svm-\u003enested.vmcb02);\n \n \tif (use_separate_l2_pat)\ndiff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c\nindex fad72cab76cf4..646108e70e21a 100644\n--- a/arch/x86/kvm/svm/sev.c\n+++ b/arch/x86/kvm/svm/sev.c\n@@ -315,11 +315,6 @@ static int sev_asid_new(struct kvm_sev_info *sev, unsigned long vm_type)\n \treturn ret;\n }\n \n-static unsigned int sev_get_asid(struct kvm *kvm)\n-{\n-\treturn to_kvm_sev_info(kvm)-\u003easid;\n-}\n-\n static void sev_asid_free(struct kvm_sev_info *sev)\n {\n \tstruct svm_cpu_data *sd;\n@@ -2037,6 +2032,28 @@ static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm)\n \tatomic_set_release(\u0026src_sev-\u003emigration_in_progress, 0);\n }\n \n+static void sev_vcpu_migrate_asid(struct vcpu_svm *dst_svm,\n+\t\t\t\t unsigned int asid)\n+{\n+\t/*\n+\t * Free the (potentially non-SEV) ASID on the destination vCPU before\n+\t * setting the new SEV ASID.\n+\t */\n+\tfree_asid(dst_svm-\u003easid);\n+\tdst_svm-\u003easid = asid;\n+\n+\t/*\n+\t * If nested is already initialized on the destination vCPU, also free\n+\t * the previous ASID (in case it was non-SEV) and update the nested ASID\n+\t * to match the new SEV ASID.\n+\t */\n+\tif (dst_svm-\u003enested.vmcb02.ptr) {\n+\t\tfree_asid(dst_svm-\u003enested.asid02);\n+\t\tdst_svm-\u003enested.asid02 = asid;\n+\t\tdst_svm-\u003enested.vmcb02.ptr-\u003econtrol.asid = asid;\n+\t}\n+}\n+\n static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)\n {\n \tstruct kvm_sev_info *dst = to_kvm_sev_info(dst_kvm);\n@@ -2101,6 +2118,7 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)\n \tkvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {\n \t\tdst_svm = to_svm(dst_vcpu);\n \n+\t\tsev_vcpu_migrate_asid(dst_svm, dst-\u003easid);\n \t\tsev_init_vmcb(dst_svm, false);\n \n \t\tif (!dst-\u003ees_active)\n@@ -3623,9 +3641,6 @@ int pre_sev_run(struct vcpu_svm *svm, int cpu)\n \tif (!cpumask_test_cpu(cpu, to_kvm_sev_info(kvm)-\u003ehave_run_cpus))\n \t\tcpumask_set_cpu(cpu, to_kvm_sev_info(kvm)-\u003ehave_run_cpus);\n \n-\t/* Assign the asid allocated with this SEV guest */\n-\tsvm-\u003easid = asid;\n-\n \t/*\n \t * Flush guest TLB:\n \t *\n@@ -3637,7 +3652,7 @@ int pre_sev_run(struct vcpu_svm *svm, int cpu)\n \t\treturn 0;\n \n \tsd-\u003esev_vmcbs[asid] = svm-\u003evmcb;\n-\tsvm-\u003evmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_FLUSH_ASID;\n+\tvmcb_set_flush_asid(svm-\u003evmcb);\n \treturn 0;\n }\n \n@@ -4889,6 +4904,10 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)\n \tsvm-\u003evmcb-\u003econtrol.misc_ctl |= SVM_MISC_ENABLE_SEV;\n \tclr_exception_intercept(svm, UD_VECTOR);\n \n+\tWARN_ON_ONCE(svm-\u003easid != sev_get_asid(vcpu-\u003ekvm));\n+\tsvm-\u003evmcb-\u003econtrol.asid = svm-\u003easid;\n+\tvmcb_mark_dirty(svm-\u003evmcb, VMCB_ASID);\n+\n \t/*\n \t * Don't intercept #GP for SEV guests, e.g. for the VMware backdoor, as\n \t * KVM can't decrypt guest memory to decode the faulting instruction.\ndiff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c\nindex 136069dfcd44f..4b95a05c37f60 100644\n--- a/arch/x86/kvm/svm/svm.c\n+++ b/arch/x86/kvm/svm/svm.c\n@@ -192,6 +192,8 @@ DEFINE_PER_CPU(struct svm_cpu_data, svm_data);\n \n static DEFINE_MUTEX(vmcb_dump_mutex);\n \n+kvm_tlb_tag_t fallback_asid;\n+\n /*\n * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via\n * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.\n@@ -583,15 +585,6 @@ static int svm_enable_virtualization_cpu(void)\n \t\treturn r;\n \n \tsd = per_cpu_ptr(\u0026svm_data, me);\n-\t/*\n-\t * Bump the current asid_generation value to ensure any vCPU that\n-\t * previously ran on this CPU sees a stale generation and is forced\n-\t * to acquire a new ASID, preventing a latent ASID collision.\n-\t */\n-\tsd-\u003easid_generation++;\n-\tsd-\u003emax_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;\n-\tsd-\u003enext_asid = sd-\u003emax_asid + 1;\n-\tsd-\u003emin_asid = max_sev_asid + 1;\n \n \twrmsrq(MSR_VM_HSAVE_PA, sd-\u003esave_area_pa);\n \n@@ -994,6 +987,8 @@ static void svm_hardware_unsetup(void)\n \n \t__free_pages(__sme_pa_to_page(iopm_base), get_order(IOPM_SIZE));\n \tiopm_base = 0;\n+\n+\tkvm_destroy_tlb_tags();\n }\n \n static void init_seg(struct vmcb_seg *seg)\n@@ -1246,8 +1241,8 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)\n \tif (gmet_enabled)\n \t\tcontrol-\u003emisc_ctl |= SVM_MISC_ENABLE_GMET;\n \n-\tsvm-\u003ecurrent_vmcb-\u003easid_generation = 0;\n-\tsvm-\u003easid = 0;\n+\tcontrol-\u003easid = svm-\u003easid;\n+\tvmcb_set_flush_asid(vmcb);\n \n \tsvm-\u003enested.vmcb12_gpa = INVALID_GPA;\n \tsvm-\u003enested.last_vmcb12_gpa = INVALID_GPA;\n@@ -1375,6 +1370,12 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)\n \t\tgoto error_free_avic;\n \t}\n \n+\tsvm-\u003easid = allocate_asid(vcpu);\n+\tif (!svm-\u003easid) {\n+\t\terr = -EINVAL;\n+\t\tgoto error_free_msrpm;\n+\t}\n+\n \tsvm-\u003ex2avic_msrs_intercepted = true;\n \tsvm-\u003elbr_msrs_intercepted = true;\n \n@@ -1386,6 +1387,8 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)\n \n \treturn 0;\n \n+error_free_msrpm:\n+\tsvm_vcpu_free_msrpm(svm-\u003emsrpm);\n error_free_avic:\n \tavic_vcpu_free(vcpu);\n error_free_sev:\n@@ -1402,6 +1405,8 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)\n static void svm_vcpu_free(struct kvm_vcpu *vcpu)\n {\n \tstruct vcpu_svm *svm = to_svm(vcpu);\n+\tstruct svm_cpu_data *sd;\n+\tint cpu;\n \n \tWARN_ON_ONCE(!list_empty(\u0026svm-\u003eir_list));\n \n@@ -1417,6 +1422,13 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)\n \n \t__free_page(__sme_pa_to_page(svm-\u003evmcb01.pa));\n \tsvm_vcpu_free_msrpm(svm-\u003emsrpm);\n+\n+\tfree_asid(svm-\u003easid);\n+\n+\tfor_each_possible_cpu(cpu) {\n+\t\tsd = per_cpu_ptr(\u0026svm_data, cpu);\n+\t\tcmpxchg(\u0026sd-\u003efallback_asid_vcpu, vcpu, NULL);\n+\t}\n }\n \n #ifdef CONFIG_CPU_MITIGATIONS\n@@ -1942,18 +1954,6 @@ static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)\n \t}\n }\n \n-static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)\n-{\n-\tif (sd-\u003enext_asid \u003e sd-\u003emax_asid) {\n-\t\t++sd-\u003easid_generation;\n-\t\tsd-\u003enext_asid = sd-\u003emin_asid;\n-\t\tsd-\u003eflush_all_asids = true;\n-\t}\n-\n-\tsvm-\u003ecurrent_vmcb-\u003easid_generation = sd-\u003easid_generation;\n-\tsvm-\u003easid = sd-\u003enext_asid++;\n-}\n-\n static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)\n {\n \tstruct vmcb *vmcb = to_svm(vcpu)-\u003evmcb;\n@@ -2468,17 +2468,51 @@ static int clgi_interception(struct kvm_vcpu *vcpu)\n \n static int invlpga_interception(struct kvm_vcpu *vcpu)\n {\n+\tstruct vcpu_svm *svm = to_svm(vcpu);\n \t/* FIXME: Handle an address size prefix. */\n \tgva_t gva = kvm_rax_read(vcpu);\n \tu32 asid = kvm_ecx_read(vcpu);\n+\tint cpu;\n \n \tif (nested_svm_check_permissions(vcpu))\n \t\treturn 1;\n \n \ttrace_kvm_invlpga(to_svm(vcpu)-\u003evmcb-\u003esave.rip, asid, gva);\n \n-\t/* Let's treat INVLPGA the same as INVLPG (can be optimized!) */\n-\tkvm_mmu_invlpg(vcpu, gva);\n+\t/*\n+\t * INVLPG on a non-canonical address is a NOP according to the SDM,\n+\t * assume the same behavior from INVLPGA since the APM doesn't specify.\n+\t */\n+\tif (is_noncanonical_invlpg_address(gva, vcpu))\n+\t\treturn kvm_skip_emulated_instruction(vcpu);\n+\n+\t/*\n+\t * Do nothing if L1 is flushing a different L2 ASID than the one KVM is\n+\t * currently tracking. KVM tracks a single L2 ASID, and performs a TLB\n+\t * flush (and MMU resync if needed) when L1 switches ASIDs anyway.\n+\t */\n+\tif (asid \u0026\u0026 asid != svm-\u003enested.last_asid)\n+\t\treturn kvm_skip_emulated_instruction(vcpu);\n+\n+\t/*\n+\t * Handle INVLPGA similar to INVLPG, with one caveat. If the specified\n+\t * ASID is non-zero (i.e. L1 is not flushing its own ASID), skip\n+\t * flushing the TLB for the current context (L1's), and use INVLPGA to\n+\t * flush L2's ASID in hardware if running on the same CPU (otherwise\n+\t * fallback to a full ASID flush).\n+\t *\n+\t * Note, if NPT is disabled, this will sync all the shadow page tables.\n+\t * This can be optimized by keying off guest_mode.\n+\t */\n+\t__kvm_mmu_invlpg(vcpu, gva, !asid);\n+\tif (asid) {\n+\t\tcpu = get_cpu();\n+\t\tif (cpu == svm-\u003enested.vmcb02.cpu)\n+\t\t\tinvlpga(gva, svm-\u003enested.asid02);\n+\t\telse\n+\t\t\tvmcb_set_flush_asid(svm-\u003enested.vmcb02.ptr);\n+\t\tput_cpu();\n+\t}\n \n \treturn kvm_skip_emulated_instruction(vcpu);\n }\n@@ -3875,37 +3909,28 @@ static void svm_set_nested_run_soft_int_state(struct kvm_vcpu *vcpu)\n \n static int pre_svm_run(struct kvm_vcpu *vcpu)\n {\n-\tstruct svm_cpu_data *sd = per_cpu_ptr(\u0026svm_data, vcpu-\u003ecpu);\n+\tstruct svm_cpu_data *sd = this_cpu_ptr(\u0026svm_data);\n \tstruct vcpu_svm *svm = to_svm(vcpu);\n \n \t/*\n \t * If the previous VMRUN of the VMCB occurred on a different physical\n-\t * cpu, then mark the VMCB dirty as hardware's clean bits are per pCPU.\n-\t *\n-\t * Reset the ASID generation in both VMCBs. This will lead to assigning\n-\t * a new ASID now, and then again when switching to the other VMCB.\n-\t * However, this is needed as the ASID is shared between the VMCBs, and\n-\t * otherwise it would be possible to use an ASID allocated on one pCPU\n-\t * on another, for example:\n-\t * - vCPU migrates from pCPU A to pCPU B, allocates a new ASID.\n-\t * - vCPU migrates back to pCPU A, and then switches the VMCB.\n-\t * - The new VMCB does not detect a pCPU change and runs on pCPU A with\n-\t * the new ASID allocated on pCPU B, which is potentially used by\n-\t * another vCPU/VM.\n+\t * cpu, then mark the VMCB dirty and flush the ASID, as hardware's clean\n+\t * bits and TLB entries are per pCPU.\n \t */\n \tif (unlikely(svm-\u003ecurrent_vmcb-\u003ecpu != vcpu-\u003ecpu)) {\n \t\tvmcb_mark_all_dirty(svm-\u003evmcb);\n \t\tsvm-\u003ecurrent_vmcb-\u003ecpu = vcpu-\u003ecpu;\n-\t\tsvm-\u003evmcb01.asid_generation = 0;\n-\t\tsvm-\u003enested.vmcb02.asid_generation = 0;\n+\t\tvmcb_set_flush_asid(svm-\u003evmcb);\n }\n \n \tif (is_sev_guest(vcpu))\n \t\treturn pre_sev_run(svm, vcpu-\u003ecpu);\n \n-\t/* FIXME: handle wraparound of asid_generation */\n-\tif (svm-\u003ecurrent_vmcb-\u003easid_generation != sd-\u003easid_generation)\n-\t\tnew_asid(svm, sd);\n+\tif (unlikely(svm-\u003evmcb-\u003econtrol.asid == fallback_asid \u0026\u0026\n+\t\t sd-\u003efallback_asid_vcpu != vcpu)) {\n+\t\tvmcb_set_flush_asid(svm-\u003evmcb);\n+\t\tsd-\u003efallback_asid_vcpu = vcpu;\n+\t}\n \n \treturn 0;\n }\n@@ -4317,7 +4342,7 @@ static void svm_flush_tlb_asid(struct kvm_vcpu *vcpu)\n \t * A TLB flush for the current ASID flushes both \"host\" and \"guest\" TLB\n \t * entries, and thus is a superset of Hyper-V's fine grained flushing.\n \t */\n-\tkvm_hv_vcpu_purge_flush_tlb(vcpu);\n+\tkvm_hv_purge_tlb_flush_fifo(vcpu);\n \n \t/*\n \t * Flush only the current ASID even if the TLB flush was invoked via\n@@ -4326,10 +4351,7 @@ static void svm_flush_tlb_asid(struct kvm_vcpu *vcpu)\n \t * unconditionally does a TLB flush on both nested VM-Enter and nested\n \t * VM-Exit (via kvm_mmu_reset_context()).\n \t */\n-\tif (cpu_feature_enabled(X86_FEATURE_FLUSHBYASID))\n-\t\tsvm-\u003evmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_FLUSH_ASID;\n-\telse\n-\t\tsvm-\u003ecurrent_vmcb-\u003easid_generation--;\n+\tvmcb_set_flush_asid(svm-\u003evmcb);\n }\n \n static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)\n@@ -4350,6 +4372,8 @@ static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)\n \n static void svm_flush_tlb_all(struct kvm_vcpu *vcpu)\n {\n+\tstruct vcpu_svm *svm = to_svm(vcpu);\n+\n \t/*\n \t * When running on Hyper-V with EnlightenedNptTlb enabled, remote TLB\n \t * flushes should be routed to hv_flush_remote_tlbs() without requesting\n@@ -4360,7 +4384,11 @@ static void svm_flush_tlb_all(struct kvm_vcpu *vcpu)\n \tif (WARN_ON_ONCE(svm_hv_is_enlightened_tlb_enabled(vcpu)))\n \t\thv_flush_remote_tlbs(vcpu-\u003ekvm);\n \n-\tsvm_flush_tlb_asid(vcpu);\n+\tkvm_hv_purge_all_tlb_flush_fifos(vcpu);\n+\n+\tvmcb_set_flush_asid(svm-\u003evmcb01.ptr);\n+\tif (svm-\u003enested.vmcb02.ptr)\n+\t\tvmcb_set_flush_asid(svm-\u003enested.vmcb02.ptr);\n }\n \n static void svm_flush_tlb_guest(struct kvm_vcpu *vcpu)\n@@ -4647,13 +4675,6 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)\n \n \tsync_lapic_to_cr8(vcpu);\n \n-\tif (unlikely(svm-\u003easid != svm-\u003evmcb-\u003econtrol.asid)) {\n-\t\tsvm-\u003evmcb-\u003econtrol.asid = svm-\u003easid;\n-\t\tvmcb_mark_dirty(svm-\u003evmcb, VMCB_ASID);\n-\t}\n-\tif (this_cpu_ptr(\u0026svm_data)-\u003eflush_all_asids)\n-\t\tsvm-\u003evmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;\n-\n \tsvm-\u003evmcb-\u003esave.cr2 = vcpu-\u003earch.cr2;\n \n \tif (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS) \u0026\u0026\n@@ -4745,8 +4766,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)\n \t}\n \n \tif (!svm_is_vmrun_failure(svm-\u003evmcb-\u003econtrol.exit_code)) {\n-\t\tthis_cpu_ptr(\u0026svm_data)-\u003eflush_all_asids = false;\n-\t\tsvm-\u003evmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_DO_NOTHING;\n+\t\tvmcb_clr_flush_asid(svm-\u003evmcb);\n \n \t\t/*\n \t\t * Unconditionally mask off the CLEAR_RAP bit, the AND is just\n@@ -5659,9 +5679,8 @@ static __init void svm_set_cpu_caps(void)\n \t\tkvm_cpu_cap_set(X86_FEATURE_VMCBCLEAN);\n \n \t\t/*\n-\t\t * KVM currently flushes TLBs on *every* nested SVM transition,\n-\t\t * and so for all intents and purposes KVM supports flushing by\n-\t\t * ASID, i.e. KVM is guaranteed to honor every L1 ASID flush.\n+\t\t * KVM handles all TLB_CONTROL values set by L1, even if the\n+\t\t * underlying CPU does not.\n \t\t */\n \t\tkvm_cpu_cap_set(X86_FEATURE_FLUSHBYASID);\n \n@@ -5757,6 +5776,7 @@ static __init void svm_set_cpu_caps(void)\n \n static __init int svm_hardware_setup(void)\n {\n+\tunsigned long nr_asids;\n \tvoid *iopm_va;\n \tint cpu, r;\n \n@@ -5914,6 +5934,14 @@ static __init int svm_hardware_setup(void)\n \n \tkvm_caps.inapplicable_quirks \u0026= ~KVM_X86_QUIRK_CD_NW_CLEARED;\n \n+\t/* Consumes max_sev_asid initialized by sev_hardware_setup() */\n+\tnr_asids = cpuid_ebx(SVM_CPUID_FUNC);\n+\tr = kvm_init_tlb_tags(nr_asids, max_sev_asid + 1);\n+\tif (r)\n+\t\tgoto err;\n+\n+\tfallback_asid = kvm_alloc_tlb_tag();\n+\n \tfor_each_possible_cpu(cpu) {\n \t\tr = svm_cpu_init(cpu);\n \t\tif (r)\ndiff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h\nindex 6d0242966dc87..15c4aa3b02c2d 100644\n--- a/arch/x86/kvm/svm/svm.h\n+++ b/arch/x86/kvm/svm/svm.h\n@@ -26,6 +26,7 @@\n #include \"regs.h\"\n #include \"x86.h\"\n #include \"pmu.h\"\n+#include \"mmu.h\"\n \n /*\n * Helpers to convert to/from physical addresses for pages whose address is\n@@ -145,7 +146,6 @@ struct kvm_vmcb_info {\n \tstruct vmcb *ptr;\n \tunsigned long pa;\n \tint cpu;\n-\tuint64_t asid_generation;\n };\n \n struct vmcb_save_area_cached {\n@@ -234,7 +234,7 @@ struct svm_nested_state {\n \t */\n \tstruct vmcb_save_area_cached save;\n \n-\tbool initialized;\n+\tkvm_tlb_tag_t asid02;\n \n \t/*\n \t * Indicates whether MSR bitmap for L2 needs to be rebuilt due to\n@@ -244,6 +244,8 @@ struct svm_nested_state {\n \t * on its side.\n \t */\n \tbool force_msr_bitmap_recalc;\n+\n+\tu32 last_asid;\n };\n \n struct vcpu_sev_es_state {\n@@ -285,7 +287,7 @@ struct vcpu_svm {\n \tstruct vmcb *vmcb;\n \tstruct kvm_vmcb_info vmcb01;\n \tstruct kvm_vmcb_info *current_vmcb;\n-\tu32 asid;\n+\tkvm_tlb_tag_t asid;\n \tu32 sysenter_esp_hi;\n \tu32 sysenter_eip_hi;\n \tuint64_t tsc_aux;\n@@ -372,17 +374,14 @@ struct vcpu_svm {\n };\n \n struct svm_cpu_data {\n-\tu64 asid_generation;\n-\tu32 max_asid;\n-\tu32 next_asid;\n-\tu32 min_asid;\n-\n-\tbool flush_all_asids;\n \tbool bp_spec_reduce_set;\n \n \tstruct vmcb *save_area;\n \tunsigned long save_area_pa;\n \n+\t/* Last vCPU to use fallback_asid on this CPU */\n+\tstruct kvm_vcpu *fallback_asid_vcpu;\n+\n \t/* index = sev_asid, value = vmcb pointer */\n \tstruct vmcb **sev_vmcbs;\n };\n@@ -432,6 +431,11 @@ static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)\n {\n \treturn ____sev_snp_guest(vcpu-\u003ekvm);\n }\n+\n+static __always_inline unsigned int sev_get_asid(struct kvm *kvm)\n+{\n+\treturn to_kvm_sev_info(kvm)-\u003easid;\n+}\n #else\n static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu)\n {\n@@ -446,6 +450,11 @@ static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)\n {\n \treturn false;\n }\n+\n+static __always_inline unsigned int sev_get_asid(struct kvm *kvm)\n+{\n+\treturn 0;\n+}\n #endif\n \n static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val)\n@@ -480,6 +489,19 @@ static inline void vmcb_set_gpat(struct vmcb *vmcb, u64 data)\n \tvmcb_mark_dirty(vmcb, VMCB_NPT);\n }\n \n+static inline void vmcb_set_flush_asid(struct vmcb *vmcb)\n+{\n+\tif (cpu_feature_enabled(X86_FEATURE_FLUSHBYASID))\n+\t\tvmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_FLUSH_ASID;\n+\telse\n+\t\tvmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;\n+}\n+\n+static inline void vmcb_clr_flush_asid(struct vmcb *vmcb)\n+{\n+\tvmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_DO_NOTHING;\n+}\n+\n static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)\n {\n \treturn container_of(vcpu, struct vcpu_svm, vcpu);\n@@ -1057,6 +1079,21 @@ static inline struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu)\n static inline void sev_free_decrypted_vmsa(struct kvm_vcpu *vcpu, struct vmcb_save_area *vmsa) {}\n #endif\n \n+extern kvm_tlb_tag_t fallback_asid;\n+\n+static inline kvm_tlb_tag_t allocate_asid(struct kvm_vcpu *vcpu)\n+{\n+\tif (is_sev_guest(vcpu))\n+\t\treturn sev_get_asid(vcpu-\u003ekvm);\n+\treturn kvm_alloc_tlb_tag() ?: fallback_asid;\n+}\n+\n+static inline void free_asid(kvm_tlb_tag_t asid)\n+{\n+\tif (asid \u003e max_sev_asid \u0026\u0026 asid != fallback_asid)\n+\t\tkvm_free_tlb_tag(asid);\n+}\n+\n /* vmenter.S */\n \n void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, unsigned int flags,\ndiff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c\nindex b341af4c6057a..a1caa2bd04d7a 100644\n--- a/arch/x86/kvm/vmx/vmx.c\n+++ b/arch/x86/kvm/vmx/vmx.c\n@@ -598,9 +598,6 @@ DEFINE_PER_CPU(struct vmcs *, current_vmcs);\n */\n static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);\n \n-static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);\n-static DEFINE_SPINLOCK(vmx_vpid_lock);\n-\n struct vmcs_config vmcs_config __ro_after_init;\n struct vmx_capability vmx_capability __ro_after_init;\n \n@@ -4092,31 +4089,6 @@ static void seg_setup(int seg)\n \tvmcs_write32(sf-\u003ear_bytes, ar);\n }\n \n-int allocate_vpid(void)\n-{\n-\tint vpid;\n-\n-\tif (!enable_vpid)\n-\t\treturn 0;\n-\tspin_lock(\u0026vmx_vpid_lock);\n-\tvpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);\n-\tif (vpid \u003c VMX_NR_VPIDS)\n-\t\t__set_bit(vpid, vmx_vpid_bitmap);\n-\telse\n-\t\tvpid = 0;\n-\tspin_unlock(\u0026vmx_vpid_lock);\n-\treturn vpid;\n-}\n-\n-void free_vpid(int vpid)\n-{\n-\tif (!enable_vpid || vpid == 0)\n-\t\treturn;\n-\tspin_lock(\u0026vmx_vpid_lock);\n-\t__clear_bit(vpid, vmx_vpid_bitmap);\n-\tspin_unlock(\u0026vmx_vpid_lock);\n-}\n-\n static void vmx_msr_bitmap_l01_changed(struct vcpu_vmx *vmx)\n {\n \t/*\n@@ -8415,6 +8387,8 @@ void vmx_hardware_unsetup(void)\n \n \tif (nested)\n \t\tnested_vmx_hardware_unsetup();\n+\n+\tdestroy_vpids();\n }\n \n void vmx_vm_destroy(struct kvm *kvm)\n@@ -8640,8 +8614,6 @@ __init int vmx_hardware_setup(void)\n \tkvm_caps.has_bus_lock_exit = cpu_has_vmx_bus_lock_detection();\n \tkvm_caps.has_notify_vmexit = cpu_has_notify_vmexit();\n \n-\tset_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */\n-\n \tif (enable_ept)\n \t\tkvm_mmu_set_ept_masks(enable_ept_ad_bits);\n \telse\n@@ -8681,6 +8653,10 @@ __init int vmx_hardware_setup(void)\n \n \tvmx_set_cpu_caps();\n \n+\tr = init_vpids();\n+\tif (r)\n+\t\treturn r;\n+\n \t/*\n \t * Configure nested capabilities after core CPU capabilities so that\n \t * nested support can be conditional on base support, e.g. so that KVM\n@@ -8688,8 +8664,10 @@ __init int vmx_hardware_setup(void)\n \t */\n \tif (nested) {\n \t\tr = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);\n-\t\tif (r)\n+\t\tif (r) {\n+\t\t\tdestroy_vpids();\n \t\t\treturn r;\n+\t\t}\n \t}\n \tvmx_nested_ops.enabled = nested;\n \ndiff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h\nindex 6c9dd5b2365d5..3e1ecd9148e9c 100644\n--- a/arch/x86/kvm/vmx/vmx.h\n+++ b/arch/x86/kvm/vmx/vmx.h\n@@ -182,7 +182,7 @@ struct nested_vmx {\n \tu64 pre_vmenter_ssp;\n \tu64 pre_vmenter_ssp_tbl;\n \n-\tu16 vpid02;\n+\tkvm_tlb_tag_t vpid02;\n \tu16 last_vpid;\n \n \tint tsc_autostore_slot;\n@@ -256,7 +256,7 @@ struct vcpu_vmx {\n \t\t\tu32 ar;\n \t\t} seg[8];\n \t} segment_cache;\n-\tint vpid;\n+\tkvm_tlb_tag_t vpid;\n \n \t/* Support for a guest hypervisor (nested VMX) */\n \tstruct nested_vmx nested;\n@@ -297,9 +297,29 @@ struct kvm_vmx {\n \tu64 *pid_table;\n };\n \n+static __always_inline int init_vpids(void)\n+{\n+\treturn enable_vpid ? kvm_init_tlb_tags(VMX_NR_VPIDS, 1) : 0;\n+}\n+\n+static __always_inline void destroy_vpids(void)\n+{\n+\tif (enable_vpid)\n+\t\tkvm_destroy_tlb_tags();\n+}\n+\n+static __always_inline kvm_tlb_tag_t allocate_vpid(void)\n+{\n+\treturn enable_vpid ? kvm_alloc_tlb_tag() : 0;\n+}\n+\n+static __always_inline void free_vpid(kvm_tlb_tag_t vpid)\n+{\n+\tif (enable_vpid)\n+\t\tkvm_free_tlb_tag(vpid);\n+}\n+\n void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu);\n-int allocate_vpid(void);\n-void free_vpid(int vpid);\n void vmx_set_constant_host_state(struct vcpu_vmx *vmx);\n void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);\n void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,\ndiff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c\nindex 32563a91fba99..a54e9da2ed09e 100644\n--- a/arch/x86/kvm/x86.c\n+++ b/arch/x86/kvm/x86.c\n@@ -2020,8 +2020,7 @@ static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)\n \t\t * a forced sync of the shadow page tables. Ensure all the\n \t\t * roots are synced and the guest TLB in hardware is clean.\n \t\t */\n-\t\tkvm_mmu_sync_roots(vcpu);\n-\t\tkvm_mmu_sync_prev_roots(vcpu);\n+\t\tkvm_mmu_sync_all_roots(vcpu);\n \t}\n \n \tkvm_x86_call(flush_tlb_guest)(vcpu);\n@@ -2030,7 +2029,7 @@ static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)\n \t * Flushing all \"guest\" TLB is always a superset of Hyper-V's fine\n \t * grained flushing.\n \t */\n-\tkvm_hv_vcpu_purge_flush_tlb(vcpu);\n+\tkvm_hv_purge_tlb_flush_fifo(vcpu);\n }\n \n \n@@ -8167,8 +8166,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)\n \t\t\tif (unlikely(r))\n \t\t\t\tgoto out;\n \t\t}\n-\t\tif (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))\n+\n+\t\tif (kvm_check_request(KVM_REQ_MMU_SYNC_ALL_ROOTS, vcpu)) {\n+\t\t\tkvm_mmu_sync_all_roots(vcpu);\n+\t\t\tkvm_clear_request(KVM_REQ_MMU_SYNC, vcpu);\n+\t\t} else if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) {\n \t\t\tkvm_mmu_sync_roots(vcpu);\n+\t\t}\n+\n \t\tif (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))\n \t\t\tkvm_mmu_load_pgd(vcpu);\n \ndiff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm\nindex be5eb2dd85aa2..5347c56786b08 100644\n--- a/tools/testing/selftests/kvm/Makefile.kvm\n+++ b/tools/testing/selftests/kvm/Makefile.kvm\n@@ -122,6 +122,7 @@ TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test\n TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test\n TEST_GEN_PROGS_x86 += x86/svm_nested_vmcb12_gpa\n TEST_GEN_PROGS_x86 += x86/svm_nested_pat_test\n+TEST_GEN_PROGS_x86 += x86/nested_tlb_flush_test\n TEST_GEN_PROGS_x86 += x86/svm_lbr_nested_state\n TEST_GEN_PROGS_x86 += x86/svm_pmu_host_guest_test\n TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync\ndiff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h\nindex ffec2291b1e1f..62d255aee5a34 100644\n--- a/tools/testing/selftests/kvm/include/x86/processor.h\n+++ b/tools/testing/selftests/kvm/include/x86/processor.h\n@@ -1617,6 +1617,7 @@ void tdp_map(struct kvm_vm *vm, gpa_t l2_gpa, gpa_t gpa, u64 size);\n void tdp_identity_map_default_memslots(struct kvm_vm *vm);\n void tdp_identity_map_1g(struct kvm_vm *vm, u64 addr, u64 size);\n u64 *tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa);\n+gpa_t tdp_copy_page_tables(struct kvm_vm *vm);\n \n /*\n * Basic CPU control in CR0\ndiff --git a/tools/testing/selftests/kvm/include/x86/svm.h b/tools/testing/selftests/kvm/include/x86/svm.h\nindex c8539166270ea..7644e37c7042c 100644\n--- a/tools/testing/selftests/kvm/include/x86/svm.h\n+++ b/tools/testing/selftests/kvm/include/x86/svm.h\n@@ -316,4 +316,9 @@ struct __attribute__ ((__packed__)) vmcb {\n \n #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)\n \n+static inline void invlpga(unsigned long addr, u32 asid)\n+{\n+\tasm volatile(\"invlpga %0, %1\" : : \"a\"(addr), \"c\"(asid));\n+}\n+\n #endif /* SELFTEST_KVM_SVM_H */\ndiff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h\nindex 8fd97434eee5c..4f2a0e3ac5ba1 100644\n--- a/tools/testing/selftests/kvm/include/x86/vmx.h\n+++ b/tools/testing/selftests/kvm/include/x86/vmx.h\n@@ -518,4 +518,25 @@ static inline bool kvm_cpu_has_vmx_virtualize_apic_accesses(void)\n void vm_enable_ept(struct kvm_vm *vm);\n void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm);\n \n+static inline void invvpid(unsigned long ext, u16 vpid, gva_t gva)\n+{\n+\tstruct {\n+\t\tu64 vpid : 16;\n+\t\tu64 rsvd : 48;\n+\t\tu64 gva;\n+\t} operand = { vpid, 0, gva };\n+\n+\tasm volatile(\"invvpid %0, %1\" : : \"m\"(operand), \"r\"(ext) : \"memory\");\n+}\n+\n+static inline void invept(unsigned long ext, u64 eptp)\n+{\n+\tstruct {\n+\t\tu64 eptp;\n+\t\tu64 rsvd;\n+\t} operand = { eptp, 0 };\n+\n+\tasm volatile(\"invept %0, %1\" : : \"m\"(operand), \"r\"(ext) : \"memory\");\n+}\n+\n #endif /* SELFTEST_KVM_VMX_H */\ndiff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c\nindex 8374afd08bafe..57ecf8b381b5a 100644\n--- a/tools/testing/selftests/kvm/lib/x86/processor.c\n+++ b/tools/testing/selftests/kvm/lib/x86/processor.c\n@@ -554,6 +554,42 @@ void tdp_identity_map_1g(struct kvm_vm *vm, u64 addr, u64 size)\n \t__tdp_map(vm, addr, addr, size, PG_LEVEL_1G);\n }\n \n+static gpa_t __virt_copy_page_tables(struct kvm_vm *vm, struct kvm_mmu *mmu,\n+\t\t\t\t gpa_t table_gpa, int level)\n+{\n+\tgpa_t copy_gpa = vm_alloc_page_table(vm);\n+\tu64 *copy = addr_gpa2hva(vm, copy_gpa);\n+\tgpa_t pa;\n+\tint i;\n+\n+\tmemcpy(copy, addr_gpa2hva(vm, table_gpa), vm-\u003epage_size);\n+\tif (level == PG_LEVEL_4K)\n+\t\treturn copy_gpa;\n+\n+\tfor (i = 0; i \u003c vm-\u003epage_size / sizeof(u64); i++) {\n+\t\tif (!is_present_pte(mmu, \u0026copy[i]) || is_huge_pte(mmu, \u0026copy[i]))\n+\t\t\tcontinue;\n+\n+\t\t/* Clear only the address bits, preserving C/S bits and flags */\n+\t\tpa = vm_untag_gpa(vm, PTE_GET_PA(copy[i]));\n+\t\tcopy[i] \u0026= ~pa;\n+\t\tcopy[i] |= __virt_copy_page_tables(vm, mmu, pa, level - 1);\n+\t}\n+\treturn copy_gpa;\n+}\n+\n+/*\n+ * Deep copy all levels of the TDP page tables, such that no page table pages\n+ * are shared with the original. Returns the GPA of the new root.\n+ */\n+gpa_t tdp_copy_page_tables(struct kvm_vm *vm)\n+{\n+\tstruct kvm_mmu *mmu = \u0026vm-\u003estage2_mmu;\n+\n+\tTEST_ASSERT(mmu-\u003epgd_created, \"TDP page tables not created\");\n+\treturn __virt_copy_page_tables(vm, mmu, mmu-\u003epgd, mmu-\u003epgtable_levels);\n+}\n+\n /*\n * Set Unusable Segment\n *\ndiff --git a/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c b/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c\nnew file mode 100644\nindex 0000000000000..fb32b7bf6b19f\n--- /dev/null\n+++ b/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c\n@@ -0,0 +1,526 @@\n+// SPDX-License-Identifier: GPL-2.0-only\n+/*\n+ * Copyright (C) 2026, Google LLC.\n+ *\n+ * Test TLB flushes with nested virtualization across three cases:\n+ * 1. When L1 updates L2's (shadow) page tables\n+ * 2. When L1 updates nested TDP\n+ * 3. When KVM updates mappings on the host (KVM-induced TLB flushes)\n+ */\n+#include \u003cfcntl.h\u003e\n+#include \u003cstdio.h\u003e\n+#include \u003cstdlib.h\u003e\n+#include \u003cstring.h\u003e\n+\n+#include \"test_util.h\"\n+#include \"kvm_util.h\"\n+#include \"processor.h\"\n+#include \"svm_util.h\"\n+#include \"vmx.h\"\n+\n+#define NR_ITERATIONS\t100\n+\n+#define VAL1\t\t0x11111111ULL\n+#define VAL2\t\t0x22222222ULL\n+\n+#define TEST_VADDR\t0x10000000ULL\n+#define TEST_GPA\t0x30000000ULL\n+#define TEST_NESTED_GPA\t0x40000000ULL\n+\n+#define TEST_MEMSLOT\t10\n+\n+#define SYNC_MOVE_MEMSLOT 1\n+\n+enum guest_flush_method {\n+\tTLB_FLUSH_TAG = 0,\n+\tTLB_FLUSH_VADDR,\n+\tTLB_CHANGE_TAG,\n+\tTLB_FLUSH_NONE,\n+};\n+\n+static u64 *pte_gva;\n+static gpa_t test_gpa[2];\n+static enum guest_flush_method flush_method;\n+static u32 max_asid;\n+\n+/* TDP roots for the multi-root test, root 1 is a non-aliasing copy of root 0 */\n+static gpa_t tdp_root_gpa[2];\n+/* Value L2 expects at TEST_VADDR, 0 if L2 shouldn't access TEST_VADDR */\n+static u64 l2_expected_val;\n+\n+static void svm_tlb_flush(struct svm_test_data *svm,\n+\t\t\t enum guest_flush_method method)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tswitch (method) {\n+\tcase TLB_CHANGE_TAG:\n+\t\tvmcb-\u003econtrol.asid++;\n+\t\tvmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_DO_NOTHING;\n+\t\tif (vmcb-\u003econtrol.asid \u003e= max_asid) {\n+\t\t\tvmcb-\u003econtrol.asid = 1;\n+\t\t\tvmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;\n+\t\t}\n+\t\tbreak;\n+\tcase TLB_FLUSH_TAG:\n+\t\tvmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_FLUSH_ASID;\n+\t\tbreak;\n+\tcase TLB_FLUSH_VADDR:\n+\t\tGUEST_ASSERT(!svm-\u003encr3_gpa);\n+\t\tvmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_DO_NOTHING;\n+\t\tinvlpga(TEST_VADDR, vmcb-\u003econtrol.asid);\n+\t\tbreak;\n+\tcase TLB_FLUSH_NONE:\n+\t\tvmcb-\u003econtrol.tlb_ctl = TLB_CONTROL_DO_NOTHING;\n+\t\tbreak;\n+\t}\n+}\n+\n+static void vmx_tlb_flush(struct vmx_pages *vmx, enum guest_flush_method method)\n+{\n+\tu16 vpid = vmread(VIRTUAL_PROCESSOR_ID);\n+\n+\tswitch (method) {\n+\tcase TLB_CHANGE_TAG:\n+\t\tGUEST_ASSERT(!vmx-\u003eeptp_gpa);\n+\t\tvmwrite(VIRTUAL_PROCESSOR_ID, vpid + 1);\n+\t\tbreak;\n+\tcase TLB_FLUSH_TAG:\n+\t\tif (vmx-\u003eeptp_gpa)\n+\t\t\tinvept(1, vmread(EPT_POINTER));\n+\t\telse\n+\t\t\tinvvpid(1, vpid, 0);\n+\t\tbreak;\n+\tcase TLB_FLUSH_VADDR:\n+\t\tGUEST_ASSERT(!vmx-\u003eeptp_gpa);\n+\t\tinvvpid(0, vpid, TEST_VADDR);\n+\t\tbreak;\n+\tcase TLB_FLUSH_NONE:\n+\t\tbreak;\n+\t}\n+}\n+\n+static void prepare_l2(void *nested_state, void *l2_guest_code)\n+{\n+\tstruct vmx_pages *vmx = nested_state;\n+\n+\tif (this_cpu_has(X86_FEATURE_VMX)) {\n+\t\tu32 sec_exec_ctl;\n+\n+\t\tprepare_for_vmx_operation(vmx);\n+\t\tload_vmcs(vmx);\n+\t\tprepare_vmcs(vmx, l2_guest_code);\n+\n+\t\t/* Enable VPID */\n+\t\tsec_exec_ctl = vmread(SECONDARY_VM_EXEC_CONTROL);\n+\t\tsec_exec_ctl |= SECONDARY_EXEC_ENABLE_VPID;\n+\t\tvmwrite(SECONDARY_VM_EXEC_CONTROL, sec_exec_ctl);\n+\t\tvmwrite(VIRTUAL_PROCESSOR_ID, 1);\n+\t} else {\n+\t\tgeneric_svm_setup(nested_state, l2_guest_code);\n+\t}\n+}\n+\n+static void __run_l2(void *nested_state, bool launch,\n+\t\t enum guest_flush_method method)\n+{\n+\tstruct svm_test_data *svm;\n+\n+\tif (this_cpu_has(X86_FEATURE_VMX)) {\n+\t\tvmx_tlb_flush(nested_state, method);\n+\t\tif (launch)\n+\t\t\tvmlaunch();\n+\t\telse\n+\t\t\tvmresume();\n+\t\tGUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_VMCALL);\n+\t\tvmwrite(GUEST_RIP, vmread(GUEST_RIP) + 3); /* skip over VMCALL */\n+\t} else {\n+\t\tsvm = nested_state;\n+\t\tsvm_tlb_flush(svm, method);\n+\t\trun_guest(svm-\u003evmcb, svm-\u003evmcb_gpa);\n+\t\tGUEST_ASSERT_EQ(svm-\u003evmcb-\u003econtrol.exit_code, SVM_EXIT_VMMCALL);\n+\t\tsvm-\u003evmcb-\u003esave.rip += 3; /* skip over VMMCALL */\n+\t}\n+}\n+\n+static void run_l2(void *nested_state, bool launch)\n+{\n+\t__run_l2(nested_state, launch, flush_method);\n+}\n+\n+static void l2_guest_code(void)\n+{\n+\tu64 expected_val;\n+\n+\tfor (;;) {\n+\t\texpected_val = READ_ONCE(l2_expected_val);\n+\t\tif (expected_val)\n+\t\t\tGUEST_ASSERT_EQ(READ_ONCE(*(u64 *)TEST_VADDR), expected_val);\n+\t\tvmcall();\n+\t}\n+}\n+\n+static void l1_guest_code(void *data)\n+{\n+\tgpa_t gpa;\n+\tint i;\n+\n+\tprepare_l2(data, l2_guest_code);\n+\n+\t/*\n+\t * Alternately switch the PTE (or TDP PTE) mapping TEST_VADDR between\n+\t * two pages containing VAL1 and VAL2, flush TLBs, and verify that L2\n+\t * reads the expected value.\n+\t */\n+\tfor (i = 0; i \u003c NR_ITERATIONS; i++) {\n+\t\tgpa = test_gpa[i % 2];\n+\n+\t\t*pte_gva \u0026= ~PHYSICAL_PAGE_MASK;\n+\t\t*pte_gva |= gpa \u0026 PHYSICAL_PAGE_MASK;\n+\n+\t\tWRITE_ONCE(l2_expected_val, (i % 2 == 0) ? VAL1 : VAL2);\n+\t\trun_l2(data, i == 0);\n+\t}\n+\n+\tGUEST_DONE();\n+}\n+\n+/*\n+ * Verify that flushing L2's ASID invalidates nested TDP translations for all\n+ * TDP roots, not just the one in use at the time of the flush. L1 updates the\n+ * nested TDP PTE for TEST_VADDR in root 0, flushes L2's ASID while running L2\n+ * with root 1, then runs L2 with root 0 *without* flushing. L2 must observe\n+ * the new mapping, as the flush invalidated all translations for the ASID.\n+ *\n+ * Root 1 is a full copy of root 0, such that none of the nested TDP pages are\n+ * shared, i.e. KVM's shadow pages for root 0 are not reachable through the\n+ * shadow root for root 1.\n+ */\n+static void l1_guest_code_multi_tdp_root(void *data)\n+{\n+\tstruct svm_test_data *svm = data;\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\tint i;\n+\n+\tGUEST_ASSERT(this_cpu_has(X86_FEATURE_SVM));\n+\tGUEST_ASSERT_EQ(svm-\u003encr3_gpa, tdp_root_gpa[0]);\n+\n+\tprepare_l2(data, l2_guest_code);\n+\n+\tfor (i = 0; i \u003c NR_ITERATIONS; i++) {\n+\t\t*pte_gva \u0026= ~PHYSICAL_PAGE_MASK;\n+\t\t*pte_gva |= test_gpa[i % 2] \u0026 PHYSICAL_PAGE_MASK;\n+\n+\t\t/* Flush L2's ASID (per flush_method) while using root 1 */\n+\t\tvmcb-\u003econtrol.nested_cr3 = tdp_root_gpa[1];\n+\t\tWRITE_ONCE(l2_expected_val, 0);\n+\t\trun_l2(data, i == 0);\n+\n+\t\t/* Switch back to root 0 without flushing */\n+\t\tvmcb-\u003econtrol.nested_cr3 = tdp_root_gpa[0];\n+\t\tWRITE_ONCE(l2_expected_val, (i % 2 == 0) ? VAL1 : VAL2);\n+\t\t__run_l2(data, false, TLB_FLUSH_NONE);\n+\t}\n+\n+\tGUEST_DONE();\n+}\n+\n+static void prep_guest_flush_test(struct kvm_vm *vm, bool tdp_enabled,\n+\t\t\t\t bool copy_tdp_root)\n+{\n+\tgva_t page_gva[2], pte_map_gva;\n+\tgpa_t pte_gpa;\n+\tu64 *ptep;\n+\n+\t/*\n+\t * Allocate two pages in the VM and initialize them with different\n+\t * values. L1 will update the mappings to switch between these pages and\n+\t * L2 will verify that the accessed value indicates the right page.\n+\t */\n+\tpage_gva[0] = vm_alloc_page(vm);\n+\tpage_gva[1] = vm_alloc_page(vm);\n+\n+\t*(u64 *)addr_gva2hva(vm, page_gva[0]) = VAL1;\n+\t*(u64 *)addr_gva2hva(vm, page_gva[1]) = VAL2;\n+\n+\ttest_gpa[0] = addr_gva2gpa(vm, page_gva[0]);\n+\ttest_gpa[1] = addr_gva2gpa(vm, page_gva[1]);\n+\n+\tif (tdp_enabled) {\n+\t\tvirt_pg_map(vm, TEST_VADDR, TEST_NESTED_GPA);\n+\t\ttdp_map(vm, TEST_NESTED_GPA, test_gpa[0], vm-\u003epage_size);\n+\t\tptep = tdp_get_pte(vm, TEST_NESTED_GPA);\n+\t} else {\n+\t\tvirt_pg_map(vm, TEST_VADDR, test_gpa[0]);\n+\t\tptep = vm_get_pte(vm, TEST_VADDR);\n+\t}\n+\n+\t/*\n+\t * Map the PTE (or TDP PTE) for TEST_VADDR, such that L1 can read and\n+\t * update the mapping. Pass the PTE GVA to L1 to directly use it (rather\n+\t * than L1 walking the page tables.\n+\t */\n+\tpte_gpa = addr_hva2gpa(vm, ptep);\n+\tpte_map_gva = vm_unused_gva_gap(vm, vm-\u003epage_size, KVM_UTIL_MIN_VADDR);\n+\tvirt_pg_map(vm, pte_map_gva, pte_gpa \u0026 PAGE_MASK);\n+\tpte_gva = (u64 *)(pte_map_gva + (pte_gpa \u0026 ~PAGE_MASK));\n+\n+\tsync_global_to_guest(vm, pte_gva);\n+\tsync_global_to_guest(vm, test_gpa);\n+\n+\tif (!tdp_enabled)\n+\t\treturn;\n+\n+\t/*\n+\t * Must be done after all pages accessed by L2 are allocated, including\n+\t * nested state (e.g. L2's stack) and page tables for TEST_VADDR.\n+\t */\n+\ttdp_identity_map_default_memslots(vm);\n+\n+\t/* Must be done after all TDP mappings are created */\n+\tif (copy_tdp_root) {\n+\t\ttdp_root_gpa[0] = vm-\u003estage2_mmu.pgd;\n+\t\ttdp_root_gpa[1] = tdp_copy_page_tables(vm);\n+\t\tsync_global_to_guest(vm, tdp_root_gpa);\n+\t}\n+}\n+\n+static void l2_guest_code_memslot_move(void)\n+{\n+\tint i;\n+\tu64 val;\n+\n+\tfor (i = 0; i \u003c NR_ITERATIONS; i++) {\n+\t\tval = READ_ONCE(*(u64 *)TEST_VADDR);\n+\t\tGUEST_ASSERT_EQ(val, VAL1);\n+\n+\t\tvmcall();\n+\n+\t\tval = READ_ONCE(*(u64 *)TEST_VADDR);\n+\t\tGUEST_ASSERT_EQ(val, VAL2);\n+\n+\t\tGUEST_SYNC2(SYNC_MOVE_MEMSLOT, 1);\n+\n+\t\tval = READ_ONCE(*(u64 *)TEST_VADDR);\n+\t\tGUEST_ASSERT_EQ(val, VAL1);\n+\n+\t\tvmcall();\n+\t}\n+}\n+\n+static void l1_guest_code_memslot_move(void *data)\n+{\n+\tint i;\n+\tu64 val;\n+\n+\tprepare_l2(data, l2_guest_code_memslot_move);\n+\n+\tfor (i = 0; i \u003c NR_ITERATIONS; i++) {\n+\t\t/* Verify that accessing VADDR reads from page 1 in both L1 \u0026 L2 */\n+\t\tval = READ_ONCE(*(u64 *)TEST_VADDR);\n+\t\tGUEST_ASSERT_EQ(val, VAL1);\n+\n+\t\trun_l2(data, i == 0);\n+\n+\t\t/*\n+\t\t * Update VADDR to point at page 2. KVM should flush both L1 and\n+\t\t * L2's TLB translations so that further accesses go to page 2.\n+\t\t */\n+\t\tGUEST_SYNC2(SYNC_MOVE_MEMSLOT, 2);\n+\n+\t\t/*\n+\t\t * Verify that accessing VADDR reads from page 2. A stale TLB\n+\t\t * entry would lead to reading from page 1. Enter L2 and check\n+\t\t * access to page 2, to verify that a TLB flush from L1's\n+\t\t * context flushes both L1 and L2.\n+\t\t */\n+\t\tval = READ_ONCE(*(u64 *)TEST_VADDR);\n+\t\tGUEST_ASSERT_EQ(val, VAL2);\n+\n+\t\trun_l2(data, false);\n+\n+\t\t/*\n+\t\t * L2 updated VADDR to point at page 1 again. Verify access to\n+\t\t * page 1 again to verify that a TLB flush from L2's context\n+\t\t * flushes both L1 and L2 TLB translations.\n+\t\t */\n+\t\tval = READ_ONCE(*(u64 *)TEST_VADDR);\n+\t\tGUEST_ASSERT_EQ(val, VAL1);\n+\t}\n+\n+\tGUEST_DONE();\n+}\n+\n+static void prep_memslot_move_test(struct kvm_vm *vm, bool tdp_enabled)\n+{\n+\t/*\n+\t * Disable KVM_X86_QUIRK_SLOT_ZAP_ALL. By default, moving a memslot\n+\t * invalidates all MMU roots, which implicitly flushes the guest TLB\n+\t * and masks missing TLB flush bugs. Disabling it forces KVM to keep\n+\t * the same roots, relying solely on explicit TLB flushes.\n+\t */\n+\tTEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) \u0026 KVM_X86_QUIRK_SLOT_ZAP_ALL);\n+\tvm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);\n+\n+\tvm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,\n+\t\t\t\t TEST_GPA, TEST_MEMSLOT,\n+\t\t\t\t /*npages=*/2, /*flags=*/0);\n+\n+\tvirt_pg_map(vm, TEST_VADDR, TEST_GPA);\n+\n+\tif (tdp_enabled)\n+\t\ttdp_map(vm, TEST_GPA, TEST_GPA, vm-\u003epage_size);\n+\n+\t*(u64 *)addr_gpa2hva(vm, TEST_GPA) = VAL1;\n+\t*(u64 *)addr_gpa2hva(vm, TEST_GPA + vm-\u003epage_size) = VAL2;\n+\n+\t/*\n+\t * Must be done after all pages accessed by L2 are allocated, including\n+\t * nested state (e.g. L2's stack) and page tables for TEST_VADDR.\n+\t */\n+\tif (tdp_enabled)\n+\t\ttdp_identity_map_default_memslots(vm);\n+}\n+\n+static void calc_max_svm_asid(void)\n+{\n+\tconst struct kvm_cpuid_entry2 *entry;\n+\n+\tif (!kvm_cpu_has(X86_FEATURE_SVM))\n+\t\treturn;\n+\n+\tentry = get_cpuid_entry(kvm_get_supported_cpuid(), 0x8000000A, 0);\n+\tmax_asid = entry ? entry-\u003eebx : 0;\n+}\n+\n+static void run_test(void *guest_code, bool tdp_enabled)\n+{\n+\tstruct kvm_vcpu *vcpu;\n+\tgva_t nested_gva = 0;\n+\tstruct kvm_vm *vm;\n+\tstruct ucall uc;\n+\n+\tvm = vm_create_with_one_vcpu(\u0026vcpu, guest_code);\n+\tif (tdp_enabled)\n+\t\tvm_enable_tdp(vm);\n+\n+\tif (kvm_cpu_has(X86_FEATURE_VMX))\n+\t\tvcpu_alloc_vmx(vm, \u0026nested_gva);\n+\telse\n+\t\tvcpu_alloc_svm(vm, \u0026nested_gva);\n+\n+\tif (guest_code == l1_guest_code_memslot_move)\n+\t\tprep_memslot_move_test(vm, tdp_enabled);\n+\telse if (guest_code == l1_guest_code_multi_tdp_root)\n+\t\tprep_guest_flush_test(vm, tdp_enabled, true);\n+\telse\n+\t\tprep_guest_flush_test(vm, tdp_enabled, false);\n+\n+\tvcpu_args_set(vcpu, 1, nested_gva);\n+\n+\tsync_global_to_guest(vm, flush_method);\n+\tsync_global_to_guest(vm, max_asid);\n+\n+\tfor (;;) {\n+\t\tvcpu_run(vcpu);\n+\t\tTEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);\n+\n+\t\tswitch (get_ucall(vcpu, \u0026uc)) {\n+\t\tcase UCALL_ABORT:\n+\t\t\tREPORT_GUEST_ASSERT(uc);\n+\t\t\tbreak;\n+\t\tcase UCALL_SYNC:\n+\t\t\t/*\n+\t\t\t * Move the base GPA of the memslot between TEST_GPA and\n+\t\t\t * TEST_GPA - PAGE_SIZE. This effectively moves\n+\t\t\t * TEST_VADDR to point at page 1 or page 2 in the\n+\t\t\t * memslot.\n+\t\t\t */\n+\t\t\tif (uc.args[0] == SYNC_MOVE_MEMSLOT) {\n+\t\t\t\tgpa_t new_gpa = (uc.args[1] == 1) ?\n+\t\t\t\t\tTEST_GPA : (TEST_GPA - vm-\u003epage_size);\n+\n+\t\t\t\tvm_mem_region_move(vm, TEST_MEMSLOT, new_gpa);\n+\t\t\t} else {\n+\t\t\t\tTEST_FAIL(\"Unknown sync code: %lu\", uc.args[0]);\n+\t\t\t}\n+\t\t\tbreak;\n+\t\tcase UCALL_DONE:\n+\t\t\tgoto done;\n+\t\tdefault:\n+\t\t\tTEST_FAIL(\"Unknown ucall %lu\", uc.cmd);\n+\t\t}\n+\t}\n+\n+done:\n+\tkvm_vm_free(vm);\n+}\n+\n+#define tlb_test(test_name, guest_code, tdp_setting, flush_setting)\t\t\\\n+do {\t\t\t\t\t\t\t\t\t\t\\\n+\ttdp_setting;\t\t\t\t\t\t\t\t\\\n+\tflush_setting;\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\t\\\n+\tif (tdp_enabled \u0026\u0026 !kvm_cpu_has_tdp()) {\t\t\t\t\\\n+\t\tpr_info(\"Skipping: \" test_name \" (no TDP support)\\n\");\t\t\\\n+\t\tbreak;\t\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\t\\\n+\t/*\t\t\t\t\t\t\t\t\t\\\n+\t * VPIDs do not tag guest-physical translations, i.e. changing or\t\\\n+\t * flushing VPIDs does not flush EPT translations, and single-context\t\\\n+\t * INVEPT only flushes the target EPTP (not all TDP roots).\t\t\\\n+\t */\t\t\t\t\t\t\t\t\t\\\n+\tif (tdp_enabled \u0026\u0026 kvm_cpu_has_ept() \u0026\u0026\t\t\t\t\t\\\n+\t (flush_method == TLB_CHANGE_TAG ||\t\t\t\t\t\\\n+\t guest_code == l1_guest_code_multi_tdp_root)) {\t\t\t\\\n+\t\tpr_info(\"Skipping: \" test_name \" (not applicable to EPTs)\\n\");\t\\\n+\t\tbreak;\t\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\t\\\n+\tpr_info(\"Testing \" test_name \"...\\n\");\t\t\t\t\t\\\n+\trun_test(guest_code, tdp_enabled);\t\t\t\t\t\\\n+} while (0)\n+\n+int main(int argc, char *argv[])\n+{\n+\tbool tdp_enabled;\n+\n+\tTEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX));\n+\n+\tcalc_max_svm_asid();\n+\n+\ttlb_test(\"Guest-triggered TLB flush (flush entire tag), TDP disabled\",\n+\t\t l1_guest_code, tdp_enabled = false,\n+\t\t flush_method = TLB_FLUSH_TAG);\n+\ttlb_test(\"Guest-triggered TLB flush (flush individual address), TDP disabled\",\n+\t\t l1_guest_code, tdp_enabled = false,\n+\t\t flush_method = TLB_FLUSH_VADDR);\n+\ttlb_test(\"Guest-triggered TLB flush (change tag), TDP disabled\",\n+\t\t l1_guest_code, tdp_enabled = false,\n+\t\t flush_method = TLB_CHANGE_TAG);\n+\n+\ttlb_test(\"Guest-triggered TLB flush (flush entire tag), TDP enabled\",\n+\t\t l1_guest_code, tdp_enabled = true,\n+\t\t flush_method = TLB_FLUSH_TAG);\n+\ttlb_test(\"Guest-triggered TLB flush (change tag), TDP enabled\",\n+\t\t l1_guest_code, tdp_enabled = true,\n+\t\t flush_method = TLB_CHANGE_TAG);\n+\n+\ttlb_test(\"Guest-triggered TLB flush with multiple TDP roots (flush entire tag), TDP enabled\",\n+\t\t l1_guest_code_multi_tdp_root, tdp_enabled = true,\n+\t\t flush_method = TLB_FLUSH_TAG);\n+\ttlb_test(\"Guest-triggered TLB flush with multiple TDP roots (change tag), TDP enabled\",\n+\t\t l1_guest_code_multi_tdp_root, tdp_enabled = true,\n+\t\t flush_method = TLB_CHANGE_TAG);\n+\n+\n+\ttlb_test(\"KVM-triggered TLB flush via memslot move, TDP disabled\",\n+\t\t l1_guest_code_memslot_move, tdp_enabled = false,\n+\t\t flush_method = TLB_FLUSH_NONE);\n+\ttlb_test(\"KVM-triggered TLB flush via memslot move, TDP enabled\",\n+\t\t l1_guest_code_memslot_move, tdp_enabled = true,\n+\t\t flush_method = TLB_FLUSH_NONE);\n+\n+\treturn 0;\n+}\n"
} |
| 2/1 | 2026/10/08 00:29 | agent | patch-evaluator |
1mModel:core Args: null Results: {
"EnableConfigs": [
"KVM",
"KVM_AMD",
"KVM_INTEL",
"KVM_AMD_SEV",
"KVM_HYPERV"
],
"FocusSymbols": [
"nested_svm_vmrun",
"nested_svm_vmexit",
"enter_svm_guest_mode",
"svm_set_nested_state",
"invlpga_interception",
"svm_flush_tlb_asid",
"svm_flush_tlb_all",
"kvm_alloc_tlb_tag",
"kvm_free_tlb_tag",
"__kvm_mmu_invlpg",
"__kvm_mmu_invalidate_addr",
"svm_allocate_nested",
"svm_free_nested",
"sev_migrate_from"
],
"Reasoning": "The patch series introduces a shared TLB tag management framework across x86 KVM, refactors VPID allocation on Intel VMX and ASID management on AMD SVM, reworks nested SVM TLB flushing and MMU synchronization (including a new KVM_REQ_MMU_SYNC_ALL_ROOTS request and dedicated asid02 allocation for nested guests), updates INVLPGA instruction interception handling, and introduces multiple new runtime assertions (WARN_ON_ONCE, KVM_BUG_ON). These changes modify reachable virtualization paths exposed via /dev/kvm ioctls and nested SVM execution, warranting fuzzing for potential state desynchronization, memory leaks, and assertion violations.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 15e9e385bcc0e8a4bbd6ce129c9a926c3d78c56c
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Oct 8 00:29:28 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index ddc61ea36ac59..60f49b3675920 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -9770,6 +9770,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 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index e2eef7057bd35..1d28bb7e92c7f 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -127,6 +127,7 @@
KVM_ARCH_REQ_FLAGS(33, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE \
KVM_ARCH_REQ_FLAGS(34, KVM_REQUEST_WAIT)
+#define KVM_REQ_MMU_SYNC_ALL_ROOTS KVM_ARCH_REQ(35)
#define INVALID_PAGE (~(hpa_t)0)
#define VALID_PAGE(x) ((x) != INVALID_PAGE)
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index adafd6fa2c542..e45c01d6faeaf 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -1846,8 +1846,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;
diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h
index 622a6553e9ac2..b82f70e6fcea1 100644
--- a/arch/x86/kvm/hyperv.h
+++ b/arch/x86/kvm/hyperv.h
@@ -312,18 +312,34 @@ 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)
+/* 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;
+
+ fifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu));
+ if (fifo)
+ __kvm_hv_purge_tlb_flush_fifo(vcpu, fifo);
+}
- kfifo_reset_out(&tlb_flush_fifo->entries);
+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)
@@ -396,7 +412,8 @@ 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 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;
diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
index 2ae7f9ed4cf86..b5100f31f6a35 100644
--- a/arch/x86/kvm/mmu.h
+++ b/arch/x86/kvm/mmu.h
@@ -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);
@@ -170,6 +173,13 @@ void kvm_mmu_unload(struct kvm_vcpu *vcpu);
void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu);
void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu);
+
+static inline void kvm_mmu_sync_all_roots(struct kvm_vcpu *vcpu)
+{
+ kvm_mmu_sync_roots(vcpu);
+ kvm_mmu_sync_prev_roots(vcpu);
+}
+
void kvm_mmu_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
int bytes);
@@ -410,4 +420,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_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);
+
#endif
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index aadd5bdc2c97a..bfc3dd08bedb0 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -6732,8 +6732,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;
@@ -6769,8 +6769,8 @@ static void __kvm_mmu_invalidate_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)
+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;
@@ -6783,7 +6783,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;
@@ -6798,16 +6799,23 @@ 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);
+
+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)
+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,
@@ -6819,11 +6827,17 @@ 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);
+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)
{
@@ -8178,6 +8192,98 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm)
vhost_task_stop(kvm->arch.nx_huge_page_recovery_thread);
}
+static struct {
+ spinlock_t lock;
+ unsigned long *bitmap;
+ unsigned int nr;
+ kvm_tlb_tag_t start;
+} tlb_tags;
+
+/*
+ * Initialize @nr_total TLB tags, where @nr_reserved of those tags are reserved.
+ * Reserved tags start at 0 and are contiguous. Hence, usable tags are
+ * [nr_reserved, nr_total). At least one reserved tag is required, as 0 is the
+ * host TLB tag on both VMX and AMD, and is the value returned on a failed tag
+ * allocation.
+ */
+int kvm_init_tlb_tags(unsigned int nr_total, unsigned int nr_reserved)
+{
+ /*
+ * Limit the number of TLB tags to VMX's hardcoded maximum of 0x10000
+ * to avoid wasting memory for the bitmap in the unlikely scenario the
+ * CPU supports an inordinate number of ASIDs (on AMD). If userspace
+ * wants to concurrently run tens of thousands of vCPUs, they'll likely
+ * need a solution that works for both Intel and AMD.
+ */
+ const unsigned int MAX_NR_TLB_TAGS = VMX_NR_VPIDS;
+ unsigned int nr;
+
+ if (WARN_ON_ONCE(!nr_reserved || nr_reserved > nr_total))
+ return -EINVAL;
+
+ nr = nr_total - nr_reserved;
+ if (!nr)
+ return 0;
+
+ if (nr > MAX_NR_TLB_TAGS) {
+ pr_warn_once("Number of TLB tags capped (%u instead of %u)\n",
+ MAX_NR_TLB_TAGS, nr);
+ nr = MAX_NR_TLB_TAGS;
+ }
+
+ tlb_tags.bitmap = bitmap_zalloc(nr, GFP_KERNEL);
+ if (!tlb_tags.bitmap)
+ return -ENOMEM;
+
+ tlb_tags.nr = nr;
+ tlb_tags.start = nr_reserved;
+ spin_lock_init(&tlb_tags.lock);
+ return 0;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_init_tlb_tags);
+
+void kvm_destroy_tlb_tags(void)
+{
+ bitmap_free(tlb_tags.bitmap);
+ tlb_tags.bitmap = NULL;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_destroy_tlb_tags);
+
+kvm_tlb_tag_t kvm_alloc_tlb_tag(void)
+{
+ unsigned int bit;
+
+ if (!tlb_tags.bitmap)
+ return 0;
+
+ guard(spinlock)(&tlb_tags.lock);
+
+ bit = find_first_zero_bit(tlb_tags.bitmap, tlb_tags.nr);
+ if (bit >= tlb_tags.nr)
+ return 0;
+
+ __set_bit(bit, tlb_tags.bitmap);
+ return tlb_tags.start + bit;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_alloc_tlb_tag);
+
+void kvm_free_tlb_tag(kvm_tlb_tag_t tag)
+{
+ unsigned int bit;
+
+ if (!tag || WARN_ON_ONCE(tag < tlb_tags.start))
+ return;
+
+ bit = tag - tlb_tags.start;
+ if (WARN_ON_ONCE(bit >= tlb_tags.nr))
+ return;
+
+ guard(spinlock)(&tlb_tags.lock);
+
+ __clear_bit(bit, tlb_tags.bitmap);
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_free_tlb_tag);
+
#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES
static bool hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn,
int level)
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index af0ef678433d7..6fe68f2ae282d 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -556,10 +556,8 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
to->nested_cr3 = from->nested_cr3;
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,26 +686,69 @@ 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)
{
+ 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;
+ }
+
/*
- * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
- * things to fix before this can be conditional:
+ * 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. Sync all roots, since the TLB flush is not
+ * specific to the currently loaded nCR3.
+ *
+ * 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.
+ *
+ * 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.
*
- * - 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
- * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
+ * 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.
*
- * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
- * NPT guest-physical mappings on VMRUN.
+ * TLB_CONTROL_FLUSH_ASID and TLB_CONTROL_FLUSH_ASID_LOCAL are handled
+ * equally for simplicity.
*/
- kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
- kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+ if (new_asid || (svm->nested.ctl.tlb_ctl != TLB_CONTROL_DO_NOTHING)) {
+ if (nested_npt_enabled(svm))
+ kvm_make_request(KVM_REQ_MMU_SYNC_ALL_ROOTS, 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);
+ }
+}
+
+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 requested 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);
+
+ if (svm->asid == svm->nested.asid02)
+ kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
}
/*
@@ -860,7 +901,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);
@@ -936,10 +977,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
else
vmcb02->control.bus_lock_counter = 0;
- /* Done at vmrun: asid. */
-
- /* Also overwritten later if necessary. */
- vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+ vmcb02->control.asid = svm->nested.asid02;
/* Use vmcb01 MMU and format if guest does not use nNPT */
if (nested_npt_enabled(svm)) {
@@ -1038,6 +1076,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);
@@ -1069,6 +1121,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);
@@ -1142,7 +1196,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);
@@ -1194,6 +1248,13 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)
svm->vmcb->control.exit_info_1 = 0;
svm->vmcb->control.exit_info_2 = 0;
+ /*
+ * Force a flush+sync on the next nested VMRUN. Otherwise,
+ * pending MMU syncs due to a changed ASID are lost, as
+ * last_asid was already updated.
+ */
+ svm->nested.last_asid = 0;
+
nested_svm_vmexit(svm);
}
@@ -1331,7 +1392,9 @@ 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 */
+ nested_svm_service_local_tlb_flushes(vcpu);
+
+ svm_switch_vmcb(svm, &svm->vmcb01);
leave_guest_mode(vcpu);
svm_pmu_handle_nested_transition(svm);
@@ -1358,8 +1421,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:
*
@@ -1438,7 +1499,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);
@@ -1489,21 +1550,36 @@ 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.cpu = -1;
- svm->nested.initialized = true;
+ svm->nested.vmcb02.ptr = page_address(vmcb02_page);
+ svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
+
+ 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 ASID (e.g. different vCPU or even different VM) could be used.
+ */
+ svm->nested.last_asid = 0;
+
return 0;
err_free_vmcb02:
@@ -1513,12 +1589,14 @@ 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))
svm_switch_vmcb(svm, &svm->vmcb01);
+ free_asid(svm->nested.asid02);
+
svm_vcpu_free_msrpm(svm->nested.msrpm);
svm->nested.msrpm = NULL;
@@ -1533,8 +1611,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)
@@ -1545,6 +1621,9 @@ 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);
/*
@@ -1556,7 +1635,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
*/
__svm_pmu_handle_nested_transition(svm, true);
- svm_switch_vmcb(svm, &svm->vmcb01);
+ nested_svm_exit_tlb_flush(vcpu);
nested_svm_uninit_mmu_context(vcpu);
vmcb_mark_all_dirty(svm->vmcb);
@@ -1853,7 +1932,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)
{
@@ -2084,6 +2163,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
svm_copy_vmrun_state(vcpu, &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)
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index fad72cab76cf4..646108e70e21a 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -315,11 +315,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;
@@ -2037,6 +2032,28 @@ 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,
+ unsigned int asid)
+{
+ /*
+ * Free the (potentially non-SEV) ASID on the destination vCPU before
+ * setting the new SEV ASID.
+ */
+ free_asid(dst_svm->asid);
+ dst_svm->asid = asid;
+
+ /*
+ * If nested is already initialized on the destination vCPU, also free
+ * the previous ASID (in case it was non-SEV) and update the nested ASID
+ * to match the new SEV ASID.
+ */
+ if (dst_svm->nested.vmcb02.ptr) {
+ free_asid(dst_svm->nested.asid02);
+ 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);
@@ -2101,6 +2118,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);
+ sev_vcpu_migrate_asid(dst_svm, dst->asid);
sev_init_vmcb(dst_svm, false);
if (!dst->es_active)
@@ -3623,9 +3641,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;
-
/*
* Flush guest TLB:
*
@@ -3637,7 +3652,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);
return 0;
}
@@ -4889,6 +4904,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);
+ WARN_ON_ONCE(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.
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 136069dfcd44f..4b95a05c37f60 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -192,6 +192,8 @@ DEFINE_PER_CPU(struct svm_cpu_data, svm_data);
static DEFINE_MUTEX(vmcb_dump_mutex);
+kvm_tlb_tag_t fallback_asid;
+
/*
* Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
* the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
@@ -583,15 +585,6 @@ static int svm_enable_virtualization_cpu(void)
return r;
sd = per_cpu_ptr(&svm_data, me);
- /*
- * Bump the current asid_generation value to ensure any vCPU that
- * previously ran on this CPU sees a stale generation and is forced
- * to acquire a new ASID, preventing a latent ASID collision.
- */
- sd->asid_generation++;
- sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
- sd->next_asid = sd->max_asid + 1;
- sd->min_asid = max_sev_asid + 1;
wrmsrq(MSR_VM_HSAVE_PA, sd->save_area_pa);
@@ -994,6 +987,8 @@ static void svm_hardware_unsetup(void)
__free_pages(__sme_pa_to_page(iopm_base), get_order(IOPM_SIZE));
iopm_base = 0;
+
+ kvm_destroy_tlb_tags();
}
static void init_seg(struct vmcb_seg *seg)
@@ -1246,8 +1241,8 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
if (gmet_enabled)
control->misc_ctl |= SVM_MISC_ENABLE_GMET;
- svm->current_vmcb->asid_generation = 0;
- svm->asid = 0;
+ control->asid = svm->asid;
+ vmcb_set_flush_asid(vmcb);
svm->nested.vmcb12_gpa = INVALID_GPA;
svm->nested.last_vmcb12_gpa = INVALID_GPA;
@@ -1375,6 +1370,12 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
goto error_free_avic;
}
+ svm->asid = allocate_asid(vcpu);
+ if (!svm->asid) {
+ err = -EINVAL;
+ goto error_free_msrpm;
+ }
+
svm->x2avic_msrs_intercepted = true;
svm->lbr_msrs_intercepted = true;
@@ -1386,6 +1387,8 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
return 0;
+error_free_msrpm:
+ svm_vcpu_free_msrpm(svm->msrpm);
error_free_avic:
avic_vcpu_free(vcpu);
error_free_sev:
@@ -1402,6 +1405,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));
@@ -1417,6 +1422,13 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)
__free_page(__sme_pa_to_page(svm->vmcb01.pa));
svm_vcpu_free_msrpm(svm->msrpm);
+
+ free_asid(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
@@ -1942,18 +1954,6 @@ static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
}
}
-static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
-{
- if (sd->next_asid > sd->max_asid) {
- ++sd->asid_generation;
- sd->next_asid = sd->min_asid;
- sd->flush_all_asids = true;
- }
-
- svm->current_vmcb->asid_generation = sd->asid_generation;
- svm->asid = sd->next_asid++;
-}
-
static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
{
struct vmcb *vmcb = to_svm(vcpu)->vmcb;
@@ -2468,17 +2468,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,
+ * assume the 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 its 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);
}
@@ -3875,37 +3909,28 @@ 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 = per_cpu_ptr(&svm_data, vcpu->cpu);
+ struct svm_cpu_data *sd = this_cpu_ptr(&svm_data);
struct vcpu_svm *svm = to_svm(vcpu);
/*
* If the previous VMRUN of the VMCB occurred on a different physical
- * cpu, then mark the VMCB dirty as hardware's clean bits are per pCPU.
- *
- * Reset the ASID generation in both VMCBs. This will lead to assigning
- * a new ASID now, and then again when switching to the other VMCB.
- * However, this is needed as the ASID is shared between the VMCBs, and
- * otherwise it would be possible to use an ASID allocated on one pCPU
- * on another, for example:
- * - vCPU migrates from pCPU A to pCPU B, allocates a new ASID.
- * - vCPU migrates back to pCPU A, and then switches the VMCB.
- * - The new VMCB does not detect a pCPU change and runs on pCPU A with
- * the new ASID allocated on pCPU B, which is potentially used by
- * another vCPU/VM.
+ * cpu, then mark the VMCB dirty and flush the ASID, as hardware's clean
+ * bits and TLB entries are per pCPU.
*/
if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
vmcb_mark_all_dirty(svm->vmcb);
svm->current_vmcb->cpu = vcpu->cpu;
- svm->vmcb01.asid_generation = 0;
- svm->nested.vmcb02.asid_generation = 0;
+ vmcb_set_flush_asid(svm->vmcb);
}
if (is_sev_guest(vcpu))
return pre_sev_run(svm, vcpu->cpu);
- /* FIXME: handle wraparound of asid_generation */
- if (svm->current_vmcb->asid_generation != sd->asid_generation)
- new_asid(svm, sd);
+ 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;
}
@@ -4317,7 +4342,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
@@ -4326,10 +4351,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 (cpu_feature_enabled(X86_FEATURE_FLUSHBYASID))
- svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
- else
- svm->current_vmcb->asid_generation--;
+ vmcb_set_flush_asid(svm->vmcb);
}
static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)
@@ -4350,6 +4372,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
@@ -4360,7 +4384,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_guest(struct kvm_vcpu *vcpu)
@@ -4647,13 +4675,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);
- }
- if (this_cpu_ptr(&svm_data)->flush_all_asids)
- svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
-
svm->vmcb->save.cr2 = vcpu->arch.cr2;
if (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS) &&
@@ -4745,8 +4766,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
}
if (!svm_is_vmrun_failure(svm->vmcb->control.exit_code)) {
- this_cpu_ptr(&svm_data)->flush_all_asids = false;
- 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
@@ -5659,9 +5679,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);
@@ -5757,6 +5776,7 @@ static __init void svm_set_cpu_caps(void)
static __init int svm_hardware_setup(void)
{
+ unsigned long nr_asids;
void *iopm_va;
int cpu, r;
@@ -5914,6 +5934,14 @@ static __init int svm_hardware_setup(void)
kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_CD_NW_CLEARED;
+ /* Consumes max_sev_asid initialized by sev_hardware_setup() */
+ nr_asids = cpuid_ebx(SVM_CPUID_FUNC);
+ r = kvm_init_tlb_tags(nr_asids, max_sev_asid + 1);
+ if (r)
+ goto err;
+
+ fallback_asid = kvm_alloc_tlb_tag();
+
for_each_possible_cpu(cpu) {
r = svm_cpu_init(cpu);
if (r)
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 6d0242966dc87..15c4aa3b02c2d 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -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
@@ -145,7 +146,6 @@ struct kvm_vmcb_info {
struct vmcb *ptr;
unsigned long pa;
int cpu;
- uint64_t asid_generation;
};
struct vmcb_save_area_cached {
@@ -234,7 +234,7 @@ struct svm_nested_state {
*/
struct vmcb_save_area_cached save;
- bool initialized;
+ kvm_tlb_tag_t asid02;
/*
* Indicates whether MSR bitmap for L2 needs to be rebuilt due to
@@ -244,6 +244,8 @@ struct svm_nested_state {
* on its side.
*/
bool force_msr_bitmap_recalc;
+
+ u32 last_asid;
};
struct vcpu_sev_es_state {
@@ -285,7 +287,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;
@@ -372,17 +374,14 @@ struct vcpu_svm {
};
struct svm_cpu_data {
- u64 asid_generation;
- u32 max_asid;
- u32 next_asid;
- u32 min_asid;
-
- bool flush_all_asids;
bool bp_spec_reduce_set;
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;
};
@@ -432,6 +431,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)
{
@@ -446,6 +450,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)
@@ -480,6 +489,19 @@ 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)
+{
+ if (cpu_feature_enabled(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)
+{
+ 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);
@@ -1057,6 +1079,21 @@ static inline struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu)
static inline void sev_free_decrypted_vmsa(struct kvm_vcpu *vcpu, struct vmcb_save_area *vmsa) {}
#endif
+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(kvm_tlb_tag_t asid)
+{
+ if (asid > max_sev_asid && asid != fallback_asid)
+ kvm_free_tlb_tag(asid);
+}
+
/* vmenter.S */
void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, unsigned int flags,
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b341af4c6057a..a1caa2bd04d7a 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -598,9 +598,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;
@@ -4092,31 +4089,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)
{
/*
@@ -8415,6 +8387,8 @@ void vmx_hardware_unsetup(void)
if (nested)
nested_vmx_hardware_unsetup();
+
+ destroy_vpids();
}
void vmx_vm_destroy(struct kvm *kvm)
@@ -8640,8 +8614,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
@@ -8681,6 +8653,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
@@ -8688,8 +8664,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;
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 6c9dd5b2365d5..3e1ecd9148e9c 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -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;
@@ -297,9 +297,29 @@ struct kvm_vmx {
u64 *pid_table;
};
+static __always_inline int init_vpids(void)
+{
+ return enable_vpid ? kvm_init_tlb_tags(VMX_NR_VPIDS, 1) : 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,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 32563a91fba99..a54e9da2ed09e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2020,8 +2020,7 @@ static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
* a forced sync of the shadow page tables. Ensure all the
* roots are synced and the guest TLB in hardware is clean.
*/
- kvm_mmu_sync_roots(vcpu);
- kvm_mmu_sync_prev_roots(vcpu);
+ kvm_mmu_sync_all_roots(vcpu);
}
kvm_x86_call(flush_tlb_guest)(vcpu);
@@ -2030,7 +2029,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);
}
@@ -8167,8 +8166,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
if (unlikely(r))
goto out;
}
- if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
+
+ if (kvm_check_request(KVM_REQ_MMU_SYNC_ALL_ROOTS, vcpu)) {
+ kvm_mmu_sync_all_roots(vcpu);
+ kvm_clear_request(KVM_REQ_MMU_SYNC, vcpu);
+ } else if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) {
kvm_mmu_sync_roots(vcpu);
+ }
+
if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
kvm_mmu_load_pgd(vcpu);
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index be5eb2dd85aa2..5347c56786b08 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -122,6 +122,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
diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index ffec2291b1e1f..62d255aee5a34 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -1617,6 +1617,7 @@ void tdp_map(struct kvm_vm *vm, gpa_t l2_gpa, gpa_t gpa, u64 size);
void tdp_identity_map_default_memslots(struct kvm_vm *vm);
void tdp_identity_map_1g(struct kvm_vm *vm, u64 addr, u64 size);
u64 *tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa);
+gpa_t tdp_copy_page_tables(struct kvm_vm *vm);
/*
* Basic CPU control in CR0
diff --git a/tools/testing/selftests/kvm/include/x86/svm.h b/tools/testing/selftests/kvm/include/x86/svm.h
index c8539166270ea..7644e37c7042c 100644
--- a/tools/testing/selftests/kvm/include/x86/svm.h
+++ b/tools/testing/selftests/kvm/include/x86/svm.h
@@ -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 */
diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
index 8fd97434eee5c..4f2a0e3ac5ba1 100644
--- a/tools/testing/selftests/kvm/include/x86/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86/vmx.h
@@ -518,4 +518,25 @@ static inline bool kvm_cpu_has_vmx_virtualize_apic_accesses(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 */
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index 8374afd08bafe..57ecf8b381b5a 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -554,6 +554,42 @@ void tdp_identity_map_1g(struct kvm_vm *vm, u64 addr, u64 size)
__tdp_map(vm, addr, addr, size, PG_LEVEL_1G);
}
+static gpa_t __virt_copy_page_tables(struct kvm_vm *vm, struct kvm_mmu *mmu,
+ gpa_t table_gpa, int level)
+{
+ gpa_t copy_gpa = vm_alloc_page_table(vm);
+ u64 *copy = addr_gpa2hva(vm, copy_gpa);
+ gpa_t pa;
+ int i;
+
+ memcpy(copy, addr_gpa2hva(vm, table_gpa), vm->page_size);
+ if (level == PG_LEVEL_4K)
+ return copy_gpa;
+
+ for (i = 0; i < vm->page_size / sizeof(u64); i++) {
+ if (!is_present_pte(mmu, ©[i]) || is_huge_pte(mmu, ©[i]))
+ continue;
+
+ /* Clear only the address bits, preserving C/S bits and flags */
+ pa = vm_untag_gpa(vm, PTE_GET_PA(copy[i]));
+ copy[i] &= ~pa;
+ copy[i] |= __virt_copy_page_tables(vm, mmu, pa, level - 1);
+ }
+ return copy_gpa;
+}
+
+/*
+ * Deep copy all levels of the TDP page tables, such that no page table pages
+ * are shared with the original. Returns the GPA of the new root.
+ */
+gpa_t tdp_copy_page_tables(struct kvm_vm *vm)
+{
+ struct kvm_mmu *mmu = &vm->stage2_mmu;
+
+ TEST_ASSERT(mmu->pgd_created, "TDP page tables not created");
+ return __virt_copy_page_tables(vm, mmu, mmu->pgd, mmu->pgtable_levels);
+}
+
/*
* Set Unusable Segment
*
diff --git a/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c b/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c
new file mode 100644
index 0000000000000..fb32b7bf6b19f
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c
@@ -0,0 +1,526 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026, Google LLC.
+ *
+ * Test TLB flushes with nested virtualization across three cases:
+ * 1. When L1 updates L2's (shadow) page tables
+ * 2. When L1 updates nested TDP
+ * 3. When KVM updates mappings on the host (KVM-induced TLB flushes)
+ */
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "svm_util.h"
+#include "vmx.h"
+
+#define NR_ITERATIONS 100
+
+#define VAL1 0x11111111ULL
+#define VAL2 0x22222222ULL
+
+#define TEST_VADDR 0x10000000ULL
+#define TEST_GPA 0x30000000ULL
+#define TEST_NESTED_GPA 0x40000000ULL
+
+#define TEST_MEMSLOT 10
+
+#define SYNC_MOVE_MEMSLOT 1
+
+enum guest_flush_method {
+ TLB_FLUSH_TAG = 0,
+ TLB_FLUSH_VADDR,
+ TLB_CHANGE_TAG,
+ TLB_FLUSH_NONE,
+};
+
+static u64 *pte_gva;
+static gpa_t test_gpa[2];
+static enum guest_flush_method flush_method;
+static u32 max_asid;
+
+/* TDP roots for the multi-root test, root 1 is a non-aliasing copy of root 0 */
+static gpa_t tdp_root_gpa[2];
+/* Value L2 expects at TEST_VADDR, 0 if L2 shouldn't access TEST_VADDR */
+static u64 l2_expected_val;
+
+static void svm_tlb_flush(struct svm_test_data *svm,
+ enum guest_flush_method method)
+{
+ struct vmcb *vmcb = svm->vmcb;
+
+ switch (method) {
+ case TLB_CHANGE_TAG:
+ vmcb->control.asid++;
+ vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+ if (vmcb->control.asid >= max_asid) {
+ vmcb->control.asid = 1;
+ vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
+ }
+ break;
+ case TLB_FLUSH_TAG:
+ vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
+ break;
+ case TLB_FLUSH_VADDR:
+ GUEST_ASSERT(!svm->ncr3_gpa);
+ vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+ invlpga(TEST_VADDR, vmcb->control.asid);
+ break;
+ case TLB_FLUSH_NONE:
+ vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+ break;
+ }
+}
+
+static void vmx_tlb_flush(struct vmx_pages *vmx, enum guest_flush_method method)
+{
+ u16 vpid = vmread(VIRTUAL_PROCESSOR_ID);
+
+ switch (method) {
+ case TLB_CHANGE_TAG:
+ GUEST_ASSERT(!vmx->eptp_gpa);
+ vmwrite(VIRTUAL_PROCESSOR_ID, vpid + 1);
+ break;
+ case TLB_FLUSH_TAG:
+ if (vmx->eptp_gpa)
+ invept(1, vmread(EPT_POINTER));
+ else
+ invvpid(1, vpid, 0);
+ break;
+ case TLB_FLUSH_VADDR:
+ GUEST_ASSERT(!vmx->eptp_gpa);
+ invvpid(0, vpid, TEST_VADDR);
+ break;
+ case TLB_FLUSH_NONE:
+ break;
+ }
+}
+
+static void prepare_l2(void *nested_state, void *l2_guest_code)
+{
+ struct vmx_pages *vmx = nested_state;
+
+ if (this_cpu_has(X86_FEATURE_VMX)) {
+ u32 sec_exec_ctl;
+
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+ prepare_vmcs(vmx, l2_guest_code);
+
+ /* Enable VPID */
+ sec_exec_ctl = vmread(SECONDARY_VM_EXEC_CONTROL);
+ sec_exec_ctl |= SECONDARY_EXEC_ENABLE_VPID;
+ vmwrite(SECONDARY_VM_EXEC_CONTROL, sec_exec_ctl);
+ vmwrite(VIRTUAL_PROCESSOR_ID, 1);
+ } else {
+ generic_svm_setup(nested_state, l2_guest_code);
+ }
+}
+
+static void __run_l2(void *nested_state, bool launch,
+ enum guest_flush_method method)
+{
+ struct svm_test_data *svm;
+
+ if (this_cpu_has(X86_FEATURE_VMX)) {
+ vmx_tlb_flush(nested_state, method);
+ if (launch)
+ vmlaunch();
+ else
+ vmresume();
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_VMCALL);
+ vmwrite(GUEST_RIP, vmread(GUEST_RIP) + 3); /* skip over VMCALL */
+ } else {
+ svm = nested_state;
+ svm_tlb_flush(svm, method);
+ run_guest(svm->vmcb, svm->vmcb_gpa);
+ GUEST_ASSERT_EQ(svm->vmcb->control.exit_code, SVM_EXIT_VMMCALL);
+ svm->vmcb->save.rip += 3; /* skip over VMMCALL */
+ }
+}
+
+static void run_l2(void *nested_state, bool launch)
+{
+ __run_l2(nested_state, launch, flush_method);
+}
+
+static void l2_guest_code(void)
+{
+ u64 expected_val;
+
+ for (;;) {
+ expected_val = READ_ONCE(l2_expected_val);
+ if (expected_val)
+ GUEST_ASSERT_EQ(READ_ONCE(*(u64 *)TEST_VADDR), expected_val);
+ vmcall();
+ }
+}
+
+static void l1_guest_code(void *data)
+{
+ gpa_t gpa;
+ int i;
+
+ prepare_l2(data, l2_guest_code);
+
+ /*
+ * Alternately switch the PTE (or TDP PTE) mapping TEST_VADDR between
+ * two pages containing VAL1 and VAL2, flush TLBs, and verify that L2
+ * reads the expected value.
+ */
+ for (i = 0; i < NR_ITERATIONS; i++) {
+ gpa = test_gpa[i % 2];
+
+ *pte_gva &= ~PHYSICAL_PAGE_MASK;
+ *pte_gva |= gpa & PHYSICAL_PAGE_MASK;
+
+ WRITE_ONCE(l2_expected_val, (i % 2 == 0) ? VAL1 : VAL2);
+ run_l2(data, i == 0);
+ }
+
+ GUEST_DONE();
+}
+
+/*
+ * Verify that flushing L2's ASID invalidates nested TDP translations for all
+ * TDP roots, not just the one in use at the time of the flush. L1 updates the
+ * nested TDP PTE for TEST_VADDR in root 0, flushes L2's ASID while running L2
+ * with root 1, then runs L2 with root 0 *without* flushing. L2 must observe
+ * the new mapping, as the flush invalidated all translations for the ASID.
+ *
+ * Root 1 is a full copy of root 0, such that none of the nested TDP pages are
+ * shared, i.e. KVM's shadow pages for root 0 are not reachable through the
+ * shadow root for root 1.
+ */
+static void l1_guest_code_multi_tdp_root(void *data)
+{
+ struct svm_test_data *svm = data;
+ struct vmcb *vmcb = svm->vmcb;
+ int i;
+
+ GUEST_ASSERT(this_cpu_has(X86_FEATURE_SVM));
+ GUEST_ASSERT_EQ(svm->ncr3_gpa, tdp_root_gpa[0]);
+
+ prepare_l2(data, l2_guest_code);
+
+ for (i = 0; i < NR_ITERATIONS; i++) {
+ *pte_gva &= ~PHYSICAL_PAGE_MASK;
+ *pte_gva |= test_gpa[i % 2] & PHYSICAL_PAGE_MASK;
+
+ /* Flush L2's ASID (per flush_method) while using root 1 */
+ vmcb->control.nested_cr3 = tdp_root_gpa[1];
+ WRITE_ONCE(l2_expected_val, 0);
+ run_l2(data, i == 0);
+
+ /* Switch back to root 0 without flushing */
+ vmcb->control.nested_cr3 = tdp_root_gpa[0];
+ WRITE_ONCE(l2_expected_val, (i % 2 == 0) ? VAL1 : VAL2);
+ __run_l2(data, false, TLB_FLUSH_NONE);
+ }
+
+ GUEST_DONE();
+}
+
+static void prep_guest_flush_test(struct kvm_vm *vm, bool tdp_enabled,
+ bool copy_tdp_root)
+{
+ gva_t page_gva[2], pte_map_gva;
+ gpa_t pte_gpa;
+ u64 *ptep;
+
+ /*
+ * Allocate two pages in the VM and initialize them with different
+ * values. L1 will update the mappings to switch between these pages and
+ * L2 will verify that the accessed value indicates the right page.
+ */
+ page_gva[0] = vm_alloc_page(vm);
+ page_gva[1] = vm_alloc_page(vm);
+
+ *(u64 *)addr_gva2hva(vm, page_gva[0]) = VAL1;
+ *(u64 *)addr_gva2hva(vm, page_gva[1]) = VAL2;
+
+ test_gpa[0] = addr_gva2gpa(vm, page_gva[0]);
+ test_gpa[1] = addr_gva2gpa(vm, page_gva[1]);
+
+ if (tdp_enabled) {
+ virt_pg_map(vm, TEST_VADDR, TEST_NESTED_GPA);
+ tdp_map(vm, TEST_NESTED_GPA, test_gpa[0], vm->page_size);
+ ptep = tdp_get_pte(vm, TEST_NESTED_GPA);
+ } else {
+ virt_pg_map(vm, TEST_VADDR, test_gpa[0]);
+ ptep = vm_get_pte(vm, TEST_VADDR);
+ }
+
+ /*
+ * Map the PTE (or TDP PTE) for TEST_VADDR, such that L1 can read and
+ * update the mapping. Pass the PTE GVA to L1 to directly use it (rather
+ * than L1 walking the page tables.
+ */
+ pte_gpa = addr_hva2gpa(vm, ptep);
+ pte_map_gva = vm_unused_gva_gap(vm, vm->page_size, KVM_UTIL_MIN_VADDR);
+ virt_pg_map(vm, pte_map_gva, pte_gpa & PAGE_MASK);
+ pte_gva = (u64 *)(pte_map_gva + (pte_gpa & ~PAGE_MASK));
+
+ sync_global_to_guest(vm, pte_gva);
+ sync_global_to_guest(vm, test_gpa);
+
+ if (!tdp_enabled)
+ return;
+
+ /*
+ * Must be done after all pages accessed by L2 are allocated, including
+ * nested state (e.g. L2's stack) and page tables for TEST_VADDR.
+ */
+ tdp_identity_map_default_memslots(vm);
+
+ /* Must be done after all TDP mappings are created */
+ if (copy_tdp_root) {
+ tdp_root_gpa[0] = vm->stage2_mmu.pgd;
+ tdp_root_gpa[1] = tdp_copy_page_tables(vm);
+ sync_global_to_guest(vm, tdp_root_gpa);
+ }
+}
+
+static void l2_guest_code_memslot_move(void)
+{
+ int i;
+ u64 val;
+
+ for (i = 0; i < NR_ITERATIONS; i++) {
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL1);
+
+ vmcall();
+
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL2);
+
+ GUEST_SYNC2(SYNC_MOVE_MEMSLOT, 1);
+
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL1);
+
+ vmcall();
+ }
+}
+
+static void l1_guest_code_memslot_move(void *data)
+{
+ int i;
+ u64 val;
+
+ prepare_l2(data, l2_guest_code_memslot_move);
+
+ for (i = 0; i < NR_ITERATIONS; i++) {
+ /* Verify that accessing VADDR reads from page 1 in both L1 & L2 */
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL1);
+
+ run_l2(data, i == 0);
+
+ /*
+ * Update VADDR to point at page 2. KVM should flush both L1 and
+ * L2's TLB translations so that further accesses go to page 2.
+ */
+ GUEST_SYNC2(SYNC_MOVE_MEMSLOT, 2);
+
+ /*
+ * Verify that accessing VADDR reads from page 2. A stale TLB
+ * entry would lead to reading from page 1. Enter L2 and check
+ * access to page 2, to verify that a TLB flush from L1's
+ * context flushes both L1 and L2.
+ */
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL2);
+
+ run_l2(data, false);
+
+ /*
+ * L2 updated VADDR to point at page 1 again. Verify access to
+ * page 1 again to verify that a TLB flush from L2's context
+ * flushes both L1 and L2 TLB translations.
+ */
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL1);
+ }
+
+ GUEST_DONE();
+}
+
+static void prep_memslot_move_test(struct kvm_vm *vm, bool tdp_enabled)
+{
+ /*
+ * Disable KVM_X86_QUIRK_SLOT_ZAP_ALL. By default, moving a memslot
+ * invalidates all MMU roots, which implicitly flushes the guest TLB
+ * and masks missing TLB flush bugs. Disabling it forces KVM to keep
+ * the same roots, relying solely on explicit TLB flushes.
+ */
+ TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & KVM_X86_QUIRK_SLOT_ZAP_ALL);
+ vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);
+
+ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+ TEST_GPA, TEST_MEMSLOT,
+ /*npages=*/2, /*flags=*/0);
+
+ virt_pg_map(vm, TEST_VADDR, TEST_GPA);
+
+ if (tdp_enabled)
+ tdp_map(vm, TEST_GPA, TEST_GPA, vm->page_size);
+
+ *(u64 *)addr_gpa2hva(vm, TEST_GPA) = VAL1;
+ *(u64 *)addr_gpa2hva(vm, TEST_GPA + vm->page_size) = VAL2;
+
+ /*
+ * Must be done after all pages accessed by L2 are allocated, including
+ * nested state (e.g. L2's stack) and page tables for TEST_VADDR.
+ */
+ if (tdp_enabled)
+ tdp_identity_map_default_memslots(vm);
+}
+
+static void calc_max_svm_asid(void)
+{
+ const struct kvm_cpuid_entry2 *entry;
+
+ if (!kvm_cpu_has(X86_FEATURE_SVM))
+ return;
+
+ entry = get_cpuid_entry(kvm_get_supported_cpuid(), 0x8000000A, 0);
+ max_asid = entry ? entry->ebx : 0;
+}
+
+static void run_test(void *guest_code, bool tdp_enabled)
+{
+ struct kvm_vcpu *vcpu;
+ gva_t nested_gva = 0;
+ struct kvm_vm *vm;
+ struct ucall uc;
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+ if (tdp_enabled)
+ vm_enable_tdp(vm);
+
+ if (kvm_cpu_has(X86_FEATURE_VMX))
+ vcpu_alloc_vmx(vm, &nested_gva);
+ else
+ vcpu_alloc_svm(vm, &nested_gva);
+
+ if (guest_code == l1_guest_code_memslot_move)
+ prep_memslot_move_test(vm, tdp_enabled);
+ else if (guest_code == l1_guest_code_multi_tdp_root)
+ prep_guest_flush_test(vm, tdp_enabled, true);
+ else
+ prep_guest_flush_test(vm, tdp_enabled, false);
+
+ vcpu_args_set(vcpu, 1, nested_gva);
+
+ sync_global_to_guest(vm, flush_method);
+ sync_global_to_guest(vm, max_asid);
+
+ for (;;) {
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ case UCALL_SYNC:
+ /*
+ * Move the base GPA of the memslot between TEST_GPA and
+ * TEST_GPA - PAGE_SIZE. This effectively moves
+ * TEST_VADDR to point at page 1 or page 2 in the
+ * memslot.
+ */
+ if (uc.args[0] == SYNC_MOVE_MEMSLOT) {
+ gpa_t new_gpa = (uc.args[1] == 1) ?
+ TEST_GPA : (TEST_GPA - vm->page_size);
+
+ vm_mem_region_move(vm, TEST_MEMSLOT, new_gpa);
+ } else {
+ TEST_FAIL("Unknown sync code: %lu", uc.args[0]);
+ }
+ break;
+ case UCALL_DONE:
+ goto done;
+ default:
+ TEST_FAIL("Unknown ucall %lu", uc.cmd);
+ }
+ }
+
+done:
+ kvm_vm_free(vm);
+}
+
+#define tlb_test(test_name, guest_code, tdp_setting, flush_setting) \
+do { \
+ tdp_setting; \
+ flush_setting; \
+ \
+ if (tdp_enabled && !kvm_cpu_has_tdp()) { \
+ pr_info("Skipping: " test_name " (no TDP support)\n"); \
+ break; \
+ } \
+ \
+ /* \
+ * VPIDs do not tag guest-physical translations, i.e. changing or \
+ * flushing VPIDs does not flush EPT translations, and single-context \
+ * INVEPT only flushes the target EPTP (not all TDP roots). \
+ */ \
+ if (tdp_enabled && kvm_cpu_has_ept() && \
+ (flush_method == TLB_CHANGE_TAG || \
+ guest_code == l1_guest_code_multi_tdp_root)) { \
+ pr_info("Skipping: " test_name " (not applicable to EPTs)\n"); \
+ break; \
+ } \
+ \
+ pr_info("Testing " test_name "...\n"); \
+ run_test(guest_code, tdp_enabled); \
+} while (0)
+
+int main(int argc, char *argv[])
+{
+ bool tdp_enabled;
+
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX));
+
+ calc_max_svm_asid();
+
+ tlb_test("Guest-triggered TLB flush (flush entire tag), TDP disabled",
+ l1_guest_code, tdp_enabled = false,
+ flush_method = TLB_FLUSH_TAG);
+ tlb_test("Guest-triggered TLB flush (flush individual address), TDP disabled",
+ l1_guest_code, tdp_enabled = false,
+ flush_method = TLB_FLUSH_VADDR);
+ tlb_test("Guest-triggered TLB flush (change tag), TDP disabled",
+ l1_guest_code, tdp_enabled = false,
+ flush_method = TLB_CHANGE_TAG);
+
+ tlb_test("Guest-triggered TLB flush (flush entire tag), TDP enabled",
+ l1_guest_code, tdp_enabled = true,
+ flush_method = TLB_FLUSH_TAG);
+ tlb_test("Guest-triggered TLB flush (change tag), TDP enabled",
+ l1_guest_code, tdp_enabled = true,
+ flush_method = TLB_CHANGE_TAG);
+
+ tlb_test("Guest-triggered TLB flush with multiple TDP roots (flush entire tag), TDP enabled",
+ l1_guest_code_multi_tdp_root, tdp_enabled = true,
+ flush_method = TLB_FLUSH_TAG);
+ tlb_test("Guest-triggered TLB flush with multiple TDP roots (change tag), TDP enabled",
+ l1_guest_code_multi_tdp_root, tdp_enabled = true,
+ flush_method = TLB_CHANGE_TAG);
+
+
+ tlb_test("KVM-triggered TLB flush via memslot move, TDP disabled",
+ l1_guest_code_memslot_move, tdp_enabled = false,
+ flush_method = TLB_FLUSH_NONE);
+ tlb_test("KVM-triggered TLB flush via memslot move, TDP enabled",
+ l1_guest_code_memslot_move, tdp_enabled = true,
+ flush_method = TLB_FLUSH_NONE);
+
+ return 0;
+}
|
| 3/2 | 2026/10/08 00:29 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31041 output: 35 thoughts: 1441 Thoughts: **KVM Patch Evaluation: WorthFuzzing=true** Okay, so the user wants me to evaluate this Linux kernel patch series for fuzzing potential, specifically targeting amd64 architecture. First, I need to look at the diff. It appears quite extensive, touching a significant number of files within the KVM subsystem. Specifically, it touches documentation, adds a new KVM request, and deeply modifies core KVM MMU, SVM nested virtualization, VMX VPID management, and `vcpu_enter_guest` code. A lot of TLB tagging, ASID allocation, INVLPGA interception, and nested SVM TLB flushes are being refactored. I need to confirm KVM's reachability on amd64. Yes, it's a primary target for syzkaller, which means it's definitely accessible via QEMU, GCE, etc. Syzkaller uses `/dev/kvm` ioctls, handles nested virtualization and SVM/VMX emulation. Since this code modifies reachable executable code within the KVM subsystem, I need to apply the rules. The rules are clear: if the patch touches reachable executable code in Core Subsystems or involves refactorings, assertions, etc., then it's worth fuzzing. This patch refactors critical KVM components exposed to userspace via `/dev/kvm` and is clearly relevant in virtualized environments. I'm also seeing several new `WARN_ON_ONCE` and `KVM_BUG_ON` assertions. That alone makes this interesting. Based on all this, *WorthFuzzing=true* is absolutely the right call. The patch series is touching all the right areas to be considered for fuzzing. Next, I need to figure out the *EnableConfigs*. This means I need to identify which Kconfig flags are required to enable the relevant KVM features on x86, like `KVM_AMD`, `KVM_INTEL`, and other relevant configurations, to properly set up the fuzzing environment. I should probably use `grepper` or just read the Kconfig files. |
| 4/2 | 2026/10/08 00:29 | tool | grepper |
0mArgs:{
"Expression": "config KVM",
"PathPrefixes": [
"arch/x86/kvm/Kconfig"
]
}Results: {
"Output": "arch/x86/kvm/Kconfig=18=if VIRTUALIZATION\narch/x86/kvm/Kconfig-19-\narch/x86/kvm/Kconfig:20:config KVM_X86\narch/x86/kvm/Kconfig-21-\tdef_tristate KVM if (KVM_INTEL != n || KVM_AMD != n)\n--\narch/x86/kvm/Kconfig-51-\narch/x86/kvm/Kconfig:52:config KVM\narch/x86/kvm/Kconfig-53-\ttristate \"Kernel-based Virtual Machine (KVM) support\"\n--\narch/x86/kvm/Kconfig-68-\narch/x86/kvm/Kconfig:69:config KVM_WERROR\narch/x86/kvm/Kconfig-70-\tbool \"Compile KVM with -Werror\"\n--\narch/x86/kvm/Kconfig-82-\narch/x86/kvm/Kconfig:83:config KVM_VM_MEMORY_ATTRIBUTES\narch/x86/kvm/Kconfig-84-\tbool \"Enable per-VM PRIVATE vs. SHARED attributes (for CoCo VMs)\"\n--\narch/x86/kvm/Kconfig-94-\narch/x86/kvm/Kconfig:95:config KVM_SW_PROTECTED_VM\narch/x86/kvm/Kconfig-96-\tbool \"Enable support for KVM software-protected VMs\"\n--\narch/x86/kvm/Kconfig-106-\narch/x86/kvm/Kconfig:107:config KVM_INTEL\narch/x86/kvm/Kconfig-108-\ttristate \"KVM for Intel (and compatible) processors support\"\n--\narch/x86/kvm/Kconfig-117-\narch/x86/kvm/Kconfig:118:config KVM_INTEL_PROVE_VE\narch/x86/kvm/Kconfig-119- bool \"Check that guests do not receive #VE exceptions\"\n--\narch/x86/kvm/Kconfig=133=config X86_SGX_KVM\n--\narch/x86/kvm/Kconfig-144-\narch/x86/kvm/Kconfig:145:config KVM_INTEL_TDX\narch/x86/kvm/Kconfig-146-\tbool \"Intel Trust Domain Extensions (TDX) support\"\n--\narch/x86/kvm/Kconfig-155-\narch/x86/kvm/Kconfig:156:config KVM_AMD\narch/x86/kvm/Kconfig-157-\ttristate \"KVM for AMD processors support\"\n--\narch/x86/kvm/Kconfig-165-\narch/x86/kvm/Kconfig:166:config KVM_AMD_SEV\narch/x86/kvm/Kconfig-167-\tbool \"AMD Secure Encrypted Virtualization (SEV) support\"\n--\narch/x86/kvm/Kconfig-181-\narch/x86/kvm/Kconfig:182:config KVM_IOAPIC\narch/x86/kvm/Kconfig-183-\tbool \"I/O APIC, PIC, and PIT emulation\"\n--\narch/x86/kvm/Kconfig-191-\narch/x86/kvm/Kconfig:192:config KVM_SMM\narch/x86/kvm/Kconfig-193-\tbool \"System Management Mode emulation\"\n--\narch/x86/kvm/Kconfig-202-\narch/x86/kvm/Kconfig:203:config KVM_HYPERV\narch/x86/kvm/Kconfig-204-\tbool \"Support for Microsoft Hyper-V emulation\"\n--\narch/x86/kvm/Kconfig-216-\narch/x86/kvm/Kconfig:217:config KVM_XEN\narch/x86/kvm/Kconfig-218-\tbool \"Support for Xen hypercall interface\"\n--\narch/x86/kvm/Kconfig-225-\narch/x86/kvm/Kconfig:226:config KVM_PROVE_MMU\narch/x86/kvm/Kconfig-227-\tbool \"Prove KVM MMU correctness\"\n--\narch/x86/kvm/Kconfig-238-\narch/x86/kvm/Kconfig:239:config KVM_EXTERNAL_WRITE_TRACKING\narch/x86/kvm/Kconfig-240-\tbool\narch/x86/kvm/Kconfig-241-\narch/x86/kvm/Kconfig:242:config KVM_MAX_NR_VCPUS\narch/x86/kvm/Kconfig-243-\tint \"Maximum number of vCPUs per KVM guest\"\n"
} |
| 5/2 | 2026/10/08 00:29 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34263 output: 38 thoughts: 24 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Diving into the KVM x86 Build Process** Okay, so I need to understand how the KVM x86 modules are compiled. I'll start with the obvious: the `arch/x86/kvm/Makefile`. That's the go-to place for understanding the build system for any arch-specific components. I'm assuming it'll define the object files, compilation flags, and linking steps, all of which are critical for the kernel's interaction with hardware virtualization extensions. Hopefully, it’s well-structured, not a massive, convoluted mess of `if/else` statements... but I'm prepared for anything. Let's see what secrets it holds. |
| 6/2 | 2026/10/08 00:29 | tool | grepper |
0mArgs:{
"Expression": "svm|vmx|mmu",
"PathPrefixes": [
"arch/x86/kvm/Makefile"
]
}Results: {
"Output": "arch/x86/kvm/Makefile=8=kvm-y\t\t\t+= x86.o emulate.o irq.o lapic.o cpuid.o msrs.o pmu.o regs.o \\\narch/x86/kvm/Makefile:9:\t\t\t mtrr.o debugfs.o mmu/mmu.o mmu/page_track.o mmu/spte.o\narch/x86/kvm/Makefile-10-\narch/x86/kvm/Makefile:11:kvm-$(CONFIG_X86_64) += mmu/tdp_iter.o mmu/tdp_mmu.o\narch/x86/kvm/Makefile-12-kvm-$(CONFIG_KVM_IOAPIC) += i8259.o i8254.o ioapic.o\n--\narch/x86/kvm/Makefile=15=kvm-$(CONFIG_KVM_SMM)\t+= smm.o\narch/x86/kvm/Makefile-16-\narch/x86/kvm/Makefile:17:kvm-intel-y\t\t+= vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \\\narch/x86/kvm/Makefile:18:\t\t\t vmx/nested.o vmx/posted_intr.o vmx/main.o\narch/x86/kvm/Makefile-19-\narch/x86/kvm/Makefile:20:kvm-intel-$(CONFIG_X86_SGX_KVM)\t+= vmx/sgx.o\narch/x86/kvm/Makefile:21:kvm-intel-$(CONFIG_KVM_HYPERV)\t+= vmx/hyperv.o vmx/hyperv_evmcs.o\narch/x86/kvm/Makefile:22:kvm-intel-$(CONFIG_KVM_INTEL_TDX)\t+= vmx/tdx.o\narch/x86/kvm/Makefile-23-\narch/x86/kvm/Makefile:24:kvm-amd-y\t\t+= svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o\narch/x86/kvm/Makefile-25-\narch/x86/kvm/Makefile:26:kvm-amd-$(CONFIG_KVM_AMD_SEV)\t+= svm/sev.o\narch/x86/kvm/Makefile:27:kvm-amd-$(CONFIG_KVM_HYPERV)\t+= svm/hyperv.o\narch/x86/kvm/Makefile-28-\n--\narch/x86/kvm/Makefile=30=kvm-y\t\t\t+= kvm_onhyperv.o\narch/x86/kvm/Makefile:31:kvm-intel-y\t\t+= vmx/vmx_onhyperv.o vmx/hyperv_evmcs.o\narch/x86/kvm/Makefile:32:kvm-amd-y\t\t+= svm/svm_onhyperv.o\narch/x86/kvm/Makefile-33-endif\n--\narch/x86/kvm/Makefile=37=obj-$(CONFIG_KVM_AMD)\t+= kvm-amd.o\narch/x86/kvm/Makefile-38-\narch/x86/kvm/Makefile:39:AFLAGS_svm/vmenter.o := -iquote $(obj)\narch/x86/kvm/Makefile:40:$(obj)/svm/vmenter.o: $(obj)/kvm-asm-offsets.h\narch/x86/kvm/Makefile-41-\narch/x86/kvm/Makefile:42:AFLAGS_vmx/vmenter.o := -iquote $(obj)\narch/x86/kvm/Makefile:43:$(obj)/vmx/vmenter.o: $(obj)/kvm-asm-offsets.h\narch/x86/kvm/Makefile-44-\n"
} |
| 7/2 | 2026/10/08 00:29 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35519 output: 40 thoughts: 400 Thoughts: **Analyzing the Patch's Impact on KVM Modules** Okay, so this patch seems to be touching several key KVM-related files. I see it modifies `mmu/mmu.c`, `x86.c`, `svm/svm.c`, `svm/nested.c`, and `svm/sev.c`. That means we're dealing with interactions between the core `kvm.ko` module, `kvm-amd.ko`, and `kvm-intel.ko` which means, it's touching AMD and Intel support. I need to quickly establish the dependencies for each of those modules. Let me check the `arch/x86/kvm/Kconfig` file. Right, here's what I'm seeing: `KVM_AMD` depends on both `KVM` and either `CPU_SUP_AMD` or `CPU_SUP_HYGON`, which is expected. `KVM_INTEL` similarly depends on `KVM` and `CPU_SUP_INTEL`. Now, the `KVM_AMD_SEV` dependency is interesting. It depends on `KVM_AMD` and also on `X86_64` and `CRYPTO_DEV_SP_PSP`. Let me verify quickly if `CONFIG_KVM_AMD_SEV` is even essential, or if `KVM`, `KVM_AMD`, and `KVM_INTEL` are the main modules to focus on. I need to re-examine the `arch/x86/kvm/Kconfig` file in the vicinity of `KVM_AMD_SEV` to be certain. |
| 8/2 | 2026/10/08 00:30 | tool | grepper |
0mArgs:{
"Expression": "config KVM_AMD_SEV",
"PathPrefixes": [
"arch/x86/kvm/Kconfig"
]
}Results: {
"Output": "arch/x86/kvm/Kconfig=156=config KVM_AMD\n--\narch/x86/kvm/Kconfig-165-\narch/x86/kvm/Kconfig:166:config KVM_AMD_SEV\narch/x86/kvm/Kconfig-167-\tbool \"AMD Secure Encrypted Virtualization (SEV) support\"\n"
} |
| 9/2 | 2026/10/08 00:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 36426 output: 39 thoughts: 28 Thoughts: Okay, let's dive into this Kconfig file, specifically the `arch/x86/kvm/Kconfig` file. I'm focusing on lines 150-180. I suspect this section probably pertains to a key KVM-related configuration option, likely dealing with a specific hardware feature, virtualization extension, or a performance optimization specific to the x86 architecture. Given the context of KVM, I'm anticipating that this segment probably covers options related to enabling or disabling certain hardware-assisted virtualization capabilities, perhaps something involving nested virtualization, or maybe an advanced instruction set extension utilized for guest OS performance. I'm also ready to see some conditionals and dependencies, checking for other kernel features or processor capabilities. It is also possible that it is some general setting to control debugging and logging options. Let's dig in and see what's what. |
| 10/2 | 2026/10/08 00:30 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/Kconfig",
"FirstLine": 150,
"LineCount": 40
}Results: {
"Contents": " 150:\t\thelp\n 151:\t\t Provides support for launching Intel Trust Domain Extensions (TDX)\n 152:\t\t confidential VMs on Intel processors.\n 153:\t\n 154:\t\t If unsure, say N.\n 155:\t\n 156:\tconfig KVM_AMD\n 157:\t\ttristate \"KVM for AMD processors support\"\n 158:\t\tdepends on KVM \u0026\u0026 (CPU_SUP_AMD || CPU_SUP_HYGON)\n 159:\t\thelp\n 160:\t\t Provides support for KVM on AMD processors equipped with the AMD-V\n 161:\t\t (SVM) extensions.\n 162:\t\n 163:\t\t To compile this as a module, choose M here: the module\n 164:\t\t will be called kvm-amd.\n 165:\t\n 166:\tconfig KVM_AMD_SEV\n 167:\t\tbool \"AMD Secure Encrypted Virtualization (SEV) support\"\n 168:\t\tdefault y\n 169:\t\tdepends on KVM_AMD \u0026\u0026 X86_64\n 170:\t\tdepends on CRYPTO_DEV_SP_PSP \u0026\u0026 !(KVM_AMD=y \u0026\u0026 CRYPTO_DEV_CCP_DD=m)\n 171:\t\tselect ARCH_HAS_CC_PLATFORM\n 172:\t\tselect HAVE_KVM_ARCH_GMEM_CONVERT\n 173:\t\tselect HAVE_KVM_ARCH_GMEM_RECLAIM\n 174:\t\tselect HAVE_KVM_ARCH_GMEM_INVALIDATE\n 175:\t\tselect HAVE_KVM_ARCH_GMEM_POPULATE\n 176:\t\thelp\n 177:\t\t Provides support for launching encrypted VMs which use Secure\n 178:\t\t Encrypted Virtualization (SEV), Secure Encrypted Virtualization with\n 179:\t\t Encrypted State (SEV-ES), and Secure Encrypted Virtualization with\n 180:\t\t Secure Nested Paging (SEV-SNP) technologies on AMD processors.\n 181:\t\n 182:\tconfig KVM_IOAPIC\n 183:\t\tbool \"I/O APIC, PIC, and PIT emulation\"\n 184:\t\tdefault y\n 185:\t\tdepends on KVM_X86\n 186:\t\thelp\n 187:\t\t Provides support for KVM to emulate an I/O APIC, PIC, and PIT, i.e.\n 188:\t\t for full in-kernel APIC emulation.\n 189:\t\n"
} |
| 11/2 | 2026/10/08 00:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37331 output: 38 thoughts: 1712 Thoughts: **Focusing Fuzzing on Nested SVM TLB Flushing and ASID Management**
Okay, let's break this down. My goal is to identify the best targets for focused fuzzing based on this patch series related to KVM, specifically the AMD-specific aspects (KVM_AMD), which primarily revolve around SVM ASID management and nested SVM TLB flushes.
First, I need to understand what's actually changed. It seems a common TLB tag management system has been introduced: functions like `kvm_init_tlb_tags`, `kvm_alloc_tlb_tag`, etc., in `mmu.c`. This is then used to refactor VMX VPID management. More significantly, SVM ASID management has been revamped from dynamic allocation to a static allocation with a fallback mechanism. The meat of this, though, appears to be the fixes for nested SVM TLB flushing, handling flushes during nested VMRUN, VMEXIT, and local flushes. This includes `nested_svm_entry_tlb_flush`, `nested_svm_exit_tlb_flush`, `nested_svm_service_local_tlb_flushes`, and the handling of L2 ASID invalidation via `invlpga_interception`, and dedicating `asid02` for nested L2. There's also some SEV guest integration with `sev_vcpu_migrate_asid` and Hyper-V TLB flush integration.
Can syzkaller even *reach* these changes? The good news is that nested SVM and KVM *are* accessible on amd64 in QEMU, either with `-enable-kvm -cpu host` or with nested SVM. Syzkaller uses `/dev/kvm` ioctls extensively, even in QEMU, so the foundation is there.
Now, let's hone in on good focus symbols for fuzzing. The guidance says to avoid the ubiquitous, so I must avoid memory allocators, page fault handlers, synchronization primitives and other hot-paths. I need to pinpoint the *specific* feature logic or entrypoints. Static inline functions are tricky, but I can target their non-static callers.
Here's my initial list of promising functions, based on changes in *.c files, avoiding generic KVM structures or entrypoints:
* **`arch/x86/kvm/mmu/mmu.c`**:
* `kvm_init_tlb_tags`, `kvm_destroy_tlb_tags`
* `kvm_alloc_tlb_tag`, `kvm_free_tlb_tag`
* `__kvm_mmu_invalidate_addr`, `kvm_mmu_invalidate_addr`
* `__kvm_mmu_invlpg`, `kvm_mmu_invlpg`
* **`arch/x86/kvm/svm/nested.c`**:
* `nested_svm_entry_tlb_flush`, `nested_svm_exit_tlb_flush`, `nested_svm_service_local_tlb_flushes` (all static)
* `enter_svm_guest_mode`
* `nested_svm_vmrun`, `nested_svm_vmexit`
* `svm_allocate_nested`, `svm_free_nested`, `svm_leave_nested`
* `svm_set_nested_state`
* **`arch/x86/kvm/svm/svm.c`**:
* `invlpga_interception`
* `svm_flush_tlb_asid`, `svm_flush_tlb_all`, `svm_flush_tlb_current`
* `pre_svm_run`
* **`arch/x86/kvm/svm/sev.c`**:
* `sev_vcpu_migrate_asid`
* `sev_migrate_from`, `pre_sev_run`, `sev_init_vmcb`
* **`arch/x86/kvm/x86.c`**:
* `kvm_vcpu_flush_tlb_guest`
Now for the crucial part: visibility. The static functions like `nested_svm_entry_tlb_flush` are ideal as they represent the *core* logic. Other functions, like `enter_svm_guest_mode`, `nested_svm_vmrun`, etc., seem like good candidates as they aren't static. `invlpga_interception` is also static, but needs
|
| 12/2 | 2026/10/08 00:30 | tool | grepper |
0mArgs:{
"Expression": "invlpga_interception",
"PathPrefixes": [
"arch/x86/kvm/svm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/svm.c=2457=static int clgi_interception(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-2468-\narch/x86/kvm/svm/svm.c:2469:static int invlpga_interception(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-2470-{\n--\narch/x86/kvm/svm/svm.c=3473=static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {\n--\narch/x86/kvm/svm/svm.c-3516-\t[SVM_EXIT_INVLPG]\t\t\t= invlpg_interception,\narch/x86/kvm/svm/svm.c:3517:\t[SVM_EXIT_INVLPGA]\t\t\t= invlpga_interception,\narch/x86/kvm/svm/svm.c-3518-\t[SVM_EXIT_IOIO]\t\t\t\t= io_interception,\n"
} |
| 13/2 | 2026/10/08 00:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40406 output: 67 thoughts: 82 Thoughts: Here's my analysis of the situation: **Symbol Visibility and Potential Inlining** Okay, so `invlpga_interception` is definitely going to have a separate symbol, since it's used in that function pointer array, `svm_exit_handlers`. No inlining there. Good. Now, I need to see how it interacts with other related functions to fully understand the execution flow. I need to dig into the details of the nested SVM implementation. Let's see... I need to examine `nested_svm_vmrun`, `enter_svm_guest_mode`, `nested_svm_vmexit`, `svm_set_nested_state`, and `svm_leave_nested`. I'm going to carefully review the code involving those functions. |
| 14/2 | 2026/10/08 00:30 | tool | grepper |
0mArgs:{
"Expression": "(nested_svm_vmrun|enter_svm_guest_mode|nested_svm_vmexit|svm_allocate_nested|svm_free_nested)",
"PathPrefixes": [
"arch/x86/kvm/svm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/hyperv.c=9=void svm_hv_inject_synthetic_vmexit_post_tlb_flush(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/hyperv.c-21-\tsvm-\u003evmcb-\u003econtrol.exit_info_2 = 0;\narch/x86/kvm/svm/hyperv.c:22:\tnested_svm_vmexit(svm);\narch/x86/kvm/svm/hyperv.c-23-}\n--\narch/x86/kvm/svm/nested.c=38=static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-70-\narch/x86/kvm/svm/nested.c:71:\tnested_svm_vmexit(svm);\narch/x86/kvm/svm/nested.c-72-}\n--\narch/x86/kvm/svm/nested.c=1079=static void nested_svm_service_local_tlb_flushes(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1092-\narch/x86/kvm/svm/nested.c:1093:int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)\narch/x86/kvm/svm/nested.c-1094-{\n--\narch/x86/kvm/svm/nested.c=1148=static int nested_svm_copy_vmcb12_to_cache(struct kvm_vcpu *vcpu, u64 vmcb12_gpa)\n--\narch/x86/kvm/svm/nested.c-1174-\narch/x86/kvm/svm/nested.c:1175:int nested_svm_vmrun(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-1176-{\n--\narch/x86/kvm/svm/nested.c-1240-\narch/x86/kvm/svm/nested.c:1241:\tif (enter_svm_guest_mode(vcpu, vmcb12_gpa, true) ||\narch/x86/kvm/svm/nested.c-1242-\t !nested_svm_merge_msrpm(vcpu)) {\n--\narch/x86/kvm/svm/nested.c-1257-\narch/x86/kvm/svm/nested.c:1258:\t\tnested_svm_vmexit(svm);\narch/x86/kvm/svm/nested.c-1259-\t}\n--\narch/x86/kvm/svm/nested.c=1302=void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)\n--\narch/x86/kvm/svm/nested.c-1317-\narch/x86/kvm/svm/nested.c:1318:static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-1319-{\n--\narch/x86/kvm/svm/nested.c-1385-\narch/x86/kvm/svm/nested.c:1386:void nested_svm_vmexit(struct vcpu_svm *svm)\narch/x86/kvm/svm/nested.c-1387-{\n--\narch/x86/kvm/svm/nested.c-1391-\narch/x86/kvm/svm/nested.c:1392:\tif (nested_svm_vmexit_update_vmcb12(vcpu))\narch/x86/kvm/svm/nested.c-1393-\t\tkvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);\n--\narch/x86/kvm/svm/nested.c=1538=static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1548-\narch/x86/kvm/svm/nested.c:1549:int svm_allocate_nested(struct vcpu_svm *svm)\narch/x86/kvm/svm/nested.c-1550-{\n--\narch/x86/kvm/svm/nested.c-1589-\narch/x86/kvm/svm/nested.c:1590:void svm_free_nested(struct vcpu_svm *svm)\narch/x86/kvm/svm/nested.c-1591-{\n--\narch/x86/kvm/svm/nested.c=1734=int nested_svm_exit_handled(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1740-\tif (vmexit == NESTED_EXIT_DONE)\narch/x86/kvm/svm/nested.c:1741:\t\tnested_svm_vmexit(svm);\narch/x86/kvm/svm/nested.c-1742-\n--\narch/x86/kvm/svm/nested.c=1769=static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1800-\narch/x86/kvm/svm/nested.c:1801:\tnested_svm_vmexit(svm);\narch/x86/kvm/svm/nested.c-1802-}\n--\narch/x86/kvm/svm/svm.c=215=int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)\n--\narch/x86/kvm/svm/svm.c-254-\t\t\tif (!is_smm(vcpu))\narch/x86/kvm/svm/svm.c:255:\t\t\t\tsvm_free_nested(svm);\narch/x86/kvm/svm/svm.c-256-\narch/x86/kvm/svm/svm.c-257-\t\t} else {\narch/x86/kvm/svm/svm.c:258:\t\t\tint ret = svm_allocate_nested(svm);\narch/x86/kvm/svm/svm.c-259-\n--\narch/x86/kvm/svm/svm.c=1405=static void svm_vcpu_free(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-1415-\tsvm_leave_nested(vcpu);\narch/x86/kvm/svm/svm.c:1416:\tsvm_free_nested(svm);\narch/x86/kvm/svm/svm.c-1417-\n--\narch/x86/kvm/svm/svm.c=2325=static int vmrun_interception(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-2329-\narch/x86/kvm/svm/svm.c:2330:\treturn nested_svm_vmrun(vcpu);\narch/x86/kvm/svm/svm.c-2331-}\n--\narch/x86/kvm/svm/svm.c=5209=static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)\n--\narch/x86/kvm/svm/svm.c-5236-\narch/x86/kvm/svm/svm.c:5237:\tif (svm_allocate_nested(svm))\narch/x86/kvm/svm/svm.c-5238-\t\treturn 1;\n--\narch/x86/kvm/svm/svm.c-5259-\narch/x86/kvm/svm/svm.c:5260:\tif (enter_svm_guest_mode(vcpu, smram64-\u003esvm_guest_vmcb_gpa, false) != 0)\narch/x86/kvm/svm/svm.c-5261-\t\treturn 1;\n--\narch/x86/kvm/svm/svm.h=214=struct svm_nested_state {\n--\narch/x86/kvm/svm/svm.h-232-\t * Note: this struct is not kept up-to-date while L2 runs; it is only\narch/x86/kvm/svm/svm.h:233:\t * valid within nested_svm_vmrun.\narch/x86/kvm/svm/svm.h-234-\t */\n--\narch/x86/kvm/svm/svm.h=903=int __init nested_svm_init_msrpm_merge_offsets(void);\narch/x86/kvm/svm/svm.h-904-\narch/x86/kvm/svm/svm.h:905:int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb_gpa, bool from_vmrun);\narch/x86/kvm/svm/svm.h-906-void svm_leave_nested(struct kvm_vcpu *vcpu);\narch/x86/kvm/svm/svm.h:907:void svm_free_nested(struct vcpu_svm *svm);\narch/x86/kvm/svm/svm.h:908:int svm_allocate_nested(struct vcpu_svm *svm);\narch/x86/kvm/svm/svm.h:909:int nested_svm_vmrun(struct kvm_vcpu *vcpu);\narch/x86/kvm/svm/svm.h-910-void svm_copy_vmrun_state(struct kvm_vcpu *vcpu, struct vmcb_save_area *to_save,\n--\narch/x86/kvm/svm/svm.h=912=void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb);\narch/x86/kvm/svm/svm.h:913:void nested_svm_vmexit(struct vcpu_svm *svm);\narch/x86/kvm/svm/svm.h-914-\narch/x86/kvm/svm/svm.h=915=static inline void nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)\n--\narch/x86/kvm/svm/svm.h-919-\tsvm-\u003evmcb-\u003econtrol.exit_info_2\t= 0;\narch/x86/kvm/svm/svm.h:920:\tnested_svm_vmexit(svm);\narch/x86/kvm/svm/svm.h-921-}\n"
} |
| 15/2 | 2026/10/08 00:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43607 output: 39 thoughts: 534 Thoughts: **Analyzing TLB Flushing Mechanisms in KVM/SVM** Okay, let's take a look at these TLB flushing functions in `nested.c`. Specifically, I want to verify how the static functions `nested_svm_entry_tlb_flush`, `nested_svm_exit_tlb_flush`, and `nested_svm_service_local_tlb_flushes` are being handled. Are they inlined or are they separate symbols? It looks like `nested_svm_entry_tlb_flush` is being called by `nested_vmcb02_prepare_control`, which in turn is called by `enter_svm_guest_mode`. Then `nested_svm_exit_tlb_flush` is called by `nested_svm_vmexit` and `svm_leave_nested`. Meanwhile, `nested_svm_service_local_tlb_flushes` is invoked by `enter_svm_guest_mode`, `nested_svm_vmexit`, `svm_leave_nested`, and `svm_set_nested_state`. Interesting. `enter_svm_guest_mode`, `nested_svm_vmrun`, `nested_svm_vmexit`, and `svm_set_nested_state` are direct exports, so they're definitely visible. Next, I need to check out the KVM TLB tag functions. What about `kvm_alloc_tlb_tag`, `kvm_free_tlb_tag`, and `kvm_init_tlb_tags`? Looks like those are in `arch/x86/kvm/mmu/mmu.c`. Yep, they're exported with `EXPORT_SYMBOL_FOR_KVM_INTERNAL`: `kvm_init_tlb_tags`, `kvm_alloc_tlb_tag`, `kvm_free_tlb_tag`, and `kvm_destroy_tlb_tags`. Also, `__kvm_mmu_invlpg` and `__kvm_mmu_invalidate_addr` are exported as well. Finally, I should take a look at `svm_flush_tlb_asid` and `svm_flush_tlb_all` in `svm.c`. Need to see how those are implemented and if they're interacting correctly with the other functions I've examined. |
| 16/2 | 2026/10/08 00:30 | tool | grepper |
0mArgs:{
"Expression": "svm_flush_tlb_",
"PathPrefixes": [
"arch/x86/kvm/svm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/svm.c=4281=static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-4335-\narch/x86/kvm/svm/svm.c:4336:static void svm_flush_tlb_asid(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-4337-{\n--\narch/x86/kvm/svm/svm.c-4356-\narch/x86/kvm/svm/svm.c:4357:static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-4358-{\n--\narch/x86/kvm/svm/svm.c-4369-\narch/x86/kvm/svm/svm.c:4370:\tsvm_flush_tlb_asid(vcpu);\narch/x86/kvm/svm/svm.c-4371-}\narch/x86/kvm/svm/svm.c-4372-\narch/x86/kvm/svm/svm.c:4373:static void svm_flush_tlb_all(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-4374-{\n--\narch/x86/kvm/svm/svm.c-4393-\narch/x86/kvm/svm/svm.c:4394:static void svm_flush_tlb_guest(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-4395-{\n--\narch/x86/kvm/svm/svm.c-4397-\narch/x86/kvm/svm/svm.c:4398:\tsvm_flush_tlb_asid(vcpu);\narch/x86/kvm/svm/svm.c-4399-}\narch/x86/kvm/svm/svm.c-4400-\narch/x86/kvm/svm/svm.c:4401:static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)\narch/x86/kvm/svm/svm.c-4402-{\n--\narch/x86/kvm/svm/svm.c-4415-\narch/x86/kvm/svm/svm.c:4416:\tsvm_flush_tlb_guest(vcpu);\narch/x86/kvm/svm/svm.c-4417-}\n--\narch/x86/kvm/svm/svm.c=5481=struct kvm_x86_ops svm_x86_ops __initdata = {\n--\narch/x86/kvm/svm/svm.c-5534-\narch/x86/kvm/svm/svm.c:5535:\t.flush_tlb_all = svm_flush_tlb_all,\narch/x86/kvm/svm/svm.c:5536:\t.flush_tlb_current = svm_flush_tlb_current,\narch/x86/kvm/svm/svm.c:5537:\t.flush_tlb_gva = svm_flush_tlb_gva,\narch/x86/kvm/svm/svm.c:5538:\t.flush_tlb_guest = svm_flush_tlb_guest,\narch/x86/kvm/svm/svm.c-5539-\n"
} |
| 17/2 | 2026/10/08 00:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 45661 output: 37 thoughts: 57 Thoughts: Here's my summary, as if I were thinking these things: **Investigating AMD SEV TLB Flushing Procedures** Okay, so I'm digging into the AMD Secure Encrypted Virtualization (SEV) code, specifically around TLB (Translation Lookaside Buffer) management. I remember seeing `svm_flush_tlb_all`, `svm_flush_tlb_current`, and other related functions defined within `svm_x86_ops`. That's where I need to start. Now, my next thought is about how ASIDs are managed during migration, especially for SEV-enabled VMs. Let's see how `sev_vcpu_migrate_asid` plays into this. I'll need to understand how the ASID is handled during the migration process. I should also investigate `sev_migrate_from` since it's likely related to migrating the ASID from the source. I need to understand the relationship between these functions and TLB flushes, to ensure a smooth and secure migration. |
| 18/2 | 2026/10/08 00:30 | tool | grepper |
0mArgs:{
"Expression": "sev_migrate_from",
"PathPrefixes": [
"arch/x86/kvm/svm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/sev.c=2035=static void sev_vcpu_migrate_asid(struct vcpu_svm *dst_svm,\n--\narch/x86/kvm/svm/sev.c-2056-\narch/x86/kvm/svm/sev.c:2057:static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)\narch/x86/kvm/svm/sev.c-2058-{\n--\narch/x86/kvm/svm/sev.c=2170=int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)\n--\narch/x86/kvm/svm/sev.c-2232-\narch/x86/kvm/svm/sev.c:2233:\tsev_migrate_from(kvm, source_kvm);\narch/x86/kvm/svm/sev.c-2234-\tkvm_vm_dead(source_kvm);\n"
} |
| 19/2 | 2026/10/08 00:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46261 output: 42 thoughts: 1064 Thoughts: **Focusing Fuzzing Targets in KVM Nested SVM**
Okay, let's break this down. My goal is to identify good targets for fuzzing within the KVM nested SVM code. I've been tracing the flow and thinking about how these functions are reached.
First, I'm noting that `sev_vm_move_enc_context_from` calls `sev_migrate_from`, which then calls `sev_vcpu_migrate_asid`. That's a migration path, and an interesting starting point to consider.
I need to confirm the *interception* of the `invlpga` instruction in guest mode while running under SVM. I see `invlpga_interception` handles this, so that's definitely a candidate. The core nested SVM functions like `nested_svm_vmrun`, `nested_svm_vmexit`, `svm_set_nested_state`, and `enter_svm_guest_mode` are also in my field of vision. Furthermore, I'm adding TLB invalidation functions, such as `kvm_alloc_tlb_tag`, `kvm_free_tlb_tag`, `__kvm_mmu_invlpg`, `__kvm_mmu_invalidate_addr`, `svm_flush_tlb_all`, and `svm_flush_tlb_asid` into the mix.
Now, let's refine this list of "FocusSymbols". The idea is to pick functions that are *specific* and not part of the common kernel code. The goal is to maximize the impact of the fuzzing.
Let's check the candidates:
* `nested_svm_vmrun` - Only called on nested VMRUN. Good!
* `nested_svm_vmexit` - Only called on nested VMEXIT. Also good.
* `enter_svm_guest_mode` - Only called when entering nested guest mode. Excellent.
* `svm_set_nested_state` - Only via ioctl KVM_SET_NESTED_STATE. Perfect.
* `invlpga_interception` - Only on INVLPGA instruction interception. Another good candidate.
* `kvm_alloc_tlb_tag` - Called on vCPU create or nested alloc or vmx init. Specific, yes.
* `kvm_free_tlb_tag` - Called on vCPU free or nested free.
* `__kvm_mmu_invlpg` - Called on invlpg/invlpga.
* `svm_flush_tlb_asid` - Called on svm tlb flush.
* `svm_flush_tlb_all` - Called on svm tlb flush all.
* `svm_allocate_nested` - Called on EFER.SVME toggle or smm.
* `svm_free_nested` - Called on nested teardown.
* `sev_migrate_from` - Also a good candidate, related to memory migration.
Alright, it looks like all of these functions are *specific* to KVM nested SVM, ASID management, or TLB invalidation. That's a useful set to focus on. They aren't ubiquitous in the kernel.
Next, I have to figure out the required configurations for the code to even be compiled and reachable. I can't fuzz code that isn't built. I see:
* `KVM` (Kernel-based Virtual Machine support) is definitely required.
* `KVM_AMD` (KVM for AMD processors support), given the focus on SVM nested ASID changes.
* `KVM_INTEL` (KVM for Intel processors) is useful here because of the `allocate_vpid` and `kvm_init_tlb_tags`
* `KVM_AMD_SEV` looks important because `sev.c` is guarded by `CONFIG_KVM_AMD_SEV`.
* This has dependencies:
* `KVM_AMD`
* `X86_64`
* `CRYPTO_DEV_SP_PSP`
* `!(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m)`
* `KVM_HYPERV` might be interesting. Even though nested SVM is AMD-centric, I see some TLB flush functions in `hyperv.h` - `kvm_hv_purge_tlb_flush_fifo`, `kvm_hv_purge_all_tlb_flush_fifos`.
The key here seems to be that `KVM`, `KVM_AMD`, and `
|
| 20/2 | 2026/10/08 00:31 | tool | grepper |
0mArgs:{
"Expression": "CONFIG_KVM_HYPERV",
"PathPrefixes": [
"arch/x86/kvm/hyperv.h"
]
}Results: {
"Output": "arch/x86/kvm/hyperv.h-27-\narch/x86/kvm/hyperv.h:28:#ifdef CONFIG_KVM_HYPERV\narch/x86/kvm/hyperv.h-29-\n--\narch/x86/kvm/hyperv.h=394=int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu);\narch/x86/kvm/hyperv.h:395:#else /* CONFIG_KVM_HYPERV */\narch/x86/kvm/hyperv.h-396-static inline void kvm_hv_setup_tsc_page(struct kvm *kvm,\n--\narch/x86/kvm/hyperv.h=443=static inline void kvm_hv_nested_transtion_tlb_flush(struct kvm_vcpu *vcpu, bool tdp_enabled) {}\narch/x86/kvm/hyperv.h:444:#endif /* CONFIG_KVM_HYPERV */\narch/x86/kvm/hyperv.h-445-\n"
} |
| 21/2 | 2026/10/08 00:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48688 output: 356 thoughts: 695 Thoughts: **Fuzzing Target: KVM x86 with SVM and VMX Enhancements** Okay, so looking at this patch, my initial thought is that `CONFIG_KVM_HYPERV` guards some specific implementations in `hyperv.h`. This means `KVM_HYPERV` is a key consideration. I confirmed this by checking `arch/x86/kvm/Kconfig` – `KVM_HYPERV` is enabled by default if KVM is enabled. Therefore, for EnableConfigs, I'll flag `KVM`, `KVM_AMD`, `KVM_INTEL`, `KVM_AMD_SEV`, and `KVM_HYPERV`. Now, digging deeper, I see a clear reason to prioritize fuzzing. The patch series introduces a significant amount of rework. Specifically, there's a unified TLB tag allocator for x86 KVM. This includes functions like `kvm_init_tlb_tags`, `kvm_alloc_tlb_tag`, and so on. They've converted Intel VMX VPID and AMD SVM ASID management to use this new allocator, which is a major shift. The nested SVM ASID allocation has also been reworked, with a dedicated hardware ASID `asid02` for nested L2 guests. Furthermore, nested SVM TLB flushing and MMU synchronization have been reworked, specifically with `KVM_REQ_MMU_SYNC_ALL_ROOTS`. The logic for INVLPGA instruction emulation and interception is also being modified. Finally, they're adding several new runtime assertions using `KVM_BUG_ON` and `WARN_ON_ONCE`. All of this modified code is absolutely reachable because it's part of the KVM subsystem on x86, accessible via standard `/dev/kvm` ioctls and guest instruction execution. This makes it a prime candidate for fuzzing. My reasoning is straightforward: We're looking at KVM on x86, with specific focus on AMD SVM and Intel VMX. The changes involve reworking ASID and VPID management with a new TLB tag allocator. There are also significant changes to nested SVM TLB flushes, MMU root synchronization, and INVLPGA interception, with multiple state-tracking and assertion updates. The code is entirely reachable through standard `/dev/kvm` ioctls and nested virtualization execution paths. The new `WARN_ON_ONCE` and `KVM_BUG_ON` checks are especially interesting because they indicate a focus on unexpected guest states and transitions, which is *exactly* what fuzzing excels at uncovering. I've selected the following FocusSymbols. They appear appropriate. * `nested_svm_vmrun` * `nested_svm_vmexit` * `enter_svm_guest_mode` * `svm_set_nested_state` * `invlpga_interception` * `svm_flush_tlb_asid` * `svm_flush_tlb_all` * `kvm_alloc_tlb_tag` * `kvm_free_tlb_tag` * `__kvm_mmu_invlpg` * `__kvm_mmu_invalidate_addr` * `svm_allocate_nested` * `svm_free_nested` * `sev_migrate_from` These look to be all valid, non-ubiquitous functions that are directly implementing or wrapping the altered logic. Time to invoke the `set-results` tool and move forward. |
| 22/2 | 2026/10/08 00:31 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"KVM",
"KVM_AMD",
"KVM_INTEL",
"KVM_AMD_SEV",
"KVM_HYPERV"
],
"FocusSymbols": [
"nested_svm_vmrun",
"nested_svm_vmexit",
"enter_svm_guest_mode",
"svm_set_nested_state",
"invlpga_interception",
"svm_flush_tlb_asid",
"svm_flush_tlb_all",
"kvm_alloc_tlb_tag",
"kvm_free_tlb_tag",
"__kvm_mmu_invlpg",
"__kvm_mmu_invalidate_addr",
"svm_allocate_nested",
"svm_free_nested",
"sev_migrate_from"
],
"Reasoning": "The patch series introduces a shared TLB tag management framework across x86 KVM, refactors VPID allocation on Intel VMX and ASID management on AMD SVM, reworks nested SVM TLB flushing and MMU synchronization (including a new KVM_REQ_MMU_SYNC_ALL_ROOTS request and dedicated asid02 allocation for nested guests), updates INVLPGA instruction interception handling, and introduces multiple new runtime assertions (WARN_ON_ONCE, KVM_BUG_ON). These changes modify reachable virtualization paths exposed via /dev/kvm ioctls and nested SVM execution, warranting fuzzing for potential state desynchronization, memory leaks, and assertion violations.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"KVM",
"KVM_AMD",
"KVM_INTEL",
"KVM_AMD_SEV",
"KVM_HYPERV"
],
"FocusSymbols": [
"nested_svm_vmrun",
"nested_svm_vmexit",
"enter_svm_guest_mode",
"svm_set_nested_state",
"invlpga_interception",
"svm_flush_tlb_asid",
"svm_flush_tlb_all",
"kvm_alloc_tlb_tag",
"kvm_free_tlb_tag",
"__kvm_mmu_invlpg",
"__kvm_mmu_invalidate_addr",
"svm_allocate_nested",
"svm_free_nested",
"sev_migrate_from"
],
"Reasoning": "The patch series introduces a shared TLB tag management framework across x86 KVM, refactors VPID allocation on Intel VMX and ASID management on AMD SVM, reworks nested SVM TLB flushing and MMU synchronization (including a new KVM_REQ_MMU_SYNC_ALL_ROOTS request and dedicated asid02 allocation for nested guests), updates INVLPGA instruction interception handling, and introduces multiple new runtime assertions (WARN_ON_ONCE, KVM_BUG_ON). These changes modify reachable virtualization paths exposed via /dev/kvm ioctls and nested SVM execution, warranting fuzzing for potential state desynchronization, memory leaks, and assertion violations.",
"WorthFuzzing": true
} |
| 23/1 | 2026/10/08 00:31 | action | If |
3mArgs:{
"WorthFuzzing": true
}Results: null |
| 24/2 | 2026/10/08 00:31 | agent | kmsan-evaluator |
3mModel:core Args: null Results: {
"KMSANReasoning": "The patch series refactors KVM x86 TLB flushing, unifies TLB tag (VPID/ASID) allocation between VMX and SVM via bitmap_zalloc, improves nested SVM ASID tracking and TLB invalidation handling, and introduces KVM_REQ_MMU_SYNC_ALL_ROOTS.\n\nKMSAN vs KASAN applicability:\n1. No uninitialized memory usage:\n - The newly allocated bitmap in `kvm_init_tlb_tags` is allocated using `bitmap_zalloc()`, which zeroes all allocated memory.\n - New struct members in `vcpu_svm` and `svm_nested_state` (`asid02`, `last_asid`) belong to `vcpu_svm`, which is allocated via `kmem_cache_zalloc()`, ensuring they are zero-initialized.\n - All stack local variables in modified/added functions (such as `new_asid`, `bit`, `nr`, `fifo`) are explicitly initialized before being read or used in conditional logic.\n2. No kernel-to-user info-leaks:\n - The patch does not modify or introduce any data copied to userspace (no `copy_to_user`, `put_user`, or ioctl output alterations).\n3. Potential bug surfaces:\n - Potential errors in this series relate to TLB synchronization correctness, logic flaws in ASID reuse, concurrency, or locking, which are either architectural emulation correctness issues or covered by KASAN and LOCKDEP.\n\nBecause no uninitialized memory risks or info-leaks are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 15e9e385bcc0e8a4bbd6ce129c9a926c3d78c56c
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Oct 8 00:29:28 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index ddc61ea36ac59..60f49b3675920 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -9770,6 +9770,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 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index e2eef7057bd35..1d28bb7e92c7f 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -127,6 +127,7 @@
KVM_ARCH_REQ_FLAGS(33, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE \
KVM_ARCH_REQ_FLAGS(34, KVM_REQUEST_WAIT)
+#define KVM_REQ_MMU_SYNC_ALL_ROOTS KVM_ARCH_REQ(35)
#define INVALID_PAGE (~(hpa_t)0)
#define VALID_PAGE(x) ((x) != INVALID_PAGE)
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index adafd6fa2c542..e45c01d6faeaf 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -1846,8 +1846,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;
diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h
index 622a6553e9ac2..b82f70e6fcea1 100644
--- a/arch/x86/kvm/hyperv.h
+++ b/arch/x86/kvm/hyperv.h
@@ -312,18 +312,34 @@ 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)
+/* 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;
+
+ fifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu));
+ if (fifo)
+ __kvm_hv_purge_tlb_flush_fifo(vcpu, fifo);
+}
- kfifo_reset_out(&tlb_flush_fifo->entries);
+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)
@@ -396,7 +412,8 @@ 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 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;
diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
index 2ae7f9ed4cf86..b5100f31f6a35 100644
--- a/arch/x86/kvm/mmu.h
+++ b/arch/x86/kvm/mmu.h
@@ -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);
@@ -170,6 +173,13 @@ void kvm_mmu_unload(struct kvm_vcpu *vcpu);
void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu);
void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu);
+
+static inline void kvm_mmu_sync_all_roots(struct kvm_vcpu *vcpu)
+{
+ kvm_mmu_sync_roots(vcpu);
+ kvm_mmu_sync_prev_roots(vcpu);
+}
+
void kvm_mmu_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
int bytes);
@@ -410,4 +420,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_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);
+
#endif
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index aadd5bdc2c97a..bfc3dd08bedb0 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -6732,8 +6732,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;
@@ -6769,8 +6769,8 @@ static void __kvm_mmu_invalidate_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)
+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;
@@ -6783,7 +6783,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;
@@ -6798,16 +6799,23 @@ 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);
+
+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)
+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,
@@ -6819,11 +6827,17 @@ 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);
+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)
{
@@ -8178,6 +8192,98 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm)
vhost_task_stop(kvm->arch.nx_huge_page_recovery_thread);
}
+static struct {
+ spinlock_t lock;
+ unsigned long *bitmap;
+ unsigned int nr;
+ kvm_tlb_tag_t start;
+} tlb_tags;
+
+/*
+ * Initialize @nr_total TLB tags, where @nr_reserved of those tags are reserved.
+ * Reserved tags start at 0 and are contiguous. Hence, usable tags are
+ * [nr_reserved, nr_total). At least one reserved tag is required, as 0 is the
+ * host TLB tag on both VMX and AMD, and is the value returned on a failed tag
+ * allocation.
+ */
+int kvm_init_tlb_tags(unsigned int nr_total, unsigned int nr_reserved)
+{
+ /*
+ * Limit the number of TLB tags to VMX's hardcoded maximum of 0x10000
+ * to avoid wasting memory for the bitmap in the unlikely scenario the
+ * CPU supports an inordinate number of ASIDs (on AMD). If userspace
+ * wants to concurrently run tens of thousands of vCPUs, they'll likely
+ * need a solution that works for both Intel and AMD.
+ */
+ const unsigned int MAX_NR_TLB_TAGS = VMX_NR_VPIDS;
+ unsigned int nr;
+
+ if (WARN_ON_ONCE(!nr_reserved || nr_reserved > nr_total))
+ return -EINVAL;
+
+ nr = nr_total - nr_reserved;
+ if (!nr)
+ return 0;
+
+ if (nr > MAX_NR_TLB_TAGS) {
+ pr_warn_once("Number of TLB tags capped (%u instead of %u)\n",
+ MAX_NR_TLB_TAGS, nr);
+ nr = MAX_NR_TLB_TAGS;
+ }
+
+ tlb_tags.bitmap = bitmap_zalloc(nr, GFP_KERNEL);
+ if (!tlb_tags.bitmap)
+ return -ENOMEM;
+
+ tlb_tags.nr = nr;
+ tlb_tags.start = nr_reserved;
+ spin_lock_init(&tlb_tags.lock);
+ return 0;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_init_tlb_tags);
+
+void kvm_destroy_tlb_tags(void)
+{
+ bitmap_free(tlb_tags.bitmap);
+ tlb_tags.bitmap = NULL;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_destroy_tlb_tags);
+
+kvm_tlb_tag_t kvm_alloc_tlb_tag(void)
+{
+ unsigned int bit;
+
+ if (!tlb_tags.bitmap)
+ return 0;
+
+ guard(spinlock)(&tlb_tags.lock);
+
+ bit = find_first_zero_bit(tlb_tags.bitmap, tlb_tags.nr);
+ if (bit >= tlb_tags.nr)
+ return 0;
+
+ __set_bit(bit, tlb_tags.bitmap);
+ return tlb_tags.start + bit;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_alloc_tlb_tag);
+
+void kvm_free_tlb_tag(kvm_tlb_tag_t tag)
+{
+ unsigned int bit;
+
+ if (!tag || WARN_ON_ONCE(tag < tlb_tags.start))
+ return;
+
+ bit = tag - tlb_tags.start;
+ if (WARN_ON_ONCE(bit >= tlb_tags.nr))
+ return;
+
+ guard(spinlock)(&tlb_tags.lock);
+
+ __clear_bit(bit, tlb_tags.bitmap);
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_free_tlb_tag);
+
#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES
static bool hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn,
int level)
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index af0ef678433d7..6fe68f2ae282d 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -556,10 +556,8 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
to->nested_cr3 = from->nested_cr3;
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,26 +686,69 @@ 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)
{
+ 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;
+ }
+
/*
- * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
- * things to fix before this can be conditional:
+ * 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. Sync all roots, since the TLB flush is not
+ * specific to the currently loaded nCR3.
+ *
+ * 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.
+ *
+ * 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.
*
- * - 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
- * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
+ * 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.
*
- * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
- * NPT guest-physical mappings on VMRUN.
+ * TLB_CONTROL_FLUSH_ASID and TLB_CONTROL_FLUSH_ASID_LOCAL are handled
+ * equally for simplicity.
*/
- kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
- kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+ if (new_asid || (svm->nested.ctl.tlb_ctl != TLB_CONTROL_DO_NOTHING)) {
+ if (nested_npt_enabled(svm))
+ kvm_make_request(KVM_REQ_MMU_SYNC_ALL_ROOTS, 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);
+ }
+}
+
+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 requested 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);
+
+ if (svm->asid == svm->nested.asid02)
+ kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
}
/*
@@ -860,7 +901,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);
@@ -936,10 +977,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
else
vmcb02->control.bus_lock_counter = 0;
- /* Done at vmrun: asid. */
-
- /* Also overwritten later if necessary. */
- vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+ vmcb02->control.asid = svm->nested.asid02;
/* Use vmcb01 MMU and format if guest does not use nNPT */
if (nested_npt_enabled(svm)) {
@@ -1038,6 +1076,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);
@@ -1069,6 +1121,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);
@@ -1142,7 +1196,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);
@@ -1194,6 +1248,13 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)
svm->vmcb->control.exit_info_1 = 0;
svm->vmcb->control.exit_info_2 = 0;
+ /*
+ * Force a flush+sync on the next nested VMRUN. Otherwise,
+ * pending MMU syncs due to a changed ASID are lost, as
+ * last_asid was already updated.
+ */
+ svm->nested.last_asid = 0;
+
nested_svm_vmexit(svm);
}
@@ -1331,7 +1392,9 @@ 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 */
+ nested_svm_service_local_tlb_flushes(vcpu);
+
+ svm_switch_vmcb(svm, &svm->vmcb01);
leave_guest_mode(vcpu);
svm_pmu_handle_nested_transition(svm);
@@ -1358,8 +1421,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:
*
@@ -1438,7 +1499,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);
@@ -1489,21 +1550,36 @@ 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.cpu = -1;
- svm->nested.initialized = true;
+ svm->nested.vmcb02.ptr = page_address(vmcb02_page);
+ svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
+
+ 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 ASID (e.g. different vCPU or even different VM) could be used.
+ */
+ svm->nested.last_asid = 0;
+
return 0;
err_free_vmcb02:
@@ -1513,12 +1589,14 @@ 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))
svm_switch_vmcb(svm, &svm->vmcb01);
+ free_asid(svm->nested.asid02);
+
svm_vcpu_free_msrpm(svm->nested.msrpm);
svm->nested.msrpm = NULL;
@@ -1533,8 +1611,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)
@@ -1545,6 +1621,9 @@ 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);
/*
@@ -1556,7 +1635,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
*/
__svm_pmu_handle_nested_transition(svm, true);
- svm_switch_vmcb(svm, &svm->vmcb01);
+ nested_svm_exit_tlb_flush(vcpu);
nested_svm_uninit_mmu_context(vcpu);
vmcb_mark_all_dirty(svm->vmcb);
@@ -1853,7 +1932,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)
{
@@ -2084,6 +2163,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
svm_copy_vmrun_state(vcpu, &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)
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index fad72cab76cf4..646108e70e21a 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -315,11 +315,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;
@@ -2037,6 +2032,28 @@ 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,
+ unsigned int asid)
+{
+ /*
+ * Free the (potentially non-SEV) ASID on the destination vCPU before
+ * setting the new SEV ASID.
+ */
+ free_asid(dst_svm->asid);
+ dst_svm->asid = asid;
+
+ /*
+ * If nested is already initialized on the destination vCPU, also free
+ * the previous ASID (in case it was non-SEV) and update the nested ASID
+ * to match the new SEV ASID.
+ */
+ if (dst_svm->nested.vmcb02.ptr) {
+ free_asid(dst_svm->nested.asid02);
+ 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);
@@ -2101,6 +2118,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);
+ sev_vcpu_migrate_asid(dst_svm, dst->asid);
sev_init_vmcb(dst_svm, false);
if (!dst->es_active)
@@ -3623,9 +3641,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;
-
/*
* Flush guest TLB:
*
@@ -3637,7 +3652,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);
return 0;
}
@@ -4889,6 +4904,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);
+ WARN_ON_ONCE(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.
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 136069dfcd44f..4b95a05c37f60 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -192,6 +192,8 @@ DEFINE_PER_CPU(struct svm_cpu_data, svm_data);
static DEFINE_MUTEX(vmcb_dump_mutex);
+kvm_tlb_tag_t fallback_asid;
+
/*
* Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
* the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
@@ -583,15 +585,6 @@ static int svm_enable_virtualization_cpu(void)
return r;
sd = per_cpu_ptr(&svm_data, me);
- /*
- * Bump the current asid_generation value to ensure any vCPU that
- * previously ran on this CPU sees a stale generation and is forced
- * to acquire a new ASID, preventing a latent ASID collision.
- */
- sd->asid_generation++;
- sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
- sd->next_asid = sd->max_asid + 1;
- sd->min_asid = max_sev_asid + 1;
wrmsrq(MSR_VM_HSAVE_PA, sd->save_area_pa);
@@ -994,6 +987,8 @@ static void svm_hardware_unsetup(void)
__free_pages(__sme_pa_to_page(iopm_base), get_order(IOPM_SIZE));
iopm_base = 0;
+
+ kvm_destroy_tlb_tags();
}
static void init_seg(struct vmcb_seg *seg)
@@ -1246,8 +1241,8 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
if (gmet_enabled)
control->misc_ctl |= SVM_MISC_ENABLE_GMET;
- svm->current_vmcb->asid_generation = 0;
- svm->asid = 0;
+ control->asid = svm->asid;
+ vmcb_set_flush_asid(vmcb);
svm->nested.vmcb12_gpa = INVALID_GPA;
svm->nested.last_vmcb12_gpa = INVALID_GPA;
@@ -1375,6 +1370,12 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
goto error_free_avic;
}
+ svm->asid = allocate_asid(vcpu);
+ if (!svm->asid) {
+ err = -EINVAL;
+ goto error_free_msrpm;
+ }
+
svm->x2avic_msrs_intercepted = true;
svm->lbr_msrs_intercepted = true;
@@ -1386,6 +1387,8 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
return 0;
+error_free_msrpm:
+ svm_vcpu_free_msrpm(svm->msrpm);
error_free_avic:
avic_vcpu_free(vcpu);
error_free_sev:
@@ -1402,6 +1405,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));
@@ -1417,6 +1422,13 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)
__free_page(__sme_pa_to_page(svm->vmcb01.pa));
svm_vcpu_free_msrpm(svm->msrpm);
+
+ free_asid(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
@@ -1942,18 +1954,6 @@ static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
}
}
-static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
-{
- if (sd->next_asid > sd->max_asid) {
- ++sd->asid_generation;
- sd->next_asid = sd->min_asid;
- sd->flush_all_asids = true;
- }
-
- svm->current_vmcb->asid_generation = sd->asid_generation;
- svm->asid = sd->next_asid++;
-}
-
static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
{
struct vmcb *vmcb = to_svm(vcpu)->vmcb;
@@ -2468,17 +2468,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,
+ * assume the 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 its 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);
}
@@ -3875,37 +3909,28 @@ 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 = per_cpu_ptr(&svm_data, vcpu->cpu);
+ struct svm_cpu_data *sd = this_cpu_ptr(&svm_data);
struct vcpu_svm *svm = to_svm(vcpu);
/*
* If the previous VMRUN of the VMCB occurred on a different physical
- * cpu, then mark the VMCB dirty as hardware's clean bits are per pCPU.
- *
- * Reset the ASID generation in both VMCBs. This will lead to assigning
- * a new ASID now, and then again when switching to the other VMCB.
- * However, this is needed as the ASID is shared between the VMCBs, and
- * otherwise it would be possible to use an ASID allocated on one pCPU
- * on another, for example:
- * - vCPU migrates from pCPU A to pCPU B, allocates a new ASID.
- * - vCPU migrates back to pCPU A, and then switches the VMCB.
- * - The new VMCB does not detect a pCPU change and runs on pCPU A with
- * the new ASID allocated on pCPU B, which is potentially used by
- * another vCPU/VM.
+ * cpu, then mark the VMCB dirty and flush the ASID, as hardware's clean
+ * bits and TLB entries are per pCPU.
*/
if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
vmcb_mark_all_dirty(svm->vmcb);
svm->current_vmcb->cpu = vcpu->cpu;
- svm->vmcb01.asid_generation = 0;
- svm->nested.vmcb02.asid_generation = 0;
+ vmcb_set_flush_asid(svm->vmcb);
}
if (is_sev_guest(vcpu))
return pre_sev_run(svm, vcpu->cpu);
- /* FIXME: handle wraparound of asid_generation */
- if (svm->current_vmcb->asid_generation != sd->asid_generation)
- new_asid(svm, sd);
+ 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;
}
@@ -4317,7 +4342,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
@@ -4326,10 +4351,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 (cpu_feature_enabled(X86_FEATURE_FLUSHBYASID))
- svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
- else
- svm->current_vmcb->asid_generation--;
+ vmcb_set_flush_asid(svm->vmcb);
}
static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)
@@ -4350,6 +4372,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
@@ -4360,7 +4384,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_guest(struct kvm_vcpu *vcpu)
@@ -4647,13 +4675,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);
- }
- if (this_cpu_ptr(&svm_data)->flush_all_asids)
- svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
-
svm->vmcb->save.cr2 = vcpu->arch.cr2;
if (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS) &&
@@ -4745,8 +4766,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
}
if (!svm_is_vmrun_failure(svm->vmcb->control.exit_code)) {
- this_cpu_ptr(&svm_data)->flush_all_asids = false;
- 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
@@ -5659,9 +5679,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);
@@ -5757,6 +5776,7 @@ static __init void svm_set_cpu_caps(void)
static __init int svm_hardware_setup(void)
{
+ unsigned long nr_asids;
void *iopm_va;
int cpu, r;
@@ -5914,6 +5934,14 @@ static __init int svm_hardware_setup(void)
kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_CD_NW_CLEARED;
+ /* Consumes max_sev_asid initialized by sev_hardware_setup() */
+ nr_asids = cpuid_ebx(SVM_CPUID_FUNC);
+ r = kvm_init_tlb_tags(nr_asids, max_sev_asid + 1);
+ if (r)
+ goto err;
+
+ fallback_asid = kvm_alloc_tlb_tag();
+
for_each_possible_cpu(cpu) {
r = svm_cpu_init(cpu);
if (r)
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 6d0242966dc87..15c4aa3b02c2d 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -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
@@ -145,7 +146,6 @@ struct kvm_vmcb_info {
struct vmcb *ptr;
unsigned long pa;
int cpu;
- uint64_t asid_generation;
};
struct vmcb_save_area_cached {
@@ -234,7 +234,7 @@ struct svm_nested_state {
*/
struct vmcb_save_area_cached save;
- bool initialized;
+ kvm_tlb_tag_t asid02;
/*
* Indicates whether MSR bitmap for L2 needs to be rebuilt due to
@@ -244,6 +244,8 @@ struct svm_nested_state {
* on its side.
*/
bool force_msr_bitmap_recalc;
+
+ u32 last_asid;
};
struct vcpu_sev_es_state {
@@ -285,7 +287,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;
@@ -372,17 +374,14 @@ struct vcpu_svm {
};
struct svm_cpu_data {
- u64 asid_generation;
- u32 max_asid;
- u32 next_asid;
- u32 min_asid;
-
- bool flush_all_asids;
bool bp_spec_reduce_set;
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;
};
@@ -432,6 +431,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)
{
@@ -446,6 +450,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)
@@ -480,6 +489,19 @@ 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)
+{
+ if (cpu_feature_enabled(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)
+{
+ 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);
@@ -1057,6 +1079,21 @@ static inline struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu)
static inline void sev_free_decrypted_vmsa(struct kvm_vcpu *vcpu, struct vmcb_save_area *vmsa) {}
#endif
+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(kvm_tlb_tag_t asid)
+{
+ if (asid > max_sev_asid && asid != fallback_asid)
+ kvm_free_tlb_tag(asid);
+}
+
/* vmenter.S */
void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, unsigned int flags,
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b341af4c6057a..a1caa2bd04d7a 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -598,9 +598,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;
@@ -4092,31 +4089,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)
{
/*
@@ -8415,6 +8387,8 @@ void vmx_hardware_unsetup(void)
if (nested)
nested_vmx_hardware_unsetup();
+
+ destroy_vpids();
}
void vmx_vm_destroy(struct kvm *kvm)
@@ -8640,8 +8614,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
@@ -8681,6 +8653,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
@@ -8688,8 +8664,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;
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 6c9dd5b2365d5..3e1ecd9148e9c 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -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;
@@ -297,9 +297,29 @@ struct kvm_vmx {
u64 *pid_table;
};
+static __always_inline int init_vpids(void)
+{
+ return enable_vpid ? kvm_init_tlb_tags(VMX_NR_VPIDS, 1) : 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,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 32563a91fba99..a54e9da2ed09e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2020,8 +2020,7 @@ static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
* a forced sync of the shadow page tables. Ensure all the
* roots are synced and the guest TLB in hardware is clean.
*/
- kvm_mmu_sync_roots(vcpu);
- kvm_mmu_sync_prev_roots(vcpu);
+ kvm_mmu_sync_all_roots(vcpu);
}
kvm_x86_call(flush_tlb_guest)(vcpu);
@@ -2030,7 +2029,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);
}
@@ -8167,8 +8166,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
if (unlikely(r))
goto out;
}
- if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
+
+ if (kvm_check_request(KVM_REQ_MMU_SYNC_ALL_ROOTS, vcpu)) {
+ kvm_mmu_sync_all_roots(vcpu);
+ kvm_clear_request(KVM_REQ_MMU_SYNC, vcpu);
+ } else if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) {
kvm_mmu_sync_roots(vcpu);
+ }
+
if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu))
kvm_mmu_load_pgd(vcpu);
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index be5eb2dd85aa2..5347c56786b08 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -122,6 +122,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
diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index ffec2291b1e1f..62d255aee5a34 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -1617,6 +1617,7 @@ void tdp_map(struct kvm_vm *vm, gpa_t l2_gpa, gpa_t gpa, u64 size);
void tdp_identity_map_default_memslots(struct kvm_vm *vm);
void tdp_identity_map_1g(struct kvm_vm *vm, u64 addr, u64 size);
u64 *tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa);
+gpa_t tdp_copy_page_tables(struct kvm_vm *vm);
/*
* Basic CPU control in CR0
diff --git a/tools/testing/selftests/kvm/include/x86/svm.h b/tools/testing/selftests/kvm/include/x86/svm.h
index c8539166270ea..7644e37c7042c 100644
--- a/tools/testing/selftests/kvm/include/x86/svm.h
+++ b/tools/testing/selftests/kvm/include/x86/svm.h
@@ -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 */
diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
index 8fd97434eee5c..4f2a0e3ac5ba1 100644
--- a/tools/testing/selftests/kvm/include/x86/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86/vmx.h
@@ -518,4 +518,25 @@ static inline bool kvm_cpu_has_vmx_virtualize_apic_accesses(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 */
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index 8374afd08bafe..57ecf8b381b5a 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -554,6 +554,42 @@ void tdp_identity_map_1g(struct kvm_vm *vm, u64 addr, u64 size)
__tdp_map(vm, addr, addr, size, PG_LEVEL_1G);
}
+static gpa_t __virt_copy_page_tables(struct kvm_vm *vm, struct kvm_mmu *mmu,
+ gpa_t table_gpa, int level)
+{
+ gpa_t copy_gpa = vm_alloc_page_table(vm);
+ u64 *copy = addr_gpa2hva(vm, copy_gpa);
+ gpa_t pa;
+ int i;
+
+ memcpy(copy, addr_gpa2hva(vm, table_gpa), vm->page_size);
+ if (level == PG_LEVEL_4K)
+ return copy_gpa;
+
+ for (i = 0; i < vm->page_size / sizeof(u64); i++) {
+ if (!is_present_pte(mmu, ©[i]) || is_huge_pte(mmu, ©[i]))
+ continue;
+
+ /* Clear only the address bits, preserving C/S bits and flags */
+ pa = vm_untag_gpa(vm, PTE_GET_PA(copy[i]));
+ copy[i] &= ~pa;
+ copy[i] |= __virt_copy_page_tables(vm, mmu, pa, level - 1);
+ }
+ return copy_gpa;
+}
+
+/*
+ * Deep copy all levels of the TDP page tables, such that no page table pages
+ * are shared with the original. Returns the GPA of the new root.
+ */
+gpa_t tdp_copy_page_tables(struct kvm_vm *vm)
+{
+ struct kvm_mmu *mmu = &vm->stage2_mmu;
+
+ TEST_ASSERT(mmu->pgd_created, "TDP page tables not created");
+ return __virt_copy_page_tables(vm, mmu, mmu->pgd, mmu->pgtable_levels);
+}
+
/*
* Set Unusable Segment
*
diff --git a/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c b/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c
new file mode 100644
index 0000000000000..fb32b7bf6b19f
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c
@@ -0,0 +1,526 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026, Google LLC.
+ *
+ * Test TLB flushes with nested virtualization across three cases:
+ * 1. When L1 updates L2's (shadow) page tables
+ * 2. When L1 updates nested TDP
+ * 3. When KVM updates mappings on the host (KVM-induced TLB flushes)
+ */
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "svm_util.h"
+#include "vmx.h"
+
+#define NR_ITERATIONS 100
+
+#define VAL1 0x11111111ULL
+#define VAL2 0x22222222ULL
+
+#define TEST_VADDR 0x10000000ULL
+#define TEST_GPA 0x30000000ULL
+#define TEST_NESTED_GPA 0x40000000ULL
+
+#define TEST_MEMSLOT 10
+
+#define SYNC_MOVE_MEMSLOT 1
+
+enum guest_flush_method {
+ TLB_FLUSH_TAG = 0,
+ TLB_FLUSH_VADDR,
+ TLB_CHANGE_TAG,
+ TLB_FLUSH_NONE,
+};
+
+static u64 *pte_gva;
+static gpa_t test_gpa[2];
+static enum guest_flush_method flush_method;
+static u32 max_asid;
+
+/* TDP roots for the multi-root test, root 1 is a non-aliasing copy of root 0 */
+static gpa_t tdp_root_gpa[2];
+/* Value L2 expects at TEST_VADDR, 0 if L2 shouldn't access TEST_VADDR */
+static u64 l2_expected_val;
+
+static void svm_tlb_flush(struct svm_test_data *svm,
+ enum guest_flush_method method)
+{
+ struct vmcb *vmcb = svm->vmcb;
+
+ switch (method) {
+ case TLB_CHANGE_TAG:
+ vmcb->control.asid++;
+ vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+ if (vmcb->control.asid >= max_asid) {
+ vmcb->control.asid = 1;
+ vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
+ }
+ break;
+ case TLB_FLUSH_TAG:
+ vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
+ break;
+ case TLB_FLUSH_VADDR:
+ GUEST_ASSERT(!svm->ncr3_gpa);
+ vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+ invlpga(TEST_VADDR, vmcb->control.asid);
+ break;
+ case TLB_FLUSH_NONE:
+ vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
+ break;
+ }
+}
+
+static void vmx_tlb_flush(struct vmx_pages *vmx, enum guest_flush_method method)
+{
+ u16 vpid = vmread(VIRTUAL_PROCESSOR_ID);
+
+ switch (method) {
+ case TLB_CHANGE_TAG:
+ GUEST_ASSERT(!vmx->eptp_gpa);
+ vmwrite(VIRTUAL_PROCESSOR_ID, vpid + 1);
+ break;
+ case TLB_FLUSH_TAG:
+ if (vmx->eptp_gpa)
+ invept(1, vmread(EPT_POINTER));
+ else
+ invvpid(1, vpid, 0);
+ break;
+ case TLB_FLUSH_VADDR:
+ GUEST_ASSERT(!vmx->eptp_gpa);
+ invvpid(0, vpid, TEST_VADDR);
+ break;
+ case TLB_FLUSH_NONE:
+ break;
+ }
+}
+
+static void prepare_l2(void *nested_state, void *l2_guest_code)
+{
+ struct vmx_pages *vmx = nested_state;
+
+ if (this_cpu_has(X86_FEATURE_VMX)) {
+ u32 sec_exec_ctl;
+
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+ prepare_vmcs(vmx, l2_guest_code);
+
+ /* Enable VPID */
+ sec_exec_ctl = vmread(SECONDARY_VM_EXEC_CONTROL);
+ sec_exec_ctl |= SECONDARY_EXEC_ENABLE_VPID;
+ vmwrite(SECONDARY_VM_EXEC_CONTROL, sec_exec_ctl);
+ vmwrite(VIRTUAL_PROCESSOR_ID, 1);
+ } else {
+ generic_svm_setup(nested_state, l2_guest_code);
+ }
+}
+
+static void __run_l2(void *nested_state, bool launch,
+ enum guest_flush_method method)
+{
+ struct svm_test_data *svm;
+
+ if (this_cpu_has(X86_FEATURE_VMX)) {
+ vmx_tlb_flush(nested_state, method);
+ if (launch)
+ vmlaunch();
+ else
+ vmresume();
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_VMCALL);
+ vmwrite(GUEST_RIP, vmread(GUEST_RIP) + 3); /* skip over VMCALL */
+ } else {
+ svm = nested_state;
+ svm_tlb_flush(svm, method);
+ run_guest(svm->vmcb, svm->vmcb_gpa);
+ GUEST_ASSERT_EQ(svm->vmcb->control.exit_code, SVM_EXIT_VMMCALL);
+ svm->vmcb->save.rip += 3; /* skip over VMMCALL */
+ }
+}
+
+static void run_l2(void *nested_state, bool launch)
+{
+ __run_l2(nested_state, launch, flush_method);
+}
+
+static void l2_guest_code(void)
+{
+ u64 expected_val;
+
+ for (;;) {
+ expected_val = READ_ONCE(l2_expected_val);
+ if (expected_val)
+ GUEST_ASSERT_EQ(READ_ONCE(*(u64 *)TEST_VADDR), expected_val);
+ vmcall();
+ }
+}
+
+static void l1_guest_code(void *data)
+{
+ gpa_t gpa;
+ int i;
+
+ prepare_l2(data, l2_guest_code);
+
+ /*
+ * Alternately switch the PTE (or TDP PTE) mapping TEST_VADDR between
+ * two pages containing VAL1 and VAL2, flush TLBs, and verify that L2
+ * reads the expected value.
+ */
+ for (i = 0; i < NR_ITERATIONS; i++) {
+ gpa = test_gpa[i % 2];
+
+ *pte_gva &= ~PHYSICAL_PAGE_MASK;
+ *pte_gva |= gpa & PHYSICAL_PAGE_MASK;
+
+ WRITE_ONCE(l2_expected_val, (i % 2 == 0) ? VAL1 : VAL2);
+ run_l2(data, i == 0);
+ }
+
+ GUEST_DONE();
+}
+
+/*
+ * Verify that flushing L2's ASID invalidates nested TDP translations for all
+ * TDP roots, not just the one in use at the time of the flush. L1 updates the
+ * nested TDP PTE for TEST_VADDR in root 0, flushes L2's ASID while running L2
+ * with root 1, then runs L2 with root 0 *without* flushing. L2 must observe
+ * the new mapping, as the flush invalidated all translations for the ASID.
+ *
+ * Root 1 is a full copy of root 0, such that none of the nested TDP pages are
+ * shared, i.e. KVM's shadow pages for root 0 are not reachable through the
+ * shadow root for root 1.
+ */
+static void l1_guest_code_multi_tdp_root(void *data)
+{
+ struct svm_test_data *svm = data;
+ struct vmcb *vmcb = svm->vmcb;
+ int i;
+
+ GUEST_ASSERT(this_cpu_has(X86_FEATURE_SVM));
+ GUEST_ASSERT_EQ(svm->ncr3_gpa, tdp_root_gpa[0]);
+
+ prepare_l2(data, l2_guest_code);
+
+ for (i = 0; i < NR_ITERATIONS; i++) {
+ *pte_gva &= ~PHYSICAL_PAGE_MASK;
+ *pte_gva |= test_gpa[i % 2] & PHYSICAL_PAGE_MASK;
+
+ /* Flush L2's ASID (per flush_method) while using root 1 */
+ vmcb->control.nested_cr3 = tdp_root_gpa[1];
+ WRITE_ONCE(l2_expected_val, 0);
+ run_l2(data, i == 0);
+
+ /* Switch back to root 0 without flushing */
+ vmcb->control.nested_cr3 = tdp_root_gpa[0];
+ WRITE_ONCE(l2_expected_val, (i % 2 == 0) ? VAL1 : VAL2);
+ __run_l2(data, false, TLB_FLUSH_NONE);
+ }
+
+ GUEST_DONE();
+}
+
+static void prep_guest_flush_test(struct kvm_vm *vm, bool tdp_enabled,
+ bool copy_tdp_root)
+{
+ gva_t page_gva[2], pte_map_gva;
+ gpa_t pte_gpa;
+ u64 *ptep;
+
+ /*
+ * Allocate two pages in the VM and initialize them with different
+ * values. L1 will update the mappings to switch between these pages and
+ * L2 will verify that the accessed value indicates the right page.
+ */
+ page_gva[0] = vm_alloc_page(vm);
+ page_gva[1] = vm_alloc_page(vm);
+
+ *(u64 *)addr_gva2hva(vm, page_gva[0]) = VAL1;
+ *(u64 *)addr_gva2hva(vm, page_gva[1]) = VAL2;
+
+ test_gpa[0] = addr_gva2gpa(vm, page_gva[0]);
+ test_gpa[1] = addr_gva2gpa(vm, page_gva[1]);
+
+ if (tdp_enabled) {
+ virt_pg_map(vm, TEST_VADDR, TEST_NESTED_GPA);
+ tdp_map(vm, TEST_NESTED_GPA, test_gpa[0], vm->page_size);
+ ptep = tdp_get_pte(vm, TEST_NESTED_GPA);
+ } else {
+ virt_pg_map(vm, TEST_VADDR, test_gpa[0]);
+ ptep = vm_get_pte(vm, TEST_VADDR);
+ }
+
+ /*
+ * Map the PTE (or TDP PTE) for TEST_VADDR, such that L1 can read and
+ * update the mapping. Pass the PTE GVA to L1 to directly use it (rather
+ * than L1 walking the page tables.
+ */
+ pte_gpa = addr_hva2gpa(vm, ptep);
+ pte_map_gva = vm_unused_gva_gap(vm, vm->page_size, KVM_UTIL_MIN_VADDR);
+ virt_pg_map(vm, pte_map_gva, pte_gpa & PAGE_MASK);
+ pte_gva = (u64 *)(pte_map_gva + (pte_gpa & ~PAGE_MASK));
+
+ sync_global_to_guest(vm, pte_gva);
+ sync_global_to_guest(vm, test_gpa);
+
+ if (!tdp_enabled)
+ return;
+
+ /*
+ * Must be done after all pages accessed by L2 are allocated, including
+ * nested state (e.g. L2's stack) and page tables for TEST_VADDR.
+ */
+ tdp_identity_map_default_memslots(vm);
+
+ /* Must be done after all TDP mappings are created */
+ if (copy_tdp_root) {
+ tdp_root_gpa[0] = vm->stage2_mmu.pgd;
+ tdp_root_gpa[1] = tdp_copy_page_tables(vm);
+ sync_global_to_guest(vm, tdp_root_gpa);
+ }
+}
+
+static void l2_guest_code_memslot_move(void)
+{
+ int i;
+ u64 val;
+
+ for (i = 0; i < NR_ITERATIONS; i++) {
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL1);
+
+ vmcall();
+
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL2);
+
+ GUEST_SYNC2(SYNC_MOVE_MEMSLOT, 1);
+
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL1);
+
+ vmcall();
+ }
+}
+
+static void l1_guest_code_memslot_move(void *data)
+{
+ int i;
+ u64 val;
+
+ prepare_l2(data, l2_guest_code_memslot_move);
+
+ for (i = 0; i < NR_ITERATIONS; i++) {
+ /* Verify that accessing VADDR reads from page 1 in both L1 & L2 */
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL1);
+
+ run_l2(data, i == 0);
+
+ /*
+ * Update VADDR to point at page 2. KVM should flush both L1 and
+ * L2's TLB translations so that further accesses go to page 2.
+ */
+ GUEST_SYNC2(SYNC_MOVE_MEMSLOT, 2);
+
+ /*
+ * Verify that accessing VADDR reads from page 2. A stale TLB
+ * entry would lead to reading from page 1. Enter L2 and check
+ * access to page 2, to verify that a TLB flush from L1's
+ * context flushes both L1 and L2.
+ */
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL2);
+
+ run_l2(data, false);
+
+ /*
+ * L2 updated VADDR to point at page 1 again. Verify access to
+ * page 1 again to verify that a TLB flush from L2's context
+ * flushes both L1 and L2 TLB translations.
+ */
+ val = READ_ONCE(*(u64 *)TEST_VADDR);
+ GUEST_ASSERT_EQ(val, VAL1);
+ }
+
+ GUEST_DONE();
+}
+
+static void prep_memslot_move_test(struct kvm_vm *vm, bool tdp_enabled)
+{
+ /*
+ * Disable KVM_X86_QUIRK_SLOT_ZAP_ALL. By default, moving a memslot
+ * invalidates all MMU roots, which implicitly flushes the guest TLB
+ * and masks missing TLB flush bugs. Disabling it forces KVM to keep
+ * the same roots, relying solely on explicit TLB flushes.
+ */
+ TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & KVM_X86_QUIRK_SLOT_ZAP_ALL);
+ vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);
+
+ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+ TEST_GPA, TEST_MEMSLOT,
+ /*npages=*/2, /*flags=*/0);
+
+ virt_pg_map(vm, TEST_VADDR, TEST_GPA);
+
+ if (tdp_enabled)
+ tdp_map(vm, TEST_GPA, TEST_GPA, vm->page_size);
+
+ *(u64 *)addr_gpa2hva(vm, TEST_GPA) = VAL1;
+ *(u64 *)addr_gpa2hva(vm, TEST_GPA + vm->page_size) = VAL2;
+
+ /*
+ * Must be done after all pages accessed by L2 are allocated, including
+ * nested state (e.g. L2's stack) and page tables for TEST_VADDR.
+ */
+ if (tdp_enabled)
+ tdp_identity_map_default_memslots(vm);
+}
+
+static void calc_max_svm_asid(void)
+{
+ const struct kvm_cpuid_entry2 *entry;
+
+ if (!kvm_cpu_has(X86_FEATURE_SVM))
+ return;
+
+ entry = get_cpuid_entry(kvm_get_supported_cpuid(), 0x8000000A, 0);
+ max_asid = entry ? entry->ebx : 0;
+}
+
+static void run_test(void *guest_code, bool tdp_enabled)
+{
+ struct kvm_vcpu *vcpu;
+ gva_t nested_gva = 0;
+ struct kvm_vm *vm;
+ struct ucall uc;
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+ if (tdp_enabled)
+ vm_enable_tdp(vm);
+
+ if (kvm_cpu_has(X86_FEATURE_VMX))
+ vcpu_alloc_vmx(vm, &nested_gva);
+ else
+ vcpu_alloc_svm(vm, &nested_gva);
+
+ if (guest_code == l1_guest_code_memslot_move)
+ prep_memslot_move_test(vm, tdp_enabled);
+ else if (guest_code == l1_guest_code_multi_tdp_root)
+ prep_guest_flush_test(vm, tdp_enabled, true);
+ else
+ prep_guest_flush_test(vm, tdp_enabled, false);
+
+ vcpu_args_set(vcpu, 1, nested_gva);
+
+ sync_global_to_guest(vm, flush_method);
+ sync_global_to_guest(vm, max_asid);
+
+ for (;;) {
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ case UCALL_SYNC:
+ /*
+ * Move the base GPA of the memslot between TEST_GPA and
+ * TEST_GPA - PAGE_SIZE. This effectively moves
+ * TEST_VADDR to point at page 1 or page 2 in the
+ * memslot.
+ */
+ if (uc.args[0] == SYNC_MOVE_MEMSLOT) {
+ gpa_t new_gpa = (uc.args[1] == 1) ?
+ TEST_GPA : (TEST_GPA - vm->page_size);
+
+ vm_mem_region_move(vm, TEST_MEMSLOT, new_gpa);
+ } else {
+ TEST_FAIL("Unknown sync code: %lu", uc.args[0]);
+ }
+ break;
+ case UCALL_DONE:
+ goto done;
+ default:
+ TEST_FAIL("Unknown ucall %lu", uc.cmd);
+ }
+ }
+
+done:
+ kvm_vm_free(vm);
+}
+
+#define tlb_test(test_name, guest_code, tdp_setting, flush_setting) \
+do { \
+ tdp_setting; \
+ flush_setting; \
+ \
+ if (tdp_enabled && !kvm_cpu_has_tdp()) { \
+ pr_info("Skipping: " test_name " (no TDP support)\n"); \
+ break; \
+ } \
+ \
+ /* \
+ * VPIDs do not tag guest-physical translations, i.e. changing or \
+ * flushing VPIDs does not flush EPT translations, and single-context \
+ * INVEPT only flushes the target EPTP (not all TDP roots). \
+ */ \
+ if (tdp_enabled && kvm_cpu_has_ept() && \
+ (flush_method == TLB_CHANGE_TAG || \
+ guest_code == l1_guest_code_multi_tdp_root)) { \
+ pr_info("Skipping: " test_name " (not applicable to EPTs)\n"); \
+ break; \
+ } \
+ \
+ pr_info("Testing " test_name "...\n"); \
+ run_test(guest_code, tdp_enabled); \
+} while (0)
+
+int main(int argc, char *argv[])
+{
+ bool tdp_enabled;
+
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX));
+
+ calc_max_svm_asid();
+
+ tlb_test("Guest-triggered TLB flush (flush entire tag), TDP disabled",
+ l1_guest_code, tdp_enabled = false,
+ flush_method = TLB_FLUSH_TAG);
+ tlb_test("Guest-triggered TLB flush (flush individual address), TDP disabled",
+ l1_guest_code, tdp_enabled = false,
+ flush_method = TLB_FLUSH_VADDR);
+ tlb_test("Guest-triggered TLB flush (change tag), TDP disabled",
+ l1_guest_code, tdp_enabled = false,
+ flush_method = TLB_CHANGE_TAG);
+
+ tlb_test("Guest-triggered TLB flush (flush entire tag), TDP enabled",
+ l1_guest_code, tdp_enabled = true,
+ flush_method = TLB_FLUSH_TAG);
+ tlb_test("Guest-triggered TLB flush (change tag), TDP enabled",
+ l1_guest_code, tdp_enabled = true,
+ flush_method = TLB_CHANGE_TAG);
+
+ tlb_test("Guest-triggered TLB flush with multiple TDP roots (flush entire tag), TDP enabled",
+ l1_guest_code_multi_tdp_root, tdp_enabled = true,
+ flush_method = TLB_FLUSH_TAG);
+ tlb_test("Guest-triggered TLB flush with multiple TDP roots (change tag), TDP enabled",
+ l1_guest_code_multi_tdp_root, tdp_enabled = true,
+ flush_method = TLB_CHANGE_TAG);
+
+
+ tlb_test("KVM-triggered TLB flush via memslot move, TDP disabled",
+ l1_guest_code_memslot_move, tdp_enabled = false,
+ flush_method = TLB_FLUSH_NONE);
+ tlb_test("KVM-triggered TLB flush via memslot move, TDP enabled",
+ l1_guest_code_memslot_move, tdp_enabled = true,
+ flush_method = TLB_FLUSH_NONE);
+
+ return 0;
+}
|
| 25/3 | 2026/10/08 00:31 | llm | kmsan-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30253 output: 62 thoughts: 2331 Thoughts: **KMSAN Assessment: KVM Patch Review** Okay, let's dive into this patch and figure out if we need to fire up a KMSAN fuzzing session in addition to the usual KASAN checks. The golden rule is, if KASAN, LOCKDEP, or any other standard tool can catch the bug, we *don't* need KMSAN. KMSAN is for catching reads of uninitialized memory, specifically in situations where we have kernel data structures heading to user space or when conditional logic depends on potentially uninitialized variables. So, let's break down these changes. It looks like we're mostly dealing with ASID (Address Space Identifier) management, TLB (Translation Lookaside Buffer) flush handling, and some nested virtualization updates within KVM/SVM (Secure Virtual Machine) context. This includes things like: * **Documentation:** Updates to documentation. Not a concern for KMSAN. * **Header Files:** Adding a new request and some definitions. Unlikely KMSAN triggers. * **CPUID:** Comment updates. Again, no KMSAN concerns. * **TLB flush FIFO:** Refactoring TLB flush FIFO purging. Potentially interesting, let's keep an eye on this. * **MMU Changes:** Adding `kvm_mmu_sync_all_roots`, and new TLB tag allocator using bitmaps. Okay, the TLB tag allocator, that uses a bitmap allocated via `bitmap_zalloc`. The code uses `bitmap_zalloc` which should zero the memory. This is critical. `kvm_init_tlb_tags` initializes the bitmap, and it looks like we check for a NULL bitmap in `kvm_alloc_tlb_tag`, so that's good. * **Nested SVM Changes:** Lots of updates to nested SVM handling: TLB flushes, ASID allocation and free. Here's where things get interesting. We have `svm_allocate_nested`, `svm_free_nested`, and `svm_vcpu_create/free`. The `svm->nested.asid02` and `svm->nested.last_asid` members are being set. The `bitmap_zalloc` calls is properly initializing memory. We're using `allocate_asid` and `free_asid` functions. Let's see how these are handled to ensure that the asid memory is initialized or zeroed. * **SEV (Secure Encrypted Virtualization) Updates:** `sev_vcpu_migrate_asid`, `sev_migrate_from`, `pre_sev_run`, and `sev_init_vmcb`. Mostly ASID and VMCB management. Need to verify ASID is not leaking into user-space via uninitialized memory. * **SVM core:** We see ASID allocation with the new TLB tag allocator, replacement of old ASID generation and `svm_hardware_setup/unsetup`. We use `kvm_alloc_tlb_tag`, which is interesting because of the bitmap usage. It looks like `svm->asid` is allocated with `allocate_asid` in `svm_vcpu_create`. `bitmap_zalloc` is zeroing `tlb_tags.bitmap`. * **VMX Changes:** Similar to SVM, VPID (Virtual Processor ID) management is updated using `kvm_init_tlb_tags`, so we need to ensure TLB memory is zeroed. * **X86:** Handling for the new `KVM_REQ_MMU_SYNC_ALL_ROOTS`. Potentially interesting but seems unlikely to involve uninitialized memory. * **Selftests:** Tests are generally not the source of uninitialized memory vulnerabilities. My thinking is, we have a number of changes related to ASID and TLB management, and the new TLB tag allocation using bitmaps. Given these changes, there is a risk that uninitialized memory could be used. However, `bitmap_zalloc` is zeroing the bitmap, and a NULL pointer is checked in `kvm_alloc_tlb_tag`. The `allocate_asid` calls will require closer inspection. Given the number of touch points and the memory being handled, let's err on the side of caution. **NeedsKMSAN = True.** We can always turn it off if the fuzzing sessions show no new issues. |
| 26/3 | 2026/10/08 00:32 | tool | grepper |
0mArgs:{
"Expression": "kmem_cache_zalloc|kzalloc",
"PathPrefixes": [
"arch/x86/kvm/svm/svm.c",
"arch/x86/kvm/x86.c"
]
}Results: {
"Output": "arch/x86/kvm/x86.c=3426=long kvm_arch_vcpu_ioctl(struct file *filp,\n--\narch/x86/kvm/x86.c-3447-\t\t\tgoto out;\narch/x86/kvm/x86.c:3448:\t\tu.lapic = kzalloc_obj(struct kvm_lapic_state);\narch/x86/kvm/x86.c-3449-\n--\narch/x86/kvm/x86.c-3643-\narch/x86/kvm/x86.c:3644:\t\tu.xsave = kzalloc_obj(struct kvm_xsave);\narch/x86/kvm/x86.c-3645-\t\tr = -ENOMEM;\n--\narch/x86/kvm/x86.c-3674-\narch/x86/kvm/x86.c:3675:\t\tu.xsave = kzalloc(size, GFP_KERNEL);\narch/x86/kvm/x86.c-3676-\t\tr = -ENOMEM;\n--\narch/x86/kvm/x86.c-3692-\tcase KVM_GET_XCRS: {\narch/x86/kvm/x86.c:3693:\t\tu.xcrs = kzalloc_obj(struct kvm_xcrs);\narch/x86/kvm/x86.c-3694-\t\tr = -ENOMEM;\n--\narch/x86/kvm/x86.c-3851-\narch/x86/kvm/x86.c:3852:\t\tu.sregs2 = kzalloc_obj(struct kvm_sregs2);\narch/x86/kvm/x86.c-3853-\t\tr = -ENOMEM;\n--\narch/x86/kvm/x86.c=5959=static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-5962-\narch/x86/kvm/x86.c:5963:\tctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT);\narch/x86/kvm/x86.c-5964-\tif (!ctxt) {\n"
} |
| 27/3 | 2026/10/08 00:32 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34254 output: 39 thoughts: 14 Thoughts: Okay, here's my attempt at summarizing that thought process, tailored for an expert audience: **Investigating vcpu_svm Allocation** Alright, let's dive in. My immediate thought is to understand the allocation strategy for `vcpu_svm`. I need a clear picture of how this structure, or the resources it manages, is being handled. I want to trace the allocation path, understand the memory model it adheres to, and potentially identify any potential bottlenecks or areas for optimization. The goal is a deep understanding of its lifecycle and how it integrates with the overall system's resource management, especially within the context of hardware-assisted virtualization. I'll need to consider aspects like NUMA affinity, memory pinning, and any potential lock contention during creation, destruction, or manipulation of the `vcpu_svm` data. Knowing the allocation patterns will be key to profiling performance and preventing memory fragmentation. |
| 28/3 | 2026/10/08 00:32 | tool | grepper |
0mError:no matches Args: {
"Expression": "vcpu_svm.*kmem_cache",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"error": "no matches"
} |
| 29/3 | 2026/10/08 00:32 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34507 output: 34 thoughts: 48 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Digging into the KVM x86 Underbelly** Alright, time to get my hands dirty. I'm thinking, "Where's the beef? Where's the *real* vCPU creation happening in the x86 KVM implementation?" The goal here is to pinpoint the exact code path responsible for spinning up those virtual CPUs. I want to trace the allocation and initialization. My mind is immediately gravitating towards a few key areas. I’m starting with the obvious: I'll hunt for `vcpu_create`. That’s the most likely candidate for the primary entry point. If that's too broad, or if the function is just a wrapper, I'll broaden my search. The kernel naming conventions tend to be pretty explicit, so `kvm_x86_ops.vcpu_create` is another potential target, more specifically tied to the x86-specific KVM operations. That would be a good next step. I also know that vCPU structures, like any other kernel data structures, need memory. So, I need to check how these are allocated and initialized. I'll include `kmem_cache_zalloc` in my search. Chances are, KVM uses a slab allocator for this, and tracing the allocation will give me a better understanding of the low-level data structure management. I'm focusing my search scope on `arch/x86/kvm` because that's where the x86-specific KVM code lives. That directory is the logical place to start digging. It’s where the architecture-specific KVM magic will be happening. I expect the code will be relatively modular, so finding the right file shouldn't be too daunting, but this will let me get a solid start on my understanding of this KVM component. |
| 30/3 | 2026/10/08 00:32 | tool | grepper |
0mArgs:{
"Expression": "vcpu_create",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/sev.c=4900=void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)\n--\narch/x86/kvm/svm/sev.c-4923-\narch/x86/kvm/svm/sev.c:4924:int sev_vcpu_create(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/sev.c-4925-{\n--\narch/x86/kvm/svm/svm.c=1333=void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)\n--\narch/x86/kvm/svm/svm.c-1338-\narch/x86/kvm/svm/svm.c:1339:static int svm_vcpu_create(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-1340-{\n--\narch/x86/kvm/svm/svm.c-1358-\narch/x86/kvm/svm/svm.c:1359:\terr = sev_vcpu_create(vcpu);\narch/x86/kvm/svm/svm.c-1360-\tif (err)\n--\narch/x86/kvm/svm/svm.c=5481=struct kvm_x86_ops svm_x86_ops __initdata = {\n--\narch/x86/kvm/svm/svm.c-5492-\t.vcpu_precreate = avic_vcpu_precreate,\narch/x86/kvm/svm/svm.c:5493:\t.vcpu_create = svm_vcpu_create,\narch/x86/kvm/svm/svm.c-5494-\t.vcpu_free = svm_vcpu_free,\n--\narch/x86/kvm/svm/svm.h=1034=void sev_snp_reload_vmsa(struct kvm_vcpu *vcpu);\narch/x86/kvm/svm/svm.h-1035-\narch/x86/kvm/svm/svm.h:1036:int sev_vcpu_create(struct kvm_vcpu *vcpu);\narch/x86/kvm/svm/svm.h-1037-void sev_free_vcpu(struct kvm_vcpu *vcpu);\n--\narch/x86/kvm/svm/svm.h=1059=static inline struct page *snp_safe_alloc_page(void)\n--\narch/x86/kvm/svm/svm.h-1063-\narch/x86/kvm/svm/svm.h:1064:static inline int sev_vcpu_create(struct kvm_vcpu *vcpu) { return 0; }\narch/x86/kvm/svm/svm.h-1065-static inline void sev_free_vcpu(struct kvm_vcpu *vcpu) {}\n--\narch/x86/kvm/vmx/main.c=70=static int vt_vcpu_precreate(struct kvm *kvm)\n--\narch/x86/kvm/vmx/main.c-77-\narch/x86/kvm/vmx/main.c:78:static int vt_vcpu_create(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/main.c-79-{\narch/x86/kvm/vmx/main.c-80-\tif (is_td_vcpu(vcpu))\narch/x86/kvm/vmx/main.c:81:\t\treturn tdx_vcpu_create(vcpu);\narch/x86/kvm/vmx/main.c-82-\narch/x86/kvm/vmx/main.c:83:\treturn vmx_vcpu_create(vcpu);\narch/x86/kvm/vmx/main.c-84-}\n--\narch/x86/kvm/vmx/main.c=1000=struct kvm_x86_ops vt_x86_ops __initdata = {\n--\narch/x86/kvm/vmx/main.c-1019-\t.vcpu_precreate = vt_op(vcpu_precreate),\narch/x86/kvm/vmx/main.c:1020:\t.vcpu_create = vt_op(vcpu_create),\narch/x86/kvm/vmx/main.c-1021-\t.vcpu_free = vt_op(vcpu_free),\n--\narch/x86/kvm/vmx/tdx.c=615=int tdx_vm_init(struct kvm *kvm)\n--\narch/x86/kvm/vmx/tdx.c-665-\narch/x86/kvm/vmx/tdx.c:666:int tdx_vcpu_create(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/tdx.c-667-{\n--\narch/x86/kvm/vmx/tdx.c=825=void tdx_vcpu_put(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/tdx.c-839- * 3. KVM_CREATE_VCPU ioctl. (only when TD is TD_STATE_INITIALIZED).\narch/x86/kvm/vmx/tdx.c:840: * 3.1 tdx_vcpu_create() transitions vCPU state to VCPU_TD_STATE_UNINITIALIZED.\narch/x86/kvm/vmx/tdx.c:841: * 3.2 vcpu_load() and vcpu_put() in kvm_arch_vcpu_create().\narch/x86/kvm/vmx/tdx.c:842: * 3.3 (conditional) if any error encountered after kvm_arch_vcpu_create()\narch/x86/kvm/vmx/tdx.c-843- * kvm_arch_vcpu_destroy() --\u003e tdx_vcpu_free().\n--\narch/x86/kvm/vmx/vmx.c=7482=void vmx_vcpu_free(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-7496-\narch/x86/kvm/vmx/vmx.c:7497:int vmx_vcpu_create(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/vmx.c-7498-{\n--\narch/x86/kvm/vmx/x86_ops.h=22=int vmx_vcpu_precreate(struct kvm *kvm);\narch/x86/kvm/vmx/x86_ops.h:23:int vmx_vcpu_create(struct kvm_vcpu *vcpu);\narch/x86/kvm/vmx/x86_ops.h-24-fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags);\n--\narch/x86/kvm/vmx/x86_ops.h=134=int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);\narch/x86/kvm/vmx/x86_ops.h-135-\narch/x86/kvm/vmx/x86_ops.h:136:int tdx_vcpu_create(struct kvm_vcpu *vcpu);\narch/x86/kvm/vmx/x86_ops.h-137-void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);\n--\narch/x86/kvm/x86.c=9397=int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)\n--\narch/x86/kvm/x86.c-9411-\t/*\narch/x86/kvm/x86.c:9412:\t * Note, any actions done by .vcpu_create() must be idempotent with\narch/x86/kvm/x86.c-9413-\t * respect to creating multiple vCPUs, and therefore are not undone if\n--\narch/x86/kvm/x86.c-9435-\narch/x86/kvm/x86.c:9436:int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-9437-{\n--\narch/x86/kvm/x86.c-9502-\narch/x86/kvm/x86.c:9503:\tr = kvm_x86_call(vcpu_create)(vcpu);\narch/x86/kvm/x86.c-9504-\tif (r)\n"
} |
| 31/3 | 2026/10/08 00:32 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37386 output: 35 thoughts: 40 Thoughts: Here's my take on the thought process, as requested:
**Diving into KVM Virtual CPU Creation**
Alright, let's get down to brass tacks. I need to pinpoint the exact location where these virtual CPUs are being born within the KVM framework. The kernel source should be my guide. My immediate thought is to zero in on `kvm_vm_ioctl_create_vcpu`. This seems like a pretty likely entry point for the process, considering the naming convention ("ioctl" strongly suggests a system call interaction, and "create_vcpu" is pretty self-explanatory). I'll want to check this function in `virt/kvm/kvm_main.c` because that directory generally houses the core KVM logic. Specifically, I'm interested in the allocation of the `vcpu` itself. That's the key thing I need to understand – where and how this `vcpu` structure is initially instantiated. I suspect `kvm_vm_ioctl_create_vcpu` will be handling the `vcpu` structure allocation.
|
| 32/3 | 2026/10/08 00:32 | tool | grepper |
0mArgs:{
"Expression": "kvm_arch_vcpu_create",
"PathPrefixes": [
"virt/kvm/"
]
}Results: {
"Output": "virt/kvm/kvm_main.c=4248=static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)\n--\nvirt/kvm/kvm_main.c-4297-\nvirt/kvm/kvm_main.c:4298:\tr = kvm_arch_vcpu_create(vcpu);\nvirt/kvm/kvm_main.c-4299-\tif (r)\n"
} |
| 33/3 | 2026/10/08 00:32 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37819 output: 40 thoughts: 29 Thoughts: Okay, let's dive into this. I'm going to pull up lines 4280-4296 of `virt/kvm/kvm_main.c` and start dissecting. I'm expecting to see something related to the core KVM functionality, probably within a critical path. This range is likely within a larger function, so I'll need to keep the context in mind. I want to see what's happening at this specific point and what it's trying to accomplish. Knowing the surrounding code is key to understanding its intended purpose. I'll be looking for any hints on how the VM is interacting with the host, potentially handling interrupts, memory accesses, or maybe even context switching. My main interest is in seeing the flow and logic, specifically, how it coordinates the guest's operation with the hypervisor's. I'm mentally parsing this as a control loop, so I'll be looking for the crucial pieces. |
| 34/3 | 2026/10/08 00:32 | tool | read-file |
0mArgs:{
"File": "virt/kvm/kvm_main.c",
"FirstLine": 4280,
"LineCount": 25
}Results: {
"Contents": "4280:\t\tvcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);\n4281:\t\tif (!vcpu) {\n4282:\t\t\tr = -ENOMEM;\n4283:\t\t\tgoto vcpu_decrement;\n4284:\t\t}\n4285:\t\n4286:\t\tvcpu-\u003evcpu_idx = -1;\n4287:\t\n4288:\t\tBUILD_BUG_ON(sizeof(struct kvm_run) \u003e PAGE_SIZE);\n4289:\t\tpage = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);\n4290:\t\tif (!page) {\n4291:\t\t\tr = -ENOMEM;\n4292:\t\t\tgoto vcpu_free;\n4293:\t\t}\n4294:\t\tvcpu-\u003erun = page_address(page);\n4295:\t\n4296:\t\tkvm_vcpu_init(vcpu, kvm, id);\n4297:\t\n4298:\t\tr = kvm_arch_vcpu_create(vcpu);\n4299:\t\tif (r)\n4300:\t\t\tgoto vcpu_free_run_page;\n4301:\t\n4302:\t\tif (kvm-\u003edirty_ring_size) {\n4303:\t\t\tr = kvm_dirty_ring_alloc(kvm, \u0026vcpu-\u003edirty_ring,\n4304:\t\t\t\t\t\t id, kvm-\u003edirty_ring_size);\n"
} |
| 35/3 | 2026/10/08 00:32 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38491 output: 43 thoughts: 177 Thoughts: Here's my understanding of the situation, tailored for my expertise: **Examining KVM VCPU Initialization and Nested State Management** Okay, so I'm thinking about how a `struct kvm_vcpu` is allocated, specifically when it's part of an SVM or VMX setup. It's allocated using `kmem_cache_zalloc`, using `kvm_vcpu_cache` and `GFP_KERNEL_ACCOUNT`, which means the entire structure, including the `vcpu_svm` or `vcpu_vmx` structure, is zeroed out during the allocation. That's good to know. This detail is important because it means variables like `svm->nested.last_asid` and `svm->nested.asid02` are initialized to zero right off the bat. Now, let's switch gears and investigate the `svm_set_nested_state` function. I'm going to take a look around line 2084 or, I think, a more relevant region is between lines 2150 and 2200 in `arch/x86/kvm/svm/nested.c` to see how nested state is managed. |
| 36/3 | 2026/10/08 00:32 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/nested.c",
"FirstLine": 2150,
"LineCount": 40
}Results: {
"Contents": "2150:\t\t\tsvm_leave_nested(vcpu);\n2151:\t\telse\n2152:\t\t\tsvm-\u003enested.vmcb02.ptr-\u003esave = svm-\u003evmcb01.ptr-\u003esave;\n2153:\t\n2154:\t\tsvm_set_gif(svm, !!(kvm_state-\u003eflags \u0026 KVM_STATE_NESTED_GIF_SET));\n2155:\t\n2156:\t\tif (kvm_state-\u003eflags \u0026 KVM_STATE_NESTED_RUN_PENDING)\n2157:\t\t\tvcpu-\u003earch.nested_run_pending = KVM_NESTED_RUN_PENDING_UNTRUSTED;\n2158:\t\telse\n2159:\t\t\tvcpu-\u003earch.nested_run_pending = 0;\n2160:\t\n2161:\t\tsvm-\u003enested.vmcb12_gpa = kvm_state-\u003ehdr.svm.vmcb_pa;\n2162:\t\n2163:\t\tsvm_copy_vmrun_state(vcpu, \u0026svm-\u003evmcb01.ptr-\u003esave, save);\n2164:\t\tnested_copy_vmcb_control_to_cache(svm, ctl);\n2165:\t\n2166:\t\tnested_svm_service_local_tlb_flushes(vcpu);\n2167:\t\n2168:\t\tsvm_switch_vmcb(svm, \u0026svm-\u003enested.vmcb02);\n2169:\t\n2170:\t\tif (use_separate_l2_pat)\n2171:\t\t\tvmcb_set_gpat(svm-\u003evmcb, kvm_state-\u003ehdr.svm.gpat);\n2172:\t\n2173:\t\tnested_vmcb02_prepare_control(svm);\n2174:\t\n2175:\t\t/*\n2176:\t\t * Any previously restored state (e.g. KVM_SET_SREGS) would mark fields\n2177:\t\t * dirty in vmcb01 instead of vmcb02, so mark all of vmcb02 dirty here.\n2178:\t\t */\n2179:\t\tvmcb_mark_all_dirty(svm-\u003evmcb);\n2180:\t\n2181:\t\t/*\n2182:\t\t * While the nested guest CR3 is already checked and set by\n2183:\t\t * KVM_SET_SREGS, it was set when nested state was yet loaded,\n2184:\t\t * thus MMU might not be initialized correctly.\n2185:\t\t * Set it again to fix this.\n2186:\t\t */\n2187:\t\tret = nested_svm_load_cr3(\u0026svm-\u003evcpu, vcpu-\u003earch.cr3,\n2188:\t\t\t\t\t nested_npt_enabled(svm), false);\n2189:\t\tif (ret)\n"
} |
| 37/3 | 2026/10/08 00:32 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39701 output: 44 thoughts: 21 Thoughts: Here's my understanding of the thought process, summarized as you requested: **Deconstructing `nested_copy_vmcb_control_to_cache`** Okay, so I'm looking at this function, `nested_copy_vmcb_control_to_cache`, and my first instinct is to understand exactly what it's doing. The name gives a general idea - copying control information related to VMCBs (Virtual Machine Control Blocks) and caching it. But "nested" adds a layer of complexity, implying this is relevant in a nested virtualization scenario, which immediately triggers a whole host of considerations in my head. I need to dive deeper. I want to break down its function and implications. I need to check exactly what's being copied and *how* it's being cached. That caching aspect is key – is it a local cache, a shared cache, what's the cache coherency strategy? Understanding the scope of the "control information" is crucial; does it deal with privilege levels, memory management, interrupt handling, or something else entirely? A quick review of the surrounding code should provide some answers, but right now, my focus is on precisely what this function *does* at its core. |
| 38/3 | 2026/10/08 00:33 | tool | grepper |
0mArgs:{
"Expression": "nested_copy_vmcb_control_to_cache",
"PathPrefixes": [
"arch/x86/kvm/svm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=512=static\narch/x86/kvm/svm/nested.c:513:void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,\narch/x86/kvm/svm/nested.c-514-\t\t\t\t\t struct vmcb_ctrl_area_cached *to,\n--\narch/x86/kvm/svm/nested.c-570-\narch/x86/kvm/svm/nested.c:571:void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,\narch/x86/kvm/svm/nested.c-572-\t\t\t\t struct vmcb_control_area *control)\narch/x86/kvm/svm/nested.c-573-{\narch/x86/kvm/svm/nested.c:574:\t__nested_copy_vmcb_control_to_cache(\u0026svm-\u003evcpu, \u0026svm-\u003enested.ctl, control);\narch/x86/kvm/svm/nested.c-575-}\n--\narch/x86/kvm/svm/nested.c=1148=static int nested_svm_copy_vmcb12_to_cache(struct kvm_vcpu *vcpu, u64 vmcb12_gpa)\n--\narch/x86/kvm/svm/nested.c-1158-\tvmcb12 = m.map.hva;\narch/x86/kvm/svm/nested.c:1159:\tnested_copy_vmcb_control_to_cache(svm, \u0026vmcb12-\u003econtrol);\narch/x86/kvm/svm/nested.c-1160-\tnested_copy_vmcb_save_to_cache(svm, \u0026vmcb12-\u003esave);\n--\narch/x86/kvm/svm/nested.c=1925=void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1934-\narch/x86/kvm/svm/nested.c:1935:/* Inverse operation of nested_copy_vmcb_control_to_cache() */\narch/x86/kvm/svm/nested.c-1936-static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,\n--\narch/x86/kvm/svm/nested.c=2041=static int svm_set_nested_state(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-2102-\tret = -EINVAL;\narch/x86/kvm/svm/nested.c:2103:\t__nested_copy_vmcb_control_to_cache(vcpu, \u0026ctl_cached, ctl);\narch/x86/kvm/svm/nested.c-2104-\tif (!nested_vmcb_check_controls(vcpu, \u0026ctl_cached))\n--\narch/x86/kvm/svm/nested.c-2163-\tsvm_copy_vmrun_state(vcpu, \u0026svm-\u003evmcb01.ptr-\u003esave, save);\narch/x86/kvm/svm/nested.c:2164:\tnested_copy_vmcb_control_to_cache(svm, ctl);\narch/x86/kvm/svm/nested.c-2165-\n--\narch/x86/kvm/svm/svm.c=5209=static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)\n--\narch/x86/kvm/svm/svm.c-5253-\tvmcb12 = m.map.hva;\narch/x86/kvm/svm/svm.c:5254:\tnested_copy_vmcb_control_to_cache(svm, \u0026vmcb12-\u003econtrol);\narch/x86/kvm/svm/svm.c-5255-\tnested_copy_vmcb_save_to_cache(svm, \u0026vmcb12-\u003esave);\n--\narch/x86/kvm/svm/svm.h=930=void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu);\narch/x86/kvm/svm/svm.h:931:void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,\narch/x86/kvm/svm/svm.h-932-\t\t\t\t struct vmcb_control_area *control);\n"
} |
| 39/3 | 2026/10/08 00:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 41224 output: 43 thoughts: 114 Thoughts: Here's my analysis of the provided code snippet: **Diving into `nested_copy_vmcb_cache_to_control`** Okay, so I'm looking at `nested.c`, specifically around line 1935, and my target function is `nested_copy_vmcb_cache_to_control`. The comments in the patch tell me it's the inverse of `nested_copy_vmcb_control_to_cache`. That's a good starting point, meaning it presumably copies data in the opposite direction, including the ASID. Now, I need to understand the function itself. I'll need to dig into its implementation to see exactly what data it's moving and how. Furthermore, I need to track down where this function is called from, to understand the context in which it operates. What triggers this "inverse operation"? Is it related to a specific stage in the virtualization process, or a particular event? Understanding the call sites is crucial. Let's get cracking. |
| 40/3 | 2026/10/08 00:33 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/nested.c",
"FirstLine": 1930,
"LineCount": 40
}Results: {
"Contents": "1930:\t\t\tkvm_calc_nested_tsc_multiplier(vcpu-\u003earch.l1_tsc_scaling_ratio,\n1931:\t\t\t\t\t\t svm-\u003etsc_ratio_msr);\n1932:\t\tsvm_write_tsc_multiplier(vcpu);\n1933:\t}\n1934:\t\n1935:\t/* Inverse operation of nested_copy_vmcb_control_to_cache() */\n1936:\tstatic void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,\n1937:\t\t\t\t\t\t struct vmcb_ctrl_area_cached *from)\n1938:\t{\n1939:\t\tunsigned int i;\n1940:\t\n1941:\t\tmemset(dst, 0, sizeof(struct vmcb_control_area));\n1942:\t\n1943:\t\tfor (i = 0; i \u003c MAX_INTERCEPT; i++)\n1944:\t\t\tdst-\u003eintercepts[i] = from-\u003eintercepts[i];\n1945:\t\n1946:\t\tdst-\u003eiopm_base_pa = from-\u003eiopm_base_pa;\n1947:\t\tdst-\u003emsrpm_base_pa = from-\u003emsrpm_base_pa;\n1948:\t\tdst-\u003etsc_offset = from-\u003etsc_offset;\n1949:\t\tdst-\u003easid = from-\u003easid;\n1950:\t\tdst-\u003etlb_ctl = from-\u003etlb_ctl;\n1951:\t\tdst-\u003eerap_ctl = from-\u003eerap_ctl;\n1952:\t\tdst-\u003eint_ctl = from-\u003eint_ctl;\n1953:\t\tdst-\u003eint_vector = from-\u003eint_vector;\n1954:\t\tdst-\u003eint_state = from-\u003eint_state;\n1955:\t\tdst-\u003eexit_code = from-\u003eexit_code;\n1956:\t\tdst-\u003eexit_info_1 = from-\u003eexit_info_1;\n1957:\t\tdst-\u003eexit_info_2 = from-\u003eexit_info_2;\n1958:\t\tdst-\u003eexit_int_info = from-\u003eexit_int_info;\n1959:\t\tdst-\u003eexit_int_info_err = from-\u003eexit_int_info_err;\n1960:\t\tdst-\u003emisc_ctl\t\t = from-\u003emisc_ctl;\n1961:\t\tdst-\u003eevent_inj = from-\u003eevent_inj;\n1962:\t\tdst-\u003eevent_inj_err = from-\u003eevent_inj_err;\n1963:\t\tdst-\u003enext_rip = from-\u003enext_rip;\n1964:\t\tdst-\u003enested_cr3\t\t = from-\u003enested_cr3;\n1965:\t\tdst-\u003emisc_ctl2\t\t = from-\u003emisc_ctl2;\n1966:\t\tdst-\u003epause_filter_count = from-\u003epause_filter_count;\n1967:\t\tdst-\u003epause_filter_thresh = from-\u003epause_filter_thresh;\n1968:\t\t/* 'clean' and 'hv_enlightenments' are not changed by KVM */\n1969:\t}\n"
} |
| 41/3 | 2026/10/08 00:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42403 output: 44 thoughts: 22 Thoughts: Here's my analysis of the provided thought process, tailored for an expert audience: **Analyzing `nested_copy_vmcb_cache_to_control` Callsites** Okay, so I need to understand the impact of the function `nested_copy_vmcb_cache_to_control`. My immediate goal is to figure out where this function is being used. That means the most efficient path forward is to identify all of its call sites. Knowing the contexts in which it's invoked will provide critical insights into its purpose and potential side effects. I'll need to use my tools – likely a code search or static analysis – to pinpoint every instance where this function is called. From there, I can start to build a mental map of its role within the larger virtual machine architecture. Understanding these calls will be the foundation for any further analysis or modification I might need to perform. |
| 42/3 | 2026/10/08 00:33 | tool | grepper |
0mArgs:{
"Expression": "nested_copy_vmcb_cache_to_control",
"PathPrefixes": [
"arch/x86/kvm/svm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=1925=void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1935-/* Inverse operation of nested_copy_vmcb_control_to_cache() */\narch/x86/kvm/svm/nested.c:1936:static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,\narch/x86/kvm/svm/nested.c-1937-\t\t\t\t\t struct vmcb_ctrl_area_cached *from)\n--\narch/x86/kvm/svm/nested.c=1971=static int svm_get_nested_state(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-2026-\narch/x86/kvm/svm/nested.c:2027:\tnested_copy_vmcb_cache_to_control(ctl, \u0026svm-\u003enested.ctl);\narch/x86/kvm/svm/nested.c-2028-\tr = copy_to_user(\u0026user_vmcb-\u003econtrol, ctl,\n"
} |
| 43/3 | 2026/10/08 00:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42981 output: 43 thoughts: 2576 Thoughts: **Analyzing Potential Uninitialized Memory in KVM SVM TLB Flush Patch** Okay, I need to methodically check this patch for potential uninitialized memory reads, particularly around TLB flushing and ASID management in KVM's SVM implementation. I've got the diff, and I'm going through the thought process. I started by seeing a potential red flag, the call to `svm_get_nested_state`, but immediately remembered that `memset` is at the beginning of the `nested_copy_vmcb_cache_to_control` function. I then realized the original functions were not modified, except a whitespace change, and quickly realized that any problems should be elsewhere. The change was simply about cleaning up the comment and fixing indentation. Since I am an expert in this field, I know the next step: to understand the *overall* impact of the patch series. From my understanding of the commit messages, this is a large refactor around TLB flushing and SVM ASID management. The key changes are the introduction of a shared TLB tag allocator, ASID fallback handling, nested ASID emulation in SVM, a new request type for MMU sync, and some Hyper-V TLB flush improvements. I'm now going function-by-function to see if there is potential for uninitialized memory reads. * `kvm_hv_purge_tlb_flush_fifo` uses `kfifo_reset_out`, which will clear everything. No problem here. * `__kvm_mmu_invalidate_addr` has been renamed to `kvm_mmu_sync_addr`, and the only change involves adding a boolean flag. This flag is being passed as `true` or `!asid`, so no uninitialized data here. * `kvm_init_tlb_tags`, `kvm_destroy_tlb_tags`, `kvm_alloc_tlb_tag`, and `kvm_free_tlb_tag` manage a global TLB tag bitmap, which is properly initialized with zeroed memory. Every allocation and deallocation is well-defined. * `nested_svm_entry_tlb_flush` and related functions: I need to trace the data flow for `svm->nested.ctl.asid` and `svm->nested.last_asid`. I know `nested.last_asid` is initialized to zero during the VM creation and again later in `svm_allocate_nested`. Now the `ctl` member, is copied from the `ctl` parameter passed to `nested_copy_vmcb_control_to_cache` via `svm_set_nested_state`. It's essential to understand *how* `ctl` is populated. The `ctl` is copied from the `vmcb` of the nested VM by `nested_copy_vmcb_control_to_cache`. I have to confirm that `nested_copy_vmcb_control_to_cache` is always called correctly. I see that this is populated correctly. It is copied from the `ctl` which itself is copied from the guest's vmcb. * I confirmed that in `svm_set_nested_state`, `nested_copy_vmcb_control_to_cache` is indeed called, which populates the `ctl` member, with no risk. Therefore, after a thorough review, I can confidently state that I *don't* see any obvious uninitialized memory read risks in this patch. All the critical data structures appear to be properly initialized, and the data flow seems sound. The patch seems safe in terms of uninitialized memory reads. |
| 44/3 | 2026/10/08 00:33 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/nested.c",
"FirstLine": 2040,
"LineCount": 60
}Results: {
"Contents": "2040:\t\n2041:\tstatic int svm_set_nested_state(struct kvm_vcpu *vcpu,\n2042:\t\t\t\t\tstruct kvm_nested_state __user *user_kvm_nested_state,\n2043:\t\t\t\t\tstruct kvm_nested_state *kvm_state)\n2044:\t{\n2045:\t\tstruct vcpu_svm *svm = to_svm(vcpu);\n2046:\t\tstruct vmcb __user *user_vmcb = (struct vmcb __user *)\n2047:\t\t\t\u0026user_kvm_nested_state-\u003edata.svm[0];\n2048:\t\tstruct vmcb_control_area *ctl;\n2049:\t\tstruct vmcb_save_area *save;\n2050:\t\tstruct vmcb_save_area_cached save_cached;\n2051:\t\tstruct vmcb_ctrl_area_cached ctl_cached;\n2052:\t\tbool use_separate_l2_pat;\n2053:\t\tunsigned long cr0;\n2054:\t\tint ret;\n2055:\t\n2056:\t\tBUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) \u003e\n2057:\t\t\t KVM_STATE_NESTED_SVM_VMCB_SIZE);\n2058:\t\n2059:\t\tif (kvm_state-\u003eformat != KVM_STATE_NESTED_FORMAT_SVM)\n2060:\t\t\treturn -EINVAL;\n2061:\t\n2062:\t\tif (kvm_state-\u003eflags \u0026 ~(KVM_STATE_NESTED_GUEST_MODE |\n2063:\t\t\t\t\t KVM_STATE_NESTED_RUN_PENDING |\n2064:\t\t\t\t\t KVM_STATE_NESTED_GIF_SET))\n2065:\t\t\treturn -EINVAL;\n2066:\t\n2067:\t\t/*\n2068:\t\t * If in guest mode, vcpu-\u003earch.efer actually refers to the L2 guest's\n2069:\t\t * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.\n2070:\t\t * If SVME is disabled, the only valid states are \"none\" and GIF=1\n2071:\t\t * (clearing SVME does NOT set GIF, i.e. GIF=0 is allowed).\n2072:\t\t */\n2073:\t\tif (!(vcpu-\u003earch.efer \u0026 EFER_SVME) \u0026\u0026 kvm_state-\u003eflags \u0026\u0026\n2074:\t\t kvm_state-\u003eflags != KVM_STATE_NESTED_GIF_SET)\n2075:\t\t\treturn -EINVAL;\n2076:\t\n2077:\t\t/* SMM temporarily disables SVM, so we cannot be in guest mode. */\n2078:\t\tif (is_smm(vcpu) \u0026\u0026 (kvm_state-\u003eflags \u0026 KVM_STATE_NESTED_GUEST_MODE))\n2079:\t\t\treturn -EINVAL;\n2080:\t\n2081:\t\tif (!(kvm_state-\u003eflags \u0026 KVM_STATE_NESTED_GUEST_MODE)) {\n2082:\t\t\tsvm_leave_nested(vcpu);\n2083:\t\t\tsvm_set_gif(svm, !!(kvm_state-\u003eflags \u0026 KVM_STATE_NESTED_GIF_SET));\n2084:\t\t\treturn 0;\n2085:\t\t}\n2086:\t\n2087:\t\tif (!page_address_valid(vcpu, kvm_state-\u003ehdr.svm.vmcb_pa))\n2088:\t\t\treturn -EINVAL;\n2089:\t\tif (kvm_state-\u003esize \u003c sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)\n2090:\t\t\treturn -EINVAL;\n2091:\t\n2092:\t\tctl = memdup_user(\u0026user_vmcb-\u003econtrol, sizeof(*ctl));\n2093:\t\tif (IS_ERR(ctl))\n2094:\t\t\treturn PTR_ERR(ctl);\n2095:\t\n2096:\t\tsave = memdup_user(\u0026user_vmcb-\u003esave, sizeof(*save));\n2097:\t\tif (IS_ERR(save)) {\n2098:\t\t\tkfree(ctl);\n2099:\t\t\treturn PTR_ERR(save);\n"
} |
| 45/3 | 2026/10/08 00:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47499 output: 43 thoughts: 582 Thoughts: **Analyzing the Nested VMCB Allocation and Deallocation in the SVM Hypervisor** Okay, so I'm diving into this SVM hypervisor code, specifically the nested virtualization aspects. My first thought is about how the control structure (`ctl`) is handled. It's copied directly from userspace using `memdup_user`, which immediately flags a potential security vulnerability if not validated carefully. Then, `nested_copy_vmcb_control_to_cache` seems to be responsible for the actual copying into `svm->nested.ctl`. I need to remember to scrutinize the validation of all these userspace provided values. And, `asid` is just an integer that's read and compared to `last_asid` - simple enough. Now, let's get into the allocation and deallocation of the nested structures. I'm focusing on `svm_allocate_nested` and `svm->nested.asid02`. It looks like `svm->nested.vmcb02.ptr` serves as the primary indicator of nested virtualization allocation, rather than an explicit `svm->nested.initialized` flag. In `svm_allocate_nested`, a page is allocated via `snp_safe_alloc_page` for `vmcb02`, and the memory region for the `msrpm` is initialized too. Then, `svm->nested.asid02` is allocated via `allocate_asid` - standard procedure. And note, the `last_asid` is initialized to 0. But, hold on. This `vmcb02.ptr` being the allocation indicator... What happens to it when the nested virtualization is freed? Let's check `svm_free_nested`. Okay, it looks like `svm_free_nested` first checks if `svm->nested.vmcb02.ptr` is NULL, which is the check for allocation. Then, if it's deallocated, `free_asid` is called on the `svm->nested.asid02`. The `msrpm` is also freed. But what about `vmcb02.ptr` itself? I don't see anything explicitly freeing the `vmcb02.ptr`? Did I miss something? I need to look closely at how the `vmcb02_page` is handled. If it's a page, how is it getting freed? That's a critical oversight I'm looking for... |
| 46/3 | 2026/10/08 00:33 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/nested.c",
"FirstLine": 1590,
"LineCount": 35
}Results: {
"Contents": "1590:\tvoid svm_free_nested(struct vcpu_svm *svm)\n1591:\t{\n1592:\t\tif (!svm-\u003enested.vmcb02.ptr)\n1593:\t\t\treturn;\n1594:\t\n1595:\t\tif (WARN_ON_ONCE(svm-\u003evmcb != svm-\u003evmcb01.ptr))\n1596:\t\t\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\n1597:\t\n1598:\t\tfree_asid(svm-\u003enested.asid02);\n1599:\t\n1600:\t\tsvm_vcpu_free_msrpm(svm-\u003enested.msrpm);\n1601:\t\tsvm-\u003enested.msrpm = NULL;\n1602:\t\n1603:\t\t__free_page(virt_to_page(svm-\u003enested.vmcb02.ptr));\n1604:\t\tsvm-\u003enested.vmcb02.ptr = NULL;\n1605:\t\n1606:\t\t/*\n1607:\t\t * When last_vmcb12_gpa matches the current vmcb12 gpa,\n1608:\t\t * some vmcb12 fields are not loaded if they are marked clean\n1609:\t\t * in the vmcb12, since in this case they are up to date already.\n1610:\t\t *\n1611:\t\t * When the vmcb02 is freed, this optimization becomes invalid.\n1612:\t\t */\n1613:\t\tsvm-\u003enested.last_vmcb12_gpa = INVALID_GPA;\n1614:\t}\n1615:\t\n1616:\tvoid svm_leave_nested(struct kvm_vcpu *vcpu)\n1617:\t{\n1618:\t\tstruct vcpu_svm *svm = to_svm(vcpu);\n1619:\t\n1620:\t\tif (is_guest_mode(vcpu)) {\n1621:\t\t\tvcpu-\u003earch.nested_run_pending = 0;\n1622:\t\t\tsvm-\u003enested.vmcb12_gpa = INVALID_GPA;\n1623:\t\n1624:\t\t\tnested_svm_service_local_tlb_flushes(vcpu);\n"
} |
| 47/3 | 2026/10/08 00:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 49229 output: 39 thoughts: 1093 Thoughts: **Analyzing Potential Race Conditions in SEV Guest Migration and ASID Handling** Okay, I'm tracing through this code related to SEV guest migration and ASID handling. I see that `svm->nested.vmcb02.ptr` is being set to `NULL`, and `svm->nested.last_vmcb12_gpa` to `INVALID_GPA`. That's the starting point. Now, let's look at `sev_vcpu_migrate_asid`. This function seems straightforward: it frees and updates ASIDs during migration. No immediate red flags for uninitialized memory exposure here. It seems like it handles the nested ASID updates correctly, or so it seems. Now, onto the `invlpga_interception` function, and this is where it gets interesting. I see that the code checks for a non-zero ASID and, if it exists and differs from the *last* ASID, it skips the instruction. But then, I notice a potential issue. What happens if `svm->nested.vmcb02.ptr` is `NULL` when the `asid` is non-zero? Could L1 potentially execute `invlpga` before ever running VMRUN, or when the nested state isn't yet allocated? I can see a potential race there. Now, let's think. Before jumping to conclusions, I should probably check `nested_svm_check_permissions(vcpu)`. What exactly does this function do? I'm going to have to dive into that function to see if it provides any protections that I might have missed, or if it has its own vulnerabilities. Time to fire up the grepper and see if it is a vulnerability or not. |
| 48/3 | 2026/10/08 00:34 | tool | grepper |
0mArgs:{
"Expression": "nested_svm_check_permissions",
"PathPrefixes": [
"arch/x86/kvm/svm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=1734=int nested_svm_exit_handled(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1745-\narch/x86/kvm/svm/nested.c:1746:int nested_svm_check_permissions(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-1747-{\n--\narch/x86/kvm/svm/nested.c=2041=static int svm_set_nested_state(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-2116-\t * Validate host state saved from before VMRUN (see\narch/x86/kvm/svm/nested.c:2117:\t * nested_svm_check_permissions). Note that the g_pat field is not\narch/x86/kvm/svm/nested.c-2118-\t * validated, because (a) it may have been clobbered by SMM before\n--\narch/x86/kvm/svm/svm.c=2280=static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)\n--\narch/x86/kvm/svm/svm.c-2286-\narch/x86/kvm/svm/svm.c:2287:\tif (nested_svm_check_permissions(vcpu))\narch/x86/kvm/svm/svm.c-2288-\t\treturn 1;\n--\narch/x86/kvm/svm/svm.c=2325=static int vmrun_interception(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-2326-{\narch/x86/kvm/svm/svm.c:2327:\tif (nested_svm_check_permissions(vcpu))\narch/x86/kvm/svm/svm.c-2328-\t\treturn 1;\n--\narch/x86/kvm/svm/svm.c=2365=static int gp_interception(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-2384-\narch/x86/kvm/svm/svm.c:2385:\t\tif (nested_svm_check_permissions(vcpu))\narch/x86/kvm/svm/svm.c-2386-\t\t\treturn 1;\n--\narch/x86/kvm/svm/svm.c=2445=static int stgi_interception(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-2448-\narch/x86/kvm/svm/svm.c:2449:\tif (nested_svm_check_permissions(vcpu))\narch/x86/kvm/svm/svm.c-2450-\t\treturn 1;\n--\narch/x86/kvm/svm/svm.c=2457=static int clgi_interception(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-2460-\narch/x86/kvm/svm/svm.c:2461:\tif (nested_svm_check_permissions(vcpu))\narch/x86/kvm/svm/svm.c-2462-\t\treturn 1;\n--\narch/x86/kvm/svm/svm.c=2469=static int invlpga_interception(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-2476-\narch/x86/kvm/svm/svm.c:2477:\tif (nested_svm_check_permissions(vcpu))\narch/x86/kvm/svm/svm.c-2478-\t\treturn 1;\n--\narch/x86/kvm/svm/svm.h=923=int nested_svm_exit_handled(struct vcpu_svm *svm);\narch/x86/kvm/svm/svm.h:924:int nested_svm_check_permissions(struct kvm_vcpu *vcpu);\narch/x86/kvm/svm/svm.h-925-int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcpu);\n"
} |
| 49/3 | 2026/10/08 00:34 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 51898 output: 43 thoughts: 19 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Diving into `nested_svm_check_permissions` in `nested.c`** Alright, let's get down to brass tacks. My initial instinct is to dig into the `nested_svm_check_permissions` function, specifically within the `nested.c` file. This is where the permission checks for nested virtualization would reside, right? I'm anticipating this is likely a critical area for security and stability. I'm keen to see the specific logic for determining whether a guest can perform certain privileged operations, and what factors are considered. I'm looking for the standard checks: are we dealing with ring 0 access? What is the level of isolation? What are the underlying hardware limitations? This will undoubtedly touch on aspects of the underlying hypervisor's capabilities and its interaction with the nested virtualization environment. I will be looking to get a high level understanding first, then dive deep. |
| 50/3 | 2026/10/08 00:34 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/nested.c",
"FirstLine": 1745,
"LineCount": 25
}Results: {
"Contents": "1745:\t\n1746:\tint nested_svm_check_permissions(struct kvm_vcpu *vcpu)\n1747:\t{\n1748:\t\tif (!(vcpu-\u003earch.efer \u0026 EFER_SVME) || !is_paging(vcpu)) {\n1749:\t\t\tkvm_queue_exception(vcpu, UD_VECTOR);\n1750:\t\t\treturn 1;\n1751:\t\t}\n1752:\t\n1753:\t\tif (to_svm(vcpu)-\u003evmcb-\u003esave.cpl) {\n1754:\t\t\tkvm_inject_gp(vcpu, 0);\n1755:\t\t\treturn 1;\n1756:\t\t}\n1757:\t\n1758:\t\treturn 0;\n1759:\t}\n1760:\t\n1761:\tstatic bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,\n1762:\t\t\t\t\t\t u32 error_code)\n1763:\t{\n1764:\t\tstruct vcpu_svm *svm = to_svm(vcpu);\n1765:\t\n1766:\t\treturn (svm-\u003enested.ctl.intercepts[INTERCEPT_EXCEPTION] \u0026 BIT(vector));\n1767:\t}\n1768:\t\n1769:\tstatic void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)\n"
} |
| 51/3 | 2026/10/08 00:34 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52557 output: 40 thoughts: 2928 Thoughts: **Analysis of KVM Interception and ASID Handling** Okay, let's break this down systematically. My primary concern is ensuring that this patch doesn't introduce any vulnerabilities related to uninitialized memory, particularly when it comes to Address Space Identifiers (ASIDs) and Translation Lookaside Buffers (TLBs) within KVM's nested virtualization implementation for AMD SVM. First, I examined `invlpga_interception` and the conditional logic around `asid`. The initial question was, *what if* `asid` is zero? The original code flushes the gva, which seemed correct. The core of this patch seems to be refining how KVM handles the `svm->nested.last_asid` variable, specifically to prevent potential TLB pollution when nested virtualization is involved. The key observation is the `svm->nested.last_asid` and its relation to skipping TLB flushes. A zero ASID would flush the TLB. If ASID is non-zero, it checks if it's the same as the *last* ASID. If they're *different*, it skips. The only way those two values would be the same is if `svm->nested.last_asid` was specifically set to the given `asid`. This is done in `nested_svm_entry_tlb_flush`. The `nested_svm_entry_tlb_flush` function is only ever called when nested is running. Nested state is dependent on the allocation succeeding for `svm_allocate_nested`, so if the values match, and the ASID is non-zero, nested *must* have been allocated. That means `svm->nested.vmcb02.ptr` is non-NULL. From that, it followed to check where the `svm->nested.last_asid` is set, and whether nested allocation occurred. Then it's important to rule out the possibility of a NULL pointer dereference. The architecture's KASAN/oops would catch that. KVM Memory Sanitizer (KMSAN) is not the applicable tool here. Next, I needed to perform a more general check to rule out any information leak via the kernel-to-userspace interface (e.g. data that is sent to the userspace in responses that haven't been initialized). I inspected areas that interact with user space: 1. **CPUID Information:** Changes to `cpuid_8000_000A_EBX` that affect what's reported in KVM's CPUID information. A code comment had changed but the code was unchanged. Then, I dug through the relevant documentation and saw it's an arbitrary value. 2. **`svm_get_nested_state` and `svm_set_nested_state`:** These functions were examined, and there were changes that involve copying data in and out of nested state. The key was to ensure these functions do not leak uninitialized data. Specifically, I reviewed changes in `nested_copy_vmcb_cache_to_control` and `svm_set_nested_state`, verifying that no new uninitialized fields are being exposed to user space. 3. **`kvm_tlb_tag_t`:** I checked the type of `kvm_tlb_tag_t`. It is just `typedef unsigned int kvm_tlb_tag_t;`. It wasn't exposed to userspace. 4. **`kvm_mmu_sync_all_roots`:** The path does not have any obvious uninitialized memory issues. `kvm_mmu_sync_all_roots` would be called, and I confirmed that data structures involved are properly initialized. 5. **`tlb_tags` bitmap:** The bitmap allocation and freeing are done using `bitmap_zalloc` and `bitmap_free`, which handle zero initialization. 6. **`fallback_asid`:** Then I checked `fallback_asid` which is a `kvm_tlb_tag_t` (unsigned int). `allocate_asid` and `free_asid` functions confirmed its proper usage. I also considered a VMX side: 1. I checked the VMX side to see if `kvm_tlb_tag_t` is being used, because either VMX or SVM could be used, and this would depend on the CPU vendor. 2. I saw that both `vpid02` and `vpid` structs were zero-initialized. Finally, I checked: 1. **`sd->fallback_asid_vcpu`:** This is zero-initialized because the `svm_data` struct is per-cpu, and per-cpu BSS memory is zero initialized. |
| 52/3 | 2026/10/08 00:34 | tool | grepper |
0mArgs:{
"Expression": "is_noncanonical_invlpg_address",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/hyperv.c=1994=int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/hyperv.c-2019-\t\tfor (j = 0; j \u003c (entries[i] \u0026 ~PAGE_MASK) + 1; j++) {\narch/x86/kvm/hyperv.c:2020:\t\t\tif (is_noncanonical_invlpg_address(gva + j * PAGE_SIZE, vcpu))\narch/x86/kvm/hyperv.c-2021-\t\t\t\tcontinue;\n--\narch/x86/kvm/mmu/mmu.c=6772=void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,\n--\narch/x86/kvm/mmu/mmu.c-6782-\t\t/* INVLPG on a non-canonical address is a NOP according to the SDM. */\narch/x86/kvm/mmu/mmu.c:6783:\t\tif (is_noncanonical_invlpg_address(addr, vcpu))\narch/x86/kvm/mmu/mmu.c-6784-\t\t\treturn;\n--\narch/x86/kvm/svm/svm.c=2469=static int invlpga_interception(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-2485-\t */\narch/x86/kvm/svm/svm.c:2486:\tif (is_noncanonical_invlpg_address(gva, vcpu))\narch/x86/kvm/svm/svm.c-2487-\t\treturn kvm_skip_emulated_instruction(vcpu);\n--\narch/x86/kvm/vmx/nested.c=6080=static int handle_invvpid(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/nested.c-6132-\tif (type == VMX_VPID_EXTENT_INDIVIDUAL_ADDR \u0026\u0026\narch/x86/kvm/vmx/nested.c:6133:\t is_noncanonical_invlpg_address(operand.gla, vcpu))\narch/x86/kvm/vmx/nested.c-6134-\t\treturn nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);\n--\narch/x86/kvm/x86.c=10831=int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)\n--\narch/x86/kvm/x86.c-10861-\t\tif ((!pcid_enabled \u0026\u0026 (operand.pcid != 0)) ||\narch/x86/kvm/x86.c:10862:\t\t is_noncanonical_invlpg_address(operand.gla, vcpu)) {\narch/x86/kvm/x86.c-10863-\t\t\tkvm_inject_gp(vcpu, 0);\n--\narch/x86/kvm/x86.h=240=static inline bool is_noncanonical_base_address(u64 la, struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.h-244-\narch/x86/kvm/x86.h:245:static inline bool is_noncanonical_invlpg_address(u64 la, struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.h-246-{\n"
} |
| 53/3 | 2026/10/08 00:34 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 57537 output: 381 thoughts: 1115 Thoughts: **KMSAN Determination for KVM x86 TLB Flushing and ASID Management Patch Series** Okay, let's break this down. First, I need to confirm the KMSAN requirement. I'll start by checking the test file, which is likely irrelevant in this context because selftests operate in userspace. Now, the main question is whether this patch series necessitates a KMSAN run. KMSAN is expensive, so I only use it if KASAN, LOCKDEP, or standard detectors won't catch potential bugs. Specifically, I'm looking for new or modified code that introduces or exposes uninitialized memory risks detectable *only* by KMSAN. The rules are pretty clear. I need to ask: Does this touch kernel structures sent to userspace, conditional logic with uninitialized variables, complex data structure allocation where uninitialized fields are potentially read, or modifications to bounds checks that might expose uninitialized bytes? Looking at the patch, I see: 1. **Userspace Exposure:** No `copy_to_user`, `put_user`, netlink, ioctl output, or BPF buffer modifications. The CPUID or nested state ioctl outputs are untouched with respect to padding/uninitialized fields. So, no direct userspace exposure of uninitialized memory here. 2. **Conditional Logic:** Let's look at variables used in branches. `new_asid` is initialized to `false`. The `tlb_tags` bitmap gets `bitmap_zalloc` (so, zeroed). The `vcpu_svm` struct is allocated with `kmem_cache_zalloc` and all the newly added fields (`asid02`, `last_asid`) start at 0. `fallback_asid_vcpu` is a BSS per-cpu variable and starts as NULL. Local variables in the added functions (i, bit, nr, cpu, gva, asid) are all initialized before use. 3. **Complex Data Structures:** The TLB tag allocation via `bitmap_zalloc` zeroes the memory, which is good. 4. **Bounds/Length Checks:** No changes that could expose uninitialized data. This whole patch series is about TLB flushing, ASID tag management, MMU root syncing, and nested SVM VM-entry/exit TLB invalidation. The risk here is not uninitialized memory. Bugs would be logic errors, locking issues (LOCKDEP), or general memory management problems (KASAN), but nothing that specifically involves uninitialized memory reads or leaks. Therefore, `NeedsKMSAN` is definitely `false`. To be clear, here's my reasoning, and to make it fully clear: The patch refactors KVM x86 TLB flushing and ASID management on AMD SVM (and unifies VPID/ASID tag allocation with VMX), adds nested SVM ASID tracking for optimized TLB flushes on nested transitions, adds `KVM_REQ_MMU_SYNC_ALL_ROOTS`, and updates INVLPGA handling. KMSAN detects uninitialized memory reads and potential information leaks to userspace. This patch *doesn't* introduce or modify any copy-to-user paths, info-leaks, or uninitialized stack/heap allocations. Newly added heap memory (`tlb_tags.bitmap`) is zero-allocated, and the `vcpu_svm` struct (with new `asid02`, `last_asid` fields) is also zeroed on allocation. Any potential concurrency, locking, or memory corruption problems would be caught by LOCKDEP or KASAN, not KMSAN. Hence, a dedicated KMSAN session is not justified, and `NeedsKMSAN` is `false`. I am satisfied that this is a conclusive determination. Now, I will invoke `set-results`. |
| 54/3 | 2026/10/08 00:35 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series refactors KVM x86 TLB flushing, unifies TLB tag (VPID/ASID) allocation between VMX and SVM via bitmap_zalloc, improves nested SVM ASID tracking and TLB invalidation handling, and introduces KVM_REQ_MMU_SYNC_ALL_ROOTS.\n\nKMSAN vs KASAN applicability:\n1. No uninitialized memory usage:\n - The newly allocated bitmap in `kvm_init_tlb_tags` is allocated using `bitmap_zalloc()`, which zeroes all allocated memory.\n - New struct members in `vcpu_svm` and `svm_nested_state` (`asid02`, `last_asid`) belong to `vcpu_svm`, which is allocated via `kmem_cache_zalloc()`, ensuring they are zero-initialized.\n - All stack local variables in modified/added functions (such as `new_asid`, `bit`, `nr`, `fifo`) are explicitly initialized before being read or used in conditional logic.\n2. No kernel-to-user info-leaks:\n - The patch does not modify or introduce any data copied to userspace (no `copy_to_user`, `put_user`, or ioctl output alterations).\n3. Potential bug surfaces:\n - Potential errors in this series relate to TLB synchronization correctness, logic flaws in ASID reuse, concurrency, or locking, which are either architectural emulation correctness issues or covered by KASAN and LOCKDEP.\n\nBecause no uninitialized memory risks or info-leaks are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series refactors KVM x86 TLB flushing, unifies TLB tag (VPID/ASID) allocation between VMX and SVM via bitmap_zalloc, improves nested SVM ASID tracking and TLB invalidation handling, and introduces KVM_REQ_MMU_SYNC_ALL_ROOTS.\n\nKMSAN vs KASAN applicability:\n1. No uninitialized memory usage:\n - The newly allocated bitmap in `kvm_init_tlb_tags` is allocated using `bitmap_zalloc()`, which zeroes all allocated memory.\n - New struct members in `vcpu_svm` and `svm_nested_state` (`asid02`, `last_asid`) belong to `vcpu_svm`, which is allocated via `kmem_cache_zalloc()`, ensuring they are zero-initialized.\n - All stack local variables in modified/added functions (such as `new_asid`, `bit`, `nr`, `fifo`) are explicitly initialized before being read or used in conditional logic.\n2. No kernel-to-user info-leaks:\n - The patch does not modify or introduce any data copied to userspace (no `copy_to_user`, `put_user`, or ioctl output alterations).\n3. Potential bug surfaces:\n - Potential errors in this series relate to TLB synchronization correctness, logic flaws in ASID reuse, concurrency, or locking, which are either architectural emulation correctness issues or covered by KASAN and LOCKDEP.\n\nBecause no uninitialized memory risks or info-leaks are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|