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