From: Sean Christopherson <seanjc@google.com>
To: "Carlos López" <clopez@suse.de>
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 <bp@alien8.de>
Subject: Re: [PATCH v2 6/6] KVM: SEV: use scoped mutex guard in sev_asid_new()
Date: Thu, 26 Feb 2026 15:56:03 -0800 [thread overview]
Message-ID: <aaDdk4FabBPOD84P@google.com> (raw)
In-Reply-To: <20260120201013.3931334-9-clopez@suse.de>
On Tue, Jan 20, 2026, Carlos López wrote:
> Simplify the lock management in sev_asid_new() by using a mutex guard,
> automatically releasing the mutex when following the goto.
>
> Signed-off-by: Carlos López <clopez@suse.de>
> ---
> arch/x86/kvm/svm/sev.c | 24 ++++++++++--------------
> 1 file changed, 10 insertions(+), 14 deletions(-)
>
> 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, unsigned long vm_type)
> return ret;
> }
>
> - mutex_lock(&sev_bitmap_lock);
> -
> + scoped_guard(mutex, &sev_bitmap_lock) {
> again:
> - asid = 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 = false;
> - goto again;
> + asid = 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 = false;
> + goto again;
> + }
> + ret = -EBUSY;
> + goto e_uncharge;
> }
> - mutex_unlock(&sev_bitmap_lock);
> - ret = -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 loop,
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 these
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);
}
+static unsigned int sev_alloc_asid(unsigned int min_asid, unsigned int max_asid)
+{
+ unsigned int asid;
+ bool retry = true;
+
+ guard(mutex)(&sev_bitmap_lock);
+
+again:
+ asid = 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 = 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, unsigned 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 = true;
int ret;
if (vm_type == KVM_X86_SNP_VM) {
@@ -238,24 +259,12 @@ static int sev_asid_new(struct kvm_sev_info *sev, unsigned long vm_type)
return ret;
}
- mutex_lock(&sev_bitmap_lock);
-
-again:
- asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid);
+ asid = sev_alloc_asid(min_asid, max_asid);
if (asid > max_asid) {
- if (retry && __sev_recycle_asids(min_asid, max_asid)) {
- retry = false;
- goto again;
- }
- mutex_unlock(&sev_bitmap_lock);
ret = -EBUSY;
goto e_uncharge;
}
- __set_bit(asid, sev_asid_bitmap);
-
- mutex_unlock(&sev_bitmap_lock);
-
sev->asid = asid;
return 0;
e_uncharge:
prev parent reply other threads:[~2026-02-26 23:56 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-20 20:10 [PATCH v2 0/6] KVM: SEV: use mutex guards for simpler error handling Carlos López
2026-01-20 20:10 ` [PATCH v2 1/6] KVM: SEV: use mutex guard in snp_launch_update() Carlos López
2026-01-20 20:10 ` [PATCH v2 2/6] KVM: SEV: use mutex guard in sev_mem_enc_ioctl() Carlos López
2026-01-20 20:10 ` [PATCH v2 3/6] KVM: SEV: use mutex guard in sev_mem_enc_register_region() Carlos López
2026-01-20 20:10 ` [PATCH v2 4/6] KVM: SEV: use mutex guard in sev_mem_enc_unregister_region() Carlos López
2026-01-20 20:10 ` [PATCH v2 5/6] KVM: SEV: use mutex guard in snp_handle_guest_req() Carlos López
2026-01-20 20:10 ` [PATCH v2 6/6] KVM: SEV: use scoped mutex guard in sev_asid_new() Carlos López
2026-02-26 23:56 ` Sean Christopherson [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aaDdk4FabBPOD84P@google.com \
--to=seanjc@google.com \
--cc=bp@alien8.de \
--cc=clopez@suse.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pankaj.gupta@amd.com \
--cc=pbonzini@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox