From nobody Sun Feb 8 14:51:16 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