From: David Woodhouse 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 Signed-off-by: David Woodhouse --- 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