All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, mreitz@redhat.com, famz@redhat.com,
	pbonzini@redhat.com, slp@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 01/14] blockjob: Wake up BDS when job becomes idle
Date: Fri,  7 Sep 2018 18:15:07 +0200	[thread overview]
Message-ID: <20180907161520.26349-2-kwolf@redhat.com> (raw)
In-Reply-To: <20180907161520.26349-1-kwolf@redhat.com>

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=true 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=false
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 <kwolf@redhat.com>
---
 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;
 
+    /** 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);
 
 /**
+ * block_job_wakeup_all_bdrv:
+ * @job: The block job
+ *
+ * Calls bdrv_wakeup() for all BlockDriverStates that have been added to the
+ * job. This function is to be called whenever child_job_drained_poll() would
+ * 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;
 
+    /** Notifiers called when the job coroutine yields or terminates */
+    NotifierList on_idle;
+
     /** Element of the list of jobs */
     QLIST_ENTRY(Job) job_list;
 
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;
 }
 
+void block_job_wakeup_all_bdrv(BlockJob *job)
+{
+    GSList *l;
+
+    for (l = job->nodes; l; l = l->next) {
+        BdrvChild *c = l->data;
+        bdrv_wakeup(c->bs);
+    }
+}
+
+static void block_job_on_idle(Notifier *n, void *opaque)
+{
+    BlockJob *job = opaque;
+    block_job_wakeup_all_bdrv(job);
+}
+
 bool block_job_is_internal(BlockJob *job)
 {
     return (job->job.id == NULL);
@@ -419,6 +435,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
     job->finalize_completed_notifier.notify = block_job_event_completed;
     job->pending_notifier.notify = block_job_event_pending;
     job->ready_notifier.notify = block_job_event_ready;
+    job->idle_notifier.notify = block_job_on_idle;
 
     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 BlockJobDriver *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);
 
     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 e36ebaafd8..9ad0b7476a 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);
 }
 
+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 = false;
+    job_event_idle(job);
     job_unlock();
     qemu_coroutine_yield();
 
@@ -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);
 }
 
 
-- 
2.13.6

  reply	other threads:[~2018-09-07 16:16 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-07 16:15 [Qemu-devel] [PATCH 00/14] Fix some jobs/drain/aio_poll related hangs Kevin Wolf
2018-09-07 16:15 ` Kevin Wolf [this message]
2018-09-11  7:58   ` [Qemu-devel] [PATCH 01/14] blockjob: Wake up BDS when job becomes idle Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 02/14] test-bdrv-drain: Drain with block jobs in an I/O thread Kevin Wolf
2018-09-11  8:09   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 03/14] test-blockjob: Acquire AioContext around job_finish_sync() Kevin Wolf
2018-09-11  8:11   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 04/14] job: Use AIO_WAIT_WHILE() in job_finish_sync() Kevin Wolf
2018-09-11  8:17   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 05/14] test-bdrv-drain: Test AIO_WAIT_WHILE() in completion callback Kevin Wolf
2018-09-11  8:17   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 06/14] block: Add missing locking in bdrv_co_drain_bh_cb() Kevin Wolf
2018-09-11  8:23   ` Fam Zheng
2018-09-11  9:17     ` Kevin Wolf
2018-09-11  9:28       ` Sergio Lopez
2018-09-11 10:22         ` Kevin Wolf
2018-09-07 16:15 ` [Qemu-devel] [PATCH 07/14] aio-wait: Increase num_waiters even in home thread Kevin Wolf
2018-09-11  8:25   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 08/14] block-backend: Add .drained_poll callback Kevin Wolf
2018-09-11  8:26   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 09/14] block-backend: Fix potential double blk_delete() Kevin Wolf
2018-09-11  8:27   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 10/14] block-backend: Decrease in_flight only after callback Kevin Wolf
2018-09-11  8:29   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 11/14] mirror: Fix potential use-after-free in active commit Kevin Wolf
2018-09-11  8:31   ` Fam Zheng
2018-09-11  9:32     ` Kevin Wolf
2018-09-07 16:15 ` [Qemu-devel] [PATCH 12/14] blockjob: Lie better in child_job_drained_poll() Kevin Wolf
2018-09-11  8:34   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 13/14] block: Remove aio_poll() in bdrv_drain_poll variants Kevin Wolf
2018-09-11  8:35   ` Fam Zheng
2018-09-07 16:15 ` [Qemu-devel] [PATCH 14/14] test-bdrv-drain: Test nested poll in bdrv_drain_poll_top_level() Kevin Wolf
2018-09-11  8:37   ` Fam Zheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180907161520.26349-2-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=famz@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=slp@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.