[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 07/16] scsi: Await request purging
From: |
Kevin Wolf |
Subject: |
[PULL 07/16] scsi: Await request purging |
Date: |
Wed, 7 Feb 2024 22:55:57 +0100 |
From: Hanna Czenczek <hreitz@redhat.com>
scsi_device_for_each_req_async() currently does not provide any way to
be awaited. One of its callers is scsi_device_purge_requests(), which
therefore currently does not guarantee that all requests are fully
settled when it returns.
We want all requests to be settled, because scsi_device_purge_requests()
is called through the unrealize path, including the one invoked by
virtio_scsi_hotunplug() through qdev_simple_device_unplug_cb(), which
most likely assumes that all SCSI requests are done then.
In fact, scsi_device_purge_requests() already contains a blk_drain(),
but this will not fully await scsi_device_for_each_req_async(), only the
I/O requests it potentially cancels (not the non-I/O requests).
However, we can have scsi_device_for_each_req_async() increment the BB
in-flight counter, and have scsi_device_for_each_req_async_bh()
decrement it when it is done. This way, the blk_drain() will fully
await all SCSI requests to be purged.
This also removes the need for scsi_device_for_each_req_async_bh() to
double-check the current context and potentially re-schedule itself,
should it now differ from the BB's context: Changing a BB's AioContext
with a root node is done through bdrv_try_change_aio_context(), which
creates a drained section. With this patch, we keep the BB in-flight
counter elevated throughout, so we know the BB's context cannot change.
Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
Message-ID: <20240202144755.671354-3-hreitz@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
hw/scsi/scsi-bus.c | 30 +++++++++++++++++++++---------
1 file changed, 21 insertions(+), 9 deletions(-)
diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c
index 0a2eb11c56..230313022c 100644
--- a/hw/scsi/scsi-bus.c
+++ b/hw/scsi/scsi-bus.c
@@ -120,17 +120,13 @@ static void scsi_device_for_each_req_async_bh(void
*opaque)
SCSIRequest *next;
/*
- * If the AioContext changed before this BH was called then reschedule into
- * the new AioContext before accessing ->requests. This can happen when
- * scsi_device_for_each_req_async() is called and then the AioContext is
- * changed before BHs are run.
+ * The BB cannot have changed contexts between this BH being scheduled and
+ * now: BBs' AioContexts, when they have a node attached, can only be
+ * changed via bdrv_try_change_aio_context(), in a drained section. While
+ * we have the in-flight counter incremented, that drain must block.
*/
ctx = blk_get_aio_context(s->conf.blk);
- if (ctx != qemu_get_current_aio_context()) {
- aio_bh_schedule_oneshot(ctx, scsi_device_for_each_req_async_bh,
- g_steal_pointer(&data));
- return;
- }
+ assert(ctx == qemu_get_current_aio_context());
QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
data->fn(req, data->fn_opaque);
@@ -138,11 +134,16 @@ static void scsi_device_for_each_req_async_bh(void
*opaque)
/* Drop the reference taken by scsi_device_for_each_req_async() */
object_unref(OBJECT(s));
+
+ /* Paired with blk_inc_in_flight() in scsi_device_for_each_req_async() */
+ blk_dec_in_flight(s->conf.blk);
}
/*
* Schedule @fn() to be invoked for each enqueued request in device @s. @fn()
* runs in the AioContext that is executing the request.
+ * Keeps the BlockBackend's in-flight counter incremented until everything is
+ * done, so draining it will settle all scheduled @fn() calls.
*/
static void scsi_device_for_each_req_async(SCSIDevice *s,
void (*fn)(SCSIRequest *, void *),
@@ -163,6 +164,8 @@ static void scsi_device_for_each_req_async(SCSIDevice *s,
*/
object_ref(OBJECT(s));
+ /* Paired with blk_dec_in_flight() in scsi_device_for_each_req_async_bh()
*/
+ blk_inc_in_flight(s->conf.blk);
aio_bh_schedule_oneshot(blk_get_aio_context(s->conf.blk),
scsi_device_for_each_req_async_bh,
data);
@@ -1728,11 +1731,20 @@ static void scsi_device_purge_one_req(SCSIRequest *req,
void *opaque)
scsi_req_cancel_async(req, NULL);
}
+/**
+ * Cancel all requests, and block until they are deleted.
+ */
void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
{
scsi_device_for_each_req_async(sdev, scsi_device_purge_one_req, NULL);
+ /*
+ * Await all the scsi_device_purge_one_req() calls scheduled by
+ * scsi_device_for_each_req_async(), and all I/O requests that were
+ * cancelled this way, but may still take a bit of time to settle.
+ */
blk_drain(sdev->conf.blk);
+
scsi_device_set_ua(sdev, sense);
}
--
2.43.0
- [PULL 00/16] Block layer patches, Kevin Wolf, 2024/02/07
- [PULL 02/16] virtio-blk: clarify that there is at least 1 virtqueue, Kevin Wolf, 2024/02/07
- [PULL 04/16] virtio-blk: declare VirtIOBlock::rq with a type, Kevin Wolf, 2024/02/07
- [PULL 05/16] monitor: use aio_co_reschedule_self(), Kevin Wolf, 2024/02/07
- [PULL 01/16] virtio-blk: enforce iothread-vq-mapping validation, Kevin Wolf, 2024/02/07
- [PULL 06/16] block-backend: Allow concurrent context changes, Kevin Wolf, 2024/02/07
- [PULL 03/16] virtio-blk: add vq_rq[] bounds check in virtio_blk_dma_restart_cb(), Kevin Wolf, 2024/02/07
- [PULL 07/16] scsi: Await request purging,
Kevin Wolf <=
- [PULL 10/16] virtio-blk: do not use C99 mixed declarations, Kevin Wolf, 2024/02/07
- [PULL 08/16] iotests: fix leak of tmpdir in dry-run mode, Kevin Wolf, 2024/02/07
- [PULL 11/16] scsi: Don't ignore most usb-storage properties, Kevin Wolf, 2024/02/07
- [PULL 13/16] virtio-scsi: Attach event vq notifier with no_poll, Kevin Wolf, 2024/02/07
- [PULL 09/16] iotests: give tempdir an identifying name, Kevin Wolf, 2024/02/07
- [PULL 12/16] blkio: Respect memory-alignment for bounce buffer allocations, Kevin Wolf, 2024/02/07
- [PULL 16/16] virtio-blk: avoid using ioeventfd state in irqfd conditional, Kevin Wolf, 2024/02/07
- [PULL 14/16] virtio: Re-enable notifications after drain, Kevin Wolf, 2024/02/07
- [PULL 15/16] virtio-blk: Use ioeventfd_attach in start_ioeventfd, Kevin Wolf, 2024/02/07
- Re: [PULL 00/16] Block layer patches, Peter Maydell, 2024/02/09