From nobody Wed May 1 09:53:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520871470716343.59939609083347; Mon, 12 Mar 2018 09:17:50 -0700 (PDT) Received: from localhost ([::1]:32993 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evQ8x-0001mw-Q5 for importer@patchew.org; Mon, 12 Mar 2018 12:17:47 -0400 Received: from eggs.gnu.org ([208.118.235.92]:51467) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evPtS-0005sk-E9 for qemu-devel@nongnu.org; Mon, 12 Mar 2018 12:01:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evPtM-00027o-E4 for qemu-devel@nongnu.org; Mon, 12 Mar 2018 12:01:46 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:35516 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1evPtG-0001wr-Qu; Mon, 12 Mar 2018 12:01:34 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3FA4376FB9; Mon, 12 Mar 2018 16:01:34 +0000 (UTC) Received: from localhost (unknown [10.36.118.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id E055E2166BB2; Mon, 12 Mar 2018 16:01:33 +0000 (UTC) From: Stefan Hajnoczi To: Date: Mon, 12 Mar 2018 16:01:25 +0000 Message-Id: <20180312160125.7596-2-stefanha@redhat.com> In-Reply-To: <20180312160125.7596-1-stefanha@redhat.com> References: <20180312160125.7596-1-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Mon, 12 Mar 2018 16:01:34 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Mon, 12 Mar 2018 16:01:34 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'stefanha@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 1/1] block: make BDRV_POLL_WHILE() re-entrancy safe X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Peter Maydell , Fam Zheng , qemu-block@nongnu.org, Peter Crosthwaite , "Michael S. Tsirkin" , Max Reitz , Stefan Hajnoczi , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Nested BDRV_POLL_WHILE() calls can occur. Currently assert(!wait_->wakeup) fails in AIO_WAIT_WHILE() when this happens. This patch converts the bool wait_->need_kick flag to an unsigned wait_->num_waiters counter. Nesting works correctly because outer AIO_WAIT_WHILE() callers evaluate the condition again after the inner caller completes (invoking the inner caller counts as aio_poll() progress). Reported-by: "fuweiwei (C)" Reviewed-by: Eric Blake Signed-off-by: Stefan Hajnoczi Message-id: 20180307124619.6218-1-stefanha@redhat.com Cc: Paolo Bonzini Signed-off-by: Stefan Hajnoczi --- include/block/aio-wait.h | 61 ++++++++++++++++++++++++--------------------= ---- util/aio-wait.c | 2 +- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/include/block/aio-wait.h b/include/block/aio-wait.h index f7a3972200..8c90a2e66e 100644 --- a/include/block/aio-wait.h +++ b/include/block/aio-wait.h @@ -50,8 +50,8 @@ * } */ typedef struct { - /* Is the main loop waiting for a kick? Accessed with atomic ops. */ - bool need_kick; + /* Number of waiting AIO_WAIT_WHILE() callers. Accessed with atomic op= s. */ + unsigned num_waiters; } AioWait; =20 /** @@ -71,35 +71,34 @@ typedef struct { * wait on conditions between two IOThreads since that could lead to deadl= ock, * go via the main loop instead. */ -#define AIO_WAIT_WHILE(wait, ctx, cond) ({ \ - bool waited_ =3D false; \ - bool busy_ =3D true; \ - AioWait *wait_ =3D (wait); \ - AioContext *ctx_ =3D (ctx); \ - if (in_aio_context_home_thread(ctx_)) { \ - while ((cond) || busy_) { \ - busy_ =3D aio_poll(ctx_, (cond)); \ - waited_ |=3D !!(cond) | busy_; \ - } \ - } else { \ - assert(qemu_get_current_aio_context() =3D=3D \ - qemu_get_aio_context()); \ - assert(!wait_->need_kick); \ - /* Set wait_->need_kick before evaluating cond. */ \ - atomic_mb_set(&wait_->need_kick, true); \ - while (busy_) { \ - if ((cond)) { \ - waited_ =3D busy_ =3D true; \ - aio_context_release(ctx_); \ - aio_poll(qemu_get_aio_context(), true); \ - aio_context_acquire(ctx_); \ - } else { \ - busy_ =3D aio_poll(ctx_, false); \ - waited_ |=3D busy_; \ - } \ - } \ - atomic_set(&wait_->need_kick, false); \ - } \ +#define AIO_WAIT_WHILE(wait, ctx, cond) ({ \ + bool waited_ =3D false; \ + bool busy_ =3D true; \ + AioWait *wait_ =3D (wait); \ + AioContext *ctx_ =3D (ctx); \ + if (in_aio_context_home_thread(ctx_)) { \ + while ((cond) || busy_) { \ + busy_ =3D aio_poll(ctx_, (cond)); \ + waited_ |=3D !!(cond) | busy_; \ + } \ + } else { \ + assert(qemu_get_current_aio_context() =3D=3D \ + qemu_get_aio_context()); \ + /* Increment wait_->num_waiters before evaluating cond. */ \ + atomic_inc(&wait_->num_waiters); \ + while (busy_) { \ + if ((cond)) { \ + waited_ =3D busy_ =3D true; \ + aio_context_release(ctx_); \ + aio_poll(qemu_get_aio_context(), true); \ + aio_context_acquire(ctx_); \ + } else { \ + busy_ =3D aio_poll(ctx_, false); \ + waited_ |=3D busy_; \ + } \ + } \ + atomic_dec(&wait_->num_waiters); \ + } \ waited_; }) =20 /** diff --git a/util/aio-wait.c b/util/aio-wait.c index 975afddf4c..b8a8f86dba 100644 --- a/util/aio-wait.c +++ b/util/aio-wait.c @@ -34,7 +34,7 @@ static void dummy_bh_cb(void *opaque) void aio_wait_kick(AioWait *wait) { /* The barrier (or an atomic op) is in the caller. */ - if (atomic_read(&wait->need_kick)) { + if (atomic_read(&wait->num_waiters)) { aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb, NULL); } } --=20 2.14.3