Re: [PATCH v7 15/17] KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack

From: Sean Christopherson

Date: Wed May 27 2026 - 23:03:03 EST


On Wed, May 27, 2026, Yosry Ahmed wrote:
> Instead of relying on the L1-provided stack for L2, which is usually an
> array on L1's own stack, allocate a dedicated page of VM memory for the
> L2 stack in vcpu_alloc_{vmx/svm}() and use that as L2's RSP in the
> VMCS/VMCB instead of the L1-provided value.
>
> Most L1 guest code does not do anything with the L2 stack other than
> stuff it in RSP, so this change is transparent and the L1-provided stack
> is silently ignored. The only exception is memstress nested L1 code
> which puts the vCPU index on L2's stack, so update this code to use the
> newly allocated stack.
>
> L1-provided stacks will be dropped and cleaned up separately.
>
> Suggested-by: Sean Christopherson <seanjc@xxxxxxxxxx>
> Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
> ---

Blech. This exposed a nasty edge in selftests. For tests that enable TDP, the
slots need to be identity mapped *after* allocating SVM/VMX. Found out the hard
way: the gPAT test starting failing.

Not worth worrying about right now, just one more wart in selftests that needs
to be cleaned up.

diff --git tools/testing/selftests/kvm/x86/svm_nested_pat_test.c tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
index 392ede74589e..6798b1b9d910 100644
--- tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
+++ tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
@@ -115,12 +115,14 @@ static void run_test(void *guest_code, bool do_save_restore, int nr_iters)
vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2,
KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

- if (npt_enabled) {
+ if (npt_enabled)
vm_enable_npt(vm);
- tdp_identity_map_default_memslots(vm);
- }

vcpu_alloc_svm(vm, &svm_gva);
+
+ if (npt_enabled)
+ tdp_identity_map_default_memslots(vm);
+
vcpu_args_set(vcpu, 1, svm_gva);

nr_iterations = nr_iters;