summaryrefslogtreecommitdiff
path: root/tools/testing
AgeCommit message (Collapse)Author
2026-09-11KVM: selftests: Add TDP detection for Zhaoxin CPUsFrank Zhu
Update the kvm_is_tdp_enabled() function to include Zhaoxin CPUs by querying checking "Intel compatible" when checking for EPT (Extended Page Tables) support. This ensures that Zhaoxin CPUs use the appropriate TDP (Two-Dimensional Paging) parameter check, similar to Intel CPUs. Signed-off-by: Frank Zhu <frankzhu@zhaoxin.com> Link: https://patch.msgid.link/20260910115414.3015260-4-ewanhai-oc@zhaoxin.com [sean: massage changelog] Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use host_cpu_is_intel_compatible for mediated PMUFrank Zhu
kvm_is_mediated_pmu_enabled() returns kvm_amd's enable_mediated_pmu module parameter unless the host CPU is Intel. Zhaoxin CPUs implement VMX-compatible virtualization and are handled by KVM's VMX backend, so on Zhaoxin hosts the helper wrongly reports kvm_amd's parameter. Gate the check on host_cpu_is_intel_compatible so that Zhaoxin CPUs, like Intel CPUs, query kvm_intel's enable_mediated_pmu instead. This does not change behavior on Intel, and AMD and other non-Intel- compatible CPUs still fall back to kvm_amd's parameter. Signed-off-by: Frank Zhu <frankzhu@zhaoxin.com> Link: https://patch.msgid.link/20260910115414.3015260-3-ewanhai-oc@zhaoxin.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add Zhaoxin CPU detection supportFrank Zhu
Add helpers to detect Zhaoxin CPUs using the "CentaurHauls" and " Shanghai " vendor strings. KVM handles Zhaoxin CPUs with its VMX backend. Add host_cpu_is_intel_compatible for selftests that exercise behavior shared by Intel and Zhaoxin CPUs, while retaining host_cpu_is_zhaoxin for Zhaoxin-specific behavior. Signed-off-by: Frank Zhu <frankzhu@zhaoxin.com> Link: https://patch.msgid.link/20260910115414.3015260-2-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>
2026-09-11KVM: selftests: Compute node masks on-demand in xAPIC IPI testSean Christopherson
Compute the node masks for the source (from) and destination (to) NUMA nodes in the xAPIC IPI test instead of pre-filling an array of masks with node per mask. Computing the mask on-demand is technically slower, but doesn't require a large-ish on-stack array, and more importantly allows the test to use a generic "get next NUMA node" API without having to commit all of KVM selftests to using a large array of single-bit nodemasks. Implement said API as a common KVM NUMA API so that it can be used by other tests, e.g. in guest_memfd tests. Deliberately make @from "exclusive" as the anticipated usage in KVM selftests is to select the next, *different* node, i.e. so that users don't have to copy+paste code to assert that the found node is different than the starting node. The obvious downside is that implementing the exclusive logic forces callers to pass -1 instead of 0 when the goal is to find the first node in the mask, but that arguably yields more intuitive code anyways. From an overall test functionality/coverage perspective, no functional change intended (the walking pattern of node migration should be unchanged). Reviewed-by: Shivank Garg <shivankg@amd.com> Link: https://patch.msgid.link/20260903001625.2792367-5-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: use BITS_PER_TYPE() for NUMA masksShivank Garg
Replace the open-coded sizeof() * 8 calculations in do_migrations() with BITS_PER_TYPE(). No functional change intended. Signed-off-by: Shivank Garg <shivankg@amd.com> Link: https://patch.msgid.link/20260903001625.2792367-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Fix maxnode argument to migrate_pages() in xapic_ipi_testShivank Garg
Use MAXNODE_FOR_MASK() to compute the @maxnode argument when migrating pages in the xAPIC IPI test, as the current code incorrectly passes the number of bytes, not the number of bits, in the mask, and also fails to account for the kernel's off-by-one bug. I.e. the test sets maxnode to 8, and so the kernel only checks node IDs 0-6 even though the nodemask covers node IDs 0-63. Fixes: 678e90a349a4 ("KVM: selftests: Test IPI to halted vCPU in xAPIC while backing page moves") Signed-off-by: Shivank Garg <shivankg@amd.com> [sean: use MAXNODE_FOR_MASK(), rewrite changelog] Link: https://patch.msgid.link/20260903001625.2792367-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Account for kernel's off-by-one bug in NUMA node syscallsSean Christopherson
Add and use MAXNODE_FOR_MASK() to compute the "correct" maxnode value that is passed to various NUMA-related syscalls, e.g. get_mempolicy(), mbind(), migrate_pages(), etc. In quotes, because the kernel has an undocumented, longstanding off-by-one bug that requires userspace to specify the number of bits plus one, i.e. the max node plus two. The kernel bug has been known since 2007[*]: : And this is I think the reason why we can't change this now. I assume : numactl allocates 1024 bits (0 to 1023) and passes 1025 to make sure all : 1024 bits are processed. If we change it now, kernel will process 1025 : bits (0 to 1024) and overflow the allocated bitmask. If it happens to be : at the border of mmaped vma, it's a segfault... But unfortunately the manpages haven't yet been updated, e.g. : The maxnode argument is the maximum node number in the bit mask plus one Link: https://lore.kernel.org/all/63ccc890-fd57-118b-5997-e0259f507d28@suse.cz[*] Reviewed-by: Shivank Garg <shivankg@amd.com> Link: https://patch.msgid.link/20260903001625.2792367-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Free the VM when NX hugepage disabling is deniedGokul K
run_test() returns as soon as it has confirmed that disabling NX huge pages fails with -EPERM, without freeing the VM created a few lines earlier. Jump to the kvm_vm_free() at the end of the function instead, matching the pattern used for the SEV smoke test in the previous patch. Fixes: b774da3f2e57 ("KVM: selftests: Test disabling NX hugepages on a VM") Signed-off-by: Gokul K <gokul02k@gmail.com> Link: https://patch.msgid.link/20260811050029.530698-3-gokul02k@gmail.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Free the VM when the SEV smoke test's guest completesGokul K
test_sev() returns directly out of the UCALL_DONE case rather than leaving the loop, so the plain-SEV path skips the kvm_vm_free() at the end of the function. The SEV-ES path is unaffected; it breaks out of the loop and frees the VM correctly. main() invokes test_sev() once per supported SEV VM type, so a full run leaks a VM and its file descriptors. Nothing fails today because the process exits shortly afterwards, which is presumably why this was not noticed, but the leak also means the plain-SEV path never exercises VM teardown. Use a goto so UCALL_DONE joins the existing exit path. A plain break would only leave the switch statement and spin the loop again. Fixes: be250ff437fa ("KVM: selftests: Add a basic SEV smoke test") Signed-off-by: Gokul K <gokul02k@gmail.com> Link: https://patch.msgid.link/20260811050029.530698-2-gokul02k@gmail.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Fix the bounds of the SEV debug dst buffer checkGokul K
validate_dst() is passed a starting offset and a byte count, but bounds its loop with "i < nr_bytes", comparing the offset against the count rather than against offset + nr_bytes. Only calls that pass offset 0 check the region they were asked to check. Every other call checks too few bytes, and any call whose offset is >= the count returns without checking anything at all. Of the 1990 validate_dst() calls a full run makes per VM type, 740 (37%) check zero bytes, only the 29 zero-offset calls are correct, and 62% of the intended bytes are checked overall. The unaligned offsets the sweep deliberately picks to exercise partial chunks are precisely the ones that end up checking nothing. The bug is latent today: validate_buffers() runs first and compares all of src[] against dst[], so a genuine {en,de}crypt corruption is still caught. That is presumably why this went unnoticed. Bound the loop with offset + nr_bytes, and rename the parameter to offset so it cannot be mistaken for a length again. The caller already guarantees offset + nr_bytes <= BUFFER_SIZE, so the corrected loop stays in bounds. Fixes: 6edd35e77a42 ("KVM: selftests: Add a test to verify SEV {en,de}crypt debug ioctls") Signed-off-by: Gokul K <gokul02k@gmail.com> Link: https://patch.msgid.link/20260808061934.400892-1-gokul02k@gmail.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Check calloc() return in vcpu_get_reg_list()Chaithanya Lagisetty
vcpu_get_reg_list() allocates reg_list with calloc() and immediately dereferences it via reg_list->n without checking for allocation failure, unlike every other allocation in this file which is guarded by TEST_ASSERT(). If calloc() returns NULL the test crashes with a NULL pointer dereference instead of a clean failure message. Add the missing TEST_ASSERT() check. Fixes: fd02029a9e01 ("KVM: selftests: Add aarch64 get-reg-list test") Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com> Link: https://patch.msgid.link/20260901090839.3338955-1-nagachaithanya9911@gmail.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Fix transposed calloc() arguments in the uffd helperChaithanya Lagisetty
uffd_setup_demand_paging() passes the element size as the first argument to calloc() and the element count as the second: uffd_desc->pipefds = calloc(sizeof(int), num_readers); calloc() expects the number of elements first and the size of each element second. The product, and therefore the allocation size, is the same either way, so this is not a functional change, but the arguments are misleading and GCC 14 diagnoses them with -Wcalloc-transposed-args, which is enabled by -Wextra. Swap the arguments to match the calloc() prototype. Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com> Reviewed-by: Gautam Menghani <gautam@linux.ibm.com> Link: https://patch.msgid.link/20260904052033.3648259-1-nagachaithanya9911@gmail.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Fix the never-true negative UFFD delay checkChaithanya Lagisetty
demand_paging_test parses the -d option with strtoul() and then asserts that the result is not negative: p.uffd_delay = strtoul(optarg, NULL, 0); TEST_ASSERT(p.uffd_delay >= 0, "A negative UFFD delay is not supported."); p.uffd_delay is a useconds_t, which is an unsigned type, so the comparison is always true and the assertion can never fire. GCC points this out with -Wtype-limits, which is enabled by -Wextra. As a result, "-d -1" is accepted and converted to a very large unsigned delay, causing each demand paging fault to sleep for an unexpectedly long time in usleep() instead of rejecting the argument up front. The return value of strtoul() is not validated either, so a non-numeric argument such as "-d abc" is silently treated as a zero delay. Use atoi_non_negative() instead. It rejects negative values, unparsable input, and trailing garbage. It is already used a few lines below for -v, and hexadecimal input keeps working because atoi_paranoid() also passes a base of 0 to strtol(). Fixes: 0119cb365c93 ("KVM: selftests: Add configurable demand paging delay") Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com> Tested-by: Gautam Menghani <gautam@linux.ibm.com> Reviewed-by: Gautam Menghani <gautam@linux.ibm.com> Link: https://patch.msgid.link/20260907045246.3783304-1-nagachaithanya9911@gmail.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Always assert that vmreadz() succeedsSean Christopherson
Rename vmreadz() => vmread() (and vmread() => __vmread()) and have it assert success instead of returning garbage on failure. While the eVMCS path avoids clobbering *value on failure, the VMREAD path unconditionally stores tmp to *value, without ever zeroing tmp. I.e. if VMREAD did fail, *value would be loaded with random stack data, not '0'. An audit of the code confirms that nothing is actually relying on vmreadz() to zero the result on failure, as all paths either check for a non-zero value, propagate the result into vmwrite(), or guard the vmread() with a pre-check that the field exists. Opportunistically fix the bug in what is now __vmread(), so that the code isn't consuming uninitialized stack data. Begrudgingly keep zeroing value in vmread(), as gcc-15 at least isn't smart enough to figure out that value won't be used uninitialized (though to be fair, strictly speaking it isn't actually unreachable code). Link: https://patch.msgid.link/20260904002423.3154436-18-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Always assert that vmwrite() succeedsSean Christopherson
Assert that vmwrite() succeeds instead of forcing all callers to do the same (or as is much more common, ignoring VM-Fail and likely letting the test fail in weird ways), opportunistically provide a more verbose error message on failure. All usage of vmwrite() "guarantees" success, either because the field in question is unconditionally emulated by KVM, by virtue of earlier support checks, or by falling over (later in the test) if the VMWRITE fails. Cc: Yosry Ahmed <yosry@kernel.org> Link: https://patch.msgid.link/20260904002423.3154436-17-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add __vmwrite() and use it when initializing optional fields.Sean Christopherson
Add __vmwrite() in anticipation of having vmwrite() assert success, and use the "failure allowed" version in the common VMCS setup code, which is the only code that wants to write fields that may or may not exist (although arguably the code should check if the field exists instead of blindly writing). E.g. When doing initial configuration of a VMCS, the core library doesn't want to assume secondary controls are supported, and so blindly does a VMWRITE and pivots on success/failure. All usage of vmwrite() outside of common setup already asserts success, or 100% relies on VMWRITE succeeding in order for the test to function correctly. No functional change intended. Link: https://patch.msgid.link/20260904002423.3154436-16-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Assert success in vmlaunch() and vmresume()Sean Christopherson
Assert success in the "outer" APIs for VMLAUNCH and VMRESUME to dedup a pile of code, and switch to the double-underscores versions as necessary, to make it more obvious which paths expect VM-Enter *failure*. Link: https://patch.msgid.link/20260904002423.3154436-15-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Dedup assembly code for VMLAUNCH and VMRESUMESean Christopherson
Dedup the assembly code for VMLAUNCH vs. VMRESUME, for both architectural VMCS and eVMCS, as the difference is literally only the actual VM-Enter instruction for VMLAUNCH vs. VMRESUME, and VMWRITE vs. MOV for VMCS vs. eVMCS. Opportunistically place the implementations in vmx.c so that the lengthy sequence (~580 bytes of code) aren't inlined for every single VM-Enter. 000000000041b910 <__vmlaunch>: 41b910: 80 3d 61 5e 10 00 00 cmpb $0x0,0x105e61(%rip) # 521778 <enable_evmcs> 41b917: 0f 85 13 01 00 00 jne 41ba30 <__vmlaunch+0x120> 41b91d: ba 14 6c 00 00 mov $0x6c14,%edx 41b922: b9 16 6c 00 00 mov $0x6c16,%ecx 41b927: 6a 00 push $0x0 41b929: 0f 79 d4 vmwrite %rsp,%rdx 41b92c: 48 8d 05 82 00 00 00 lea 0x82(%rip),%rax # 41b9b5 <__vmlaunch+0xa5> 41b933: 0f 79 c8 vmwrite %rax,%rcx <xchg GPRs> 41b9ae: 0f 01 c2 vmlaunch 41b9b1: 48 ff 04 24 incq (%rsp) <xchg GPRs> 41ba2d: 58 pop %rax 41ba2e: c3 ret 41ba2f: 90 nop 41ba30: 48 8b 35 39 5d 10 00 mov 0x105d39(%rip),%rsi # 521770 <current_evmcs> 41ba37: c7 86 38 03 00 00 00 00 00 00 movl $0x0,0x338(%rsi) 41ba41: 6a 00 push $0x0 41ba43: 48 89 a6 68 02 00 00 mov %rsp,0x268(%rsi) 41ba4a: 48 8d 05 83 00 00 00 lea 0x83(%rip),%rax # 41bad4 <__vmlaunch+0x1c4> 41ba51: 48 89 46 50 mov %rax,0x50(%rsi) <xchg GPRs> 41bacd: 0f 01 c2 vmlaunch 41bad0: 48 ff 04 24 incq (%rsp) <xchg GPRs> 41bb4c: 58 pop %rax 41bb4d: c3 ret No functional change intended (though the code generation will be quite different). Cc: Yosry Ahmed <yosry@kernel.org> Link: https://patch.msgid.link/20260904002423.3154436-14-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Drop dead return code from evmcs_vmptrld() and load_evmcs()Sean Christopherson
Drop the return codes from evmcs_vmptrld() and load_evmcs() as they are hardcoded to return '0' and "true" respectively. No functional change intended. Link: https://patch.msgid.link/20260904002423.3154436-13-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add macros to handle simple VMX instructionsSean Christopherson
Add macros to build helpers for VMXON, VMPTRLD, and VMCLEAR, i.e. the VMX instructions that take a VMCS address as input, and don't have outputs. Getting the params right is annoying, especially when using KVM_ASM_SAFE(). As a bonus, this adds non-asserting double-underscores versions of VMXON and VMCLEAR for "free". Link: https://patch.msgid.link/20260904002423.3154436-12-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Drop useless return code from load_vmcs()Sean Christopherson
Now that load_vmcs() either asserts or returns success, drop the return code and the many asserts in its callers. Link: https://patch.msgid.link/20260904002423.3154436-11-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Always assert that vmptrld() succeedsSean Christopherson
Assert that vmptrld() succeeds instead of forcing all callers to do the same, and opportunistically provide a macro to print (hopefully) useful information on VMX instruction failures. Leave load_vmcs() as-is for the moment to minimize the amount of churn since the vmptrld() changes aren't completely trivial. Link: https://patch.msgid.link/20260904002423.3154436-10-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Always assert that vmclear() succeedsSean Christopherson
Continue removing dead error "handling", and assert that VMCLEAR succeeds instead of forcing callers of load_vmcs() to assert success. For all intents and purposes, no functional change intended. Link: https://patch.msgid.link/20260904002423.3154436-9-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Always assert that vmxon() and prepare_for_vmx_operation() ↵Sean Christopherson
succeed Assert that vmxon() succeeds, and by extension prepare_for_vmx_operation(). Forcing every caller to assert that a core preparation helper succeeds is absurd and generates hard-to-read code. For all intents and purposes, no functional change intended. Link: https://patch.msgid.link/20260904002423.3154436-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Assert success in vmptrst(), kill off vmptrstz()Sean Christopherson
Assert success in vmptrst() instead of punting to the caller, as literally every user expects VMPTRST to succeed (and because '0' is a valid physical address). Link: https://patch.msgid.link/20260904002423.3154436-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Check VMPTRLD with active eVMCSVitaly Kuznetsov
Check that VMPTRLD when eVMCS is active results in #UD. This matches genuine Hyper-V's behavior. Use KVM_ASM_SAFE framework to handle #UD from VMPTRLD. Unfortunately, the same trick cannot be applied to the existing #UD check on VMLAUNCH as VMLAUNCH clobbers all registers which KVM_ASM_SAFE depends on. Keep VMLAUNCH handling separately. Deliberately use a double-underscores prefix instead of a "safe" postfix for the helper, to communicate that the inner/outer helpers handle more than just exceptions, they also handle VM-Fail conditions. Assisted-by: Claude:claude-sonnet-4-6 Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> [sean: use __ instead of _safe] Link: https://patch.msgid.link/20260904002423.3154436-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Adapt to the updated VMPTRST behavior when eVMCS is usedVitaly Kuznetsov
Previously, VMPTRST was forbidden with eVMCS and selftests were mocking the correct behavior in vmptrst() by returning enlightened vmptr directly. Since KVM's behavior has changed to match genuine Hyper-V, adjust evmcs test accordingly. Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Link: https://patch.msgid.link/20260904002423.3154436-5-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Don't clobber RFLAGS in happy path of __KVM_ASM_SAFE()Sean Christopherson
Use a straight MOV instead of an XOR to zero R9 in the non-faulting path of __KVM_ASM_SAFE() so as not to clobber RFLAGS. XOR generates smaller code, but optimizing performance by minimizing the code footprint is just about the last thing that KVM selftests care about. The bug is relatively benign in the current code base, e.g. it marginally degrades the coverage in the fastops test, but otherwise does no harm. But the bug will become actively problematic if/when the "safe assembly" macros are used for instructions that communicate results via RFLAGS, e.g. for VMX instructions, which signal failure via RFLAGS.CF and RFLAGS.ZF. Opportunistically use R9D instead of the full R9, as bits 63:32 of GPRs are zeroed on writes to bit 31:0. Reported-by: Sashiko Bot <sashiko-bot@kernel.org> Closes: https://lore.kernel.org/all/20260818165258.2613603-5-vkuznets@redhat.com Cc: Vitaly Kuznetsov <vkuznets@redhat.com> Link: https://patch.msgid.link/20260904002423.3154436-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add arch hook to force page tables to be naturally alignedSean Christopherson
Add an arch hook to communicate to the core physical page allocator that page table allocations need to be naturally aligned. The hook will be used for upcoming PowerPC support, which has MMU tables that are larger than the guest base page size and need to be naturally aligned. Bury the hook in the physical page allocator to avoid having to plumb a rarely used param into the main APIs. Cc: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-21-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Automatically pick min_gpa for allocations based on region typeSean Christopherson
Automatically choose the minimum GPA for physical page allocations based on the region type instead of sprinkling the logic over various wrappers and tests. All usage falls into three categories: (a) don't care, just use the bare minimum GPA, (b) page tables, use a slightly higher min to keep low memory available, (c) custom memslot, use the base of the memslot. I.e. there isn't a strong need to allow completely custom minimums. Opportunistically move the vGIC LPI stress test's "gpa_base" variable into setup_memslot() as the only reason it was a global variable was so that it could be passed as the minimum GPA for allocations from the extra memslot. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-20-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use TEST_ASSERT(), not assert(), in vm_get_mem_region()Sean Christopherson
Use a proper selftests TEST_ASSERT() in vm_get_mem_region() so that bad input is guaranteed to fail the test, and with the standard error message format. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-19-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Take the memory region type, not memslot, in page allocatorsSean Christopherson
Take the memory region type instead of the memslot in all page allocator APIs, except for the innermost quad-underscores function. This will allow automatically selecting the minimum GPA based on the allocation type, which can't be done using the memslot due to all regions sharing a single memslot by default. PowerPC support will also leverage the type to ensure that page table allocations are naturally aligned. No functional change intended. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-18-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use TEST_EXTRA region in set memory region testSean Christopherson
Use the TEST_EXTRA region/memslot in the "set memory region" test instead of a custom memslot, so that the primary vm_phy_pages_alloc() APIs can be reworked to take a region type instead of a raw memslot. For all intents and purposes, no functional change intended, as vm_override_mem_region() is barely more than a wrapper for vm_userspace_mem_region_add(). Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-17-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use TEST_EXTRA region in x86's smaller MAXPHYADDR testSean Christopherson
Use the TEST_EXTRA region/memslot for x86's smaller MAXPHYADDR test instead of a custom memslot, so that the primary vm_phy_pages_alloc() APIs can be reworked to take a region type instead of a raw memslot. For all intents and purposes, no functional change intended, as vm_add_extra_mem_region() is barely more than a wrapper for vm_userspace_mem_region_add(). Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-16-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use TEST_EXTRA region in arm64's vGIC LPI stress testSean Christopherson
Use the TEST_EXTRA region/memslot for arm64's vGIC LPI stress test instead of a custom memslot, so that the primary vm_phy_pages_alloc() APIs can be reworked to take a region type instead of a raw memslot. For all intents and purposes, no functional change intended, as vm_override_mem_region() is barely more than a wrapper for vm_userspace_mem_region_add(). Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-15-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add TEST_EXTRA memory region type for "special" memslotsSean Christopherson
And another memory region type to deal with extra, one-off memory regions, and use the new type to manage x86's SMRAM memslot, as another step towards taking the region type instead of the raw memslot in the physical page allocator APIs. Alternatively, SMRAM setup could simply use the quad-underscore API to continue passing in the memslot, but a surprising number of tests use an "extra" memslot for a variety of reasons. I.e. allocating memory from one (and exactly one) extra memslot isn't all that rare, and so should be treated as normal behavior, not as something extraordinary, as quad-underscore functions typically suggest. Opportunistically add comments to document the intended usage of the types, as the difference between DATA, TEST_DATA, and TEST_EXTRA in particular isn't exactly obvious. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-14-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add APIs to override memory region types with custom memslotsSean Christopherson
Add and use APIs to override memory region types instead of open coding the logic in tests, partly to dedup test code, but mostly to add some amount of protection against conflicting overrides. This will allow converting more tests to override memory regions without taking on too much risk of bugs in the long term, e.g. due to silently overriding a region multiple times. Deliberately "unroll" the loops in __vm_create() and the CMMA test's create_main_memslot() to avoid filling all memslots in anticipation of adding an "extra" memslot that is for test use and *only* test use, i.e. shouldn't be configured by core library code. Converting tests to overriding memory regions will allow taking the region type instead of the raw memslot in the physical page allocator APIs. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Acked-by: Claudio Imbrenda <imbrenda@linux.ibm.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-13-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Initialize vm->memslots[] with invalid memslots during creationSean Christopherson
Initialize vm->memslots[] with an invalid memslots value during very initial VM creation so that failure to configure the core memory regions generates an immediate assert instead of potential weirdness, e.g. if slot0 is coincidentally valid. This will also allow hardening the region APIs to guard overriding a region's memslot multiple times. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Acked-by: Gautam Menghani <gautam@linux.ibm.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-12-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Add a wrapper API to allocate multiple page table pagesSean Christopherson
Add vm_alloc_page_table_pages() and use it throughout arch code to replace open coded equivalents. No functional change intended. Reviewed-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Acked-by: Claudio Imbrenda <imbrenda@linux.ibm.com> Acked-by: Anup Patel <anup@brainfault.org> Acked-by: Gautam Menghani <gautam@linux.ibm.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-11-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use the innermost page allocator API in s390's IRQ routing testSean Christopherson
Use the quad-underscores page allocator in s390's IRQ routing test in anticipation of dropping the @min_gpa param from the primary APIs. Note, it's not entirely clear that the test *needs* to use a very magical @min_gpa, i.e. it's possible a more elegant solution exists. But that's a problem for a different day. No functional change intended. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Acked-by: Claudio Imbrenda <imbrenda@linux.ibm.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-10-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use the innermost page allocator API in the memslot perf testSean Christopherson
Use the quad-underscores physical page allocator API in the memslot perf test in anticipation of converting the outer versions to take the memory region type instead of the raw memslot. Stating the obvious, the memslot perf test creates and allocates from multiple memslots, and so can't use MEM_REGION_TEST_DATA to communicate which region/slot to allocate from. No functional change intended. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-9-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Make the single-page allocator APIs static inlineSean Christopherson
Move the single-page allocator APIs to kvm_util.h as inline functions, as the are trivial wrappers, and unlike __vm_phy_pages_alloc(), are expected to remain that way for the foreseeable future. No functional change intended. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Extend page allocator to support naturally aligned allocationsSean Christopherson
Extend the page allocator to support naturally aligned allocations, as some flavors of PowerPC page tables require multiple guest pages per table, and those tables need to be naturally aligned. Take care to align the potential base address *inside* the retry loop, i.e. the base needs to be (re)aligned on every attempt. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use goto instead of do-while to retry finding unused ↵Nicholas Piggin
physical pages Rework the core loop of the physical page allocator to use a goto instead of do-while loop when retrying with a new base address. The do-while loop obviously works, but it obfuscates the success vs. failure paths, and in general is convoluted. For all intents and purposes, no functional change intended. Signed-off-by: Nicholas Piggin <npiggin@gmail.com> [Rebased to latest mainline tree] Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> [sean: separate from alignment change, use enomem label, write changelog] Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Acked-by: Gautam Menghani <gautam@linux.ibm.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Rename "num" param to "nr_pages" for physical page allocatorsSean Christopherson
Rename the "num" param in the physical page allocator APIs to "nr_pages" to make the unit and role of the param more obvious. Opportunistically rework the function comment to drop the superfluous kerneldoc style, and to very explicitly call out that success is guaranteed. No functional change intended. Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-5-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use vm_alloc_page_table() to allocate LoongArch page tablesSean Christopherson
Now that LoongArch uses KVM_GUEST_PAGE_TABLE_MIN_PADDR for its page table page allocations, use the common vm_alloc_page_table() instead of open coding the same. Opportunistically drop the assert that the allocation succeeded, as the allocator itself guarantees success. No functional change intended. Reviewed-by: Bibo Mao <maobibo@loongson.cn> Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-09-11KVM: selftests: Use the common minimum GPA for page tables on LoongArchSean Christopherson
Use the common minimum GPA for page tables (0x180000) on LoongArch instead of LoongArch's custom minimum of 0x200000, so that LoongArch can use vm_alloc_page_table() instead of open coding a rough equivalent. There are no known reasons why LoongArch can't use a lower, less-aligned minimum. The more-aligned value was originally selected to support huge pages for the guest, but that only requires the address of the final allocation to be aligned, the addresses of page table pages is irrelevant on that front. Reviewed-by: Bibo Mao <maobibo@loongson.cn> Tested-by: Itaru Kitayam <itaru.kitayama@fujitsu.com> Link: https://patch.msgid.link/20260902164123.2546762-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>