From: Jim Mattson <jmattson@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Shuah Khan <shuah@kernel.org>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
Cc: Jim Mattson <jmattson@google.com>
Subject: [PATCH v2 0/8] KVM: x86: nSVM: Improve PAT virtualization
Date: Thu, 15 Jan 2026 15:21:39 -0800 [thread overview]
Message-ID: <20260115232154.3021475-1-jmattson@google.com> (raw)
Currently, KVM's implementation of nested SVM treats the PAT MSR the same
way whether or not nested NPT is enabled: L1 and L2 share a single
PAT. However, the APM specifies that when nested NPT is enabled, the host
(L1) and the guest (L2) should have independent PATs: hPAT for L1 and gPAT
for L2. This patch series implements the architectural specification in
KVM.
The existing PAT MSR (vcpu->arch.pat) is used for hPAT, and the
vmcb02.save.g_pat field is used for gPAT. With nested NPT enabled, guest
accesses to the IA32_PAT MSR are redirected to gPAT, which is stored in
vmcb02->save.g_pat. All other accesses, including userspace accesses via
KVM_{GET,SET}_MSRS, continue to reference hPAT.
The special handling of userspace accesses ensures save/restore forward
compatibility (i.e. resuming a new checkpoint on an older kernel). When an
old kernel restores a checkpoint from a new kernel, the gPAT will be lost,
and L2 will simply use L1's PAT, which is the behavior of the old kernel
anyway.
v1 -> v2:
Adhere to the architectural specification
Drop the preservation of vmcb01->g_pat across virtual SMM
Store the gPAT rather than the hPAT in the nested state (save.g_pat)
Fix forward compatibility
Handle backward compatibility when MSRs are restored after nested state
(setq-default fill-column 75) [Sean]
Or the KVM_STATE_SVM_VALID_GPAT bit into flags [Sean]
Jim Mattson (8):
KVM: x86: nSVM: Redirect IA32_PAT accesses to either hPAT or gPAT
KVM: x86: nSVM: Cache g_pat in vmcb_save_area_cached
KVM: x86: nSVM: Add validity check for vmcb12 g_pat
KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT
KVM: x86: nSVM: Save gPAT to vmcb12.g_pat on VMEXIT
KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE
KVM: x86: nSVM: Handle restore of legacy nested state
KVM: selftests: nSVM: Add svm_nested_pat test
arch/x86/include/uapi/asm/kvm.h | 3 +
arch/x86/kvm/svm/nested.c | 49 ++-
arch/x86/kvm/svm/svm.c | 39 +-
arch/x86/kvm/svm/svm.h | 7 +
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/x86/svm_nested_pat_test.c | 357 ++++++++++++++++++
6 files changed, 442 insertions(+), 14 deletions(-)
create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
base-commit: f62b64b970570c92fe22503b0cdc65be7ce7fc7c
--
2.52.0.457.g6b5491de43-goog
next reply other threads:[~2026-01-15 23:22 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-15 23:21 Jim Mattson [this message]
2026-01-15 23:21 ` [PATCH v2 1/8] KVM: x86: nSVM: Redirect IA32_PAT accesses to either hPAT or gPAT Jim Mattson
2026-01-16 4:08 ` Jim Mattson
2026-01-22 1:20 ` Yosry Ahmed
2026-02-02 20:02 ` Jim Mattson
2026-01-15 23:21 ` [PATCH v2 2/8] KVM: x86: nSVM: Cache g_pat in vmcb_save_area_cached Jim Mattson
2026-01-22 1:28 ` Yosry Ahmed
2026-02-02 20:35 ` Jim Mattson
2026-01-15 23:21 ` [PATCH v2 3/8] KVM: x86: nSVM: Add validity check for vmcb12 g_pat Jim Mattson
2026-01-22 1:40 ` Yosry Ahmed
2026-02-02 20:40 ` Jim Mattson
2026-01-15 23:21 ` [PATCH v2 4/8] KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT Jim Mattson
2026-01-22 1:54 ` Yosry Ahmed
2026-01-15 23:21 ` [PATCH v2 5/8] KVM: x86: nSVM: Save gPAT to vmcb12.g_pat on VMEXIT Jim Mattson
2026-01-15 23:21 ` [PATCH v2 6/8] KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE Jim Mattson
2026-01-16 4:23 ` Jim Mattson
2026-01-22 1:51 ` Yosry Ahmed
2026-01-22 1:52 ` Yosry Ahmed
2026-01-15 23:21 ` [PATCH v2 7/8] KVM: x86: nSVM: Handle restore of legacy nested state Jim Mattson
2026-01-20 18:27 ` Jim Mattson
2026-01-15 23:21 ` [PATCH v2 8/8] KVM: selftests: nSVM: Add svm_nested_pat test Jim Mattson
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=20260115232154.3021475-1-jmattson@google.com \
--to=jmattson@google.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=tglx@linutronix.de \
--cc=x86@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox