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

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