Blame SOURCES/kvm-job-Add-reference-counting.patch

1bdc94
From 3f7eb910b9ce698ebc33a039e6bbef1df1437072 Mon Sep 17 00:00:00 2001
1bdc94
From: Kevin Wolf <kwolf@redhat.com>
1bdc94
Date: Tue, 26 Jun 2018 09:48:05 +0200
1bdc94
Subject: [PATCH 36/89] job: Add reference counting
1bdc94
1bdc94
RH-Author: Kevin Wolf <kwolf@redhat.com>
1bdc94
Message-id: <20180626094856.6924-23-kwolf@redhat.com>
1bdc94
Patchwork-id: 81073
1bdc94
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 22/73] job: Add reference counting
1bdc94
Bugzilla: 1513543
1bdc94
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
1bdc94
RH-Acked-by: Max Reitz <mreitz@redhat.com>
1bdc94
RH-Acked-by: Fam Zheng <famz@redhat.com>
1bdc94
1bdc94
This moves reference counting from BlockJob to Job.
1bdc94
1bdc94
In order to keep calling the BlockJob cleanup code when the job is
1bdc94
deleted via job_unref(), introduce a new JobDriver.free callback. Every
1bdc94
block job must use block_job_free() for this callback, this is asserted
1bdc94
in block_job_create().
1bdc94
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
Reviewed-by: Max Reitz <mreitz@redhat.com>
1bdc94
Reviewed-by: John Snow <jsnow@redhat.com>
1bdc94
(cherry picked from commit 80fa2c756b3241f24015a7503a01f7999d4a942d)
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 block/backup.c               |  1 +
1bdc94
 block/commit.c               |  1 +
1bdc94
 block/mirror.c               |  2 ++
1bdc94
 block/stream.c               |  1 +
1bdc94
 blockjob.c                   | 48 +++++++++++++++++++-------------------------
1bdc94
 include/block/blockjob.h     | 21 -------------------
1bdc94
 include/block/blockjob_int.h |  7 +++++++
1bdc94
 include/qemu/job.h           | 19 ++++++++++++++++--
1bdc94
 job.c                        | 22 ++++++++++++++++----
1bdc94
 qemu-img.c                   |  4 ++--
1bdc94
 tests/test-bdrv-drain.c      |  1 +
1bdc94
 tests/test-blockjob-txn.c    |  1 +
1bdc94
 tests/test-blockjob.c        |  6 ++++--
1bdc94
 13 files changed, 76 insertions(+), 58 deletions(-)
1bdc94
1bdc94
diff --git a/block/backup.c b/block/backup.c
1bdc94
index baf8d43..cfdb89d 100644
1bdc94
--- a/block/backup.c
1bdc94
+++ b/block/backup.c
1bdc94
@@ -526,6 +526,7 @@ static const BlockJobDriver backup_job_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size          = sizeof(BackupBlockJob),
1bdc94
         .job_type               = JOB_TYPE_BACKUP,
1bdc94
+        .free                   = block_job_free,
1bdc94
     },
1bdc94
     .start                  = backup_run,
1bdc94
     .commit                 = backup_commit,
1bdc94
diff --git a/block/commit.c b/block/commit.c
1bdc94
index 32d29c8..925c96a 100644
1bdc94
--- a/block/commit.c
1bdc94
+++ b/block/commit.c
1bdc94
@@ -218,6 +218,7 @@ static const BlockJobDriver commit_job_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size = sizeof(CommitBlockJob),
1bdc94
         .job_type      = JOB_TYPE_COMMIT,
1bdc94
+        .free          = block_job_free,
1bdc94
     },
1bdc94
     .start         = commit_run,
1bdc94
 };
1bdc94
diff --git a/block/mirror.c b/block/mirror.c
1bdc94
index 35fcc1f..0df4f70 100644
1bdc94
--- a/block/mirror.c
1bdc94
+++ b/block/mirror.c
1bdc94
@@ -989,6 +989,7 @@ static const BlockJobDriver mirror_job_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size          = sizeof(MirrorBlockJob),
1bdc94
         .job_type               = JOB_TYPE_MIRROR,
1bdc94
+        .free                   = block_job_free,
1bdc94
     },
1bdc94
     .start                  = mirror_run,
1bdc94
     .complete               = mirror_complete,
1bdc94
@@ -1001,6 +1002,7 @@ static const BlockJobDriver commit_active_job_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size          = sizeof(MirrorBlockJob),
1bdc94
         .job_type               = JOB_TYPE_COMMIT,
1bdc94
+        .free                   = block_job_free,
1bdc94
     },
1bdc94
     .start                  = mirror_run,
1bdc94
     .complete               = mirror_complete,
1bdc94
diff --git a/block/stream.c b/block/stream.c
1bdc94
index cb723f1..7273d22 100644
1bdc94
--- a/block/stream.c
1bdc94
+++ b/block/stream.c
1bdc94
@@ -212,6 +212,7 @@ static const BlockJobDriver stream_job_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size = sizeof(StreamBlockJob),
1bdc94
         .job_type      = JOB_TYPE_STREAM,
1bdc94
+        .free          = block_job_free,
1bdc94
     },
1bdc94
     .start         = stream_run,
1bdc94
 };
1bdc94
diff --git a/blockjob.c b/blockjob.c
1bdc94
index 0fba01e..0bf0a26 100644
1bdc94
--- a/blockjob.c
1bdc94
+++ b/blockjob.c
1bdc94
@@ -190,31 +190,25 @@ static void block_job_resume(BlockJob *job)
1bdc94
     block_job_enter_cond(job, block_job_timer_not_pending);
1bdc94
 }
1bdc94
 
1bdc94
-void block_job_ref(BlockJob *job)
1bdc94
-{
1bdc94
-    ++job->refcnt;
1bdc94
-}
1bdc94
-
1bdc94
 static void block_job_attached_aio_context(AioContext *new_context,
1bdc94
                                            void *opaque);
1bdc94
 static void block_job_detach_aio_context(void *opaque);
1bdc94
 
1bdc94
-void block_job_unref(BlockJob *job)
1bdc94
+void block_job_free(Job *job)
1bdc94
 {
1bdc94
-    if (--job->refcnt == 0) {
1bdc94
-        assert(job->job.status == JOB_STATUS_NULL);
1bdc94
-        assert(!job->txn);
1bdc94
-        BlockDriverState *bs = blk_bs(job->blk);
1bdc94
-        bs->job = NULL;
1bdc94
-        block_job_remove_all_bdrv(job);
1bdc94
-        blk_remove_aio_context_notifier(job->blk,
1bdc94
-                                        block_job_attached_aio_context,
1bdc94
-                                        block_job_detach_aio_context, job);
1bdc94
-        blk_unref(job->blk);
1bdc94
-        error_free(job->blocker);
1bdc94
-        assert(!timer_pending(&job->sleep_timer));
1bdc94
-        job_delete(&job->job);
1bdc94
-    }
1bdc94
+    BlockJob *bjob = container_of(job, BlockJob, job);
1bdc94
+    BlockDriverState *bs = blk_bs(bjob->blk);
1bdc94
+
1bdc94
+    assert(!bjob->txn);
1bdc94
+
1bdc94
+    bs->job = NULL;
1bdc94
+    block_job_remove_all_bdrv(bjob);
1bdc94
+    blk_remove_aio_context_notifier(bjob->blk,
1bdc94
+                                    block_job_attached_aio_context,
1bdc94
+                                    block_job_detach_aio_context, bjob);
1bdc94
+    blk_unref(bjob->blk);
1bdc94
+    error_free(bjob->blocker);
1bdc94
+    assert(!timer_pending(&bjob->sleep_timer));
1bdc94
 }
1bdc94
 
1bdc94
 static void block_job_attached_aio_context(AioContext *new_context,
1bdc94
@@ -245,7 +239,7 @@ static void block_job_detach_aio_context(void *opaque)
1bdc94
     BlockJob *job = opaque;
1bdc94
 
1bdc94
     /* In case the job terminates during aio_poll()... */
1bdc94
-    block_job_ref(job);
1bdc94
+    job_ref(&job->job);
1bdc94
 
1bdc94
     block_job_pause(job);
1bdc94
 
1bdc94
@@ -253,7 +247,7 @@ static void block_job_detach_aio_context(void *opaque)
1bdc94
         block_job_drain(job);
1bdc94
     }
1bdc94
 
1bdc94
-    block_job_unref(job);
1bdc94
+    job_unref(&job->job);
1bdc94
 }
1bdc94
 
1bdc94
 static char *child_job_get_parent_desc(BdrvChild *c)
1bdc94
@@ -367,7 +361,7 @@ static void block_job_decommission(BlockJob *job)
1bdc94
     job->deferred_to_main_loop = true;
1bdc94
     block_job_txn_del_job(job);
1bdc94
     job_state_transition(&job->job, JOB_STATUS_NULL);
1bdc94
-    block_job_unref(job);
1bdc94
+    job_unref(&job->job);
1bdc94
 }
1bdc94
 
1bdc94
 static void block_job_do_dismiss(BlockJob *job)
1bdc94
@@ -506,14 +500,14 @@ static int block_job_finish_sync(BlockJob *job,
1bdc94
 
1bdc94
     assert(blk_bs(job->blk)->job == job);
1bdc94
 
1bdc94
-    block_job_ref(job);
1bdc94
+    job_ref(&job->job);
1bdc94
 
1bdc94
     if (finish) {
1bdc94
         finish(job, &local_err);
1bdc94
     }
1bdc94
     if (local_err) {
1bdc94
         error_propagate(errp, local_err);
1bdc94
-        block_job_unref(job);
1bdc94
+        job_unref(&job->job);
1bdc94
         return -EBUSY;
1bdc94
     }
1bdc94
     /* block_job_drain calls block_job_enter, and it should be enough to
1bdc94
@@ -526,7 +520,7 @@ static int block_job_finish_sync(BlockJob *job,
1bdc94
         aio_poll(qemu_get_aio_context(), true);
1bdc94
     }
1bdc94
     ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
1bdc94
-    block_job_unref(job);
1bdc94
+    job_unref(&job->job);
1bdc94
     return ret;
1bdc94
 }
1bdc94
 
1bdc94
@@ -909,6 +903,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
1bdc94
     }
1bdc94
 
1bdc94
     assert(is_block_job(&job->job));
1bdc94
+    assert(job->job.driver->free == &block_job_free);
1bdc94
 
1bdc94
     job->driver        = driver;
1bdc94
     job->blk           = blk;
1bdc94
@@ -917,7 +912,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
1bdc94
     job->busy          = false;
1bdc94
     job->paused        = true;
1bdc94
     job->pause_count   = 1;
1bdc94
-    job->refcnt        = 1;
1bdc94
     job->auto_finalize = !(flags & BLOCK_JOB_MANUAL_FINALIZE);
1bdc94
     job->auto_dismiss  = !(flags & BLOCK_JOB_MANUAL_DISMISS);
1bdc94
     aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
1bdc94
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
1bdc94
index 01cdee6..087e782 100644
1bdc94
--- a/include/block/blockjob.h
1bdc94
+++ b/include/block/blockjob.h
1bdc94
@@ -132,9 +132,6 @@ typedef struct BlockJob {
1bdc94
     /** The opaque value that is passed to the completion function.  */
1bdc94
     void *opaque;
1bdc94
 
1bdc94
-    /** Reference count of the block job */
1bdc94
-    int refcnt;
1bdc94
-
1bdc94
     /** True when job has reported completion by calling block_job_completed. */
1bdc94
     bool completed;
1bdc94
 
1bdc94
@@ -400,24 +397,6 @@ void block_job_iostatus_reset(BlockJob *job);
1bdc94
 BlockJobTxn *block_job_txn_new(void);
1bdc94
 
1bdc94
 /**
1bdc94
- * block_job_ref:
1bdc94
- *
1bdc94
- * Add a reference to BlockJob refcnt, it will be decreased with
1bdc94
- * block_job_unref, and then be freed if it comes to be the last
1bdc94
- * reference.
1bdc94
- */
1bdc94
-void block_job_ref(BlockJob *job);
1bdc94
-
1bdc94
-/**
1bdc94
- * block_job_unref:
1bdc94
- *
1bdc94
- * Release a reference that was previously acquired with block_job_ref
1bdc94
- * or block_job_create. If it's the last reference to the object, it will be
1bdc94
- * freed.
1bdc94
- */
1bdc94
-void block_job_unref(BlockJob *job);
1bdc94
-
1bdc94
-/**
1bdc94
  * block_job_txn_unref:
1bdc94
  *
1bdc94
  * Release a reference that was previously acquired with block_job_txn_add_job
1bdc94
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
1bdc94
index 1e62d6d..6f0fe3c 100644
1bdc94
--- a/include/block/blockjob_int.h
1bdc94
+++ b/include/block/blockjob_int.h
1bdc94
@@ -144,6 +144,13 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
1bdc94
                        BlockCompletionFunc *cb, void *opaque, Error **errp);
1bdc94
 
1bdc94
 /**
1bdc94
+ * block_job_free:
1bdc94
+ * Callback to be used for JobDriver.free in all block jobs. Frees block job
1bdc94
+ * specific resources in @job.
1bdc94
+ */
1bdc94
+void block_job_free(Job *job);
1bdc94
+
1bdc94
+/**
1bdc94
  * block_job_sleep_ns:
1bdc94
  * @job: The job that calls the function.
1bdc94
  * @ns: How many nanoseconds to stop for.
1bdc94
diff --git a/include/qemu/job.h b/include/qemu/job.h
1bdc94
index 0b78778..0751e2a 100644
1bdc94
--- a/include/qemu/job.h
1bdc94
+++ b/include/qemu/job.h
1bdc94
@@ -41,6 +41,9 @@ typedef struct Job {
1bdc94
     /** The type of this job. */
1bdc94
     const JobDriver *driver;
1bdc94
 
1bdc94
+    /** Reference count of the block job */
1bdc94
+    int refcnt;
1bdc94
+
1bdc94
     /** Current state; See @JobStatus for details. */
1bdc94
     JobStatus status;
1bdc94
 
1bdc94
@@ -57,6 +60,9 @@ struct JobDriver {
1bdc94
 
1bdc94
     /** Enum describing the operation */
1bdc94
     JobType job_type;
1bdc94
+
1bdc94
+    /** Called when the job is freed */
1bdc94
+    void (*free)(Job *job);
1bdc94
 };
1bdc94
 
1bdc94
 
1bdc94
@@ -69,8 +75,17 @@ struct JobDriver {
1bdc94
  */
1bdc94
 void *job_create(const char *job_id, const JobDriver *driver, Error **errp);
1bdc94
 
1bdc94
-/** Frees the @job object. */
1bdc94
-void job_delete(Job *job);
1bdc94
+/**
1bdc94
+ * Add a reference to Job refcnt, it will be decreased with job_unref, and then
1bdc94
+ * be freed if it comes to be the last reference.
1bdc94
+ */
1bdc94
+void job_ref(Job *job);
1bdc94
+
1bdc94
+/**
1bdc94
+ * Release a reference that was previously acquired with job_ref() or
1bdc94
+ * job_create(). If it's the last reference to the object, it will be freed.
1bdc94
+ */
1bdc94
+void job_unref(Job *job);
1bdc94
 
1bdc94
 /** Returns the JobType of a given Job. */
1bdc94
 JobType job_type(const Job *job);
1bdc94
diff --git a/job.c b/job.c
1bdc94
index b049a32..926f1de 100644
1bdc94
--- a/job.c
1bdc94
+++ b/job.c
1bdc94
@@ -134,6 +134,7 @@ void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
1bdc94
     job = g_malloc0(driver->instance_size);
1bdc94
     job->driver        = driver;
1bdc94
     job->id            = g_strdup(job_id);
1bdc94
+    job->refcnt        = 1;
1bdc94
 
1bdc94
     job_state_transition(job, JOB_STATUS_CREATED);
1bdc94
 
1bdc94
@@ -142,10 +143,23 @@ void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
1bdc94
     return job;
1bdc94
 }
1bdc94
 
1bdc94
-void job_delete(Job *job)
1bdc94
+void job_ref(Job *job)
1bdc94
 {
1bdc94
-    QLIST_REMOVE(job, job_list);
1bdc94
+    ++job->refcnt;
1bdc94
+}
1bdc94
+
1bdc94
+void job_unref(Job *job)
1bdc94
+{
1bdc94
+    if (--job->refcnt == 0) {
1bdc94
+        assert(job->status == JOB_STATUS_NULL);
1bdc94
 
1bdc94
-    g_free(job->id);
1bdc94
-    g_free(job);
1bdc94
+        if (job->driver->free) {
1bdc94
+            job->driver->free(job);
1bdc94
+        }
1bdc94
+
1bdc94
+        QLIST_REMOVE(job, job_list);
1bdc94
+
1bdc94
+        g_free(job->id);
1bdc94
+        g_free(job);
1bdc94
+    }
1bdc94
 }
1bdc94
diff --git a/qemu-img.c b/qemu-img.c
1bdc94
index e2395b9..f745919 100644
1bdc94
--- a/qemu-img.c
1bdc94
+++ b/qemu-img.c
1bdc94
@@ -873,7 +873,7 @@ static void run_block_job(BlockJob *job, Error **errp)
1bdc94
     int ret = 0;
1bdc94
 
1bdc94
     aio_context_acquire(aio_context);
1bdc94
-    block_job_ref(job);
1bdc94
+    job_ref(&job->job);
1bdc94
     do {
1bdc94
         aio_poll(aio_context, true);
1bdc94
         qemu_progress_print(job->len ?
1bdc94
@@ -885,7 +885,7 @@ static void run_block_job(BlockJob *job, Error **errp)
1bdc94
     } else {
1bdc94
         ret = job->ret;
1bdc94
     }
1bdc94
-    block_job_unref(job);
1bdc94
+    job_unref(&job->job);
1bdc94
     aio_context_release(aio_context);
1bdc94
 
1bdc94
     /* publish completion progress only when success */
1bdc94
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
1bdc94
index fe9f412..f9e37d4 100644
1bdc94
--- a/tests/test-bdrv-drain.c
1bdc94
+++ b/tests/test-bdrv-drain.c
1bdc94
@@ -522,6 +522,7 @@ static void test_job_complete(BlockJob *job, Error **errp)
1bdc94
 BlockJobDriver test_job_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size  = sizeof(TestBlockJob),
1bdc94
+        .free           = block_job_free,
1bdc94
     },
1bdc94
     .start          = test_job_start,
1bdc94
     .complete       = test_job_complete,
1bdc94
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
1bdc94
index 48b12d1..b49b28c 100644
1bdc94
--- a/tests/test-blockjob-txn.c
1bdc94
+++ b/tests/test-blockjob-txn.c
1bdc94
@@ -76,6 +76,7 @@ static void test_block_job_cb(void *opaque, int ret)
1bdc94
 static const BlockJobDriver test_block_job_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size = sizeof(TestBlockJob),
1bdc94
+        .free          = block_job_free,
1bdc94
     },
1bdc94
     .start = test_block_job_run,
1bdc94
 };
1bdc94
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
1bdc94
index 6ccd585..e24fc3f 100644
1bdc94
--- a/tests/test-blockjob.c
1bdc94
+++ b/tests/test-blockjob.c
1bdc94
@@ -19,6 +19,7 @@
1bdc94
 static const BlockJobDriver test_block_job_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size = sizeof(BlockJob),
1bdc94
+        .free          = block_job_free,
1bdc94
     },
1bdc94
 };
1bdc94
 
1bdc94
@@ -196,6 +197,7 @@ static void coroutine_fn cancel_job_start(void *opaque)
1bdc94
 static const BlockJobDriver test_cancel_driver = {
1bdc94
     .job_driver = {
1bdc94
         .instance_size = sizeof(CancelJob),
1bdc94
+        .free          = block_job_free,
1bdc94
     },
1bdc94
     .start         = cancel_job_start,
1bdc94
     .complete      = cancel_job_complete,
1bdc94
@@ -210,7 +212,7 @@ static CancelJob *create_common(BlockJob **pjob)
1bdc94
     blk = create_blk(NULL);
1bdc94
     job = mk_job(blk, "Steve", &test_cancel_driver, true,
1bdc94
                  BLOCK_JOB_MANUAL_FINALIZE | BLOCK_JOB_MANUAL_DISMISS);
1bdc94
-    block_job_ref(job);
1bdc94
+    job_ref(&job->job);
1bdc94
     assert(job->job.status == JOB_STATUS_CREATED);
1bdc94
     s = container_of(job, CancelJob, common);
1bdc94
     s->blk = blk;
1bdc94
@@ -231,7 +233,7 @@ static void cancel_common(CancelJob *s)
1bdc94
         block_job_dismiss(&dummy, &error_abort);
1bdc94
     }
1bdc94
     assert(job->job.status == JOB_STATUS_NULL);
1bdc94
-    block_job_unref(job);
1bdc94
+    job_unref(&job->job);
1bdc94
     destroy_blk(blk);
1bdc94
 }
1bdc94
 
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94