From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 7C32B338596 for ; Thu, 5 Feb 2026 21:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770327828; cv=none; b=XG+Yng3DH04doGqebqcP7V7mOywaymyqIF5glj7CNXDLFSOtQMGRpfUmmdMYwAjeaWmuhOECB7Cum0BF8Cj0hQA7r6QHLByc9643W1cY6/OS52+5tSrcvjIsCJ6yhlseDd8XdsBTxtFW5FTRul4OeJ41jPNM4AQghVULHxhY6ZA= 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.202 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-f202.google.com with SMTP id d9443c01a7336-2a8c54bbe46so32903615ad.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=OGfqTxIxx2cOzTfZ6WkBTDvRyKfBhBVHb5+uaSJ8g4gf4mgQd5Vhx477GaAO9bgW5N H48fhrLCZQ/jCzX/rfV6yIUl3F8D5nZkJWv8f4vS0aUfwPT1v87AzGc3Up4UF4H85jxo OI/fxC32m6v5/jx5lwapFyFXEoInlk5zi4781prs9C5B3Fq6bdbhX/LBHgsJd7j0TINn oOZ5zz2pa4pc+c6Z4V7Ip3S5smAtm+760PJV7VcjlC5hnkGnL8vE0nTY+cJ3zrSmmm8G 7PxiYSxXgMAW4WLhbhRQ3YRxwjd8Qpa0QZZ2vN32/2GoP3deWy5RDsRoykrbCnRZ8+qk ZEzQ== X-Forwarded-Encrypted: i=1; AJvYcCXFRVBuxUh4nYsQ4zPcJUcsNvqlJXSLclb+57vsl8IdERZoiKG2aLXOBFwDdyqDMU2E+8uX6o9IguTG9/+tgIM=@vger.kernel.org X-Gm-Message-State: AOJu0YyXmkuQz8cQ3vkBFeg9FVoL58mTpyB/+AWEvQxZCtMJbhWxO/DS OdVxv0GtlVWPJvjs78+dO1GSfgIbjLiSUPNLM/bEP2X6W0hsirsg/f+ph7ys+UcHlbe5zsDWKwL WlWk2vBV1M4xB3g== 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-kselftest@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-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 *vcpu, if (ret) goto out_free; - 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 = true; + } + } svm->nested.force_msr_bitmap_recalc = true; 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; + to_svm(vcpu)->nested.legacy_gpat_semantics = false; + return 1; } 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; }; struct vcpu_sev_es_state { @@ -604,6 +611,8 @@ static inline void svm_set_hpat(struct vcpu_svm *svm, u64 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); } static inline bool nested_vnmi_enabled(struct vcpu_svm *svm) -- 2.53.0.rc2.204.g2597b5adb4-goog