From patchwork Mon Jul 22 13:30:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 11052375 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1219B746 for ; Mon, 22 Jul 2019 13:31:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 00E162849B for ; Mon, 22 Jul 2019 13:31:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E97A8284C9; Mon, 22 Jul 2019 13:31:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A01DB28640 for ; Mon, 22 Jul 2019 13:31:34 +0000 (UTC) Received: from localhost ([::1]:33582 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpYPd-0004CY-Az for patchwork-qemu-devel@patchwork.kernel.org; Mon, 22 Jul 2019 09:31:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51914) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpYPK-00037m-Sf for qemu-devel@nongnu.org; Mon, 22 Jul 2019 09:31:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hpYPJ-00026o-Sb for qemu-devel@nongnu.org; Mon, 22 Jul 2019 09:31:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43692) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hpYPH-00021t-J3; Mon, 22 Jul 2019 09:31:11 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DDC9A307D88C; Mon, 22 Jul 2019 13:31:10 +0000 (UTC) Received: from localhost (unknown [10.40.205.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6F8F5646CD; Mon, 22 Jul 2019 13:31:08 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 22 Jul 2019 15:30:53 +0200 Message-Id: <20190722133054.21781-2-mreitz@redhat.com> In-Reply-To: <20190722133054.21781-1-mreitz@redhat.com> References: <20190722133054.21781-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Mon, 22 Jul 2019 13:31:10 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-4.1 1/2] block: Dec. drained_end_counter before bdrv_wakeup X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Decrementing drained_end_counter after bdrv_dec_in_flight() (which in turn invokes bdrv_wakeup() and thus aio_wait_kick()) is not very clever. We should decrement it beforehand, so that any waiting aio_poll() that is woken by bdrv_dec_in_flight() sees the decremented drained_end_counter. Because the time window between decrementing drained_end_counter and aio_wait_kick() is very small, I cannot supply a reliable regression test. However, running e.g. the /bdrv-drain/blockjob/iothread/drain_all test in test-bdrv-drain has a small chance of hanging without this patch (about 1/200 or so; it gets to nearly 100 % if you add e.g. an fputc(' ', stderr); after the bdrv_dec_flight()). Fixes: e037c09c78520cbdb6da7cfc6ad0256d5870b814 Signed-off-by: Max Reitz --- block/io.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/block/io.c b/block/io.c index b89e155d21..06305c6ea6 100644 --- a/block/io.c +++ b/block/io.c @@ -217,13 +217,12 @@ static void coroutine_fn bdrv_drain_invoke_entry(void *opaque) bs->drv->bdrv_co_drain_end(bs); } - /* Set data->done before reading bs->wakeup. */ + /* Set data->done and decrement drained_end_counter before bdrv_wakeup() */ atomic_mb_set(&data->done, true); - bdrv_dec_in_flight(bs); - if (!data->begin) { atomic_dec(data->drained_end_counter); } + bdrv_dec_in_flight(bs); g_free(data); } From patchwork Mon Jul 22 13:30:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 11052377 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B9FFF746 for ; Mon, 22 Jul 2019 13:32:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AA4E328676 for ; Mon, 22 Jul 2019 13:32:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9EF0728640; Mon, 22 Jul 2019 13:32:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A97C02849B for ; Mon, 22 Jul 2019 13:32:00 +0000 (UTC) Received: from localhost ([::1]:33600 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpYQ4-0005oa-2e for patchwork-qemu-devel@patchwork.kernel.org; Mon, 22 Jul 2019 09:32:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52082) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpYPY-0003ui-AK for qemu-devel@nongnu.org; Mon, 22 Jul 2019 09:31:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hpYPX-0002M7-4g for qemu-devel@nongnu.org; Mon, 22 Jul 2019 09:31:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43490) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hpYPQ-0002EU-NW; Mon, 22 Jul 2019 09:31:20 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EFD4581DE3; Mon, 22 Jul 2019 13:31:19 +0000 (UTC) Received: from localhost (unknown [10.40.205.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C989960BF1; Mon, 22 Jul 2019 13:31:16 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 22 Jul 2019 15:30:54 +0200 Message-Id: <20190722133054.21781-3-mreitz@redhat.com> In-Reply-To: <20190722133054.21781-1-mreitz@redhat.com> References: <20190722133054.21781-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Mon, 22 Jul 2019 13:31:20 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-4.1 2/2] block: Only the main loop can change AioContexts X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP bdrv_set_aio_context_ignore() can only work in the main loop: bdrv_drained_begin() only works in the main loop and the node's (old) AioContext; and bdrv_drained_end() really only works in the main loop and the node's (new) AioContext (contrary to its current comment, which is just wrong). Consequentially, bdrv_set_aio_context_ignore() must be called from the main loop. Luckily, assuming that we can make block graph changes only from the main loop as well, all its callers do that already. Note that changing a node's context in a sense is an operation that changes the block graph, so it actually makes sense to require this function to be called from the main loop. Also, fix bdrv_drained_end()'s description. You can only use it from the main loop or the node's AioContext, and in the latter case, the whole subtree must be in the same context. Fixes: e037c09c78520cbdb6da7cfc6ad0256d5870b814 Signed-off-by: Max Reitz --- include/block/block.h | 8 +++----- block.c | 13 ++++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index 60f00479e0..50a07c1c33 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -667,11 +667,9 @@ void bdrv_subtree_drained_begin(BlockDriverState *bs); * * This polls @bs's AioContext until all scheduled sub-drained_ends * have settled. On one hand, that may result in graph changes. On - * the other, this requires that all involved nodes (@bs and all of - * its parents) are in the same AioContext, and that the caller has - * acquired it. - * If there are any nodes that are in different contexts from @bs, - * these contexts must not be acquired. + * the other, this requires that the caller either runs in the main + * loop; or that all involved nodes (@bs and all of its parents) are + * in the caller's AioContext. */ void bdrv_drained_end(BlockDriverState *bs); diff --git a/block.c b/block.c index 9c94f7f28a..cbd8da5f3b 100644 --- a/block.c +++ b/block.c @@ -5914,6 +5914,8 @@ static void bdrv_attach_aio_context(BlockDriverState *bs, * Changes the AioContext used for fd handlers, timers, and BHs by this * BlockDriverState and all its children and parents. * + * Must be called from the main AioContext. + * * The caller must own the AioContext lock for the old AioContext of bs, but it * must not own the AioContext lock for new_context (unless new_context is the * same as the current context of bs). @@ -5925,9 +5927,10 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs, AioContext *new_context, GSList **ignore) { AioContext *old_context = bdrv_get_aio_context(bs); - AioContext *current_context = qemu_get_current_aio_context(); BdrvChild *child; + g_assert(qemu_get_current_aio_context() == qemu_get_aio_context()); + if (old_context == new_context) { return; } @@ -5953,7 +5956,7 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs, bdrv_detach_aio_context(bs); /* Acquire the new context, if necessary */ - if (current_context != new_context) { + if (qemu_get_aio_context() != new_context) { aio_context_acquire(new_context); } @@ -5965,16 +5968,16 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs, * subtree that have not yet been moved to the new AioContext. * Release the old one so bdrv_drained_end() can poll them. */ - if (current_context != old_context) { + if (qemu_get_aio_context() != old_context) { aio_context_release(old_context); } bdrv_drained_end(bs); - if (current_context != old_context) { + if (qemu_get_aio_context() != old_context) { aio_context_acquire(old_context); } - if (current_context != new_context) { + if (qemu_get_aio_context() != new_context) { aio_context_release(new_context); } }