]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT
authorJim Mattson <jmattson@google.com>
Tue, 7 Apr 2026 19:03:27 +0000 (12:03 -0700)
committerSean Christopherson <seanjc@google.com>
Thu, 14 May 2026 12:45:11 +0000 (05:45 -0700)
When KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled and nested NPT is
enabled in vmcb12, copy the (cached and validated) vmcb12 g_pat field to
vmcb02's g_pat, giving L2 its own independent guest PAT register.

When the quirk is enabled (default), or when NPT is enabled but nested NPT
is disabled, copy L1's IA32_PAT MSR to the vmcb02 g_pat field, since L2
shares the IA32_PAT MSR with L1.

When NPT is disabled, the g_pat field is ignored by hardware.

Fixes: 15038e147247 ("KVM: SVM: obey guest PAT")
Signed-off-by: Jim Mattson <jmattson@google.com>
Link: https://patch.msgid.link/20260407190343.325299-5-jmattson@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/svm/nested.c

index 98701c23bc991b6e49f318da1859463a2ad476eb..536252e2d1bcab82c6a831f23e702814a14be828 100644 (file)
@@ -731,9 +731,6 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
        struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
        struct kvm_vcpu *vcpu = &svm->vcpu;
 
-       nested_vmcb02_compute_g_pat(svm);
-       vmcb_mark_dirty(vmcb02, VMCB_NPT);
-
        /* Load the nested guest state */
        if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
                new_vmcb12 = true;
@@ -764,6 +761,13 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
                vmcb_mark_dirty(vmcb02, VMCB_CET);
        }
 
+       if (l2_has_separate_pat(vcpu)) {
+               if (unlikely(new_vmcb12 || vmcb12_is_dirty(control, VMCB_NPT)))
+                       vmcb_set_gpat(vmcb02, svm->nested.save.g_pat);
+       } else if (npt_enabled) {
+               vmcb_set_gpat(vmcb02, vcpu->arch.pat);
+       }
+
        kvm_set_rflags(vcpu, save->rflags | X86_EFLAGS_FIXED);
 
        svm_set_efer(vcpu, svm->nested.save.efer);