[PATCH] x86/apic: KVM: Use guard() instead of mutex_lock() to simplify code

Liao Yuanhong posted 1 patch 1 month ago
arch/x86/kvm/lapic.c | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
[PATCH] x86/apic: KVM: Use guard() instead of mutex_lock() to simplify code
Posted by Liao Yuanhong 1 month ago
Using guard(mutex) instead of mutex_lock/mutex_unlock pair. Simplifies the
error handling to just return in case of error. No need for the 'out' label
and variable 'ret' anymore so remove it.

Signed-off-by: Liao Yuanhong <liaoyuanhong@vivo.com>
---
 arch/x86/kvm/lapic.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index ed636c1f5e58..fa52622bcf5f 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -2683,24 +2683,20 @@ void kvm_apic_update_apicv(struct kvm_vcpu *vcpu)
 int kvm_alloc_apic_access_page(struct kvm *kvm)
 {
 	void __user *hva;
-	int ret = 0;
 
-	mutex_lock(&kvm->slots_lock);
+	guard(mutex)(&kvm->slots_lock);
 	if (kvm->arch.apic_access_memslot_enabled ||
 	    kvm->arch.apic_access_memslot_inhibited)
-		goto out;
+		return 0;
 
 	hva = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
 				      APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
-	if (IS_ERR(hva)) {
-		ret = PTR_ERR(hva);
-		goto out;
-	}
+	if (IS_ERR(hva))
+		return PTR_ERR(hva);
 
 	kvm->arch.apic_access_memslot_enabled = true;
-out:
-	mutex_unlock(&kvm->slots_lock);
-	return ret;
+
+	return 0;
 }
 EXPORT_SYMBOL_GPL(kvm_alloc_apic_access_page);
 
-- 
2.34.1
Re: [PATCH] x86/apic: KVM: Use guard() instead of mutex_lock() to simplify code
Posted by Sean Christopherson 2 weeks, 3 days ago
On Mon, 01 Sep 2025 21:18:21 +0800, Liao Yuanhong wrote:
> Using guard(mutex) instead of mutex_lock/mutex_unlock pair. Simplifies the
> error handling to just return in case of error. No need for the 'out' label
> and variable 'ret' anymore so remove it.

Applied to kvm-x86 misc, thanks!

[1/1] x86/apic: KVM: Use guard() instead of mutex_lock() to simplify code
      https://github.com/kvm-x86/linux/commit/50f4db196766

--
https://github.com/kvm-x86/linux/tree/next
Re: [PATCH] x86/apic: KVM: Use guard() instead of mutex_lock() to simplify code
Posted by Sean Christopherson 2 weeks, 2 days ago
On Mon, Sep 15, 2025, Sean Christopherson wrote:
> On Mon, 01 Sep 2025 21:18:21 +0800, Liao Yuanhong wrote:
> > Using guard(mutex) instead of mutex_lock/mutex_unlock pair. Simplifies the
> > error handling to just return in case of error. No need for the 'out' label
> > and variable 'ret' anymore so remove it.
> 
> Applied to kvm-x86 misc, thanks!
> 
> [1/1] x86/apic: KVM: Use guard() instead of mutex_lock() to simplify code
>       https://github.com/kvm-x86/linux/commit/50f4db196766

I force pushed to fixup an unrelated commit, new hash:

      https://github.com/kvm-x86/linux/commit/4319fa120f0f