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

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