From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86CA332BF23 for ; Thu, 5 Feb 2026 21:43:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770327818; cv=none; b=n7nXqnhtszhJY73sxDN6u54acU8hPfL/ps374AMxbWTbH6ffcF9+JkH0Nu1zD44pFQnDHiSj+3LaUKVEXKlEXtxhluBm/pgPYPV5xQXBb2EuH595fsr4u95LdLKTLmRUpRbcKeXFBer/n0p0rF3fmOiWLZG78R56ef+CAVZhwxs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770327818; c=relaxed/simple; bh=tCblJoq9g/HMbi+utPa6tWkGtYvQHIGZ3smM0wbHPKQ=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=TbZLTLbkP7pGeEzLdTGpADuDrUl9Qrm7H9gUWmH8LWLY3FpPOqUqaCvWfGh/U1yVOQBo3a/9dzHz/GuhXX6w4qVj88v+6hcS58y9+3mcS/cjpruti45j8BjCHgOQZUnyIWW34xB+B5tUv37/2ag2FdOyfjy4AXDFBvCjWhT7mcs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jmattson.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QiQpQ0Su; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jmattson.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QiQpQ0Su" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c54e81eeab9so870566a12.3 for ; Thu, 05 Feb 2026 13:43:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770327818; x=1770932618; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=NTt3Yt6WZ7YQmoJsNOI79U3marZK5bHkKaL7b6P4x+M=; b=QiQpQ0Su/+s8beQk6Qehefqcr245UbugC3o6dfQsP3oW072RdO07kI3f8gvezNN2S5 SxjzRsl9cvWL4vkUD7iz8gYn7Dm/ILqMzJKDr8vFGtbUen+qsd74Z3rwFKIJboFkBkve 0aM6h9caeoETQdOnMCSL/zyfrj5INbsWTGweeFshsUnDqcmUOwDR4RadMKUKqEhpxRi2 BhPVHYluKuxbFt/wn6wo/OWtBJ3UW+jbLem0jhE/uSHWF9D6YUD1gdi78P3fNSZ1dXk5 PlqN3DFcHuWZtLn5B+zgR56H6oBevqi2bANXOWPEAe5I7CoPsTrEGFj1ZR7OqyfMhZ9r r0Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770327818; x=1770932618; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=NTt3Yt6WZ7YQmoJsNOI79U3marZK5bHkKaL7b6P4x+M=; b=gAtr1cNf3fRK93nCIZKtp25PShKjT5vpGARvIt2zQ5bosAJqYx8HvMkU89DJzx0yQf /jymfekKlGJeGvFGUpftEOqbLrJ9S6x7RjfpbtufJjLY9R8YAjNBJb8Tl3XVYRz4ISsu nh0+U6ozyuak6d5cmzSaBeceIxBU5gupi5pKBXxW6NiTm5378keM4vUY147fhjuvH/2A FUEEJ3eHEcTr28csz8D2vO1TiFtN1bThSLtjUvPTkdWHTlH7pOSGkPeKyaccm07VatHZ 3ZqMyQPyoQb7STi/lDgj/6vyZbGUBSmX8d73yMUxpyncHWeRAuHrLFipjrYt50E551bG 6O/w== X-Forwarded-Encrypted: i=1; AJvYcCV0o+DFY2jhUb8k2OEdizay7IlEjjwetqvt/reZ7WwzEdNXog+aNOmWmouQAUAbu3cl7Feda38KZbj2PqY=@vger.kernel.org X-Gm-Message-State: AOJu0Yw2Q3z2XyKyxgivaoyKiPXK65KYUDsIi9WIYaAir5xAxpsbMP8y 4rNpWpdTXfHdkbEQGCwGdCM//6Fde1Nq6MyDGz+F5nGkRTp9XVOxCXirUFJRT/WDjHGy2Rm51K0 TexvDf6X1HkBdIg== X-Received: from pge15.prod.google.com ([2002:a05:6a02:2d0f:b0:c61:7e5d:a81]) (user=jmattson job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:151b:b0:342:44f3:d1bc with SMTP id adf61e73a8af0-393aef76be4mr328688637.35.1770327817947; Thu, 05 Feb 2026 13:43:37 -0800 (PST) Date: Thu, 5 Feb 2026 13:43:00 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260205214326.1029278-1-jmattson@google.com> Subject: [PATCH v3 0/8] KVM: x86: nSVM: Improve PAT virtualization From: Jim Mattson To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Shuah Khan , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Yosry Ahmed Cc: Jim Mattson Content-Type: text/plain; charset="UTF-8" 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. Use the existing PAT MSR (vcpu->arch.pat) for hPAT. Add a new field, svm->nested.gpat, for gPAT. With nested NPT enabled, redirect guest accesses to the IA32_PAT MSR to gPAT. 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 existing behavior of the old kernel anyway. v1: https://lore.kernel.org/kvm/20260113003016.3511895-1-jmattson@google.com/ v2: https://lore.kernel.org/kvm/20260115232154.3021475-1-jmattson@google.com/ v2 -> v3: * Extract VMCB_NPT clean bit fix as a separate patch [Yosry] * Squash v2 patches 2 and 3 (cache and validate g_pat) [Yosry] * Drop redundant npt_enabled check in g_pat validation since existing nested_vmcb_check_controls() already rejects NP_ENABLE when !npt_enabled [Yosry] * Fix svm_set_hpat() to propagate to vmcb02 only when !nested_npt_enabled, not unconditionally when in guest mode [Jim] * Warn in svm_{get,set}_msr() if host_initiated and vcpu_wants_to_run when accessing IA32_PAT [Sean] * Use dedicated svm->nested.gpat field instead of vmcb_save_area_cached * Use dedicated header field (kvm_svm_nested_state_hdr.gpat) for nested state save/restore instead of overwriting vmcb01 save area * Replace restore_gpat_from_pat with legacy_gpat_semantics to correctly handle KVM_GET_NESTED_STATE before the first KVM_RUN [Jim] * Remove nested_vmcb02_compute_g_pat() after removing all callers [Yosry] Jim Mattson (8): KVM: x86: nSVM: Clear VMCB_NPT clean bit when updating g_pat in L2 KVM: x86: nSVM: Cache and validate vmcb12 g_pat KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT KVM: x86: nSVM: Redirect IA32_PAT accesses to either hPAT or gPAT 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 | 5 + arch/x86/kvm/svm/nested.c | 51 ++- arch/x86/kvm/svm/svm.c | 37 ++- arch/x86/kvm/svm/svm.h | 35 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/svm_nested_pat_test.c | 298 ++++++++++++++++++ 6 files changed, 406 insertions(+), 21 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_pat_test.c base-commit: e944fe2c09f405a2e2d147145c9b470084bc4c9a -- 2.53.0.rc2.204.g2597b5adb4-goog