virtio: Re-enable notifications after drain
v2 cover virtio: Re-enable notifications after drain
v1:
https://lists.nongnu.org/archive/html/qemu-block/2024-01/msg00336.html
Hi,
This is basically the same series as v1: When using aio_set_event_notifier_poll(), the io_poll_end() callback is only invoked when polling ends, not when the notifier is being removed while in a polling section. This can leave the virtqueue notifier disabled during drained sections, which however is not a bad thing. We just need to ensure they are re-enabled after the drain, and kick the virtqueue once to pick up all the requests that came in during the drained section.
Patch 1 is a technically unrelated fix, but addresses a problem that became visible with patch 2 applied.
Patch 3 is a small (optional) clean-up patch.
v2:
- Changed the title of this series and patch 2 (was: "Keep notifications disabled durin drain"): Keeping the notifier disabled was something the initial RFC did, this version (v1 too) just ensures the notifier is enabled after the drain, regardless of its state before.
- Use event_notifier_set() instead of virtio_queue_notify() in patch 2
- Added patch 3
Hanna Czenczek (3):
virtio-scsi: Attach event vq notifier with no_poll
virtio: Re-enable notifications after drain
virtio-blk: Use ioeventfd_attach in start_ioeventfd
include/block/aio.h | 7 ++++++- hw/block/virtio-blk.c | 21 ++++++++++----------- hw/scsi/virtio-scsi.c | 7 ++++++- hw/virtio/virtio.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 13 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 virtio-scsi: Attach event vq notifier with no_poll
As of commit 38738f7dbbda90fbc161757b7f4be35b52205552 ("virtio-scsi:
don't waste CPU polling the event virtqueue"), we only attach an io_read notifier for the virtio-scsi event virtqueue instead, and no polling notifiers. During operation, the event virtqueue is typically non-empty, but none of the buffers are intended to be used immediately. Instead, they only get used when certain events occur. Therefore, it makes no sense to continuously poll it when non-empty, because it is supposed to be and stay non-empty.
We do this by using virtio_queue_aio_attach_host_notifier_no_poll() instead of virtio_queue_aio_attach_host_notifier() for the event virtqueue.
Commit 766aa2de0f29b657148e04599320d771c36fd126 ("virtio-scsi: implement BlockDevOps->drained_begin()") however has virtio_scsi_drained_end() use virtio_queue_aio_attach_host_notifier() for all virtqueues, including the event virtqueue. This can lead to it being polled again, undoing the benefit of commit 38738f7dbbda90fbc161757b7f4be35b52205552.
Fix it by using virtio_queue_aio_attach_host_notifier_no_poll() for the event virtqueue.
("virtio-scsi: implement BlockDevOps->drained_begin()")--- hw/scsi/virtio-scsi.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +6 -1
@@ -1149,6 +1149,7 @@ static void virtio_scsi_drained_begin(SCSIBus *bus)static void virtio_scsi_drained_end (SCSIBus *bus) {VirtIOSCSI *s =container_of (bus, VirtIOSCSI, bus); +VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (s);VirtIODevice *vdev =VIRTIO_DEVICE (s);uint32_t total_queues =VIRTIO_SCSI_VQ_NUM_FIXED + s->parent_obj.conf.num_queues;
@@ -1166,7 +1167,11 @@ static void virtio_scsi_drained_end(SCSIBus *bus)for (uint32_t i =0 ; i < total_queues; i++) {VirtQueue *vq =virtio_get_queue (vdev, i); -virtio_queue_aio_attach_host_notifier (vq, s->ctx); +if (vq == vs->event_vq) { +virtio_queue_aio_attach_host_notifier_no_poll (vq, s->ctx); + }else { +virtio_queue_aio_attach_host_notifier (vq, s->ctx); + } } }
Signature · 1 line
2.43.0
PATCH v2 2/3 virtio: Re-enable notifications after drain
In reply to Hanna CzenczekDuring drain, we do not care about virtqueue notifications, which is why we remove the handlers on it. When removing those handlers, whether vq notifications are enabled or not depends on whether we were in polling mode or not; if not, they are enabled (by default); if so, they have been disabled by the io_poll_start callback.
Because we do not care about those notifications after removing the handlers, this is fine. However, we have to explicitly ensure they are enabled when re-attaching the handlers, so we will resume receiving notifications. We do this in virtio_queue_aio_attach_host_notifier*(). If such a function is called while we are in a polling section, attaching the notifiers will then invoke the io_poll_start callback, re-disabling notifications.
Because we will always miss virtqueue updates in the drained section, we also need to poll the virtqueue once after attaching the notifiers.
--- include/block/aio.h | 7 ++++++- hw/virtio/virtio.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-)
include/block/aio.h +6 -1
@@ -480,9 +480,14 @@ void aio_set_event_notifier(AioContext *ctx, AioPollFn *io_poll, EventNotifierHandler *io_poll_ready); -/* Set polling begin/end callbacks for an event notifier that has already been +/* +* Set polling begin/end callbacks for an event notifier that has already been * registered with aio_set_event_notifier. Do nothing if the event notifier is * not registered. +* +* Note that if the io_poll_end() callback (or the entire notifier) is removed +* during polling, it will not be called, so an io_poll_begin() is not +* necessarily always followed by an io_poll_end(). */ void aio_set_event_notifier_poll(AioContext *ctx, EventNotifier *notifier,
hw/virtio/virtio.c +42 -0
68 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 3/3 virtio-blk: Use ioeventfd_attach in start_ioeventfd
In reply to Hanna CzenczekCommit d3f6f294aeadd5f88caf0155e4360808c95b3146 ("virtio-blk: always set ioeventfd during startup") has made virtio_blk_start_ioeventfd() always kick the virtqueue (set the ioeventfd), regardless of whether the BB is drained. That is no longer necessary, because attaching the host notifier will now set the ioeventfd, too; this happens either immediately right here in virtio_blk_start_ioeventfd(), or later when the drain ends, in virtio_blk_ioeventfd_attach().
With event_notifier_set() removed, the code becomes the same as the one in virtio_blk_ioeventfd_attach(), so we can reuse that function.
--- hw/block/virtio-blk.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
hw/block/virtio-blk.c +10 -11
@@ -37,6 +37,8 @@#include "hw/virtio/virtio-blk-common.h" #include "qemu/coroutine.h" +static void virtio_blk_ioeventfd_attach (VirtIOBlock *s); +static void virtio_blk_init_request (VirtIOBlock *s,VirtQueue *vq,VirtIOBlockReq *req) {
@@ -1808,17 +1810,14 @@ static int virtio_blk_start_ioeventfd(VirtIODevice *vdev) s->ioeventfd_started = true;smp_wmb ();/* paired with aio_notify_accept() on the read side */ -/* Get this show started by hooking up our callbacks */ -for (i =0 ; i < nvqs; i++) { -VirtQueue *vq =virtio_get_queue (vdev, i); -AioContext *ctx = s->vq_aio_context[i]; - -/* Kick right away to begin processing requests already in vring */ -event_notifier_set (virtio_queue_get_host_notifier (vq)); - -if (!blk_in_drain (s->conf.conf.blk)) { -virtio_queue_aio_attach_host_notifier (vq, ctx); - } +/* +* Get this show started by hooking up our callbacks. If drained now, +* virtio_blk_drained_end() will do this later. +* Attaching the notifier also kicks the virtqueues, processing any requests +* they may already have. +*/ +if (!blk_in_drain (s->conf.conf.blk)) { +virtio_blk_ioeventfd_attach (s); }return 0 ;
Signature · 1 line
2.43.0
Re: [PATCH v2 3/3] virtio-blk: Use ioeventfd_attach in start_ioeventfd
02.02.2024 18:31, Hanna Czenczek :
Quoted 10 lines
Commit
d3f6f294aeadd5f88caf0155e4360808c95b3146("virtio-blk: always set ioeventfd during startup") has madevirtio_blk_start_ioeventfd()always kick the virtqueue (set the ioeventfd), regardless of whether the BB is drained. That is no longer necessary, because attaching the host notifier will now set the ioeventfd, too; this happens either immediately right here invirtio_blk_start_ioeventfd(), or later when the drain ends, invirtio_blk_ioeventfd_attach().With
event_notifier_set()removed, the code becomes the same as the one invirtio_blk_ioeventfd_attach(), so we can reuse that function.
The mentioned comit is v8.2.0-812-gd3f6f294ae, - ie, past 8.2. Is this new change still relevant for stable?
Thanks,
/mjt
Re: [PATCH v2 3/3] virtio-blk: Use ioeventfd_attach in start_ioeventfd
Quoted 9 lines · Michael Tokarev
02.02.2024 18:31, Hanna Czenczek :
Quoted 8 lines
Commit
d3f6f294aeadd5f88caf0155e4360808c95b3146("virtio-blk: always set ioeventfd during startup") has madevirtio_blk_start_ioeventfd()always kick the virtqueue (set the ioeventfd), regardless of whether the BB is drained. That is no longer necessary, because attaching the host notifier will now set the ioeventfd, too; this happens either immediately right here invirtio_blk_start_ioeventfd(), or later when the drain ends, invirtio_blk_ioeventfd_attach().
Quoted 5 lines
Quoted 2 lines
With
event_notifier_set()removed, the code becomes the same as the one invirtio_blk_ioeventfd_attach(), so we can reuse that function.The mentioned comit is v8.2.0-812-gd3f6f294ae, - ie, past 8.2. Is this new change still relevant for stable?
Sorry again. :/ This patch is a clean-up patch that won’t apply to 8.2. Now, 8.2 does have basically the same logic as described in the patch message (d3f6f294aea restored it after it was broken), so a similar patch could be made for it (removing the event_notifier_set() from virtio_blk_data_plane_start()), but whether we kick the virtqueues once or twice on start-up probably won’t make a difference, certainly not in terms of correctness.
Hanna
Re: [PATCH v2 3/3] virtio-blk: Use ioeventfd_attach in start_ioeventfd
09.02.2024 20:11, Hanna Czenczek :
Quoted 4 lines
Quoted 2 lines
The mentioned comit is v8.2.0-812-gd3f6f294ae, - ie, past 8.2. Is this new change still relevant for stable?
Sorry again. :/
There's nothing to be sorry about here - it's regular work, and is quite good at it, - I just asked to be sure, maybe I misunderstood something.
Quoted 2 lines
This patch is a clean-up patch that won’t apply to 8.2. Now, 8.2 does have basically the same logic as described in the patch
message (d3f6f294aearestored it after it was broken), so a similar patch could be made for it (removing theevent_notifier_set()fromvirtio_blk_data_plane_start()), but whether we kick the virtqueues once or twice on start-up probably won’t make a difference, certainly not in terms of correctness.
Ok, excellent, this makes good sense now.
I'm not including this one in stable-8.2 :)
Thank you very much for the excellent work and
the clarification!
/mjt
Quoted 38 lines · Hanna Czenczek
v1:
https://lists.nongnu.org/archive/html/qemu-block/2024-01/msg00336.html
Hi,
This is basically the same series as v1: When using
aio_set_event_notifier_poll(), theio_poll_end()callback is only invoked when polling ends, not when the notifier is being removed while in a polling section. This can leave the virtqueue notifier disabled during drained sections, which however is not a bad thing. We just need to ensure they are re-enabled after the drain, and kick the virtqueue once to pick up all the requests that came in during the drained section.Patch 1 is a technically unrelated fix, but addresses a problem that became visible with patch 2 applied.
Patch 3 is a small (optional) clean-up patch.
v2:
- Changed the title of this series and patch 2 (was: "Keep notifications disabled durin drain"): Keeping the notifier disabled was something the initial RFC did, this version (v1 too) just ensures the notifier is enabled after the drain, regardless of its state before.- Use
event_notifier_set()instead ofvirtio_queue_notify()in patch 2- Added patch 3
Hanna Czenczek (3):
virtio-scsi: Attach event vq notifier withno_poll
virtio: Re-enable notifications after drain
virtio-blk: Useioeventfd_attachinstart_ioeventfd
Quoted 9 lines
include/block/aio.h | 7 ++++++- hw/block/virtio-blk.c | 21 ++++++++++----------- hw/scsi/virtio-scsi.c | 7 ++++++- hw/virtio/virtio.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 13 deletions(-)Signature · 1 line
2.43.0
Kevin Wolf
AppliedAm 02.02.2024 um 16:31 hat Hanna Czenczek geschrieben:
Quoted 10 lines
Hanna Czenczek (3):
virtio-scsi: Attach event vq notifier withno_poll
virtio: Re-enable notifications after drain
virtio-blk: Useioeventfd_attachinstart_ioeventfdinclude/block/aio.h | 7 ++++++- hw/block/virtio-blk.c | 21 ++++++++++----------- hw/scsi/virtio-scsi.c | 7 ++++++- hw/virtio/virtio.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 13 deletions(-)
Thanks, applied to the block branch.
Kevin