From nobody Tue Apr 15 14:48:18 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 1537890148544101.2885994915705; Tue, 25 Sep 2018 08:42:28 -0700 (PDT) Received: from localhost ([::1]:53802 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g4pTn-0007EI-7S for importer@patchew.org; Tue, 25 Sep 2018 11:42:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45091) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g4p50-0001Md-W1 for qemu-devel@nongnu.org; Tue, 25 Sep 2018 11:16:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g4p4z-0005eT-I5 for qemu-devel@nongnu.org; Tue, 25 Sep 2018 11:16:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52354) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g4p4w-0005Ze-4N; Tue, 25 Sep 2018 11:16:46 -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 6764B3078AC7; Tue, 25 Sep 2018 15:16:45 +0000 (UTC) Received: from localhost (unknown [10.40.205.127]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E0FB060BF6; Tue, 25 Sep 2018 15:16:44 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Tue, 25 Sep 2018 17:15:24 +0200 Message-Id: <20180925151541.18932-26-mreitz@redhat.com> In-Reply-To: <20180925151541.18932-1-mreitz@redhat.com> References: <20180925151541.18932-1-mreitz@redhat.com> 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.48]); Tue, 25 Sep 2018 15:16:45 +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] [PULL 25/42] 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: Kevin Wolf , Peter Maydell , qemu-devel@nongnu.org, Max Reitz 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" From: Kevin Wolf 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 Reviewed-by: Fam Zheng Reviewed-by: Max Reitz --- 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; @@ -118,6 +121,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. diff --git a/include/qemu/job.h b/include/qemu/job.h index 5cb0681834..b4a784d3cc 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -156,6 +156,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 bf7ef48f98..58dbd87a51 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); @@ -416,6 +432,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); @@ -423,6 +440,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 d17b1c82da..4812962fb5 100644 --- a/job.c +++ b/job.c @@ -402,6 +402,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)) { @@ -447,6 +452,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 @@ -865,6 +871,7 @@ static void coroutine_fn job_co_entry(void *opaque) assert(job && job->driver && job->driver->run); job_pause_point(job); job->ret =3D job->driver->run(job, &job->err); + job_event_idle(job); job->deferred_to_main_loop =3D true; aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job); } --=20 2.17.1