[PATCH v2] hw/i386/kvm/xen_evtchn: use lock guards

Drif Abdelmalek Mohamed Said posted 1 patch 3 days, 10 hours ago
hw/i386/kvm/xen_evtchn.c | 62 +++++++++++++++-------------------------
1 file changed, 23 insertions(+), 39 deletions(-)
[PATCH v2] hw/i386/kvm/xen_evtchn: use lock guards
Posted by Drif Abdelmalek Mohamed Said 3 days, 10 hours ago
Replace qemu_mutex_lock()/qemu_mutex_unlock() pairs with
QEMU_LOCK_GUARD()/WITH_QEMU_LOCK_GUARD() in functions with error paths
and early returns: xen_evtchn_bind_pirq_op(), xen_physdev_unmap_pirq(),
xen_be_evtchn_bind_interdomain() and xen_be_evtchn_unbind().

No functional change intended.

Signed-off-by: Drif Abdelmalek Mohamed Said <drifabdelmalekmohamedsaid@gmail.com>
---
v2:
  - Rebased on top of current master (some changes from time of sending 1st patch)
v1: https://lore.kernel.org/all/20261128103100.357-1-drifabdelmalekmohamedsaid@gmail.com/#t
 hw/i386/kvm/xen_evtchn.c | 62 +++++++++++++++-------------------------
 1 file changed, 23 insertions(+), 39 deletions(-)

diff --git a/hw/i386/kvm/xen_evtchn.c b/hw/i386/kvm/xen_evtchn.c
index 5a986c1e2a..5d09d52ba3 100644
--- a/hw/i386/kvm/xen_evtchn.c
+++ b/hw/i386/kvm/xen_evtchn.c
@@ -1916,29 +1916,26 @@ int xen_physdev_unmap_pirq(struct physdev_unmap_pirq *unmap)
     }
 
     BQL_LOCK_GUARD();
-    qemu_mutex_lock(&s->port_lock);
+    WITH_QEMU_LOCK_GUARD(&s->port_lock) {
+        if (!pirq_inuse(s, pirq)) {
+            return -ENOENT;
+        }
 
-    if (!pirq_inuse(s, pirq)) {
-        qemu_mutex_unlock(&s->port_lock);
-        return -ENOENT;
-    }
+        gsi = s->pirq[pirq].gsi;
 
-    gsi = s->pirq[pirq].gsi;
+        /* We can only unmap GSI PIRQs and emulated MSIs */
+        if (gsi < 0 && gsi != IRQ_MSI_EMU) {
+            return -EINVAL;
+        }
 
-    /* We can only unmap GSI PIRQs and emulated MSIs */
-    if (gsi < 0 && gsi != IRQ_MSI_EMU) {
-        qemu_mutex_unlock(&s->port_lock);
-        return -EINVAL;
-    }
+        if (gsi >= 0) {
+            s->gsi_pirq[gsi] = 0;
+        }
+        s->pirq[pirq].gsi = IRQ_UNBOUND; /* Doesn't actually matter because: */
+        pirq_inuse_word(s, pirq) &= ~pirq_inuse_bit(pirq);
 
-    if (gsi >= 0) {
-        s->gsi_pirq[gsi] = 0;
+        trace_kvm_xen_unmap_pirq(pirq, gsi);
     }
-    s->pirq[pirq].gsi = IRQ_UNBOUND; /* Doesn't actually matter because: */
-    pirq_inuse_word(s, pirq) &= ~pirq_inuse_bit(pirq);
-
-    trace_kvm_xen_unmap_pirq(pirq, gsi);
-    qemu_mutex_unlock(&s->port_lock);
 
     if (gsi == IRQ_MSI_EMU) {
         kvm_update_msi_routes_all(NULL, true, 0, 0);
@@ -2095,7 +2092,7 @@ int xen_be_evtchn_bind_interdomain(struct xenevtchn_handle *xc, uint32_t domid,
         return -EINVAL;
     }
 
-    qemu_mutex_lock(&s->port_lock);
+    QEMU_LOCK_GUARD(&s->port_lock);
 
     /* The guest has to have an unbound port waiting for us to bind */
     gp = &s->port_table[guest_port];
@@ -2112,15 +2109,14 @@ int xen_be_evtchn_bind_interdomain(struct xenevtchn_handle *xc, uint32_t domid,
             if (kvm_xen_has_cap(EVTCHN_SEND)) {
                 assign_kernel_eventfd(gp->type, guest_port, xc->fd);
             }
-            break;
+            return ret;
         }
         /* fall through */
 
     case EVTCHNSTAT_unbound:
         be_port = find_be_port(s, xc);
         if (!be_port) {
-            ret = -ENOSPC;
-            goto out;
+            return -ENOSPC;
         }
 
         gp->type = EVTCHNSTAT_interdomain;
@@ -2130,24 +2126,16 @@ int xen_be_evtchn_bind_interdomain(struct xenevtchn_handle *xc, uint32_t domid,
         if (kvm_xen_has_cap(EVTCHN_SEND)) {
             assign_kernel_eventfd(gp->type, guest_port, xc->fd);
         }
-        ret = be_port;
-        break;
+        return be_port;
 
     default:
-        ret = -EINVAL;
-        break;
+        return -EINVAL;
     }
-
- out:
-    qemu_mutex_unlock(&s->port_lock);
-
-    return ret;
 }
 
 int xen_be_evtchn_unbind(struct xenevtchn_handle *xc, evtchn_port_t port)
 {
     XenEvtchnState *s = xen_evtchn_singleton;
-    int ret;
 
     if (!s) {
         return -ENOTSUP;
@@ -2157,11 +2145,10 @@ int xen_be_evtchn_unbind(struct xenevtchn_handle *xc, evtchn_port_t port)
         return -EFAULT;
     }
 
-    qemu_mutex_lock(&s->port_lock);
+    QEMU_LOCK_GUARD(&s->port_lock);
 
     if (port && port != xc->be_port) {
-        ret = -EINVAL;
-        goto out;
+        return -EINVAL;
     }
 
     if (xc->guest_port) {
@@ -2181,10 +2168,7 @@ int xen_be_evtchn_unbind(struct xenevtchn_handle *xc, evtchn_port_t port)
 
     s->be_handles[xc->be_port] = NULL;
     xc->be_port = 0;
-    ret = 0;
- out:
-    qemu_mutex_unlock(&s->port_lock);
-    return ret;
+    return 0;
 }
 
 int xen_be_evtchn_close(struct xenevtchn_handle *xc)
-- 
2.47.3