From nobody Mon Feb 9 14:38:35 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 74BC0337B99 for ; Thu, 5 Feb 2026 21:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770327828; cv=none; b=RYHTi0pjXMg+8jB6pYculNWa7ZBeY8ZmcGaU1BPcJnWleFFMzueDn/+hmS18D3/YtFE/Zi18onjjVRKKxBl1QBLyjSL1djfAWfAnJ3FOmkg5iTluqNxv2EZ77HhEY2E4UFU0EFzYXX4bFj/oy190R7gC54COiRWSKsqbSpTa8Tw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770327828; c=relaxed/simple; bh=MekaBo4sf17xgGY9W1J8mQajBnaq5uK/pQ/wy1TwC7A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YIS3HvibWxxGhDsp6pz1BUXON2MS+IKbuaCmkTLRx5mKYllKxVhnVaFyscDCN/YIKKcTS5z9Ul3ZzOK2hr0RHCDJIAhgPBDDDCyT168kAkMbEjZ+aoaLdMiJYHd9YbzmTu19qwCZPingQ9FRidiGHWS+gTtkjle7o+YsUvAIASY= 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=TNbVf3Vz; arc=none smtp.client-ip=209.85.214.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="TNbVf3Vz" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a8c54bbe46so32903605ad.2 for ; Thu, 05 Feb 2026 13:43:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770327828; x=1770932628; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=43/5MPyJUuLlvxAVOINMUPcXPK41vsmXFa7nB5Y5I2U=; b=TNbVf3Vzy4UmZYIeScvgt0I7YPk97440u/F9plyESJkWLVuhLygfR24Ic9sKd1VhWz 844DuQWjTowOSCB2jaVOVIv1Il5Vzbap7gcY1v3WDGPFUZby2CV37fAaMotm9baaf7wU d+XNg9vsNAwPoRmm/2MKxZDDnURFvbYmCqZahW/EK6QLMdRFT2twyg9g/S169sgsgyoZ GWAC9lIeJWhIpBK6xZqAgIO2QgA1kUT1mf7QfzNEG4e9phz4k4tLD9UtVpTpLnljVu02 +rS2/xahXdEFodUwhN2D2V5T+WGp5pRAi4CeSl0/jyLRvwKpglP+3uPIjdwrGUtVs+eN Y+3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770327828; x=1770932628; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=43/5MPyJUuLlvxAVOINMUPcXPK41vsmXFa7nB5Y5I2U=; b=nkc8XgHx7C0Vsbo9V33WwCQ+LOtml6Mt1cqdmbQfKC2ju/VdVrwyuDz5FiCSzhHrlW hB63V/Pxs+yjgH59LgQr8JMWbbYmBiqy+lmUSYaIrC49UyLqYPLD+SKJWfUmdJtJSmdC CUC3AFG52kbeEv6CBfdUDGThMM5+vnFENJ4XNprc6+eYmTywdxEzH6pkZcbMfWljbJg6 5KgTLPJbVX1nKqpQSJwyaa7+E0/KizwfW0IGEYuK7JI6T3VYd1W5aBdEewx6tb0e7OWk 44uHklo6ZBC5dNSyxdQ+ZztGaWYkzGnEXz33x1bKcffH4CZJsAmo/6shW/xuptn43SFG CmPw== X-Forwarded-Encrypted: i=1; AJvYcCVVtN4vmUC+WAHk3jS74b//oeVzOA5nJFV6j0sY7vo/b4Y83Me9ZNNu9lUbl7FE26J6nEhwl6AvlMtmlsY=@vger.kernel.org X-Gm-Message-State: AOJu0YzG6/VYV1eRSpWTvatSZtxgamhQog47FPjqXvc2mTP18YbZBVoy SkmuxpCx4g7NqOnGVTcF9duGlXz7dmAcPCdrBmA95aimtvOoO/U+Z2/t+vsTr1XXl2VpSm9Y0KJ PwlmnV57G2lLVBw== X-Received: from plpr14.prod.google.com ([2002:a17:903:3e2e:b0:29f:2b44:973b]) (user=jmattson job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:eb14:b0:2a3:bf9d:9399 with SMTP id d9443c01a7336-2a9516d4f31mr4351265ad.35.1770327827905; Thu, 05 Feb 2026 13:43:47 -0800 (PST) Date: Thu, 5 Feb 2026 13:43:07 -0800 In-Reply-To: <20260205214326.1029278-1-jmattson@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260205214326.1029278-1-jmattson@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260205214326.1029278-8-jmattson@google.com> Subject: [PATCH v3 7/8] KVM: x86: nSVM: Handle restore of legacy nested state 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When nested NPT is enabled and KVM_SET_NESTED_STATE is used to restore an old checkpoint (without a valid gPAT), the current IA32_PAT value must be used as L2's gPAT. The current IA32_PAT value may be restored by KVM_SET_MSRS after KVM_SET_NESTED_STATE. Furthermore, there may be a KVM_GET_NESTED_STATE before the first KVM_RUN. Introduce a new boolean, svm->nested.legacy_gpat_semantics. When set, hPAT updates are also applied to gPAT, preserving the old behavior where L2 shared L1's PAT. svm_vcpu_pre_run() clears this boolean at the first KVM_RUN. Signed-off-by: Jim Mattson --- arch/x86/kvm/svm/nested.c | 11 ++++++++--- arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/svm/svm.h | 9 +++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 3f512fb630db..a7d6fc1382a7 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1936,9 +1936,14 @@ static int svm_set_nested_state(struct kvm_vcpu *vcp= u, if (ret) goto out_free; =20 - if (nested_npt_enabled(svm) && - (kvm_state->hdr.svm.flags & KVM_STATE_SVM_VALID_GPAT)) - svm_set_gpat(svm, kvm_state->hdr.svm.gpat); + if (nested_npt_enabled(svm)) { + if (kvm_state->hdr.svm.flags & KVM_STATE_SVM_VALID_GPAT) { + svm_set_gpat(svm, kvm_state->hdr.svm.gpat); + } else { + svm_set_gpat(svm, vcpu->arch.pat); + svm->nested.legacy_gpat_semantics =3D true; + } + } =20 svm->nested.force_msr_bitmap_recalc =3D true; =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b62c32c3942d..a6a44deec82b 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4239,6 +4239,8 @@ static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu) if (to_kvm_sev_info(vcpu->kvm)->need_init) return -EINVAL; =20 + to_svm(vcpu)->nested.legacy_gpat_semantics =3D false; + return 1; } =20 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index a0e94a2c51a1..a559cd45c8a9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -221,6 +221,13 @@ struct svm_nested_state { * on its side. */ bool force_msr_bitmap_recalc; + + /* + * Indicates that a legacy nested state was restored (without a + * valid gPAT). In this mode, updates to hPAT are also applied to + * gPAT, preserving the old behavior where L2 shared L1's PAT. + */ + bool legacy_gpat_semantics; }; =20 struct vcpu_sev_es_state { @@ -604,6 +611,8 @@ static inline void svm_set_hpat(struct vcpu_svm *svm, u= 64 data) if (is_guest_mode(&svm->vcpu) && !nested_npt_enabled(svm)) svm_set_vmcb_gpat(svm->nested.vmcb02.ptr, data); } + if (svm->nested.legacy_gpat_semantics) + svm_set_gpat(svm, data); } =20 static inline bool nested_vnmi_enabled(struct vcpu_svm *svm) --=20 2.53.0.rc2.204.g2597b5adb4-goog