From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>, <qemu-block@nongnu.org>,
Hanna Reitz <hreitz@redhat.com>,
Laurent Vivier <lvivier@redhat.com>, Fam Zheng <fam@euphon.net>,
Fabiano Rosas <farosas@suse.de>,
Paolo Bonzini <pbonzini@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Stefano Garzarella <sgarzare@redhat.com>
Subject: [PATCH 2/5] virtio: unify virtio_notify_irqfd() and virtio_notify()
Date: Mon, 22 Sep 2025 18:01:46 -0400 [thread overview]
Message-ID: <20250922220149.498967-3-stefanha@redhat.com> (raw)
In-Reply-To: <20250922220149.498967-1-stefanha@redhat.com>
The difference between these two functions:
- virtio_notify() uses the interrupt code path (MSI or classic IRQs)
- virtio_notify_irqfd() uses guest notifiers (irqfds)
virtio_notify() can only be called with the BQL held because the
interrupt code path requires the BQL. Device models use
virtio_notify_irqfd() from IOThreads since the BQL is not held.
The two functions can be unified by pushing down the if
(qemu_in_iothread()) check from virtio-blk and virtio-scsi into core
virtio code. This is in preparation for the next commit that will add
irqfd support to virtio_notify_config() and where it's unattractive to
introduce another irqfd-only API for device model callers.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
include/hw/virtio/virtio.h | 1 -
hw/block/virtio-blk.c | 6 +-----
hw/scsi/virtio-scsi.c | 6 +-----
hw/virtio/virtio.c | 28 +++++++++++++---------------
hw/virtio/trace-events | 1 -
5 files changed, 15 insertions(+), 27 deletions(-)
diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
index c594764f23..8435f5e8fc 100644
--- a/include/hw/virtio/virtio.h
+++ b/include/hw/virtio/virtio.h
@@ -290,7 +290,6 @@ int virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
unsigned int *out_bytes, unsigned max_in_bytes,
unsigned max_out_bytes);
-void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq);
void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
int virtio_save(VirtIODevice *vdev, QEMUFile *f);
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
index 9bab2716c1..64efce4846 100644
--- a/hw/block/virtio-blk.c
+++ b/hw/block/virtio-blk.c
@@ -62,11 +62,7 @@ void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
iov_discard_undo(&req->inhdr_undo);
iov_discard_undo(&req->outhdr_undo);
virtqueue_push(req->vq, &req->elem, req->in_len);
- if (qemu_in_iothread()) {
- virtio_notify_irqfd(vdev, req->vq);
- } else {
- virtio_notify(vdev, req->vq);
- }
+ virtio_notify(vdev, req->vq);
}
static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c
index 34ae14f7bf..d817fc42b4 100644
--- a/hw/scsi/virtio-scsi.c
+++ b/hw/scsi/virtio-scsi.c
@@ -116,11 +116,7 @@ static void virtio_scsi_complete_req(VirtIOSCSIReq *req, QemuMutex *vq_lock)
}
virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size);
- if (s->dataplane_started && !s->dataplane_fenced) {
- virtio_notify_irqfd(vdev, vq);
- } else {
- virtio_notify(vdev, vq);
- }
+ virtio_notify(vdev, vq);
if (vq_lock) {
qemu_mutex_unlock(vq_lock);
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 9a81ad912e..13830debfd 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -31,6 +31,7 @@
#include "hw/qdev-properties.h"
#include "hw/virtio/virtio-access.h"
#include "system/dma.h"
+#include "system/iothread.h"
#include "system/runstate.h"
#include "virtio-qmp.h"
@@ -2654,16 +2655,8 @@ static void virtio_notify_irqfd_deferred_fn(void *opaque)
event_notifier_set(notifier);
}
-void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
+static void virtio_irq(VirtQueue *vq)
{
- WITH_RCU_READ_LOCK_GUARD() {
- if (!virtio_should_notify(vdev, vq)) {
- return;
- }
- }
-
- trace_virtio_notify_irqfd(vdev, vq);
-
/*
* virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
* windows drivers included in virtio-win 1.8.0 (circa 2015) are
@@ -2680,13 +2673,18 @@ void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
* to an atomic operation.
*/
virtio_set_isr(vq->vdev, 0x1);
- defer_call(virtio_notify_irqfd_deferred_fn, &vq->guest_notifier);
-}
-static void virtio_irq(VirtQueue *vq)
-{
- virtio_set_isr(vq->vdev, 0x1);
- virtio_notify_vector(vq->vdev, vq->vector);
+ /*
+ * The interrupt code path requires the Big QEMU Lock (BQL), so use the
+ * notifier instead when in an IOThread. This assumes that device models
+ * have already called ->set_guest_notifiers() sometime before calling this
+ * function.
+ */
+ if (qemu_in_iothread()) {
+ defer_call(virtio_notify_irqfd_deferred_fn, &vq->guest_notifier);
+ } else {
+ virtio_notify_vector(vq->vdev, vq->vector);
+ }
}
void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events
index 76f0d458b2..658cc365e7 100644
--- a/hw/virtio/trace-events
+++ b/hw/virtio/trace-events
@@ -75,7 +75,6 @@ virtqueue_flush(void *vq, unsigned int count) "vq %p count %u"
virtqueue_pop(void *vq, void *elem, unsigned int in_num, unsigned int out_num) "vq %p elem %p in_num %u out_num %u"
virtio_queue_notify(void *vdev, int n, void *vq) "vdev %p n %d vq %p"
virtio_notify_irqfd_deferred_fn(void *vdev, void *vq) "vdev %p vq %p"
-virtio_notify_irqfd(void *vdev, void *vq) "vdev %p vq %p"
virtio_notify(void *vdev, void *vq) "vdev %p vq %p"
virtio_set_status(void *vdev, uint8_t val) "vdev %p val %u"
--
2.51.0
next prev parent reply other threads:[~2025-09-22 22:02 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-22 22:01 [PATCH 0/5] virtio: allow virtio_error() in IOThreads Stefan Hajnoczi
2025-09-22 22:01 ` [PATCH 1/5] vhost: use virtio_config_get_guest_notifier() Stefan Hajnoczi
2025-09-23 8:33 ` Philippe Mathieu-Daudé
2025-09-22 22:01 ` Stefan Hajnoczi [this message]
2025-09-23 8:41 ` [PATCH 2/5] virtio: unify virtio_notify_irqfd() and virtio_notify() Philippe Mathieu-Daudé
2025-09-22 22:01 ` [PATCH 3/5] virtio: support irqfd in virtio_notify_config() Stefan Hajnoczi
2025-09-22 22:01 ` [PATCH 4/5] tests/libqos: extract qvirtqueue_set_avail_idx() Stefan Hajnoczi
2025-09-23 8:45 ` Philippe Mathieu-Daudé
2025-09-23 13:07 ` Fabiano Rosas
2025-09-22 22:01 ` [PATCH 5/5] tests/virtio-scsi: add a virtio_error() IOThread test Stefan Hajnoczi
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=20250922220149.498967-3-stefanha@redhat.com \
--to=stefanha@redhat.com \
--cc=fam@euphon.net \
--cc=farosas@suse.de \
--cc=hreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=lvivier@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=sgarzare@redhat.com \
/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;
as well as URLs for NNTP newsgroup(s).