From nobody Wed Nov 5 10:34:25 2025 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 1534525691718967.5242860277691; Fri, 17 Aug 2018 10:08:11 -0700 (PDT) Received: from localhost ([::1]:35497 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqiEM-0008F6-Ix for importer@patchew.org; Fri, 17 Aug 2018 13:08:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58639) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqi9M-0002iN-JD for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqi9L-0002Cr-Cm for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:00 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47020 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 1fqi9H-00028Z-1r; Fri, 17 Aug 2018 13:02:55 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A0D5B40201CC; Fri, 17 Aug 2018 17:02:54 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id A8F021049495; Fri, 17 Aug 2018 17:02:53 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 17 Aug 2018 19:02:42 +0200 Message-Id: <20180817170246.14641-2-kwolf@redhat.com> In-Reply-To: <20180817170246.14641-1-kwolf@redhat.com> References: <20180817170246.14641-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 17 Aug 2018 17:02:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 17 Aug 2018 17:02:54 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [RFC PATCH 1/5] blockjob: Wake up BDS when job becomes idle 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: kwolf@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the context of draining a BDS, the .drained_poll callback of block jobs is called. If this returns true (i.e. there is still some activity pending), the drain operation may call aio_poll() with blocking=3Dtrue to wait for completion. As soon as the pending activity is completed and the job finally arrives in a quiescent state (i.e. its coroutine either yields with busy=3Dfalse or terminates), the block job must notify the aio_poll() loop to wake up, otherwise we get a deadlock if both are running in different threads. Signed-off-by: Kevin Wolf --- include/block/blockjob.h | 13 +++++++++++++ include/qemu/job.h | 3 +++ blockjob.c | 18 ++++++++++++++++++ job.c | 7 +++++++ 4 files changed, 41 insertions(+) diff --git a/include/block/blockjob.h b/include/block/blockjob.h index 32c00b7dc0..2290bbb824 100644 --- a/include/block/blockjob.h +++ b/include/block/blockjob.h @@ -70,6 +70,9 @@ typedef struct BlockJob { /** Called when the job transitions to READY */ Notifier ready_notifier; =20 + /** Called when the job coroutine yields or terminates */ + Notifier idle_notifier; + /** BlockDriverStates that are involved in this block job */ GSList *nodes; } BlockJob; @@ -119,6 +122,16 @@ int block_job_add_bdrv(BlockJob *job, const char *name= , BlockDriverState *bs, void block_job_remove_all_bdrv(BlockJob *job); =20 /** + * block_job_wakeup_all_bdrv: + * @job: The block job + * + * Calls bdrv_wakeup() for all BlockDriverStates that have been added to t= he + * job. This function is to be called whenever child_job_drained_poll() wo= uld + * go from true to false to notify waiting drain requests. + */ +void block_job_wakeup_all_bdrv(BlockJob *job); + +/** * block_job_set_speed: * @job: The job to set the speed for. * @speed: The new value diff --git a/include/qemu/job.h b/include/qemu/job.h index 18c9223e31..0dae5b8481 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -148,6 +148,9 @@ typedef struct Job { /** Notifiers called when the job transitions to READY */ NotifierList on_ready; =20 + /** Notifiers called when the job coroutine yields or terminates */ + NotifierList on_idle; + /** Element of the list of jobs */ QLIST_ENTRY(Job) job_list; =20 diff --git a/blockjob.c b/blockjob.c index be5903aa96..8d27e8e1ea 100644 --- a/blockjob.c +++ b/blockjob.c @@ -221,6 +221,22 @@ int block_job_add_bdrv(BlockJob *job, const char *name= , BlockDriverState *bs, return 0; } =20 +void block_job_wakeup_all_bdrv(BlockJob *job) +{ + GSList *l; + + for (l =3D job->nodes; l; l =3D l->next) { + BdrvChild *c =3D l->data; + bdrv_wakeup(c->bs); + } +} + +static void block_job_on_idle(Notifier *n, void *opaque) +{ + BlockJob *job =3D opaque; + block_job_wakeup_all_bdrv(job); +} + bool block_job_is_internal(BlockJob *job) { return (job->job.id =3D=3D NULL); @@ -419,6 +435,7 @@ void *block_job_create(const char *job_id, const BlockJ= obDriver *driver, job->finalize_completed_notifier.notify =3D block_job_event_completed; job->pending_notifier.notify =3D block_job_event_pending; job->ready_notifier.notify =3D block_job_event_ready; + job->idle_notifier.notify =3D block_job_on_idle; =20 notifier_list_add(&job->job.on_finalize_cancelled, &job->finalize_cancelled_notifier); @@ -426,6 +443,7 @@ void *block_job_create(const char *job_id, const BlockJ= obDriver *driver, &job->finalize_completed_notifier); notifier_list_add(&job->job.on_pending, &job->pending_notifier); notifier_list_add(&job->job.on_ready, &job->ready_notifier); + notifier_list_add(&job->job.on_idle, &job->idle_notifier); =20 error_setg(&job->blocker, "block device is in use by block job: %s", job_type_str(&job->job)); diff --git a/job.c b/job.c index fa671b431a..a746bfe70b 100644 --- a/job.c +++ b/job.c @@ -410,6 +410,11 @@ static void job_event_ready(Job *job) notifier_list_notify(&job->on_ready, job); } =20 +static void job_event_idle(Job *job) +{ + notifier_list_notify(&job->on_idle, job); +} + void job_enter_cond(Job *job, bool(*fn)(Job *job)) { if (!job_started(job)) { @@ -455,6 +460,7 @@ static void coroutine_fn job_do_yield(Job *job, uint64_= t ns) timer_mod(&job->sleep_timer, ns); } job->busy =3D false; + job_event_idle(job); job_unlock(); qemu_coroutine_yield(); =20 @@ -547,6 +553,7 @@ static void coroutine_fn job_co_entry(void *opaque) assert(job && job->driver && job->driver->start); job_pause_point(job); job->driver->start(job); + job_event_idle(job); } =20 =20 --=20 2.13.6 From nobody Wed Nov 5 10:34:25 2025 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 1534525536256788.0630814622899; Fri, 17 Aug 2018 10:05:36 -0700 (PDT) Received: from localhost ([::1]:35480 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqiBl-0004Ps-Tm for importer@patchew.org; Fri, 17 Aug 2018 13:05:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58644) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqi9M-0002iQ-LZ for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqi9L-0002D2-HM for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:00 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33332 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 1fqi9I-00029Y-5b; Fri, 17 Aug 2018 13:02:56 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D3D4826A43; Fri, 17 Aug 2018 17:02:55 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id DD558112D197; Fri, 17 Aug 2018 17:02:54 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 17 Aug 2018 19:02:43 +0200 Message-Id: <20180817170246.14641-3-kwolf@redhat.com> In-Reply-To: <20180817170246.14641-1-kwolf@redhat.com> References: <20180817170246.14641-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Fri, 17 Aug 2018 17:02:55 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Fri, 17 Aug 2018 17:02:55 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [RFC PATCH 2/5] tests: Acquire AioContext around job_finish_sync() 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: kwolf@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" All callers in QEMU proper hold the AioContext lock when calling job_finish_sync(). The tests should do the same. Signed-off-by: Kevin Wolf --- include/qemu/job.h | 6 ++++++ tests/test-bdrv-drain.c | 6 ++++++ tests/test-blockjob.c | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/include/qemu/job.h b/include/qemu/job.h index 0dae5b8481..8ac48dbd28 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -520,6 +520,8 @@ void job_user_cancel(Job *job, bool force, Error **errp= ); * * Returns the return value from the job if the job actually completed * during the call, or -ECANCELED if it was canceled. + * + * Callers must hold the AioContext lock of job->aio_context. */ int job_cancel_sync(Job *job); =20 @@ -537,6 +539,8 @@ void job_cancel_sync_all(void); * function). * * Returns the return value from the job. + * + * Callers must hold the AioContext lock of job->aio_context. */ int job_complete_sync(Job *job, Error **errp); =20 @@ -579,6 +583,8 @@ void job_defer_to_main_loop(Job *job, JobDeferToMainLoo= pFn *fn, void *opaque); * * Returns 0 if the job is successfully completed, -ECANCELED if the job w= as * cancelled before completing, and -errno in other error cases. + * + * Callers must hold the AioContext lock of job->aio_context. */ int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error *= *errp); =20 diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c index 17bb8508ae..30294038ef 100644 --- a/tests/test-bdrv-drain.c +++ b/tests/test-bdrv-drain.c @@ -795,6 +795,7 @@ static void test_blockjob_common(enum drain_type drain_= type) BlockBackend *blk_src, *blk_target; BlockDriverState *src, *target; BlockJob *job; + AioContext *ctx; int ret; =20 src =3D bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR, @@ -807,6 +808,9 @@ static void test_blockjob_common(enum drain_type drain_= type) blk_target =3D blk_new(BLK_PERM_ALL, BLK_PERM_ALL); blk_insert_bs(blk_target, target, &error_abort); =20 + ctx =3D qemu_get_aio_context(); + aio_context_acquire(ctx); + job =3D block_job_create("job0", &test_job_driver, NULL, src, 0, BLK_P= ERM_ALL, 0, 0, NULL, NULL, &error_abort); block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abor= t); @@ -853,6 +857,8 @@ static void test_blockjob_common(enum drain_type drain_= type) ret =3D job_complete_sync(&job->job, &error_abort); g_assert_cmpint(ret, =3D=3D, 0); =20 + aio_context_release(ctx); + blk_unref(blk_src); blk_unref(blk_target); bdrv_unref(src); diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c index cb42f06e61..8c2babbe35 100644 --- a/tests/test-blockjob.c +++ b/tests/test-blockjob.c @@ -230,6 +230,10 @@ static void cancel_common(CancelJob *s) BlockJob *job =3D &s->common; BlockBackend *blk =3D s->blk; JobStatus sts =3D job->job.status; + AioContext *ctx; + + ctx =3D job->job.aio_context; + aio_context_acquire(ctx); =20 job_cancel_sync(&job->job); if (sts !=3D JOB_STATUS_CREATED && sts !=3D JOB_STATUS_CONCLUDED) { @@ -239,6 +243,8 @@ static void cancel_common(CancelJob *s) assert(job->job.status =3D=3D JOB_STATUS_NULL); job_unref(&job->job); destroy_blk(blk); + + aio_context_release(ctx); } =20 static void test_cancel_created(void) --=20 2.13.6 From nobody Wed Nov 5 10:34:25 2025 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 1534525549802312.67549857512904; Fri, 17 Aug 2018 10:05:49 -0700 (PDT) Received: from localhost ([::1]:35484 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqiC4-0004fg-JF for importer@patchew.org; Fri, 17 Aug 2018 13:05:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58630) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqi9M-0002i1-Cw for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqi9L-0002CV-3a for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:00 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47022 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 1fqi9J-0002AO-Bv; Fri, 17 Aug 2018 13:02:57 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 13085401DEC6; Fri, 17 Aug 2018 17:02:57 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C58510FD285; Fri, 17 Aug 2018 17:02:55 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 17 Aug 2018 19:02:44 +0200 Message-Id: <20180817170246.14641-4-kwolf@redhat.com> In-Reply-To: <20180817170246.14641-1-kwolf@redhat.com> References: <20180817170246.14641-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 17 Aug 2018 17:02:57 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 17 Aug 2018 17:02:57 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [RFC PATCH 3/5] job: Drop AioContext lock around aio_poll() 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: kwolf@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Simimlar to AIO_WAIT_WHILE(), job_finish_sync() needs to release the AioContext lock of the job before calling aio_poll(). Otherwise, callbacks called by aio_poll() would possibly take the lock a second time and run into a deadlock with a nested AIO_WAIT_WHILE() call. Signed-off-by: Kevin Wolf --- job.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/job.c b/job.c index a746bfe70b..6acf55bceb 100644 --- a/job.c +++ b/job.c @@ -1016,7 +1016,10 @@ int job_finish_sync(Job *job, void (*finish)(Job *, = Error **errp), Error **errp) job_drain(job); } while (!job_is_completed(job)) { + AioContext *aio_context =3D job->aio_context; + aio_context_release(aio_context); aio_poll(qemu_get_aio_context(), true); + aio_context_acquire(aio_context); } ret =3D (job_is_cancelled(job) && job->ret =3D=3D 0) ? -ECANCELED : jo= b->ret; job_unref(job); --=20 2.13.6 From nobody Wed Nov 5 10:34:25 2025 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534525861806683.6511242906988; Fri, 17 Aug 2018 10:11:01 -0700 (PDT) Received: from localhost ([::1]:35513 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqiGy-0002aT-CR for importer@patchew.org; Fri, 17 Aug 2018 13:10:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58670) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqi9N-0002j2-Cq for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqi9M-0002EO-Hd for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:01 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47024 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 1fqi9K-0002BX-JR; Fri, 17 Aug 2018 13:02:58 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 460B9401DEC6; Fri, 17 Aug 2018 17:02:58 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4F0F5112D197; Fri, 17 Aug 2018 17:02:57 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 17 Aug 2018 19:02:45 +0200 Message-Id: <20180817170246.14641-5-kwolf@redhat.com> In-Reply-To: <20180817170246.14641-1-kwolf@redhat.com> References: <20180817170246.14641-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 17 Aug 2018 17:02:58 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 17 Aug 2018 17:02:58 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [RFC PATCH 4/5] block: Drop AioContext lock in bdrv_drain_poll_top_level() 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: kwolf@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Simimlar to AIO_WAIT_WHILE(), bdrv_drain_poll_top_level() needs to release the AioContext lock of the node to be drained before calling aio_poll(). Otherwise, callbacks called by aio_poll() would possibly take the lock a second time and run into a deadlock with a nested AIO_WAIT_WHILE() call. Signed-off-by: Kevin Wolf --- block/io.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/block/io.c b/block/io.c index 7100344c7b..832d2536bf 100644 --- a/block/io.c +++ b/block/io.c @@ -268,9 +268,32 @@ bool bdrv_drain_poll(BlockDriverState *bs, bool recurs= ive, static bool bdrv_drain_poll_top_level(BlockDriverState *bs, bool recursive, BdrvChild *ignore_parent) { + AioContext *ctx =3D bdrv_get_aio_context(bs); + + /* + * We cannot easily release the lock unconditionally here because many + * callers of drain function (like qemu initialisation, tools, etc.) d= on't + * even hold the main context lock. + * + * This means that we fix potential deadlocks for the case where we ar= e in + * the main context and polling a BDS in a different AioContext, but + * draining a BDS in the main context from a different I/O thread would + * still have this problem. Fortunately, this isn't supposed to happen + * anyway. + */ + if (ctx !=3D qemu_get_aio_context()) { + aio_context_release(ctx); + } else { + assert(qemu_get_current_aio_context() =3D=3D qemu_get_aio_context(= )); + } + /* Execute pending BHs first and check everything else only after the = BHs * have executed. */ - while (aio_poll(bs->aio_context, false)); + while (aio_poll(ctx, false)); + + if (ctx !=3D qemu_get_aio_context()) { + aio_context_acquire(ctx); + } =20 return bdrv_drain_poll(bs, recursive, ignore_parent, false); } --=20 2.13.6 From nobody Wed Nov 5 10:34:25 2025 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 153452553617538.55878211459549; Fri, 17 Aug 2018 10:05:36 -0700 (PDT) Received: from localhost ([::1]:35481 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqiBn-0004R2-BD for importer@patchew.org; Fri, 17 Aug 2018 13:05:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58683) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqi9O-0002kC-Ef for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqi9N-0002FX-OQ for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:03:02 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:60352 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 1fqi9L-0002DC-RB; Fri, 17 Aug 2018 13:02:59 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7B139F2B5A; Fri, 17 Aug 2018 17:02:59 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82E5A1049495; Fri, 17 Aug 2018 17:02:58 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 17 Aug 2018 19:02:46 +0200 Message-Id: <20180817170246.14641-6-kwolf@redhat.com> In-Reply-To: <20180817170246.14641-1-kwolf@redhat.com> References: <20180817170246.14641-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 17 Aug 2018 17:02:59 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 17 Aug 2018 17:02:59 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [RFC PATCH 5/5] [WIP] Lock AioContext in bdrv_co_drain_bh_cb() 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: kwolf@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Not sure if this is correct, but at least it makes qemu-iotests 127 pass again. Signed-off-by: Kevin Wolf --- block/io.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/block/io.c b/block/io.c index 832d2536bf..d3dde4d7fd 100644 --- a/block/io.c +++ b/block/io.c @@ -309,6 +309,10 @@ static void bdrv_co_drain_bh_cb(void *opaque) BdrvCoDrainData *data =3D opaque; Coroutine *co =3D data->co; BlockDriverState *bs =3D data->bs; + AioContext *ctx; + + ctx =3D bdrv_get_aio_context(bs); + aio_context_acquire(ctx); =20 if (bs) { bdrv_dec_in_flight(bs); @@ -324,6 +328,8 @@ static void bdrv_co_drain_bh_cb(void *opaque) bdrv_drain_all_begin(); } =20 + aio_context_release(ctx); + data->done =3D true; aio_co_wake(co); } --=20 2.13.6