From nobody Sat Feb 7 22:21:10 2026 Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (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 7E1DC274B48; Fri, 30 Jan 2026 02:08:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769738902; cv=none; b=U9C95OlY2+cxSeIQLiX7uU1qPGELh+SptgYzZsNovO9V1epYBmqlVF7Y3tmMEG7YJcHe3aYkrYW2ag1Rsho0w5T9TSaVXclMeNM+D1p+U7iIRbXEnGGxpJzJiFyfm6l9ZS8J1IR/6J4p/G4XcgPOuWDMp1g2H2i2XWFKPlNIVQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769738902; c=relaxed/simple; bh=O1QRrPqKg5mC648xLFRpHLgL7eVY2iM8HRp/SF3TDwo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SuU+MuEnyGBFrXcvrzOdbE2xe2AQR53jOnfzYLh7FKVzK03Vq8JTVTMif5AiFipUzwaOZ6/J17zhc8mTtm/IFo4OHgMeqiG2+ELOiAPWUm955/pkjr/CI0kxGVRJZaS1BsRzA94Ezabe22057Gye3pVCJgxI+u9dYq2WsySTy7c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Ko/KcRKr; arc=none smtp.client-ip=91.218.175.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Ko/KcRKr" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1769738898; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JF31oSFpVl5RgizgjEqu47HRxbHu0UtARVtac3Gefbo=; b=Ko/KcRKr2eYc9pI3f0n/Chw4f0qQvFPzrh7H7ugX5S2u8AMdajPYVdi8//6UWiN4wwWzdl 7CYmZC53LKGz92fnojo0oRk07Yl0MRvprJFVozgAYIChPjaSZNv8R7PPkhoO+P73X0z+pc kB8a+0EF28dp7JcmEQmu6ccks+dWA4k= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH 1/3] KVM: SVM: Refactor EFER.SVME switching logic out of svm_set_efer() Date: Fri, 30 Jan 2026 02:07:33 +0000 Message-ID: <20260130020735.2517101-2-yosry.ahmed@linux.dev> In-Reply-To: <20260130020735.2517101-1-yosry.ahmed@linux.dev> References: <20260130020735.2517101-1-yosry.ahmed@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Move the logic of switching EFER.SVME in the guest outside of svm_set_efer(). This makes it possible to easily check the skip conditions separately (and add more) and reduce indentation level. No functional change intended. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/svm.c | 72 ++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 5f0136dbdde6b..4575a6a7d6c4e 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -200,11 +200,49 @@ static int get_npt_level(void) #endif } =20 +static int svm_set_efer_svme(struct kvm_vcpu *vcpu, u64 old_efer, u64 new_= efer) +{ + struct vcpu_svm *svm =3D to_svm(vcpu); + int r; + + if ((old_efer & EFER_SVME) =3D=3D (new_efer & EFER_SVME)) + return 0; + + if (new_efer & EFER_SVME) { + r =3D svm_allocate_nested(svm); + if (r) + return r; + + /* + * Never intercept #GP for SEV guests, KVM can't decrypt guest + * memory to workaround the erratum. + */ + if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm)) + set_exception_intercept(svm, GP_VECTOR); + } else { + + svm_leave_nested(vcpu); + /* #GP intercept is still needed for vmware backdoor */ + if (!enable_vmware_backdoor) + clr_exception_intercept(svm, GP_VECTOR); + + /* + * Free the nested guest state, unless we are in SMM. In this + * case we will return to the nested guest as soon as we leave + * SMM. + */ + if (!is_smm(vcpu)) + svm_free_nested(svm); + } + return 0; +} + int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) { struct vcpu_svm *svm =3D to_svm(vcpu); u64 old_efer =3D vcpu->arch.efer; vcpu->arch.efer =3D efer; + int r; =20 if (!npt_enabled) { /* Shadow paging assumes NX to be available. */ @@ -214,36 +252,10 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) efer &=3D ~EFER_LME; } =20 - if ((old_efer & EFER_SVME) !=3D (efer & EFER_SVME)) { - if (!(efer & EFER_SVME)) { - svm_leave_nested(vcpu); - /* #GP intercept is still needed for vmware backdoor */ - if (!enable_vmware_backdoor) - clr_exception_intercept(svm, GP_VECTOR); - - /* - * Free the nested guest state, unless we are in SMM. - * In this case we will return to the nested guest - * as soon as we leave SMM. - */ - if (!is_smm(vcpu)) - svm_free_nested(svm); - - } else { - int ret =3D svm_allocate_nested(svm); - - if (ret) { - vcpu->arch.efer =3D old_efer; - return ret; - } - - /* - * Never intercept #GP for SEV guests, KVM can't - * decrypt guest memory to workaround the erratum. - */ - if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm)) - set_exception_intercept(svm, GP_VECTOR); - } + r =3D svm_set_efer_svme(vcpu, old_efer, efer); + if (r) { + vcpu->arch.efer =3D old_efer; + return r; } =20 svm->vmcb->save.efer =3D efer | EFER_SVME; --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 22:21:10 2026 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) (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 3CE8F2F39CF for ; Fri, 30 Jan 2026 02:08:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769738903; cv=none; b=jONNyvquBEwnxv1iM2e9DiVXCcEmHKL/xMqRAI9nNgnSWxKZV5eY9o1eaavNRjsJ+pwBUNTIr/eTOER8CnsRG7bX6efMp8rIdV/lt4YK9O83uAiL1OUrlAcidpGh3+8xtVkpeLWUXav9RlPT1WxtS6kpOdsv72g03AOKQUue0FY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769738903; c=relaxed/simple; bh=m9/zeSNq14CxyVs51HJh4An7BZ1Ecba7dMEw86U27pQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o52CnCzwLdIbqkicsq98xymxLbW7F5pkERu1/CvoobkLnjApp2rzON9NtXBv9zMd3MKZZ2H+EvrmI95Od+1qTS1ZoaKw8u+Ps5uPNDNhzRt3wfDXiDAP35FOVjvmMx95mCIVc9BfwWwuz+oUibK1Su5xqu1gR/rAFxU7zhm8Mhg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Xnsj8Ic4; arc=none smtp.client-ip=91.218.175.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Xnsj8Ic4" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1769738900; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wvS3fiEQ8H0qPwvR4JG9T/NRRh+U6aRn0/0hOCj+hP8=; b=Xnsj8Ic42LQx/7P+RiebCfmUmnOYFX1mqolXFg4Fw6jcabhIhbHwlbAv5jUU4WZuqgcPFo YJgH6U636muJlp6hGOWVh6TlkEFgU7jfSjBzcJyn0Ue/sa/GBL89J/JhwR/8whMgNPmxCR 6HG3B+fnIyQlGa/4zEnUaQj7mbnlzM4= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH 2/3] KVM: nSVM: Do not track EFER.SVME toggling in guest mode Date: Fri, 30 Jan 2026 02:07:34 +0000 Message-ID: <20260130020735.2517101-3-yosry.ahmed@linux.dev> In-Reply-To: <20260130020735.2517101-1-yosry.ahmed@linux.dev> References: <20260130020735.2517101-1-yosry.ahmed@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" KVM tracks when EFER.SVME is set and cleared to initialize and tear down nested state. However, it doesn't differentiate if EFER.SVME is getting toggled in L1 or L2+. Toggling EFER.SVME in L2+ is inconsequential from KVM's perspective, as the vCPU is still obviously using nested. This causes a problem if L2 sets then clears EFER.SVME without L1 interception, as KVM exits guest mode and tears down nested state while L2 is running, executing L1 without injecting a proper #VMEXIT. Technically, it's not a bug as the APM states that an L1 hypervisor should intercept EFER writes: The effect of turning off EFER.SVME while a guest is running is undefined; therefore, the VMM should always prevent guests from writing EFER. However, it would be nice if KVM handled it more gracefully. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/svm/svm.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 4575a6a7d6c4e..eaf0f8053fbfb 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -208,6 +208,13 @@ static int svm_set_efer_svme(struct kvm_vcpu *vcpu, u6= 4 old_efer, u64 new_efer) if ((old_efer & EFER_SVME) =3D=3D (new_efer & EFER_SVME)) return 0; =20 + /* + * An L2 guest setting or clearing EFER_SVME does not change whether or + * not the vCPU can use nested from KVM's perspective. + */ + if (is_guest_mode(vcpu)) + return 0; + if (new_efer & EFER_SVME) { r =3D svm_allocate_nested(svm); if (r) --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 22:21:10 2026 Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (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 B296B2DFF28 for ; Fri, 30 Jan 2026 02:08:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769738905; cv=none; b=eXJHVVwVj0kEZ+vRlzFddO+b8lol0YpSjzjz93xpktxRNP4/KuBTbx/Q0sKd++03sGv6yJ7RTS47B4k47zAPC1Pg21KArnVBlOApgqPI94qipQSy0cITEP6iem7TjqmFghke0LsEa8tvs7i8LcCKsBCBz/dU//8dzY99e97pOC0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769738905; c=relaxed/simple; bh=aQ5VNgkHVq38PZ95gRT3ahLqlKJLdIB+3idB5ksK5Rc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s+44EZ2vKaFwqSl3o2VNZZjsuIiD4f5wNe4sDtOTF4v7XzRx42jh2o450G6i5yLggobuVQwE4sDJuNMwhhd7hgE8tr+q6YPyVMShux9swTe/+gpJ+Lu7Rp+ZQpr70cGkCZrMHk8oSVzr8gEy8Vx32PzB9jg1OPZghX7HqaUuhE4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=h/ZbRrnq; arc=none smtp.client-ip=91.218.175.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="h/ZbRrnq" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1769738901; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qdp+L8U+yhHpW+/9jFSxvb9uYHCqer0u894nfeOf5RA=; b=h/ZbRrnqws83vxMk/u3oiNpVlLGAFouiHQ94mBa9LI2wBmk1OcqW2DvEsHoldDiRwlFSFY +8ZycLcItA+FUwI8svBjA6izDpm85Q6UMyCjMex6IKE+0ErDL0qJjlVrh3MjDm22KQH0LW 75G/DgoRT8IA+X75Q6YtUp949QYm1WQ= From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH 3/3] KVM: selftests: Add a test for L2 toggling EFER.SVME Date: Fri, 30 Jan 2026 02:07:35 +0000 Message-ID: <20260130020735.2517101-4-yosry.ahmed@linux.dev> In-Reply-To: <20260130020735.2517101-1-yosry.ahmed@linux.dev> References: <20260130020735.2517101-1-yosry.ahmed@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Add a test to make sure that L2 toggling EFER.SVME works as intended. This is a regression test for KVM mistakenly leaving nested and tearing down nested state if L2 disables EFER.SVME, even though L1 is still using nested. Signed-off-by: Yosry Ahmed --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/x86/svm_nested_toggle_efer_svme.c | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_toggle_efer_= svme.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index 58eee0474db6a..7bd2297b5494e 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_nested_toggle_efer_svme 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/x86/svm_nested_toggle_efer_svme.c = b/tools/testing/selftests/kvm/x86/svm_nested_toggle_efer_svme.c new file mode 100644 index 0000000000000..5267fbdbf01cc --- /dev/null +++ b/tools/testing/selftests/kvm/x86/svm_nested_toggle_efer_svme.c @@ -0,0 +1,76 @@ +// 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 + +static void l2_guest_code(void) +{ + unsigned long efer =3D rdmsr(MSR_EFER); + + /* generic_svm_setup() initializes EFER_SVME set for L2 */ + GUEST_ASSERT(efer & EFER_SVME); + + wrmsr(MSR_EFER, efer & ~EFER_SVME); + GUEST_ASSERT(rdmsr(MSR_EFER) =3D=3D (efer & ~EFER_SVME)); + + wrmsr(MSR_EFER, efer); + GUEST_ASSERT(rdmsr(MSR_EFER) =3D=3D efer); + + vmmcall(); +} + +static void l1_guest_code(struct svm_test_data *svm) +{ + 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, 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_vcpu *vcpu; + struct kvm_vm *vm; + vm_vaddr_t nested_gva =3D 0; + + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM)); + + vm =3D vm_create_with_one_vcpu(&vcpu, l1_guest_code); + + vcpu_alloc_svm(vm, &nested_gva); + vcpu_args_set(vcpu, 1, nested_gva); + + for (;;) { + struct ucall uc; + + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + case UCALL_SYNC: + break; + case UCALL_DONE: + goto done; + default: + TEST_FAIL("Unknown ucall %lu", uc.cmd); + } + } + +done: + kvm_vm_free(vm); + return 0; +} --=20 2.53.0.rc1.225.gd81095ad13-goog