From nobody Mon Feb 9 23:42:59 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 F1BDF3376AC for ; Thu, 5 Feb 2026 21:43:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770327827; cv=none; b=P1Reuoi7FPgRQPVhzvQnTPUcp8y9/zRKTkSz0GXQy9d9G6/4SIUsngBKfvUbtas9L5kNnpMQcbsbSK64pNkf0o5MUbUFSS7p5HBN2C6NesKreZklvHDT6cvqs/IVABc9VX2cmy9yLLGPo1h4DZ/jogRLrN9HrpJEOPyhNrDrUMw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770327827; c=relaxed/simple; bh=REMk7MsNEK46ltVnyaD2qUn2Wq3i1H3LHfd2I6/ePAo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mTRnE97wWilao3kREI4OhqSSpiIHuLJ0ftjHWMiwAn8Qj0sh8tiG+v/PqSG7Srg7MgFp2WKvMIUw51QK7VPnFvK8+LgyTVAgSBDJhCIpOC39uuLUBjF+bOawgXbVJxN09MDCQKr5Rkts55ZZwRler1YwaHJuo77/ZvYxnLYzCxU= 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=VrkP8LCG; arc=none smtp.client-ip=209.85.215.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="VrkP8LCG" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c636238ec57so903050a12.1 for ; Thu, 05 Feb 2026 13:43:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770327826; x=1770932626; 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=S4KbIplwSpOgL8y77jJIQF/6UFPYYOp7aggt19vghWY=; b=VrkP8LCG4GKYYzkmPlZ9/1Y+U6sAc/Alv8yMlTxscyNXD8QqKNjjJ0NXbeIRWscEKF nF19crYtRaMYwTMJsgQt44O7WuE4m6FVDiHXyeeJ38KfHhYOfm5LesKIOJn9fg9ECYpa MioRdoBZ+Qv2kUovJLYnjUYEj/zD+0Ec796gJ4079L0is2x08tPCi8kbjFnWaWDpSIuQ XbIrc0Hyvwy+eOVW+0FIJnliupj+5Qs71V4/sKh3fiXNrI4RlRU7EbJBUGdbQ4NM/4SX x4TOL/aPYsvGZ8wIFVAB4pkzQYWZ2P3IiFGdg6NTP/u0vpwUgpz0oCCfePAG1dhZZBTU W8EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770327826; x=1770932626; 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=S4KbIplwSpOgL8y77jJIQF/6UFPYYOp7aggt19vghWY=; b=rwKte8r7WzK5c5dRL8S7tPesOIAv3LEZvqr6zmou/9zuxJqW+K0cPjD8Qnb9o+FUJd l0rSZCGBmu45zJJHRiOoP/arMWRbd0QWatulxlaXSx2CR2WoqI9DFvOGvYQ2JfwY0sVK rJfTZOPWufWF8S1wn1biw1OW6vakJ33fGaE8+QxPiXCNHsHQkibs8NGNDU0SFg6XuQIm XlkrkiYduww0LLAgDaDZiCzqZNK5Qb39WnH2f4swKPhv2dxn7ZKuG5XPqqp6Tr1fY8I7 NShNWgSMnGg4StLmr5/Snyb7LgjmfyD+CTWYBoteoKl2v8+CQ0h9eoI4TGC49fGBlnoZ EhWQ== X-Forwarded-Encrypted: i=1; AJvYcCU0rOZloRClHWihiBYO1y0QQLfgwTWpZZJImxFLSMujWrLw8iuhfKiokNceDD5SVZUpPu9tJPDWIvprk+E=@vger.kernel.org X-Gm-Message-State: AOJu0YyuAoCzF2ECWYtlmyiRjQP514+iVSDwL/EvuDVqy0eLhIFKZ+F3 NqJ5WFjkn2xzmXuSIQU4tQEQqED2pAhEMwNUYfo7ym0o2GQ8WzXPc6Q5U8HVPcTN5GWSc/+5bb9 /3ScbQYrjYvWR4A== X-Received: from pjup21.prod.google.com ([2002:a17:90a:d315:b0:354:a065:ec58]) (user=jmattson job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:69b:b0:38d:f084:e351 with SMTP id adf61e73a8af0-393af2a9b96mr335204637.80.1770327826507; Thu, 05 Feb 2026 13:43:46 -0800 (PST) Date: Thu, 5 Feb 2026 13:43:06 -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-7-jmattson@google.com> Subject: [PATCH v3 6/8] KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_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" Add a 'flags' field to the SVM nested state header, and use bit 0 of the flags to indicate that gPAT is stored in the nested state. If in guest mode with NPT enabled, store the current vmcb->save.g_pat value into the header of the nested state, and set the flag. Note that struct kvm_svm_nested_state_hdr is included in a union padded to 120 bytes, so there is room to add the flags field and the gpt field without changing any offsets. Fixes: cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET= _NESTED_STATE") Signed-off-by: Jim Mattson --- arch/x86/include/uapi/asm/kvm.h | 5 +++++ arch/x86/kvm/svm/nested.c | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index 846a63215ce1..664d04d1db3f 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -495,6 +495,8 @@ struct kvm_sync_regs { =20 #define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001 =20 +#define KVM_STATE_SVM_VALID_GPAT 0x00000001 + /* vendor-independent attributes for system fd (group 0) */ #define KVM_X86_GRP_SYSTEM 0 # define KVM_X86_XCOMP_GUEST_SUPP 0 @@ -531,6 +533,9 @@ struct kvm_svm_nested_state_data { =20 struct kvm_svm_nested_state_hdr { __u64 vmcb_pa; + __u32 flags; + __u32 reserved; + __u64 gpat; }; =20 /* for KVM_CAP_NESTED_STATE */ diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 0b95ae1e864b..3f512fb630db 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1764,6 +1764,10 @@ static int svm_get_nested_state(struct kvm_vcpu *vcp= u, /* First fill in the header and copy it out. */ if (is_guest_mode(vcpu)) { kvm_state.hdr.svm.vmcb_pa =3D svm->nested.vmcb12_gpa; + if (nested_npt_enabled(svm)) { + kvm_state.hdr.svm.flags |=3D KVM_STATE_SVM_VALID_GPAT; + kvm_state.hdr.svm.gpat =3D svm->nested.gpat; + } kvm_state.size +=3D KVM_STATE_NESTED_SVM_VMCB_SIZE; kvm_state.flags |=3D KVM_STATE_NESTED_GUEST_MODE; =20 @@ -1889,6 +1893,12 @@ static int svm_set_nested_state(struct kvm_vcpu *vcp= u, !__nested_vmcb_check_save(vcpu, &save_cached)) goto out_free; =20 + /* + * Validate gPAT, if provided. + */ + if ((kvm_state->hdr.svm.flags & KVM_STATE_SVM_VALID_GPAT) && + !kvm_pat_valid(kvm_state->hdr.svm.gpat)) + goto out_free; =20 /* * All checks done, we can enter guest mode. Userspace provides @@ -1926,6 +1936,10 @@ 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); + svm->nested.force_msr_bitmap_recalc =3D true; =20 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); --=20 2.53.0.rc2.204.g2597b5adb4-goog