|
|
97168e |
From 103608465b8bd2edf7f9aaef5c3c93309ccf9ec2 Mon Sep 17 00:00:00 2001
|
|
|
97168e |
From: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
97168e |
Date: Tue, 21 Feb 2023 16:22:17 -0500
|
|
|
97168e |
Subject: [PATCH 12/13] dma-helpers: prevent dma_blk_cb() vs dma_aio_cancel()
|
|
|
97168e |
race
|
|
|
97168e |
|
|
|
97168e |
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
97168e |
RH-MergeRequest: 264: scsi: protect req->aiocb with AioContext lock
|
|
|
97168e |
RH-Bugzilla: 2090990
|
|
|
97168e |
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
|
|
|
97168e |
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
97168e |
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
97168e |
RH-Commit: [2/3] 14f5835093ba8c5111f3ada2fe87730371aca733
|
|
|
97168e |
|
|
|
97168e |
dma_blk_cb() only takes the AioContext lock around ->io_func(). That
|
|
|
97168e |
means the rest of dma_blk_cb() is not protected. In particular, the
|
|
|
97168e |
DMAAIOCB field accesses happen outside the lock.
|
|
|
97168e |
|
|
|
97168e |
There is a race when the main loop thread holds the AioContext lock and
|
|
|
97168e |
invokes scsi_device_purge_requests() -> bdrv_aio_cancel() ->
|
|
|
97168e |
dma_aio_cancel() while an IOThread executes dma_blk_cb(). The dbs->acb
|
|
|
97168e |
field determines how cancellation proceeds. If dma_aio_cancel() sees
|
|
|
97168e |
dbs->acb == NULL while dma_blk_cb() is still running, the request can be
|
|
|
97168e |
completed twice (-ECANCELED and the actual return value).
|
|
|
97168e |
|
|
|
97168e |
The following assertion can occur with virtio-scsi when an IOThread is
|
|
|
97168e |
used:
|
|
|
97168e |
|
|
|
97168e |
../hw/scsi/scsi-disk.c:368: scsi_dma_complete: Assertion `r->req.aiocb != NULL' failed.
|
|
|
97168e |
|
|
|
97168e |
Fix the race by holding the AioContext across dma_blk_cb(). Now
|
|
|
97168e |
dma_aio_cancel() under the AioContext lock will not see
|
|
|
97168e |
inconsistent/intermediate states.
|
|
|
97168e |
|
|
|
97168e |
Cc: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
97168e |
Reviewed-by: Eric Blake <eblake@redhat.com>
|
|
|
97168e |
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
97168e |
Message-Id: <20230221212218.1378734-3-stefanha@redhat.com>
|
|
|
97168e |
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
97168e |
(cherry picked from commit abfcd2760b3e70727bbc0792221b8b98a733dc32)
|
|
|
97168e |
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
97168e |
---
|
|
|
97168e |
hw/scsi/scsi-disk.c | 4 +---
|
|
|
97168e |
softmmu/dma-helpers.c | 12 +++++++-----
|
|
|
97168e |
2 files changed, 8 insertions(+), 8 deletions(-)
|
|
|
97168e |
|
|
|
97168e |
diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
|
|
|
97168e |
index 179ce22c4a..c8109a673e 100644
|
|
|
97168e |
--- a/hw/scsi/scsi-disk.c
|
|
|
97168e |
+++ b/hw/scsi/scsi-disk.c
|
|
|
97168e |
@@ -351,13 +351,12 @@ done:
|
|
|
97168e |
scsi_req_unref(&r->req);
|
|
|
97168e |
}
|
|
|
97168e |
|
|
|
97168e |
+/* Called with AioContext lock held */
|
|
|
97168e |
static void scsi_dma_complete(void *opaque, int ret)
|
|
|
97168e |
{
|
|
|
97168e |
SCSIDiskReq *r = (SCSIDiskReq *)opaque;
|
|
|
97168e |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
|
|
|
97168e |
|
|
|
97168e |
- aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
|
|
|
97168e |
-
|
|
|
97168e |
assert(r->req.aiocb != NULL);
|
|
|
97168e |
r->req.aiocb = NULL;
|
|
|
97168e |
|
|
|
97168e |
@@ -367,7 +366,6 @@ static void scsi_dma_complete(void *opaque, int ret)
|
|
|
97168e |
block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
|
|
|
97168e |
}
|
|
|
97168e |
scsi_dma_complete_noio(r, ret);
|
|
|
97168e |
- aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
|
|
|
97168e |
}
|
|
|
97168e |
|
|
|
97168e |
static void scsi_read_complete_noio(SCSIDiskReq *r, int ret)
|
|
|
97168e |
diff --git a/softmmu/dma-helpers.c b/softmmu/dma-helpers.c
|
|
|
97168e |
index 7d766a5e89..42af18719a 100644
|
|
|
97168e |
--- a/softmmu/dma-helpers.c
|
|
|
97168e |
+++ b/softmmu/dma-helpers.c
|
|
|
97168e |
@@ -127,17 +127,19 @@ static void dma_complete(DMAAIOCB *dbs, int ret)
|
|
|
97168e |
static void dma_blk_cb(void *opaque, int ret)
|
|
|
97168e |
{
|
|
|
97168e |
DMAAIOCB *dbs = (DMAAIOCB *)opaque;
|
|
|
97168e |
+ AioContext *ctx = dbs->ctx;
|
|
|
97168e |
dma_addr_t cur_addr, cur_len;
|
|
|
97168e |
void *mem;
|
|
|
97168e |
|
|
|
97168e |
trace_dma_blk_cb(dbs, ret);
|
|
|
97168e |
|
|
|
97168e |
+ aio_context_acquire(ctx);
|
|
|
97168e |
dbs->acb = NULL;
|
|
|
97168e |
dbs->offset += dbs->iov.size;
|
|
|
97168e |
|
|
|
97168e |
if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) {
|
|
|
97168e |
dma_complete(dbs, ret);
|
|
|
97168e |
- return;
|
|
|
97168e |
+ goto out;
|
|
|
97168e |
}
|
|
|
97168e |
dma_blk_unmap(dbs);
|
|
|
97168e |
|
|
|
97168e |
@@ -177,9 +179,9 @@ static void dma_blk_cb(void *opaque, int ret)
|
|
|
97168e |
|
|
|
97168e |
if (dbs->iov.size == 0) {
|
|
|
97168e |
trace_dma_map_wait(dbs);
|
|
|
97168e |
- dbs->bh = aio_bh_new(dbs->ctx, reschedule_dma, dbs);
|
|
|
97168e |
+ dbs->bh = aio_bh_new(ctx, reschedule_dma, dbs);
|
|
|
97168e |
cpu_register_map_client(dbs->bh);
|
|
|
97168e |
- return;
|
|
|
97168e |
+ goto out;
|
|
|
97168e |
}
|
|
|
97168e |
|
|
|
97168e |
if (!QEMU_IS_ALIGNED(dbs->iov.size, dbs->align)) {
|
|
|
97168e |
@@ -187,11 +189,11 @@ static void dma_blk_cb(void *opaque, int ret)
|
|
|
97168e |
QEMU_ALIGN_DOWN(dbs->iov.size, dbs->align));
|
|
|
97168e |
}
|
|
|
97168e |
|
|
|
97168e |
- aio_context_acquire(dbs->ctx);
|
|
|
97168e |
dbs->acb = dbs->io_func(dbs->offset, &dbs->iov,
|
|
|
97168e |
dma_blk_cb, dbs, dbs->io_func_opaque);
|
|
|
97168e |
- aio_context_release(dbs->ctx);
|
|
|
97168e |
assert(dbs->acb);
|
|
|
97168e |
+out:
|
|
|
97168e |
+ aio_context_release(ctx);
|
|
|
97168e |
}
|
|
|
97168e |
|
|
|
97168e |
static void dma_aio_cancel(BlockAIOCB *acb)
|
|
|
97168e |
--
|
|
|
97168e |
2.37.3
|
|
|
97168e |
|