summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kvm/x86
AgeCommit message (Collapse)Author
9 daysselftests: kvm: remove useless comma operatorPaolo Bonzini
This is the result of an incorrect conflict resolution in merge commit 994489f04ebb ("Merge tag 'kvm-x86-selftests-7.4' of https://github.com/kvm-x86/linux into HEAD", 2026-10-16). Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
14 daysMerge tag 'kvm-x86-selftests-7.4' of https://github.com/kvm-x86/linux into HEADPaolo Bonzini
KVM selftests changes and a few KVM x86 fixes for 7.4 - Fix emulation of VMPTRST and VMPTRLD when eVMCS is in-use to match real Hyper-V's behavior. - Saturate L2's TSC frequency if stacking L1 and L2 scaling multipliers exceeds what hardware supports, as attempting to run L2 with an illegal multiplier will either cause VM-Entry to fail (VMX) or trigger a #GP when writing MSR_AMD64_TSC_RATIO (SVM). The #GP is thankfully benign in the grand scheme as even "unsafe" WRMSRs have exception fixup. - Rework KVM selftest's handling of memory regions so that PPC can force its page table allocations to be naturally aligned. - Clean up selftest's VMX instruction helpers, and drop an absurd number of completely useless return values, e.g. functions that unconditionally return %true and then have every caller assert success. - Add selftests support for Zhaoxin CPUs. - Overhaul the TSC scaling test to add a regression test for the saturation behavior, and to increase its coverage in general. - Fix selftests usage various NUMA syscalls to account for an undocumented, but known since 2007, off-by-one kernel bug. Add helpers to try and avoid re-introducing the same flaws in the future. - Automatically run xAPIC IPI migration test (between NUMA nodes) when possible, as the entire point of the test is verify KVM's handling of page migration of the APIC backing page. - Misc cleanups.
14 daysMerge tag 'kvm-x86-misc-7.4' of https://github.com/kvm-x86/linux into HEADPaolo Bonzini
KVM x86 misc changes for 7.4 - Mask off the address space ID when checking for illegal usage of kvm_set_internal_memslot() to prevent false negatives on the sanity check. - Explicitly zero reserved flags when delivering a SynIC message to avoid leaking a byte of stack information to the guest. - Rework TDX's handling of the canonical MMU root level to not rely on userspace to fill CPUID correctly, as doing so lets userspace trivially trigger a KVM_BUG_ON(). - Reject KVM_SET_NESTED_STATE if userspace "restores" L1 with EFER.LMA=1 && EFER.LME=0, which confuses KVM into trying to configure an 64-bit MMU without PAE. - Ignore EFER.LMA (clear it from KVM's cached save state) if EFER.LME=0 when preparing L2 state, to fix another way to confuse KVM's MMU (and because real hardware also ignores EFER.LMA). - Improve KVM's sanity checks for a misconfigured MMU to (hopefully) detect all of the ways KVM can botch EFER.LMA vs. CR4.PAE. - Remove the "full" out-param from kvm_x86_ops.flush_tlb_gva() as the param is only used to avoid what effectively amounts to a single memory write. - Fix a mostly-benign bug where KVM can write through a guest's read-only mapping due to not checking the error code when expediting emulated MMIO page faults. - Honor the guest's view of the "EFER.LMSLE must be zero" defeature so that userspace can provide migration compatibility between Rome and Milan+. - Pend a KVM_REQ_EVENT request when an exception is morphed into a nested VM-Exit, otherwise KVM can fail to actually deliver the nested VM-Exit, e.g. when injecting a page fault. - Add support for PML on SVM. - Add support for virtualizing Bus Lock Detect on SVM, and fix a number of DR6 bugs and warts along the way. - Misc fixes and cleanups.
14 daysMerge tag 'kvm-x86-coco-7.4' of https://github.com/kvm-x86/linux into HEADPaolo Bonzini
KVM guest_memfd and CoCo changes for 7.4 - Take the invalidate lock when unbinding a dying file to fix a race with kvm_gmem_invalidate_end() that results in KVM leaving the MMU invalidation count elevated. Not taking the lock in the first place was pure misguided paranoid. - When binding a memslot, purge the bindings if storing the new memslot fails, as Xarray's "store range" API can subtly fail partway through. - Establish memslot<=>guest_memfd bindings *after* the memslot is ready, as the memslot becomes reachable the instant the binding is installed. - Don't grab a page refcount when reloading an SNP VMSA and stop returning a "struct page" from guest_memfd's PFN lookup altogether, to ensure that KVM doesn't hold any unnecessary refcounts (the lifetime of the page is ensured by MMU notifiers and similar hooks). Preventing unnecessary refcounts greatly simplifies supporting in-place SHARED=>PRIVATE conversions. - Add support for in-place PRIVATE<=>SHARED conversion, i.e. allow a single guest_memfd instance to be used for both PRIVATE and SHARED variations of CoCo guests' memory (though not at the same time). This eliminates the need to double allocate guest memory for TDX and SNP, and is basically a hard requirement for pKVM, which relies on being able to preserve memory contents on conversions. - Hack-a-fix a deadlock bug in ->error_remove_folio() by returning -EBUSY if the filemap invalidate lock is contended. Punt on a proper fix for the moment, as a proper fix likely involves reworking truncate_error_folio(). - Add a pile of selftests for in-place conversions. - Misc guest_memfd cleanups Conflicts in arch/arm64/kvm/mmu.c: convert to use s2vi.
2026-09-30KVM: selftests: Spell out UCALL in nested TSC scaling test's enumsSean Christopherson
Explicitly spell out UCALL instead of prefixing only U in the nested TSC scaling test's ucall commands, as it's all too easy to think USLEEP means "do usleep()", when it actually means "do sleep() in userspace". Opportunistically name the enum and take an enum instead of a bare integer for the to-be-checked level, to make it more obvious that the fact that the level values happen to correspond to '1' and '2' for the nesting level is largely coincidental (or perhaps it was intentional, but it's not a hard requirement for the test). Link: https://patch.msgid.link/20260930173635.3362655-22-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Use GUEST_SYNC2() in nested TSC scaling testSean Christopherson
Use GUEST_SYNC2() instead of open coding equivalent functionality using raw ucall() invocations in the nested TSC scaling test. No functional change intended. Link: https://patch.msgid.link/20260930173635.3362655-21-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Randomize L2's TSC offset in the nested TSC scaling testSean Christopherson
Randomize L2's TSC offset in the nested TSC scaling test, not because it's all that interesting, but because hardcoding a copmletely arbitrary value is all kinds of silly. Link: https://patch.msgid.link/20260930173635.3362655-20-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Test non-zero TSC offset on SVM in nested TSC scaling testSean Christopherson
Set L2's TSC offset for SVM as well as VMX in the nested TSC scaling test. Odds are good skipping SVM was pure oversight. Fixes: e6bcdd212238 ("KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM") Link: https://patch.msgid.link/20260930173635.3362655-19-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Verify that KVM saturates L2 TSC freq on {under,over}flowSean Christopherson
Extend the nested TSC scaling test to validate the extreme ends of what hardware supports, i.e. validate that KVM allows L1 to scale L2's virtual TSC frequency up/down to the architectural maximum/minimum, from L1's perspective, and that KVM saturates L2's effective frequency when L2's frequency can't be virtualized by hardware (which is possible when stacking multipliers/ratios across L1 and L2). Link: https://patch.msgid.link/20260930173635.3362655-18-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Test L1 "up" and L2 "down" in nested TSC scaling testSean Christopherson
Now that the guts of the nested TSC scaling test support scaling L2 down, validate all four combinations of scaling up/down L1 and L2. Ideally, the test would also validate integer+fractional scale up scenarios, but prioritize keeping the test and math somewhat understandable. Opportunistically drop the stale file comment about the test's name. Link: https://patch.msgid.link/20260930173635.3362655-17-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Sanity check KVM's default TSC freq in nested TSC scaling testSean Christopherson
Explicitly verify that both L1 and L2 can run at KVM's default TSC frequency reported via KVM_GET_TSC_KHZ, e.g. that KVM_SET_TSC_KHZ can take in what KVM_GET_TSC_KHZ spits out. Link: https://patch.msgid.link/20260930173635.3362655-16-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Explicitly pass TSC frequencies to guts of TSC scaling testSean Christopherson
Explicitly pass the L1 and L2 TSC frequencies, along with the L2 multiplier, to the main test routine in the nested TSC scaling test. This will allow testing both "scale up" and "scale down" scenarios, and that KVM saturates L2's frequency if it's scale up/down beyond what hardware can support, in which case the expected frequency will not exactly match the L2 multiplier. Link: https://patch.msgid.link/20260930173635.3362655-15-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Use KVM's reported TSC KHz as L0's frequency (sanity checked)Sean Christopherson
Use KVM's reported default TSC frequency as L0's frequency instead of calibrating L0's frequency using RDTSC. Trying to calibrate the frequency in userspace is all but guaranteed to be wildly inaccurate, e.g. due to interrupts, preemption, etc, and no known CPU supports TSC scaling without also having a constant TSC. I.e. there's no need to calibrate the current frequency, as it should also be the same as KVM's default frequency. Opportunistically sanity check KVM's reported frequency against what is observed via RDTSC, e.g. so that KVM_GET_TSC_KHZ or platform issues show up much earlier in the test, before VMs get involved. Link: https://patch.msgid.link/20260930173635.3362655-14-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Allow +/- 1 tolerance if expected TSC frequency is <100Sean Christopherson
Massage the nested TSC scaling test's checker to allow for validating an expected TSC frequency of 1Hz, which is achievable for L2 since L1 can use a completely arbitrary fractional ratio. To do so, set the minimum tolerance to 1, and make the thresholds inclusive, i.e. for an expected frequency of 1, allow the actual frequency to be in the range of [0,2]. Because scaling L2's frequency down to a single Hz makes the virtual TSC run soooo comically slow, and because all of the math is done with basic integer arithmetic, it's very easy for the test to observe what appears to be a frequency of 0. Link: https://patch.msgid.link/20260930173635.3362655-13-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Print out the failing L{0,1,2} level in nested TSC scaling testSean Christopherson
Print out which one of L0, L1, or L2 TSC frequency checks failed if the nested TSC scaling test detects an out-of-bounds frequency. Triaging test failures without the level information is unnecessarily painful. Link: https://patch.msgid.link/20260930173635.3362655-12-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Track L2 multiplier, not scale-up factor, in nested TSC testSean Christopherson
Track L2's multiplier, not just the non-fractional scale-up factor, so that the nested TSC scaling test can also validate scaling L2's frequency *down*. No functional change intended. Link: https://patch.msgid.link/20260930173635.3362655-11-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Extract guts of nested TSC scaling test to helper functionSean Christopherson
Extract the guts of the nested TSC scaling test to a separate helper in anticipation of extending the test beyond its single "scale L1 up, L2 down" testcase. No functional change intended. Link: https://patch.msgid.link/20260930173635.3362655-10-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Rename TSC freq checkers in nested TSC scaling testSean Christopherson
Rename the nested TSC scaling test's helpers to clarify which one is used by guest code and which one is used by host code, and to make it more obvious that the host version *checks* the result and asserts on errors. Contrary to what "compare" suggests, the helper doesn't return whether or not the actual vs. expected values match. Link: https://patch.msgid.link/20260930173635.3362655-9-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Randomize L2's scale factor in nested TSC scaling testSean Christopherson
Randomize L2's scale factor in the nested TSC scaling test; the test's comment that "using a global variable does not work" just means the author didn't know how to use sync_global_to_guest(). Link: https://patch.msgid.link/20260930173635.3362655-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Drop unnecessary use of PRIu64 in nested TSC scaling testSean Christopherson
Use a plain "lu" instead of "PRIu64" to print u64s in the nested TSC scaling test, as KVM selftests are 64-bit only (and supporting 32-bit x86 selftest would be an absurdly large lift, for something absolutely no one cares about). Link: https://patch.msgid.link/20260930173635.3362655-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Drop redundant VMWRITE of TSC_MULTIPLIER_HIGHSean Christopherson
Drop a redundant VMWRITE of TSC_MULTIPLIER_HIGH in the nested TSC scaling test. KVM selftests are 64-bit only, i.e. the full 64-bit multiplier is always written by setting TSC_MULTIPLIER. Link: https://patch.msgid.link/20260930173635.3362655-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Use KVM's pRNG to randomize L1's TSC ratio in TSC scaling testSean Christopherson
Use KVM's pRNG to randomize L1's TSC ratio in the nested TSC scaling test, so that reproducing a failure with a specific seed is slightly easier. Link: https://patch.msgid.link/20260930173635.3362655-5-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Add coverage for the EFER_LMSLE_MBZ defeatureJim Mattson
Add coverage for KVM's virtualization of EFER_LMSLE_MBZ, CPUID.80000008H:EBX[bit 20], which is set when the CPU does *not* support long mode segment limits. Verify that KVM's enumeration of the defeature matches the expectation derived from hardware plus kvm_amd's "nested" module param, so that the assertion doesn't compare KVM's enumeration against itself, and that WRMSR(EFER), nested VMRUN, and KVM_SET_SREGS reject EFER.LMSLE=1 if and only if guest CPUID enumerates the defeature. Also verify that host-initiated KVM_SET_MSRS is exempt from the check, as host-initiated writes skip guest CPUID checks so that userspace can set MSRs before it sets guest CPUID. Extend svm_nested_efer_test rather than add yet another test binary for a single CPUID bit; the test already has the L1/L2 harness needed to exercise EFER.LMSLE. Note, only a CPU that supports LMSLE, i.e. Rome and earlier, exercises the emulation path; elsewhere KVM enumerates the defeature straight from hardware. Opportunistically drop the unnecessary #include of vmx.h. Assisted-by: LLM Signed-off-by: Jim Mattson <jmattson@google.com> [sean: drop specific APM volume references, use common "nested" getter] Link: https://patch.msgid.link/20260930212114.3500045-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-30KVM: selftests: Rename svm_nested_clear_efer_svme to svm_nested_efer_testJim Mattson
Rename svm_nested_clear_efer_svme.c to svm_nested_efer_test.c so that the test can host coverage for other EFER bits whose behavior is tied to nested SVM, and so that the name matches the svm_nested_<topic>_test convention used by svm_nested_pat_test, svm_nested_shutdown_test, etc. No functional change intended. Assisted-by: LLM Signed-off-by: Jim Mattson <jmattson@google.com> Link: https://patch.msgid.link/20260930212114.3500045-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-28KVM: selftests: Extend nested x2APIC test to validate using eVMCS for vmcs12Sean Christopherson
Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20260813223610.2043560-7-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2026-09-28KVM: selftests: Extend nested x2APIC test to validate disabling x2APIC virtSean Christopherson
Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20260813223610.2043560-6-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2026-09-26KVM: selftests: Verify that L0's TPR doesn't get clobberedSean Christopherson
Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20260813223610.2043560-5-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2026-09-26KVM: selftests: Run the nested x2APIC with and without APICv being inhibited ↵Sean Christopherson
in L2 Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20260813223610.2043560-4-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2026-09-26KVM: selftests: Add x2APIC MSR test for inhibiting APICv while nestedSean Christopherson
Add a selftest to verify that KVM intercepts x2APIC MSR accesses for L1 after APICv is inhibited while L2 is active. This is a regression test for an AVIC bug where KVM would skip updating x2APIC MSR intercepts while L2 is active, thus giving L1 access to a wide swath of L0's x2APIC surface. Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20260710162052.2188574-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2026-09-22KVM: selftests: Update private memory exits test to work with per-gmem ↵Sean Christopherson
attributes Skip setting memory to private in the private memory exits test when using per-gmem memory attributes, as memory is initialized to private by default for guest_memfd, and using vm_mem_set_private() on a guest_memfd instance requires creating guest_memfd with GUEST_MEMFD_FLAG_MMAP (which is totally doable, but would need to be conditional and is ultimately unnecessary). Expect an emulated MMIO instead of a memory fault exit when attributes are per-gmem, as deleting the memslot effectively drops the private status, i.e. the GPA becomes shared and thus supports emulated MMIO. Skip the "memslot not private" test entirely, as private vs. shared state for x86 software-protected VMs comes from the memory attributes themselves, and so when doing in-place conversions there can never be a disconnect between the expected and actual states. Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-44-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Update private_mem_conversions_test for in-place conversionsAckerley Tng
Update private_mem_conversions_test for in-place conversions. In-place conversions support is detected in selftests with kvm_has_gmem_attributes. With in-place conversions, specifying userspace_addr from some other memory provider that is not the guest_memfd associated with the memslot is a user error, since KVM will only use both shared and private memory from the guest_memfd. Hence, when kvm_has_gmem_attributes, only test in-place conversions with single backing, where guest_memfd provides both shared and private memory. For single backing, guest_memfd must be created with GUEST_MEMFD_FLAG_MMAP. Initialize the guest_memfd as shared to align with how memory would default to shared when shared/private state was tracked at the VM level (the test expects this, it was written for non-in-place-conversions). When handling a hypercall to set attributes, use vm_mem_set_memory_attributes() to send the ioctl to the guest_memfd instead of the VM. When testing in-place conversions (single-backing), don't allow the user to configure src_type, since src_type will be ignored. Don't use src_type to determine alignment for rounding up per-cpu test memory size, since guest_memfd's backing page size is always the system PAGE_SIZE. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-43-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Reset shared memory after hole-punchingAckerley Tng
private_mem_conversions_test used to reset the shared memory that was used for the test to an initial pattern at the end of each test iteration. Then, it would punch out the pages, which would zero memory. Without in-place conversion, the resetting would write shared memory, and hole-punching will zero private memory, hence resetting the test to the state at the beginning of the for loop. With in-place conversion, resetting writes memory as shared, and hole-punching zeroes the same physical memory, hence undoing the reset done before the hole punch. Move the resetting after the hole-punching, and reset the entire PER_CPU_DATA_SIZE instead of just the tested range. With in-place conversion, this zeroes and then resets the same physical memory. Without in-place conversion, the private memory is zeroed, and the shared memory is reset to init_p. This is sufficient since at each test stage, the memory is assumed to start as shared, and private memory is always assumed to start zeroed. Conversion zeroes memory, so the future test stages will work as expected. Fixes: 43f623f350ce1 ("KVM: selftests: Add x86-only selftest for private memory conversions") Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-38-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Test conversion with elevated page refcountAckerley Tng
Add a selftest to verify that converting a shared guest_memfd page to a private page fails if the page has an elevated reference count. When KVM converts a shared page to a private one, it expects the page to have a reference count equal to the reference counts taken by the filemap. If another kernel subsystem holds a reference to the page, the conversion must be aborted. The test asserts that both bulk and single-page conversion attempts correctly fail with EAGAIN for the pinned page. After the page is unpinned, the test verifies that subsequent conversions succeed. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-37-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Test that shared/private status is consistent across processesSean Christopherson
Add a test to verify that a guest_memfd's shared/private status is consistent across processes, and that any shared pages previously mapped in any process are unmapped from all processes. The test forks a child process after creating the shared guest_memfd region so that the second process exists alongside the main process for the entire test. The processes then take turns to access memory to check that the shared/private status is consistent across processes. Co-developed-by: Ackerley Tng <ackerleytng@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-35-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Test that truncation does not change shared/private statusAckerley Tng
Add a test to verify that deallocating a page in a guest memfd region via fallocate() with FALLOC_FL_PUNCH_HOLE does not alter the shared or private status of the corresponding memory range. When a page backing a guest memfd mapping is deallocated, e.g., by punching a hole or truncating the file, and then subsequently faulted back in, the new page must inherit the correct shared/private status tracked by guest_memfd. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-34-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Convert with allocated folios in different layoutsAckerley Tng
Add a guest_memfd selftest to verify that memory conversions work correctly with allocated folios in different layouts. By iterating through which pages are initially faulted, the test covers various layouts of contiguous allocated and unallocated regions, exercising conversion with different range layouts. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-33-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Test conversion before allocationAckerley Tng
Add two test cases to the guest_memfd conversions selftest to cover the scenario where a conversion is requested before any memory has been allocated in the guest_memfd region. The KVM_SET_MEMORY_ATTRIBUTES2 ioctl can be called on a memory region at any time. If the guest had not yet faulted in any pages for that region, the kernel must record the conversion request and apply the requested state when the pages are eventually allocated. The new tests cover both conversion directions. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-32-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Test conversion precision in guest_memfdAckerley Tng
The existing guest_memfd conversion tests only use single-page memory regions. This provides no coverage for multi-page guest_memfd objects, specifically whether KVM correctly handles the page index for conversion operations. An incorrect implementation could, for example, always operate on the first page regardless of the index provided. Add a new test case to verify that conversions between private and shared memory correctly target the specified page within a multi-page guest_memfd. This test also verifies the precision of memory conversions by converting a single page and then iterating through all other pages to ensure they remain in their original state. To support this test, add a new GMEM_CONVERSION_MULTIPAGE_TEST_INIT_SHARED macro that handles setting up and tearing down the VM for each page iteration. The teardown logic is adjusted to prevent a double-free in this new scenario. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-31-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Test conversion flow when INIT_SHAREDAckerley Tng
Add a test case to verify that conversions between private and shared memory work correctly when the memory is initially created as shared. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-30-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Test basic single-page conversion flowAckerley Tng
Add a selftest for the guest_memfd memory attribute conversion ioctls. The test starts the guest_memfd as all-private (the default state), and verifies the basic flow of converting a single page to shared and then back to private. Add infrastructure that supports extensions to other conversion flow tests. This infrastructure will be used in upcoming patches for other conversion tests. Add test as an x86-specific test since guest_memfd's testing vehicle (KVM_X86_SW_PROTECTED_VM) is x86-specific. Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-29-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-22KVM: selftests: Add support for mmap() on guest_memfd in core librarySean Christopherson
Accept gmem_flags in vm_mem_add() to be able to create a guest_memfd within vm_mem_add(). When vm_mem_add() is used to set up a guest_memfd for a memslot, set up the provided (or created) gmem_fd as the fd for the user memory region. This makes it available to be mmap()-ed from just like fds from other memory sources. For guest_memfds, mmap() using gmem_offset instead of 0 all the time. Always use MAP_SHARED if mmap-ing from guest_memfd instead of reading flag from the configured src_type, which doesn't include guest_memfd. Add a kvm_slot_to_fd() helper to provide convenient access to the file descriptor of a memslot. Update existing callers of vm_mem_add() to pass 0 for gmem_flags to preserve existing behavior. Co-developed-by: Ackerley Tng <ackerleytng@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-26-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add CR8 reserved-bit checks to set_sregs_testTharit Tangkijwanichakul
Extend set_sregs_test to cover CR8. Verify that the architecturally allowed bits [3:0] can be set and read back, that every reserved bit [63:4] is rejected by KVM_SET_SREGS, and that a successful set is observable via KVM_GET_SREGS. Without the accompanying fix to kvm_is_valid_sregs(), setting a reserved CR8 bit is silently accepted and the read-back value diverges from the value written, so this test fails on an unpatched kernel and passes once the fix is applied. Signed-off-by: Tharit Tangkijwanichakul <tharitt97@gmail.com> Link: https://patch.msgid.link/20260908160426.6547-3-tharitt97@gmail.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Check for APIC access virtualization support in said testFrank Zhu
Not all CPUs that support VMX necessarily support the "virtualize APIC accesses" secondary processor-based VM-execution control. For example, some Zhaoxin CPUs lack this feature. Add a capability check before running the test to properly skip on unsupported hardware instead of failing. Carve out the guts of kvm_cpu_has_ept() into a generic helper and use said helper to provide kvm_cpu_has_vmx_apic_access_virt(). Signed-off-by: Frank Zhu <frankzhu@zhaoxin.com> Link: https://patch.msgid.link/20260910115414.3015260-10-ewanhai-oc@zhaoxin.com [sean: add a helper instead of copy+pasting kvm_cpu_has_ept()] Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add Zhaoxin CPU support to nested exceptions testFrank Zhu
When vectoring an injected #SS through the empty L2 IDT, Zhaoxin CPUs report the resulting #GP with the IDT bit set and EXT clear. Zhaoxin sets EXT only for interrupt delivery and selected guest-mode events; #SS delivery is not one of those cases. Expect error code 0x62 when running the test on Zhaoxin CPUs. Signed-off-by: Frank Zhu <frankzhu@zhaoxin.com> Link: https://patch.msgid.link/20260910115414.3015260-9-ewanhai-oc@zhaoxin.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Extend Intel MSR_IA32_UCODE_REV quirk to ZhaoxinFrank Zhu
Zhaoxin CPUs use KVM's VMX backend and therefore observe the same KVM-defined initial value as Intel vCPUs. Update feature_msrs_test to expect 0x100000000ULL for Zhaoxin vCPUs. Signed-off-by: Frank Zhu <frankzhu@zhaoxin.com> Link: https://patch.msgid.link/20260910115414.3015260-8-ewanhai-oc@zhaoxin.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add Zhaoxin CPU support in x86's "fix" hypercall testFrank Zhu
Zhaoxin CPUs use VMCALL as the native hypercall instruction. Use host_cpu_is_intel_compatible so that fix_hypercall_test selects the VMX hypercall path on Zhaoxin CPUs. Signed-off-by: Frank Zhu <frankzhu@zhaoxin.com> Link: https://patch.msgid.link/20260910115414.3015260-6-ewanhai-oc@zhaoxin.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Allow running invalid guest state test on Zhaoxin CPUsFrank Zhu
The vmx_exception_with_invalid_guest_state test currently requires an Intel CPU. However, Zhaoxin CPUs also support VMX and the relevant invalid guest state handling under KVM. Update the test requirement to allow execution on Zhaoxin platforms in addition to Intel, enabling broader coverage of VMX-related error paths. Signed-off-by: Frank Zhu <frankzhu@zhaoxin.com> Link: https://patch.msgid.link/20260910115414.3015260-5-ewanhai-oc@zhaoxin.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Skip xAPIC IPI migration test when forced but unsupportedSean Christopherson
Skip the xAPIC IPI migration test instead of failing outright if migration is forced by the user, but is unsupported due to lack of NUMA support. Failing the test is arguably "fine" for opt-in behavior, but in practice doesn't allow running the test in forced-migration mode in any kind of automated test environment. Reviewed-by: Shivank Garg <shivankg@amd.com> Link: https://patch.msgid.link/20260903001625.2792367-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Automatically run xAPIC IPI migration test when possibleSean Christopherson
Rework the interface to the xAPIC IPI test to automatically run the migration testcase if at least two NUMA nodes are found. The test exists specifically to validate KVM's handling of migration of the APIC backing page, i.e. forcing end users to opt-in to running the test in migration mode largely defeats the purpose of the test. Run both the "sleeping" and "migration" testcases by default, e.g. so that the more basic testcase will fail if KVM completely breaks IPI delivery. Keep the -m / migration parameter so that infrastructure that wants to specifically test the migration case can do so and not get false passes. Reviewed-by: Shivank Garg <shivankg@amd.com> Link: https://patch.msgid.link/20260903001625.2792367-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add common helper to get mask+number of usable memory NUMA nodesSean Christopherson
Extract and slightly adopt the xAPIC IPI test's logic for getting the mask and number of usable memory NUMA nodes into a common helper. To allow for friendlier behavior when the underlying kernel doesn't support NUMA, or the test was run without sufficient permissions, zero out the mask and return "zero nodes" if get_mempolicy() fails with ENOSYS or EPERM respectively. I.e. allow tests to skip (sub)tests instead of throwing an assert. Suggested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Shivank Garg <shivankg@amd.com> Link: https://patch.msgid.link/20260903001625.2792367-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>