22c213
From 8e8f421cce99543081f225acf46541312cfbc371 Mon Sep 17 00:00:00 2001
22c213
From: Laurent Vivier <lvivier@redhat.com>
22c213
Date: Tue, 17 Mar 2020 17:05:18 +0000
22c213
Subject: [PATCH 1/2] migration: Rate limit inside host pages
22c213
22c213
RH-Author: Laurent Vivier <lvivier@redhat.com>
22c213
Message-id: <20200317170518.9303-1-lvivier@redhat.com>
22c213
Patchwork-id: 94374
22c213
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH] migration: Rate limit inside host pages
22c213
Bugzilla: 1814336
22c213
RH-Acked-by: Peter Xu <peterx@redhat.com>
22c213
RH-Acked-by: Juan Quintela <quintela@redhat.com>
22c213
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
22c213
When using hugepages, rate limiting is necessary within each huge
22c213
page, since a 1G huge page can take a significant time to send, so
22c213
you end up with bursty behaviour.
22c213
22c213
Fixes: 4c011c37ecb3 ("postcopy: Send whole huge pages")
22c213
Reported-by: Lin Ma <LMa@suse.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Reviewed-by: Juan Quintela <quintela@redhat.com>
22c213
Reviewed-by: Peter Xu <peterx@redhat.com>
22c213
Signed-off-by: Juan Quintela <quintela@redhat.com>
22c213
(cherry picked from commit 97e1e06780e70f6e98a0d2df881e0c0927d3aeb6)
22c213
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
22c213
22c213
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1814336
22c213
BRANCH: rhel-av-8.2.0
22c213
UPSTREAM: Merged
22c213
BREW: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=27283241
22c213
TESTED: Tested that the migration abort doesn't trigger an error message in
22c213
        the kernel logs on P9
22c213
22c213
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
22c213
---
22c213
 migration/migration.c  | 57 ++++++++++++++++++++++++++++----------------------
22c213
 migration/migration.h  |  1 +
22c213
 migration/ram.c        |  2 ++
22c213
 migration/trace-events |  4 ++--
22c213
 4 files changed, 37 insertions(+), 27 deletions(-)
22c213
22c213
diff --git a/migration/migration.c b/migration/migration.c
22c213
index ed18c59..e31d0f5 100644
22c213
--- a/migration/migration.c
22c213
+++ b/migration/migration.c
22c213
@@ -3253,6 +3253,37 @@ void migration_consume_urgent_request(void)
22c213
     qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
22c213
 }
22c213
 
22c213
+/* Returns true if the rate limiting was broken by an urgent request */
22c213
+bool migration_rate_limit(void)
22c213
+{
22c213
+    int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
22c213
+    MigrationState *s = migrate_get_current();
22c213
+
22c213
+    bool urgent = false;
22c213
+    migration_update_counters(s, now);
22c213
+    if (qemu_file_rate_limit(s->to_dst_file)) {
22c213
+        /*
22c213
+         * Wait for a delay to do rate limiting OR
22c213
+         * something urgent to post the semaphore.
22c213
+         */
22c213
+        int ms = s->iteration_start_time + BUFFER_DELAY - now;
22c213
+        trace_migration_rate_limit_pre(ms);
22c213
+        if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
22c213
+            /*
22c213
+             * We were woken by one or more urgent things but
22c213
+             * the timedwait will have consumed one of them.
22c213
+             * The service routine for the urgent wake will dec
22c213
+             * the semaphore itself for each item it consumes,
22c213
+             * so add this one we just eat back.
22c213
+             */
22c213
+            qemu_sem_post(&s->rate_limit_sem);
22c213
+            urgent = true;
22c213
+        }
22c213
+        trace_migration_rate_limit_post(urgent);
22c213
+    }
22c213
+    return urgent;
22c213
+}
22c213
+
22c213
 /*
22c213
  * Master migration thread on the source VM.
22c213
  * It drives the migration and pumps the data down the outgoing channel.
22c213
@@ -3319,8 +3350,6 @@ static void *migration_thread(void *opaque)
22c213
     trace_migration_thread_setup_complete();
22c213
 
22c213
     while (migration_is_active(s)) {
22c213
-        int64_t current_time;
22c213
-
22c213
         if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
22c213
             MigIterateState iter_state = migration_iteration_run(s);
22c213
             if (iter_state == MIG_ITERATE_SKIP) {
22c213
@@ -3347,29 +3376,7 @@ static void *migration_thread(void *opaque)
22c213
             update_iteration_initial_status(s);
22c213
         }
22c213
 
22c213
-        current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
22c213
-
22c213
-        migration_update_counters(s, current_time);
22c213
-
22c213
-        urgent = false;
22c213
-        if (qemu_file_rate_limit(s->to_dst_file)) {
22c213
-            /* Wait for a delay to do rate limiting OR
22c213
-             * something urgent to post the semaphore.
22c213
-             */
22c213
-            int ms = s->iteration_start_time + BUFFER_DELAY - current_time;
22c213
-            trace_migration_thread_ratelimit_pre(ms);
22c213
-            if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
22c213
-                /* We were worken by one or more urgent things but
22c213
-                 * the timedwait will have consumed one of them.
22c213
-                 * The service routine for the urgent wake will dec
22c213
-                 * the semaphore itself for each item it consumes,
22c213
-                 * so add this one we just eat back.
22c213
-                 */
22c213
-                qemu_sem_post(&s->rate_limit_sem);
22c213
-                urgent = true;
22c213
-            }
22c213
-            trace_migration_thread_ratelimit_post(urgent);
22c213
-        }
22c213
+        urgent = migration_rate_limit();
22c213
     }
22c213
 
22c213
     trace_migration_thread_after_loop();
22c213
diff --git a/migration/migration.h b/migration/migration.h
22c213
index a2b2336..a15e8d8 100644
22c213
--- a/migration/migration.h
22c213
+++ b/migration/migration.h
22c213
@@ -347,5 +347,6 @@ extern bool migrate_pre_2_2;
22c213
 
22c213
 void migration_make_urgent_request(void);
22c213
 void migration_consume_urgent_request(void);
22c213
+bool migration_rate_limit(void);
22c213
 
22c213
 #endif
22c213
diff --git a/migration/ram.c b/migration/ram.c
22c213
index 3891eff..5344c7d 100644
22c213
--- a/migration/ram.c
22c213
+++ b/migration/ram.c
22c213
@@ -2661,6 +2661,8 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
22c213
 
22c213
         pages += tmppages;
22c213
         pss->page++;
22c213
+        /* Allow rate limiting to happen in the middle of huge pages */
22c213
+        migration_rate_limit();
22c213
     } while ((pss->page & (pagesize_bits - 1)) &&
22c213
              offset_in_ramblock(pss->block, pss->page << TARGET_PAGE_BITS));
22c213
 
22c213
diff --git a/migration/trace-events b/migration/trace-events
22c213
index 6dee7b5..2f9129e 100644
22c213
--- a/migration/trace-events
22c213
+++ b/migration/trace-events
22c213
@@ -138,12 +138,12 @@ migrate_send_rp_recv_bitmap(char *name, int64_t size) "block '%s' size 0x%"PRIi6
22c213
 migration_completion_file_err(void) ""
22c213
 migration_completion_postcopy_end(void) ""
22c213
 migration_completion_postcopy_end_after_complete(void) ""
22c213
+migration_rate_limit_pre(int ms) "%d ms"
22c213
+migration_rate_limit_post(int urgent) "urgent: %d"
22c213
 migration_return_path_end_before(void) ""
22c213
 migration_return_path_end_after(int rp_error) "%d"
22c213
 migration_thread_after_loop(void) ""
22c213
 migration_thread_file_err(void) ""
22c213
-migration_thread_ratelimit_pre(int ms) "%d ms"
22c213
-migration_thread_ratelimit_post(int urgent) "urgent: %d"
22c213
 migration_thread_setup_complete(void) ""
22c213
 open_return_path_on_source(void) ""
22c213
 open_return_path_on_source_continue(void) ""
22c213
-- 
22c213
1.8.3.1
22c213