Skip to contentSkip to conversation

job: replace AioContext lock with job_mutex

PATCH Superseded Emanuele Giuseppe Esposito 3 participants 38 messages Jan 5, 2022 – Jan 26, 2022 Block layer

v3 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v3 cover job: replace AioContext lock with job_mutex

In this series, we want to remove the AioContext lock and instead use the already existent job_mutex to protect the job structures and list. This is part of the work to get rid of AioContext lock usage in favour of smaller granularity locks.

In order to simplify reviewer's job, job lock/unlock functions and macros are added as empty prototypes (nop) in patch 1. They are converted to use the actual job mutex only in the last patch, 14. In this way we can freely create locking sections without worrying about deadlocks with the aiocontext lock.

Patch 2 defines what fields in the job structure need protection, and patches 3-4 categorize respectively locked and unlocked functions in the job API.

Patch 5-9 are in preparation to the job locks, they try to reduce the aiocontext critical sections and other minor fixes.

Patch 10-13 introduces the (nop) job lock into the job API and its users, following the comments and categorizations done in patch 2-3-4.

Patch 14 makes the prototypes in patch 1 use the job_mutex and removes all aiocontext lock at the same time.

Tested this series by running unit tests, qemu-iotests and qtests (x86_64).

This serie is based on my previous series "block layer: split block APIs in global state and I/O".

Based-on: <[email protected]>
---
v3:
* add "_locked" suffix to the functions called under job_mutex lock
* rename _job_lock in real_job_lock
* job_mutex is now public, and drivers like monitor use it directly
* introduce and protect job_get_aio_context
* remove mirror-specific APIs and just use WITH_JOB_GUARD
* more extensive use of WITH_JOB_GUARD and JOB_LOCK_GUARD

RFC v2:
* use JOB_LOCK_GUARD and WITH_JOB_LOCK_GUARD
* mu(u)ltiple typos in commit messages
* job API split patches are sent separately in another series
* use of empty job_{lock/unlock} and JOB_LOCK_GUARD/WITH_JOB_LOCK_GUARD to avoid deadlocks and simplify the reviewer job
* move patch 11 (block_job_query: remove atomic read) as last

Emanuele Giuseppe Esposito (16):
job.c: make job_mutex and job_lock/unlock() public job.h: categorize fields in struct Job
job.h: define locked functions
job.h: define unlocked functions
block/mirror.c: use of job helpers in drivers to avoid TOC/TOU job.c: make job_event_* functions static
job.c: move inner aiocontext lock in callbacks aio-wait.h: introduce AIO_WAIT_WHILE_UNLOCKED
jobs: remove aiocontext locks since the functions are under BQL
jobs: protect jobs with job_lock/unlock
jobs: document all static functions and add _locked() suffix
jobs: use job locks and helpers also in the unit tests
jobs: add job lock in find_* functions
job.c: use job_get_aio_context()
job.c: enable job lock/unlock and remove Aiocontext locks
block_job_query: remove atomic read

 block.c                          |  18 +-
 block/commit.c                   |   4 +-
 block/mirror.c                   |  21 +-
 block/replication.c              |  10 +-
 blockdev.c                       | 112 ++----
 blockjob.c                       | 122 +++---
 include/block/aio-wait.h         |  15 +-
 include/qemu/job.h               | 317 +++++++++++----
 job-qmp.c                        |  74 ++--
 job.c                            | 656 +++++++++++++++++++------------
 monitor/qmp-cmds.c               |   6 +-
 qemu-img.c                       |  41 +-
 tests/unit/test-bdrv-drain.c     |  46 +--
 tests/unit/test-block-iothread.c |  14 +-
 tests/unit/test-blockjob-txn.c   |  24 +-
 tests/unit/test-blockjob.c       |  98 ++---
 16 files changed, 947 insertions(+), 631 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/16 job.c: make job_mutex and job_lock/unlock() public

job mutex will be used to protect the job struct elements and list, replacing AioContext locks.

Right now use a shared lock for all jobs, in order to keep things simple. Once the AioContext lock is gone, we can introduce per-job locks.

To simplify the switch from aiocontext to job lock, introduce *nop* lock/unlock functions and macros. Once everything is protected by jobs, we can add the mutex and remove the aiocontext.

Since job_mutex is already being used, add static
real_job_{lock/unlock}.

---
 include/qemu/job.h | 24 ++++++++++++++++++++++++
 job.c              | 35 +++++++++++++++++++++++------------
 2 files changed, 47 insertions(+), 12 deletions(-)
include/qemu/job.h +24 -0
index 915ceff425..8d0d370dda 100644
@@ -312,6 +312,30 @@ typedef enum JobCreateFlags {
     JOB_MANUAL_DISMISS = 0x04,
 } JobCreateFlags;
 
+extern QemuMutex job_mutex;
+
+#define JOB_LOCK_GUARD() /* QEMU_LOCK_GUARD(&job_mutex) */
+
+#define WITH_JOB_LOCK_GUARD() /* WITH_QEMU_LOCK_GUARD(&job_mutex) */
+
+/**
+ * job_lock:
+ *
+ * Take the mutex protecting the list of jobs and their status.
+ * Most functions called by the monitor need to call job_lock
+ * and job_unlock manually.  On the other hand, function called
+ * by the block jobs themselves and by the block layer will take the
+ * lock for you.
+ */
+void job_lock(void);
+
+/**
+ * job_unlock:
+ *
+ * Release the mutex protecting the list of jobs and their status.
+ */
+void job_unlock(void);
+
 /**
  * Allocate and return a new job transaction. Jobs can be added to the
  * transaction using job_txn_add_job().
job.c +23 -12
index e048037099..ccf737a179 100644
@@ -32,6 +32,12 @@
 #include "trace/trace-root.h"
 #include "qapi/qapi-events-job.h"
 
+/*
+ * job_mutex protects the jobs list, but also makes the
+ * struct job fields thread-safe.
+ */
+QemuMutex job_mutex;
+
 static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
 
 /* Job State Transition Table */
@@ -74,17 +80,22 @@ struct JobTxn {
     int refcnt;
 };
 
-/* Right now, this mutex is only needed to synchronize accesses to job->busy
- * and job->sleep_timer, such as concurrent calls to job_do_yield and
- * job_enter. */
-static QemuMutex job_mutex;
+void job_lock(void)
+{
+    /* nop */
+}
+
+void job_unlock(void)
+{
+    /* nop */
+}
 
-static void job_lock(void)
+static void real_job_lock(void)
 {
     qemu_mutex_lock(&job_mutex);
 }
 
-static void job_unlock(void)
+static void real_job_unlock(void)
 {
     qemu_mutex_unlock(&job_mutex);
 }
@@ -449,21 +460,21 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job))
         return;
     }
 
-    job_lock();
+    real_job_lock();
     if (job->busy) {
-        job_unlock();
+        real_job_unlock();
         return;
     }
 
     if (fn && !fn(job)) {
-        job_unlock();
+        real_job_unlock();
         return;
     }
 
     assert(!job->deferred_to_main_loop);
     timer_del(&job->sleep_timer);
     job->busy = true;
-    job_unlock();
+    real_job_unlock();
     aio_co_enter(job->aio_context, job->co);
 }
 
@@ -480,13 +491,13 @@ void job_enter(Job *job)
  * called explicitly. */
 static void coroutine_fn job_do_yield(Job *job, uint64_t ns)
 {
-    job_lock();
+    real_job_lock();
     if (ns != -1) {
         timer_mod(&job->sleep_timer, ns);
     }
     job->busy = false;
     job_event_idle(job);
-    job_unlock();
+    real_job_unlock();
     qemu_coroutine_yield();
 
     /* Set by job_enter_cond() before re-entering the coroutine.  */
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v3 01/16] job.c: make job_mutex and job_lock/unlock() public

Quoted 6 lines · Emanuele Giuseppe Esposito

job mutex will be used to protect the job struct elements and list, replacing AioContext locks.

Right now use a shared lock for all jobs, in order to keep things simple. Once the AioContext lock is gone, we can introduce per-job locks.

Not even needed in my opinion, this is not a fast path. But we'll see.

Quoted 6 lines

To simplify the switch from aiocontext to job lock, introduce *nop* lock/unlock functions and macros. Once everything is protected by jobs, we can add the mutex and remove the aiocontext.

Since job_mutex is already being used, add static
real_job_{lock/unlock}.

Out of curiosity, what breaks if the real job lock is used from the start? (It probably should be mentioned in the commit message).

job.c struct JobTxn { · line 101

Quoted 11 lines
job.c +2 -2
-static void job_lock(void)
+static void real_job_lock(void)
  {
      qemu_mutex_lock(&job_mutex);
  }
 
-static void job_unlock(void)
+static void real_job_unlock(void)
  {
      qemu_mutex_unlock(&job_mutex);
  }

Would it work to

#define job_lock real_job_lock
#define job_unlock real_job_unlock

instead of having to do the changes below?

Paolo

Quoted 42 lines
job.c +6 -6
@@ -449,21 +460,21 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job))
          return;
      }
 
-    job_lock();
+    real_job_lock();
      if (job->busy) {
-        job_unlock();
+        real_job_unlock();
          return;
      }
 
      if (fn && !fn(job)) {
-        job_unlock();
+        real_job_unlock();
          return;
      }
 
      assert(!job->deferred_to_main_loop);
      timer_del(&job->sleep_timer);
      job->busy = true;
-    job_unlock();
+    real_job_unlock();
      aio_co_enter(job->aio_context, job->co);
  }
 
@@ -480,13 +491,13 @@ void job_enter(Job *job)
   * called explicitly. */
  static void coroutine_fn job_do_yield(Job *job, uint64_t ns)
  {
-    job_lock();
+    real_job_lock();
      if (ns != -1) {
          timer_mod(&job->sleep_timer, ns);
      }
      job->busy = false;
      job_event_idle(job);
-    job_unlock();
+    real_job_unlock();
      qemu_coroutine_yield();
 
      /* Set by job_enter_cond() before re-entering the coroutine.  */
#3 lore raw

Re: [PATCH v3 01/16] job.c: make job_mutex and job_lock/unlock() public

Quoted 33 lines · Paolo Bonzini
Quoted 6 lines · Emanuele Giuseppe Esposito

job mutex will be used to protect the job struct elements and list, replacing AioContext locks.

Right now use a shared lock for all jobs, in order to keep things simple. Once the AioContext lock is gone, we can introduce per-job locks.

Not even needed in my opinion, this is not a fast path.  But we'll see.

Quoted 6 lines

To simplify the switch from aiocontext to job lock, introduce *nop* lock/unlock functions and macros. Once everything is protected by jobs, we can add the mutex and remove the aiocontext.

Since job_mutex is already being used, add static
real_job_{lock/unlock}.

Out of curiosity, what breaks if the real job lock is used from the start?  (It probably should be mentioned in the commit message).

Quoted 10 lines
job.c +1 -1
-static void job_lock(void)
+static void real_job_lock(void)

  {
      qemu_mutex_lock(&job_mutex);
  }

job.c +1 -1
-static void job_unlock(void)
+static void real_job_unlock(void)

  {
      qemu_mutex_unlock(&job_mutex);
  }

Would it work to

Quoted 4 lines

#define job_lock real_job_lock
#define job_unlock real_job_unlock

instead of having to do the changes below?

Ignore all this, please.

Paolo

#4 lore raw

PATCH v3 2/16 job.h: categorize fields in struct Job

In reply to Emanuele Giuseppe Esposito

Categorize the fields in struct Job to understand which ones need to be protected by the job mutex and which don't.

---
 include/qemu/job.h | 63 +++++++++++++++++++++++++++-------------------
 1 file changed, 37 insertions(+), 26 deletions(-)
include/qemu/job.h +37 -26
index 8d0d370dda..0d348ff186 100644
@@ -40,27 +40,52 @@ typedef struct JobTxn JobTxn;
  * Long-running operation.
  */
 typedef struct Job {
+
+    /* Fields set at initialization (job_create), and never modified */
+
     /** The ID of the job. May be NULL for internal jobs. */
     char *id;
 
-    /** The type of this job. */
+    /**
+     * The type of this job.
+     * All callbacks are called with job_mutex *not* held.
+     */
     const JobDriver *driver;
 
-    /** Reference count of the block job */
-    int refcnt;
-
-    /** Current state; See @JobStatus for details. */
-    JobStatus status;
-
-    /** AioContext to run the job coroutine in */
-    AioContext *aio_context;
-
     /**
      * The coroutine that executes the job.  If not NULL, it is reentered when
      * busy is false and the job is cancelled.
+     * Initialized in job_start()
      */
     Coroutine *co;
 
+    /** True if this job should automatically finalize itself */
+    bool auto_finalize;
+
+    /** True if this job should automatically dismiss itself */
+    bool auto_dismiss;
+
+    /** The completion function that will be called when the job completes.  */
+    BlockCompletionFunc *cb;
+
+    /** The opaque value that is passed to the completion function.  */
+    void *opaque;
+
+    /* ProgressMeter API is thread-safe */
+    ProgressMeter progress;
+
+
+    /** Protected by job_mutex */
+
+    /** AioContext to run the job coroutine in */
+    AioContext *aio_context;
+
+    /** Reference count of the block job */
+    int refcnt;
+
+    /** Current state; See @JobStatus for details. */
+    JobStatus status;
+
     /**
      * Timer that is used by @job_sleep_ns. Accessed under job_mutex (in
      * job.c).
@@ -76,7 +101,7 @@ typedef struct Job {
     /**
      * Set to false by the job while the coroutine has yielded and may be
      * re-entered by job_enter(). There may still be I/O or event loop activity
-     * pending. Accessed under block_job_mutex (in blockjob.c).
+     * pending. Accessed under job_mutex.
      *
      * When the job is deferred to the main loop, busy is true as long as the
      * bottom half is still pending.
@@ -112,14 +137,6 @@ typedef struct Job {
     /** Set to true when the job has deferred work to the main loop. */
     bool deferred_to_main_loop;
 
-    /** True if this job should automatically finalize itself */
-    bool auto_finalize;
-
-    /** True if this job should automatically dismiss itself */
-    bool auto_dismiss;
-
-    ProgressMeter progress;
-
     /**
      * Return code from @run and/or @prepare callback(s).
      * Not final until the job has reached the CONCLUDED status.
@@ -134,12 +151,6 @@ typedef struct Job {
      */
     Error *err;
 
-    /** The completion function that will be called when the job completes.  */
-    BlockCompletionFunc *cb;
-
-    /** The opaque value that is passed to the completion function.  */
-    void *opaque;
-
     /** Notifiers called when a cancelled job is finalised */
     NotifierList on_finalize_cancelled;
 
@@ -167,6 +178,7 @@ typedef struct Job {
 
 /**
  * Callbacks and other information about a Job driver.
+ * All callbacks are invoked with job_mutex *not* held.
  */
 struct JobDriver {
 
@@ -481,7 +493,6 @@ void job_yield(Job *job);
  */
 void coroutine_fn job_sleep_ns(Job *job, int64_t ns);
 
-
 /** Returns the JobType of a given Job. */
 JobType job_type(const Job *job);
 
Signature · 1 line
2.31.1
#5 lore raw

Re: [PATCH v3 02/16] job.h: categorize fields in struct Job

Quoted 1 line · Emanuele Giuseppe Esposito

+ /** Protected by job_mutex */

Technically not yet true. You can add this in patch 15 and at the same time remove this one:

include/qemu/job.h typedef struct Job { · line 104

Quoted 4 lines

* Set to false by the job while the coroutine has yielded and may be
* re-entered by job_enter(). There may still be I/O or event loop activity

include/qemu/job.h +1 -1
-     * pending. Accessed under block_job_mutex (in blockjob.c).
+     * pending. Accessed under job_mutex.

Paolo

#6 lore raw

PATCH v3 3/16 job.h: define locked functions

In reply to Emanuele Giuseppe Esposito

These functions assume that the job lock is held by the caller, to avoid TOC/TOU conditions. Therefore, their name must end with _locked.

Introduce also additional helpers that define _locked functions (useful when the job_mutex is globally applied).

Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*.

---
 block.c                          |   2 +-
 block/replication.c              |   4 +-
 blockdev.c                       |  32 +++----
 blockjob.c                       |  16 ++--
 include/qemu/job.h               | 153 +++++++++++++++++++++---------
 job-qmp.c                        |  26 +++---
 job.c                            | 155 +++++++++++++++++--------------
 qemu-img.c                       |  10 +-
 tests/unit/test-bdrv-drain.c     |   2 +-
 tests/unit/test-block-iothread.c |   4 +-
 tests/unit/test-blockjob-txn.c   |  14 +--
 tests/unit/test-blockjob.c       |  30 +++---
 12 files changed, 263 insertions(+), 185 deletions(-)
block.c +1 -1
index ca70bcc807..8fcd525fa0 100644
@@ -4976,7 +4976,7 @@ static void bdrv_close(BlockDriverState *bs)
 
 void bdrv_close_all(void)
 {
-    assert(job_next(NULL) == NULL);
+    assert(job_next_locked(NULL) == NULL);
     assert(qemu_in_main_thread());
 
     /* Drop references from requests still in flight, such as canceled block
block/replication.c +2 -2
index 55c8f894aa..5215c328c1 100644
@@ -149,7 +149,7 @@ static void replication_close(BlockDriverState *bs)
     if (s->stage == BLOCK_REPLICATION_FAILOVER) {
         commit_job = &s->commit_job->job;
         assert(commit_job->aio_context == qemu_get_current_aio_context());
-        job_cancel_sync(commit_job, false);
+        job_cancel_sync_locked(commit_job, false);
     }
 
     if (s->mode == REPLICATION_MODE_SECONDARY) {
@@ -726,7 +726,7 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
          * disk, secondary disk in backup_job_completed().
          */
         if (s->backup_job) {
-            job_cancel_sync(&s->backup_job->job, true);
+            job_cancel_sync_locked(&s->backup_job->job, true);
         }
 
         if (!failover) {
blockdev.c +16 -16
index a3b9aeb3c2..11fd651bde 100644
@@ -160,7 +160,7 @@ void blockdev_mark_auto_del(BlockBackend *blk)
             AioContext *aio_context = job->job.aio_context;
             aio_context_acquire(aio_context);
 
-            job_cancel(&job->job, false);
+            job_cancel_locked(&job->job, false);
 
             aio_context_release(aio_context);
         }
@@ -1832,7 +1832,7 @@ static void drive_backup_abort(BlkActionState *common)
         aio_context = bdrv_get_aio_context(state->bs);
         aio_context_acquire(aio_context);
 
-        job_cancel_sync(&state->job->job, true);
+        job_cancel_sync_locked(&state->job->job, true);
 
         aio_context_release(aio_context);
     }
@@ -1933,7 +1933,7 @@ static void blockdev_backup_abort(BlkActionState *common)
         aio_context = bdrv_get_aio_context(state->bs);
         aio_context_acquire(aio_context);
 
-        job_cancel_sync(&state->job->job, true);
+        job_cancel_sync_locked(&state->job->job, true);
 
         aio_context_release(aio_context);
     }
@@ -2382,7 +2382,7 @@ exit:
     if (!has_props) {
         qapi_free_TransactionProperties(props);
     }
-    job_txn_unref(block_job_txn);
+    job_txn_unref_locked(block_job_txn);
 }
 
 BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
@@ -3347,14 +3347,14 @@ void qmp_block_job_cancel(const char *device,
         force = false;
     }
 
-    if (job_user_paused(&job->job) && !force) {
+    if (job_user_paused_locked(&job->job) && !force) {
         error_setg(errp, "The block job for device '%s' is currently paused",
                    device);
         goto out;
     }
 
     trace_qmp_block_job_cancel(job);
-    job_user_cancel(&job->job, force, errp);
+    job_user_cancel_locked(&job->job, force, errp);
 out:
     aio_context_release(aio_context);
 }
@@ -3369,7 +3369,7 @@ void qmp_block_job_pause(const char *device, Error **errp)
     }
 
     trace_qmp_block_job_pause(job);
-    job_user_pause(&job->job, errp);
+    job_user_pause_locked(&job->job, errp);
     aio_context_release(aio_context);
 }
 
@@ -3383,7 +3383,7 @@ void qmp_block_job_resume(const char *device, Error **errp)
     }
 
     trace_qmp_block_job_resume(job);
-    job_user_resume(&job->job, errp);
+    job_user_resume_locked(&job->job, errp);
     aio_context_release(aio_context);
 }
 
@@ -3397,7 +3397,7 @@ void qmp_block_job_complete(const char *device, Error **errp)
     }
 
     trace_qmp_block_job_complete(job);
-    job_complete(&job->job, errp);
+    job_complete_locked(&job->job, errp);
     aio_context_release(aio_context);
 }
 
@@ -3411,16 +3411,16 @@ void qmp_block_job_finalize(const char *id, Error **errp)
     }
 
     trace_qmp_block_job_finalize(job);
-    job_ref(&job->job);
-    job_finalize(&job->job, errp);
+    job_ref_locked(&job->job);
+    job_finalize_locked(&job->job, errp);
 
     /*
-     * Job's context might have changed via job_finalize (and job_txn_apply
-     * automatically acquires the new one), so make sure we release the correct
-     * one.
+     * Job's context might have changed via job_finalize_locked
+     * (and job_txn_apply automatically acquires the new one),
+     * so make sure we release the correct one.
      */
     aio_context = blk_get_aio_context(job->blk);
-    job_unref(&job->job);
+    job_unref_locked(&job->job);
     aio_context_release(aio_context);
 }
 
@@ -3436,7 +3436,7 @@ void qmp_block_job_dismiss(const char *id, Error **errp)
 
     trace_qmp_block_job_dismiss(bjob);
     job = &bjob->job;
-    job_dismiss(&job, errp);
+    job_dismiss_locked(&job, errp);
     aio_context_release(aio_context);
 }
 
blockjob.c +8 -8
index 74476af473..5b5d7f26b3 100644
@@ -65,7 +65,7 @@ BlockJob *block_job_next(BlockJob *bjob)
     assert(qemu_in_main_thread());
 
     do {
-        job = job_next(job);
+        job = job_next_locked(job);
     } while (job && !is_block_job(job));
 
     return job ? container_of(job, BlockJob, job) : NULL;
@@ -73,7 +73,7 @@ BlockJob *block_job_next(BlockJob *bjob)
 
 BlockJob *block_job_get(const char *id)
 {
-    Job *job = job_get(id);
+    Job *job = job_get_locked(id);
     assert(qemu_in_main_thread());
 
     if (job && is_block_job(job)) {
@@ -103,7 +103,7 @@ static char *child_job_get_parent_desc(BdrvChild *c)
 static void child_job_drained_begin(BdrvChild *c)
 {
     BlockJob *job = c->opaque;
-    job_pause(&job->job);
+    job_pause_locked(&job->job);
 }
 
 static bool child_job_drained_poll(BdrvChild *c)
@@ -115,7 +115,7 @@ static bool child_job_drained_poll(BdrvChild *c)
     /* An inactive or completed job doesn't have any pending requests. Jobs
      * with !job->busy are either already paused or have a pause point after
      * being reentered, so no job driver code will run before they pause. */
-    if (!job->busy || job_is_completed(job)) {
+    if (!job->busy || job_is_completed_locked(job)) {
         return false;
     }
 
@@ -131,7 +131,7 @@ static bool child_job_drained_poll(BdrvChild *c)
 static void child_job_drained_end(BdrvChild *c, int *drained_end_counter)
 {
     BlockJob *job = c->opaque;
-    job_resume(&job->job);
+    job_resume_locked(&job->job);
 }
 
 static bool child_job_can_set_aio_ctx(BdrvChild *c, AioContext *ctx,
@@ -279,7 +279,7 @@ bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
 
     assert(qemu_in_main_thread());
 
-    if (job_apply_verb(&job->job, JOB_VERB_SET_SPEED, errp) < 0) {
+    if (job_apply_verb_locked(&job->job, JOB_VERB_SET_SPEED, errp) < 0) {
         return false;
     }
     if (speed < 0) {
@@ -301,7 +301,7 @@ bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
     }
 
     /* kick only if a timer is pending */
-    job_enter_cond(&job->job, job_timer_pending);
+    job_enter_cond_locked(&job->job, job_timer_pending);
 
     return true;
 }
@@ -553,7 +553,7 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
     }
     if (action == BLOCK_ERROR_ACTION_STOP) {
         if (!job->job.user_paused) {
-            job_pause(&job->job);
+            job_pause_locked(&job->job);
             /* make the pause user visible, which will be resumed from QMP. */
             job->job.user_paused = true;
         }
include/qemu/job.h +108 -45

291 lines not on this page · raw message

job-qmp.c +13 -13
index 829a28aa70..de4120a1d4 100644
@@ -36,7 +36,7 @@ static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
 
     *aio_context = NULL;
 
-    job = job_get(id);
+    job = job_get_locked(id);
     if (!job) {
         error_setg(errp, "Job not found");
         return NULL;
@@ -58,7 +58,7 @@ void qmp_job_cancel(const char *id, Error **errp)
     }
 
     trace_qmp_job_cancel(job);
-    job_user_cancel(job, true, errp);
+    job_user_cancel_locked(job, true, errp);
     aio_context_release(aio_context);
 }
 
@@ -72,7 +72,7 @@ void qmp_job_pause(const char *id, Error **errp)
     }
 
     trace_qmp_job_pause(job);
-    job_user_pause(job, errp);
+    job_user_pause_locked(job, errp);
     aio_context_release(aio_context);
 }
 
@@ -86,7 +86,7 @@ void qmp_job_resume(const char *id, Error **errp)
     }
 
     trace_qmp_job_resume(job);
-    job_user_resume(job, errp);
+    job_user_resume_locked(job, errp);
     aio_context_release(aio_context);
 }
 
@@ -100,7 +100,7 @@ void qmp_job_complete(const char *id, Error **errp)
     }
 
     trace_qmp_job_complete(job);
-    job_complete(job, errp);
+    job_complete_locked(job, errp);
     aio_context_release(aio_context);
 }
 
@@ -114,16 +114,16 @@ void qmp_job_finalize(const char *id, Error **errp)
     }
 
     trace_qmp_job_finalize(job);
-    job_ref(job);
-    job_finalize(job, errp);
+    job_ref_locked(job);
+    job_finalize_locked(job, errp);
 
     /*
-     * Job's context might have changed via job_finalize (and job_txn_apply
-     * automatically acquires the new one), so make sure we release the correct
-     * one.
+     * Job's context might have changed via job_finalize_locked
+     * (and job_txn_apply automatically acquires the new one),
+     * so make sure we release the correct one.
      */
     aio_context = job->aio_context;
-    job_unref(job);
+    job_unref_locked(job);
     aio_context_release(aio_context);
 }
 
@@ -137,7 +137,7 @@ void qmp_job_dismiss(const char *id, Error **errp)
     }
 
     trace_qmp_job_dismiss(job);
-    job_dismiss(&job, errp);
+    job_dismiss_locked(&job, errp);
     aio_context_release(aio_context);
 }
 
@@ -171,7 +171,7 @@ JobInfoList *qmp_query_jobs(Error **errp)
     JobInfoList *head = NULL, **tail = &head;
     Job *job;
 
-    for (job = job_next(NULL); job; job = job_next(job)) {
+    for (job = job_next_locked(NULL); job; job = job_next_locked(job)) {
         JobInfo *value;
         AioContext *aio_context;
 
job.c +85 -70

453 lines not on this page · raw message

qemu-img.c +5 -5
index f036a1d428..09f3b11eab 100644
@@ -906,7 +906,7 @@ static void run_block_job(BlockJob *job, Error **errp)
     int ret = 0;
 
     aio_context_acquire(aio_context);
-    job_ref(&job->job);
+    job_ref_locked(&job->job);
     do {
         float progress = 0.0f;
         aio_poll(aio_context, true);
@@ -917,14 +917,14 @@ static void run_block_job(BlockJob *job, Error **errp)
             progress = (float)progress_current / progress_total * 100.f;
         }
         qemu_progress_print(progress, 0);
-    } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
+    } while (!job_is_ready(&job->job) && !job_is_completed_locked(&job->job));
 
-    if (!job_is_completed(&job->job)) {
-        ret = job_complete_sync(&job->job, errp);
+    if (!job_is_completed_locked(&job->job)) {
+        ret = job_complete_sync_locked(&job->job, errp);
     } else {
         ret = job->job.ret;
     }
-    job_unref(&job->job);
+    job_unref_locked(&job->job);
     aio_context_release(aio_context);
 
     /* publish completion progress only when success */
tests/unit/test-bdrv-drain.c +1 -1
index 2d3c17e566..3f344a0d0d 100644
@@ -995,7 +995,7 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type,
     g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
 
     aio_context_acquire(ctx);
-    ret = job_complete_sync(&job->job, &error_abort);
+    ret = job_complete_sync_locked(&job->job, &error_abort);
     g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
 
     if (use_iothread) {
tests/unit/test-block-iothread.c +2 -2
index aea660aeed..7e1b521d61 100644
@@ -456,7 +456,7 @@ static void test_attach_blockjob(void)
     }
 
     aio_context_acquire(ctx);
-    job_complete_sync(&tjob->common.job, &error_abort);
+    job_complete_sync_locked(&tjob->common.job, &error_abort);
     blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
     aio_context_release(ctx);
 
@@ -630,7 +630,7 @@ static void test_propagate_mirror(void)
                  BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
                  false, "filter_node", MIRROR_COPY_MODE_BACKGROUND,
                  &error_abort);
-    job = job_get("job0");
+    job = job_get_locked("job0");
     filter = bdrv_find_node("filter_node");
 
     /* Change the AioContext of src */
tests/unit/test-blockjob-txn.c +7 -7
index 8bd13b9949..5396fcef10 100644
@@ -125,7 +125,7 @@ static void test_single_job(int expected)
     job_start(&job->job);
 
     if (expected == -ECANCELED) {
-        job_cancel(&job->job, false);
+        job_cancel_locked(&job->job, false);
     }
 
     while (result == -EINPROGRESS) {
@@ -133,7 +133,7 @@ static void test_single_job(int expected)
     }
     g_assert_cmpint(result, ==, expected);
 
-    job_txn_unref(txn);
+    job_txn_unref_locked(txn);
 }
 
 static void test_single_job_success(void)
@@ -168,13 +168,13 @@ static void test_pair_jobs(int expected1, int expected2)
     /* Release our reference now to trigger as many nice
      * use-after-free bugs as possible.
      */
-    job_txn_unref(txn);
+    job_txn_unref_locked(txn);
 
     if (expected1 == -ECANCELED) {
-        job_cancel(&job1->job, false);
+        job_cancel_locked(&job1->job, false);
     }
     if (expected2 == -ECANCELED) {
-        job_cancel(&job2->job, false);
+        job_cancel_locked(&job2->job, false);
     }
 
     while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
@@ -227,7 +227,7 @@ static void test_pair_jobs_fail_cancel_race(void)
     job_start(&job1->job);
     job_start(&job2->job);
 
-    job_cancel(&job1->job, false);
+    job_cancel_locked(&job1->job, false);
 
     /* Now make job2 finish before the main loop kicks jobs.  This simulates
      * the race between a pending kick and another job completing.
@@ -242,7 +242,7 @@ static void test_pair_jobs_fail_cancel_race(void)
     g_assert_cmpint(result1, ==, -ECANCELED);
     g_assert_cmpint(result2, ==, -ECANCELED);
 
-    job_txn_unref(txn);
+    job_txn_unref_locked(txn);
 }
 
 int main(int argc, char **argv)
tests/unit/test-blockjob.c +15 -15
index 4c9e1bf1e5..2beed3623e 100644
@@ -211,7 +211,7 @@ static CancelJob *create_common(Job **pjob)
     bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
                   JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
     job = &bjob->job;
-    job_ref(job);
+    job_ref_locked(job);
     assert(job->status == JOB_STATUS_CREATED);
     s = container_of(bjob, CancelJob, common);
     s->blk = blk;
@@ -230,13 +230,13 @@ static void cancel_common(CancelJob *s)
     ctx = job->job.aio_context;
     aio_context_acquire(ctx);
 
-    job_cancel_sync(&job->job, true);
+    job_cancel_sync_locked(&job->job, true);
     if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
         Job *dummy = &job->job;
-        job_dismiss(&dummy, &error_abort);
+        job_dismiss_locked(&dummy, &error_abort);
     }
     assert(job->job.status == JOB_STATUS_NULL);
-    job_unref(&job->job);
+    job_unref_locked(&job->job);
     destroy_blk(blk);
 
     aio_context_release(ctx);
@@ -274,7 +274,7 @@ static void test_cancel_paused(void)
     job_start(job);
     assert(job->status == JOB_STATUS_RUNNING);
 
-    job_user_pause(job, &error_abort);
+    job_user_pause_locked(job, &error_abort);
     job_enter(job);
     assert(job->status == JOB_STATUS_PAUSED);
 
@@ -312,7 +312,7 @@ static void test_cancel_standby(void)
     job_enter(job);
     assert(job->status == JOB_STATUS_READY);
 
-    job_user_pause(job, &error_abort);
+    job_user_pause_locked(job, &error_abort);
     job_enter(job);
     assert(job->status == JOB_STATUS_STANDBY);
 
@@ -333,7 +333,7 @@ static void test_cancel_pending(void)
     job_enter(job);
     assert(job->status == JOB_STATUS_READY);
 
-    job_complete(job, &error_abort);
+    job_complete_locked(job, &error_abort);
     job_enter(job);
     while (!job->deferred_to_main_loop) {
         aio_poll(qemu_get_aio_context(), true);
@@ -359,7 +359,7 @@ static void test_cancel_concluded(void)
     job_enter(job);
     assert(job->status == JOB_STATUS_READY);
 
-    job_complete(job, &error_abort);
+    job_complete_locked(job, &error_abort);
     job_enter(job);
     while (!job->deferred_to_main_loop) {
         aio_poll(qemu_get_aio_context(), true);
@@ -369,7 +369,7 @@ static void test_cancel_concluded(void)
     assert(job->status == JOB_STATUS_PENDING);
 
     aio_context_acquire(job->aio_context);
-    job_finalize(job, &error_abort);
+    job_finalize_locked(job, &error_abort);
     aio_context_release(job->aio_context);
     assert(job->status == JOB_STATUS_CONCLUDED);
 
@@ -417,7 +417,7 @@ static const BlockJobDriver test_yielding_driver = {
 };
 
 /*
- * Test that job_complete() works even on jobs that are in a paused
+ * Test that job_complete_locked() works even on jobs that are in a paused
  * state (i.e., STANDBY).
  *
  * To do this, run YieldingJob in an IO thread, get it into the READY
@@ -425,7 +425,7 @@ static const BlockJobDriver test_yielding_driver = {
  * acquire the context so the job will not be entered and will thus
  * remain on STANDBY.
  *
- * job_complete() should still work without error.
+ * job_complete_locked() should still work without error.
  *
  * Note that on the QMP interface, it is impossible to lock an IO
  * thread before a drained section ends.  In practice, the
@@ -479,16 +479,16 @@ static void test_complete_in_standby(void)
     assert(job->status == JOB_STATUS_STANDBY);
 
     /* Even though the job is on standby, this should work */
-    job_complete(job, &error_abort);
+    job_complete_locked(job, &error_abort);
 
     /* The test is done now, clean up. */
-    job_finish_sync(job, NULL, &error_abort);
+    job_finish_sync_locked(job, NULL, &error_abort);
     assert(job->status == JOB_STATUS_PENDING);
 
-    job_finalize(job, &error_abort);
+    job_finalize_locked(job, &error_abort);
     assert(job->status == JOB_STATUS_CONCLUDED);
 
-    job_dismiss(&job, &error_abort);
+    job_dismiss_locked(&job, &error_abort);
 
     destroy_blk(blk);
     aio_context_release(ctx);
Signature · 1 line
2.31.1
#7 lore raw

Re: [PATCH v3 03/16] job.h: define locked functions

Quoted 11 lines · Emanuele Giuseppe Esposito

These functions assume that the job lock is held by the caller, to avoid TOC/TOU conditions. Therefore, their name must end with _locked.

Introduce also additional helpers that define _locked functions (useful when the job_mutex is globally applied).

Note: at this stage, job_{lock/unlock} and job lock guard macros are*nop*.

So, this is the only remaining issue: I am not sure about this rename. The functions you are changing are

+void job_txn_unref_locked(JobTxn *txn);
+void job_txn_add_job_locked(JobTxn *txn, Job *job);
+void job_ref_locked(Job *job);
+void job_unref_locked(Job *job);
+void job_enter_cond_locked(Job *job, bool(*fn)(Job *job)); +bool job_is_completed_locked(Job *job);
+bool job_is_ready_locked(Job *job);
+void job_pause_locked(Job *job);
+void job_resume_locked(Job *job);
+void job_user_pause_locked(Job *job, Error **errp);
+bool job_user_paused_locked(Job *job);
+void job_user_resume_locked(Job *job, Error **errp);
+Job *job_next_locked(Job *job);
+Job *job_get_locked(const char *id);
+int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp); +void job_early_fail_locked(Job *job);
+void job_complete_locked(Job *job, Error **errp);
+void job_cancel_locked(Job *job, bool force);
+void job_user_cancel_locked(Job *job, bool force, Error **errp); +int job_cancel_sync_locked(Job *job, bool force);
+int job_complete_sync_locked(Job *job, Error **errp);
+void job_finalize_locked(Job *job, Error **errp);
+void job_dismiss_locked(Job **job, Error **errp);
+int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),

and most of them (if not all?) will never be called by the job driver, only by the monitor. The two APIs (for driver / for monitor) are quite separate and have different locking policies: the monitor needs to take the lock to avoid TOC/TOU races, the driver generally can let the API take the lock.

The rename makes the monitor code heavier, but if you don't do the rename the functions in job.c are named very inconsistently. So I'm inclined to say this patch is fine---but I'd like to hear from others as well.

I think the two APIs should be in two different header files, similar to how you did the graph/IO split.

Paolo

#8 lore raw

Re: [PATCH v3 03/16] job.h: define locked functions

Quoted 46 lines · Paolo Bonzini
Quoted 11 lines · Emanuele Giuseppe Esposito

These functions assume that the job lock is held by the caller, to avoid TOC/TOU conditions. Therefore, their name must end with _locked.

Introduce also additional helpers that define _locked functions (useful when the job_mutex is globally applied).

Note: at this stage, job_{lock/unlock} and job lock guard macros are*nop*.

So, this is the only remaining issue: I am not sure about this rename. The functions you are changing are

(quoted diff) +24 -0
+void job_txn_unref_locked(JobTxn *txn);
+void job_txn_add_job_locked(JobTxn *txn, Job *job);
+void job_ref_locked(Job *job);
+void job_unref_locked(Job *job);
+void job_enter_cond_locked(Job *job, bool(*fn)(Job *job));
+bool job_is_completed_locked(Job *job);
+bool job_is_ready_locked(Job *job);
+void job_pause_locked(Job *job);
+void job_resume_locked(Job *job);
+void job_user_pause_locked(Job *job, Error **errp);
+bool job_user_paused_locked(Job *job);
+void job_user_resume_locked(Job *job, Error **errp);
+Job *job_next_locked(Job *job);
+Job *job_get_locked(const char *id);
+int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp);
+void job_early_fail_locked(Job *job);
+void job_complete_locked(Job *job, Error **errp);
+void job_cancel_locked(Job *job, bool force);
+void job_user_cancel_locked(Job *job, bool force, Error **errp);
+int job_cancel_sync_locked(Job *job, bool force);
+int job_complete_sync_locked(Job *job, Error **errp);
+void job_finalize_locked(Job *job, Error **errp);
+void job_dismiss_locked(Job **job, Error **errp);
+int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),

and most of them (if not all?) will never be called by the job driver, only by the monitor.  The two APIs (for driver / for monitor) are quite separate and have different locking policies: the monitor needs to take the lock to avoid TOC/TOU races, the driver generally can let the API take the lock.

Quoted 6 lines

The rename makes the monitor code heavier, but if you don't do the rename the functions in job.c are named very inconsistently.  So I'm inclined to say this patch is fine---but I'd like to hear from others as well.

I think the two APIs should be in two different header files, similar to how you did the graph/IO split.

The split was proposed in previous versions, but Vladimir did not really like it and suggested to send it as a separate series:

https://patchew.org/QEMU/[email protected]/

Vladimir's comment:
https://patchew.org/QEMU/[email protected]/

Thank you,
Emanuele

Quoted 3 lines

Paolo

#9 lore raw

Re: [PATCH v3 03/16] job.h: define locked functions

Quoted 49 lines · Emanuele Giuseppe Esposito
Quoted 46 lines · Paolo Bonzini
Quoted 11 lines · Emanuele Giuseppe Esposito

These functions assume that the job lock is held by the caller, to avoid TOC/TOU conditions. Therefore, their name must end with _locked.

Introduce also additional helpers that define _locked functions (useful when the job_mutex is globally applied).

Note: at this stage, job_{lock/unlock} and job lock guard macros are*nop*.

So, this is the only remaining issue: I am not sure about this rename. The functions you are changing are

(quoted diff) +24 -0
+void job_txn_unref_locked(JobTxn *txn);
+void job_txn_add_job_locked(JobTxn *txn, Job *job);
+void job_ref_locked(Job *job);
+void job_unref_locked(Job *job);
+void job_enter_cond_locked(Job *job, bool(*fn)(Job *job));
+bool job_is_completed_locked(Job *job);
+bool job_is_ready_locked(Job *job);
+void job_pause_locked(Job *job);
+void job_resume_locked(Job *job);
+void job_user_pause_locked(Job *job, Error **errp);
+bool job_user_paused_locked(Job *job);
+void job_user_resume_locked(Job *job, Error **errp);
+Job *job_next_locked(Job *job);
+Job *job_get_locked(const char *id);
+int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp);
+void job_early_fail_locked(Job *job);
+void job_complete_locked(Job *job, Error **errp);
+void job_cancel_locked(Job *job, bool force);
+void job_user_cancel_locked(Job *job, bool force, Error **errp);
+int job_cancel_sync_locked(Job *job, bool force);
+int job_complete_sync_locked(Job *job, Error **errp);
+void job_finalize_locked(Job *job, Error **errp);
+void job_dismiss_locked(Job **job, Error **errp);
+int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),

and most of them (if not all?) will never be called by the job driver, only by the monitor.  The two APIs (for driver / for monitor) are quite separate and have different locking policies: the monitor needs to take the lock to avoid TOC/TOU races, the driver generally can let the API take the lock.

Quoted 8 lines
Quoted 6 lines

The rename makes the monitor code heavier, but if you don't do the rename the functions in job.c are named very inconsistently.  So I'm inclined to say this patch is fine---but I'd like to hear from others as well.

I think the two APIs should be in two different header files, similar to how you did the graph/IO split.

The split was proposed in previous versions, but Vladimir did not really like it and suggested to send it as a separate series:

I didn't really like it as it seemed unusual and unobvious to me. But if we already accepted similar split for generic block layer, no way for me to resist :) And if we follow new logic of generic block layer in jobs, it's not "unusual" any more.

Quoted 14 lines

https://patchew.org/QEMU/[email protected]/

Vladimir's comment:
https://patchew.org/QEMU/[email protected]/

Thank you,
Emanuele

Quoted 3 lines

Paolo

Signature · 2 lines
Best regards,
Vladimir
#10 lore raw

Re: [PATCH v3 03/16] job.h: define locked functions

Quoted 4 lines · Vladimir Sementsov-Ogievskiy
Quoted 2 lines

The split was proposed in previous versions, but Vladimir did not really like it and suggested to send it as a separate series:

I didn't really like it as it seemed unusual and unobvious to me. But if we already accepted similar split for generic block layer, no way for me to resist :) And if we follow new logic of generic block layer in jobs, it's not "unusual" any more.

Either way I think it's okay to have it as a follow-up. The explicit naming in the API is a bit verbose but definitely clearer, so it's okay to order different than the graph/IO split. In that case we weren't even sure, until you went through all the testcase failures, that a _locked or rather "_drained" API was possible.

Paolo

#11 lore raw

Re: [PATCH v3 03/16] job.h: define locked functions

Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Vladimir Sementsov-Ogievskiy
Quoted 3 lines

The split was proposed in previous versions, but Vladimir did not really like it and suggested to send it as a separate series:

I didn't really like it as it seemed unusual and unobvious to me. But if we already accepted similar split for generic block layer, no way for me to resist :) And if we follow new logic of generic block layer in jobs, it's not "unusual" any more.

Quoted 8 lines

Either way I think it's okay to have it as a follow-up.  The explicit naming in the API is a bit verbose but definitely clearer, so it's okay to order different than the graph/IO split.  In that case we weren't even sure, until you went through all the testcase failures, that a _locked or rather "_drained" API was possible.

Paolo

Ok, I will send the split in a separate series.

Thank you,
Emanuele

#12 lore raw

PATCH v3 4/16 job.h: define unlocked functions

In reply to Emanuele Giuseppe Esposito

All these functions assume that the lock is not held, and acquire it internally.

These functions will be useful when job_lock is globally applied, as they will allow callers to access the job struct fields without worrying about the job lock.

Update also the comments in blockjob.c (and move them in job.c).

Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*.

---
 blockjob.c         | 20 -------------
 include/qemu/job.h | 68 ++++++++++++++++++++++++++++++++++++++++++--
 job.c              | 70 ++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 133 insertions(+), 25 deletions(-)
blockjob.c +0 -20
index 5b5d7f26b3..ce356be51e 100644
@@ -36,21 +36,6 @@
 #include "qemu/main-loop.h"
 #include "qemu/timer.h"
 
-/*
- * The block job API is composed of two categories of functions.
- *
- * The first includes functions used by the monitor.  The monitor is
- * peculiar in that it accesses the block job list with block_job_get, and
- * therefore needs consistency across block_job_get and the actual operation
- * (e.g. block_job_set_speed).  The consistency is achieved with
- * aio_context_acquire/release.  These functions are declared in blockjob.h.
- *
- * The second includes functions used by the block job drivers and sometimes
- * by the core block layer.  These do not care about locking, because the
- * whole coroutine runs under the AioContext lock, and are declared in
- * blockjob_int.h.
- */
-
 static bool is_block_job(Job *job)
 {
     return job_type(job) == JOB_TYPE_BACKUP ||
@@ -433,11 +418,6 @@ static void block_job_event_ready(Notifier *n, void *opaque)
 }
 
 
-/*
- * API for block job drivers and the block layer.  These functions are
- * declared in blockjob_int.h.
- */
-
 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
                        JobTxn *txn, BlockDriverState *bs, uint64_t perm,
                        uint64_t shared_perm, int64_t speed, int flags,
include/qemu/job.h +65 -3

149 lines not on this page · raw message

job.c +68 -2

98 lines not on this page · raw message

Signature · 1 line
2.31.1
#13 lore raw

PATCH v3 5/16 block/mirror.c: use of job helpers in drivers to avoid TOC/TOU

In reply to Emanuele Giuseppe Esposito

Once job lock is used and aiocontext is removed, mirror has to perform job operations under the same critical section, using the helpers prepared in previous commit.

Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*.

---
 block/mirror.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
block/mirror.c +14 -5
index 00089e519b..41450df55c 100644
@@ -653,9 +653,13 @@ static int mirror_exit_common(Job *job)
     BlockDriverState *target_bs;
     BlockDriverState *mirror_top_bs;
     Error *local_err = NULL;
-    bool abort = job->ret < 0;
+    bool abort;
     int ret = 0;
 
+    WITH_JOB_LOCK_GUARD() {
+        abort = job->ret < 0;
+    }
+
     if (s->prepared) {
         return 0;
     }
@@ -1161,8 +1165,10 @@ static void mirror_complete(Job *job, Error **errp)
     s->should_complete = true;
 
     /* If the job is paused, it will be re-entered when it is resumed */
-    if (!job->paused) {
-        job_enter(job);
+    WITH_JOB_LOCK_GUARD() {
+        if (!job->paused) {
+            job_enter_cond_locked(job, NULL);
+        }
     }
 }
 
@@ -1182,8 +1188,11 @@ static bool mirror_drained_poll(BlockJob *job)
      * from one of our own drain sections, to avoid a deadlock waiting for
      * ourselves.
      */
-    if (!s->common.job.paused && !job_is_cancelled(&job->job) && !s->in_drain) {
-        return true;
+    WITH_JOB_LOCK_GUARD() {
+        if (!s->common.job.paused && !job_is_cancelled_locked(&job->job)
+            && !s->in_drain) {
+            return true;
+        }
     }
 
     return !!s->in_flight;
Signature · 1 line
2.31.1
#14 lore raw

Re: [PATCH v3 05/16] block/mirror.c: use of job helpers in drivers to avoid TOC/TOU

block/mirror.c static int mirror_exit_common(Job *job) · line 665

Quoted 8 lines · Emanuele Giuseppe Esposito
block/mirror.c +4 -0
+    WITH_JOB_LOCK_GUARD() {
+        abort = job->ret < 0;
+    }
+
     if (s->prepared) {
         return 0;
     }

At this point I think job->ret is stable and can be accessed without guard. The question however is what serializes calls to job_prepare. Is it the BQL? Can we say that job->ret is only written under BQL, just like job->aio_context.

block/mirror.c static void mirror_complete(Job *job, Error **errp) · line 1172

Quoted 11 lines
block/mirror.c +4 -2
@@ -1161,8 +1165,10 @@ static void mirror_complete(Job *job, Error **errp)
     s->should_complete = true;
 
     /* If the job is paused, it will be re-entered when it is resumed */
-    if (!job->paused) {
-        job_enter(job);
+    WITH_JOB_LOCK_GUARD() {
+        if (!job->paused) {
+            job_enter_cond_locked(job, NULL);
+        }
     }

I don't want to open a can of worms, but does it ever make sense to call job_enter while the job is paused? Should this condition be moved to job_enter_cond_locked?

Paolo

#15 lore raw

PATCH v3 6/16 job.c: make job_event_* functions static

In reply to Emanuele Giuseppe Esposito

job_event_* functions can all be static, as they are not used outside job.c.

---
 include/qemu/job.h |  6 ------
 job.c              | 12 ++++++++++--
 2 files changed, 10 insertions(+), 8 deletions(-)
include/qemu/job.h +0 -6
index f800b0b881..c95f9fa8d1 100644
@@ -454,12 +454,6 @@ void job_progress_set_remaining(Job *job, uint64_t remaining);
  */
 void job_progress_increase_remaining(Job *job, uint64_t delta);
 
-/** To be called when a cancelled job is finalised. */
-void job_event_cancelled(Job *job);
-
-/** To be called when a successfully completed job is finalised. */
-void job_event_completed(Job *job);
-
 /**
  * Conditionally enter the job coroutine if the job is ready to run, not
  * already busy and fn() returns true. fn() is called while under the job_lock
job.c +10 -2
index f4e1a56705..b0dba40728 100644
@@ -498,12 +498,20 @@ void job_progress_increase_remaining(Job *job, uint64_t delta)
     progress_increase_remaining(&job->progress, delta);
 }
 
-void job_event_cancelled(Job *job)
+/**
+ * To be called when a cancelled job is finalised.
+ * Called with job_mutex held.
+ */
+static void job_event_cancelled(Job *job)
 {
     notifier_list_notify(&job->on_finalize_cancelled, job);
 }
 
-void job_event_completed(Job *job)
+/**
+ * To be called when a successfully completed job is finalised.
+ * Called with job_mutex held.
+ */
+static void job_event_completed(Job *job)
 {
     notifier_list_notify(&job->on_finalize_completed, job);
 }
Signature · 1 line
2.31.1
#16 lore raw

PATCH v3 7/16 job.c: move inner aiocontext lock in callbacks

In reply to Emanuele Giuseppe Esposito

Instead of having the lock in job_tnx_apply, move it inside in the callback. This will be helpful for next commits, when we introduce job_lock/unlock pairs.

job_transition_to_pending() and job_needs_finalize() do not need to be protected by the aiocontext lock.

No functional change intended.

---
 job.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
job.c +11 -4
index b0dba40728..2ee7233763 100644
@@ -165,7 +165,6 @@ static void job_txn_del_job(Job *job)
 
 static int job_txn_apply(Job *job, int fn(Job *))
 {
-    AioContext *inner_ctx;
     Job *other_job, *next;
     JobTxn *txn = job->txn;
     int rc = 0;
@@ -180,10 +179,7 @@ static int job_txn_apply(Job *job, int fn(Job *))
     aio_context_release(job->aio_context);
 
     QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
-        inner_ctx = other_job->aio_context;
-        aio_context_acquire(inner_ctx);
         rc = fn(other_job);
-        aio_context_release(inner_ctx);
         if (rc) {
             break;
         }
@@ -796,11 +792,15 @@ static void job_clean(Job *job)
 
 static int job_finalize_single(Job *job)
 {
+    AioContext *ctx = job->aio_context;
+
     assert(job_is_completed_locked(job));
 
     /* Ensure abort is called for late-transactional failures */
     job_update_rc(job);
 
+    aio_context_acquire(ctx);
+
     if (!job->ret) {
         job_commit(job);
     } else {
@@ -808,6 +808,8 @@ static int job_finalize_single(Job *job)
     }
     job_clean(job);
 
+    aio_context_release(ctx);
+
     if (job->cb) {
         job->cb(job->opaque, job->ret);
     }
@@ -928,11 +930,16 @@ static void job_completed_txn_abort(Job *job)
 
 static int job_prepare(Job *job)
 {
+    AioContext *ctx = job->aio_context;
     assert(qemu_in_main_thread());
+
     if (job->ret == 0 && job->driver->prepare) {
+        aio_context_acquire(ctx);
         job->ret = job->driver->prepare(job);
+        aio_context_release(ctx);
         job_update_rc(job);
     }
+
     return job->ret;
 }
 
Signature · 1 line
2.31.1
#17 lore raw

PATCH v3 8/16 aio-wait.h: introduce AIO_WAIT_WHILE_UNLOCKED

In reply to Emanuele Giuseppe Esposito

Same as AIO_WAIT_WHILE macro, but if we are in the Main loop do not release and then acquire ctx_ 's aiocontext.

Once all Aiocontext locks go away, this macro will replace AIO_WAIT_WHILE.

---
 include/block/aio-wait.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
include/block/aio-wait.h +11 -4
index b39eefb38d..ff27fe4eab 100644
@@ -59,10 +59,11 @@ typedef struct {
 extern AioWait global_aio_wait;
 
 /**
- * AIO_WAIT_WHILE:
+ * _AIO_WAIT_WHILE:
  * @ctx: the aio context, or NULL if multiple aio contexts (for which the
  *       caller does not hold a lock) are involved in the polling condition.
  * @cond: wait while this conditional expression is true
+ * @unlock: whether to unlock and then lock again @ctx
  *
  * Wait while a condition is true.  Use this to implement synchronous
  * operations that require event loop activity.
@@ -75,7 +76,7 @@ extern AioWait global_aio_wait;
  * wait on conditions between two IOThreads since that could lead to deadlock,
  * go via the main loop instead.
  */
-#define AIO_WAIT_WHILE(ctx, cond) ({                               \
+#define _AIO_WAIT_WHILE(ctx, cond, unlock) ({                      \
     bool waited_ = false;                                          \
     AioWait *wait_ = &global_aio_wait;                             \
     AioContext *ctx_ = (ctx);                                      \
@@ -90,11 +91,11 @@ extern AioWait global_aio_wait;
         assert(qemu_get_current_aio_context() ==                   \
                qemu_get_aio_context());                            \
         while ((cond)) {                                           \
-            if (ctx_) {                                            \
+            if (unlock && ctx_) {                                  \
                 aio_context_release(ctx_);                         \
             }                                                      \
             aio_poll(qemu_get_aio_context(), true);                \
-            if (ctx_) {                                            \
+            if (unlock && ctx_) {                                  \
                 aio_context_acquire(ctx_);                         \
             }                                                      \
             waited_ = true;                                        \
@@ -103,6 +104,12 @@ extern AioWait global_aio_wait;
     qatomic_dec(&wait_->num_waiters);                              \
     waited_; })
 
+#define AIO_WAIT_WHILE(ctx, cond)                                  \
+    _AIO_WAIT_WHILE(ctx, cond, true)
+
+#define AIO_WAIT_WHILE_UNLOCKED(ctx, cond)                         \
+    _AIO_WAIT_WHILE(ctx, cond, false)
+
 /**
  * aio_wait_kick:
  * Wake up the main thread if it is waiting on AIO_WAIT_WHILE().  During
Signature · 1 line
2.31.1
#18 lore raw

PATCH v3 9/16 jobs: remove aiocontext locks since the functions are under BQL

In reply to Emanuele Giuseppe Esposito

In preparation to the job_lock/unlock patch, remove these aiocontext locks.
The main reason these two locks are removed here is because they are inside a loop iterating on the jobs list. Once the job_lock is added, it will have to protect the whole loop, wrapping also the aiocontext acquire/release.

We don't want this, as job_lock can only be *wrapped by* the aiocontext lock, and not vice-versa, to avoid deadlocks.

---
 blockdev.c | 4 ----
 job-qmp.c  | 4 ----
 2 files changed, 8 deletions(-)
blockdev.c +0 -4
index 11fd651bde..ee35aff13a 100644
@@ -3707,15 +3707,11 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp)
 
     for (job = block_job_next(NULL); job; job = block_job_next(job)) {
         BlockJobInfo *value;
-        AioContext *aio_context;
 
         if (block_job_is_internal(job)) {
             continue;
         }
-        aio_context = blk_get_aio_context(job->blk);
-        aio_context_acquire(aio_context);
         value = block_job_query(job, errp);
-        aio_context_release(aio_context);
         if (!value) {
             qapi_free_BlockJobInfoList(head);
             return NULL;
job-qmp.c +0 -4
index de4120a1d4..f6f9840436 100644
@@ -173,15 +173,11 @@ JobInfoList *qmp_query_jobs(Error **errp)
 
     for (job = job_next_locked(NULL); job; job = job_next_locked(job)) {
         JobInfo *value;
-        AioContext *aio_context;
 
         if (job_is_internal(job)) {
             continue;
         }
-        aio_context = job->aio_context;
-        aio_context_acquire(aio_context);
         value = job_query_single(job, errp);
-        aio_context_release(aio_context);
         if (!value) {
             qapi_free_JobInfoList(head);
             return NULL;
Signature · 1 line
2.31.1
#19 lore raw

Re: [PATCH v3 09/16] jobs: remove aiocontext locks since the functions are under BQL

Quoted 9 lines · Emanuele Giuseppe Esposito

In preparation to the job_lock/unlock patch, remove these aiocontext locks.
The main reason these two locks are removed here is because they are inside a loop iterating on the jobs list. Once the job_lock is added, it will have to protect the whole loop, wrapping also the aiocontext acquire/release.

We don't want this, as job_lock can only be *wrapped by* the aiocontext lock, and not vice-versa, to avoid deadlocks.

Better to avoid the passive: "must be taken inside the AioContext lock, and taking it outside would cause deadlocks". Also add a note about the lock hierarchy to patch 1.

blockdev.c BlockJobInfoList *qmp_query_block_jobs(Error **errp) · line 3713

Quoted 9 lines
blockdev.c +0 -1
@@ -3707,15 +3707,11 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp)
 
      for (job = block_job_next(NULL); job; job = block_job_next(job)) {
          BlockJobInfo *value;
-        AioContext *aio_context;
 
          if (block_job_is_internal(job)) {
              continue;
          }

block_job_next, block_job_query, etc. do not have the _locked suffix. Is this because all block_job_ functions need the job_mutex held, or just laziness? :)

Paolo

Quoted 27 lines
blockdev.c +0 -3
-        aio_context = blk_get_aio_context(job->blk);
-        aio_context_acquire(aio_context);
          value = block_job_query(job, errp);
-        aio_context_release(aio_context);
          if (!value) {
              qapi_free_BlockJobInfoList(head);
              return NULL;
job-qmp.c +0 -4
index de4120a1d4..f6f9840436 100644
@@ -173,15 +173,11 @@ JobInfoList *qmp_query_jobs(Error **errp)
 
      for (job = job_next_locked(NULL); job; job = job_next_locked(job)) {
          JobInfo *value;
-        AioContext *aio_context;
 
          if (job_is_internal(job)) {
              continue;
          }
-        aio_context = job->aio_context;
-        aio_context_acquire(aio_context);
          value = job_query_single(job, errp);
-        aio_context_release(aio_context);
          if (!value) {
              qapi_free_JobInfoList(head);
              return NULL;
#20 lore raw

Re: [PATCH v3 09/16] jobs: remove aiocontext locks since the functions are under BQL

Quoted 5 lines · Paolo Bonzini
Quoted 5 lines
(quoted diff) +0 -0
@@ -3707,15 +3707,11 @@ BlockJobInfoList *qmp_query_block_jobs(Error
 **errp)

        for (job = block_job_next(NULL); job; job = block_job_next(job)) {
          BlockJobInfo *value;

Quoted 9 lines
Quoted 4 lines

-        AioContext *aio_context;
            if (block_job_is_internal(job)) {
              continue;
          }

block_job_next, block_job_query, etc. do not have the _locked suffix. Is this because all block_job_ functions need the job_mutex held, or just laziness? :)

I wasn't really sure whether to touch that API naming or not (+ laziness :D )

But it makes sense to add _locked also there. Will do.

Thank you,
Emanuele

#21 lore raw

PATCH v3 10/16 jobs: protect jobs with job_lock/unlock

In reply to Emanuele Giuseppe Esposito

Introduce the job locking mechanism through the whole job API, following the comments and requirements of job-monitor (assume lock is held) and job-driver (lock is not held).

job_{lock/unlock} is independent from real_job_{lock/unlock}.

Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*.

---
 block.c             |  18 +++---
 block/replication.c |   8 ++-
 blockdev.c          |  17 +++++-
 blockjob.c          |  64 ++++++++++++++-------
 job-qmp.c           |   2 +
 job.c               | 132 +++++++++++++++++++++++++++++++-------------
 monitor/qmp-cmds.c  |   6 +-
 qemu-img.c          |  41 ++++++++------
 8 files changed, 199 insertions(+), 89 deletions(-)
block.c +11 -7
index 8fcd525fa0..fac0759422 100644
@@ -4976,7 +4976,9 @@ static void bdrv_close(BlockDriverState *bs)
 
 void bdrv_close_all(void)
 {
-    assert(job_next_locked(NULL) == NULL);
+    WITH_JOB_LOCK_GUARD() {
+        assert(job_next_locked(NULL) == NULL);
+    }
     assert(qemu_in_main_thread());
 
     /* Drop references from requests still in flight, such as canceled block
@@ -6154,13 +6156,15 @@ XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp)
         }
     }
 
-    for (job = block_job_next(NULL); job; job = block_job_next(job)) {
-        GSList *el;
+    WITH_JOB_LOCK_GUARD() {
+        for (job = block_job_next(NULL); job; job = block_job_next(job)) {
+            GSList *el;
 
-        xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB,
-                           job->job.id);
-        for (el = job->nodes; el; el = el->next) {
-            xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data);
+            xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB,
+                                job->job.id);
+            for (el = job->nodes; el; el = el->next) {
+                xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data);
+            }
         }
     }
 
block/replication.c +6 -2
index 5215c328c1..50ea778937 100644
@@ -149,7 +149,9 @@ static void replication_close(BlockDriverState *bs)
     if (s->stage == BLOCK_REPLICATION_FAILOVER) {
         commit_job = &s->commit_job->job;
         assert(commit_job->aio_context == qemu_get_current_aio_context());
-        job_cancel_sync_locked(commit_job, false);
+        WITH_JOB_LOCK_GUARD() {
+            job_cancel_sync_locked(commit_job, false);
+        }
     }
 
     if (s->mode == REPLICATION_MODE_SECONDARY) {
@@ -726,7 +728,9 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
          * disk, secondary disk in backup_job_completed().
          */
         if (s->backup_job) {
-            job_cancel_sync_locked(&s->backup_job->job, true);
+            WITH_JOB_LOCK_GUARD() {
+                job_cancel_sync_locked(&s->backup_job->job, true);
+            }
         }
 
         if (!failover) {
blockdev.c +14 -3
index ee35aff13a..099d57e0d2 100644
@@ -155,6 +155,8 @@ void blockdev_mark_auto_del(BlockBackend *blk)
         return;
     }
 
+    JOB_LOCK_GUARD();
+
     for (job = block_job_next(NULL); job; job = block_job_next(job)) {
         if (block_job_has_bdrv(job, blk_bs(blk))) {
             AioContext *aio_context = job->job.aio_context;
@@ -1832,7 +1834,9 @@ static void drive_backup_abort(BlkActionState *common)
         aio_context = bdrv_get_aio_context(state->bs);
         aio_context_acquire(aio_context);
 
-        job_cancel_sync_locked(&state->job->job, true);
+        WITH_JOB_LOCK_GUARD() {
+            job_cancel_sync_locked(&state->job->job, true);
+        }
 
         aio_context_release(aio_context);
     }
@@ -1933,7 +1937,9 @@ static void blockdev_backup_abort(BlkActionState *common)
         aio_context = bdrv_get_aio_context(state->bs);
         aio_context_acquire(aio_context);
 
-        job_cancel_sync_locked(&state->job->job, true);
+        WITH_JOB_LOCK_GUARD() {
+            job_cancel_sync_locked(&state->job->job, true);
+        }
 
         aio_context_release(aio_context);
     }
@@ -2382,7 +2388,10 @@ exit:
     if (!has_props) {
         qapi_free_TransactionProperties(props);
     }
-    job_txn_unref_locked(block_job_txn);
+
+    WITH_JOB_LOCK_GUARD() {
+        job_txn_unref_locked(block_job_txn);
+    }
 }
 
 BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
@@ -3705,6 +3714,8 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp)
     BlockJobInfoList *head = NULL, **tail = &head;
     BlockJob *job;
 
+    JOB_LOCK_GUARD();
+
     for (job = block_job_next(NULL); job; job = block_job_next(job)) {
         BlockJobInfo *value;
 
blockjob.c +45 -19

122 lines not on this page · raw message

job-qmp.c +2 -0
index f6f9840436..9fa14bf761 100644
@@ -171,6 +171,8 @@ JobInfoList *qmp_query_jobs(Error **errp)
     JobInfoList *head = NULL, **tail = &head;
     Job *job;
 
+    JOB_LOCK_GUARD();
+
     for (job = job_next_locked(NULL); job; job = job_next_locked(job)) {
         JobInfo *value;
 
job.c +93 -39

358 lines not on this page · raw message

monitor/qmp-cmds.c +4 -2
index 343353e27a..2f11d086a6 100644
@@ -133,8 +133,10 @@ void qmp_cont(Error **errp)
         blk_iostatus_reset(blk);
     }
 
-    for (job = block_job_next(NULL); job; job = block_job_next(job)) {
-        block_job_iostatus_reset(job);
+    WITH_JOB_LOCK_GUARD() {
+        for (job = block_job_next(NULL); job; job = block_job_next(job)) {
+            block_job_iostatus_reset(job);
+        }
     }
 
     /* Continuing after completed migration. Images have been inactivated to
qemu-img.c +24 -17

56 lines not on this page · raw message

Signature · 1 line
2.31.1
#22 lore raw

Re: [PATCH v3 10/16] jobs: protect jobs with job_lock/unlock

Quoted 746 lines · Emanuele Giuseppe Esposito

Introduce the job locking mechanism through the whole job API, following the comments and requirements of job-monitor (assume lock is held) and job-driver (lock is not held).

job_{lock/unlock} is independent from real_job_{lock/unlock}.

Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*.

---
  block.c             |  18 +++---
  block/replication.c |   8 ++-
  blockdev.c          |  17 +++++-
  blockjob.c          |  64 ++++++++++++++-------
  job-qmp.c           |   2 +
  job.c               | 132 +++++++++++++++++++++++++++++++-------------
  monitor/qmp-cmds.c  |   6 +-
  qemu-img.c          |  41 ++++++++------
  8 files changed, 199 insertions(+), 89 deletions(-)
block.c +11 -7
index 8fcd525fa0..fac0759422 100644
@@ -4976,7 +4976,9 @@ static void bdrv_close(BlockDriverState *bs)
 
  void bdrv_close_all(void)
  {
-    assert(job_next_locked(NULL) == NULL);
+    WITH_JOB_LOCK_GUARD() {
+        assert(job_next_locked(NULL) == NULL);
+    }
      assert(qemu_in_main_thread());
 
      /* Drop references from requests still in flight, such as canceled block
@@ -6154,13 +6156,15 @@ XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp)
          }
      }
 
-    for (job = block_job_next(NULL); job; job = block_job_next(job)) {
-        GSList *el;
+    WITH_JOB_LOCK_GUARD() {
+        for (job = block_job_next(NULL); job; job = block_job_next(job)) {
+            GSList *el;
 
-        xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB,
-                           job->job.id);
-        for (el = job->nodes; el; el = el->next) {
-            xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data);
+            xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB,
+                                job->job.id);
+            for (el = job->nodes; el; el = el->next) {
+                xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data);
+            }
          }
      }
 
block/replication.c +6 -2
index 5215c328c1..50ea778937 100644
@@ -149,7 +149,9 @@ static void replication_close(BlockDriverState *bs)
      if (s->stage == BLOCK_REPLICATION_FAILOVER) {
          commit_job = &s->commit_job->job;
          assert(commit_job->aio_context == qemu_get_current_aio_context());
-        job_cancel_sync_locked(commit_job, false);
+        WITH_JOB_LOCK_GUARD() {
+            job_cancel_sync_locked(commit_job, false);
+        }
      }
 
      if (s->mode == REPLICATION_MODE_SECONDARY) {
@@ -726,7 +728,9 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
           * disk, secondary disk in backup_job_completed().
           */
          if (s->backup_job) {
-            job_cancel_sync_locked(&s->backup_job->job, true);
+            WITH_JOB_LOCK_GUARD() {
+                job_cancel_sync_locked(&s->backup_job->job, true);
+            }
          }
 
          if (!failover) {
blockdev.c +14 -3
index ee35aff13a..099d57e0d2 100644
@@ -155,6 +155,8 @@ void blockdev_mark_auto_del(BlockBackend *blk)
          return;
      }
 
+    JOB_LOCK_GUARD();
+
      for (job = block_job_next(NULL); job; job = block_job_next(job)) {
          if (block_job_has_bdrv(job, blk_bs(blk))) {
              AioContext *aio_context = job->job.aio_context;
@@ -1832,7 +1834,9 @@ static void drive_backup_abort(BlkActionState *common)
          aio_context = bdrv_get_aio_context(state->bs);
          aio_context_acquire(aio_context);
 
-        job_cancel_sync_locked(&state->job->job, true);
+        WITH_JOB_LOCK_GUARD() {
+            job_cancel_sync_locked(&state->job->job, true);
+        }
 
          aio_context_release(aio_context);
      }
@@ -1933,7 +1937,9 @@ static void blockdev_backup_abort(BlkActionState *common)
          aio_context = bdrv_get_aio_context(state->bs);
          aio_context_acquire(aio_context);
 
-        job_cancel_sync_locked(&state->job->job, true);
+        WITH_JOB_LOCK_GUARD() {
+            job_cancel_sync_locked(&state->job->job, true);
+        }
 
          aio_context_release(aio_context);
      }
@@ -2382,7 +2388,10 @@ exit:
      if (!has_props) {
          qapi_free_TransactionProperties(props);
      }
-    job_txn_unref_locked(block_job_txn);
+
+    WITH_JOB_LOCK_GUARD() {
+        job_txn_unref_locked(block_job_txn);
+    }
  }
 
  BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
@@ -3705,6 +3714,8 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp)
      BlockJobInfoList *head = NULL, **tail = &head;
      BlockJob *job;
 
+    JOB_LOCK_GUARD();
+
      for (job = block_job_next(NULL); job; job = block_job_next(job)) {
          BlockJobInfo *value;
 
blockjob.c +45 -19
index ce356be51e..e00c8d31d5 100644
@@ -88,7 +88,9 @@ static char *child_job_get_parent_desc(BdrvChild *c)
  static void child_job_drained_begin(BdrvChild *c)
  {
      BlockJob *job = c->opaque;
-    job_pause_locked(&job->job);
+    WITH_JOB_LOCK_GUARD() {
+        job_pause_locked(&job->job);
+    }
  }
 
  static bool child_job_drained_poll(BdrvChild *c)
@@ -100,8 +102,10 @@ static bool child_job_drained_poll(BdrvChild *c)
      /* An inactive or completed job doesn't have any pending requests. Jobs
       * with !job->busy are either already paused or have a pause point after
       * being reentered, so no job driver code will run before they pause. */
-    if (!job->busy || job_is_completed_locked(job)) {
-        return false;
+    WITH_JOB_LOCK_GUARD() {
+        if (!job->busy || job_is_completed_locked(job)) {
+            return false;
+        }
      }
 
      /* Otherwise, assume that it isn't fully stopped yet, but allow the job to
@@ -116,7 +120,9 @@ static bool child_job_drained_poll(BdrvChild *c)
  static void child_job_drained_end(BdrvChild *c, int *drained_end_counter)
  {
      BlockJob *job = c->opaque;
-    job_resume_locked(&job->job);
+    WITH_JOB_LOCK_GUARD() {
+        job_resume_locked(&job->job);
+    }
  }
 
  static bool child_job_can_set_aio_ctx(BdrvChild *c, AioContext *ctx,
@@ -238,7 +244,13 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
 
  static void block_job_on_idle(Notifier *n, void *opaque)
  {
+    /*
+     * we can't kick with job_mutex held, but we also want
+     * to protect the notifier list.
+     */
+    job_unlock();
      aio_wait_kick();
+    job_lock();
  }
 
  bool block_job_is_internal(BlockJob *job)
@@ -278,7 +290,9 @@ bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
      job->speed = speed;
 
      if (drv->set_speed) {
+        job_unlock();
          drv->set_speed(job, speed);
+        job_lock();
      }
 
      if (speed && speed <= old_speed) {
@@ -458,13 +472,15 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
      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);
-    notifier_list_add(&job->job.on_finalize_completed,
-                      &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);
+    WITH_JOB_LOCK_GUARD() {
+        notifier_list_add(&job->job.on_finalize_cancelled,
+                          &job->finalize_cancelled_notifier);
+        notifier_list_add(&job->job.on_finalize_completed,
+                          &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));
@@ -477,11 +493,14 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
      blk_set_disable_request_queuing(blk, true);
      blk_set_allow_aio_context_change(blk, true);
 
-    if (!block_job_set_speed(job, speed, errp)) {
-        job_early_fail(&job->job);
-        return NULL;
+    WITH_JOB_LOCK_GUARD() {
+        if (!block_job_set_speed(job, speed, errp)) {
+            job_early_fail_locked(&job->job);
+            return NULL;
+        }
      }
 
+
      return job;
  }
 
@@ -499,7 +518,9 @@ void block_job_user_resume(Job *job)
  {
      BlockJob *bjob = container_of(job, BlockJob, job);
      assert(qemu_in_main_thread());
-    block_job_iostatus_reset(bjob);
+    WITH_JOB_LOCK_GUARD() {
+        block_job_iostatus_reset(bjob);
+    }
  }
 
  BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
@@ -532,10 +553,15 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
                                          action);
      }
      if (action == BLOCK_ERROR_ACTION_STOP) {
-        if (!job->job.user_paused) {
-            job_pause_locked(&job->job);
-            /* make the pause user visible, which will be resumed from QMP. */
-            job->job.user_paused = true;
+        WITH_JOB_LOCK_GUARD() {
+            if (!job->job.user_paused) {
+                job_pause_locked(&job->job);
+                /*
+                 * make the pause user visible, which will be
+                 * resumed from QMP.
+                 */
+                job->job.user_paused = true;
+            }
          }
          block_job_iostatus_set_err(job, error);
      }
job-qmp.c +2 -0
index f6f9840436..9fa14bf761 100644
@@ -171,6 +171,8 @@ JobInfoList *qmp_query_jobs(Error **errp)
      JobInfoList *head = NULL, **tail = &head;
      Job *job;
 
+    JOB_LOCK_GUARD();
+
      for (job = job_next_locked(NULL); job; job = job_next_locked(job)) {
          JobInfo *value;
 
job.c +93 -39
index 2ee7233763..56722a5043 100644
@@ -394,6 +394,8 @@ void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
  {
      Job *job;
 
+    JOB_LOCK_GUARD();
+
      if (job_id) {
          if (flags & JOB_INTERNAL) {
              error_setg(errp, "Cannot specify job ID for internal job");
@@ -467,7 +469,9 @@ void job_unref_locked(Job *job)
          assert(!job->txn);
 
          if (job->driver->free) {
+            job_unlock();
              job->driver->free(job);
+            job_lock();
          }
 
          QLIST_REMOVE(job, job_list);
@@ -551,11 +555,14 @@ void job_enter_cond_locked(Job *job, bool(*fn)(Job *job))
      timer_del(&job->sleep_timer);
      job->busy = true;
      real_job_unlock();
+    job_unlock();
      aio_co_enter(job->aio_context, job->co);
+    job_lock();
  }
 
  void job_enter(Job *job)
  {
+    JOB_LOCK_GUARD();
      job_enter_cond_locked(job, NULL);
  }
 
@@ -574,7 +581,9 @@ static void coroutine_fn job_do_yield(Job *job, uint64_t ns)
      job->busy = false;
      job_event_idle(job);
      real_job_unlock();
+    job_unlock();
      qemu_coroutine_yield();
+    job_lock();
 
      /* Set by job_enter_cond_locked() before re-entering the coroutine.  */
      assert(job->busy);
@@ -584,18 +593,23 @@ void coroutine_fn job_pause_point(Job *job)
  {
      assert(job && job_started(job));
 
+    job_lock();
      if (!job_should_pause(job)) {
+        job_unlock();
          return;
      }
-    if (job_is_cancelled(job)) {
+    if (job_is_cancelled_locked(job)) {
+        job_unlock();
          return;
      }
 
      if (job->driver->pause) {
+        job_unlock();
          job->driver->pause(job);
+        job_lock();
      }
 
-    if (job_should_pause(job) && !job_is_cancelled(job)) {
+    if (job_should_pause(job) && !job_is_cancelled_locked(job)) {
          JobStatus status = job->status;
          job_state_transition(job, status == JOB_STATUS_READY
                                    ? JOB_STATUS_STANDBY
@@ -605,6 +619,7 @@ void coroutine_fn job_pause_point(Job *job)
          job->paused = false;
          job_state_transition(job, status);
      }
+    job_unlock();
 
      if (job->driver->resume) {
          job->driver->resume(job);
@@ -613,15 +628,17 @@ void coroutine_fn job_pause_point(Job *job)
 
  void job_yield(Job *job)
  {
-    assert(job->busy);
+    WITH_JOB_LOCK_GUARD() {
+        assert(job->busy);
 
-    /* Check cancellation *before* setting busy = false, too!  */
-    if (job_is_cancelled(job)) {
-        return;
-    }
+        /* Check cancellation *before* setting busy = false, too!  */
+        if (job_is_cancelled_locked(job)) {
+            return;
+        }
 
-    if (!job_should_pause(job)) {
-        job_do_yield(job, -1);
+        if (!job_should_pause(job)) {
+            job_do_yield(job, -1);
+        }
      }
 
      job_pause_point(job);
@@ -629,21 +646,23 @@ void job_yield(Job *job)
 
  void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
  {
-    assert(job->busy);
+    WITH_JOB_LOCK_GUARD() {
+        assert(job->busy);
 
-    /* Check cancellation *before* setting busy = false, too!  */
-    if (job_is_cancelled(job)) {
-        return;
-    }
+        /* Check cancellation *before* setting busy = false, too!  */
+        if (job_is_cancelled_locked(job)) {
+            return;
+        }
 
-    if (!job_should_pause(job)) {
-        job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
+        if (!job_should_pause(job)) {
+            job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
+        }
      }
 
      job_pause_point(job);
  }
 
-/* Assumes the block_job_mutex is held */
+/* Assumes the job_mutex is held */
  static bool job_timer_not_pending(Job *job)
  {
      return !timer_pending(&job->sleep_timer);
@@ -653,7 +672,7 @@ void job_pause_locked(Job *job)
  {
      job->pause_count++;
      if (!job->paused) {
-        job_enter(job);
+        job_enter_cond_locked(job, NULL);
      }
  }
 
@@ -699,7 +718,9 @@ void job_user_resume_locked(Job *job, Error **errp)
          return;
      }
      if (job->driver->user_resume) {
+        job_unlock();
          job->driver->user_resume(job);
+        job_lock();
      }
      job->user_paused = false;
      job_resume_locked(job);
@@ -753,7 +774,7 @@ static void job_conclude(Job *job)
 
  static void job_update_rc(Job *job)
  {
-    if (!job->ret && job_is_cancelled(job)) {
+    if (!job->ret && job_is_cancelled_locked(job)) {
          job->ret = -ECANCELED;
      }
      if (job->ret) {
@@ -769,7 +790,9 @@ static void job_commit(Job *job)
      assert(!job->ret);
      assert(qemu_in_main_thread());
      if (job->driver->commit) {
+        job_unlock();
          job->driver->commit(job);
+        job_lock();
      }
  }
 
@@ -778,7 +801,9 @@ static void job_abort(Job *job)
      assert(job->ret);
      assert(qemu_in_main_thread());
      if (job->driver->abort) {
+        job_unlock();
          job->driver->abort(job);
+        job_lock();
      }
  }
 
@@ -786,12 +811,15 @@ static void job_clean(Job *job)
  {
      assert(qemu_in_main_thread());
      if (job->driver->clean) {
+        job_unlock();
          job->driver->clean(job);
+        job_lock();
      }
  }
 
  static int job_finalize_single(Job *job)
  {
+    int job_ret;
      AioContext *ctx = job->aio_context;
 
      assert(job_is_completed_locked(job));
@@ -811,12 +839,15 @@ static int job_finalize_single(Job *job)
      aio_context_release(ctx);
 
      if (job->cb) {
-        job->cb(job->opaque, job->ret);
+        job_ret = job->ret;
+        job_unlock();
+        job->cb(job->opaque, job_ret);
+        job_lock();
      }
 
      /* Emit events only if we actually started */
      if (job_started(job)) {
-        if (job_is_cancelled(job)) {
+        if (job_is_cancelled_locked(job)) {
              job_event_cancelled(job);
          } else {
              job_event_completed(job);
@@ -832,7 +863,9 @@ static void job_cancel_async(Job *job, bool force)
  {
      assert(qemu_in_main_thread());
      if (job->driver->cancel) {
+        job_unlock();
          force = job->driver->cancel(job, force);
+        job_lock();
      } else {
          /* No .cancel() means the job will behave as if force-cancelled */
          force = true;
@@ -841,7 +874,9 @@ static void job_cancel_async(Job *job, bool force)
      if (job->user_paused) {
          /* Do not call job_enter here, the caller will handle it.  */
          if (job->driver->user_resume) {
+            job_unlock();
              job->driver->user_resume(job);
+            job_lock();
          }
          job->user_paused = false;
          assert(job->pause_count > 0);
@@ -911,7 +946,7 @@ static void job_completed_txn_abort(Job *job)
          ctx = other_job->aio_context;
          aio_context_acquire(ctx);
          if (!job_is_completed_locked(other_job)) {
-            assert(job_cancel_requested(other_job));
+            assert(job_cancel_requested_locked(other_job));
              job_finish_sync_locked(other_job, NULL, NULL);
          }
          job_finalize_single(other_job);
@@ -930,13 +965,17 @@ static void job_completed_txn_abort(Job *job)
 
  static int job_prepare(Job *job)
  {
+    int ret;
      AioContext *ctx = job->aio_context;
      assert(qemu_in_main_thread());
 
      if (job->ret == 0 && job->driver->prepare) {
+        job_unlock();
          aio_context_acquire(ctx);
-        job->ret = job->driver->prepare(job);
+        ret = job->driver->prepare(job);
          aio_context_release(ctx);
+        job_lock();
+        job->ret = ret;
          job_update_rc(job);
      }
 
@@ -982,6 +1021,7 @@ static int job_transition_to_pending(Job *job)
 
  void job_transition_to_ready(Job *job)
  {
+    JOB_LOCK_GUARD();
      job_state_transition(job, JOB_STATUS_READY);
      job_event_ready(job);
  }
@@ -1031,6 +1071,7 @@ static void job_exit(void *opaque)
      Job *job = (Job *)opaque;
      AioContext *ctx;
 
+    JOB_LOCK_GUARD();
      job_ref_locked(job);
      aio_context_acquire(job->aio_context);
 
@@ -1061,13 +1102,17 @@ static void job_exit(void *opaque)
  static void coroutine_fn job_co_entry(void *opaque)
  {
      Job *job = opaque;
+    int ret;
 
      assert(job->aio_context == qemu_get_current_aio_context());
      assert(job && job->driver && job->driver->run);
      job_pause_point(job);
-    job->ret = job->driver->run(job, &job->err);
-    job->deferred_to_main_loop = true;
-    job->busy = true;
+    ret = job->driver->run(job, &job->err);
+    WITH_JOB_LOCK_GUARD() {
+        job->ret = ret;
+        job->deferred_to_main_loop = true;
+        job->busy = true;
+    }
      aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job);
  }
 
@@ -1083,16 +1128,20 @@ static int job_pre_run(Job *job)
 
  void job_start(Job *job)
  {
-    assert(job && !job_started(job) && job->paused &&
-           job->driver && job->driver->run);
-    job->co = qemu_coroutine_create(job_co_entry, job);
+    WITH_JOB_LOCK_GUARD() {
+        assert(job && !job_started(job) && job->paused &&
+            job->driver && job->driver->run);
+        job->co = qemu_coroutine_create(job_co_entry, job);
+    }
      if (job_pre_run(job)) {
          return;
      }
-    job->pause_count--;
-    job->busy = true;
-    job->paused = false;
-    job_state_transition(job, JOB_STATUS_RUNNING);
+    WITH_JOB_LOCK_GUARD() {
+        job->pause_count--;
+        job->busy = true;
+        job->paused = false;
+        job_state_transition(job, JOB_STATUS_RUNNING);
+    }
      aio_co_enter(job->aio_context, job->co);
  }
 
@@ -1116,11 +1165,11 @@ void job_cancel_locked(Job *job, bool force)
           * choose to call job_is_cancelled() to show that we invoke
           * job_completed_txn_abort() only for force-cancelled jobs.)
           */
-        if (job_is_cancelled(job)) {
+        if (job_is_cancelled_locked(job)) {
              job_completed_txn_abort(job);
          }
      } else {
-        job_enter(job);
+        job_enter_cond_locked(job, NULL);
      }
  }
 
@@ -1164,6 +1213,7 @@ void job_cancel_sync_all(void)
      Job *job;
      AioContext *aio_context;
 
+    JOB_LOCK_GUARD();
      while ((job = job_next_locked(NULL))) {
          aio_context = job->aio_context;
          aio_context_acquire(aio_context);
@@ -1185,13 +1235,15 @@ void job_complete_locked(Job *job, Error **errp)
      if (job_apply_verb_locked(job, JOB_VERB_COMPLETE, errp)) {
          return;
      }
-    if (job_cancel_requested(job) || !job->driver->complete) {
+    if (job_cancel_requested_locked(job) || !job->driver->complete) {
          error_setg(errp, "The active block job '%s' cannot be completed",
                     job->id);
          return;
      }
 
+    job_unlock();
      job->driver->complete(job, errp);
+    job_lock();
  }
 
  int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),
@@ -1211,10 +1263,12 @@ int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),
          return -EBUSY;
      }
 
-    AIO_WAIT_WHILE(job->aio_context,
-                   (job_enter(job), !job_is_completed_locked(job)));
+    job_unlock();
+    AIO_WAIT_WHILE(job->aio_context, (job_enter(job), !job_is_completed(job)));
+    job_lock();
 
-    ret = (job_is_cancelled(job) && job->ret == 0) ? -ECANCELED : job->ret;
+    ret = (job_is_cancelled_locked(job) && job->ret == 0) ?
+           -ECANCELED : job->ret;
      job_unref_locked(job);
      return ret;
  }
monitor/qmp-cmds.c +4 -2
index 343353e27a..2f11d086a6 100644
@@ -133,8 +133,10 @@ void qmp_cont(Error **errp)
          blk_iostatus_reset(blk);
      }
 
-    for (job = block_job_next(NULL); job; job = block_job_next(job)) {
-        block_job_iostatus_reset(job);
+    WITH_JOB_LOCK_GUARD() {
+        for (job = block_job_next(NULL); job; job = block_job_next(job)) {
+            block_job_iostatus_reset(job);
+        }
      }
 
      /* Continuing after completed migration. Images have been inactivated to
qemu-img.c +21 -16
index 09f3b11eab..95e2e33e61 100644
@@ -906,25 +906,30 @@ static void run_block_job(BlockJob *job, Error **errp)
      int ret = 0;
 
      aio_context_acquire(aio_context);
-    job_ref_locked(&job->job);
-    do {
-        float progress = 0.0f;
-        aio_poll(aio_context, true);
+    WITH_JOB_LOCK_GUARD() {
+        job_ref_locked(&job->job);
+        do {
+            float progress = 0.0f;
+            job_unlock();
+            aio_poll(aio_context, true);
+
+            progress_get_snapshot(&job->job.progress, &progress_current,
+                                &progress_total);
+            if (progress_total) {
+                progress = (float)progress_current / progress_total * 100.f;
+            }
+            qemu_progress_print(progress, 0);
+            job_lock();
+        } while (!job_is_ready_locked(&job->job) &&
+                !job_is_completed_locked(&job->job));
 
-        progress_get_snapshot(&job->job.progress, &progress_current,
-                              &progress_total);
-        if (progress_total) {
-            progress = (float)progress_current / progress_total * 100.f;
+        if (!job_is_completed_locked(&job->job)) {
+            ret = job_complete_sync_locked(&job->job, errp);
+        } else {
+            ret = job->job.ret;
          }
-        qemu_progress_print(progress, 0);
-    } while (!job_is_ready(&job->job) && !job_is_completed_locked(&job->job));
-
-    if (!job_is_completed_locked(&job->job)) {
-        ret = job_complete_sync_locked(&job->job, errp);
-    } else {
-        ret = job->job.ret;
+        job_unref_locked(&job->job);
      }
-    job_unref_locked(&job->job);
      aio_context_release(aio_context);
 
      /* publish completion progress only when success */

qemu-img.c static int img_commit(int argc, char **argv) · line 1090

Quoted 10 lines
qemu-img.c +3 -1
@@ -1077,7 +1082,9 @@ static int img_commit(int argc, char **argv)
          bdrv_ref(bs);
      }
 
-    job = block_job_get("commit");
+    WITH_JOB_LOCK_GUARD() {
+        job = block_job_get("commit");
+    }
      assert(job);
      run_block_job(job, &local_err);
      if (local_err) {

If meaningful, I'd either do this much earlier, or do the _locked rename much later. Having such a large part of the series where _locked functions are *not* locked is confusing.

Paolo

#23 lore raw

PATCH v3 11/16 jobs: document all static functions and add _locked() suffix

In reply to Emanuele Giuseppe Esposito

Now that we added the job_lock/unlock pairs, we can also rename all static functions in job.c that are called with the job mutex held as _locked(), and add a little comment on top.

No functional change intended.

---
 blockjob.c |   8 ++
 job.c      | 243 +++++++++++++++++++++++++++++++----------------------
 2 files changed, 149 insertions(+), 102 deletions(-)
blockjob.c +8 -0
index e00c8d31d5..cf1f49f6c2 100644
@@ -242,6 +242,7 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
     return 0;
 }
 
+/* Called with job_mutex lock held. */
 static void block_job_on_idle(Notifier *n, void *opaque)
 {
     /*
@@ -269,6 +270,7 @@ static bool job_timer_pending(Job *job)
     return timer_pending(&job->sleep_timer);
 }
 
+/* Called with job_mutex held. May temporarly release the lock. */
 bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
 {
     const BlockJobDriver *drv = block_job_driver(job);
@@ -310,6 +312,7 @@ int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n)
     return ratelimit_calculate_delay(&job->limit, n);
 }
 
+/* Called with job_mutex lock held. */
 BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
 {
     BlockJobInfo *info;
@@ -355,6 +358,7 @@ static void block_job_iostatus_set_err(BlockJob *job, int error)
     }
 }
 
+/* Called with job_mutex lock held. */
 static void block_job_event_cancelled(Notifier *n, void *opaque)
 {
     BlockJob *job = opaque;
@@ -374,6 +378,7 @@ static void block_job_event_cancelled(Notifier *n, void *opaque)
                                         job->speed);
 }
 
+/* Called with job_mutex lock held. */
 static void block_job_event_completed(Notifier *n, void *opaque)
 {
     BlockJob *job = opaque;
@@ -400,6 +405,7 @@ static void block_job_event_completed(Notifier *n, void *opaque)
                                         msg);
 }
 
+/* Called with job_mutex lock held. */
 static void block_job_event_pending(Notifier *n, void *opaque)
 {
     BlockJob *job = opaque;
@@ -412,6 +418,7 @@ static void block_job_event_pending(Notifier *n, void *opaque)
                                       job->job.id);
 }
 
+/* Called with job_mutex lock held. */
 static void block_job_event_ready(Notifier *n, void *opaque)
 {
     BlockJob *job = opaque;
@@ -504,6 +511,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
     return job;
 }
 
+/* Called with job_mutex lock held. */
 void block_job_iostatus_reset(BlockJob *job)
 {
     assert(qemu_in_main_thread());
job.c +141 -102

620 lines not on this page · raw message

Signature · 1 line
2.31.1
#24 lore raw

PATCH v3 12/16 jobs: use job locks and helpers also in the unit tests

In reply to Emanuele Giuseppe Esposito

Add missing job synchronization in the unit tests, with both explicit locks and helpers.

Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*.

---
 tests/unit/test-bdrv-drain.c     | 40 +++++++++++-----------
 tests/unit/test-block-iothread.c |  4 +++
 tests/unit/test-blockjob-txn.c   | 10 ++++++
 tests/unit/test-blockjob.c       | 57 +++++++++++++++++++++-----------
 4 files changed, 72 insertions(+), 39 deletions(-)
tests/unit/test-bdrv-drain.c +21 -19
index 3f344a0d0d..c03560e63d 100644
@@ -941,61 +941,63 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type,
         }
     }
 
-    g_assert_cmpint(job->job.pause_count, ==, 0);
-    g_assert_false(job->job.paused);
+    g_assert_cmpint(job_get_pause_count(&job->job), ==, 0);
+    g_assert_false(job_get_paused(&job->job));
     g_assert_true(tjob->running);
-    g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
+    g_assert_true(job_get_busy(&job->job)); /* We're in qemu_co_sleep_ns() */
 
     do_drain_begin_unlocked(drain_type, drain_bs);
 
     if (drain_type == BDRV_DRAIN_ALL) {
         /* bdrv_drain_all() drains both src and target */
-        g_assert_cmpint(job->job.pause_count, ==, 2);
+        g_assert_cmpint(job_get_pause_count(&job->job), ==, 2);
     } else {
-        g_assert_cmpint(job->job.pause_count, ==, 1);
+        g_assert_cmpint(job_get_pause_count(&job->job), ==, 1);
     }
-    g_assert_true(job->job.paused);
-    g_assert_false(job->job.busy); /* The job is paused */
+    g_assert_true(job_get_paused(&job->job));
+    g_assert_false(job_get_busy(&job->job)); /* The job is paused */
 
     do_drain_end_unlocked(drain_type, drain_bs);
 
     if (use_iothread) {
         /* paused is reset in the I/O thread, wait for it */
-        while (job->job.paused) {
+        while (job_get_paused(&job->job)) {
             aio_poll(qemu_get_aio_context(), false);
         }
     }
 
-    g_assert_cmpint(job->job.pause_count, ==, 0);
-    g_assert_false(job->job.paused);
-    g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
+    g_assert_cmpint(job_get_pause_count(&job->job), ==, 0);
+    g_assert_false(job_get_paused(&job->job));
+    g_assert_true(job_get_busy(&job->job)); /* We're in qemu_co_sleep_ns() */
 
     do_drain_begin_unlocked(drain_type, target);
 
     if (drain_type == BDRV_DRAIN_ALL) {
         /* bdrv_drain_all() drains both src and target */
-        g_assert_cmpint(job->job.pause_count, ==, 2);
+        g_assert_cmpint(job_get_pause_count(&job->job), ==, 2);
     } else {
-        g_assert_cmpint(job->job.pause_count, ==, 1);
+        g_assert_cmpint(job_get_pause_count(&job->job), ==, 1);
     }
-    g_assert_true(job->job.paused);
-    g_assert_false(job->job.busy); /* The job is paused */
+    g_assert_true(job_get_paused(&job->job));
+    g_assert_false(job_get_busy(&job->job)); /* The job is paused */
 
     do_drain_end_unlocked(drain_type, target);
 
     if (use_iothread) {
         /* paused is reset in the I/O thread, wait for it */
-        while (job->job.paused) {
+        while (job_get_paused(&job->job)) {
             aio_poll(qemu_get_aio_context(), false);
         }
     }
 
-    g_assert_cmpint(job->job.pause_count, ==, 0);
-    g_assert_false(job->job.paused);
-    g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
+    g_assert_cmpint(job_get_pause_count(&job->job), ==, 0);
+    g_assert_false(job_get_paused(&job->job));
+    g_assert_true(job_get_busy(&job->job)); /* We're in qemu_co_sleep_ns() */
 
     aio_context_acquire(ctx);
+    job_lock();
     ret = job_complete_sync_locked(&job->job, &error_abort);
+    job_unlock();
     g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
 
     if (use_iothread) {
tests/unit/test-block-iothread.c +4 -0
index 7e1b521d61..b9309beec2 100644
@@ -456,7 +456,9 @@ static void test_attach_blockjob(void)
     }
 
     aio_context_acquire(ctx);
+    job_lock();
     job_complete_sync_locked(&tjob->common.job, &error_abort);
+    job_unlock();
     blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
     aio_context_release(ctx);
 
@@ -630,7 +632,9 @@ static void test_propagate_mirror(void)
                  BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
                  false, "filter_node", MIRROR_COPY_MODE_BACKGROUND,
                  &error_abort);
+    job_lock();
     job = job_get_locked("job0");
+    job_unlock();
     filter = bdrv_find_node("filter_node");
 
     /* Change the AioContext of src */
tests/unit/test-blockjob-txn.c +10 -0
index 5396fcef10..bd69076300 100644
@@ -124,16 +124,20 @@ static void test_single_job(int expected)
     job = test_block_job_start(1, true, expected, &result, txn);
     job_start(&job->job);
 
+    job_lock();
     if (expected == -ECANCELED) {
         job_cancel_locked(&job->job, false);
     }
+    job_unlock();
 
     while (result == -EINPROGRESS) {
         aio_poll(qemu_get_aio_context(), true);
     }
     g_assert_cmpint(result, ==, expected);
 
+    job_lock();
     job_txn_unref_locked(txn);
+    job_unlock();
 }
 
 static void test_single_job_success(void)
@@ -168,6 +172,7 @@ static void test_pair_jobs(int expected1, int expected2)
     /* Release our reference now to trigger as many nice
      * use-after-free bugs as possible.
      */
+    job_lock();
     job_txn_unref_locked(txn);
 
     if (expected1 == -ECANCELED) {
@@ -176,6 +181,7 @@ static void test_pair_jobs(int expected1, int expected2)
     if (expected2 == -ECANCELED) {
         job_cancel_locked(&job2->job, false);
     }
+    job_unlock();
 
     while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
         aio_poll(qemu_get_aio_context(), true);
@@ -227,7 +233,9 @@ static void test_pair_jobs_fail_cancel_race(void)
     job_start(&job1->job);
     job_start(&job2->job);
 
+    job_lock();
     job_cancel_locked(&job1->job, false);
+    job_unlock();
 
     /* Now make job2 finish before the main loop kicks jobs.  This simulates
      * the race between a pending kick and another job completing.
@@ -242,7 +250,9 @@ static void test_pair_jobs_fail_cancel_race(void)
     g_assert_cmpint(result1, ==, -ECANCELED);
     g_assert_cmpint(result2, ==, -ECANCELED);
 
+    job_lock();
     job_txn_unref_locked(txn);
+    job_unlock();
 }
 
 int main(int argc, char **argv)
tests/unit/test-blockjob.c +37 -20

174 lines not on this page · raw message

Signature · 1 line
2.31.1
#25 lore raw

PATCH v3 13/16 jobs: add job lock in find_* functions

In reply to Emanuele Giuseppe Esposito

Both blockdev.c and job-qmp.c have TOC/TOU conditions, because they first search for the job and then perform an action on it. Therefore, we need to do the search + action under the same job mutex critical section.

Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*.

---
 blockdev.c | 14 +++++++++++++-
 job-qmp.c  | 13 ++++++++++++-
 2 files changed, 25 insertions(+), 2 deletions(-)
blockdev.c +13 -1
index 099d57e0d2..1fbd9b9e04 100644
@@ -3305,7 +3305,10 @@ out:
     aio_context_release(aio_context);
 }
 
-/* Get a block job using its ID and acquire its AioContext */
+/*
+ * Get a block job using its ID and acquire its AioContext.
+ * Returns with job_lock held on success.
+ */
 static BlockJob *find_block_job(const char *id, AioContext **aio_context,
                                 Error **errp)
 {
@@ -3314,12 +3317,14 @@ static BlockJob *find_block_job(const char *id, AioContext **aio_context,
     assert(id != NULL);
 
     *aio_context = NULL;
+    job_lock();
 
     job = block_job_get(id);
 
     if (!job) {
         error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
                   "Block job '%s' not found", id);
+        job_unlock();
         return NULL;
     }
 
@@ -3340,6 +3345,7 @@ void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
 
     block_job_set_speed(job, speed, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_block_job_cancel(const char *device,
@@ -3366,6 +3372,7 @@ void qmp_block_job_cancel(const char *device,
     job_user_cancel_locked(&job->job, force, errp);
 out:
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_block_job_pause(const char *device, Error **errp)
@@ -3380,6 +3387,7 @@ void qmp_block_job_pause(const char *device, Error **errp)
     trace_qmp_block_job_pause(job);
     job_user_pause_locked(&job->job, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_block_job_resume(const char *device, Error **errp)
@@ -3394,6 +3402,7 @@ void qmp_block_job_resume(const char *device, Error **errp)
     trace_qmp_block_job_resume(job);
     job_user_resume_locked(&job->job, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_block_job_complete(const char *device, Error **errp)
@@ -3408,6 +3417,7 @@ void qmp_block_job_complete(const char *device, Error **errp)
     trace_qmp_block_job_complete(job);
     job_complete_locked(&job->job, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_block_job_finalize(const char *id, Error **errp)
@@ -3431,6 +3441,7 @@ void qmp_block_job_finalize(const char *id, Error **errp)
     aio_context = blk_get_aio_context(job->blk);
     job_unref_locked(&job->job);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_block_job_dismiss(const char *id, Error **errp)
@@ -3447,6 +3458,7 @@ void qmp_block_job_dismiss(const char *id, Error **errp)
     job = &bjob->job;
     job_dismiss_locked(&job, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_change_backing_file(const char *device,
job-qmp.c +12 -1
index 9fa14bf761..615e056fc4 100644
@@ -29,16 +29,21 @@
 #include "qapi/error.h"
 #include "trace/trace-root.h"
 
-/* Get a job using its ID and acquire its AioContext */
+/*
+ * Get a block job using its ID and acquire its AioContext.
+ * Returns with job_lock held on success.
+ */
 static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
 {
     Job *job;
 
     *aio_context = NULL;
+    job_lock();
 
     job = job_get_locked(id);
     if (!job) {
         error_setg(errp, "Job not found");
+        job_unlock();
         return NULL;
     }
 
@@ -60,6 +65,7 @@ void qmp_job_cancel(const char *id, Error **errp)
     trace_qmp_job_cancel(job);
     job_user_cancel_locked(job, true, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_job_pause(const char *id, Error **errp)
@@ -74,6 +80,7 @@ void qmp_job_pause(const char *id, Error **errp)
     trace_qmp_job_pause(job);
     job_user_pause_locked(job, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_job_resume(const char *id, Error **errp)
@@ -88,6 +95,7 @@ void qmp_job_resume(const char *id, Error **errp)
     trace_qmp_job_resume(job);
     job_user_resume_locked(job, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_job_complete(const char *id, Error **errp)
@@ -102,6 +110,7 @@ void qmp_job_complete(const char *id, Error **errp)
     trace_qmp_job_complete(job);
     job_complete_locked(job, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_job_finalize(const char *id, Error **errp)
@@ -125,6 +134,7 @@ void qmp_job_finalize(const char *id, Error **errp)
     aio_context = job->aio_context;
     job_unref_locked(job);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 void qmp_job_dismiss(const char *id, Error **errp)
@@ -139,6 +149,7 @@ void qmp_job_dismiss(const char *id, Error **errp)
     trace_qmp_job_dismiss(job);
     job_dismiss_locked(&job, errp);
     aio_context_release(aio_context);
+    job_unlock();
 }
 
 static JobInfo *job_query_single(Job *job, Error **errp)
Signature · 1 line
2.31.1
#26 lore raw

PATCH v3 14/16 job.c: use job_get_aio_context()

In reply to Emanuele Giuseppe Esposito

If the job->aio_context is accessed under job_mutex, leave as it is. Otherwise use job_get_aio_context().

---
 block/commit.c                   |  4 ++--
 block/mirror.c                   |  2 +-
 block/replication.c              |  2 +-
 blockjob.c                       | 18 +++++++++++-------
 job.c                            |  8 ++++----
 tests/unit/test-block-iothread.c |  6 +++---
 6 files changed, 22 insertions(+), 18 deletions(-)
block/commit.c +2 -2
index f639eb49c5..961b57edf0 100644
@@ -369,7 +369,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,
         goto fail;
     }
 
-    s->base = blk_new(s->common.job.aio_context,
+    s->base = blk_new(job_get_aio_context(&s->common.job),
                       base_perms,
                       BLK_PERM_CONSISTENT_READ
                       | BLK_PERM_GRAPH_MOD
@@ -382,7 +382,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,
     s->base_bs = base;
 
     /* Required permissions are already taken with block_job_add_bdrv() */
-    s->top = blk_new(s->common.job.aio_context, 0, BLK_PERM_ALL);
+    s->top = blk_new(job_get_aio_context(&s->common.job), 0, BLK_PERM_ALL);
     ret = blk_insert_bs(s->top, top, errp);
     if (ret < 0) {
         goto fail;
block/mirror.c +1 -1
index 41450df55c..72b4367b4e 100644
@@ -1743,7 +1743,7 @@ static BlockJob *mirror_start_job(
         target_perms |= BLK_PERM_GRAPH_MOD;
     }
 
-    s->target = blk_new(s->common.job.aio_context,
+    s->target = blk_new(job_get_aio_context(&s->common.job),
                         target_perms, target_shared_perms);
     ret = blk_insert_bs(s->target, target, errp);
     if (ret < 0) {
block/replication.c +1 -1
index 50ea778937..68018948b9 100644
@@ -148,8 +148,8 @@ static void replication_close(BlockDriverState *bs)
     }
     if (s->stage == BLOCK_REPLICATION_FAILOVER) {
         commit_job = &s->commit_job->job;
-        assert(commit_job->aio_context == qemu_get_current_aio_context());
         WITH_JOB_LOCK_GUARD() {
+            assert(commit_job->aio_context == qemu_get_current_aio_context());
             job_cancel_sync_locked(commit_job, false);
         }
     }
blockjob.c +11 -7
index cf1f49f6c2..468ba735c5 100644
@@ -155,14 +155,16 @@ static void child_job_set_aio_ctx(BdrvChild *c, AioContext *ctx,
         bdrv_set_aio_context_ignore(sibling->bs, ctx, ignore);
     }
 
-    job->job.aio_context = ctx;
+    WITH_JOB_LOCK_GUARD() {
+        job->job.aio_context = ctx;
+    }
 }
 
 static AioContext *child_job_get_parent_aio_context(BdrvChild *c)
 {
     BlockJob *job = c->opaque;
 
-    return job->job.aio_context;
+    return job_get_aio_context(&job->job);
 }
 
 static const BdrvChildClass child_job = {
@@ -218,19 +220,21 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
 {
     BdrvChild *c;
     bool need_context_ops;
+    AioContext *job_aiocontext;
     assert(qemu_in_main_thread());
 
     bdrv_ref(bs);
 
-    need_context_ops = bdrv_get_aio_context(bs) != job->job.aio_context;
+    job_aiocontext = job_get_aio_context(&job->job);
+    need_context_ops = bdrv_get_aio_context(bs) != job_aiocontext;
 
-    if (need_context_ops && job->job.aio_context != qemu_get_aio_context()) {
-        aio_context_release(job->job.aio_context);
+    if (need_context_ops && job_aiocontext != qemu_get_aio_context()) {
+        aio_context_release(job_aiocontext);
     }
     c = bdrv_root_attach_child(bs, name, &child_job, 0, perm, shared_perm, job,
                                errp);
-    if (need_context_ops && job->job.aio_context != qemu_get_aio_context()) {
-        aio_context_acquire(job->job.aio_context);
+    if (need_context_ops && job_aiocontext != qemu_get_aio_context()) {
+        aio_context_acquire(job_aiocontext);
     }
     if (c == NULL) {
         return -EPERM;
job.c +4 -4
index f16a4ef542..8a5b710d9b 100644
@@ -566,7 +566,7 @@ void job_enter_cond_locked(Job *job, bool(*fn)(Job *job))
     job->busy = true;
     real_job_unlock();
     job_unlock();
-    aio_co_enter(job->aio_context, job->co);
+    aio_co_enter(job_get_aio_context(job), job->co);
     job_lock();
 }
 
@@ -1138,7 +1138,6 @@ static void coroutine_fn job_co_entry(void *opaque)
     Job *job = opaque;
     int ret;
 
-    assert(job->aio_context == qemu_get_current_aio_context());
     assert(job && job->driver && job->driver->run);
     job_pause_point(job);
     ret = job->driver->run(job, &job->err);
@@ -1177,7 +1176,7 @@ void job_start(Job *job)
         job->paused = false;
         job_state_transition_locked(job, JOB_STATUS_RUNNING);
     }
-    aio_co_enter(job->aio_context, job->co);
+    aio_co_enter(job_get_aio_context(job), job->co);
 }
 
 void job_cancel_locked(Job *job, bool force)
@@ -1303,7 +1302,8 @@ int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),
     }
 
     job_unlock();
-    AIO_WAIT_WHILE(job->aio_context, (job_enter(job), !job_is_completed(job)));
+    AIO_WAIT_WHILE(job_get_aio_context(job),
+                   (job_enter(job), !job_is_completed(job)));
     job_lock();
 
     ret = (job_is_cancelled_locked(job) && job->ret == 0) ?
tests/unit/test-block-iothread.c +3 -3
index b9309beec2..addcb5846b 100644
@@ -379,7 +379,7 @@ static int coroutine_fn test_job_run(Job *job, Error **errp)
     job_transition_to_ready(&s->common.job);
     while (!s->should_complete) {
         s->n++;
-        g_assert(qemu_get_current_aio_context() == job->aio_context);
+        g_assert(qemu_get_current_aio_context() == job_get_aio_context(job));
 
         /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
          * emulate some actual activity (probably some I/O) here so that the
@@ -390,7 +390,7 @@ static int coroutine_fn test_job_run(Job *job, Error **errp)
         job_pause_point(&s->common.job);
     }
 
-    g_assert(qemu_get_current_aio_context() == job->aio_context);
+    g_assert(qemu_get_current_aio_context() == job_get_aio_context(job));
     return 0;
 }
 
@@ -642,7 +642,7 @@ static void test_propagate_mirror(void)
     g_assert(bdrv_get_aio_context(src) == ctx);
     g_assert(bdrv_get_aio_context(target) == ctx);
     g_assert(bdrv_get_aio_context(filter) == ctx);
-    g_assert(job->aio_context == ctx);
+    g_assert(job_get_aio_context(job) == ctx);
 
     /* Change the AioContext of target */
     aio_context_acquire(ctx);
Signature · 1 line
2.31.1
#27 lore raw

Re: [PATCH v3 14/16] job.c: use job_get_aio_context()

Getters such as job_get_aio_context are often wrong because the AioContext can change immediately after returning.

So, I wonder if job.aio_context should be protected with a kind of "fake rwlock": read under BQL or job_lock, write under BQL+job_lock. For this to work, you can add an assertion for qemu_in_main_thread() to child_job_set_aio_ctx, or even better have the assertion in a wrapper API job_set_aio_context_locked().

And then, we can remove job_get_aio_context().

Let's look at all cases individually:

block/commit.c void commit_start(const char *job_id, BlockDriverState *bs, · line 375

Quoted 13 lines · Emanuele Giuseppe Esposito
block/commit.c +1 -1
index f639eb49c5..961b57edf0 100644
@@ -369,7 +369,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,
          goto fail;
      }
 
-    s->base = blk_new(s->common.job.aio_context,
+    s->base = blk_new(job_get_aio_context(&s->common.job),
                        base_perms,
                        BLK_PERM_CONSISTENT_READ
                        | BLK_PERM_GRAPH_MOD

Here the AioContext is the one of bs. It cannot change because we're under BQL. Replace with bdrv_get_aio_context.

block/commit.c void commit_start(const char *job_id, BlockDriverState *bs, · line 388

Quoted 9 lines
block/commit.c +1 -1
@@ -382,7 +382,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,
      s->base_bs = base;
 
      /* Required permissions are already taken with block_job_add_bdrv() */
-    s->top = blk_new(s->common.job.aio_context, 0, BLK_PERM_ALL);
+    s->top = blk_new(job_get_aio_context(&s->common.job), 0, BLK_PERM_ALL);
      ret = blk_insert_bs(s->top, top, errp);
      if (ret < 0) {
          goto fail;

Same.

block/mirror.c static BlockJob *mirror_start_job( · line 1749

Quoted 13 lines
block/mirror.c +1 -1
index 41450df55c..72b4367b4e 100644
@@ -1743,7 +1743,7 @@ static BlockJob *mirror_start_job(
          target_perms |= BLK_PERM_GRAPH_MOD;
      }
 
-    s->target = blk_new(s->common.job.aio_context,
+    s->target = blk_new(job_get_aio_context(&s->common.job),
                          target_perms, target_shared_perms);
      ret = blk_insert_bs(s->target, target, errp);
      if (ret < 0) {

Same.

Quoted 5 lines
block/replication.c +0 -0
index 50ea778937..68018948b9 100644

block/replication.c static void replication_close(BlockDriverState *bs) · line 155

Quoted 9 lines
block/replication.c +1 -1
@@ -148,8 +148,8 @@ static void replication_close(BlockDriverState *bs)
      }
      if (s->stage == BLOCK_REPLICATION_FAILOVER) {
          commit_job = &s->commit_job->job;
-        assert(commit_job->aio_context == qemu_get_current_aio_context());
          WITH_JOB_LOCK_GUARD() {
+            assert(commit_job->aio_context == qemu_get_current_aio_context());
              job_cancel_sync_locked(commit_job, false);
          }
      }

Ok.

Quoted 13 lines
blockjob.c +3 -1
index cf1f49f6c2..468ba735c5 100644
@@ -155,14 +155,16 @@ static void child_job_set_aio_ctx(BdrvChild *c, AioContext *ctx,
          bdrv_set_aio_context_ignore(sibling->bs, ctx, ignore);
      }
 
-    job->job.aio_context = ctx;
+    WITH_JOB_LOCK_GUARD() {
+        job->job.aio_context = ctx;
+    }
  }

blockjob.c static void child_job_set_aio_ctx(BdrvChild *c, AioContext *ctx, · line 170

Quoted 10 lines
blockjob.c +1 -1
 
  static AioContext *child_job_get_parent_aio_context(BdrvChild *c)
  {
      BlockJob *job = c->opaque;
 
-    return job->job.aio_context;
+    return job_get_aio_context(&job->job);
  }
 
  static const BdrvChildClass child_job = {

Both called with BQL held, I think.

Quoted 17 lines
blockjob.c +5 -3
@@ -218,19 +220,21 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
  {
      BdrvChild *c;
      bool need_context_ops;
+    AioContext *job_aiocontext;
      assert(qemu_in_main_thread());
 
      bdrv_ref(bs);
 
-    need_context_ops = bdrv_get_aio_context(bs) != job->job.aio_context;
+    job_aiocontext = job_get_aio_context(&job->job);
+    need_context_ops = bdrv_get_aio_context(bs) != job_aiocontext;
 
-    if (need_context_ops && job->job.aio_context != qemu_get_aio_context()) {
-        aio_context_release(job->job.aio_context);
+    if (need_context_ops && job_aiocontext != qemu_get_aio_context()) {
+        aio_context_release(job_aiocontext);

blockjob.c int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, · line 240

Quoted 10 lines
blockjob.c +2 -2
      }
      c = bdrv_root_attach_child(bs, name, &child_job, 0, perm, shared_perm, job,
                                 errp);
-    if (need_context_ops && job->job.aio_context != qemu_get_aio_context()) {
-        aio_context_acquire(job->job.aio_context);
+    if (need_context_ops && job_aiocontext != qemu_get_aio_context()) {
+        aio_context_acquire(job_aiocontext);
      }
      if (c == NULL) {
          return -EPERM;

BQL held, too.

job.c void job_enter_cond_locked(Job *job, bool(*fn)(Job *job)) · line 572

Quoted 13 lines
job.c +1 -1
index f16a4ef542..8a5b710d9b 100644
@@ -566,7 +566,7 @@ void job_enter_cond_locked(Job *job, bool(*fn)(Job *job))
      job->busy = true;
      real_job_unlock();
      job_unlock();
-    aio_co_enter(job->aio_context, job->co);
+    aio_co_enter(job_get_aio_context(job), job->co);
      job_lock();
  }
 

If you replace aio_co_enter with aio_co_schedule, you can call it without dropping the lock. The difference being that aio_co_schedule will always go through a bottom half.

Quoted 9 lines
job.c +0 -1
@@ -1138,7 +1138,6 @@ static void coroutine_fn job_co_entry(void *opaque)
      Job *job = opaque;
      int ret;
 
-    assert(job->aio_context == qemu_get_current_aio_context());
      assert(job && job->driver && job->driver->run);
      job_pause_point(job);
      ret = job->driver->run(job, &job->err);

job.c void job_start(Job *job) · line 1179

Quoted 5 lines
job.c +1 -1
@@ -1177,7 +1176,7 @@ void job_start(Job *job)
          job->paused = false;
          job_state_transition_locked(job, JOB_STATUS_RUNNING);
      }
-    aio_co_enter(job->aio_context, job->co);
+    aio_co_enter(job_get_aio_context(job), job->co);

Better to use aio_co_schedule here, too, and move it under the previous WITH_JOB_LOCK_GUARD.

job.c int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp), · line 1307

Quoted 11 lines
job.c +2 -1
  }
 
  void job_cancel_locked(Job *job, bool force)
@@ -1303,7 +1302,8 @@ int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),
      }
 
      job_unlock();
-    AIO_WAIT_WHILE(job->aio_context, (job_enter(job), !job_is_completed(job)));
+    AIO_WAIT_WHILE(job_get_aio_context(job),
+                   (job_enter(job), !job_is_completed(job)));
      job_lock();

Here I think we are also holding the BQL, because this function is "sync", so it's safe to use job->aio_context.

Paolo

#28 lore raw

Re: [PATCH v3 14/16] job.c: use job_get_aio_context()

Quoted 14 lines · Paolo Bonzini
Quoted 14 lines
blockjob.c +3 -1
index cf1f49f6c2..468ba735c5 100644
@@ -155,14 +155,16 @@ static void child_job_set_aio_ctx(BdrvChild *c, 
 AioContext *ctx,
           bdrv_set_aio_context_ignore(sibling->bs, ctx, ignore);
       }
-    job->job.aio_context = ctx;
+    WITH_JOB_LOCK_GUARD() {
+        job->job.aio_context = ctx;
+    }
   }
   static AioContext *child_job_get_parent_aio_context(BdrvChild *c)
Quoted 8 lines

blockjob.c static void child_job_set_aio_ctx(BdrvChild *c, AioContext *ctx, · line 170

Quoted 6 lines
blockjob.c +1 -1
   {
       BlockJob *job = c->opaque;
-    return job->job.aio_context;
+    return job_get_aio_context(&job->job);
   }
   static const BdrvChildClass child_job = {

Both called with BQL held, I think.

Yes, as their callbacks .get_parent_aio_context and .set_aio_context are defined as GS functions in block_int-common.h

Quoted 22 lines
Quoted 22 lines
blockjob.c +5 -4
@@ -218,19 +220,21 @@ int block_job_add_bdrv(BlockJob *job, const char 
 *name, BlockDriverState *bs,
   {
       BdrvChild *c;
       bool need_context_ops;
+    AioContext *job_aiocontext;
       assert(qemu_in_main_thread());
       bdrv_ref(bs);
-    need_context_ops = bdrv_get_aio_context(bs) != job->job.aio_context;
+    job_aiocontext = job_get_aio_context(&job->job);
+    need_context_ops = bdrv_get_aio_context(bs) != job_aiocontext;
-    if (need_context_ops && job->job.aio_context != 
 qemu_get_aio_context()) {
-        aio_context_release(job->job.aio_context);
+    if (need_context_ops && job_aiocontext != qemu_get_aio_context()) {
+        aio_context_release(job_aiocontext);
       }
       c = bdrv_root_attach_child(bs, name, &child_job, 0, perm, shared_perm, job,
                                  errp);
-    if (need_context_ops && job->job.aio_context != 
 qemu_get_aio_context()) {
Quoted 8 lines

blockjob.c int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, · line 240

Quoted 6 lines
blockjob.c +2 -1
-        aio_context_acquire(job->job.aio_context);
+    if (need_context_ops && job_aiocontext != qemu_get_aio_context()) {
+        aio_context_acquire(job_aiocontext);
       }
       if (c == NULL) {
           return -EPERM;

BQL held, too.

Wouldn't it be better to keep job_get_aio_context and implement like this:

AioContext *job_get_aio_context(Job *job)

{
    /*
     * Job AioContext can be written under BQL+job_mutex,
     * but can be read with just the BQL held.
     */
    assert(qemu_in_main_thread());
    return job->aio_context;
}

and instead job_set_aio_context:

void job_set_aio_context(Job *job, AioContext *ctx)

{
    JOB_LOCK_GUARD();
    assert(qemu_in_main_thread());
    job->aio_context = ctx;
}

(obviously implement also _locked version, if needed, and probably move the comment in get_aio_context in job.h).

Thank you,
Emanuele

#29 lore raw

Re: [PATCH v3 14/16] job.c: use job_get_aio_context()

Quoted 61 lines · Emanuele Giuseppe Esposito
Quoted 14 lines · Paolo Bonzini
Quoted 14 lines
blockjob.c +3 -1
index cf1f49f6c2..468ba735c5 100644
@@ -155,14 +155,16 @@ static void child_job_set_aio_ctx(BdrvChild *c, 
 AioContext *ctx,
           bdrv_set_aio_context_ignore(sibling->bs, ctx, ignore);
       }
-    job->job.aio_context = ctx;
+    WITH_JOB_LOCK_GUARD() {
+        job->job.aio_context = ctx;
+    }
   }
   static AioContext *child_job_get_parent_aio_context(BdrvChild *c)
Quoted 8 lines

blockjob.c static void child_job_set_aio_ctx(BdrvChild *c, AioContext *ctx, · line 170

Quoted 6 lines
blockjob.c +1 -1
   {
       BlockJob *job = c->opaque;
-    return job->job.aio_context;
+    return job_get_aio_context(&job->job);
   }
   static const BdrvChildClass child_job = {

Both called with BQL held, I think.

Yes, as their callbacks .get_parent_aio_context and .set_aio_context are defined as GS functions in block_int-common.h

Quoted 23 lines
Quoted 23 lines
blockjob.c +5 -4
@@ -218,19 +220,21 @@ int block_job_add_bdrv(BlockJob *job, const 
 char *name, BlockDriverState *bs,
   {
       BdrvChild *c;
       bool need_context_ops;
+    AioContext *job_aiocontext;
       assert(qemu_in_main_thread());
       bdrv_ref(bs);
-    need_context_ops = bdrv_get_aio_context(bs) != 
 job->job.aio_context;
+    job_aiocontext = job_get_aio_context(&job->job);
+    need_context_ops = bdrv_get_aio_context(bs) != job_aiocontext;
-    if (need_context_ops && job->job.aio_context != 
 qemu_get_aio_context()) {
-        aio_context_release(job->job.aio_context);
+    if (need_context_ops && job_aiocontext != qemu_get_aio_context()) {
+        aio_context_release(job_aiocontext);
       }
       c = bdrv_root_attach_child(bs, name, &child_job, 0, perm, shared_perm, job,
                                  errp);
-    if (need_context_ops && job->job.aio_context != 
 qemu_get_aio_context()) {
Quoted 8 lines
Quoted 6 lines
blockjob.c +2 -1
-        aio_context_acquire(job->job.aio_context);
+    if (need_context_ops && job_aiocontext != qemu_get_aio_context()) {
+        aio_context_acquire(job_aiocontext);
       }
       if (c == NULL) {

          return -EPERM;

BQL held, too.

Wouldn't it be better to keep job_get_aio_context and implement like this:

Quoted 9 lines

AioContext *job_get_aio_context(Job *job)
{
    /*
     * Job AioContext can be written under BQL+job_mutex,
     * but can be read with just the BQL held. */
    assert(qemu_in_main_thread());
    return job->aio_context;
}

Uhm ok this one doesn't really work, because it's ok to read it under either BQL or job lock. So I will get rid of job_get_aio_context, but add job_set_aio_context (and use it in child_job_set_aio_ctx).

Emanuele

Quoted 14 lines

and instead job_set_aio_context:

void job_set_aio_context(Job *job, AioContext *ctx) {
    JOB_LOCK_GUARD(); assert(qemu_in_main_thread()); job->aio_context = ctx;
}

(obviously implement also _locked version, if needed, and probably move the comment in get_aio_context in job.h).

Thank you,
Emanuele

#30 lore raw

Re: [PATCH v3 14/16] job.c: use job_get_aio_context()

In reply to Paolo Bonzini
Quoted 17 lines · Paolo Bonzini
Quoted 6 lines
job.c +0 -0
index f16a4ef542..8a5b710d9b 100644

job.c void job_enter_cond_locked(Job *job, bool(*fn)(Job · line 572

Quoted 8 lines
job.c +1 -1
@@ -566,7 +566,7 @@ void job_enter_cond_locked(Job *job, bool(*fn)(Job 
 *job))
       job->busy = true;
       real_job_unlock();
       job_unlock();
-    aio_co_enter(job->aio_context, job->co);
+    aio_co_enter(job_get_aio_context(job), job->co);
       job_lock();
   }

If you replace aio_co_enter with aio_co_schedule, you can call it without dropping the lock.  The difference being that aio_co_schedule will always go through a bottom half.

Quoted 15 lines

job.c void job_start(Job *job) · line 1179

Quoted 13 lines
job.c +1 -2
@@ -1138,7 +1138,6 @@ static void coroutine_fn job_co_entry(void *opaque)
       Job *job = opaque;
       int ret;
-    assert(job->aio_context == qemu_get_current_aio_context());
       assert(job && job->driver && job->driver->run);
       job_pause_point(job);
       ret = job->driver->run(job, &job->err);
@@ -1177,7 +1176,7 @@ void job_start(Job *job)
           job->paused = false;
           job_state_transition_locked(job, JOB_STATUS_RUNNING);
       }
-    aio_co_enter(job->aio_context, job->co);
+    aio_co_enter(job_get_aio_context(job), job->co);

Better to use aio_co_schedule here, too, and move it under the previous WITH_JOB_LOCK_GUARD.

Unfortunately this does not work straightforward: aio_co_enter invokes aio_co_schedule only if the context is different from the main loop, otherwise it can directly enter the coroutine with qemu_aio_coroutine_enter. So always replacing it with aio_co_schedule breaks the unit tests assumptions, as they expect that when control is returned the job has already executed.

A possible solution is to aio_poll() on the condition we want to assert, waiting for the bh to be scheduled. But I don't know if this is then useful to test something.

Thank you,
Emanuele

#31 lore raw

Re: [PATCH v3 14/16] job.c: use job_get_aio_context()

Quoted 7 lines · Emanuele Giuseppe Esposito
Quoted 3 lines
Quoted 1 line

Better to use aio_co_schedule here, too, and move it under the previous WITH_JOB_LOCK_GUARD.

Unfortunately this does not work straightforward: aio_co_enter invokes aio_co_schedule only if the context is different from the main loop, otherwise it can directly enter the coroutine with qemu_aio_coroutine_enter. So always replacing it with aio_co_schedule breaks the unit tests assumptions, as they expect that when control is returned the job has already executed.

A possible solution is to aio_poll() on the condition we want to assert, waiting for the bh to be scheduled. But I don't know if this is then useful to test something.

I think you sorted that out, based on IRC conversation?

Paolo

#32 lore raw

Re: [PATCH v3 14/16] job.c: use job_get_aio_context()

Quoted 13 lines · Paolo Bonzini
Quoted 12 lines · Emanuele Giuseppe Esposito
Quoted 4 lines
Quoted 1 line

Better to use aio_co_schedule here, too, and move it under the previous WITH_JOB_LOCK_GUARD.

Unfortunately this does not work straightforward: aio_co_enter invokes aio_co_schedule only if the context is different from the main loop, otherwise it can directly enter the coroutine with qemu_aio_coroutine_enter. So always replacing it with aio_co_schedule breaks the unit tests assumptions, as they expect that when control is returned the job has already executed.

Quoted 6 lines
Quoted 3 lines

A possible solution is to aio_poll() on the condition we want to assert, waiting for the bh to be scheduled. But I don't know if this is then useful to test something.

I think you sorted that out, based on IRC conversation?

Yes.

Thank you,
Emanuele

#33 lore raw

PATCH v3 15/16 job.c: enable job lock/unlock and remove Aiocontext locks

In reply to Emanuele Giuseppe Esposito

Change the job_{lock/unlock} and macros to use job_mutex.

Now that they are not nop anymore, remove the aiocontext to avoid deadlocks.

Therefore:
- when possible, remove completely the aiocontext lock/unlock pair
- if it is used by some other functions too, reduce the locking section as much as possible, leaving the job API outside.

There is only one JobDriver callback, ->free() that assumes that the aiocontext lock is held (because it calls bdrv_unref), so for now keep that under aiocontext lock.

Also remove real_job_{lock/unlock}, as they are replaced by the public functions.

---
 blockdev.c                       | 65 ++++-----------------------
 include/qemu/job.h               | 11 +----
 job-qmp.c                        | 41 ++++-------------
 job.c                            | 76 +++-----------------------------
 tests/unit/test-bdrv-drain.c     |  4 +-
 tests/unit/test-block-iothread.c |  2 +-
 tests/unit/test-blockjob.c       | 13 ++----
 7 files changed, 31 insertions(+), 181 deletions(-)
blockdev.c +9 -56
index 1fbd9b9e04..ebc14daa86 100644
@@ -159,12 +159,7 @@ void blockdev_mark_auto_del(BlockBackend *blk)
 
     for (job = block_job_next(NULL); job; job = block_job_next(job)) {
         if (block_job_has_bdrv(job, blk_bs(blk))) {
-            AioContext *aio_context = job->job.aio_context;
-            aio_context_acquire(aio_context);
-
             job_cancel_locked(&job->job, false);
-
-            aio_context_release(aio_context);
         }
     }
 
@@ -1829,16 +1824,9 @@ static void drive_backup_abort(BlkActionState *common)
     DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
 
     if (state->job) {
-        AioContext *aio_context;
-
-        aio_context = bdrv_get_aio_context(state->bs);
-        aio_context_acquire(aio_context);
-
         WITH_JOB_LOCK_GUARD() {
             job_cancel_sync_locked(&state->job->job, true);
         }
-
-        aio_context_release(aio_context);
     }
 }
 
@@ -1932,16 +1920,9 @@ static void blockdev_backup_abort(BlkActionState *common)
     BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
 
     if (state->job) {
-        AioContext *aio_context;
-
-        aio_context = bdrv_get_aio_context(state->bs);
-        aio_context_acquire(aio_context);
-
         WITH_JOB_LOCK_GUARD() {
             job_cancel_sync_locked(&state->job->job, true);
         }
-
-        aio_context_release(aio_context);
     }
 }
 
@@ -3305,18 +3286,13 @@ out:
     aio_context_release(aio_context);
 }
 
-/*
- * Get a block job using its ID and acquire its AioContext.
- * Returns with job_lock held on success.
- */
-static BlockJob *find_block_job(const char *id, AioContext **aio_context,
-                                Error **errp)
+/* Get a block job using its ID. Returns with job_lock held on success */
+static BlockJob *find_block_job(const char *id, Error **errp)
 {
     BlockJob *job;
 
     assert(id != NULL);
 
-    *aio_context = NULL;
     job_lock();
 
     job = block_job_get(id);
@@ -3328,31 +3304,25 @@ static BlockJob *find_block_job(const char *id, AioContext **aio_context,
         return NULL;
     }
 
-    *aio_context = blk_get_aio_context(job->blk);
-    aio_context_acquire(*aio_context);
-
     return job;
 }
 
 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
 {
-    AioContext *aio_context;
-    BlockJob *job = find_block_job(device, &aio_context, errp);
+    BlockJob *job = find_block_job(device, errp);
 
     if (!job) {
         return;
     }
 
     block_job_set_speed(job, speed, errp);
-    aio_context_release(aio_context);
     job_unlock();
 }
 
 void qmp_block_job_cancel(const char *device,
                           bool has_force, bool force, Error **errp)
 {
-    AioContext *aio_context;
-    BlockJob *job = find_block_job(device, &aio_context, errp);
+    BlockJob *job = find_block_job(device, errp);
 
     if (!job) {
         return;
@@ -3371,14 +3341,12 @@ void qmp_block_job_cancel(const char *device,
     trace_qmp_block_job_cancel(job);
     job_user_cancel_locked(&job->job, force, errp);
 out:
-    aio_context_release(aio_context);
     job_unlock();
 }
 
 void qmp_block_job_pause(const char *device, Error **errp)
 {
-    AioContext *aio_context;
-    BlockJob *job = find_block_job(device, &aio_context, errp);
+    BlockJob *job = find_block_job(device, errp);
 
     if (!job) {
         return;
@@ -3386,14 +3354,12 @@ void qmp_block_job_pause(const char *device, Error **errp)
 
     trace_qmp_block_job_pause(job);
     job_user_pause_locked(&job->job, errp);
-    aio_context_release(aio_context);
     job_unlock();
 }
 
 void qmp_block_job_resume(const char *device, Error **errp)
 {
-    AioContext *aio_context;
-    BlockJob *job = find_block_job(device, &aio_context, errp);
+    BlockJob *job = find_block_job(device, errp);
 
     if (!job) {
         return;
@@ -3401,14 +3367,12 @@ void qmp_block_job_resume(const char *device, Error **errp)
 
     trace_qmp_block_job_resume(job);
     job_user_resume_locked(&job->job, errp);
-    aio_context_release(aio_context);
     job_unlock();
 }
 
 void qmp_block_job_complete(const char *device, Error **errp)
 {
-    AioContext *aio_context;
-    BlockJob *job = find_block_job(device, &aio_context, errp);
+    BlockJob *job = find_block_job(device, errp);
 
     if (!job) {
         return;
@@ -3416,14 +3380,12 @@ void qmp_block_job_complete(const char *device, Error **errp)
 
     trace_qmp_block_job_complete(job);
     job_complete_locked(&job->job, errp);
-    aio_context_release(aio_context);
     job_unlock();
 }
 
 void qmp_block_job_finalize(const char *id, Error **errp)
 {
-    AioContext *aio_context;
-    BlockJob *job = find_block_job(id, &aio_context, errp);
+    BlockJob *job = find_block_job(id, errp);
 
     if (!job) {
         return;
@@ -3433,21 +3395,13 @@ void qmp_block_job_finalize(const char *id, Error **errp)
     job_ref_locked(&job->job);
     job_finalize_locked(&job->job, errp);
 
-    /*
-     * Job's context might have changed via job_finalize_locked
-     * (and job_txn_apply automatically acquires the new one),
-     * so make sure we release the correct one.
-     */
-    aio_context = blk_get_aio_context(job->blk);
     job_unref_locked(&job->job);
-    aio_context_release(aio_context);
     job_unlock();
 }
 
 void qmp_block_job_dismiss(const char *id, Error **errp)
 {
-    AioContext *aio_context;
-    BlockJob *bjob = find_block_job(id, &aio_context, errp);
+    BlockJob *bjob = find_block_job(id, errp);
     Job *job;
 
     if (!bjob) {
@@ -3457,7 +3411,6 @@ void qmp_block_job_dismiss(const char *id, Error **errp)
     trace_qmp_block_job_dismiss(bjob);
     job = &bjob->job;
     job_dismiss_locked(&job, errp);
-    aio_context_release(aio_context);
     job_unlock();
 }
 
include/qemu/job.h +2 -9
index c95f9fa8d1..602ee56ae6 100644
@@ -326,9 +326,9 @@ typedef enum JobCreateFlags {
 
 extern QemuMutex job_mutex;
 
-#define JOB_LOCK_GUARD() /* QEMU_LOCK_GUARD(&job_mutex) */
+#define JOB_LOCK_GUARD() QEMU_LOCK_GUARD(&job_mutex)
 
-#define WITH_JOB_LOCK_GUARD() /* WITH_QEMU_LOCK_GUARD(&job_mutex) */
+#define WITH_JOB_LOCK_GUARD() WITH_QEMU_LOCK_GUARD(&job_mutex)
 
 /**
  * job_lock:
@@ -667,8 +667,6 @@ void job_user_cancel_locked(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_locked(Job *job, bool force);
 
@@ -692,9 +690,6 @@ void job_cancel_sync_all(void);
  * function).
  *
  * Returns the return value from the job.
- *
- * Callers must hold the AioContext lock of job->aio_context.
- *
  * Called between job_lock and job_unlock.
  */
 int job_complete_sync_locked(Job *job, Error **errp);
@@ -726,8 +721,6 @@ void job_dismiss_locked(Job **job, Error **errp);
  * Returns 0 if the job is successfully completed, -ECANCELED if the job was
  * cancelled before completing, and -errno in other error cases.
  *
- * Callers must hold the AioContext lock of job->aio_context.
- *
  * Called between job_lock and job_unlock.
  */
 int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),
job-qmp.c +8 -33

123 lines not on this page · raw message

job.c +6 -70

196 lines not on this page · raw message

tests/unit/test-bdrv-drain.c +2 -2
index c03560e63d..dae207e24e 100644
@@ -928,9 +928,9 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type,
         tjob->prepare_ret = -EIO;
         break;
     }
+    aio_context_release(ctx);
 
     job_start(&job->job);
-    aio_context_release(ctx);
 
     if (use_iothread) {
         /* job_co_entry() is run in the I/O thread, wait for the actual job
@@ -994,12 +994,12 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type,
     g_assert_false(job_get_paused(&job->job));
     g_assert_true(job_get_busy(&job->job)); /* We're in qemu_co_sleep_ns() */
 
-    aio_context_acquire(ctx);
     job_lock();
     ret = job_complete_sync_locked(&job->job, &error_abort);
     job_unlock();
     g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
 
+    aio_context_acquire(ctx);
     if (use_iothread) {
         blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
         assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
tests/unit/test-block-iothread.c +1 -1
index addcb5846b..e09e440342 100644
@@ -455,10 +455,10 @@ static void test_attach_blockjob(void)
         aio_poll(qemu_get_aio_context(), false);
     }
 
-    aio_context_acquire(ctx);
     job_lock();
     job_complete_sync_locked(&tjob->common.job, &error_abort);
     job_unlock();
+    aio_context_acquire(ctx);
     blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
     aio_context_release(ctx);
 
tests/unit/test-blockjob.c +3 -10
index ec9128dbb5..c926db7b5d 100644
@@ -228,10 +228,6 @@ static void cancel_common(CancelJob *s)
     BlockJob *job = &s->common;
     BlockBackend *blk = s->blk;
     JobStatus sts = job->job.status;
-    AioContext *ctx;
-
-    ctx = job->job.aio_context;
-    aio_context_acquire(ctx);
 
     job_lock();
     job_cancel_sync_locked(&job->job, true);
@@ -244,7 +240,6 @@ static void cancel_common(CancelJob *s)
     job_unlock();
     destroy_blk(blk);
 
-    aio_context_release(ctx);
 }
 
 static void test_cancel_created(void)
@@ -381,11 +376,9 @@ static void test_cancel_concluded(void)
     aio_poll(qemu_get_aio_context(), true);
     assert(job_get_status(job) == JOB_STATUS_PENDING);
 
-    aio_context_acquire(job->aio_context);
     job_lock();
     job_finalize_locked(job, &error_abort);
     job_unlock();
-    aio_context_release(job->aio_context);
     assert(job_get_status(job) == JOB_STATUS_CONCLUDED);
 
     cancel_common(s);
@@ -478,9 +471,7 @@ static void test_complete_in_standby(void)
 
     /* Wait for the job to become READY */
     job_start(job);
-    aio_context_acquire(ctx);
-    AIO_WAIT_WHILE(ctx, job_get_status(job) != JOB_STATUS_READY);
-    aio_context_release(ctx);
+    AIO_WAIT_WHILE_UNLOCKED(ctx, job_get_status(job) != JOB_STATUS_READY);
 
     /* Begin the drained section, pausing the job */
     bdrv_drain_all_begin();
@@ -498,6 +489,7 @@ static void test_complete_in_standby(void)
     job_complete_locked(job, &error_abort);
 
     /* The test is done now, clean up. */
+    aio_context_release(ctx);
     job_finish_sync_locked(job, NULL, &error_abort);
     assert(job->status == JOB_STATUS_PENDING);
 
@@ -507,6 +499,7 @@ static void test_complete_in_standby(void)
     job_dismiss_locked(&job, &error_abort);
     job_unlock();
 
+    aio_context_acquire(ctx);
     destroy_blk(blk);
     aio_context_release(ctx);
     iothread_join(iothread);
Signature · 1 line
2.31.1
#34 lore raw

Re: [PATCH v3 15/16] job.c: enable job lock/unlock and remove Aiocontext locks

Quoted 3 lines · Emanuele Giuseppe Esposito

Now that they are not nop anymore, remove the aiocontext to avoid deadlocks.

Ok, that should have been in patch 1, together with a description of the deadlocks. :) Disregard that review.

Quoted 3 lines

There is only one JobDriver callback, ->free() that assumes that the aiocontext lock is held (because it calls bdrv_unref), so for now keep that under aiocontext lock.

This explains the issue with detach_child in the other series, too.

Paolo

#35 lore raw

PATCH v3 16/16 block_job_query: remove atomic read

In reply to Emanuele Giuseppe Esposito

Not sure what the atomic here was supposed to do, since job.busy is protected by the job lock. Since the whole function is called under job_mutex, just remove the atomic.

---
 blockjob.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
blockjob.c +2 -2
index 468ba735c5..d1d8808a56 100644
@@ -335,13 +335,13 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
     info = g_new0(BlockJobInfo, 1);
     info->type      = g_strdup(job_type_str(&job->job));
     info->device    = g_strdup(job->job.id);
-    info->busy      = qatomic_read(&job->job.busy);
+    info->busy      = job->job.busy;
     info->paused    = job->job.pause_count > 0;
     info->offset    = progress_current;
     info->len       = progress_total;
     info->speed     = job->speed;
     info->io_status = job->iostatus;
-    info->ready     = job_is_ready(&job->job),
+    info->ready     = job_is_ready_locked(&job->job),
     info->status    = job->job.status;
     info->auto_finalize = job->job.auto_finalize;
     info->auto_dismiss  = job->job.auto_dismiss;
Signature · 1 line
2.31.1
#36 lore raw

Re: [PATCH v3 16/16] block_job_query: remove atomic read

Quoted 4 lines · Emanuele Giuseppe Esposito

+++ b/blockjob.c

blockjob.c +0 -0
@@ -335,13 +335,13 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
      info = g_new0(BlockJobInfo, 1);
      info->type      = g_strdup(job_type_str(&job->job));

blockjob.c BlockJobInfo *block_job_query(BlockJob *job, Error **errp) · line 344

Quoted 10 lines
blockjob.c +2 -2
      info->device    = g_strdup(job->job.id);
-    info->busy      = qatomic_read(&job->job.busy);
+    info->busy      = job->job.busy;
      info->paused    = job->job.pause_count > 0;
      info->offset    = progress_current;
      info->len       = progress_total;
      info->speed     = job->speed;
      info->io_status = job->iostatus;
-    info->ready     = job_is_ready(&job->job),
+    info->ready     = job_is_ready_locked(&job->job),

This second part belongs earlier in the series, I think?

Paolo

#37 lore raw
In reply to Emanuele Giuseppe Esposito
Quoted 10 lines · Emanuele Giuseppe Esposito

In this series, we want to remove the AioContext lock and instead use the already existent job_mutex to protect the job structures and list. This is part of the work to get rid of AioContext lock usage in favour of smaller granularity locks.

In order to simplify reviewer's job, job lock/unlock functions and macros are added as empty prototypes (nop) in patch 1. They are converted to use the actual job mutex only in the last patch, 14. In this way we can freely create locking sections without worrying about deadlocks with the aiocontext lock.

Oops, sorry -- I missed this explanation when first reading the cover letter. Good job, though it needs another iteration; especially for patch 14, and possibly to decide the right placement of patches 10-13.

Thanks,

Paolo

Quoted 74 lines

Patch 2 defines what fields in the job structure need protection, and patches 3-4 categorize respectively locked and unlocked functions in the job API.

Patch 5-9 are in preparation to the job locks, they try to reduce the aiocontext critical sections and other minor fixes.

Patch 10-13 introduces the (nop) job lock into the job API and its users, following the comments and categorizations done in patch 2-3-4.

Patch 14 makes the prototypes in patch 1 use the job_mutex and removes all aiocontext lock at the same time.

Tested this series by running unit tests, qemu-iotests and qtests (x86_64).

This serie is based on my previous series "block layer: split block APIs in global state and I/O".

Based-on: <[email protected]>
---
v3:
* add "_locked" suffix to the functions called under job_mutex lock
* rename _job_lock in real_job_lock
* job_mutex is now public, and drivers like monitor use it directly
* introduce and protect job_get_aio_context
* remove mirror-specific APIs and just use WITH_JOB_GUARD
* more extensive use of WITH_JOB_GUARD and JOB_LOCK_GUARD

RFC v2:
* use JOB_LOCK_GUARD and WITH_JOB_LOCK_GUARD
* mu(u)ltiple typos in commit messages
* job API split patches are sent separately in another series
* use of empty job_{lock/unlock} and JOB_LOCK_GUARD/WITH_JOB_LOCK_GUARD to avoid deadlocks and simplify the reviewer job
* move patch 11 (block_job_query: remove atomic read) as last

Emanuele Giuseppe Esposito (16):
job.c: make job_mutex and job_lock/unlock() public job.h: categorize fields in struct Job
job.h: define locked functions
job.h: define unlocked functions
block/mirror.c: use of job helpers in drivers to avoid TOC/TOU job.c: make job_event_* functions static
job.c: move inner aiocontext lock in callbacks aio-wait.h: introduce AIO_WAIT_WHILE_UNLOCKED
jobs: remove aiocontext locks since the functions are under BQL
jobs: protect jobs with job_lock/unlock
jobs: document all static functions and add _locked() suffix
jobs: use job locks and helpers also in the unit tests
jobs: add job lock in find_* functions
job.c: use job_get_aio_context()
job.c: enable job lock/unlock and remove Aiocontext locks
block_job_query: remove atomic read

  block.c                          |  18 +-
  block/commit.c                   |   4 +-
  block/mirror.c                   |  21 +-
  block/replication.c              |  10 +-
  blockdev.c                       | 112 ++----
  blockjob.c                       | 122 +++---
  include/block/aio-wait.h         |  15 +-
  include/qemu/job.h               | 317 +++++++++++----
  job-qmp.c                        |  74 ++--
  job.c                            | 656 +++++++++++++++++++------------
  monitor/qmp-cmds.c               |   6 +-
  qemu-img.c                       |  41 +-
  tests/unit/test-bdrv-drain.c     |  46 +--
  tests/unit/test-block-iothread.c |  14 +-
  tests/unit/test-blockjob-txn.c   |  24 +-
  tests/unit/test-blockjob.c       |  98 ++---
  16 files changed, 947 insertions(+), 631 deletions(-)