From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 F011E2FF14D; Tue, 24 Feb 2026 22:34:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972462; cv=none; b=UDkGcDi/Cs5JVmnTK3ePCwWjhi6g9+0nPTwJ5CFQGc2y6AgDafqmqAS746PnLXF33dPzZcbK3qV1v89i8U2NN2d8alrG9OB712aQQZqHt5+5BBR/RhsdePfpMI0ebInw5hZE7fl7ypt8mthbBQP+xQesdcuYIilbXg8JvQKDH/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972462; c=relaxed/simple; bh=Vhp3vuWokN317wHEb9O9UL3ilPmfH+y4ZZA1i44uBXg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YwPe9cisE/AXEJdox5xjh5HZbL7lJ932gnHH+iJAyCLweqdlVoNT4ufQpdHsO7A6Qqxa+beJEuMqv5M8NaDWYZVkhTlOtqqgFdsBmUvwjuaEdHzzh78b79cuUzO1HrV2osksO+xgdslBNyckQI+IuLaRII+b6I/m/SNbEixr2J4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rdEt87CW; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rdEt87CW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96E8FC19423; Tue, 24 Feb 2026 22:34:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972461; bh=Vhp3vuWokN317wHEb9O9UL3ilPmfH+y4ZZA1i44uBXg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rdEt87CWIFfBIlDirRh7TT4naJikdwC6O5lZdYxe05MXCamybiAz2YU/TPbWwSqTR neMf7Cj6EXr18sskbWLV7G6yzL5nCdxN70P41B8qKDun4dgkS8Vmc3aaYPCgIY41hn Rs/gcVUK79cRSbXM4cyNzl+Ae3RHdAGoKYV8cm3/eyOhueLhxCCg2RKqD/+OVSzadW BwvbamYKK8RQaASYhRDF8JRBzvocv9ot3JuyhvnJhOrB/en9C/0n6Kxk0YRUSeH4HQ BJW33ERf7PYRjnOerXRP9CwvCNR+F28jT8s+ozpqxIkWggX0kb3MtY2QPd2WEPk9u7 xmn/DWKuiAPPA== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 01/31] KVM: nSVM: Avoid clearing VMCB_LBR in vmcb12 Date: Tue, 24 Feb 2026 22:33:35 +0000 Message-ID: <20260224223405.3270433-2-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" svm_copy_lbrs() always marks VMCB_LBR dirty in the destination VMCB. However, nested_svm_vmexit() uses it to copy LBRs to vmcb12, and clearing clean bits in vmcb12 is not architecturally defined. Move vmcb_mark_dirty() to callers and drop it for vmcb12. This also facilitates incoming refactoring that does not pass the entire VMCB to svm_copy_lbrs(). Fixes: d20c796ca370 ("KVM: x86: nSVM: implement nested LBR virtualization") Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 7 +++++-- arch/x86/kvm/svm/svm.c | 2 -- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index de90b104a0dd5..a31f3be1e16ec 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -714,6 +714,7 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm = *svm, struct vmcb *vmcb12 } else { svm_copy_lbrs(vmcb02, vmcb01); } + vmcb_mark_dirty(vmcb02, VMCB_LBR); svm_update_lbrv(&svm->vcpu); } =20 @@ -1232,10 +1233,12 @@ int nested_svm_vmexit(struct vcpu_svm *svm) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); =20 if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && - (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) + (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) { svm_copy_lbrs(vmcb12, vmcb02); - else + } else { svm_copy_lbrs(vmcb01, vmcb02); + vmcb_mark_dirty(vmcb01, VMCB_LBR); + } =20 svm_update_lbrv(vcpu); =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 8f8bc863e2143..a2452b8ec49db 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -848,8 +848,6 @@ void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *f= rom_vmcb) to_vmcb->save.br_to =3D from_vmcb->save.br_to; to_vmcb->save.last_excp_from =3D from_vmcb->save.last_excp_from; to_vmcb->save.last_excp_to =3D from_vmcb->save.last_excp_to; - - vmcb_mark_dirty(to_vmcb, VMCB_LBR); } =20 static void __svm_enable_lbrv(struct kvm_vcpu *vcpu) --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 83A4634BA4B; Tue, 24 Feb 2026 22:34:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972462; cv=none; b=Q0SNVR+Yhpk4JsmS8Vu5CakK2HKlHGfBhXHwme1kVmiU5F0VivPqKciPwuJGbMZjN2hihIjDlx6bE7iIGkQf7XegB0bAh/+Gh3eFJ2/oQ5qoiXUFFGFbahB0YTlMazKjNK/weu4PXkZ6+V9/uPRq6/JVTzE1b0a7GhbpvKMEBNQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972462; c=relaxed/simple; bh=30Ml8LFnO+8ooUnf0EPltYHb3swY1mHNC3qB72yZp0s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=keKsSSTQG1ut5t5ivzC8WX//rTNcNpxMYsX6ytiz3cXKapogMDzy6MFNABhjsVQyA/cVT/LlNfsDE5xYw1ogg8urD22SDOFw7V87xiSx9tPNrMzdoeh0ODkhqAO55lVYt1CEf+hdhg+37AKoyZD5+jk7QeNFumSI4entG87cOVE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LoWJBczx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LoWJBczx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0527FC116D0; Tue, 24 Feb 2026 22:34:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972462; bh=30Ml8LFnO+8ooUnf0EPltYHb3swY1mHNC3qB72yZp0s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LoWJBczxVMfjHKJeKGFv/SnFPu9Q8BHGjUvJ+XxWI0YtBz3m7xvmv7apcIfxyNnvV lkEsAd+OOaXaOH3wozqIoprzmrEG0NH24WLivT3HYBQlGNrWVlfzK1AZxgRScBFfOz nnZCB8LvKRMCPFkreVDRv7/+mRe1mDfcfjgKg1Phpf2JFBEjy3FziCrYljdfpPYDtP uyqXSkKS99RDAXlZpeeIXiZuBFzcTDmFuomKCORN8+MtXCTH2GHqzwpE5GNOXInU4J HNl6hhZd4+r7UgfPbZlGiOy6W8ZDWCEn1deWYEXGcGbD7a7lZWseQnydEPUk7L8rgs l39TgDdoHuy1Q== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 02/31] KVM: SVM: Switch svm_copy_lbrs() to a macro Date: Tue, 24 Feb 2026 22:33:36 +0000 Message-ID: <20260224223405.3270433-3-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for using svm_copy_lbrs() with 'struct vmcb_save_area' without a containing 'struct vmcb', and later even 'struct vmcb_save_area_cached', make it a macro. Macros are generally not preferred compared to functions, mainly due to type-safety. However, in this case it seems like having a simple macro copying a few fields is better than copy-pasting the same 5 lines of code in different places. Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 8 ++++---- arch/x86/kvm/svm/svm.c | 9 --------- arch/x86/kvm/svm/svm.h | 10 +++++++++- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index a31f3be1e16ec..f7d5db0af69ac 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -709,10 +709,10 @@ static void nested_vmcb02_prepare_save(struct vcpu_sv= m *svm, struct vmcb *vmcb12 * Reserved bits of DEBUGCTL are ignored. Be consistent with * svm_set_msr's definition of reserved bits. */ - svm_copy_lbrs(vmcb02, vmcb12); + svm_copy_lbrs(&vmcb02->save, &vmcb12->save); vmcb02->save.dbgctl &=3D ~DEBUGCTL_RESERVED_BITS; } else { - svm_copy_lbrs(vmcb02, vmcb01); + svm_copy_lbrs(&vmcb02->save, &vmcb01->save); } vmcb_mark_dirty(vmcb02, VMCB_LBR); svm_update_lbrv(&svm->vcpu); @@ -1234,9 +1234,9 @@ int nested_svm_vmexit(struct vcpu_svm *svm) =20 if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) { - svm_copy_lbrs(vmcb12, vmcb02); + svm_copy_lbrs(&vmcb12->save, &vmcb02->save); } else { - svm_copy_lbrs(vmcb01, vmcb02); + svm_copy_lbrs(&vmcb01->save, &vmcb02->save); vmcb_mark_dirty(vmcb01, VMCB_LBR); } =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index a2452b8ec49db..f52e588317fcf 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -841,15 +841,6 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu = *vcpu) */ } =20 -void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb) -{ - to_vmcb->save.dbgctl =3D from_vmcb->save.dbgctl; - to_vmcb->save.br_from =3D from_vmcb->save.br_from; - to_vmcb->save.br_to =3D from_vmcb->save.br_to; - to_vmcb->save.last_excp_from =3D from_vmcb->save.last_excp_from; - to_vmcb->save.last_excp_to =3D from_vmcb->save.last_excp_to; -} - static void __svm_enable_lbrv(struct kvm_vcpu *vcpu) { to_svm(vcpu)->vmcb->control.virt_ext |=3D LBR_CTL_ENABLE_MASK; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index ebd7b36b1ceb9..44d767cd1d25a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -713,8 +713,16 @@ static inline void *svm_vcpu_alloc_msrpm(void) return svm_alloc_permissions_map(MSRPM_SIZE, GFP_KERNEL_ACCOUNT); } =20 +#define svm_copy_lbrs(to, from) \ +do { \ + (to)->dbgctl =3D (from)->dbgctl; \ + (to)->br_from =3D (from)->br_from; \ + (to)->br_to =3D (from)->br_to; \ + (to)->last_excp_from =3D (from)->last_excp_from; \ + (to)->last_excp_to =3D (from)->last_excp_to; \ +} while (0) + void svm_vcpu_free_msrpm(void *msrpm); -void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb); void svm_enable_lbrv(struct kvm_vcpu *vcpu); void svm_update_lbrv(struct kvm_vcpu *vcpu); =20 --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 C7DB834DB44; Tue, 24 Feb 2026 22:34:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972462; cv=none; b=i9oI84Md4XNthoNlxfS/T8fYYGp3EW0iecMs3po4vMlaugF9GsVUqg5dlLsp5xsjYHlOBuhX3fanuTgWodY3AKRUBABmYiJAfW+h63sn+4/9lQ0N28Q2sPBRndt9vTan++mu6aLeuY1YiRxwL2jJvEAfwJSEAWDf6xKsirCZQUQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972462; c=relaxed/simple; bh=TQeKGqoqKxkagIjzDzM1UTz/dPro9Gug3QNtz5J/bNk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lJJ9OBOLtuwQabRHfHcPutAM7nzmV+bFjTGhk+fH8Y1ylc9lty6YozaxxmupdSliww2OVke3W1vd9pIY0+Q3bYurlkRO3YYuz1KU2ObvrgnivbTfWoBNP8vvN0ACzULNd4+DmgGlPNZJ+60kfQSFXdbsDuINMx0iplyeCJyGlTY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mle5tSWp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Mle5tSWp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67A6DC19424; Tue, 24 Feb 2026 22:34:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972462; bh=TQeKGqoqKxkagIjzDzM1UTz/dPro9Gug3QNtz5J/bNk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mle5tSWp7zF1prFhF7Bb6gPlj3gmrNqrfM0FdCUXk7Q3yAp2ZZPojLIVJYxpEBMat nRxpmFTtkyx3kyPWXBd+tskcBMLtRO6uhqnOKOt6zC9hrsbndJVIwZTAQ7Z1CYUI4R AnO/ouNzavm9FLZbgDiHrtua83pUf8Y8YFB8zaTpJZX4O8nccmYR/y4BnmAIN+1Nqt XiSRanGMFmqzSmtq9GW97qe6+D41fv4z9Z9ce4akzfZHV5OZ3Rm/dkFU69eNJVDZ7A LCcACrO92NyyrNGhq+6/94BMJsMQYHJLeMHuXrXfn27Tp4h3gATARuq5icnLgu8DUo WHisPIcDB7CJQ== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org, Jim Mattson Subject: [PATCH v6 03/31] KVM: SVM: Add missing save/restore handling of LBR MSRs Date: Tue, 24 Feb 2026 22:33:37 +0000 Message-ID: <20260224223405.3270433-4-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" MSR_IA32_DEBUGCTLMSR and LBR MSRs are currently not enumerated by KVM_GET_MSR_INDEX_LIST, and LBR MSRs cannot be set with KVM_SET_MSRS. So save/restore is completely broken. Fix it by adding the MSRs to msrs_to_save_base, and allowing writes to LBR MSRs from userspace only (as they are read-only MSRs). Additionally, to correctly restore L1's LBRs while L2 is running, make sure the LBRs are copied from the captured VMCB01 save area in svm_copy_vmrun_state(). Fixes: 24e09cbf480a ("KVM: SVM: enable LBR virtualization") Cc: stable@vger.kernel.org Reported-by: Jim Mattson Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 3 +++ arch/x86/kvm/svm/svm.c | 24 ++++++++++++++++++++++++ arch/x86/kvm/x86.c | 3 +++ 3 files changed, 30 insertions(+) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index f7d5db0af69ac..52d8536845927 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1100,6 +1100,9 @@ void svm_copy_vmrun_state(struct vmcb_save_area *to_s= ave, to_save->isst_addr =3D from_save->isst_addr; to_save->ssp =3D from_save->ssp; } + + if (lbrv) + svm_copy_lbrs(to_save, from_save); } =20 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmc= b) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index f52e588317fcf..cb53174583a26 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3071,6 +3071,30 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct= msr_data *msr) vmcb_mark_dirty(svm->vmcb, VMCB_LBR); svm_update_lbrv(vcpu); break; + case MSR_IA32_LASTBRANCHFROMIP: + if (!msr->host_initiated) + return 1; + svm->vmcb->save.br_from =3D data; + vmcb_mark_dirty(svm->vmcb, VMCB_LBR); + break; + case MSR_IA32_LASTBRANCHTOIP: + if (!msr->host_initiated) + return 1; + svm->vmcb->save.br_to =3D data; + vmcb_mark_dirty(svm->vmcb, VMCB_LBR); + break; + case MSR_IA32_LASTINTFROMIP: + if (!msr->host_initiated) + return 1; + svm->vmcb->save.last_excp_from =3D data; + vmcb_mark_dirty(svm->vmcb, VMCB_LBR); + break; + case MSR_IA32_LASTINTTOIP: + if (!msr->host_initiated) + return 1; + svm->vmcb->save.last_excp_to =3D data; + vmcb_mark_dirty(svm->vmcb, VMCB_LBR); + break; case MSR_VM_HSAVE_PA: /* * Old kernels did not validate the value written to diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index db3f393192d94..416899b5dbe4d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -351,6 +351,9 @@ static const u32 msrs_to_save_base[] =3D { MSR_IA32_U_CET, MSR_IA32_S_CET, MSR_IA32_PL0_SSP, MSR_IA32_PL1_SSP, MSR_IA32_PL2_SSP, MSR_IA32_PL3_SSP, MSR_IA32_INT_SSP_TAB, + MSR_IA32_DEBUGCTLMSR, + MSR_IA32_LASTBRANCHFROMIP, MSR_IA32_LASTBRANCHTOIP, + MSR_IA32_LASTINTFROMIP, MSR_IA32_LASTINTTOIP, }; =20 static const u32 msrs_to_save_pmu[] =3D { --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 55A70352926; Tue, 24 Feb 2026 22:34:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972463; cv=none; b=bt27ywinI9/eHfPeXPRqYw8elBzGFgw5/7P+KzefGcZ2vW84AZG9iWl0NfI209UbwL5FG7iviWoiYXMxUed6P5MDkdap17xfn0TxFOYoR6kqAdOunGr3mPgE+4T+Etcnx0wFJ5eyiTmMQUpsxjjlVdsUGphtipBu9T//SMOAaxo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972463; c=relaxed/simple; bh=j5MTxH2ZUc2Um2PtXTxg0iEINlTGV/IJPm7hODaEIpQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RkdE0uEdIcglSicdMONm1c35OT+mri6N5ma500rXn4U7BEAyaX31Zgs3cjZ1EzLAe64W5zliZkK94+7tAwCbMdRpn9yGhUonqCN1Sx/GX6N2sjW2EjAumUhCgotY9VPCCHOK7KzTD2nHlEYfc4Haf2HDcCjpkvFxXwMVSZKjkuU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZWpt5+pr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZWpt5+pr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D28DBC2BCB1; Tue, 24 Feb 2026 22:34:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972463; bh=j5MTxH2ZUc2Um2PtXTxg0iEINlTGV/IJPm7hODaEIpQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZWpt5+prx1HpijmbAs1NeqkPWpYsG4fHECKVRfLfz3GbOqq22bgyPbW7/9aHKSMis G1w/M1F/wJuSvzSckAA0Ks+b8vFCZT/t76hCvi0jbli+HgQx4OduX33cy9wV8ePkEr u9zpiTvMt54pxTt9jg3xrWLSpG2Z7o5A+RgAg9RVh2OzxSGkwQjH9vF0duij1RAV3H VOvwgAed8AinyJ42okHykngzhi5J5P67A9GkKhdVQGfjxfGndHQfzlJVqlS+rdbnfN OwP+yGkgkfvoSmI31G5YqITNlF0A4JSYKXBL2syGCmRxvU1kDkBDcy1XhKz0AeTjoN ujJLi9275XEYA== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v6 04/31] KVM: selftests: Add a test for LBR save/restore (ft. nested) Date: Tue, 24 Feb 2026 22:33:38 +0000 Message-ID: <20260224223405.3270433-5-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a selftest exercising save/restore with usage of LBRs in both L1 and L2, and making sure all LBRs remain intact. Signed-off-by: Yosry Ahmed --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/processor.h | 5 + .../selftests/kvm/x86/svm_lbr_nested_state.c | 145 ++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index fdec90e854671..36b48e766e499 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -112,6 +112,7 @@ TEST_GEN_PROGS_x86 +=3D x86/svm_vmcall_test TEST_GEN_PROGS_x86 +=3D x86/svm_int_ctl_test TEST_GEN_PROGS_x86 +=3D x86/svm_nested_shutdown_test TEST_GEN_PROGS_x86 +=3D x86/svm_nested_soft_inject_test +TEST_GEN_PROGS_x86 +=3D x86/svm_lbr_nested_state TEST_GEN_PROGS_x86 +=3D x86/tsc_scaling_sync TEST_GEN_PROGS_x86 +=3D x86/sync_regs_test TEST_GEN_PROGS_x86 +=3D x86/ucna_injection_test diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/te= sting/selftests/kvm/include/x86/processor.h index 4ebae4269e681..db0171935197d 100644 --- a/tools/testing/selftests/kvm/include/x86/processor.h +++ b/tools/testing/selftests/kvm/include/x86/processor.h @@ -1360,6 +1360,11 @@ static inline bool kvm_is_ignore_msrs(void) return get_kvm_param_bool("ignore_msrs"); } =20 +static inline bool kvm_is_lbrv_enabled(void) +{ + return !!get_kvm_amd_param_integer("lbrv"); +} + uint64_t *vm_get_pte(struct kvm_vm *vm, uint64_t vaddr); =20 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, diff --git a/tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c b/tools= /testing/selftests/kvm/x86/svm_lbr_nested_state.c new file mode 100644 index 0000000000000..bf16abb1152e0 --- /dev/null +++ b/tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2026, Google, Inc. + */ + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" + + +#define L2_GUEST_STACK_SIZE 64 + +#define DO_BRANCH() do { asm volatile("jmp 1f\n 1: nop"); } while (0) + +struct lbr_branch { + u64 from, to; +}; + +volatile struct lbr_branch l2_branch; + +#define RECORD_AND_CHECK_BRANCH(b) \ +do { \ + wrmsr(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR); \ + DO_BRANCH(); \ + (b)->from =3D rdmsr(MSR_IA32_LASTBRANCHFROMIP); \ + (b)->to =3D rdmsr(MSR_IA32_LASTBRANCHTOIP); \ + /* Disable LBR right after to avoid overriding the IPs */ \ + wrmsr(MSR_IA32_DEBUGCTLMSR, 0); \ + \ + GUEST_ASSERT_NE((b)->from, 0); \ + GUEST_ASSERT_NE((b)->to, 0); \ +} while (0) + +#define CHECK_BRANCH_MSRS(b) \ +do { \ + GUEST_ASSERT_EQ((b)->from, rdmsr(MSR_IA32_LASTBRANCHFROMIP)); \ + GUEST_ASSERT_EQ((b)->to, rdmsr(MSR_IA32_LASTBRANCHTOIP)); \ +} while (0) + +#define CHECK_BRANCH_VMCB(b, vmcb) \ +do { \ + GUEST_ASSERT_EQ((b)->from, vmcb->save.br_from); \ + GUEST_ASSERT_EQ((b)->to, vmcb->save.br_to); \ +} while (0) + +static void l2_guest_code(struct svm_test_data *svm) +{ + /* Record a branch, trigger save/restore, and make sure LBRs are intact */ + RECORD_AND_CHECK_BRANCH(&l2_branch); + GUEST_SYNC(true); + CHECK_BRANCH_MSRS(&l2_branch); + vmmcall(); +} + +static void l1_guest_code(struct svm_test_data *svm, bool nested_lbrv) +{ + unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; + struct vmcb *vmcb =3D svm->vmcb; + struct lbr_branch l1_branch; + + /* Record a branch, trigger save/restore, and make sure LBRs are intact */ + RECORD_AND_CHECK_BRANCH(&l1_branch); + GUEST_SYNC(true); + CHECK_BRANCH_MSRS(&l1_branch); + + /* Run L2, which will also do the same */ + generic_svm_setup(svm, l2_guest_code, + &l2_guest_stack[L2_GUEST_STACK_SIZE]); + + if (nested_lbrv) + vmcb->control.virt_ext =3D LBR_CTL_ENABLE_MASK; + else + vmcb->control.virt_ext &=3D ~LBR_CTL_ENABLE_MASK; + + run_guest(vmcb, svm->vmcb_gpa); + GUEST_ASSERT(svm->vmcb->control.exit_code =3D=3D SVM_EXIT_VMMCALL); + + /* Trigger save/restore one more time before checking, just for kicks */ + GUEST_SYNC(true); + + /* + * If LBR_CTL_ENABLE is set, L1 and L2 should have separate LBR MSRs, so + * expect L1's LBRs to remain intact and L2 LBRs to be in the VMCB. + * Otherwise, the MSRs are shared between L1 & L2 so expect L2's LBRs. + */ + if (nested_lbrv) { + CHECK_BRANCH_MSRS(&l1_branch); + CHECK_BRANCH_VMCB(&l2_branch, vmcb); + } else { + CHECK_BRANCH_MSRS(&l2_branch); + } + GUEST_DONE(); +} + +void test_lbrv_nested_state(bool nested_lbrv) +{ + struct kvm_x86_state *state =3D NULL; + struct kvm_vcpu *vcpu; + vm_vaddr_t svm_gva; + struct kvm_vm *vm; + struct ucall uc; + + pr_info("Testing with nested LBRV %s\n", nested_lbrv ? "enabled" : "disab= led"); + + vm =3D vm_create_with_one_vcpu(&vcpu, l1_guest_code); + vcpu_alloc_svm(vm, &svm_gva); + vcpu_args_set(vcpu, 2, svm_gva, nested_lbrv); + + for (;;) { + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + switch (get_ucall(vcpu, &uc)) { + case UCALL_SYNC: + /* Save the vCPU state and restore it in a new VM on sync */ + pr_info("Guest triggered save/restore.\n"); + state =3D vcpu_save_state(vcpu); + kvm_vm_release(vm); + vcpu =3D vm_recreate_with_one_vcpu(vm); + vcpu_load_state(vcpu, state); + kvm_x86_state_cleanup(state); + break; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + /* NOT REACHED */ + case UCALL_DONE: + goto done; + default: + TEST_FAIL("Unknown ucall %lu", uc.cmd); + } + } +done: + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM)); + TEST_REQUIRE(kvm_is_lbrv_enabled()); + + test_lbrv_nested_state(/*nested_lbrv=3D*/false); + test_lbrv_nested_state(/*nested_lbrv=3D*/true); + + return 0; +} --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9DC7C35B62C; Tue, 24 Feb 2026 22:34:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972463; cv=none; b=fjlnBSd6VN+J7+a5Ayw4DyPZvZcy9vk7FNMtr/GGyK7G9wQ5Cv1BewD+atUs3ShU04kcsJI9QTu/+V2Zf8ipzzl+4q3wRz/LP7CFCFI4PP7VgIzkdfhelE6B9g+n7N93flXiSvXx/QWTCIK2avmY+eGYrNbR7JglKfih0aiScYs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972463; c=relaxed/simple; bh=Lf5SqEFhbFhXQTZaAcWAw6rxFcn0CcQDoxXm+TMCMvI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tHtWvwFJWzV2aGoiPvkyFR7OhxmMaH4KXwlnAgYrkUQTTSdepXc4KW/+bfHBgpR3OIbAaAGEhQPmF4Rsqsbu9e/0JPOphd9YJx53KZlor2pjGYCu8F5yKL6hTn/z5dTRUB93aNrPZ47NTddwwuAm9K3t9JNx2hfDRcGbs3y/8LQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rACZK4IG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rACZK4IG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 38DBFC19423; Tue, 24 Feb 2026 22:34:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972463; bh=Lf5SqEFhbFhXQTZaAcWAw6rxFcn0CcQDoxXm+TMCMvI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rACZK4IGW5NpfpXH2dkv5x024becpOWb/6V3wK0U8bEFwvmy2Olj6w1hurKFiABc6 k9xgb8drnS49mtcDRveiFEFYyMXOtz9xxkV5J3vCUgR8JDHqH3Ge02QahICVqKRiqR wzVg/YYYqLb9nQOyc/ffzpPA/f9YIhTAwRJCtCvZ/s5jzJAs9/0X8jbCI+NvSw1T25 JMkYgkMEmOMPhx7nsgzaSViA1NS4soK5JaXnmKpeyz8P9KzWxAfRMiv4yOd/UPbdU6 Eb53d+k1PglUOcwhb5gzMiGFjberYTwsKjLuEJYivbHNST3DqSGhMmrmiQGG9trJ2c STEP5fdmdowDw== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 05/31] KVM: nSVM: Always inject a #GP if mapping VMCB12 fails on nested VMRUN Date: Tue, 24 Feb 2026 22:33:39 +0000 Message-ID: <20260224223405.3270433-6-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" nested_svm_vmrun() currently only injects a #GP if kvm_vcpu_map() fails with -EINVAL. But it could also fail with -EFAULT if creating a host mapping failed. Inject a #GP in all cases, no reason to treat failure modes differently. Fixes: 8c5fbf1a7231 ("KVM/nSVM: Use the new mapping API for mapping guest m= emory") CC: stable@vger.kernel.org Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 52d8536845927..fab0d3d5baa27 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1011,12 +1011,9 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) } =20 vmcb12_gpa =3D svm->vmcb->save.rax; - ret =3D kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map); - if (ret =3D=3D -EINVAL) { + if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map)) { kvm_inject_gp(vcpu, 0); return 1; - } else if (ret) { - return kvm_skip_emulated_instruction(vcpu); } =20 ret =3D kvm_skip_emulated_instruction(vcpu); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1AE49331A53; Tue, 24 Feb 2026 22:34:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972464; cv=none; b=I3dCHLIhH9szF4KuIYyGMwRR9q2huZBcTQcj2RGOd4kNrsy++5xsktMMbvsQspKUaR7Vq6SMK34qE6a+f/STWLwqnWEssof5mlAmzPOmOEb1E/P1qcKnDP3f0SmMe9g9kXh4CIrxKNqdBB4YmjTDIajkzZvdCH6tmyv2HWYtprU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972464; c=relaxed/simple; bh=9ShEO/QZRZBcKkoR1itXOhFupVlsvEfFEAnT4eRqWzU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fALD/k0+RLa5KqlVXNGkff86z7+RAmsl3wZbKdTZ7Bj94GpjUP4IwnTY/oePvfTP250kovKQI7VmZbBqSStCWLlGDM2wF3evZJgVeEbP6+8wJ7MZ+4B+PWCurV2iX1dHKcEflnMASLjNPyI5GNmgfxXTTmZh1plM9GmRDV6DcAU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r6t5A8AG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r6t5A8AG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BB7FC116D0; Tue, 24 Feb 2026 22:34:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972463; bh=9ShEO/QZRZBcKkoR1itXOhFupVlsvEfFEAnT4eRqWzU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r6t5A8AG86SBeI03wXSpI6G/WcTEAQ+4bJg8/2NyhEJ5HujejEuoIgom62DT5kMX6 A4Xq/M5aL3/duo2BGRSP80A9ymADvVRTmYtlSo43yC3RWHOYwxuokXCsjvgrdgXVWe 7cfrSI14U7k5biQmVqD7KGhNFU7bAgCbLT5F7mrlNjpo3JmroDrXbPf19FpPGOvcD1 8wD2i6/viAimr+SUgDUuJq6y4xQzt7DAEhCXMx1bkRsHXIEnjBdo459T4mx89hSCX6 8HKzs/RNzkLo4CtyFZsfofwp40s3gOB1ZNN6Cb6/knF2W/OFdh13aXYR32ng7+VR1D wXVilpIdo2xoA== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 06/31] KVM: nSVM: Refactor checking LBRV enablement in vmcb12 into a helper Date: Tue, 24 Feb 2026 22:33:40 +0000 Message-ID: <20260224223405.3270433-7-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor the vCPU cap and vmcb12 flag checks into a helper. The unlikely() annotation is dropped, it's unlikely (huh) to make a difference and the CPU will probably predict it better on its own. CC: stable@vger.kernel.org Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index fab0d3d5baa27..d11cf4968adbe 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -639,6 +639,12 @@ void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm) svm->nested.vmcb02.ptr->save.g_pat =3D svm->vmcb01.ptr->save.g_pat; } =20 +static bool nested_vmcb12_has_lbrv(struct kvm_vcpu *vcpu) +{ + return guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && + (to_svm(vcpu)->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK); +} + static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *= vmcb12) { bool new_vmcb12 =3D false; @@ -703,8 +709,7 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm = *svm, struct vmcb *vmcb12 vmcb_mark_dirty(vmcb02, VMCB_DR); } =20 - if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && - (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) { + if (nested_vmcb12_has_lbrv(vcpu)) { /* * Reserved bits of DEBUGCTL are ignored. Be consistent with * svm_set_msr's definition of reserved bits. @@ -1232,8 +1237,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) if (!nested_exit_on_intr(svm)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); =20 - if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && - (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) { + if (nested_vmcb12_has_lbrv(vcpu)) { svm_copy_lbrs(&vmcb12->save, &vmcb02->save); } else { svm_copy_lbrs(&vmcb01->save, &vmcb02->save); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6F7B1346A0C; Tue, 24 Feb 2026 22:34:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972464; cv=none; b=eoC7ckWFBomeo3oyPO17Q64LwIW9o06X9115yu1oeKirCYaZETPoM8qrcCHinvfZSg1hVrd7C/qt70w8lxOfYLMRPcny1TtJBFOnB0dgFIusFSewx3x46T19A0G1pTjP3nGJFiAdWprSUVsZHOFEK7xPHwrcoRzK3T8FDFmz6fk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972464; c=relaxed/simple; bh=myMSS4//ns4VuLWyalWOsz3xbALr3H3i0KHVPyhwLp8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CtGzPdSmLzxT3+LLdSRx3cDPxlCjBATO1t96L6mvvsXEQL7x8lmt7ahqgrbDrcI+2MYD2E3ARBJOz5CdZjlc8dEGfLJ4mDODG6IVc5OZwAaN2u83nTlvYctwjKXxkNLa+XC148HcK0kKfZ9TNTVPf+N8P2owboHofSiAknmJKNk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AfroW8Vc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AfroW8Vc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 09EE6C19423; Tue, 24 Feb 2026 22:34:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972464; bh=myMSS4//ns4VuLWyalWOsz3xbALr3H3i0KHVPyhwLp8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AfroW8VcNAd3AEhrTOPRqJM6XHihNjsy/bI4EZmG6BpY8lKVRMzq7OB/hgiQYvA6C 0KgKkDwA5M07YEJH7C1uhC1t7tHPi31AtxmupGBO8mDJiXgO/Mevzc/K0prFObj+zB a7jFWpgEpKwXmB5D4Ai6XdCwi2DfxTgkNFLzAVRzpVBdFdlyNS1wuOTQfOoWKfX8ki oyMIAk+22a8mNDMWLix5hyVYIcWAIJsMqpzOH1ADTDbBG4l+W7+2iRhLxd3nPdYd+U EPU4qZMH+JLJIyNK68sWn/3rXoVWH0HBpiD+Lly//xCyEaFgzkbmOx3dlRrW24KRzc /0M9o75ghRM5g== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 07/31] KVM: nSVM: Refactor writing vmcb12 on nested #VMEXIT as a helper Date: Tue, 24 Feb 2026 22:33:41 +0000 Message-ID: <20260224223405.3270433-8-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move mapping vmcb12 and updating it out of nested_svm_vmexit() into a helper, no functional change intended. CC: stable@vger.kernel.org Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 77 ++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index d11cf4968adbe..6364c52e6e0a8 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1123,36 +1123,20 @@ void svm_copy_vmloadsave_state(struct vmcb *to_vmcb= , struct vmcb *from_vmcb) to_vmcb->save.sysenter_eip =3D from_vmcb->save.sysenter_eip; } =20 -int nested_svm_vmexit(struct vcpu_svm *svm) +static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu) { - struct kvm_vcpu *vcpu =3D &svm->vcpu; - struct vmcb *vmcb01 =3D svm->vmcb01.ptr; + struct vcpu_svm *svm =3D to_svm(vcpu); struct vmcb *vmcb02 =3D svm->nested.vmcb02.ptr; - struct vmcb *vmcb12; struct kvm_host_map map; + struct vmcb *vmcb12; int rc; =20 rc =3D kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map); - if (rc) { - if (rc =3D=3D -EINVAL) - kvm_inject_gp(vcpu, 0); - return 1; - } + if (rc) + return rc; =20 vmcb12 =3D map.hva; =20 - /* Exit Guest-Mode */ - leave_guest_mode(vcpu); - svm->nested.vmcb12_gpa =3D 0; - WARN_ON_ONCE(svm->nested.nested_run_pending); - - kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); - - /* in case we halted in L2 */ - kvm_set_mp_state(vcpu, KVM_MP_STATE_RUNNABLE); - - /* Give the current vmcb to the guest */ - vmcb12->save.es =3D vmcb02->save.es; vmcb12->save.cs =3D vmcb02->save.cs; vmcb12->save.ss =3D vmcb02->save.ss; @@ -1189,10 +1173,48 @@ int nested_svm_vmexit(struct vcpu_svm *svm) if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS)) vmcb12->control.next_rip =3D vmcb02->control.next_rip; =20 + if (nested_vmcb12_has_lbrv(vcpu)) + svm_copy_lbrs(&vmcb12->save, &vmcb02->save); + vmcb12->control.int_ctl =3D svm->nested.ctl.int_ctl; vmcb12->control.event_inj =3D svm->nested.ctl.event_inj; vmcb12->control.event_inj_err =3D svm->nested.ctl.event_inj_err; =20 + trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code, + vmcb12->control.exit_info_1, + vmcb12->control.exit_info_2, + vmcb12->control.exit_int_info, + vmcb12->control.exit_int_info_err, + KVM_ISA_SVM); + + kvm_vcpu_unmap(vcpu, &map); + return 0; +} + +int nested_svm_vmexit(struct vcpu_svm *svm) +{ + struct kvm_vcpu *vcpu =3D &svm->vcpu; + struct vmcb *vmcb01 =3D svm->vmcb01.ptr; + struct vmcb *vmcb02 =3D svm->nested.vmcb02.ptr; + int rc; + + rc =3D nested_svm_vmexit_update_vmcb12(vcpu); + if (rc) { + if (rc =3D=3D -EINVAL) + kvm_inject_gp(vcpu, 0); + return 1; + } + + /* Exit Guest-Mode */ + leave_guest_mode(vcpu); + svm->nested.vmcb12_gpa =3D 0; + WARN_ON_ONCE(svm->nested.nested_run_pending); + + kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); + + /* in case we halted in L2 */ + kvm_set_mp_state(vcpu, KVM_MP_STATE_RUNNABLE); + if (!kvm_pause_in_guest(vcpu->kvm)) { vmcb01->control.pause_filter_count =3D vmcb02->control.pause_filter_coun= t; vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS); @@ -1237,9 +1259,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) if (!nested_exit_on_intr(svm)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); =20 - if (nested_vmcb12_has_lbrv(vcpu)) { - svm_copy_lbrs(&vmcb12->save, &vmcb02->save); - } else { + if (!nested_vmcb12_has_lbrv(vcpu)) { svm_copy_lbrs(&vmcb01->save, &vmcb02->save); vmcb_mark_dirty(vmcb01, VMCB_LBR); } @@ -1295,15 +1315,6 @@ int nested_svm_vmexit(struct vcpu_svm *svm) svm->vcpu.arch.dr7 =3D DR7_FIXED_1; kvm_update_dr7(&svm->vcpu); =20 - trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code, - vmcb12->control.exit_info_1, - vmcb12->control.exit_info_2, - vmcb12->control.exit_int_info, - vmcb12->control.exit_int_info_err, - KVM_ISA_SVM); - - kvm_vcpu_unmap(vcpu, &map); - nested_svm_transition_tlb_flush(vcpu); =20 nested_svm_uninit_mmu_context(vcpu); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 C530E361675; Tue, 24 Feb 2026 22:34:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972464; cv=none; b=g+Tn1DwNRiWIaWlHFxLKpvPwKlB+RnaoULnvnvc4XEEsGrJ5ZQvTD3VaX055LKJtj1XLNXzpgyK36989S8Jco56QM/VpKs+Y7+F5yyx98Y+8nk6hYVN/4kJyb61uUXqx3nqWFmgafVSh2kaTgGXCtn0/GzxeDHEd1pEyA5tMEF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972464; c=relaxed/simple; bh=09jqxGNj7+pt+3TIkV+wQfEzua56Gn/NW8yGD9b2J4w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IwmeHkiQ9c2nlZMDcGogMd/RiIVKUGvAVPs4Qbyex9ybeIbvaAkUF/kbCne5kfkBAu09Vvm1Y3Hx4XFWK84FfKWoNGBz8ZVimTHcqWq5kXAXyQ1zaT/NwilI4BVQdhrL4Me9ngtJIaYnAf2JtN9AbQXN9I4Q6tVAhheAJs0UDpU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rKgAFSYC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rKgAFSYC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C305C2BC86; Tue, 24 Feb 2026 22:34:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972464; bh=09jqxGNj7+pt+3TIkV+wQfEzua56Gn/NW8yGD9b2J4w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rKgAFSYCio+7URovISk75T8ETV4xlv8cO4PdBcc+wyGRaPg45pL7N1KvU7GBhwCDe PgSUmZ8+zSmV/IB3hEchPtvrXXdYxw4puLZHRIyHYlQgn7HJ+zZBWoCIxyd7OBrfGY 6VcSRMrTK4xmcNoIxnLfNJpAna+kKdrcL4c+POXI7Qd9GJyymNcK7yBXVtMWtV8/+c MB/1LyNnwrX1kbA1gN7zAE8aXQsDxjnvNYum9EQvcOBQNNCodDJJ47axHUsXh5S4mb ZBttZNaPHm5FicGtG3MhVt/eDI+1/rLJHBy7hTQ1y5K4hY1mT/77dCLeaZBfbvQxaa ae+L9OYA1mmAw== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 08/31] KVM: nSVM: Triple fault if mapping VMCB12 fails on nested #VMEXIT Date: Tue, 24 Feb 2026 22:33:42 +0000 Message-ID: <20260224223405.3270433-9-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" KVM currently injects a #GP and hopes for the best if mapping VMCB12 fails on nested #VMEXIT, and only if the failure mode is -EINVAL. Mapping the VMCB12 could also fail if creating host mappings fails. After the #GP is injected, nested_svm_vmexit() bails early, without cleaning up (e.g. KVM_REQ_GET_NESTED_STATE_PAGES is set, is_guest_mode() is true, etc). Instead of optionally injecting a #GP, triple fault the guest if mapping VMCB12 fails since KVM cannot make a sane recovery. The APM states that a #VMEXIT will triple fault if host state is illegal or an exception occurs while loading host state, so the behavior is not entirely made up. Do not return early from nested_svm_vmexit(), continue cleaning up the vCPU state (e.g. switch back to vmcb01), to handle the failure as gracefully as possible. Fixes: cf74a78b229d ("KVM: SVM: Add VMEXIT handler and intercepts") CC: stable@vger.kernel.org Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 6364c52e6e0a8..280d0fccd1971 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1198,12 +1198,8 @@ int nested_svm_vmexit(struct vcpu_svm *svm) struct vmcb *vmcb02 =3D svm->nested.vmcb02.ptr; int rc; =20 - rc =3D nested_svm_vmexit_update_vmcb12(vcpu); - if (rc) { - if (rc =3D=3D -EINVAL) - kvm_inject_gp(vcpu, 0); - return 1; - } + if (nested_svm_vmexit_update_vmcb12(vcpu)) + kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); =20 /* Exit Guest-Mode */ leave_guest_mode(vcpu); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3462334B1A3; Tue, 24 Feb 2026 22:34:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972465; cv=none; b=sOtXRoK6Am1xr7e6ohkNeDoYElAz3itdGBRSq4a7MEugQeuZrws+hwK6ohvPFR3bTfJLYt/lMWj0pRlUg2par1NnaIxZAL6ib/2kTYP6aNAmc5ZNp1blnTTEJstOUH62t2NpWJLez5mSdgxqFa+oJEeB6qzHoyGO3xtSnWhVE64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972465; c=relaxed/simple; bh=mGtF0Nz4GONPYrQId/2sXtHw9VAke+w2/SaOhrC75C8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EhwXwvZDs8DKBGn1z/h3WLD+5rRaHPsKkN3zbtWPqHtt1gM7k6J/JcAg00u/aTlXRt53Jl2KNZhimI5jf0tgDq9kOLbv5rlTg25fDjcriZ85KFcsH/KDJpPlYsvHBLCkeGZnIy3M45PZorHW9Hd2CLGAtnjy0mGgJCyPU2T+zjo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VpPESNg4; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VpPESNg4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF243C2BCB2; Tue, 24 Feb 2026 22:34:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972465; bh=mGtF0Nz4GONPYrQId/2sXtHw9VAke+w2/SaOhrC75C8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VpPESNg4eCdQDirTNQcFeP8vHfGs5k86JQxMBNIr1PzFl8sytWiUKaWr1vHYzf0v1 9mylC27l7qdBJKUeOKqQgQNQNKxIgMfrHM2E1XY43fdKR4vlhLyMwfvkYrBuB7Rd/n 3uP8JkmhxyHqAvPVqIKAkaGY3FrthmKpDFbJDOFAfFZ8J3xjO+tKjvmlbrleomK0lD ro2uTqpRTclzynclJ/xCv4tFj7psJR6bJ/iPEHudS1EeTyqgCNyEgd3+G8uh/cawrS LiGhRZpKzVHG2V4/IAqog8Xd9Xcw0A4iUxuLfN6gYHKtTvuss9uIm76e5oNalcKD+J BeZzgX63bkauw== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 09/31] KVM: nSVM: Triple fault if restore host CR3 fails on nested #VMEXIT Date: Tue, 24 Feb 2026 22:33:43 +0000 Message-ID: <20260224223405.3270433-10-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If loading L1's CR3 fails on a nested #VMEXIT, nested_svm_vmexit() returns an error code that is ignored by most callers, and continues to run L1 with corrupted state. A sane recovery is not possible in this case, and HW behavior is to cause a shutdown. Inject a triple fault ,nstead, and do not return early from nested_svm_vmexit(). Continue cleaning up the vCPU state (e.g. clear pending exceptions), to handle the failure as gracefully as possible. From the APM: Upon #VMEXIT, the processor performs the following actions in order to return to the host execution context: ... if (illegal host state loaded, or exception while loading host state) shutdown else execute first host instruction following the VMRUN Remove the return value of nested_svm_vmexit(), which is mostly unchecked anyway. Fixes: d82aaef9c88a ("KVM: nSVM: use nested_svm_load_cr3() on guest->host s= witch") CC: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 10 +++------- arch/x86/kvm/svm/svm.c | 11 ++--------- arch/x86/kvm/svm/svm.h | 6 +++--- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 280d0fccd1971..d734cd5eef5e7 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1191,12 +1191,11 @@ static int nested_svm_vmexit_update_vmcb12(struct k= vm_vcpu *vcpu) return 0; } =20 -int nested_svm_vmexit(struct vcpu_svm *svm) +void nested_svm_vmexit(struct vcpu_svm *svm) { struct kvm_vcpu *vcpu =3D &svm->vcpu; struct vmcb *vmcb01 =3D svm->vmcb01.ptr; struct vmcb *vmcb02 =3D svm->nested.vmcb02.ptr; - int rc; =20 if (nested_svm_vmexit_update_vmcb12(vcpu)) kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); @@ -1315,9 +1314,8 @@ int nested_svm_vmexit(struct vcpu_svm *svm) =20 nested_svm_uninit_mmu_context(vcpu); =20 - rc =3D nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true); - if (rc) - return 1; + if (nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true)) + kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); =20 /* * Drop what we picked up for L2 via svm_complete_interrupts() so it @@ -1342,8 +1340,6 @@ int nested_svm_vmexit(struct vcpu_svm *svm) */ if (kvm_apicv_activated(vcpu->kvm)) __kvm_vcpu_update_apicv(vcpu); - - return 0; } =20 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index cb53174583a26..1b31b033d79b0 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2234,13 +2234,9 @@ static int emulate_svm_instr(struct kvm_vcpu *vcpu, = int opcode) [SVM_INSTR_VMSAVE] =3D vmsave_interception, }; struct vcpu_svm *svm =3D to_svm(vcpu); - int ret; =20 if (is_guest_mode(vcpu)) { - /* Returns '1' or -errno on failure, '0' on success. */ - ret =3D nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]); - if (ret) - return ret; + nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]); return 1; } return svm_instr_handlers[opcode](vcpu); @@ -4796,7 +4792,6 @@ static int svm_enter_smm(struct kvm_vcpu *vcpu, union= kvm_smram *smram) { struct vcpu_svm *svm =3D to_svm(vcpu); struct kvm_host_map map_save; - int ret; =20 if (!is_guest_mode(vcpu)) return 0; @@ -4816,9 +4811,7 @@ static int svm_enter_smm(struct kvm_vcpu *vcpu, union= kvm_smram *smram) svm->vmcb->save.rsp =3D vcpu->arch.regs[VCPU_REGS_RSP]; svm->vmcb->save.rip =3D vcpu->arch.regs[VCPU_REGS_RIP]; =20 - ret =3D nested_svm_simple_vmexit(svm, SVM_EXIT_SW); - if (ret) - return ret; + nested_svm_simple_vmexit(svm, SVM_EXIT_SW); =20 /* * KVM uses VMCB01 to store L1 host state while L2 runs but diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 44d767cd1d25a..7629cb37c9302 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -793,14 +793,14 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu); void svm_copy_vmrun_state(struct vmcb_save_area *to_save, struct vmcb_save_area *from_save); void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmc= b); -int nested_svm_vmexit(struct vcpu_svm *svm); +void nested_svm_vmexit(struct vcpu_svm *svm); =20 -static inline int nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_= code) +static inline void nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit= _code) { svm->vmcb->control.exit_code =3D exit_code; svm->vmcb->control.exit_info_1 =3D 0; svm->vmcb->control.exit_info_2 =3D 0; - return nested_svm_vmexit(svm); + nested_svm_vmexit(svm); } =20 int nested_svm_exit_handled(struct vcpu_svm *svm); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AE7FA362132; Tue, 24 Feb 2026 22:34:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972465; cv=none; b=AQjT0tbIesCjlquZiPOGFwT4rjMres7U8+rWIR0ixGL+d3bz9F5YkT5D4gHvmrN1z7v2GCwH9V/yscnhxfr2i3bqg0aFCSNgq3m39NZriSaegV0P7ZOhctAPurvXLXELOHnQpbZae8SrK0CstpJSO/VpP/BTa46DnbEvnVgRj5A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972465; c=relaxed/simple; bh=drX3y5hy6AEbca6xRJ5i+n/gyJBJwMNXexAazCTSk+s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r3rNvHaDswCI057x/YrF6twhmNsgv6uUfQqnf+gsDcGQ2VVbSFQqlS/fiVB2AxLphXxq6zGkxGHy/ajCQr5j3amrm3ecHIxPWJ8Tc+qOkJzxgHuaEfqQZUGaO8Cun5G5SVStMFxLwSGk1e3yKaB5NVSrlAvv+1fn4xYg9NZhSP0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EZ1zeMy1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EZ1zeMy1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D869C2BC86; Tue, 24 Feb 2026 22:34:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972465; bh=drX3y5hy6AEbca6xRJ5i+n/gyJBJwMNXexAazCTSk+s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EZ1zeMy1LcZoBbafHoY8s6tvitjkiTbEXOYyEDoyXg4/u1bO8QKrTa/l0kL9ZNSkL Q6jW6U4r/F+aUNbbJX9GZdMHJS6Hj//Og7mZHYva/HxNk/OIBBI1Exq16ZZWDEIm+K qERSa6NFe5eIvz4pAZx8l9IM811Bml0/C31Cd0AX6x/q7cE8Cbx0m81ekbDj/0PlWX dbo6h1x00RF01EQqEwFvR/cYTlz1p+EeXA/pc2+ZEb5UiZdTcoXGGKhOgVE54W4jJL jH8/wsKh7NbJK6fILQqmtLqj+MxlV4QFZvmq5/l0/nb6Fbn72hXUOSt3KIlxewgVsf kAfZ+8+n6bZdA== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 10/31] KVM: nSVM: Drop nested_vmcb_check_{save/control}() wrappers Date: Tue, 24 Feb 2026 22:33:44 +0000 Message-ID: <20260224223405.3270433-11-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The wrappers provide little value and make it harder to see what KVM is checking in the normal flow. Drop them. Opportunistically fixup comments referring to the functions, adding '()' to make it clear it's a reference to a function. No functional change intended. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index d734cd5eef5e7..0592690e75164 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -339,8 +339,8 @@ static bool nested_svm_check_bitmap_pa(struct kvm_vcpu = *vcpu, u64 pa, u32 size) kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1); } =20 -static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu, - struct vmcb_ctrl_area_cached *control) +static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu, + struct vmcb_ctrl_area_cached *control) { if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN))) return false; @@ -367,8 +367,8 @@ static bool __nested_vmcb_check_controls(struct kvm_vcp= u *vcpu, } =20 /* Common checks that apply to both L1 and L2 state. */ -static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu, - struct vmcb_save_area_cached *save) +static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu, + struct vmcb_save_area_cached *save) { if (CC(!(save->efer & EFER_SVME))) return false; @@ -402,22 +402,6 @@ static bool __nested_vmcb_check_save(struct kvm_vcpu *= vcpu, return true; } =20 -static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu) -{ - struct vcpu_svm *svm =3D to_svm(vcpu); - struct vmcb_save_area_cached *save =3D &svm->nested.save; - - return __nested_vmcb_check_save(vcpu, save); -} - -static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu) -{ - struct vcpu_svm *svm =3D to_svm(vcpu); - struct vmcb_ctrl_area_cached *ctl =3D &svm->nested.ctl; - - return __nested_vmcb_check_controls(vcpu, ctl); -} - /* * If a feature is not advertised to L1, clear the corresponding vmcb12 * intercept. @@ -469,7 +453,7 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcp= u *vcpu, to->pause_filter_count =3D from->pause_filter_count; to->pause_filter_thresh =3D from->pause_filter_thresh; =20 - /* Copy asid here because nested_vmcb_check_controls will check it. */ + /* Copy asid here because nested_vmcb_check_controls() will check it */ to->asid =3D from->asid; to->msrpm_base_pa &=3D ~0x0fffULL; to->iopm_base_pa &=3D ~0x0fffULL; @@ -1031,8 +1015,8 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) nested_copy_vmcb_control_to_cache(svm, &vmcb12->control); nested_copy_vmcb_save_to_cache(svm, &vmcb12->save); =20 - if (!nested_vmcb_check_save(vcpu) || - !nested_vmcb_check_controls(vcpu)) { + if (!nested_vmcb_check_save(vcpu, &svm->nested.save) || + !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) { vmcb12->control.exit_code =3D SVM_EXIT_ERR; vmcb12->control.exit_info_1 =3D 0; vmcb12->control.exit_info_2 =3D 0; @@ -1871,12 +1855,12 @@ static int svm_set_nested_state(struct kvm_vcpu *vc= pu, =20 ret =3D -EINVAL; __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl); - if (!__nested_vmcb_check_controls(vcpu, &ctl_cached)) + if (!nested_vmcb_check_controls(vcpu, &ctl_cached)) goto out_free; =20 /* * Processor state contains L2 state. Check that it is - * valid for guest mode (see nested_vmcb_check_save). + * valid for guest mode (see nested_vmcb_check_save()). */ cr0 =3D kvm_read_cr0(vcpu); if (((cr0 & X86_CR0_CD) =3D=3D 0) && (cr0 & X86_CR0_NW)) @@ -1890,7 +1874,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, if (!(save->cr0 & X86_CR0_PG) || !(save->cr0 & X86_CR0_PE) || (save->rflags & X86_EFLAGS_VM) || - !__nested_vmcb_check_save(vcpu, &save_cached)) + !nested_vmcb_check_save(vcpu, &save_cached)) goto out_free; =20 =20 --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 02F85362157; Tue, 24 Feb 2026 22:34:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972466; cv=none; b=n5DEY73Dd5m+ofK0/JXzS4fSadqC+fA/Y/Zn+pcRymfFJZVvzavUTEIaRjAn07LQUqLcy8x+iXz26EoYMEOs0JoGcIa7R3bC8fOB6RTQFy1ajXYl7uC/FPVFC6q0nfG4Sk1yZec7aPDWNa6iqN4t1c2UkTO9EwizOtTJQhNJ8U0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972466; c=relaxed/simple; bh=Tg11fWMN6z8tkZRwo0otGiUR8cVHWqjvqfkJjiotBuQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VogpDjofZKv7e8bk9dYplMGEDKGZmyWwxLFLBJozDCno4VriwEtl+QLCV9lVI6J7AVShhJ57bH6++npSqOZ09pqlQzNrSX1gEio1oXiMwwIXwS5dfz2ESb0OUh96dMT6g77MMkguf3fcCe2fLHqYWTM4pSWpeaFQ4hV5/l0CklI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DDr9rb51; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DDr9rb51" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F594C19424; Tue, 24 Feb 2026 22:34:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972465; bh=Tg11fWMN6z8tkZRwo0otGiUR8cVHWqjvqfkJjiotBuQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DDr9rb51kWMAIp6sOPf28/8PJ4GJbGpl8EjZ7oVuZdfS9YMmk/FEhVatdIktZDmLR lnGLwKa00ku5FDNtqr9FCT6JFuFOpMOwPkg/oj//f+QNSFLVYG1CHMKpZBC0eyRYCx sgJhQzXeWkJVeVagnbwkOWNaAfSf3xyb0FtnGFON8a64tmutzCy1f7A/gMIvNq4qV3 I0EB4202KfPNlkHxozWfX2dUp4m7TIcJpKGHj4B1FOFO2ErpuEbKXX1oz2fdq3Zhhi qJKj9Q7PeRFThHDSRGccu+QsInFzxarUXCcU3mQvwx+xZJTZVyPkRy57RL/t22B3Qt EP7utbARwp+Aw== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 11/31] KVM: nSVM: Call enter_guest_mode() before switching to VMCB02 Date: Tue, 24 Feb 2026 22:33:45 +0000 Message-ID: <20260224223405.3270433-12-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for moving more changes that rely on is_guest_mode() before switching to VMCB02, move entering guest mode a bit earlier. Nothing between the new callsite(s) and the old ones rely on is_guest_mode(), so this should be safe. No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 0592690e75164..5ba3f8de0a939 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -746,9 +746,6 @@ static void nested_vmcb02_prepare_control(struct vcpu_s= vm *svm, =20 nested_svm_transition_tlb_flush(vcpu); =20 - /* Enter Guest-Mode */ - enter_guest_mode(vcpu); - /* * Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info, * exit_int_info_err, next_rip, insn_len, insn_bytes. @@ -949,6 +946,8 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmc= b12_gpa, =20 WARN_ON(svm->vmcb =3D=3D svm->nested.vmcb02.ptr); =20 + enter_guest_mode(vcpu); + nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr); =20 svm_switch_vmcb(svm, &svm->nested.vmcb02); @@ -1900,6 +1899,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save); nested_copy_vmcb_control_to_cache(svm, ctl); =20 + enter_guest_mode(vcpu); svm_switch_vmcb(svm, &svm->nested.vmcb02); nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.c= s.base); =20 --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 653093624C0; Tue, 24 Feb 2026 22:34:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972466; cv=none; b=X8xkbczVlwqypDOhOEPZAdm2c3oRN0uYjAi9O3Dvpsvbc5Ynt5rum0qea1lpj4vGKMbQGJzeJFJncQDAeI7HtetlqzVvwt/4PsRm4C77pTpUT1a9SmNc6RIfrnl+EHE1/yjIYVpsv6Zt9bsg6kQu7l5bRizmgXiWVemSmSlJvc0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972466; c=relaxed/simple; bh=IFCEWEs+v5uNIrZaMJ5+GDYk34dfb0i36jV1dJ/PfK0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RqPKXiuJCpmWY97n8u2STOSM1fF1E7ZIWfsFCuE+TBE0xWvjSSqWyaZa0LDQ0kNhM8f8bvf6Ig6Mmga7baLrqf2KNt/dU33bVUcOKBEflReDmczMC6Og20uZAUyPA1/9ZoOT6tA9RUuK44xgYdjuxfxVPm28aso9DlSJc8WTR98= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fV/mr7mn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fV/mr7mn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0DB1FC19423; Tue, 24 Feb 2026 22:34:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972466; bh=IFCEWEs+v5uNIrZaMJ5+GDYk34dfb0i36jV1dJ/PfK0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fV/mr7mn6BdXIw12qag8AVS2NVj+YhR/SYvyI6hdj2KZQ6QjMyOjcJ4GuR39W1+kg 5TmrbAnpS7ueoKCmq9K+QnHuXZqL1ph68PIjZP8H6z0hBj9kaR8nyx37LGAFyevZxF /6mifr5xlqOU3amNp//JpFJYClbP2lKI9j3HxMTdHuIHUGnxetes5OsewHaNeMpOtO pBBvotfJJPkBoSGIeA3pKbyPobzv2oG/8+P/FfB1MLtnhizmIccaqtD9dMbk0CT7FZ sVvXC90oHB4TiyuYz3fLSJHTgjeSYBEMTRyiElbv9wqeCd5XkzLxBD9S2nxPHfa8pI ki1ZmRGH/U+6g== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 12/31] KVM: nSVM: Make nested_svm_merge_msrpm() return an errno Date: Tue, 24 Feb 2026 22:33:46 +0000 Message-ID: <20260224223405.3270433-13-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for moving nested_svm_merge_msrpm() within enter_svm_guest_mode(), which returns an errno, return an errno from nested_svm_merge_msrpm(). No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 5ba3f8de0a939..78caa75fe619a 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -276,7 +276,7 @@ int __init nested_svm_init_msrpm_merge_offsets(void) * is optimized in that it only merges the parts where KVM MSR permission = bitmap * may contain zero bits. */ -static bool nested_svm_merge_msrpm(struct kvm_vcpu *vcpu) +static int nested_svm_merge_msrpm(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); nsvm_msrpm_merge_t *msrpm02 =3D svm->nested.msrpm; @@ -303,17 +303,19 @@ static bool nested_svm_merge_msrpm(struct kvm_vcpu *v= cpu) #endif =20 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT))) - return true; + return 0; =20 for (i =3D 0; i < nested_svm_nr_msrpm_merge_offsets; i++) { const int p =3D nested_svm_msrpm_merge_offsets[i]; nsvm_msrpm_merge_t l1_val; gpa_t gpa; + int r; =20 gpa =3D svm->nested.ctl.msrpm_base_pa + (p * sizeof(l1_val)); =20 - if (kvm_vcpu_read_guest(vcpu, gpa, &l1_val, sizeof(l1_val))) - return false; + r =3D kvm_vcpu_read_guest(vcpu, gpa, &l1_val, sizeof(l1_val)); + if (r) + return r; =20 msrpm02[p] =3D msrpm01[p] | l1_val; } @@ -325,7 +327,7 @@ static bool nested_svm_merge_msrpm(struct kvm_vcpu *vcp= u) #endif svm->vmcb->control.msrpm_base_pa =3D __sme_set(__pa(svm->nested.msrpm)); =20 - return true; + return 0; } =20 /* @@ -1040,7 +1042,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true)) goto out_exit_err; =20 - if (nested_svm_merge_msrpm(vcpu)) + if (!nested_svm_merge_msrpm(vcpu)) goto out; =20 out_exit_err: @@ -1940,7 +1942,7 @@ static bool svm_get_nested_state_pages(struct kvm_vcp= u *vcpu) if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3))) return false; =20 - if (!nested_svm_merge_msrpm(vcpu)) { + if (nested_svm_merge_msrpm(vcpu)) { vcpu->run->exit_reason =3D KVM_EXIT_INTERNAL_ERROR; vcpu->run->internal.suberror =3D KVM_INTERNAL_ERROR_EMULATION; --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E6139363C59; Tue, 24 Feb 2026 22:34:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972467; cv=none; b=Oqj0oXHL/xW3DvyDXjDQntRoM5dkkxzoXcWQ36DXNMRQ4X2S50ua4gjjocklwRLzAB/BfEL5xSxy6VLqoS/YHqV39N3xsDTXy1lNqKn9xFchEI8DLu760FxYT2cDCeRqUAq+B7LbzuD3momdB60KkkAC7/bAZqUfnGxWL/EG8xg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972467; c=relaxed/simple; bh=v3k1XuVOPyc0D6iXWZn2WZUJVDAowvvqRBuCr8T4O2o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M2UOKk5Zao2zB1Zx9dSN5gaF54U4TRZcGQE5jaLPdcY89cIK756Jwc8j6a8ZOhUfeodf8foBnYVcEcyDUv1Yj9DoOwlpxpEjLF+Or4uxUDUV4pRLFuZEuoX1nlOPqvOLMxnl6e85+3+SJZ25gb7Ohp77XNIb2mdf6NmQqvt9H+k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=P1Usk6gg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="P1Usk6gg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FC16C2BC86; Tue, 24 Feb 2026 22:34:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972466; bh=v3k1XuVOPyc0D6iXWZn2WZUJVDAowvvqRBuCr8T4O2o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P1Usk6gg8O30iusrKx3oyvCjQZemZU0lSL8Updf3YF0fq0rtl4iJjzPKiTN9rbwp7 V6twI3Fho2UGRAkcqnGh/dqWNbstwBdGbPVIHezX3VNGSAyPPo/QNRL4mAZmDHBEIW bu3KUpQbzhMMbFkKMvXjWSb+myxHmAJlfDsrrqysPIAT5q+asl16xha73UdWcLMrDI Cjs4i3S9TzIV0d3eEYdlVxqN1SOeI9yCHs1tQl9JAXNChuyn6zE1umUAUcnYjzJqox /wyf+5Lv/LliWBmu2lm/JsaY28CGkKqOAG9rMBv7kni2fbQdoHChzcJ4RsIs989LT9 IFsl6T35h0MjQ== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 13/31] KVM: nSVM: Call nested_svm_merge_msrpm() from enter_svm_guest_mode() Date: Tue, 24 Feb 2026 22:33:47 +0000 Message-ID: <20260224223405.3270433-14-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for unifying the VMRUN failure code paths, move calling nested_svm_merge_msrpm() into enter_svm_guest_mode() next to the nested_svm_load_cr3() call (the other failure path in enter_svm_guest_mode()). Adding more uses of the from_vmrun parameter is not pretty, but it is plumbed all the way to nested_svm_load_cr3() so it's not going away soon anyway. No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 78caa75fe619a..5276e41605d43 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -961,6 +961,12 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vm= cb12_gpa, if (ret) return ret; =20 + if (from_vmrun) { + ret =3D nested_svm_merge_msrpm(vcpu); + if (ret) + return ret; + } + if (!from_vmrun) kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); =20 @@ -1039,22 +1045,17 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) =20 svm->nested.nested_run_pending =3D 1; =20 - if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true)) - goto out_exit_err; - - if (!nested_svm_merge_msrpm(vcpu)) - goto out; - -out_exit_err: - svm->nested.nested_run_pending =3D 0; - svm->nmi_l1_to_l2 =3D false; - svm->soft_int_injected =3D false; + if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true)) { + svm->nested.nested_run_pending =3D 0; + svm->nmi_l1_to_l2 =3D false; + svm->soft_int_injected =3D false; =20 - svm->vmcb->control.exit_code =3D SVM_EXIT_ERR; - svm->vmcb->control.exit_info_1 =3D 0; - svm->vmcb->control.exit_info_2 =3D 0; + svm->vmcb->control.exit_code =3D SVM_EXIT_ERR; + svm->vmcb->control.exit_info_1 =3D 0; + svm->vmcb->control.exit_info_2 =3D 0; =20 - nested_svm_vmexit(svm); + nested_svm_vmexit(svm); + } =20 out: kvm_vcpu_unmap(vcpu, &map); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6950F364020; Tue, 24 Feb 2026 22:34:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972467; cv=none; b=VeeKFchDF38eazJBgSQv6WQQSESls1a5aeLxEsRwg9Rwcv4oe00NQUGbyXJfCtd5lpgtgZVicM8XzS4/nXDO+bUaeDZ2if2WPcsNBici7W9h8ZkAoGtvDYHamZzOR44fJxmX+62fMBXi2lV3Pa/4ZyZBC3UbeU0kRvoyrNWGMaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972467; c=relaxed/simple; bh=DV+TTPrOLl4mBHZuzEMhsKt2bk9xjJWqiVi/tNeYK8Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ErssH2zccjiqq5gTAvZLASxSXGC9cpCsli4GNwefrveKnxizQESGOxWa3zxWZpef3PWDQdrGOXwQeUsV7wqF8B1RMpI6WkAFxjBLnEQteqhgBNFKSbJHuP4nlcN4rSFNeWtZbvBXtoekFeuiF81+1BUmLdRLFgAZdvOd9KqSawY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mVh/QiC2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mVh/QiC2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1A7EC19423; Tue, 24 Feb 2026 22:34:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972467; bh=DV+TTPrOLl4mBHZuzEMhsKt2bk9xjJWqiVi/tNeYK8Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mVh/QiC2QRNwcGeWiGaw8/nHCoKNufaxsp2IQi9ba2/wXWwwTKkgwSFemkpiAhd5n TrHMvA+fFLHfz2NU8NbddWaNC/fB8+Z4NLxJKUG+HkJwgih0W2MyqKwdbKeuS5OROv EU3Q1IYhr9GilhVfUDTKKMsDRlPZWdNhubGvS9zIH0KUJO+0LNGSanuj3n84zZpGEo VYxwaWbbcBAzjw4vxXgFosLLnqnD0YXvz1QO/gjjacFQZhAWh1lkkF9m7rUAOB/NLU twQMTasEH577aeZC5Br2u6kAra3olyqCjHQ4Yt/Fqzd3TxhOJ4OY1MJT/ho3R4ldlQ w2rXEJIBHO8sQ== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 14/31] KVM: nSVM: Call nested_svm_init_mmu_context() before switching to VMCB02 Date: Tue, 24 Feb 2026 22:33:48 +0000 Message-ID: <20260224223405.3270433-15-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for moving more code that depends on nested_svm_init_mmu_context() before switching to VMCB02, move the call outside of nested_vmcb02_prepare_control() into callers, a bit earlier. nested_svm_init_mmu_context() needs to be called after enter_guest_mode(), but not after switching to VMCB02. No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 5276e41605d43..13d1276940330 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -808,10 +808,6 @@ static void nested_vmcb02_prepare_control(struct vcpu_= svm *svm, /* Also overwritten later if necessary. */ vmcb02->control.tlb_ctl =3D TLB_CONTROL_DO_NOTHING; =20 - /* nested_cr3. */ - if (nested_npt_enabled(svm)) - nested_svm_init_mmu_context(vcpu); - vcpu->arch.tsc_offset =3D kvm_calc_nested_tsc_offset( vcpu->arch.l1_tsc_offset, svm->nested.ctl.tsc_offset, @@ -950,6 +946,9 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmc= b12_gpa, =20 enter_guest_mode(vcpu); =20 + if (nested_npt_enabled(svm)) + nested_svm_init_mmu_context(vcpu); + nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr); =20 svm_switch_vmcb(svm, &svm->nested.vmcb02); @@ -1903,6 +1902,10 @@ static int svm_set_nested_state(struct kvm_vcpu *vcp= u, nested_copy_vmcb_control_to_cache(svm, ctl); =20 enter_guest_mode(vcpu); + + if (nested_npt_enabled(svm)) + nested_svm_init_mmu_context(vcpu); + svm_switch_vmcb(svm, &svm->nested.vmcb02); nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.c= s.base); =20 --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 B18EF364055; Tue, 24 Feb 2026 22:34:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972467; cv=none; b=tEPh/iEMHdjVBTt072fVoK0W/kag2CU4/nhkXwQrv/AVSlkBfM/tCZg9Kxwrbr8RlrKEVImJ8rX42O9DGg64CJu28KkBUdYf+g/MnLak4RyAIdWsluRSHyjUNig9SJ6hKx4xKdprOPkco9Z3oWZu4uefz1M1r/sBAvuVjeVnIIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972467; c=relaxed/simple; bh=o/TTK+emy1Ox83p7KmqZovyUmvrbAaSzPmRDbdTnp2E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SA7BDGClVJgurNc5wTAE6mF+sXIscCE710Kz8lNYI8kxlWTy+nfWF0YuFVAxag+/rjEoE26i/4uIEtL2B9EZfRtG/cqpkGUUlG8h0wu3FZKboPeZQB5Ukx8j7i/mbQlKueOeZ0r2cGgWGtSg9puWLX0aw1T03W2KHhyOXgHXJr8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DawznD1v; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DawznD1v" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 400A1C19424; Tue, 24 Feb 2026 22:34:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972467; bh=o/TTK+emy1Ox83p7KmqZovyUmvrbAaSzPmRDbdTnp2E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DawznD1vy7DDMbtllzrFMkP3Ex4oFSEWM0+iMdrDkcyo5hHZg/Z+zlcxbBdSNxM3p 8TPqdqvibXZcNwegJJEINUGQx9p76SPXm9g4yirevW9eIT5IRvYiW5OD0moAmYz29h 98crEt64+/wpZzdffDQDXMP6ud/dwcVDWxfDdLhds11O3Rqz7/sbNqDcxvrf2wsEhs B6hKZ9L4ucvnuauhJimDb/C1h1IeEMric6ltV4TgLHfJO8N5XpFfRcyg33GDyASRu8 oCLDrp9OblmliOKXXQ7ASIEeUUhfzaR4AjeJ1jbP1HOrdVw0HDoOVxQhdSTt2dH303 yE+KrZDU2/YuQ== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 15/31] KVM: nSVM: Refactor minimal #VMEXIT handling out of nested_svm_vmexit() Date: Tue, 24 Feb 2026 22:33:49 +0000 Message-ID: <20260224223405.3270433-16-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for having a separate minimal #VMEXIT path for handling failed VMRUNs, move the minimal logic out of nested_svm_vmexit() into a helper. This includes clearing the GIF, handling single-stepping on VMRUN, and a few data structure cleanups. Basically, everything that is required by the architecture (or KVM) on a #VMEXIT where L2 never actually ran. Additionally move uninitializing the nested MMU and reloading host CR3 to the new helper. It is not required at this point, but following changes will require it. No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 54 ++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 13d1276940330..a6e8c0d26c64e 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -979,6 +979,33 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vm= cb12_gpa, return 0; } =20 +static void __nested_svm_vmexit(struct vcpu_svm *svm) +{ + struct vmcb *vmcb01 =3D svm->vmcb01.ptr; + struct kvm_vcpu *vcpu =3D &svm->vcpu; + + svm->nested.vmcb12_gpa =3D 0; + svm->nested.ctl.nested_cr3 =3D 0; + + /* GIF is cleared on #VMEXIT, no event can be injected in L1 */ + svm_set_gif(svm, false); + vmcb01->control.exit_int_info =3D 0; + + nested_svm_uninit_mmu_context(vcpu); + + if (nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true)) + kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); + + /* + * If we are here following the completion of a VMRUN that + * is being single-stepped, queue the pending #DB intercept + * right now so that it an be accounted for before we execute + * L1's next instruction. + */ + if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF)) + kvm_queue_exception(vcpu, DB_VECTOR); +} + int nested_svm_vmrun(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); @@ -1187,7 +1214,6 @@ void nested_svm_vmexit(struct vcpu_svm *svm) =20 /* Exit Guest-Mode */ leave_guest_mode(vcpu); - svm->nested.vmcb12_gpa =3D 0; WARN_ON_ONCE(svm->nested.nested_run_pending); =20 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); @@ -1260,13 +1286,6 @@ void nested_svm_vmexit(struct vcpu_svm *svm) } } =20 - /* - * On vmexit the GIF is set to false and - * no event can be injected in L1. - */ - svm_set_gif(svm, false); - vmcb01->control.exit_int_info =3D 0; - svm->vcpu.arch.tsc_offset =3D svm->vcpu.arch.l1_tsc_offset; if (vmcb01->control.tsc_offset !=3D svm->vcpu.arch.tsc_offset) { vmcb01->control.tsc_offset =3D svm->vcpu.arch.tsc_offset; @@ -1279,8 +1298,6 @@ void nested_svm_vmexit(struct vcpu_svm *svm) svm_write_tsc_multiplier(vcpu); } =20 - svm->nested.ctl.nested_cr3 =3D 0; - /* * Restore processor state that had been saved in vmcb01 */ @@ -1297,11 +1314,6 @@ void nested_svm_vmexit(struct vcpu_svm *svm) =20 nested_svm_transition_tlb_flush(vcpu); =20 - nested_svm_uninit_mmu_context(vcpu); - - if (nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true)) - kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); - /* * Drop what we picked up for L2 via svm_complete_interrupts() so it * doesn't end up in L1. @@ -1310,21 +1322,15 @@ void nested_svm_vmexit(struct vcpu_svm *svm) kvm_clear_exception_queue(vcpu); kvm_clear_interrupt_queue(vcpu); =20 - /* - * If we are here following the completion of a VMRUN that - * is being single-stepped, queue the pending #DB intercept - * right now so that it an be accounted for before we execute - * L1's next instruction. - */ - if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF)) - kvm_queue_exception(&(svm->vcpu), DB_VECTOR); - /* * Un-inhibit the AVIC right away, so that other vCPUs can start * to benefit from it right away. */ if (kvm_apicv_activated(vcpu->kvm)) __kvm_vcpu_update_apicv(vcpu); + + /* After clearing exceptions as it may need to queue an exception */ + __nested_svm_vmexit(svm); } =20 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu) --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 11A14364EBA; Tue, 24 Feb 2026 22:34:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972468; cv=none; b=om0ZB90YsJEinpMUBdsMf/a6Dx0osfA1BUsdlZ11r2OiH/0T9bUwGaRahQv/BqcxXuZV+LCedEtgzmUU87hMdK8PWXBrRO5AerUntHQYlGB8MuqjryMLrwNwZetXlLuEDgLjNTPjJjQfM1Z5FGuvssmsrzWvjZK1c7XImSy5/2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972468; c=relaxed/simple; bh=/00Jh19cVwrJ78qvAoBT86Jj00pLMsKnQxm14ypB3gQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k6bAVeU/B/ZoDqJjIt1rL8m1Y0MC8LUYcKP3N8XAw+9VCYtnI9H8H1byWpMXZGGQaLSbnt8sSYClznRxLBlHeLdPJwpRhSCGHDx/5K6LANmVvLymKPX8EFjHv/7hEGzL413jUuIH9esaAAnvNUL9tvrvgzxcTUnonkl3hNAL0fM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pjWsy2Cx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pjWsy2Cx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2A94C19423; Tue, 24 Feb 2026 22:34:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972467; bh=/00Jh19cVwrJ78qvAoBT86Jj00pLMsKnQxm14ypB3gQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pjWsy2Cx0pC+N6bmpiokhtQfYlfnjXS02u1bITWqgN8swGgNg3XRW28c2UTwGmdeU AFp2cfq+fcYWZIVeSeJyC2SqMzM1qVxq5YxAAuNvDmcq7pghJontty53cx2jsJkUpI xrdHfWKO3r5C73zQ4Zk0M30ImMIf+SkbWJhJQjNyY/tyc+6Q5WYLJzuhgOZn2ALXWE FU6J4GQgAOvnBC3IeMSPIKF/KMMHnCsgDJsu0YAyCulLAYlYeUnTnu50DzxKP4+caK D7Qx75uL2hmfJkL3qKxj4g9yLMxOXOp9c2x1DEsdhqhvYSTHmSQG9K+ZtwAsYwEJkO sjsW+TWXac0tw== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 16/31] KVM: nSVM: Unify handling of VMRUN failures with proper cleanup Date: Tue, 24 Feb 2026 22:33:50 +0000 Message-ID: <20260224223405.3270433-17-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There are currently two possible causes of VMRUN failures emulated by KVM: 1) Consistency checks failures. In this case, KVM updates the exit code in the mapped VMCB12 and exits early in nested_svm_vmrun(). This causes a few problems: A) KVM does not clear the GIF if the early consistency checks fail (because nested_svm_vmexit() is not called). Nothing requires GIF=3D0 before a VMRUN, from the APM: It is assumed that VMM software cleared GIF some time before executing the VMRUN instruction, to ensure an atomic state switch. So an early #VMEXIT from early consistency checks could leave the GIF set. B) svm_leave_smm() is missing consistency checks on the newly loaded guest state, because the checks aren't performed by enter_svm_guest_mode(). 2) Failure to load L2's CR3 or merge the MSR bitmaps. In this case, a fully-fledged #VMEXIT injection is performed as VMCB02 is already prepared. Arguably all VMRUN failures should be handled before the VMCB02 is prepared, but with proper cleanup (e.g. clear the GIF). Move all the potential failure checks inside enter_svm_guest_mode() before switching to VMCB02. On failure of any of these checks, nested_svm_vmrun() synthesizes a minimal #VMEXIT through the new nested_svm_failed_vmrun() helper. __nested_svm_vmexit() already performs the necessary cleanup for a failed VMRUN, including uninitializing the nested MMU and reloading L1's CR3. This ensures that consistency check failures do proper necessary cleanup, while other failures do not doo too much cleanup. It also leaves a unified path for handling VMRUN failures. Cc: stable@vger.kernel.org Fixes: 52c65a30a5c6 ("KVM: SVM: Check for nested vmrun intercept before emu= lating vmrun") Suggested-by: Sean Christopherson Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 66 +++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index a6e8c0d26c64e..2c5878cbb3940 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -939,22 +939,19 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 v= mcb12_gpa, vmcb12->control.intercepts[INTERCEPT_WORD4], vmcb12->control.intercepts[INTERCEPT_WORD5]); =20 - svm->nested.vmcb12_gpa =3D vmcb12_gpa; =20 WARN_ON(svm->vmcb =3D=3D svm->nested.vmcb02.ptr); =20 enter_guest_mode(vcpu); =20 + if (!nested_vmcb_check_save(vcpu, &svm->nested.save) || + !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) + return -EINVAL; + if (nested_npt_enabled(svm)) nested_svm_init_mmu_context(vcpu); =20 - nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr); - - svm_switch_vmcb(svm, &svm->nested.vmcb02); - nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base= ); - nested_vmcb02_prepare_save(svm, vmcb12); - ret =3D nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3, nested_npt_enabled(svm), from_vmrun); if (ret) @@ -966,6 +963,17 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vm= cb12_gpa, return ret; } =20 + /* + * Any VMRUN failure needs to happen before this point, such that the + * nested #VMEXIT is injected properly by nested_svm_vmrun_error_vmexit(). + */ + + nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr); + + svm_switch_vmcb(svm, &svm->nested.vmcb02); + nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base= ); + nested_vmcb02_prepare_save(svm, vmcb12); + if (!from_vmrun) kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); =20 @@ -984,6 +992,8 @@ static void __nested_svm_vmexit(struct vcpu_svm *svm) struct vmcb *vmcb01 =3D svm->vmcb01.ptr; struct kvm_vcpu *vcpu =3D &svm->vcpu; =20 + WARN_ON_ONCE(is_guest_mode(vcpu)); + svm->nested.vmcb12_gpa =3D 0; svm->nested.ctl.nested_cr3 =3D 0; =20 @@ -1006,6 +1016,20 @@ static void __nested_svm_vmexit(struct vcpu_svm *svm) kvm_queue_exception(vcpu, DB_VECTOR); } =20 +static void nested_svm_vmrun_error_vmexit(struct kvm_vcpu *vcpu, struct vm= cb *vmcb12) +{ + struct vcpu_svm *svm =3D to_svm(vcpu); + + WARN_ON_ONCE(svm->vmcb =3D=3D svm->nested.vmcb02.ptr); + + leave_guest_mode(vcpu); + + vmcb12->control.exit_code =3D SVM_EXIT_ERR; + vmcb12->control.exit_info_1 =3D 0; + vmcb12->control.exit_info_2 =3D 0; + __nested_svm_vmexit(svm); +} + int nested_svm_vmrun(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); @@ -1048,14 +1072,6 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) nested_copy_vmcb_control_to_cache(svm, &vmcb12->control); nested_copy_vmcb_save_to_cache(svm, &vmcb12->save); =20 - if (!nested_vmcb_check_save(vcpu, &svm->nested.save) || - !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) { - vmcb12->control.exit_code =3D SVM_EXIT_ERR; - vmcb12->control.exit_info_1 =3D 0; - vmcb12->control.exit_info_2 =3D 0; - goto out; - } - /* * Since vmcb01 is not in use, we can use it to store some of the L1 * state. @@ -1076,14 +1092,9 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) svm->nmi_l1_to_l2 =3D false; svm->soft_int_injected =3D false; =20 - svm->vmcb->control.exit_code =3D SVM_EXIT_ERR; - svm->vmcb->control.exit_info_1 =3D 0; - svm->vmcb->control.exit_info_2 =3D 0; - - nested_svm_vmexit(svm); + nested_svm_vmrun_error_vmexit(vcpu, vmcb12); } =20 -out: kvm_vcpu_unmap(vcpu, &map); =20 return ret; @@ -1241,6 +1252,13 @@ void nested_svm_vmexit(struct vcpu_svm *svm) if (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS)) vmcb01->control.erap_ctl |=3D ERAP_CONTROL_CLEAR_RAP; =20 + /* + * nested_svm_vmexit() is intended for use only when KVM is synthesizing + * a #VMEXIT after a successful nested VMRUN. All VMRUN consistency + * checks must be performed before loading guest state, and so should + * use __nested_svm_vmexit(). + */ + WARN_ON_ONCE(svm->vmcb !=3D svm->nested.vmcb02.ptr); svm_switch_vmcb(svm, &svm->vmcb01); =20 /* @@ -1912,9 +1930,6 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, if (nested_npt_enabled(svm)) nested_svm_init_mmu_context(vcpu); =20 - svm_switch_vmcb(svm, &svm->nested.vmcb02); - nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.c= s.base); - /* * While the nested guest CR3 is already checked and set by * KVM_SET_SREGS, it was set when nested state was yet loaded, @@ -1926,6 +1941,9 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, if (ret) goto out_free; =20 + svm_switch_vmcb(svm, &svm->nested.vmcb02); + nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.c= s.base); + svm->nested.force_msr_bitmap_recalc =3D true; =20 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6952B366823; Tue, 24 Feb 2026 22:34:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972468; cv=none; b=bV53ibQ1DSKgDxZIUkqMgcN+lAn56wF8nhv7nh3ZNnul8/M/izzxz4THivvh/ila7cWFPe0FZWmqH38n//3nAQAPk0pcWC+9yCO1+t49ManYNtsbJwSj7g0Hunc1RFeTN0Y8sC4SBqZCJsSfoJmAKKkQnq8OY678pEH4Oj+rLeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972468; c=relaxed/simple; bh=/pn0CYze4ws752rwcsq4J0RXY1UHXEv744XlprrqCGw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DugK4vkCs0ttY8q7AcJMkwUfPW138is0ZyCqL7f2742Lo70zmYC2EtIkeVB9yjYkURC+/2tFc1jp++33IuzExqq7SU3lBB7lq+IOZNIqMmjsc5Hcstra07mZ3iI+saaMVkSidqn7b6pFXy/5sJ6IH9L2Gqotv12AMjhm2pjD8FM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gyov+M/b; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gyov+M/b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1069BC19424; Tue, 24 Feb 2026 22:34:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972468; bh=/pn0CYze4ws752rwcsq4J0RXY1UHXEv744XlprrqCGw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gyov+M/bxZb4D7HJdWCVXlxc+MfMsgJTwOyiWTLLW1KGOuH73HC3iDBQEwCvEiiwA qlXPqkRWYoOAFdalsOhB5Xpy5PRJiVQ9sSfgpDvSFN62WyXzRfUZHFJOnmtn8acxSX 5cxngxZ3Kw/5kcDjQF5iFyfaOi0nNWnD4oQ8EUJB2/+5MuLfOpmTZCKFrZyJdP3T8c i2Cil9XTqs8YtWc1dvF1j/T9eQg8EZe2ml58SO2n31YIuBwKezVPYKfGUxi0qmTIj1 m6uyP1F7uQff4iRSNaWFpwOcL8OiPlN2YymU468SjLDXfvFuXprxyTAHEX8fMYnHFl UbbuVFINVHsNg== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 17/31] KVM: nSVM: Clear EVENTINJ field in VMCB12 on nested #VMEXIT Date: Tue, 24 Feb 2026 22:33:51 +0000 Message-ID: <20260224223405.3270433-18-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" According to the APM, from the reference of the VMRUN instruction: Upon #VMEXIT, the processor performs the following actions in order to return to the host execution context: ... clear EVENTINJ field in VMCB KVM correctly cleared EVENTINJ (i.e. event_inj and event_inj_err) on nested #VMEXIT before commit 2d8a42be0e2b ("KVM: nSVM: synchronize VMCB controls updated by the processor on every vmexit"). That commit made sure the fields are synchronized between VMCB02 and KVM's cached VMCB12 on every L2->L0 #VMEXIT, such that they are serialized correctly on save/restore. However, the commit also incorrectly copied the fields from KVM's cached VMCB12 to L1's VMCB12 on nested #VMEXIT. This is mostly correct, as the fields will be zeroed by the CPU, but it doesn't handle a #VMEXIT due to an invalid VMRUN. Explicitly clear the fields all nested #VMEXIT code paths. Fixes: 2d8a42be0e2b ("KVM: nSVM: synchronize VMCB controls updated by the p= rocessor on every vmexit") Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 2c5878cbb3940..dd95c6434403f 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1027,6 +1027,8 @@ static void nested_svm_vmrun_error_vmexit(struct kvm_= vcpu *vcpu, struct vmcb *vm vmcb12->control.exit_code =3D SVM_EXIT_ERR; vmcb12->control.exit_info_1 =3D 0; vmcb12->control.exit_info_2 =3D 0; + vmcb12->control.event_inj =3D 0; + vmcb12->control.event_inj_err =3D 0; __nested_svm_vmexit(svm); } =20 @@ -1199,9 +1201,9 @@ static int nested_svm_vmexit_update_vmcb12(struct kvm= _vcpu *vcpu) if (nested_vmcb12_has_lbrv(vcpu)) svm_copy_lbrs(&vmcb12->save, &vmcb02->save); =20 + vmcb12->control.event_inj =3D 0; + vmcb12->control.event_inj_err =3D 0; vmcb12->control.int_ctl =3D svm->nested.ctl.int_ctl; - vmcb12->control.event_inj =3D svm->nested.ctl.event_inj; - vmcb12->control.event_inj_err =3D svm->nested.ctl.event_inj_err; =20 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code, vmcb12->control.exit_info_1, --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D9E0036C0B7; Tue, 24 Feb 2026 22:34:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972468; cv=none; b=pYOzmdVHpMZJ4oE7KYACXgGgyFPg8m1WYhGw0u8nW0Yo7gNJlk/iSsfYAUnQCqKvoWXsuZojqqvnlNyKp95Jsejsa6/tL/eC+6SBlegYj4N0G0W3L0HS2J2lrYzxVHpRP8UrtGkfR66TpQZcIcTgtRLPZV/uxzsbojyFNHKuBT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972468; c=relaxed/simple; bh=Y8/QwEVqLoWElzGzDO9XBHgtA8uAN6YBWaSR8ZmZZ7w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MqbcDAaiHOHEeX1Rrkcuihx8uXZl0EU8WeurbR8qDRp2tGEM5jRmksix5nfLdqMysD1ZzS1xzeV/4DyrgrXuZkETV6HuXYE3C7iqdPmKx4V6IpL86th8626CyUEJtFVwCT+wACYoznaaFocXaVCHtDY0tr6YopGAEIVbLWvy7no= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RVNu9yAk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RVNu9yAk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 727CDC19423; Tue, 24 Feb 2026 22:34:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972468; bh=Y8/QwEVqLoWElzGzDO9XBHgtA8uAN6YBWaSR8ZmZZ7w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RVNu9yAkbw2EiJUbSaR1U74iWE4sVzwcSUTcjbARiKmyP4v5tB/o4cdt0xzGfZc+X krJtHlrAUYBTDbsAIk1N2xUsY8z43mE8ie4kqqsDh/3OAMqAHAlB6K/IO9jLhVFchK HgQA5qlaMfFEcqeT9AGeN1A4fGh5XIuTMAYOf7K6BEIl8fkEKFK1v+IQLlVffWZuSP 9z7hajfIkA++7+d3uBhEY3FQda7jVCuNd4UYo1KpRvBkG6O6IqH1mdUWMNqg8GMksI O0tqnnPwmeRndMFrPq9eiHclF41CB7peywUg1HVDSPHhMaWqCadcZACd0RuAW0A3Ep ds5QiRwgWaAKQ== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 18/31] KVM: nSVM: Drop the non-architectural consistency check for NP_ENABLE Date: Tue, 24 Feb 2026 22:33:52 +0000 Message-ID: <20260224223405.3270433-19-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" KVM currenty fails a nested VMRUN and injects VMEXIT_INVALID (aka SVM_EXIT_ERR) if L1 sets NP_ENABLE and the host does not support NPTs. On first glance, it seems like the check should actually be for guest_cpu_cap_has(X86_FEATURE_NPT) instead, as it is possible for the host to support NPTs but the guest CPUID to not advertise it. However, the consistency check is not architectural to begin with. The APM does not mention VMEXIT_INVALID if NP_ENABLE is set on a processor that does not have X86_FEATURE_NPT. Hence, NP_ENABLE should be ignored if X86_FEATURE_NPT is not available for L1, so sanitize it when copying from the VMCB12 to KVM's cache. Apart from the consistency check, NP_ENABLE in VMCB12 is currently ignored because the bit is actually copied from VMCB01 to VMCB02, not from VMCB12. Fixes: 4b16184c1cca ("KVM: SVM: Initialize Nested Nested MMU context on VMR= UN") Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index dd95c6434403f..0f18123f5461f 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -350,9 +350,6 @@ static bool nested_vmcb_check_controls(struct kvm_vcpu = *vcpu, if (CC(control->asid =3D=3D 0)) return false; =20 - if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled)) - return false; - if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa, MSRPM_SIZE))) return false; @@ -433,6 +430,11 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vc= pu *vcpu, nested_svm_sanitize_intercept(vcpu, to, SKINIT); nested_svm_sanitize_intercept(vcpu, to, RDPRU); =20 + /* Always clear SVM_NESTED_CTL_NP_ENABLE if the guest cannot use NPTs */ + to->nested_ctl =3D from->nested_ctl; + if (!guest_cpu_cap_has(vcpu, X86_FEATURE_NPT)) + to->nested_ctl &=3D ~SVM_NESTED_CTL_NP_ENABLE; + to->iopm_base_pa =3D from->iopm_base_pa; to->msrpm_base_pa =3D from->msrpm_base_pa; to->tsc_offset =3D from->tsc_offset; @@ -446,7 +448,6 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcp= u *vcpu, to->exit_info_2 =3D from->exit_info_2; to->exit_int_info =3D from->exit_int_info; to->exit_int_info_err =3D from->exit_int_info_err; - to->nested_ctl =3D from->nested_ctl; to->event_inj =3D from->event_inj; to->event_inj_err =3D from->event_inj_err; to->next_rip =3D from->next_rip; --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 385B336D4FD; Tue, 24 Feb 2026 22:34:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972469; cv=none; b=WchY7pnbSE5647sJ7zL3tNfos3Wt5eQEDAidbhFJCZGY/Jx2NxluynIDr7yeyf4vR1xQvcHKj+ZD/4vhOdpjCitwqkb52XRngyXupCL1bRnGwQsnb8venttzE8G1mCV/V7p1CMeMikzRkp8GGsc4MRMCRMM+tB/BAJsjOavIkS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972469; c=relaxed/simple; bh=89Uh9bf3kyv7g5pliglimBSNkSLXd6p0n1HAtULumzE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JY/58r8C2Cw3sTk4f5cXxVN91FSHx1ZMrKd3R3iJtQ64P6SWSW9urElN8vs27C3fi9Ki4H2Y+pj72ZHZToArEAfkmygrxPDf3IXzfjx9QZl/pcZ4zjDwSnouJrUCJWFhqKx6K4uUKk1xe/eOzPKtcRst6gA0CaNmVBIVCg47Pgw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lcmNmzmN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lcmNmzmN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D446AC116D0; Tue, 24 Feb 2026 22:34:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972469; bh=89Uh9bf3kyv7g5pliglimBSNkSLXd6p0n1HAtULumzE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lcmNmzmN3C2Sh8SEioRGp6IHWOB9kBhzj8IWU1v2H2f8LDbgrWrQ5Dg3A3rnelXvq OVu19vOepuCNonAdlswifsf1c0/O2TdLImoQCqoYsZBYM+zVUPXZnrP8XpLBjH/rsC f842/fSDTIVj/UBXD1Esari8iZKtLkPBYKyQjmbTw7DaR3NthQtFEWP4Djrzz2HMt8 qE+6CJoTwYhVlQ2saUTu0awSKO8vrrO8m5SutGunUUSf9VKXFtu3eEpU2Be2LcmGBw IYfVSMVe3LiFK85a+QpBEjbr62VlZDgGvmr0PGUzjrdqCgFsPirSFVIyGaGAG8wzgi NnhV0ptyjiGfw== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 19/31] KVM: nSVM: Add missing consistency check for nCR3 validity Date: Tue, 24 Feb 2026 22:33:53 +0000 Message-ID: <20260224223405.3270433-20-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From the APM Volume #2, 15.25.4 (24593=E2=80=94Rev. 3.42=E2=80=94March 2024= ): When VMRUN is executed with nested paging enabled (NP_ENABLE =3D 1), the following conditions are considered illegal state combinations, in addition to those mentioned in =E2=80=9CCanonicalization and Consistency Checks=E2=80=9D: =E2=80=A2 Any MBZ bit of nCR3 is set. =E2=80=A2 Any G_PAT.PA field has an unsupported type encoding or any reserved field in G_PAT has a nonzero value. Add the consistency check for nCR3 being a legal GPA with no MBZ bits set. The G_PAT.PA check was proposed separately [*]. [*]https://lore.kernel.org/kvm/20260205214326.1029278-3-jmattson@google.com/ Fixes: 4b16184c1cca ("KVM: SVM: Initialize Nested Nested MMU context on VMR= UN") Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 0f18123f5461f..752dd9eb98a84 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -350,6 +350,11 @@ static bool nested_vmcb_check_controls(struct kvm_vcpu= *vcpu, if (CC(control->asid =3D=3D 0)) return false; =20 + if (control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) { + if (CC(!kvm_vcpu_is_legal_gpa(vcpu, control->nested_cr3))) + return false; + } + if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa, MSRPM_SIZE))) return false; --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A5B0636F40C; Tue, 24 Feb 2026 22:34:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972469; cv=none; b=lFL95gJCBFgOqmDvMiHBR5rSuF18Bsa4ryiu3kBjsN4Cwwv9Wzs6EN7b9rUf48y+wHARDubYhjrX7w3PuPme7M7FWHkB7SpTZxOLTn7LebRjakGbMK+20rC/FsPTlPoa8Ll8bjY5EU0u+llEdYYSvWs/qX+LQTRSM8Kx4dAd3lA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972469; c=relaxed/simple; bh=C3tF9JsJzbsyykHYuvTRTgF0nYa7yshMd2hy9I1QB9o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DIIZ6i7nwME4bvLEkd5CGfdvkPFCrtaXyCV+tCkTaflG+uxKGURGFMXfburLOkb7l71g055WmseqvCgklweBHezet9QLCoDMSkzqillauiLP3ezQOrByMKa6gWDU1fS64daeTo017JFlwOeWJr1jcb95n08IaUD0FR/7Pj0GWnM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lrW3PJls; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lrW3PJls" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 428D0C19423; Tue, 24 Feb 2026 22:34:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972469; bh=C3tF9JsJzbsyykHYuvTRTgF0nYa7yshMd2hy9I1QB9o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lrW3PJlsY7MKVzsssSx0S5yLI55t1gf9Nw8JVmbZaTfB5bDBABhQiSn/T7ukwzd+f ibNRXS5kBuFR2okjAvUEow3degwdYDqCXZtDbvE5UeyADmfuKbRiTn63lOVbz+u/e7 Rc1YLZruvl6gEI9vZbtayrNXXlb2vOtowSLNmy8q3o97sQxYLgqgOvpGTdXHZSdq0H onB83w5A5CpBojBkFBn1znmfH6+/r+T3LmlUxNW1lCYBylbCDNr2TQiKoNToH3vnTt sjx5H44QVGbdVzIlG+2rHZZ5O+PT6WE5zz75mOSDxnUcmkl1NScabPbbZg2fYUGcXY sJ7YWZgfXwLhQ== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 20/31] KVM: nSVM: Add missing consistency check for hCR0.PG and NP_ENABLE Date: Tue, 24 Feb 2026 22:33:54 +0000 Message-ID: <20260224223405.3270433-21-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From the APM Volume #2, 15.25.3 (24593=E2=80=94Rev. 3.42=E2=80=94March 2024= ): If VMRUN is executed with hCR0.PG cleared to zero and NP_ENABLE set to 1 , VMRUN terminates with #VMEXIT(VMEXIT_INVALID). Add the consistency check by plumbing L1's CR0 to nested_vmcb_check_controls(). Fixes: 4b16184c1cca ("KVM: SVM: Initialize Nested Nested MMU context on VMR= UN") Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 752dd9eb98a84..6fffb6ae6b88b 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -342,7 +342,8 @@ static bool nested_svm_check_bitmap_pa(struct kvm_vcpu = *vcpu, u64 pa, u32 size) } =20 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu, - struct vmcb_ctrl_area_cached *control) + struct vmcb_ctrl_area_cached *control, + unsigned long l1_cr0) { if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN))) return false; @@ -353,6 +354,8 @@ static bool nested_vmcb_check_controls(struct kvm_vcpu = *vcpu, if (control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) { if (CC(!kvm_vcpu_is_legal_gpa(vcpu, control->nested_cr3))) return false; + if (CC(!(l1_cr0 & X86_CR0_PG))) + return false; } =20 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa, @@ -952,7 +955,8 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmc= b12_gpa, enter_guest_mode(vcpu); =20 if (!nested_vmcb_check_save(vcpu, &svm->nested.save) || - !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) + !nested_vmcb_check_controls(vcpu, &svm->nested.ctl, + svm->vmcb01.ptr->save.cr0)) return -EINVAL; =20 if (nested_npt_enabled(svm)) @@ -1888,7 +1892,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, =20 ret =3D -EINVAL; __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl); - if (!nested_vmcb_check_controls(vcpu, &ctl_cached)) + /* 'save' contains L1 state saved from before VMRUN */ + if (!nested_vmcb_check_controls(vcpu, &ctl_cached, save->cr0)) goto out_free; =20 /* --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 0708937416E; Tue, 24 Feb 2026 22:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972470; cv=none; b=q79+9dE4F1u+g8WANXfQT/rYE00vQg3DvxXv0o9oJSyFuXE4yfQtkvStZpieaCx4pqLquIb5MRP6Z/7bil1ixaoWhCa2kHtiHJJNq5BpMxUeLtps0Rtnv0uN5VOgT8jsO7MfeFI8m+9QVZuNeWEWHoSmmOM89SH58TJ+wlcWgA0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972470; c=relaxed/simple; bh=k4+yXe4ROo+NBnHHVzfrgeWre0T/weuNn//DP9UhS0o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lQjTAZesPFTEaJrR5ycVbiUebh0sotO9PmcsHytYDaz5kj768WCdDWgLkgqAiiDL4LPnSknYWWDL4i2EkSib+aknK8sIY8zCY8D1EHv3EBn4XKfigmXs2IsniF6YlOAnMLa/NuHWh0OZtwhDQxFOM5yAlK2c9xFTy8LANGDYX04= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BdWNyYFo; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BdWNyYFo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A46E1C2BC87; Tue, 24 Feb 2026 22:34:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972469; bh=k4+yXe4ROo+NBnHHVzfrgeWre0T/weuNn//DP9UhS0o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BdWNyYFoDmIAs0p1ufqBoCb+obXSqq/sqJE6ucczyfLgD/ocBoi62anGAWFdLIRVP rR1Z9Pkkmtx8UE8nOaqB78Bqb5drs1s8OE6JCC1Y0HhpWQXuUInrkpQhZH8bQio/Oq dPV0Odv2MC2PRT2Q+DlnfVkZTBuRjGaBUVshR0zr+x6KgnQyIs5kFEwLIOIL7UcOdf zQCa2L+iOMVnu4fOT75Jtep/q48gaREMtk/duz/lGeA6iKhDRV3+mp7sFFndV+1NTc 1wxYSYKI5tO1aEc3vPvzFlWZmfMeLgYWN96X3KzNIRjlY5iOSUgbOsFWop3DmxrhiW 92LHvGbIGPMew== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 21/31] KVM: nSVM: Add missing consistency check for EFER, CR0, CR4, and CS Date: Tue, 24 Feb 2026 22:33:55 +0000 Message-ID: <20260224223405.3270433-22-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable According to the APM Volume #2, 15.5, Canonicalization and Consistency Checks (24593=E2=80=94Rev. 3.42=E2=80=94March 2024), the following conditio= n (among others) results in a #VMEXIT with VMEXIT_INVALID (aka SVM_EXIT_ERR): EFER.LME, CR0.PG, CR4.PAE, CS.L, and CS.D are all non-zero. Add the missing consistency check. This is functionally a nop because the nested VMRUN results in SVM_EXIT_ERR in HW, which is forwarded to L1, but KVM makes all consistency checks before a VMRUN is actually attempted. Fixes: 3d6368ef580a ("KVM: SVM: Add VMRUN handler") Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 7 +++++++ arch/x86/kvm/svm/svm.h | 1 + 2 files changed, 8 insertions(+) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 6fffb6ae6b88b..2c852e94a9ad9 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -397,6 +397,11 @@ static bool nested_vmcb_check_save(struct kvm_vcpu *vc= pu, CC(!(save->cr0 & X86_CR0_PE)) || CC(!kvm_vcpu_is_legal_cr3(vcpu, save->cr3))) return false; + + if (CC((save->cr4 & X86_CR4_PAE) && + (save->cs.attrib & SVM_SELECTOR_L_MASK) && + (save->cs.attrib & SVM_SELECTOR_DB_MASK))) + return false; } =20 /* Note, SVM doesn't have any additional restrictions on CR4. */ @@ -492,6 +497,8 @@ static void __nested_copy_vmcb_save_to_cache(struct vmc= b_save_area_cached *to, * Copy only fields that are validated, as we need them * to avoid TOC/TOU races. */ + to->cs =3D from->cs; + to->efer =3D from->efer; to->cr0 =3D from->cr0; to->cr3 =3D from->cr3; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 7629cb37c9302..0a5d5a4453b7e 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -140,6 +140,7 @@ struct kvm_vmcb_info { }; =20 struct vmcb_save_area_cached { + struct vmcb_seg cs; u64 efer; u64 cr4; u64 cr3; --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6980E376491; Tue, 24 Feb 2026 22:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972470; cv=none; b=OFLWj2nQnQsbeLSKLP0/YDmaEhMJ0kwnoCcUgdnIwBDPonv0gLLYBiKEPCHjMnn/HehEymTIHgnS3NC1jlHdIN/FIhY9++GA10ey6kOcbxuZBX1JhudjpNu4f0uCl2QReBX+7gEi3GHhkXet/aa+Waj9jufBKPBnxckNWddnxeQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972470; c=relaxed/simple; bh=DxoJTrYPRiCr+cybBN/3fLkSB0kroFFBYrVWTNY1gXw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=c9Gb2wG6MTOmorp8j4j+fa73JMeoePgFKBeXU3VtYtzfF57JlQHpR3Kfsuix9kYKZ4S8eEj/+T+ScpGqwUqSa53fMy4qUK9f4bOJaBMSHVL31DWKoJOrCvkI4MUQnVSIygsnfI6wXOV5OG8vC9yBrsTOR7awbuwBBlIjBUQK3k8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kCh+TeJ5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kCh+TeJ5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12A0CC116D0; Tue, 24 Feb 2026 22:34:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972470; bh=DxoJTrYPRiCr+cybBN/3fLkSB0kroFFBYrVWTNY1gXw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kCh+TeJ5zUhvVJ1VqQMFD9uDHyXOmKQ4lHReHeb8E8CdEjjViU1JJQvIGVrQZiM7r 6j6AWY2UKGUK0JGouYld8z8EDEw/mawk+rtJtu4AIDUb8XTLWw3GouL5cl1scOM69a xbXO6ZvHNQE7cvyvf6cjSDnklZRtzYuykx6b1w0lKdJ5rvEDhU3rqcliPPkDMibO/E 8Z1i7BeFJRJ8eQXgb/IW8OPKOQmLD4+LR9oRIeVy7dkl/ic36F3P7u4yyj5uz9kda4 xqaZlBGZqJxW9Xi4QzGRx63dEioMKYgF7jj3JpBEyyHyB2vW0dFOJ+waLaK6zOGaAW DLY+ZykEq4j0Q== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , stable@vger.kernel.org Subject: [PATCH v6 22/31] KVM: nSVM: Add missing consistency check for EVENTINJ Date: Tue, 24 Feb 2026 22:33:56 +0000 Message-ID: <20260224223405.3270433-23-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable According to the APM Volume #2, 15.20 (24593=E2=80=94Rev. 3.42=E2=80=94Marc= h 2024): VMRUN exits with VMEXIT_INVALID error code if either: =E2=80=A2 Reserved values of TYPE have been specified, or =E2=80=A2 TYPE =3D 3 (exception) has been specified with a vector that do= es not correspond to an exception (this includes vector 2, which is an NMI, not an exception). Add the missing consistency checks to KVM. For the second point, inject VMEXIT_INVALID if the vector is anything but the vectors defined by the APM for exceptions. Reserved vectors are also considered invalid, which matches the HW behavior. Vector 9 (i.e. #CSO) is considered invalid because it is reserved on modern CPUs, and according to LLMs no CPUs exist supporting SVM and producing #CSOs. Defined exceptions could be different between virtual CPUs as new CPUs define new vectors. In a best effort to dynamically define the valid vectors, make all currently defined vectors as valid except those obviously tied to a CPU feature: SHSTK -> #CP and SEV-ES -> #VC. As new vectors are defined, they can similarly be tied to corresponding CPU features. Invalid vectors on specific (e.g. old) CPUs that are missed by KVM should be rejected by HW anyway. Fixes: 3d6368ef580a ("KVM: SVM: Add VMRUN handler") CC: stable@vger.kernel.org Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 2c852e94a9ad9..dc0e0ac881979 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -341,6 +341,54 @@ static bool nested_svm_check_bitmap_pa(struct kvm_vcpu= *vcpu, u64 pa, u32 size) kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1); } =20 +static bool nested_svm_event_inj_valid_exept(struct kvm_vcpu *vcpu, u8 vec= tor) +{ + /* + * Vectors that do not correspond to a defined exception are invalid + * (including #NMI and reserved vectors). In a best effort to define + * valid exceptions based on the virtual CPU, make all exceptions always + * valid except those obviously tied to a CPU feature. + */ + switch (vector) { + case DE_VECTOR: case DB_VECTOR: case BP_VECTOR: case OF_VECTOR: + case BR_VECTOR: case UD_VECTOR: case NM_VECTOR: case DF_VECTOR: + case TS_VECTOR: case NP_VECTOR: case SS_VECTOR: case GP_VECTOR: + case PF_VECTOR: case MF_VECTOR: case AC_VECTOR: case MC_VECTOR: + case XM_VECTOR: case HV_VECTOR: case SX_VECTOR: + return true; + case CP_VECTOR: + return guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK); + case VC_VECTOR: + return guest_cpu_cap_has(vcpu, X86_FEATURE_SEV_ES); + } + return false; +} + +/* + * According to the APM, VMRUN exits with SVM_EXIT_ERR if SVM_EVTINJ_VALID= is + * set and: + * - The type of event_inj is not one of the defined values. + * - The type is SVM_EVTINJ_TYPE_EXEPT, but the vector is not a valid exce= ption. + */ +static bool nested_svm_check_event_inj(struct kvm_vcpu *vcpu, u32 event_in= j) +{ + u32 type =3D event_inj & SVM_EVTINJ_TYPE_MASK; + u8 vector =3D event_inj & SVM_EVTINJ_VEC_MASK; + + if (!(event_inj & SVM_EVTINJ_VALID)) + return true; + + if (type !=3D SVM_EVTINJ_TYPE_INTR && type !=3D SVM_EVTINJ_TYPE_NMI && + type !=3D SVM_EVTINJ_TYPE_EXEPT && type !=3D SVM_EVTINJ_TYPE_SOFT) + return false; + + if (type =3D=3D SVM_EVTINJ_TYPE_EXEPT && + !nested_svm_event_inj_valid_exept(vcpu, vector)) + return false; + + return true; +} + static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu, struct vmcb_ctrl_area_cached *control, unsigned long l1_cr0) @@ -370,6 +418,9 @@ static bool nested_vmcb_check_controls(struct kvm_vcpu = *vcpu, return false; } =20 + if (CC(!nested_svm_check_event_inj(vcpu, control->event_inj))) + return false; + return true; } =20 --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 F1B05363C59; Tue, 24 Feb 2026 22:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972471; cv=none; b=PuOKh5TfeJlvQmMcgMARfNNliorE3crC3IUYup18eRHBBjZo+s2+Rf7fSJXX3WSmbyhwdNDz8POdWgZkBz1PpnQ1g55I8uhewu0fsfME7hkvm6qCSyiugBL8xGnMV35iNtcnwQVQ3MGtvBUmbDkM8qFDkfuSkib/1VQ+l2K21Sg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972471; c=relaxed/simple; bh=ldyOOahHuuAgbY1ASVezABfDTw3UegDvGr8MaGFyqzs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=haLwEDR0+I7/BJoPZK/l8gzsLKG6nNe71XucqSNIw63rALW2CWq85n4H94O13FwZLSD9aspyB0zGGSrS24gToCSX1eQ8tWeiY2cOPXLT2SAqdpbzo/UrwVTZiBFVyc53pTWJIabZfO7F+AMUdFt0Wt8aiconOA/YCZnSzdCafUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FXKo9ruk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FXKo9ruk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74CD6C19423; Tue, 24 Feb 2026 22:34:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972470; bh=ldyOOahHuuAgbY1ASVezABfDTw3UegDvGr8MaGFyqzs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FXKo9rukEaHKRyXUXgXd5E6vW+7s8apk6WCgxCz0MrNDLrakVVk221m7A9Lsg+/DV 8Lqr7R+a68b6rwaSmgWJcRZ0RWLQbWnHzBzSEiB/I8XtrR6J1binfOGflq1Hl+A/GH jvKX5QV++G3d1j3X/goj7nnUNv4R/Fsec2pHetE1kmkLMi1m8MtmTlWRPyfhMI35gG 96oQeiYLaJqbZlfceBhwRDCUmLBkC6GIfJOTN3EMwfksIRs6tWPquBtvxYONsrG0r1 Sc+IkkBWwIg0Swx8OIxIHz6WF5k15VcSiTYb4TalJbp7aEHzfSjIZfcq+6twtIjsXC kK+14JsIcDURg== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v6 23/31] KVM: SVM: Rename vmcb->nested_ctl to vmcb->misc_ctl Date: Tue, 24 Feb 2026 22:33:57 +0000 Message-ID: <20260224223405.3270433-24-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The 'nested_ctl' field is misnamed. Although the first bit is for nested paging, the other defined bits are for SEV/SEV-ES. Other bits in the same field according to the APM (but not defined by KVM) include "Guest Mode Execution Trap", "Enable INVLPGB/TLBSYNC", and other control bits unrelated to 'nested'. There is nothing common among these bits, so just name the field misc_ctl. Also rename the flags accordingly. Signed-off-by: Yosry Ahmed --- arch/x86/include/asm/svm.h | 8 ++++---- arch/x86/kvm/svm/nested.c | 14 +++++++------- arch/x86/kvm/svm/sev.c | 4 ++-- arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/svm/svm.h | 4 ++-- tools/testing/selftests/kvm/include/x86/svm.h | 6 +++--- tools/testing/selftests/kvm/lib/x86/svm.c | 2 +- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index edde36097ddc3..983db6575141d 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -142,7 +142,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area { u64 exit_info_2; u32 exit_int_info; u32 exit_int_info_err; - u64 nested_ctl; + u64 misc_ctl; u64 avic_vapic_bar; u64 ghcb_gpa; u32 event_inj; @@ -239,9 +239,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_IOIO_SIZE_MASK (7 << SVM_IOIO_SIZE_SHIFT) #define SVM_IOIO_ASIZE_MASK (7 << SVM_IOIO_ASIZE_SHIFT) =20 -#define SVM_NESTED_CTL_NP_ENABLE BIT(0) -#define SVM_NESTED_CTL_SEV_ENABLE BIT(1) -#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_MISC_ENABLE_NP BIT(0) +#define SVM_MISC_ENABLE_SEV BIT(1) +#define SVM_MISC_ENABLE_SEV_ES BIT(2) =20 =20 #define SVM_TSC_RATIO_RSVD 0xffffff0000000000ULL diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index dc0e0ac881979..099cdab878d45 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -399,7 +399,7 @@ static bool nested_vmcb_check_controls(struct kvm_vcpu = *vcpu, if (CC(control->asid =3D=3D 0)) return false; =20 - if (control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) { + if (control->misc_ctl & SVM_MISC_ENABLE_NP) { if (CC(!kvm_vcpu_is_legal_gpa(vcpu, control->nested_cr3))) return false; if (CC(!(l1_cr0 & X86_CR0_PG))) @@ -494,10 +494,10 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_v= cpu *vcpu, nested_svm_sanitize_intercept(vcpu, to, SKINIT); nested_svm_sanitize_intercept(vcpu, to, RDPRU); =20 - /* Always clear SVM_NESTED_CTL_NP_ENABLE if the guest cannot use NPTs */ - to->nested_ctl =3D from->nested_ctl; + /* Always clear SVM_MISC_ENABLE_NP if the guest cannot use NPTs */ + to->misc_ctl =3D from->misc_ctl; if (!guest_cpu_cap_has(vcpu, X86_FEATURE_NPT)) - to->nested_ctl &=3D ~SVM_NESTED_CTL_NP_ENABLE; + to->misc_ctl &=3D ~SVM_MISC_ENABLE_NP; =20 to->iopm_base_pa =3D from->iopm_base_pa; to->msrpm_base_pa =3D from->msrpm_base_pa; @@ -838,7 +838,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_s= vm *svm, } =20 /* Copied from vmcb01. msrpm_base can be overwritten later. */ - vmcb02->control.nested_ctl =3D vmcb01->control.nested_ctl; + vmcb02->control.misc_ctl =3D vmcb01->control.misc_ctl; vmcb02->control.iopm_base_pa =3D vmcb01->control.iopm_base_pa; vmcb02->control.msrpm_base_pa =3D vmcb01->control.msrpm_base_pa; vmcb_mark_dirty(vmcb02, VMCB_PERM_MAP); @@ -994,7 +994,7 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmc= b12_gpa, vmcb12->save.rip, vmcb12->control.int_ctl, vmcb12->control.event_inj, - vmcb12->control.nested_ctl, + vmcb12->control.misc_ctl, vmcb12->control.nested_cr3, vmcb12->save.cr3, KVM_ISA_SVM); @@ -1810,7 +1810,7 @@ static void nested_copy_vmcb_cache_to_control(struct = vmcb_control_area *dst, dst->exit_info_2 =3D from->exit_info_2; dst->exit_int_info =3D from->exit_int_info; dst->exit_int_info_err =3D from->exit_int_info_err; - dst->nested_ctl =3D from->nested_ctl; + dst->misc_ctl =3D from->misc_ctl; dst->event_inj =3D from->event_inj; dst->event_inj_err =3D from->event_inj_err; dst->next_rip =3D from->next_rip; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index ea515cf411686..0ed9cfed1cbcc 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4599,7 +4599,7 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm, bo= ol init_event) struct kvm_sev_info *sev =3D to_kvm_sev_info(svm->vcpu.kvm); struct vmcb *vmcb =3D svm->vmcb01.ptr; =20 - svm->vmcb->control.nested_ctl |=3D SVM_NESTED_CTL_SEV_ES_ENABLE; + svm->vmcb->control.misc_ctl |=3D SVM_MISC_ENABLE_SEV_ES; =20 /* * An SEV-ES guest requires a VMSA area that is a separate from the @@ -4670,7 +4670,7 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_ev= ent) { struct kvm_vcpu *vcpu =3D &svm->vcpu; =20 - svm->vmcb->control.nested_ctl |=3D SVM_NESTED_CTL_SEV_ENABLE; + svm->vmcb->control.misc_ctl |=3D SVM_MISC_ENABLE_SEV; clr_exception_intercept(svm, UD_VECTOR); =20 /* diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 1b31b033d79b0..7bc8b72fe5ad8 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1152,7 +1152,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool ini= t_event) =20 if (npt_enabled) { /* Setup VMCB for Nested Paging */ - control->nested_ctl |=3D SVM_NESTED_CTL_NP_ENABLE; + control->misc_ctl |=3D SVM_MISC_ENABLE_NP; svm_clr_intercept(svm, INTERCEPT_INVLPG); clr_exception_intercept(svm, PF_VECTOR); svm_clr_intercept(svm, INTERCEPT_CR3_READ); @@ -3362,7 +3362,7 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); - pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); + pr_err("%-20s%lld\n", "misc_ctl:", control->misc_ctl); pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0a5d5a4453b7e..f66e5c8565aad 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -167,7 +167,7 @@ struct vmcb_ctrl_area_cached { u64 exit_info_2; u32 exit_int_info; u32 exit_int_info_err; - u64 nested_ctl; + u64 misc_ctl; u32 event_inj; u32 event_inj_err; u64 next_rip; @@ -579,7 +579,7 @@ static inline bool gif_set(struct vcpu_svm *svm) =20 static inline bool nested_npt_enabled(struct vcpu_svm *svm) { - return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE; + return svm->nested.ctl.misc_ctl & SVM_MISC_ENABLE_NP; } =20 static inline bool nested_vnmi_enabled(struct vcpu_svm *svm) diff --git a/tools/testing/selftests/kvm/include/x86/svm.h b/tools/testing/= selftests/kvm/include/x86/svm.h index 10b30b38bb3fd..d81d8a9f5bfb6 100644 --- a/tools/testing/selftests/kvm/include/x86/svm.h +++ b/tools/testing/selftests/kvm/include/x86/svm.h @@ -97,7 +97,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area { u64 exit_info_2; u32 exit_int_info; u32 exit_int_info_err; - u64 nested_ctl; + u64 misc_ctl; u64 avic_vapic_bar; u8 reserved_4[8]; u32 event_inj; @@ -175,8 +175,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL #define SVM_VM_CR_SVM_DIS_MASK 0x0010ULL =20 -#define SVM_NESTED_CTL_NP_ENABLE BIT(0) -#define SVM_NESTED_CTL_SEV_ENABLE BIT(1) +#define SVM_MISC_ENABLE_NP BIT(0) +#define SVM_MISC_ENABLE_SEV BIT(1) =20 struct __attribute__ ((__packed__)) vmcb_seg { u16 selector; diff --git a/tools/testing/selftests/kvm/lib/x86/svm.c b/tools/testing/self= tests/kvm/lib/x86/svm.c index 2e5c480c9afd4..eb20b00112c76 100644 --- a/tools/testing/selftests/kvm/lib/x86/svm.c +++ b/tools/testing/selftests/kvm/lib/x86/svm.c @@ -126,7 +126,7 @@ void generic_svm_setup(struct svm_test_data *svm, void = *guest_rip, void *guest_r guest_regs.rdi =3D (u64)svm; =20 if (svm->ncr3_gpa) { - ctrl->nested_ctl |=3D SVM_NESTED_CTL_NP_ENABLE; + ctrl->misc_ctl |=3D SVM_MISC_ENABLE_NP; ctrl->nested_cr3 =3D svm->ncr3_gpa; } } --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3312A3A1E82; Tue, 24 Feb 2026 22:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972471; cv=none; b=Ukj+Izcg5mcX9fcPCc2WU7fkcdAHJz+pNo2B6Aj+M2JetlYX91mQzuPk0l1y704RH/LOX/YgWghI0F1aaN/u8imf2x5onRU0fEMBewyu5/TxRpcZBEQnjgM01WeMJ8aNIKE+9Y4Yy1Lc1g2hw0WEEt9NwxFJw3APAKAN5N1c+ac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972471; c=relaxed/simple; bh=yp8GpW6eTx61cSDikx3my3G1B8Y0aqzVvQBCkxnm7iA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tjLV23KvnMXaIlTlzb0vpGqqARh/c1PiMT3bYnMDj7WzcpoyZ9Pnrk/1CtrpAglv9xtNO5FCu04kWWYh/ETDvoIAO36gY8sBMmsbcaJe2Nvcrg92oBDmpJt7MdI++/VjOh3ojy7q2dXbHoizMu1Nizi/Le0MCdARfNqsYrKNdK8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qr+9HrcZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qr+9HrcZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE9BFC116D0; Tue, 24 Feb 2026 22:34:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972471; bh=yp8GpW6eTx61cSDikx3my3G1B8Y0aqzVvQBCkxnm7iA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qr+9HrcZriySJm4io98YLZydHdxQwI/EYFrbAS3Xd75swJeBKF2y5fN7uFlFmonm0 OXFoAcZdR8P+Tpzrh8XbPpuvXMk4nNlLFH7XG4DN8jxaru7oN5THTOn8dnt5Y50Zp2 bLnio/UOlE0arQB8TIabv7Tc52+0sTuMIe04P/XmgjVcbkqpSuMt7v96FdmNa/rAZv MMclVuGud3zxg08xE9z0Tsy6BKI22N0cRauMZWoJUP46FdCgOuphqXWVGIh3jGNAS3 roc/ugloRe0sJEiricEu+DrQIQXl+A9YANAxiYv96Wspz+BNQudT/m3XFBRSwfg4cT 9tXAzw5DEZp3g== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v6 24/31] KVM: SVM: Rename vmcb->virt_ext to vmcb->misc_ctl2 Date: Tue, 24 Feb 2026 22:33:58 +0000 Message-ID: <20260224223405.3270433-25-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 'virt' is confusing in the VMCB because it is relative and ambiguous. The 'virt_ext' field includes bits for LBR virtualization and VMSAVE/VMLOAD virtualization, so it's just another miscellaneous control field. Name it as such. While at it, move the definitions of the bits below those for 'misc_ctl' and rename them for consistency. Signed-off-by: Yosry Ahmed --- arch/x86/include/asm/svm.h | 7 +++---- arch/x86/kvm/svm/nested.c | 16 +++++++-------- arch/x86/kvm/svm/svm.c | 20 +++++++++---------- arch/x86/kvm/svm/svm.h | 2 +- tools/testing/selftests/kvm/include/x86/svm.h | 8 ++++---- .../kvm/x86/nested_vmsave_vmload_test.c | 16 +++++++-------- .../selftests/kvm/x86/svm_lbr_nested_state.c | 4 ++-- 7 files changed, 36 insertions(+), 37 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 983db6575141d..c169256c415fb 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -148,7 +148,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area { u32 event_inj; u32 event_inj_err; u64 nested_cr3; - u64 virt_ext; + u64 misc_ctl2; u32 clean; u32 reserved_5; u64 next_rip; @@ -222,9 +222,6 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define X2APIC_MODE_SHIFT 30 #define X2APIC_MODE_MASK (1 << X2APIC_MODE_SHIFT) =20 -#define LBR_CTL_ENABLE_MASK BIT_ULL(0) -#define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1) - #define SVM_INTERRUPT_SHADOW_MASK BIT_ULL(0) #define SVM_GUEST_INTERRUPT_MASK BIT_ULL(1) =20 @@ -243,6 +240,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_MISC_ENABLE_SEV BIT(1) #define SVM_MISC_ENABLE_SEV_ES BIT(2) =20 +#define SVM_MISC2_ENABLE_V_LBR BIT_ULL(0) +#define SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE BIT_ULL(1) =20 #define SVM_TSC_RATIO_RSVD 0xffffff0000000000ULL #define SVM_TSC_RATIO_MIN 0x0000000000000001ULL diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 099cdab878d45..679ac9f6dfe80 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -116,7 +116,7 @@ static bool nested_vmcb_needs_vls_intercept(struct vcpu= _svm *svm) if (!nested_npt_enabled(svm)) return true; =20 - if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK)) + if (!(svm->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE)) return true; =20 return false; @@ -179,7 +179,7 @@ void recalc_intercepts(struct vcpu_svm *svm) vmcb_set_intercept(c, INTERCEPT_VMLOAD); vmcb_set_intercept(c, INTERCEPT_VMSAVE); } else { - WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK)); + WARN_ON(!(c->misc_ctl2 & SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE)); } } =20 @@ -516,7 +516,7 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcp= u *vcpu, to->event_inj_err =3D from->event_inj_err; to->next_rip =3D from->next_rip; to->nested_cr3 =3D from->nested_cr3; - to->virt_ext =3D from->virt_ext; + to->misc_ctl2 =3D from->misc_ctl2; to->pause_filter_count =3D from->pause_filter_count; to->pause_filter_thresh =3D from->pause_filter_thresh; =20 @@ -695,7 +695,7 @@ void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm) static bool nested_vmcb12_has_lbrv(struct kvm_vcpu *vcpu) { return guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && - (to_svm(vcpu)->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK); + (to_svm(vcpu)->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_LBR); } =20 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *= vmcb12) @@ -919,10 +919,10 @@ static void nested_vmcb02_prepare_control(struct vcpu= _svm *svm, svm->soft_int_next_rip =3D vmcb12_rip; } =20 - /* LBR_CTL_ENABLE_MASK is controlled by svm_update_lbrv() */ + /* SVM_MISC2_ENABLE_V_LBR is controlled by svm_update_lbrv() */ =20 if (!nested_vmcb_needs_vls_intercept(svm)) - vmcb02->control.virt_ext |=3D VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; + vmcb02->control.misc_ctl2 |=3D SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE; =20 if (guest_cpu_cap_has(vcpu, X86_FEATURE_PAUSEFILTER)) pause_count12 =3D svm->nested.ctl.pause_filter_count; @@ -1814,8 +1814,8 @@ static void nested_copy_vmcb_cache_to_control(struct = vmcb_control_area *dst, dst->event_inj =3D from->event_inj; dst->event_inj_err =3D from->event_inj_err; dst->next_rip =3D from->next_rip; - dst->nested_cr3 =3D from->nested_cr3; - dst->virt_ext =3D from->virt_ext; + dst->nested_cr3 =3D from->nested_cr3; + dst->misc_ctl2 =3D from->misc_ctl2; dst->pause_filter_count =3D from->pause_filter_count; dst->pause_filter_thresh =3D from->pause_filter_thresh; /* 'clean' and 'hv_enlightenments' are not changed by KVM */ diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 7bc8b72fe5ad8..94e14badddfa2 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -710,7 +710,7 @@ void *svm_alloc_permissions_map(unsigned long size, gfp= _t gfp_mask) static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); - bool intercept =3D !(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK); + bool intercept =3D !(svm->vmcb->control.misc_ctl2 & SVM_MISC2_ENABLE_V_LB= R); =20 if (intercept =3D=3D svm->lbr_msrs_intercepted) return; @@ -843,7 +843,7 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *= vcpu) =20 static void __svm_enable_lbrv(struct kvm_vcpu *vcpu) { - to_svm(vcpu)->vmcb->control.virt_ext |=3D LBR_CTL_ENABLE_MASK; + to_svm(vcpu)->vmcb->control.misc_ctl2 |=3D SVM_MISC2_ENABLE_V_LBR; } =20 void svm_enable_lbrv(struct kvm_vcpu *vcpu) @@ -855,16 +855,16 @@ void svm_enable_lbrv(struct kvm_vcpu *vcpu) static void __svm_disable_lbrv(struct kvm_vcpu *vcpu) { KVM_BUG_ON(sev_es_guest(vcpu->kvm), vcpu->kvm); - to_svm(vcpu)->vmcb->control.virt_ext &=3D ~LBR_CTL_ENABLE_MASK; + to_svm(vcpu)->vmcb->control.misc_ctl2 &=3D ~SVM_MISC2_ENABLE_V_LBR; } =20 void svm_update_lbrv(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); - bool current_enable_lbrv =3D svm->vmcb->control.virt_ext & LBR_CTL_ENABLE= _MASK; + bool current_enable_lbrv =3D svm->vmcb->control.misc_ctl2 & SVM_MISC2_ENA= BLE_V_LBR; bool enable_lbrv =3D (svm->vmcb->save.dbgctl & DEBUGCTLMSR_LBR) || (is_guest_mode(vcpu) && guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && - (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK)); + (svm->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_LBR)); =20 if (enable_lbrv && !current_enable_lbrv) __svm_enable_lbrv(vcpu); @@ -1023,7 +1023,7 @@ static void svm_recalc_instruction_intercepts(struct = kvm_vcpu *vcpu) } =20 /* - * No need to toggle VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK here, it is + * No need to toggle SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE here, it is * always set if vls is enabled. If the intercepts are set, the bit is * meaningless anyway. */ @@ -1191,7 +1191,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool ini= t_event) } =20 if (vls) - svm->vmcb->control.virt_ext |=3D VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; + svm->vmcb->control.misc_ctl2 |=3D SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE; =20 if (vcpu->kvm->arch.bus_lock_detection_enabled) svm_set_intercept(svm, INTERCEPT_BUSLOCK); @@ -3368,7 +3368,7 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa); pr_err("%-20s%08x\n", "event_inj:", control->event_inj); pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); - pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext); + pr_err("%-20s%lld\n", "misc_ctl2:", control->misc_ctl2); pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page= ); pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id); @@ -4363,7 +4363,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_= vcpu *vcpu, u64 run_flags) * VM-Exit), as running with the host's DEBUGCTL can negatively affect * guest state and can even be fatal, e.g. due to Bus Lock Detect. */ - if (!(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) && + if (!(svm->vmcb->control.misc_ctl2 & SVM_MISC2_ENABLE_V_LBR) && vcpu->arch.host_debugctl !=3D svm->vmcb->save.dbgctl) update_debugctlmsr(svm->vmcb->save.dbgctl); =20 @@ -4394,7 +4394,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_= vcpu *vcpu, u64 run_flags) if (unlikely(svm->vmcb->control.exit_code =3D=3D SVM_EXIT_NMI)) kvm_before_interrupt(vcpu, KVM_HANDLING_NMI); =20 - if (!(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) && + if (!(svm->vmcb->control.misc_ctl2 & SVM_MISC2_ENABLE_V_LBR) && vcpu->arch.host_debugctl !=3D svm->vmcb->save.dbgctl) update_debugctlmsr(vcpu->arch.host_debugctl); =20 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f66e5c8565aad..304328c33e960 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -172,7 +172,7 @@ struct vmcb_ctrl_area_cached { u32 event_inj_err; u64 next_rip; u64 nested_cr3; - u64 virt_ext; + u64 misc_ctl2; u32 clean; u64 bus_lock_rip; union { diff --git a/tools/testing/selftests/kvm/include/x86/svm.h b/tools/testing/= selftests/kvm/include/x86/svm.h index d81d8a9f5bfb6..c8539166270ea 100644 --- a/tools/testing/selftests/kvm/include/x86/svm.h +++ b/tools/testing/selftests/kvm/include/x86/svm.h @@ -103,7 +103,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area { u32 event_inj; u32 event_inj_err; u64 nested_cr3; - u64 virt_ext; + u64 misc_ctl2; u32 clean; u32 reserved_5; u64 next_rip; @@ -155,9 +155,6 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define AVIC_ENABLE_SHIFT 31 #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT) =20 -#define LBR_CTL_ENABLE_MASK BIT_ULL(0) -#define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1) - #define SVM_INTERRUPT_SHADOW_MASK 1 =20 #define SVM_IOIO_STR_SHIFT 2 @@ -178,6 +175,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_MISC_ENABLE_NP BIT(0) #define SVM_MISC_ENABLE_SEV BIT(1) =20 +#define SVM_MISC2_ENABLE_V_LBR BIT_ULL(0) +#define SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE BIT_ULL(1) + struct __attribute__ ((__packed__)) vmcb_seg { u16 selector; u16 attrib; diff --git a/tools/testing/selftests/kvm/x86/nested_vmsave_vmload_test.c b/= tools/testing/selftests/kvm/x86/nested_vmsave_vmload_test.c index 6764a48f9d4d9..71717118d6924 100644 --- a/tools/testing/selftests/kvm/x86/nested_vmsave_vmload_test.c +++ b/tools/testing/selftests/kvm/x86/nested_vmsave_vmload_test.c @@ -79,8 +79,8 @@ static void l1_guest_code(struct svm_test_data *svm) svm->vmcb->control.intercept |=3D (BIT_ULL(INTERCEPT_VMSAVE) | BIT_ULL(INTERCEPT_VMLOAD)); =20 - /* ..VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK cleared.. */ - svm->vmcb->control.virt_ext &=3D ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; + /* ..SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE cleared.. */ + svm->vmcb->control.misc_ctl2 &=3D ~SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE; =20 svm->vmcb->save.rip =3D (u64)l2_guest_code_vmsave; run_guest(svm->vmcb, svm->vmcb_gpa); @@ -90,8 +90,8 @@ static void l1_guest_code(struct svm_test_data *svm) run_guest(svm->vmcb, svm->vmcb_gpa); GUEST_ASSERT_EQ(svm->vmcb->control.exit_code, SVM_EXIT_VMLOAD); =20 - /* ..and VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK set */ - svm->vmcb->control.virt_ext |=3D VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; + /* ..and SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE set */ + svm->vmcb->control.misc_ctl2 |=3D SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE; =20 svm->vmcb->save.rip =3D (u64)l2_guest_code_vmsave; run_guest(svm->vmcb, svm->vmcb_gpa); @@ -106,20 +106,20 @@ static void l1_guest_code(struct svm_test_data *svm) BIT_ULL(INTERCEPT_VMLOAD)); =20 /* - * Without VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK, the GPA will be + * Without SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE, the GPA will be * interpreted as an L1 GPA, so VMCB0 should be used. */ svm->vmcb->save.rip =3D (u64)l2_guest_code_vmcb0; - svm->vmcb->control.virt_ext &=3D ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; + svm->vmcb->control.misc_ctl2 &=3D ~SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE; run_guest(svm->vmcb, svm->vmcb_gpa); GUEST_ASSERT_EQ(svm->vmcb->control.exit_code, SVM_EXIT_VMMCALL); =20 /* - * With VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK, the GPA will be interpeted as + * With SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE, the GPA will be interpeted as * an L2 GPA, and translated through the NPT to VMCB1. */ svm->vmcb->save.rip =3D (u64)l2_guest_code_vmcb1; - svm->vmcb->control.virt_ext |=3D VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; + svm->vmcb->control.misc_ctl2 |=3D SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE; run_guest(svm->vmcb, svm->vmcb_gpa); GUEST_ASSERT_EQ(svm->vmcb->control.exit_code, SVM_EXIT_VMMCALL); =20 diff --git a/tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c b/tools= /testing/selftests/kvm/x86/svm_lbr_nested_state.c index bf16abb1152e0..ff99438824d3a 100644 --- a/tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c +++ b/tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c @@ -69,9 +69,9 @@ static void l1_guest_code(struct svm_test_data *svm, bool= nested_lbrv) &l2_guest_stack[L2_GUEST_STACK_SIZE]); =20 if (nested_lbrv) - vmcb->control.virt_ext =3D LBR_CTL_ENABLE_MASK; + vmcb->control.misc_ctl2 =3D SVM_MISC2_ENABLE_V_LBR; else - vmcb->control.virt_ext &=3D ~LBR_CTL_ENABLE_MASK; + vmcb->control.misc_ctl2 &=3D ~SVM_MISC2_ENABLE_V_LBR; =20 run_guest(vmcb, svm->vmcb_gpa); GUEST_ASSERT(svm->vmcb->control.exit_code =3D=3D SVM_EXIT_VMMCALL); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 840953ACA40; Tue, 24 Feb 2026 22:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972471; cv=none; b=XTFpUMnWKWN18YEBA6+ehjGiBJVxxFALBRZ8rhujnzhQmYy0XY0ZWoxLHdwsEZLmY6B5ZByaM0XNH7liQbEIKdttFPE4GjRTEorwpFWWiyzevTMF6sjDeXIrzj0g0PXQf9kgUcBshDscR0620MklEPlw/j3W4zcvFHZkcsLW6+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972471; c=relaxed/simple; bh=W95vuyyK51TUcF2c4SwML0MoPJBVe5Wza26pH9cvE7s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T0KZ4Km70Vi+NFoDWvfTLPEc/kmITv9b9Mxsv52LJ8jRzC4rho0hvqOlBC4WuhuJr+4BzrLuCuPCEC7RWCudvcSdWzzWPBSjRlsO8pIFPC4JpezQp0pf9efzK+5xx1Q/k8/G0Z6Y/qKhhf/T740MjTEZzJk7UEYDaGJu41ouqH0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bK912UjU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bK912UjU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 34479C19423; Tue, 24 Feb 2026 22:34:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972471; bh=W95vuyyK51TUcF2c4SwML0MoPJBVe5Wza26pH9cvE7s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bK912UjUsLOl77V97YDE/U/oXQHTykNB+V1vLT69jklS9xEi6W4mRkxVCL+5RZcXY 9s6UYDCM4XzEFDFY8DE3lhFUMCn68JWcvXJgVwXtKZ3tQ+40GN3Owjb/7zXiE9dwlu 7i7g1Ta5YGlgOe+Um8N+SnJkmS9BdKAHcNZkkLM6zG8pVDeB7sHDil9Af0oqFWglO8 n0iCxzpjlM4fE5kqhGRRX2K1uTSR9eTKMwqYq540RczFpZke8cMg393QKhX9CR33SN 5DJOeZiaD4l0lg0SCEnHoE/ujXj03O4DfdGZ44YwCAMooewkyM467/St4K5ydnCeLu AFnYjat/DeESg== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v6 25/31] KVM: nSVM: Cache all used fields from VMCB12 Date: Tue, 24 Feb 2026 22:33:59 +0000 Message-ID: <20260224223405.3270433-26-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, most fields used from VMCB12 are cached in svm->nested.{ctl/save}. This is mainly to avoid TOC-TOU bugs. However, for the save area, only the fields used in the consistency checks (i.e. nested_vmcb_check_save()) were being cached. Other fields are read directly from guest memory in nested_vmcb02_prepare_save(). While probably benign, this still makes it possible for TOC-TOU bugs to happen. For example, RAX, RSP, and RIP are read twice, once to store in VMCB02, and once to store in vcpu->arch.regs. It is possible for the guest to modify the value between both reads, potentially causing nasty bugs. Harden against such bugs by caching everything in svm->nested.save. Cache all the needed fields, and keep all accesses to the VMCB12 strictly in nested_svm_vmrun() for caching and early error injection. Following changes will further limit the access to the VMCB12 in the nested VMRUN path. Introduce vmcb12_is_dirty() to use with the cached control fields instead of vmcb_is_dirty(), similar to vmcb12_is_intercept(). Opportunistically order the copies in __nested_copy_vmcb_save_to_cache() by the order in which the fields are defined in struct vmcb_save_area. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 116 ++++++++++++++++++++++---------------- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/svm/svm.h | 27 ++++++++- 3 files changed, 93 insertions(+), 52 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 679ac9f6dfe80..2159f5fbfc314 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -544,19 +544,34 @@ void nested_copy_vmcb_control_to_cache(struct vcpu_sv= m *svm, static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached = *to, struct vmcb_save_area *from) { - /* - * Copy only fields that are validated, as we need them - * to avoid TOC/TOU races. - */ + to->es =3D from->es; to->cs =3D from->cs; + to->ss =3D from->ss; + to->ds =3D from->ds; + to->gdtr =3D from->gdtr; + to->idtr =3D from->idtr; + + to->cpl =3D from->cpl; =20 to->efer =3D from->efer; - to->cr0 =3D from->cr0; - to->cr3 =3D from->cr3; to->cr4 =3D from->cr4; - - to->dr6 =3D from->dr6; + to->cr3 =3D from->cr3; + to->cr0 =3D from->cr0; to->dr7 =3D from->dr7; + to->dr6 =3D from->dr6; + + to->rflags =3D from->rflags; + to->rip =3D from->rip; + to->rsp =3D from->rsp; + + to->s_cet =3D from->s_cet; + to->ssp =3D from->ssp; + to->isst_addr =3D from->isst_addr; + + to->rax =3D from->rax; + to->cr2 =3D from->cr2; + + svm_copy_lbrs(to, from); } =20 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm, @@ -698,8 +713,10 @@ static bool nested_vmcb12_has_lbrv(struct kvm_vcpu *vc= pu) (to_svm(vcpu)->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_LBR); } =20 -static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *= vmcb12) +static void nested_vmcb02_prepare_save(struct vcpu_svm *svm) { + struct vmcb_ctrl_area_cached *control =3D &svm->nested.ctl; + struct vmcb_save_area_cached *save =3D &svm->nested.save; bool new_vmcb12 =3D false; struct vmcb *vmcb01 =3D svm->vmcb01.ptr; struct vmcb *vmcb02 =3D svm->nested.vmcb02.ptr; @@ -715,48 +732,48 @@ static void nested_vmcb02_prepare_save(struct vcpu_sv= m *svm, struct vmcb *vmcb12 svm->nested.force_msr_bitmap_recalc =3D true; } =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; - vmcb02->save.ss =3D vmcb12->save.ss; - vmcb02->save.ds =3D vmcb12->save.ds; - vmcb02->save.cpl =3D vmcb12->save.cpl; + if (unlikely(new_vmcb12 || vmcb12_is_dirty(control, VMCB_SEG))) { + vmcb02->save.es =3D save->es; + vmcb02->save.cs =3D save->cs; + vmcb02->save.ss =3D save->ss; + vmcb02->save.ds =3D save->ds; + vmcb02->save.cpl =3D save->cpl; vmcb_mark_dirty(vmcb02, VMCB_SEG); } =20 - if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) { - vmcb02->save.gdtr =3D vmcb12->save.gdtr; - vmcb02->save.idtr =3D vmcb12->save.idtr; + if (unlikely(new_vmcb12 || vmcb12_is_dirty(control, VMCB_DT))) { + vmcb02->save.gdtr =3D save->gdtr; + vmcb02->save.idtr =3D save->idtr; vmcb_mark_dirty(vmcb02, VMCB_DT); } =20 if (guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK) && - (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_CET)))) { - vmcb02->save.s_cet =3D vmcb12->save.s_cet; - vmcb02->save.isst_addr =3D vmcb12->save.isst_addr; - vmcb02->save.ssp =3D vmcb12->save.ssp; + (unlikely(new_vmcb12 || vmcb12_is_dirty(control, VMCB_CET)))) { + vmcb02->save.s_cet =3D save->s_cet; + vmcb02->save.isst_addr =3D save->isst_addr; + vmcb02->save.ssp =3D save->ssp; vmcb_mark_dirty(vmcb02, VMCB_CET); } =20 - kvm_set_rflags(vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED); + kvm_set_rflags(vcpu, save->rflags | X86_EFLAGS_FIXED); =20 svm_set_efer(vcpu, svm->nested.save.efer); =20 svm_set_cr0(vcpu, svm->nested.save.cr0); svm_set_cr4(vcpu, svm->nested.save.cr4); =20 - svm->vcpu.arch.cr2 =3D vmcb12->save.cr2; + svm->vcpu.arch.cr2 =3D save->cr2; =20 - kvm_rax_write(vcpu, vmcb12->save.rax); - kvm_rsp_write(vcpu, vmcb12->save.rsp); - kvm_rip_write(vcpu, vmcb12->save.rip); + kvm_rax_write(vcpu, save->rax); + kvm_rsp_write(vcpu, save->rsp); + kvm_rip_write(vcpu, save->rip); =20 /* In case we don't even reach vcpu_run, the fields are not updated */ - vmcb02->save.rax =3D vmcb12->save.rax; - vmcb02->save.rsp =3D vmcb12->save.rsp; - vmcb02->save.rip =3D vmcb12->save.rip; + vmcb02->save.rax =3D save->rax; + vmcb02->save.rsp =3D save->rsp; + vmcb02->save.rip =3D save->rip; =20 - if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) { + if (unlikely(new_vmcb12 || vmcb12_is_dirty(control, VMCB_DR))) { vmcb02->save.dr7 =3D svm->nested.save.dr7 | DR7_FIXED_1; svm->vcpu.arch.dr6 =3D svm->nested.save.dr6 | DR6_ACTIVE_LOW; vmcb_mark_dirty(vmcb02, VMCB_DR); @@ -767,7 +784,7 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm = *svm, struct vmcb *vmcb12 * Reserved bits of DEBUGCTL are ignored. Be consistent with * svm_set_msr's definition of reserved bits. */ - svm_copy_lbrs(&vmcb02->save, &vmcb12->save); + svm_copy_lbrs(&vmcb02->save, save); vmcb02->save.dbgctl &=3D ~DEBUGCTL_RESERVED_BITS; } else { svm_copy_lbrs(&vmcb02->save, &vmcb01->save); @@ -983,28 +1000,29 @@ static void nested_svm_copy_common_state(struct vmcb= *from_vmcb, struct vmcb *to to_vmcb->save.spec_ctrl =3D from_vmcb->save.spec_ctrl; } =20 -int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, - struct vmcb *vmcb12, bool from_vmrun) +int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_= vmrun) { struct vcpu_svm *svm =3D to_svm(vcpu); + struct vmcb_ctrl_area_cached *control =3D &svm->nested.ctl; + struct vmcb_save_area_cached *save =3D &svm->nested.save; int ret; =20 trace_kvm_nested_vmenter(svm->vmcb->save.rip, vmcb12_gpa, - vmcb12->save.rip, - vmcb12->control.int_ctl, - vmcb12->control.event_inj, - vmcb12->control.misc_ctl, - vmcb12->control.nested_cr3, - vmcb12->save.cr3, + save->rip, + control->int_ctl, + control->event_inj, + control->misc_ctl, + control->nested_cr3, + save->cr3, KVM_ISA_SVM); =20 - trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0x= ffff, - vmcb12->control.intercepts[INTERCEPT_CR] >> 16, - vmcb12->control.intercepts[INTERCEPT_EXCEPTION], - vmcb12->control.intercepts[INTERCEPT_WORD3], - vmcb12->control.intercepts[INTERCEPT_WORD4], - vmcb12->control.intercepts[INTERCEPT_WORD5]); + trace_kvm_nested_intercepts(control->intercepts[INTERCEPT_CR] & 0xffff, + control->intercepts[INTERCEPT_CR] >> 16, + control->intercepts[INTERCEPT_EXCEPTION], + control->intercepts[INTERCEPT_WORD3], + control->intercepts[INTERCEPT_WORD4], + control->intercepts[INTERCEPT_WORD5]); =20 svm->nested.vmcb12_gpa =3D vmcb12_gpa; =20 @@ -1039,8 +1057,8 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 v= mcb12_gpa, nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr); =20 svm_switch_vmcb(svm, &svm->nested.vmcb02); - nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base= ); - nested_vmcb02_prepare_save(svm, vmcb12); + nested_vmcb02_prepare_control(svm, save->rip, save->cs.base); + nested_vmcb02_prepare_save(svm); =20 if (!from_vmrun) kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); @@ -1157,7 +1175,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) =20 svm->nested.nested_run_pending =3D 1; =20 - if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true)) { + if (enter_svm_guest_mode(vcpu, vmcb12_gpa, true)) { svm->nested.nested_run_pending =3D 0; svm->nmi_l1_to_l2 =3D false; svm->soft_int_injected =3D false; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 94e14badddfa2..19112ec48c0f7 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4885,7 +4885,7 @@ static int svm_leave_smm(struct kvm_vcpu *vcpu, const= union kvm_smram *smram) vmcb12 =3D map.hva; nested_copy_vmcb_control_to_cache(svm, &vmcb12->control); nested_copy_vmcb_save_to_cache(svm, &vmcb12->save); - ret =3D enter_svm_guest_mode(vcpu, smram64->svm_guest_vmcb_gpa, vmcb12, f= alse); + ret =3D enter_svm_guest_mode(vcpu, smram64->svm_guest_vmcb_gpa, false); =20 if (ret) goto unmap_save; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 304328c33e960..388aaa5d63d29 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -140,13 +140,32 @@ struct kvm_vmcb_info { }; =20 struct vmcb_save_area_cached { + struct vmcb_seg es; struct vmcb_seg cs; + struct vmcb_seg ss; + struct vmcb_seg ds; + struct vmcb_seg gdtr; + struct vmcb_seg idtr; + u8 cpl; u64 efer; u64 cr4; u64 cr3; u64 cr0; u64 dr7; u64 dr6; + u64 rflags; + u64 rip; + u64 rsp; + u64 s_cet; + u64 ssp; + u64 isst_addr; + u64 rax; + u64 cr2; + u64 dbgctl; + u64 br_from; + u64 br_to; + u64 last_excp_from; + u64 last_excp_to; }; =20 struct vmcb_ctrl_area_cached { @@ -421,6 +440,11 @@ static inline bool vmcb_is_dirty(struct vmcb *vmcb, in= t bit) return !test_bit(bit, (unsigned long *)&vmcb->control.clean); } =20 +static inline bool vmcb12_is_dirty(struct vmcb_ctrl_area_cached *control, = int bit) +{ + return !test_bit(bit, (unsigned long *)&control->clean); +} + static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) { return container_of(vcpu, struct vcpu_svm, vcpu); @@ -785,8 +809,7 @@ static inline bool nested_exit_on_nmi(struct vcpu_svm *= svm) =20 int __init nested_svm_init_msrpm_merge_offsets(void); =20 -int enter_svm_guest_mode(struct kvm_vcpu *vcpu, - u64 vmcb_gpa, struct vmcb *vmcb12, bool from_vmrun); +int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb_gpa, bool from_vm= run); void svm_leave_nested(struct kvm_vcpu *vcpu); void svm_free_nested(struct vcpu_svm *svm); int svm_allocate_nested(struct vcpu_svm *svm); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2E7BC3563E9; Tue, 24 Feb 2026 22:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972472; cv=none; b=fddsm6BihHMbtEGxARVXy+1JAVd6mZkPdWaIfQd8oMV0d7XA6uvlH9AiGVqm5ZW77FZOMtz5deV+Si6WOcod46hsY/hB/asH8oqqyT93vKOH5MmuMxkb+9NQYtnX+1+lUOxJmwsAHgVP9c2rNw6tmdmbMjFF0wI8UT5rRvE/MyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972472; c=relaxed/simple; bh=FPAWkNTV8oYTTiMhCjkNFMa3V2d8Jopr+IUsm+6qo+8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tLmeNokA4pCftxYCj45foIHSLf8ucIIcTqzfGxYVLblN8yP/hg/8xP4oz4DbRYSWV9Aa7fXZcwKt38jv2lo7R3nOVFiP0HTfc4UHuTy/bHzf8WC+69qKWji6QQuM177I22oFihehDiZnu+8cT7cm3e9456jbwrapl0aTbw2LZQ4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kzFTn4Kg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kzFTn4Kg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E263C2BC87; Tue, 24 Feb 2026 22:34:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972471; bh=FPAWkNTV8oYTTiMhCjkNFMa3V2d8Jopr+IUsm+6qo+8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kzFTn4KgmgfdfsfZb5K8iq28A0LQkl7c1ptre6wb+rMtIdVia7pvbcnJrR3Y+9AqP IcbKzv5uU18XERZQ6+jTUBNeIIKP/F6QyZ2Tw3HG1cdV669DHE11HkUpXHZ+a2ZKOq jzbzxVjyqgk3jqsv9BjoFc4r7Zrgk7BuCKH+Ak9QoXSwb73XxraaE+q72LPCOZwj2+ B5GJ3h7nUufZKmgN14Bvvvx/Z06Q0GNGf1M24uIHGyV3j3Z/2OEsu5N8exXMAjMMiv uTaKaH9HGN/sceoPlkQV7N1ZqkfgCTvy8pscLZ5XAZVGKE6IPJGw2M+isvDxBRCHTu 2l7jKDIHxhJNg== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v6 26/31] KVM: nSVM: Restrict mapping VMCB12 on nested VMRUN Date: Tue, 24 Feb 2026 22:34:00 +0000 Message-ID: <20260224223405.3270433-27-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" All accesses to the VMCB12 in the guest memory on nested VMRUN are limited to nested_svm_vmrun() and nested_svm_vmrun_error_vmexit(). However, the VMCB12 remains mapped throughout nested_svm_vmrun(). Mapping and unmapping around usages is possible, but it becomes easy-ish to introduce bugs where 'vmcb12' is used after being unmapped. Move reading the VMCB12 and copying to cache from nested_svm_vmrun() into a new helper, nested_svm_copy_vmcb12_to_cache(), that maps the VMCB12, caches the needed fields, and unmaps it. Use kvm_vcpu_map_readonly() as only reading the VMCB12 is needed. Similarly, move mapping the VMCB12 on VMRUN failure into nested_svm_vmrun_error_vmexit(). Inject a triple fault if the mapping fails, similar to nested_svm_vmexit(). Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 49 +++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 2159f5fbfc314..5c8449bc6fa0c 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1102,28 +1102,54 @@ static void __nested_svm_vmexit(struct vcpu_svm *sv= m) kvm_queue_exception(vcpu, DB_VECTOR); } =20 -static void nested_svm_vmrun_error_vmexit(struct kvm_vcpu *vcpu, struct vm= cb *vmcb12) +static void nested_svm_vmrun_error_vmexit(struct kvm_vcpu *vcpu, u64 vmcb1= 2_gpa) { struct vcpu_svm *svm =3D to_svm(vcpu); + struct kvm_host_map map; + struct vmcb *vmcb12; =20 WARN_ON_ONCE(svm->vmcb =3D=3D svm->nested.vmcb02.ptr); =20 leave_guest_mode(vcpu); =20 + if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map)) { + kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); + goto out; + } + + vmcb12 =3D map.hva; vmcb12->control.exit_code =3D SVM_EXIT_ERR; vmcb12->control.exit_info_1 =3D 0; vmcb12->control.exit_info_2 =3D 0; vmcb12->control.event_inj =3D 0; vmcb12->control.event_inj_err =3D 0; + kvm_vcpu_unmap(vcpu, &map); +out: __nested_svm_vmexit(svm); } =20 +static int nested_svm_copy_vmcb12_to_cache(struct kvm_vcpu *vcpu, u64 vmcb= 12_gpa) +{ + struct vcpu_svm *svm =3D to_svm(vcpu); + struct kvm_host_map map; + struct vmcb *vmcb12; + int r; + + r =3D kvm_vcpu_map_readonly(vcpu, gpa_to_gfn(vmcb12_gpa), &map); + if (r) + return r; + + vmcb12 =3D map.hva; + nested_copy_vmcb_control_to_cache(svm, &vmcb12->control); + nested_copy_vmcb_save_to_cache(svm, &vmcb12->save); + kvm_vcpu_unmap(vcpu, &map); + return 0; +} + int nested_svm_vmrun(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); int ret; - struct vmcb *vmcb12; - struct kvm_host_map map; u64 vmcb12_gpa; struct vmcb *vmcb01 =3D svm->vmcb01.ptr; =20 @@ -1144,22 +1170,17 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) return ret; } =20 + if (WARN_ON_ONCE(!svm->nested.initialized)) + return -EINVAL; + vmcb12_gpa =3D svm->vmcb->save.rax; - if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map)) { + if (nested_svm_copy_vmcb12_to_cache(vcpu, vmcb12_gpa)) { kvm_inject_gp(vcpu, 0); return 1; } =20 ret =3D kvm_skip_emulated_instruction(vcpu); =20 - vmcb12 =3D map.hva; - - if (WARN_ON_ONCE(!svm->nested.initialized)) - return -EINVAL; - - nested_copy_vmcb_control_to_cache(svm, &vmcb12->control); - nested_copy_vmcb_save_to_cache(svm, &vmcb12->save); - /* * Since vmcb01 is not in use, we can use it to store some of the L1 * state. @@ -1180,11 +1201,9 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) svm->nmi_l1_to_l2 =3D false; svm->soft_int_injected =3D false; =20 - nested_svm_vmrun_error_vmexit(vcpu, vmcb12); + nested_svm_vmrun_error_vmexit(vcpu, vmcb12_gpa); } =20 - kvm_vcpu_unmap(vcpu, &map); - return ret; } =20 --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4342C3ACF05; Tue, 24 Feb 2026 22:34:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972472; cv=none; b=qMpgpskemnBdBqVSjmnZN09uZkzlTaCdJiN4VluQMoixdA2DyX3A7v2eICS/5+OZMcQLQmXsugeZFPnkajZJafW/3SaTPDEdgUKRCi7tCspEH60Qb98qFFdT9mTW0yKITtc6HgUV322Qrj0fLP5/FRXxkBat6TxatNcp7g2NuaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972472; c=relaxed/simple; bh=IM+ni7R0tCySorWNqi7DDiY8uOysUtiVySdq/OQQOEY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HXc0M8qHwaNu4xpmS4abFMtWYQYsTRMSqtK1/GbtZcpq+elCih4UzYoEnATKYaPKJI6guXfKuStnQCQWPM0TUJSvQsTq8PJJSn9+khXSM5BOZfzBjEdtd8LDfPpDBhEx/uj918SSeiku3px2Sri9+CI1BMLNdXBZUbT/Yj8Mc5M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JxOqL00o; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JxOqL00o" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E74AEC19424; Tue, 24 Feb 2026 22:34:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972472; bh=IM+ni7R0tCySorWNqi7DDiY8uOysUtiVySdq/OQQOEY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JxOqL00osshPsWMWJ0Hmy+vZliDAPz50WQXbux9eBjvkslA+M7zIB/G6wbDcPVusO wLGWLzrtXMsaOE/Mcvg0i+MamV6JeIF4IXGM86TDwrQCBfC4faONfE9lWo8E4UIjak hvclrD+g02atVzT5qCedHvEaWLT8Cjjz5UF4juKhmBw67XkXXGmi+QT+JOuZE9Fgk9 ihJuHoND3CXvRqoFTAGt0E+auTHqDp2UJlVJfKokRHm8A2DKPdZRwFO158l4//2H4f zusU95LvGC20HjBSs9aSCAcTAL2Y9uzbWz4R29kyR0QZpypaUadV4cP9WdSKvz0WUK o36Ok45oqm+Nw== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v6 27/31] KVM: nSVM: Use PAGE_MASK to drop lower bits of bitmap GPAs from vmcb12 Date: Tue, 24 Feb 2026 22:34:01 +0000 Message-ID: <20260224223405.3270433-28-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use PAGE_MASK to drop the lower bits from IOPM_BASE_PA and MSRPM_BASE_PA while copying them instead of dropping the bits afterward with a hardcoded mask. No functional change intended. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 5c8449bc6fa0c..28a8bfc632ef5 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -499,8 +499,8 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcp= u *vcpu, if (!guest_cpu_cap_has(vcpu, X86_FEATURE_NPT)) to->misc_ctl &=3D ~SVM_MISC_ENABLE_NP; =20 - to->iopm_base_pa =3D from->iopm_base_pa; - to->msrpm_base_pa =3D from->msrpm_base_pa; + to->iopm_base_pa =3D from->iopm_base_pa & PAGE_MASK; + to->msrpm_base_pa =3D from->msrpm_base_pa & PAGE_MASK; to->tsc_offset =3D from->tsc_offset; to->tlb_ctl =3D from->tlb_ctl; to->erap_ctl =3D from->erap_ctl; @@ -522,8 +522,6 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcp= u *vcpu, =20 /* Copy asid here because nested_vmcb_check_controls() will check it */ to->asid =3D from->asid; - to->msrpm_base_pa &=3D ~0x0fffULL; - to->iopm_base_pa &=3D ~0x0fffULL; =20 #ifdef CONFIG_KVM_HYPERV /* Hyper-V extensions (Enlightened VMCB) */ --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9ED793AE6E2; Tue, 24 Feb 2026 22:34:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972472; cv=none; b=s3dJm5j/tb8hqdbhIYr39Dxv+TuQkEzKN5/zSXdyg8iUQOt+vx3GeteDpWiI3tq2GzEQqWuuHQVy3acx0/b5Kc5S/0azhPNZco3oWuLOAbS4sUMBF+67dTZ0Yf+BnObMlRklt+rQkjN4qXkcSNHnMfFZDjjctd8ZuLlS0ksSxus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972472; c=relaxed/simple; bh=iE/e2/DcJolfWaFpaNXrD2k53hM94zPMsuIvNRn7BlY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LvQyvfTKzyPQnqzCPYzPjhuAAHVqUyuODvrqcjZDxsiPlrGn87gI+dvSsU+9kW/f+hJN1jfuR/mI/53zIu0eIjdRZkPdR3ywspLwplLFfMTdUEHmIujIDVQu/E8+YiN1NL/51izmGbTYGsqhjKmr3yWk786GYbHzEaIiMmzQOH0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HgsKovIz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HgsKovIz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D0B5C19423; Tue, 24 Feb 2026 22:34:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972472; bh=iE/e2/DcJolfWaFpaNXrD2k53hM94zPMsuIvNRn7BlY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HgsKovIzcan0SWOAK88n1VBFRnDb2G98wYW+sTFWGcPvBJzchk3i8u95BRpUw7+k3 QJv1Io6Sa0Kob0ucd2D7Wpf0tTvkUXmui7lNBT58y8ylf8v2EgRkAN+bwnOgHVTQBc HEX2mj2dQAV0QbZujAXOk91SHhcb7xwRJLTfL79aQlHB/aOKh3YUKzzdvGaR44ZxH2 yBMDxtpi+SSCkwg26xZZ1LWuSg2b50WMwTqGuJSHJITqjmr+VAIlU/YMtWui3M6ceh 96PV5bk8XZIJo1lMYc6qvsnRkTezhkOVCW51d3KhlC/R9KKK7w69YTfHdsFNQSw3Ke 4efM30TsITnDg== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v6 28/31] KVM: nSVM: Sanitize TLB_CONTROL field when copying from vmcb12 Date: Tue, 24 Feb 2026 22:34:02 +0000 Message-ID: <20260224223405.3270433-29-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The APM defines possible values for TLB_CONTROL as 0, 1, 3, and 7 -- all of which are always allowed for KVM guests as KVM always supports X86_FEATURE_FLUSHBYASID. Only copy bits 0 to 2 from vmcb12's TLB_CONTROL, such that no unhandled or reserved bits end up in vmcb02. Note that TLB_CONTROL in vmcb12 is currently ignored by KVM, as it nukes the TLB on nested transitions anyway (see nested_svm_transition_tlb_flush()). However, such sanitization will be needed once the TODOs there are addressed, and it's minimal churn to add it now. Signed-off-by: Yosry Ahmed --- arch/x86/include/asm/svm.h | 2 ++ arch/x86/kvm/svm/nested.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index c169256c415fb..16cf4f435aebd 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -182,6 +182,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define TLB_CONTROL_FLUSH_ASID 3 #define TLB_CONTROL_FLUSH_ASID_LOCAL 7 =20 +#define TLB_CONTROL_MASK GENMASK(2, 0) + #define ERAP_CONTROL_ALLOW_LARGER_RAP BIT(0) #define ERAP_CONTROL_CLEAR_RAP BIT(1) =20 diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 28a8bfc632ef5..d7c353ac42d88 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -502,7 +502,7 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcp= u *vcpu, to->iopm_base_pa =3D from->iopm_base_pa & PAGE_MASK; to->msrpm_base_pa =3D from->msrpm_base_pa & PAGE_MASK; to->tsc_offset =3D from->tsc_offset; - to->tlb_ctl =3D from->tlb_ctl; + to->tlb_ctl =3D from->tlb_ctl & TLB_CONTROL_MASK; to->erap_ctl =3D from->erap_ctl; to->int_ctl =3D from->int_ctl; to->int_vector =3D from->int_vector; --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 320A83AE70F; Tue, 24 Feb 2026 22:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972473; cv=none; b=LH739MLVnC6HCYyCjM/GQXPEKaNhQMRKsfdd4VS3QyUzyGEjcPMUlNs2x86O1MAdKqCFaO0B2OUEjNfKbNhHT6d3ZmN7ARLW3B/6B0Bq1U2dz2HrTDGEjcWPOrMto1RNjXUeRuMURK1yHLbnz4HJ27VlS1905W7o2Aba5h/XqrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972473; c=relaxed/simple; bh=RrqiU/db4McrUKSt3VpYR/YqxtlOXYT7mo6zTzAkg7Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sNdhY6SqHOM//y7j1bHhKR+n0QHwp2qv5afHb+fcvEkK/+5GgB4lgAGAApU/BVbWyOoR/hx33BoD0IUwkmg6ntcxyHjiJGZ8TgM5lgC9/TjOm/gE2iceUczqj6GX0lwYsDr88zvMeGRAr6Y1BJuRA1oGLElAyntW0gIYSDi74Hc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Fp2duO4U; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Fp2duO4U" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A7091C19424; Tue, 24 Feb 2026 22:34:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972472; bh=RrqiU/db4McrUKSt3VpYR/YqxtlOXYT7mo6zTzAkg7Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fp2duO4Ukx+HxEjZxHeBSPdyP4r+bgkBknl8mKXsyNsnwEYsCcPbIYa6wkP769x5U pn7R/IvFwVA9m/TKP6nIOmDCXklH225+MwxRyczA4c6dUUbkNDSNJOwr27bef1bwP9 c7EoTQnrBToYKrYv7h06w4drdA1f8KTH0hxIUvnuduW0qrseb1u7IPHg8ItDe+Hcr/ 4yy6UvoybyvkgVSEzNRArShSsNOq8oZnYJY5E0f4Qb5cZv19TjxXtcRu74dbTxd1+y 2qdfeJ5FE0/CQBxuPcp08jf4Y/46cg6LUevYjKMg96qNR7x8h1LkH4UBhV+ZhUa2UK UjFujrR+JCaEA== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , Jim Mattson Subject: [PATCH v6 29/31] KVM: nSVM: Sanitize INT/EVENTINJ fields when copying from vmcb12 Date: Tue, 24 Feb 2026 22:34:03 +0000 Message-ID: <20260224223405.3270433-30-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Make sure all fields used from vmcb12 in creating the vmcb02 are sanitized, such that no unhandled or reserved bits end up in the vmcb02. The following control fields are read from vmcb12 and have bits that are either reserved or not handled/advertised by KVM: tlb_ctl, int_ctl, int_state, int_vector, event_inj, misc_ctl, and misc_ctl2. The following fields do not require any extra sanitizing: - tlb_ctl: already being sanitized. - int_ctl: bits from vmcb12 are copied bit-by-bit as needed. - misc_ctl: only used in consistency checks (particularly NP_ENABLE). - misc_ctl2: bits from vmcb12 are copied bit-by-bit as needed. For the remaining fields (int_vector, int_state, and event_inj), make sure only defined bits are copied from L1's vmcb12 into KVM'cache by defining appropriate masks where needed. Suggested-by: Jim Mattson Signed-off-by: Yosry Ahmed --- arch/x86/include/asm/svm.h | 5 +++++ arch/x86/kvm/svm/nested.c | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 16cf4f435aebd..bcfeb5e7c0edf 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -224,6 +224,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define X2APIC_MODE_SHIFT 30 #define X2APIC_MODE_MASK (1 << X2APIC_MODE_SHIFT) =20 +#define SVM_INT_VECTOR_MASK GENMASK(7, 0) + #define SVM_INTERRUPT_SHADOW_MASK BIT_ULL(0) #define SVM_GUEST_INTERRUPT_MASK BIT_ULL(1) =20 @@ -637,6 +639,9 @@ static inline void __unused_size_checks(void) #define SVM_EVTINJ_VALID (1 << 31) #define SVM_EVTINJ_VALID_ERR (1 << 11) =20 +#define SVM_EVTINJ_RESERVED_BITS ~(SVM_EVTINJ_VEC_MASK | SVM_EVTINJ_TYPE_M= ASK | \ + SVM_EVTINJ_VALID_ERR | SVM_EVTINJ_VALID) + #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK =20 diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index d7c353ac42d88..6dd31ed2bed5e 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -505,18 +505,18 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_v= cpu *vcpu, to->tlb_ctl =3D from->tlb_ctl & TLB_CONTROL_MASK; to->erap_ctl =3D from->erap_ctl; to->int_ctl =3D from->int_ctl; - to->int_vector =3D from->int_vector; - to->int_state =3D from->int_state; + to->int_vector =3D from->int_vector & SVM_INT_VECTOR_MASK; + to->int_state =3D from->int_state & SVM_INTERRUPT_SHADOW_MASK; to->exit_code =3D from->exit_code; to->exit_info_1 =3D from->exit_info_1; to->exit_info_2 =3D from->exit_info_2; to->exit_int_info =3D from->exit_int_info; to->exit_int_info_err =3D from->exit_int_info_err; - to->event_inj =3D from->event_inj; + to->event_inj =3D from->event_inj & ~SVM_EVTINJ_RESERVED_BITS; to->event_inj_err =3D from->event_inj_err; to->next_rip =3D from->next_rip; to->nested_cr3 =3D from->nested_cr3; - to->misc_ctl2 =3D from->misc_ctl2; + to->misc_ctl2 =3D from->misc_ctl2; to->pause_filter_count =3D from->pause_filter_count; to->pause_filter_thresh =3D from->pause_filter_thresh; =20 --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6C9343AE71F; Tue, 24 Feb 2026 22:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972473; cv=none; b=CtmxmgWcCLg1J6Xfk2qZnuKdXhgHZV0ICK5EJry7BcLHnzPgjYHs0WkI0Sy/Xep/1HQHnDwmJ2ru/DwnCBOSlUc9C3P5iOG04MlQ5LvoT9Bo9vLyucc/MN42X9+56zDFer6QAM7iVQ2sTNFgQQZeNj1s7Nudz9YJj4AhsdCELqA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972473; c=relaxed/simple; bh=RBpKxUYlziCfgALtI3kgPDKPT9IypbTvVuebfGkC5nI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N0WqSDhxuOLJ/oH1bBEz3Bi2O8GlEQYG2hM5gHmWFgHM+lipCVf+zu1jfVWoT0WDsd2Quc8IgCoC0lbPHR+aaJ/Mwt89PAgvXUMVay3pQ0tCVH/umWw4w/cv9W6/GHTsb55xWiEYXyI0jpEMySzE5CpkfqWJ6pkiQDxdNlXMH3I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m6iNaEbp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="m6iNaEbp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1533DC19423; Tue, 24 Feb 2026 22:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972473; bh=RBpKxUYlziCfgALtI3kgPDKPT9IypbTvVuebfGkC5nI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m6iNaEbpSWxB17i7feynMWeE5Id58o6QEqE+A+OnWKGdKHcy+9Jp2btVriIPbwEAa wLBYhfoTWeo2lHZ8FHWzE62/qwYkPAGqXyitgEzfTEwnR2lbvRn4OinmEYX/YUCGql JprvzYAsoO/8FX38aB5bCMiRucXTyuJ2ZCDJt4/+8yUAZ/XgYv07oahaSBcSD8Ixmt HpYQYkCf9YK9PfMn5RdwMoO8sSyPmUW7R75BTEitR9gZjaRxtbTf865KCc4ssnC8sL 00gCgEDehgGJwL6sZT+lbVUpt6Qw4voCJOZWHxQ2pP8KekU2g64QjCmOXnL+jArn5/ /k8Pewe/3MaqQ== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , Jim Mattson Subject: [PATCH v6 30/31] KVM: nSVM: Only copy SVM_MISC_ENABLE_NP from VMCB01's misc_ctl Date: Tue, 24 Feb 2026 22:34:04 +0000 Message-ID: <20260224223405.3270433-31-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The 'misc_ctl' field in VMCB02 is taken as-is from VMCB01. However, the only bit that needs to copied is SVM_MISC_ENABLE_NP, as all other known bits in misc_ctl are related to SEV guests, and KVM doesn't support nested virtualization for SEV guests. Only copy SVM_MISC_ENABLE_NP to harden against future bugs if/when other bits are set for L1 but should not be set for L2. Opportunistically add a comment explaining why SVM_MISC_ENABLE_NP is taken from VMCB01 and not VMCB02. Suggested-by: Jim Mattson Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/nested.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 6dd31ed2bed5e..acc213e675ac7 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -852,8 +852,16 @@ static void nested_vmcb02_prepare_control(struct vcpu_= svm *svm, V_NMI_BLOCKING_MASK); } =20 - /* Copied from vmcb01. msrpm_base can be overwritten later. */ - vmcb02->control.misc_ctl =3D vmcb01->control.misc_ctl; + /* + * Copied from vmcb01. msrpm_base can be overwritten later. + * + * SVM_MISC_ENABLE_NP in vmcb12 is only used for consistency checks. If + * L1 enables NPTs, KVM shadows L1's NPTs and uses those to run L2. If + * L1 disables NPT, KVM runs L2 with the same NPTs used to run L1. For + * the latter, L1 runs L2 with shadow page tables that translate L2 GVAs + * to L1 GPAs, so the same NPTs can be used for L1 and L2. + */ + vmcb02->control.misc_ctl =3D vmcb01->control.misc_ctl & SVM_MISC_ENABLE_N= P; vmcb02->control.iopm_base_pa =3D vmcb01->control.iopm_base_pa; vmcb02->control.msrpm_base_pa =3D vmcb01->control.msrpm_base_pa; vmcb_mark_dirty(vmcb02, VMCB_PERM_MAP); --=20 2.53.0.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:53 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 CA7863AEF49; Tue, 24 Feb 2026 22:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972473; cv=none; b=ZOQIrG1zCDocezZMKL0iD3L0KfvaRzqQrv0djE8aliCCfHyQCjiaPF6iCRecbgSaF3NzWIFpDZ9x/oX4GN2q3wkCoearZ5ynWBUIlqXzi15L5sZWGefYgS4OdgawgOC9acOGHn8vqQoguDUZdUI0XBKH+yD6QSd6gWmzp1jdA4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771972473; c=relaxed/simple; bh=Azr0nTsvGAE8w6vk55HoET74YyhG7h/Gv57ZeCxeZk0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PpCw6sxmv9ggvj+2AmNngF7Jy9hz7gZR6J9MjpE1HamxV5qEIjKheQMMGDRMTRAfCWw0RYAu2iFlHK9DS0t3sKFyLVZ4iS5gAf8LyQOzvmoT3QmfAgGWsNgID4EswHeBFEWro/sgs264NePqM6kUEADS8c10YFL/OboldfCNDXI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZqvtB6eA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZqvtB6eA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77025C116D0; Tue, 24 Feb 2026 22:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771972473; bh=Azr0nTsvGAE8w6vk55HoET74YyhG7h/Gv57ZeCxeZk0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZqvtB6eA637PXWxwkxFweBsNrez2K55TAf60zVa1wCU9/koR/pB6bF0an6NMiVuB+ gcyu2A26Cdo16MJZLvTBM/fyqh8a6YjVbgHRrXU6x0OMh3Whue4P4V5dEYg+X8yawt VyNhBZnTkZfmjNUUIUwc0g3MZnCSK7TGYFIkqRjMN5sVwYO6+s1LMd8uFNJLOsVRys VT9svY7iSmf6QK0i8kxx/+k6mQXxtlebJhVi/gWDUElvo6zyMtMTTZcMq5a1xuDEnG 4a86eQ95WmI6UCUNMoCcNrrHXZCZ6pxykVHbCKqNhuepH6FBBusJTwQxGZyoDlR+LJ H3/aFvlujVu5A== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v6 31/31] KVM: selftest: Add a selftest for VMRUN/#VMEXIT with unmappable vmcb12 Date: Tue, 24 Feb 2026 22:34:05 +0000 Message-ID: <20260224223405.3270433-32-yosry@kernel.org> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <20260224223405.3270433-1-yosry@kernel.org> References: <20260224223405.3270433-1-yosry@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a test that verifies that KVM correctly injects a #GP for nested VMRUN and a shutdown for nested #VMEXIT, if the GPA of vmcb12 cannot be mapped. Signed-off-by: Yosry Ahmed --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/x86/svm_nested_invalid_vmcb12_gpa.c | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_invalid_vmcb= 12_gpa.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index 36b48e766e499..f12e7c17d379d 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -110,6 +110,7 @@ TEST_GEN_PROGS_x86 +=3D x86/state_test TEST_GEN_PROGS_x86 +=3D x86/vmx_preemption_timer_test TEST_GEN_PROGS_x86 +=3D x86/svm_vmcall_test TEST_GEN_PROGS_x86 +=3D x86/svm_int_ctl_test +TEST_GEN_PROGS_x86 +=3D x86/svm_nested_invalid_vmcb12_gpa TEST_GEN_PROGS_x86 +=3D x86/svm_nested_shutdown_test TEST_GEN_PROGS_x86 +=3D x86/svm_nested_soft_inject_test TEST_GEN_PROGS_x86 +=3D x86/svm_lbr_nested_state diff --git a/tools/testing/selftests/kvm/x86/svm_nested_invalid_vmcb12_gpa.= c b/tools/testing/selftests/kvm/x86/svm_nested_invalid_vmcb12_gpa.c new file mode 100644 index 0000000000000..5b0dec4240937 --- /dev/null +++ b/tools/testing/selftests/kvm/x86/svm_nested_invalid_vmcb12_gpa.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2026, Google LLC. + */ +#include "kvm_util.h" +#include "vmx.h" +#include "svm_util.h" +#include "kselftest.h" + + +#define L2_GUEST_STACK_SIZE 64 + +#define SYNC_GP 101 +#define SYNC_L2_STARTED 102 + +extern char invalid_vmrun; + +static void guest_gp_handler(struct ex_regs *regs) +{ + GUEST_SYNC(SYNC_GP); + regs->rip =3D (uintptr_t)&invalid_vmrun; +} + +static void l2_guest_code(void) +{ + GUEST_SYNC(SYNC_L2_STARTED); + vmcall(); +} + +static void l1_guest_code(struct svm_test_data *svm, u64 invalid_vmcb12_gp= a) +{ + unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; + + generic_svm_setup(svm, l2_guest_code, + &l2_guest_stack[L2_GUEST_STACK_SIZE]); + + run_guest(svm->vmcb, invalid_vmcb12_gpa); /* #GP */ + + /* GP handler should jump here */ + asm volatile ("invalid_vmrun:"); + run_guest(svm->vmcb, svm->vmcb_gpa); + GUEST_ASSERT(svm->vmcb->control.exit_code =3D=3D SVM_EXIT_VMMCALL); + GUEST_DONE(); +} + +int main(int argc, char *argv[]) +{ + struct kvm_x86_state *state; + vm_vaddr_t nested_gva =3D 0; + struct kvm_vcpu *vcpu; + uint32_t maxphyaddr; + u64 max_legal_gpa; + struct kvm_vm *vm; + struct ucall uc; + + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM)); + + vm =3D vm_create_with_one_vcpu(&vcpu, l1_guest_code); + vm_install_exception_handler(vcpu->vm, GP_VECTOR, guest_gp_handler); + + /* + * Find the max legal GPA that is not backed by a memslot (i.e. cannot + * be mapped by KVM). + */ + maxphyaddr =3D kvm_cpuid_property(vcpu->cpuid, X86_PROPERTY_MAX_PHY_ADDR); + max_legal_gpa =3D BIT_ULL(maxphyaddr) - PAGE_SIZE; + vcpu_alloc_svm(vm, &nested_gva); + vcpu_args_set(vcpu, 2, nested_gva, max_legal_gpa); + + /* VMRUN with max_legal_gpa, KVM injects a #GP */ + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_SYNC); + TEST_ASSERT_EQ(uc.args[1], SYNC_GP); + + /* + * Enter L2 (with a legit vmcb12 GPA), then overwrite vmcb12 GPA with + * max_legal_gpa. KVM will fail to map vmcb12 on nested VM-Exit and + * cause a shutdown. + */ + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_SYNC); + TEST_ASSERT_EQ(uc.args[1], SYNC_L2_STARTED); + + state =3D vcpu_save_state(vcpu); + state->nested.hdr.svm.vmcb_pa =3D max_legal_gpa; + vcpu_load_state(vcpu, state); + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_SHUTDOWN); + + kvm_x86_state_cleanup(state); + kvm_vm_free(vm); + return 0; +} --=20 2.53.0.414.gf7e9f6c205-goog