mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Jim Mattson <jmattson@google.com>,
	 kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] KVM: selftests: Add a test for gPAT handling in L2
Date: Thu, 21 May 2026 04:14:46 +0000	[thread overview]
Message-ID: <ag6F3NcxI8xCJnus@google.com> (raw)
In-Reply-To: <20260521023448.3826878-1-yosry@kernel.org>

On Thu, May 21, 2026 at 02:34:48AM +0000, Yosry Ahmed wrote:
[..]
> +
> +static void l2_guest_code(void)
> +{
> +	u64 expected_initial_pat = npt_enabled ? L2_VMCB12_PAT : L1_PAT_VALUE;
> +	int i;
> +
> +	for (i = 0; i < nr_iterations; i++) {
> +		GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_initial_pat);
> +		GUEST_SYNC(1);
> +		GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_initial_pat);
> +
> +		wrmsr(MSR_IA32_CR_PAT, L2_PAT_MODIFIED);

expected_initial_pat (or more maybe just expected_pat now) should be
updated here.

> +
> +		GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
> +		GUEST_SYNC(2);
> +		GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
> +
> +		vmmcall();
> +	}
> +}
> +
> +static void l1_guest_code(struct svm_test_data *svm)
> +{
> +	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
> +	struct vmcb *vmcb = svm->vmcb;
> +	int i;
> +
> +	wrmsr(MSR_IA32_CR_PAT, L1_PAT_VALUE);
> +	GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L1_PAT_VALUE);
> +
> +	generic_svm_setup(svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
> +
> +	vmcb->save.g_pat = L2_VMCB12_PAT;
> +	vmcb->control.intercept &= ~(1ULL << INTERCEPT_MSR_PROT);
> +
> +	for (i = 0; i < nr_iterations; i++) {
> +		run_guest(vmcb, svm->vmcb_gpa);
> +
> +		GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_VMMCALL);
> +
> +		/*
> +		 * If NPT is enabled by L1, L2 has a unique PAT and L1's PAT is
> +		 * unchanged. Otherwise, PAT is shared between L1 and L2.
> +		 */
> +		if (npt_enabled) {
> +			GUEST_ASSERT_EQ(vmcb->save.g_pat, L2_PAT_MODIFIED);
> +			GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L1_PAT_VALUE);
> +		} else {
> +			GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
> +		}

..and we should skip over VMMCALL here.

> +	}
> +
> +	GUEST_DONE();
> +}

[..]

> +int main(int argc, char *argv[])
> +{
> +	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
> +	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_NPT));
> +	TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
> +	TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
> +		     KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);
> +
> +	NESTED_PAT_TEST("Invalid gPAT", l1_guest_code_invalid_gpat,
> +			NPT_ENABLED, NO_SAVE_RESTORE, 1);
> +
> +	NESTED_PAT_TEST("Nested NPT enabled", l1_guest_code,
> +			NPT_DISABLED, NO_SAVE_RESTORE, 1);
> +
> +	NESTED_PAT_TEST("Nested NPT enabled", l1_guest_code,
> +			NPT_ENABLED, NO_SAVE_RESTORE, 1);
> +
> +	NESTED_PAT_TEST("Save/Restore", l1_guest_code,
> +			NPT_ENABLED, DO_SAVE_RESTORE, 1);
> +
> +	NESTED_PAT_TEST("Multile VM-Entries", l1_guest_code,
> +			NPT_ENABLED, NO_SAVE_RESTORE, 1);

..and I didn't notice any of the above because I am passing in
nr_iterations=1 here lol.

This diff fixes it, let me know if you want a v3:

diff --git a/tools/testing/selftests/kvm/x86/svm_nested_pat_test.c b/tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
index b9301b3e3f4bd..d9d78db847d61 100644
--- a/tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
+++ b/tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
@@ -38,15 +38,16 @@ int nr_iterations;

 static void l2_guest_code(void)
 {
-       u64 expected_initial_pat = npt_enabled ? L2_VMCB12_PAT : L1_PAT_VALUE;
+       u64 expected_pat = npt_enabled ? L2_VMCB12_PAT : L1_PAT_VALUE;
        int i;

        for (i = 0; i < nr_iterations; i++) {
-               GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_initial_pat);
+               GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_pat);
                GUEST_SYNC(1);
-               GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_initial_pat);
+               GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_pat);

                wrmsr(MSR_IA32_CR_PAT, L2_PAT_MODIFIED);
+               expected_pat = L2_PAT_MODIFIED;

                GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
                GUEST_SYNC(2);
@@ -85,6 +86,7 @@ static void l1_guest_code(struct svm_test_data *svm)
                } else {
                        GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
                }
+               vmcb->save.rip += 3; /* VMMCALL */
        }

        GUEST_DONE();

  reply	other threads:[~2026-05-21  4:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-21  2:34 Yosry Ahmed
2026-05-21  4:14 ` Yosry Ahmed [this message]
2026-05-21  4:18   ` Yosry Ahmed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ag6F3NcxI8xCJnus@google.com \
    --to=yosry@kernel.org \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®