From patchwork Fri Apr 1 09:46:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 8722031 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id F0B47C0553 for ; Fri, 1 Apr 2016 09:46:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 54D61203B5 for ; Fri, 1 Apr 2016 09:46:59 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 84E6920398 for ; Fri, 1 Apr 2016 09:46:58 +0000 (UTC) Received: from localhost ([::1]:43024 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alvfM-00039Z-R2 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 01 Apr 2016 05:46:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42287) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alvfF-00038W-7q for qemu-devel@nongnu.org; Fri, 01 Apr 2016 05:46:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1alvfE-0006Cu-4T for qemu-devel@nongnu.org; Fri, 01 Apr 2016 05:46:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44374) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alvf9-0006C8-NN; Fri, 01 Apr 2016 05:46:43 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7391EC0467EC; Fri, 1 Apr 2016 09:46:42 +0000 (UTC) Received: from lemon.redhat.com (vpn1-7-128.pek2.redhat.com [10.72.7.128]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u319kcJM027668; Fri, 1 Apr 2016 05:46:39 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Fri, 1 Apr 2016 17:46:38 +0800 Message-Id: <1459503998-31592-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Cc: Kevin Wolf , lvivier@redhat.com, qemu-block@nongnu.org, Stefan Hajnoczi , pbonzini@redhat.com Subject: [Qemu-devel] [PATCH] block: Fix bdrv_drain in coroutine X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Using the nested aio_poll() in coroutine is a bad idea. This patch replaces the aio_poll loop in bdrv_drain with a BH, if called in coroutine. For example, the bdrv_drain() in mirror.c can hang when a guest issued request is pending on it in qemu_co_mutex_lock(). Mirror coroutine in this case has just finished a request, and the block job is about to complete. It calls bdrv_drain() which waits for the other coroutine to complete. The other coroutine is a scsi-disk request. The deadlock happens when the latter is in turn pending on the former to yield/terminate, qemu_co_mutex_lock(). The state flow is as below (assuming a qcow2 image): mirror coroutine scsi-disk coroutine ------------------------------------------------------------- do last write qcow2:qemu_co_mutex_lock() ... scsi disk read tracked request begin qcow2:qemu_co_mutex_lock.enter qcow2:qemu_co_mutex_unlock() bdrv_drain while (has tracked request) aio_poll() In the scsi-disk coroutine, the qemu_co_mutex_lock() will never return because the mirror coroutine is blocked in the aio_poll(blocking=true). Reported-by: Laurent Vivier Suggested-by: Paolo Bonzini Signed-off-by: Fam Zheng --- block/io.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/block/io.c b/block/io.c index c4869b9..d0a4551 100644 --- a/block/io.c +++ b/block/io.c @@ -253,6 +253,42 @@ static void bdrv_drain_recurse(BlockDriverState *bs) } } +typedef struct { + Coroutine *co; + BlockDriverState *bs; + bool done; +} BdrvCoDrainData; + +static void bdrv_co_drain_bh_cb(void *opaque) +{ + BdrvCoDrainData *data = opaque; + Coroutine *co = data->co; + + bdrv_drain(data->bs); + data->done = true; + qemu_coroutine_enter(co, NULL); +} + +static void coroutine_fn bdrv_co_drain(BlockDriverState *bs) +{ + QEMUBH *bh; + BdrvCoDrainData data; + + assert(qemu_in_coroutine()); + data = (BdrvCoDrainData) { + .co = qemu_coroutine_self(), + .bs = bs, + .done = false, + }; + bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_co_drain_bh_cb, &data), + qemu_bh_schedule(bh); + + do { + qemu_coroutine_yield(); + } while (!data.done); + qemu_bh_delete(bh); +} + /* * Wait for pending requests to complete on a single BlockDriverState subtree, * and suspend block driver's internal I/O until next request arrives. @@ -269,6 +305,10 @@ void bdrv_drain(BlockDriverState *bs) bool busy = true; bdrv_drain_recurse(bs); + if (qemu_in_coroutine()) { + bdrv_co_drain(bs); + return; + } while (busy) { /* Keep iterating */ bdrv_flush_io_queue(bs);