[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v1 20/21] hw/i386/kvm/xen_evtchn: Hold port_lock for whole of bind_pirq_op



From: David Woodhouse <dwmw@xxxxxxxxxxxx>

The check for an existing binding read s->pirq[].port before taking
port_lock, and the allocation wrote it after. The BQL serialises the
writers in practice, but holding port_lock across the check and the
allocation is clearly correct and lets Coverity see it too.

The region now under the lock is pure port_table/pirq bookkeeping.
The MSI unmask and kvm_update_msi_routes_all() calls must remain
outside port_lock: they can re-enter via the MSI translate function,
which takes port_lock itself. The PIRQ→evtchn→GSI delivery paths have
further BQL/port_lock ordering constraints — see the comments in
xen_evtchn_set_callback_level() and xen_evtchn_set_gsi().

Resolves: Coverity CID 1527403
Fixes: 6096cf7877bc ("hw/xen: Support MSI mapping to PIRQ")
Reported-by: Peter Maydell <peter.maydell@xxxxxxxxxx>
Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
 hw/i386/kvm/xen_evtchn.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/hw/i386/kvm/xen_evtchn.c b/hw/i386/kvm/xen_evtchn.c
index e2803cad0a8..cfe735219bf 100644
--- a/hw/i386/kvm/xen_evtchn.c
+++ b/hw/i386/kvm/xen_evtchn.c
@@ -1289,24 +1289,21 @@ int xen_evtchn_bind_pirq_op(struct evtchn_bind_pirq 
*pirq)
 
     BQL_LOCK_GUARD();
 
-    if (s->pirq[pirq->pirq].port) {
-        return -EBUSY;
-    }
+    WITH_QEMU_LOCK_GUARD(&s->port_lock) {
+        if (s->pirq[pirq->pirq].port) {
+            return -EBUSY;
+        }
 
-    qemu_mutex_lock(&s->port_lock);
+        ret = allocate_port(s, 0, EVTCHNSTAT_pirq, pirq->pirq,
+                            &pirq->port);
+        if (ret) {
+            return ret;
+        }
 
-    ret = allocate_port(s, 0, EVTCHNSTAT_pirq, pirq->pirq,
-                        &pirq->port);
-    if (ret) {
-        qemu_mutex_unlock(&s->port_lock);
-        return ret;
+        s->pirq[pirq->pirq].port = pirq->port;
+        trace_kvm_xen_bind_pirq(pirq->pirq, pirq->port);
     }
 
-    s->pirq[pirq->pirq].port = pirq->port;
-    trace_kvm_xen_bind_pirq(pirq->pirq, pirq->port);
-
-    qemu_mutex_unlock(&s->port_lock);
-
     /*
      * Need to do the unmask outside port_lock because it may call
      * back into the MSI translate function.
-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.