From 4720cbeea1f42fd905fc69338fd42b191e58b412 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Mon, 7 Jan 2019 13:02:48 +0100 Subject: block: Fix hangs in synchronous APIs with iothreads In the block layer, synchronous APIs are often implemented by creating a coroutine that calls the asynchronous coroutine-based implementation and then waiting for completion with BDRV_POLL_WHILE(). For this to work with iothreads (more specifically, when the synchronous API is called in a thread that is not the home thread of the block device, so that the coroutine will run in a different thread), we must make sure to call aio_wait_kick() at the end of the operation. Many places are missing this, so that BDRV_POLL_WHILE() keeps hanging even if the condition has long become false. Note that bdrv_dec_in_flight() involves an aio_wait_kick() call. This corresponds to the BDRV_POLL_WHILE() in the drain functions, but it is generally not enough for most other operations because they haven't set the return value in the coroutine entry stub yet. To avoid race conditions there, we need to kick after setting the return value. The race window is small enough that the problem doesn't usually surface in the common path. However, it does surface and causes easily reproducible hangs if the operation can return early before even calling bdrv_inc/dec_in_flight, which many of them do (trivial error or no-op success paths). The bug in bdrv_truncate(), bdrv_check() and bdrv_invalidate_cache() is slightly different: These functions even neglected to schedule the coroutine in the home thread of the node. This avoids the hang, but is obviously wrong, too. Fix those to schedule the coroutine in the right AioContext in addition to adding aio_wait_kick() calls. Cc: qemu-stable@nongnu.org Signed-off-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi --- block/nvme.c | 1 + 1 file changed, 1 insertion(+) (limited to 'block/nvme.c') diff --git a/block/nvme.c b/block/nvme.c index 982097b5b1..b5952c9b08 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -390,6 +390,7 @@ static void nvme_cmd_sync_cb(void *opaque, int ret) { int *pret = opaque; *pret = ret; + aio_wait_kick(); } static int nvme_cmd_sync(BlockDriverState *bs, NVMeQueuePair *q, -- cgit v1.2.3