All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: Jim Mattson <jmattson@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] KVM: selftests: Add a test for gPAT handling in L2
Date: Wed, 3 Jun 2026 11:33:10 -0700	[thread overview]
Message-ID: <aiBzZuSk-9d7YnzT@google.com> (raw)
In-Reply-To: <CAO9r8zMrXbGevTgNttshFf88RDKrsaa_r5WryczFK8OMVcdHDA@mail.gmail.com>

On Fri, May 29, 2026, Yosry Ahmed wrote:
> On Fri, May 29, 2026 at 3:56 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Fri, May 29, 2026, Jim Mattson wrote:
> > > On Fri, May 29, 2026 at 6:29 AM Sean Christopherson <seanjc@google.com> wrote:
> > > >
> > > > On Thu, May 28, 2026, Yosry Ahmed wrote:
> > > > > > +int main(int argc, char *argv[])
> > > > > > +{
> > > > > > +       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
> > > > > > +       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);
> > > > > > +
> > > > > > +       if (!kvm_cpu_has(X86_FEATURE_NPT))
> > > > > > +               goto skip_npt;
> > > > > > +
> > > > > > +       gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
> > > > > > +       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
> > > > > > +skip_npt:
> > > > > > +       gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
> > > > > > +       return 0;
> > > > > > +}
> > > > >
> > > > > The goto is really unnecessary here:
> > > > >
> > > > > if (kvm_cpu_has(X86_FEATURE_NPT)) {
> > > > >        gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat,
> > > > > npt_enabled = true);
> > > > >        gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
> > > > > }
> > > > > gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
> > > >
> > > > Well, it's necessary to avoid the indentation.
> > >
> > > Or refactor as:
> > >
> > > if (kvm_cpu_has(X86_FEATURE_NPT))
> > >         run_npt_tests();
> >
> > Anyone have a strong preference?  I like the goto approach (obviously), so that
> > that all the gpat_test() invocations are prettily aligned.  But I can live with
> > an if-statement as Yosry suggested.  I think the only option I don't like is
> > moving the NPT tests to a separate helper (it's two lines of code, and testing
> > the NPT case really is the focal point of the test).
> 
> No strong preference, I obviously also like my suggestion :)

Ok, third option, which I've pushed to kvm-x86/svm (but not kvm-x86/next, yet)
in anticipation of overwhelming support:

#define gpat_test(test_name, guest_code, npt_setting)			\
do {									\
	npt_setting;							\
									\
	if (npt_enabled && !kvm_cpu_has(X86_FEATURE_NPT)) {		\
		pr_info("Skipping: " test_name " (no NPT support)\n");	\
		break;							\
	}								\
									\
	pr_info("Testing: " test_name "\n");				\
	run_test(guest_code, false, 1);					\
									\
	if (guest_code == l1_guest_code) {				\
		pr_info("Testing: " test_name " Save/Restore\n");	\
		run_test(guest_code, true, 1);				\
									\
		pr_info("Testing: " test_name " Multiple VMRUNs\n");	\
		run_test(guest_code, false, 10);			\
	}								\
} while (0)

int main(int argc, char *argv[])
{
	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
	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);

	gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
	gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
	gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
	return 0;
}

Which (obviously) allows printing out which subtests are being skipped:

# ./x86/svm_nested_pat_test 
Random seed: 0x6b8b4567
Skipping: Invalid gPAT (no NPT support)
Skipping: Nested NPT enabled (no NPT support)
Testing: Nested NPT disabled
Testing: Nested NPT disabled Save/Restore
Testing: Nested NPT disabled Multiple VMRUNs


  reply	other threads:[~2026-06-03 18:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-28 23:10 [PATCH v4] KVM: selftests: Add a test for gPAT handling in L2 Sean Christopherson
2026-05-29  0:47 ` Yosry Ahmed
2026-05-29 13:27   ` Sean Christopherson
2026-05-29 14:10     ` Jim Mattson
2026-05-29 22:55       ` Sean Christopherson
2026-05-29 23:01         ` Yosry Ahmed
2026-06-03 18:33           ` Sean Christopherson [this message]
2026-06-03 18:39             ` Yosry Ahmed
2026-06-05 18:31 ` Sean Christopherson

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=aiBzZuSk-9d7YnzT@google.com \
    --to=seanjc@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=yosry@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.