Just forward the request to bdrv_aio_cancel_async. Use a flag to fix the ret value in completion code. Also check memory address before calling dma_memory_unmap.
Signed-off-by: Fam Zheng <f...@redhat.com> --- dma-helpers.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/dma-helpers.c b/dma-helpers.c index 499b52b..f9c1a79 100644 --- a/dma-helpers.c +++ b/dma-helpers.c @@ -74,6 +74,7 @@ typedef struct { uint64_t sector_num; DMADirection dir; bool in_cancel; + bool cancelled; int sg_cur_index; dma_addr_t sg_cur_byte; QEMUIOVector iov; @@ -105,6 +106,9 @@ static void dma_bdrv_unmap(DMAAIOCB *dbs) int i; for (i = 0; i < dbs->iov.niov; ++i) { + if (!(dbs->iov.iov[i].iov_base && dbs->iov.iov[i].iov_len)) { + break; + } dma_memory_unmap(dbs->sg->as, dbs->iov.iov[i].iov_base, dbs->iov.iov[i].iov_len, dbs->dir, dbs->iov.iov[i].iov_len); @@ -141,6 +145,9 @@ static void dma_bdrv_cb(void *opaque, int ret) trace_dma_bdrv_cb(dbs, ret); + if (dbs->cancelled) { + ret = -ECANCELED; + } dbs->acb = NULL; dbs->sector_num += dbs->iov.size / 512; @@ -185,6 +192,7 @@ static void dma_aio_cancel(BlockDriverAIOCB *acb) trace_dma_aio_cancel(dbs); + dbs->cancelled = true; if (dbs->acb) { BlockDriverAIOCB *acb = dbs->acb; dbs->acb = NULL; @@ -196,9 +204,24 @@ static void dma_aio_cancel(BlockDriverAIOCB *acb) dma_complete(dbs, 0); } +static void dma_aio_cancel_async(BlockDriverAIOCB *acb) +{ + DMAAIOCB *dbs = container_of(acb, DMAAIOCB, common); + + trace_dma_aio_cancel(dbs); + + dbs->cancelled = true; + if (dbs->acb) { + acb = dbs->acb; + bdrv_aio_cancel_async(acb); + } +} + + static const AIOCBInfo dma_aiocb_info = { .aiocb_size = sizeof(DMAAIOCB), .cancel = dma_aio_cancel, + .cancel_async = dma_aio_cancel_async, }; BlockDriverAIOCB *dma_bdrv_io( @@ -218,6 +241,7 @@ BlockDriverAIOCB *dma_bdrv_io( dbs->sg_cur_byte = 0; dbs->dir = dir; dbs->in_cancel = false; + dbs->cancelled = false; dbs->io_func = io_func; dbs->bh = NULL; qemu_iovec_init(&dbs->iov, sg->nsg); -- 2.1.0