Blame SOURCES/kvm-block-add-aio_context-field-in-ThrottleGroupMember.patch

9bac43
From 5531090858f0cb3aabf6e95a948256b4eeb36e5a Mon Sep 17 00:00:00 2001
9bac43
From: Stefan Hajnoczi <stefanha@redhat.com>
9bac43
Date: Fri, 17 Nov 2017 11:19:01 +0100
9bac43
Subject: [PATCH 02/15] block: add aio_context field in ThrottleGroupMember
9bac43
9bac43
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
9bac43
Message-id: <20171117111908.8815-3-stefanha@redhat.com>
9bac43
Patchwork-id: 77735
9bac43
O-Subject: [RHV7.5 qemu-kvm-rhev PATCH 2/9] block: add aio_context field in ThrottleGroupMember
9bac43
Bugzilla: 1492295
9bac43
RH-Acked-by: John Snow <jsnow@redhat.com>
9bac43
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
9bac43
RH-Acked-by: Thomas Huth <thuth@redhat.com>
9bac43
9bac43
From: Manos Pitsidianakis <el13635@mail.ntua.gr>
9bac43
9bac43
timer_cb() needs to know about the current Aio context of the throttle
9bac43
request that is woken up. In order to make ThrottleGroupMember backend
9bac43
agnostic, this information is stored in an aio_context field instead of
9bac43
accessing it from BlockBackend.
9bac43
9bac43
Reviewed-by: Alberto Garcia <berto@igalia.com>
9bac43
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
9bac43
Signed-off-by: Manos Pitsidianakis <el13635@mail.ntua.gr>
9bac43
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9bac43
(cherry picked from commit c61791fc23ecd96e6a1e038c379c4033ffd5f40c)
9bac43
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9bac43
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9bac43
---
9bac43
 block/block-backend.c           | 15 ++++------
9bac43
 block/throttle-groups.c         | 38 ++++++++++++++++---------
9bac43
 include/block/throttle-groups.h |  7 ++++-
9bac43
 tests/test-throttle.c           | 63 +++++++++++++++++++++--------------------
9bac43
 4 files changed, 69 insertions(+), 54 deletions(-)
9bac43
9bac43
diff --git a/block/block-backend.c b/block/block-backend.c
9bac43
index e61f072..515be10 100644
9bac43
--- a/block/block-backend.c
9bac43
+++ b/block/block-backend.c
9bac43
@@ -1766,18 +1766,14 @@ static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
9bac43
 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
9bac43
 {
9bac43
     BlockDriverState *bs = blk_bs(blk);
9bac43
-    ThrottleTimers *tt;
9bac43
+    ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
9bac43
 
9bac43
     if (bs) {
9bac43
-        if (blk->public.throttle_group_member.throttle_state) {
9bac43
-            tt = &blk->public.throttle_group_member.throttle_timers;
9bac43
-            throttle_timers_detach_aio_context(tt);
9bac43
+        if (tgm->throttle_state) {
9bac43
+            throttle_group_detach_aio_context(tgm);
9bac43
+            throttle_group_attach_aio_context(tgm, new_context);
9bac43
         }
9bac43
         bdrv_set_aio_context(bs, new_context);
9bac43
-        if (blk->public.throttle_group_member.throttle_state) {
9bac43
-            tt = &blk->public.throttle_group_member.throttle_timers;
9bac43
-            throttle_timers_attach_aio_context(tt, new_context);
9bac43
-        }
9bac43
     }
9bac43
 }
9bac43
 
9bac43
@@ -2010,7 +2006,8 @@ void blk_io_limits_disable(BlockBackend *blk)
9bac43
 void blk_io_limits_enable(BlockBackend *blk, const char *group)
9bac43
 {
9bac43
     assert(!blk->public.throttle_group_member.throttle_state);
9bac43
-    throttle_group_register_tgm(&blk->public.throttle_group_member, group);
9bac43
+    throttle_group_register_tgm(&blk->public.throttle_group_member,
9bac43
+                                group, blk_get_aio_context(blk));
9bac43
 }
9bac43
 
9bac43
 void blk_io_limits_update_group(BlockBackend *blk, const char *group)
9bac43
diff --git a/block/throttle-groups.c b/block/throttle-groups.c
9bac43
index c8ed16d..3b07b25 100644
9bac43
--- a/block/throttle-groups.c
9bac43
+++ b/block/throttle-groups.c
9bac43
@@ -391,9 +391,6 @@ static void coroutine_fn throttle_group_restart_queue_entry(void *opaque)
9bac43
 
9bac43
 static void throttle_group_restart_queue(ThrottleGroupMember *tgm, bool is_write)
9bac43
 {
9bac43
-    BlockBackendPublic *blkp = container_of(tgm, BlockBackendPublic,
9bac43
-            throttle_group_member);
9bac43
-    BlockBackend *blk = blk_by_public(blkp);
9bac43
     Coroutine *co;
9bac43
     RestartData rd = {
9bac43
         .tgm = tgm,
9bac43
@@ -401,7 +398,7 @@ static void throttle_group_restart_queue(ThrottleGroupMember *tgm, bool is_write
9bac43
     };
9bac43
 
9bac43
     co = qemu_coroutine_create(throttle_group_restart_queue_entry, &rd);
9bac43
-    aio_co_enter(blk_get_aio_context(blk), co);
9bac43
+    aio_co_enter(tgm->aio_context, co);
9bac43
 }
9bac43
 
9bac43
 void throttle_group_restart_tgm(ThrottleGroupMember *tgm)
9bac43
@@ -449,13 +446,11 @@ void throttle_group_get_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg)
9bac43
 /* ThrottleTimers callback. This wakes up a request that was waiting
9bac43
  * because it had been throttled.
9bac43
  *
9bac43
- * @blk:       the BlockBackend whose request had been throttled
9bac43
+ * @tgm:       the ThrottleGroupMember whose request had been throttled
9bac43
  * @is_write:  the type of operation (read/write)
9bac43
  */
9bac43
-static void timer_cb(BlockBackend *blk, bool is_write)
9bac43
+static void timer_cb(ThrottleGroupMember *tgm, bool is_write)
9bac43
 {
9bac43
-    BlockBackendPublic *blkp = blk_get_public(blk);
9bac43
-    ThrottleGroupMember *tgm = &blkp->throttle_group_member;
9bac43
     ThrottleState *ts = tgm->throttle_state;
9bac43
     ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
9bac43
 
9bac43
@@ -484,18 +479,18 @@ static void write_timer_cb(void *opaque)
9bac43
  *
9bac43
  * @tgm:       the ThrottleGroupMember to insert
9bac43
  * @groupname: the name of the group
9bac43
+ * @ctx:       the AioContext to use
9bac43
  */
9bac43
 void throttle_group_register_tgm(ThrottleGroupMember *tgm,
9bac43
-                                 const char *groupname)
9bac43
+                                 const char *groupname,
9bac43
+                                 AioContext *ctx)
9bac43
 {
9bac43
     int i;
9bac43
-    BlockBackendPublic *blkp = container_of(tgm, BlockBackendPublic,
9bac43
-            throttle_group_member);
9bac43
-    BlockBackend *blk = blk_by_public(blkp);
9bac43
     ThrottleState *ts = throttle_group_incref(groupname);
9bac43
     ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
9bac43
 
9bac43
     tgm->throttle_state = ts;
9bac43
+    tgm->aio_context = ctx;
9bac43
 
9bac43
     qemu_mutex_lock(&tg->lock);
9bac43
     /* If the ThrottleGroup is new set this ThrottleGroupMember as the token */
9bac43
@@ -508,11 +503,11 @@ void throttle_group_register_tgm(ThrottleGroupMember *tgm,
9bac43
     QLIST_INSERT_HEAD(&tg->head, tgm, round_robin);
9bac43
 
9bac43
     throttle_timers_init(&tgm->throttle_timers,
9bac43
-                         blk_get_aio_context(blk),
9bac43
+                         tgm->aio_context,
9bac43
                          tg->clock_type,
9bac43
                          read_timer_cb,
9bac43
                          write_timer_cb,
9bac43
-                         blk);
9bac43
+                         tgm);
9bac43
 
9bac43
     qemu_mutex_unlock(&tg->lock);
9bac43
 }
9bac43
@@ -559,6 +554,21 @@ void throttle_group_unregister_tgm(ThrottleGroupMember *tgm)
9bac43
     tgm->throttle_state = NULL;
9bac43
 }
9bac43
 
9bac43
+void throttle_group_attach_aio_context(ThrottleGroupMember *tgm,
9bac43
+                                       AioContext *new_context)
9bac43
+{
9bac43
+    ThrottleTimers *tt = &tgm->throttle_timers;
9bac43
+    throttle_timers_attach_aio_context(tt, new_context);
9bac43
+    tgm->aio_context = new_context;
9bac43
+}
9bac43
+
9bac43
+void throttle_group_detach_aio_context(ThrottleGroupMember *tgm)
9bac43
+{
9bac43
+    ThrottleTimers *tt = &tgm->throttle_timers;
9bac43
+    throttle_timers_detach_aio_context(tt);
9bac43
+    tgm->aio_context = NULL;
9bac43
+}
9bac43
+
9bac43
 static void throttle_groups_init(void)
9bac43
 {
9bac43
     qemu_mutex_init(&throttle_groups_lock);
9bac43
diff --git a/include/block/throttle-groups.h b/include/block/throttle-groups.h
9bac43
index 1a6bcda..a0f27ca 100644
9bac43
--- a/include/block/throttle-groups.h
9bac43
+++ b/include/block/throttle-groups.h
9bac43
@@ -33,6 +33,7 @@
9bac43
  */
9bac43
 
9bac43
 typedef struct ThrottleGroupMember {
9bac43
+    AioContext   *aio_context;
9bac43
     /* throttled_reqs_lock protects the CoQueues for throttled requests.  */
9bac43
     CoMutex      throttled_reqs_lock;
9bac43
     CoQueue      throttled_reqs[2];
9bac43
@@ -61,12 +62,16 @@ void throttle_group_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg);
9bac43
 void throttle_group_get_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg);
9bac43
 
9bac43
 void throttle_group_register_tgm(ThrottleGroupMember *tgm,
9bac43
-                                const char *groupname);
9bac43
+                                const char *groupname,
9bac43
+                                AioContext *ctx);
9bac43
 void throttle_group_unregister_tgm(ThrottleGroupMember *tgm);
9bac43
 void throttle_group_restart_tgm(ThrottleGroupMember *tgm);
9bac43
 
9bac43
 void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgm,
9bac43
                                                         unsigned int bytes,
9bac43
                                                         bool is_write);
9bac43
+void throttle_group_attach_aio_context(ThrottleGroupMember *tgm,
9bac43
+                                       AioContext *new_context);
9bac43
+void throttle_group_detach_aio_context(ThrottleGroupMember *tgm);
9bac43
 
9bac43
 #endif
9bac43
diff --git a/tests/test-throttle.c b/tests/test-throttle.c
9bac43
index 6e6d926..57cf5ba 100644
9bac43
--- a/tests/test-throttle.c
9bac43
+++ b/tests/test-throttle.c
9bac43
@@ -24,8 +24,9 @@
9bac43
 static AioContext     *ctx;
9bac43
 static LeakyBucket    bkt;
9bac43
 static ThrottleConfig cfg;
9bac43
+static ThrottleGroupMember tgm;
9bac43
 static ThrottleState  ts;
9bac43
-static ThrottleTimers tt;
9bac43
+static ThrottleTimers *tt;
9bac43
 
9bac43
 /* useful function */
9bac43
 static bool double_cmp(double x, double y)
9bac43
@@ -153,19 +154,21 @@ static void test_init(void)
9bac43
 {
9bac43
     int i;
9bac43
 
9bac43
+    tt = &tgm.throttle_timers;
9bac43
+
9bac43
     /* fill the structures with crap */
9bac43
     memset(&ts, 1, sizeof(ts));
9bac43
-    memset(&tt, 1, sizeof(tt));
9bac43
+    memset(tt, 1, sizeof(*tt));
9bac43
 
9bac43
     /* init structures */
9bac43
     throttle_init(&ts);
9bac43
-    throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
+    throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
                          read_timer_cb, write_timer_cb, &ts);
9bac43
 
9bac43
     /* check initialized fields */
9bac43
-    g_assert(tt.clock_type == QEMU_CLOCK_VIRTUAL);
9bac43
-    g_assert(tt.timers[0]);
9bac43
-    g_assert(tt.timers[1]);
9bac43
+    g_assert(tt->clock_type == QEMU_CLOCK_VIRTUAL);
9bac43
+    g_assert(tt->timers[0]);
9bac43
+    g_assert(tt->timers[1]);
9bac43
 
9bac43
     /* check other fields where cleared */
9bac43
     g_assert(!ts.previous_leak);
9bac43
@@ -176,18 +179,18 @@ static void test_init(void)
9bac43
         g_assert(!ts.cfg.buckets[i].level);
9bac43
     }
9bac43
 
9bac43
-    throttle_timers_destroy(&tt;;
9bac43
+    throttle_timers_destroy(tt);
9bac43
 }
9bac43
 
9bac43
 static void test_destroy(void)
9bac43
 {
9bac43
     int i;
9bac43
     throttle_init(&ts);
9bac43
-    throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
+    throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
                          read_timer_cb, write_timer_cb, &ts);
9bac43
-    throttle_timers_destroy(&tt;;
9bac43
+    throttle_timers_destroy(tt);
9bac43
     for (i = 0; i < 2; i++) {
9bac43
-        g_assert(!tt.timers[i]);
9bac43
+        g_assert(!tt->timers[i]);
9bac43
     }
9bac43
 }
9bac43
 
9bac43
@@ -224,7 +227,7 @@ static void test_config_functions(void)
9bac43
     orig_cfg.op_size = 1;
9bac43
 
9bac43
     throttle_init(&ts);
9bac43
-    throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
+    throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
                          read_timer_cb, write_timer_cb, &ts);
9bac43
     /* structure reset by throttle_init previous_leak should be null */
9bac43
     g_assert(!ts.previous_leak);
9bac43
@@ -236,7 +239,7 @@ static void test_config_functions(void)
9bac43
     /* get back the fixed configuration */
9bac43
     throttle_get_config(&ts, &final_cfg);
9bac43
 
9bac43
-    throttle_timers_destroy(&tt;;
9bac43
+    throttle_timers_destroy(tt);
9bac43
 
9bac43
     g_assert(final_cfg.buckets[THROTTLE_BPS_TOTAL].avg == 153);
9bac43
     g_assert(final_cfg.buckets[THROTTLE_BPS_READ].avg  == 56);
9bac43
@@ -417,45 +420,45 @@ static void test_have_timer(void)
9bac43
 {
9bac43
     /* zero structures */
9bac43
     memset(&ts, 0, sizeof(ts));
9bac43
-    memset(&tt, 0, sizeof(tt));
9bac43
+    memset(tt, 0, sizeof(*tt));
9bac43
 
9bac43
     /* no timer set should return false */
9bac43
-    g_assert(!throttle_timers_are_initialized(&tt));
9bac43
+    g_assert(!throttle_timers_are_initialized(tt));
9bac43
 
9bac43
     /* init structures */
9bac43
     throttle_init(&ts);
9bac43
-    throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
+    throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
                          read_timer_cb, write_timer_cb, &ts);
9bac43
 
9bac43
     /* timer set by init should return true */
9bac43
-    g_assert(throttle_timers_are_initialized(&tt));
9bac43
+    g_assert(throttle_timers_are_initialized(tt));
9bac43
 
9bac43
-    throttle_timers_destroy(&tt;;
9bac43
+    throttle_timers_destroy(tt);
9bac43
 }
9bac43
 
9bac43
 static void test_detach_attach(void)
9bac43
 {
9bac43
     /* zero structures */
9bac43
     memset(&ts, 0, sizeof(ts));
9bac43
-    memset(&tt, 0, sizeof(tt));
9bac43
+    memset(tt, 0, sizeof(*tt));
9bac43
 
9bac43
     /* init the structure */
9bac43
     throttle_init(&ts);
9bac43
-    throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
+    throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
                          read_timer_cb, write_timer_cb, &ts);
9bac43
 
9bac43
     /* timer set by init should return true */
9bac43
-    g_assert(throttle_timers_are_initialized(&tt));
9bac43
+    g_assert(throttle_timers_are_initialized(tt));
9bac43
 
9bac43
     /* timer should no longer exist after detaching */
9bac43
-    throttle_timers_detach_aio_context(&tt;;
9bac43
-    g_assert(!throttle_timers_are_initialized(&tt));
9bac43
+    throttle_timers_detach_aio_context(tt);
9bac43
+    g_assert(!throttle_timers_are_initialized(tt));
9bac43
 
9bac43
     /* timer should exist again after attaching */
9bac43
-    throttle_timers_attach_aio_context(&tt, ctx);
9bac43
-    g_assert(throttle_timers_are_initialized(&tt));
9bac43
+    throttle_timers_attach_aio_context(tt, ctx);
9bac43
+    g_assert(throttle_timers_are_initialized(tt));
9bac43
 
9bac43
-    throttle_timers_destroy(&tt;;
9bac43
+    throttle_timers_destroy(tt);
9bac43
 }
9bac43
 
9bac43
 static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */
9bac43
@@ -484,7 +487,7 @@ static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */
9bac43
     cfg.op_size = op_size;
9bac43
 
9bac43
     throttle_init(&ts);
9bac43
-    throttle_timers_init(&tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
+    throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
9bac43
                          read_timer_cb, write_timer_cb, &ts);
9bac43
     throttle_config(&ts, QEMU_CLOCK_VIRTUAL, &cfg;;
9bac43
 
9bac43
@@ -511,7 +514,7 @@ static bool do_test_accounting(bool is_ops, /* are we testing bps or ops */
9bac43
         return false;
9bac43
     }
9bac43
 
9bac43
-    throttle_timers_destroy(&tt;;
9bac43
+    throttle_timers_destroy(tt);
9bac43
 
9bac43
     return true;
9bac43
 }
9bac43
@@ -611,9 +614,9 @@ static void test_groups(void)
9bac43
     g_assert(tgm2->throttle_state == NULL);
9bac43
     g_assert(tgm3->throttle_state == NULL);
9bac43
 
9bac43
-    throttle_group_register_tgm(tgm1, "bar");
9bac43
-    throttle_group_register_tgm(tgm2, "foo");
9bac43
-    throttle_group_register_tgm(tgm3, "bar");
9bac43
+    throttle_group_register_tgm(tgm1, "bar", blk_get_aio_context(blk1));
9bac43
+    throttle_group_register_tgm(tgm2, "foo", blk_get_aio_context(blk2));
9bac43
+    throttle_group_register_tgm(tgm3, "bar", blk_get_aio_context(blk3));
9bac43
 
9bac43
     g_assert(tgm1->throttle_state != NULL);
9bac43
     g_assert(tgm2->throttle_state != NULL);
9bac43
-- 
9bac43
1.8.3.1
9bac43