From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF35042EEA8; Fri, 6 Feb 2026 18:29:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.186 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770402562; cv=none; b=GmnvASBqGzyUqhHd6qSUKpwHsTEV1D9sQhfiFKirgTjVOqDLLinyCaAJD6zXpgdou5+NOWDUIyUbH3ykmvvKDgG3+OHSSO6/VGZEegNZh6glmYIS/shcNxRfuZPFre0AayWU3TBpuwbnvztVFEtm4mXo7R8JkCEUJbgPNyCn4r0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770402562; c=relaxed/simple; bh=ymfAs3xB8hAuETyebogRx9mBd07Qw8b8lS7I2myLjSk=; h=MIME-Version:Date:Content-Type:From:Message-ID:Subject:To:Cc: In-Reply-To:References; b=OHQ7Z0ti5wpv9URddRyrSI+J7sNMTGX6VVWnTUb/DVVYkjpuleTQJ983cwfSIUFkjrDgU+b3PxJoOECZ5INZmY1jj/FyFMPmKKaRll2FNq0qIhpLQn0261agZWvq0RJG21AuNK+N38BNI1hS6r3Zzis0QUzAYK27sHnHCrf3ybU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=w61hwdrn; arc=none smtp.client-ip=95.215.58.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="w61hwdrn" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1770402550; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oEqsT/9o28ooWpXG8lklZvWxvcXOZono2CP5ZVtyGks=; b=w61hwdrnqOhRiR8XiXSao9xW8xRL17RkWx+39mLRCIIk3YV1x8EbiAkvCOqof1kzmj8VYn 08G9VMj4R2nQ/4DApBJzcBTXgBGyEy/etkcjF79+zG5WZ5ghjhBo397b3K5QG/E58awrx9 QJk9jm89AgUR715SuijijjPvaGyjsBg= Date: Fri, 06 Feb 2026 18:29:05 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: "Yosry Ahmed" Message-ID: <0468715595718af34a8a3551663cffa79dd3ce2e@linux.dev> TLS-Required: No Subject: Re: [PATCH v3 3/8] KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT To: "Sean Christopherson" , "Jim Mattson" Cc: "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 In-Reply-To: References: <20260205214326.1029278-1-jmattson@google.com> <20260205214326.1029278-4-jmattson@google.com> X-Migadu-Flow: FLOW_OUT February 6, 2026 at 10:23 AM, "Sean Christopherson" w= rote: >=20 >=20On Thu, Feb 05, 2026, Jim Mattson wrote: >=20 >=20>=20 >=20> When nested NPT is enabled in vmcb12, copy the (cached and validate= d) > > vmcb12 g_pat field to the guest PAT register. Under KVM, the guest P= AT > > register lives in the vmcb02 g_pat field. > >=20=20 >=20> When NPT is enabled, but nested NPT is disabled, copy L1's IA32_PA= T MSR to > > the vmcb02 g_pat field, since L2 shares the IA32_PAT MSR with L1. > >=20=20 >=20> When NPT is disabled, the vmcb02 g_pat field is ignored by hardwar= e. > >=20 >=20Uber nit, the "vmcb02" qualifier can be dropped, i.e. >=20 >=20 When NPT is disabled, the g_pat field is ignored by hardware. >=20 >=20Scoping it to vmcb02 makes it sound like there's a special rule about= vmcb02. >=20 >=20>=20 >=20> Fixes: 15038e147247 ("KVM: SVM: obey guest PAT") > > Signed-off-by: Jim Mattson > > --- > > arch/x86/kvm/svm/nested.c | 16 +++++++++++++--- > > 1 file changed, 13 insertions(+), 3 deletions(-) > >=20=20 >=20> diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c > > index 1d4ff6408b34..1ff2ede96094 100644 > > --- a/arch/x86/kvm/svm/nested.c > > +++ b/arch/x86/kvm/svm/nested.c > > @@ -646,9 +646,6 @@ static void nested_vmcb02_prepare_save(struct vc= pu_svm *svm, struct vmcb *vmcb12 > > struct vmcb *vmcb02 =3D svm->nested.vmcb02.ptr; > > struct kvm_vcpu *vcpu =3D &svm->vcpu; > >=20=20 >=20> - nested_vmcb02_compute_g_pat(svm); > > - vmcb_mark_dirty(vmcb02, VMCB_NPT); > > - > > /* Load the nested guest state */ > > if (svm->nested.vmcb12_gpa !=3D svm->nested.last_vmcb12_gpa) { > > new_vmcb12 =3D true; > > @@ -656,6 +653,19 @@ static void nested_vmcb02_prepare_save(struct v= cpu_svm *svm, struct vmcb *vmcb12 > > svm->nested.force_msr_bitmap_recalc =3D true; > > } > >=20=20 >=20> + if (npt_enabled) { > > + if (nested_npt_enabled(svm)) { > > + if (unlikely(new_vmcb12 || > > + vmcb_is_dirty(vmcb12, VMCB_NPT))) { > > + vmcb02->save.g_pat =3D svm->nested.gpat; > > + vmcb_mark_dirty(vmcb02, VMCB_NPT); > > + } > > + } else { > > + vmcb02->save.g_pat =3D vcpu->arch.pat; > > + vmcb_mark_dirty(vmcb02, VMCB_NPT); > > + } > > + } > >=20 >=20To reduce indentation, how about this? There's a consistency check fo= r > nested_npt_enabled() vs. npt_enabled, so it's guaranteed to do the righ= t thing. You mean the one that goes away after this patch: https://lore.kernel.org= /kvm/20260115011312.3675857-16-yosry.ahmed@linux.dev/? >=20 >=20 if (nested_npt_enabled(svm)) { > if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_NPT))) { > vmcb02->save.g_pat =3D svm->nested.gpat; > vmcb_mark_dirty(vmcb02, VMCB_NPT); > } > } else if (npt_enabled) { > vmcb02->save.g_pat =3D vcpu->arch.pat; > vmcb_mark_dirty(vmcb02, VMCB_NPT); > } >=20 >=20>=20 >=20> + > > if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) { > > vmcb02->save.es =3D vmcb12->save.es; > > vmcb02->save.cs =3D vmcb12->save.cs; > > --=20 >=20> 2.53.0.rc2.204.g2597b5adb4-goog > > >