From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A4603346A7 for ; Thu, 26 Feb 2026 23:56:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772150168; cv=none; b=avanB71FYQzrosuhVLNV32dc/tQ/CyJxgavaF9gKrvHg5GuaR9FdlGxu3LWWBAPZK65oJwYvd8ExyE6biqWEZ2HuC++Q+KnHgm8QPzeg2c3Gbp5ncGWwV2yXZyqLK2lGRPW288HCE2azYPuYPxNzeMNkno5FGx4umM83SS9dSeM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772150168; c=relaxed/simple; bh=dO5j/8P0CGpy9elIlT4S456OEShdLdz1gkyhZcO5kTo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Zb6eD91ZW6Frr5GAeAFnFjz7W9w0uukJyR02kG6pPVuMZ2reR3VsgZBv3LruLo5PNAaRmrpvo5tMy5VSD+CDGI9Pb7rzyS8Yh5LekFp0JdnVG7kUTPims0V4AmtGDojNA3/+K0CehnCrAnvWjYmyOOVq49TI6vB9Rs883NMD9sI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2XgYuY16; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2XgYuY16" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-358ffccebf1so1351938a91.3 for ; Thu, 26 Feb 2026 15:56:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772150165; x=1772754965; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=6kZCSGy2Zm1RN5RRM4KyVYeQq5QPnXjbgoTlqEo7MGA=; b=2XgYuY16ZlAw5mXD4/PNOYMYuoLIk17aYXkY/zKG59kcjMER6McvDs0OZDWeNxJjyy Ky4fs05XpRojUYGL/0007FuY6tfNMbeoeSCManWHmpzK9svUTBV6KmPLyqPerqPHLUkS Ao4HezkBEK4WmZw1vVRD8Aa2MMyX3DgLQ/4L2jBVyeqxw3IExapNeH/AWCFZFN+Eko8K wpq4JxmvEvhaqAS91TDgcWteuMoAvnw3EdWqRahjhSDJ7JyU+hiiprCZYiQfp4aNq4AI 5qhqg1r/MKFhm5x3LtuhQ/CkCVBFiDaOgYtzTadx6IcLWjtBEXltnmbC8TjHnk3PRAqt npHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772150165; x=1772754965; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=6kZCSGy2Zm1RN5RRM4KyVYeQq5QPnXjbgoTlqEo7MGA=; b=kkbeUtsU/X2gXcRv8OMJnUkYDGf4JeT50PtNSL9A99j1YkHk+iJVnkNfrW0DNkLy11 8dprF+MC7F+hZe6NZt6feMG7+W5xdl06WgHuk2R5tHmEuoHVGXyIBRLhgw2oMsrF/Uvl 3d8vp+k1OhsBKc9v/zCa7Zw1X91eVf+tbjTGsD8+IGVIPRCXc9X80oLPdx6P7d4ztuEX fh8XxlptKzpvi0ERS8rbbcR7LwsO/Ub/JKm6D8HKOqv09Ujyjd9z79FWwrK87tzD2Vgt O8YKBGyK3+tjvXMq495g13wHUj6pMyTGRZVcJ/6W5HFJt//5uts8TJMnvkTVFH+xkVB1 mO8Q== X-Forwarded-Encrypted: i=1; AJvYcCVuDCETVByu4eQcvBjQI1L26p+CdvLZrV2DSLFfGsRbtLWcXxnrvsk7eu/f6RK5YEh4M6wQacsxLXyNr7I=@vger.kernel.org X-Gm-Message-State: AOJu0YwseafnI8Q0sN1tUfIL/D9+CBUVV/rQs7jvhbUIDQDVTHnLezUT yEFMdHHUfS23rXi1Rcx/fJ+ZEW/2xmZpL6nEsjVPtSyHOHyL7+G92Ak881zk4hs7GzSzDQG4BES lugH9Jg== X-Received: from pjbnd5.prod.google.com ([2002:a17:90b:4cc5:b0:358:f532:89b2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d46:b0:343:684c:f8ad with SMTP id 98e67ed59e1d1-35965c3871dmr765837a91.4.1772150164478; Thu, 26 Feb 2026 15:56:04 -0800 (PST) Date: Thu, 26 Feb 2026 15:56:03 -0800 In-Reply-To: <20260120201013.3931334-9-clopez@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260120201013.3931334-3-clopez@suse.de> <20260120201013.3931334-9-clopez@suse.de> Message-ID: Subject: Re: [PATCH v2 6/6] KVM: SEV: use scoped mutex guard in sev_asid_new() From: Sean Christopherson To: "Carlos =?utf-8?B?TMOzcGV6?=" Cc: kvm@vger.kernel.org, pbonzini@redhat.com, pankaj.gupta@amd.com, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, linux-kernel@vger.kernel.org, Borislav Petkov Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Tue, Jan 20, 2026, Carlos L=C3=B3pez wrote: > Simplify the lock management in sev_asid_new() by using a mutex guard, > automatically releasing the mutex when following the goto. >=20 > Signed-off-by: Carlos L=C3=B3pez > --- > arch/x86/kvm/svm/sev.c | 24 ++++++++++-------------- > 1 file changed, 10 insertions(+), 14 deletions(-) >=20 > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index d3fa0963465d..d8d5c3a703f9 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c > @@ -231,24 +231,20 @@ static int sev_asid_new(struct kvm_sev_info *sev, u= nsigned long vm_type) > return ret; > } > =20 > - mutex_lock(&sev_bitmap_lock); > - > + scoped_guard(mutex, &sev_bitmap_lock) { > again: > - asid =3D find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid); > - if (asid > max_asid) { > - if (retry && __sev_recycle_asids(min_asid, max_asid)) { > - retry =3D false; > - goto again; > + asid =3D find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid); > + if (asid > max_asid) { > + if (retry && __sev_recycle_asids(min_asid, max_asid)) { > + retry =3D false; > + goto again; > + } > + ret =3D -EBUSY; > + goto e_uncharge; > } > - mutex_unlock(&sev_bitmap_lock); > - ret =3D -EBUSY; > - goto e_uncharge; > + __set_bit(asid, sev_asid_bitmap); > } I think I'd prefer to throw this into a helper to avoid the goto within the scoped guard. FWIW, I also tried (quite hard) to replace the goto with a l= oop, and couldn't come up with anything better. No need for you to send a v3, I'll incorporate these patches into a larger = series (there are some locking goofs that need to be fixed, and at least one of th= ese patches will generate an annoying-but-easy-to-resolve conflict). Thanks! diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index af84357dc954..249384e30320 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -198,6 +198,28 @@ static void sev_misc_cg_uncharge(struct kvm_sev_info *= sev) misc_cg_uncharge(type, sev->misc_cg, 1); } =20 +static unsigned int sev_alloc_asid(unsigned int min_asid, unsigned int max= _asid) +{ + unsigned int asid; + bool retry =3D true; + + guard(mutex)(&sev_bitmap_lock); + +again: + asid =3D find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid= ); + if (asid > max_asid) { + if (retry && __sev_recycle_asids(min_asid, max_asid)) { + retry =3D false; + goto again; + } + + return asid; + } + + __set_bit(asid, sev_asid_bitmap); + return asid; +} + static int sev_asid_new(struct kvm_sev_info *sev, unsigned long vm_type) { /* @@ -205,7 +227,6 @@ static int sev_asid_new(struct kvm_sev_info *sev, unsig= ned long vm_type) * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. */ unsigned int min_asid, max_asid, asid; - bool retry =3D true; int ret; =20 if (vm_type =3D=3D KVM_X86_SNP_VM) { @@ -238,24 +259,12 @@ static int sev_asid_new(struct kvm_sev_info *sev, uns= igned long vm_type) return ret; } =20 - mutex_lock(&sev_bitmap_lock); - -again: - asid =3D find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid= ); + asid =3D sev_alloc_asid(min_asid, max_asid); if (asid > max_asid) { - if (retry && __sev_recycle_asids(min_asid, max_asid)) { - retry =3D false; - goto again; - } - mutex_unlock(&sev_bitmap_lock); ret =3D -EBUSY; goto e_uncharge; } =20 - __set_bit(asid, sev_asid_bitmap); - - mutex_unlock(&sev_bitmap_lock); - sev->asid =3D asid; return 0; e_uncharge: