Skip to contentSkip to conversation

migration: Skip sync in ram_init_bitmaps()

RFC PATCH In review [email protected] 2 participants 7 messages Oct 23, 2024 – Oct 31, 2024

lore
Share & tools
lore
#0 lore raw

cover migration: Skip sync in ram_init_bitmaps()

From: Hyman Huang <[email protected]>

As discussed in:
https://lore.kernel.org/qemu-devel/ZvbQ0RQx-zxOeo4Y@x1n/
RAMBlock dirty sync in ram_init_bitmaps() appears to be unnecessary; this patchset attempts to eliminate it and asks for comments on how to do so.

Please review, thanks

Yong

Hyman Huang (2):
accel/kvm: Introduce kvm_dirty_log_manual_enabled
migration: Avoid doing RAMBlock dirty sync in the initial iteration

 accel/kvm/kvm-all.c      |  2 ++
 include/sysemu/kvm.h     |  8 ++++++++
 migration/cpu-throttle.c |  3 ++-
 migration/ram.c          | 30 +++++++++++++++++++++++++++---
 4 files changed, 39 insertions(+), 4 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

RFC PATCH 1/2 accel/kvm: Introduce kvm_dirty_log_manual_enabled

From: Hyman Huang <[email protected]>

Introudce kvm_dirty_log_manual_enabled to indicate if dirty logging manually was enabled in KVM.

The kvm_dirty_log_manual_enabled will be used in the next commit.

---
 accel/kvm/kvm-all.c  | 2 ++
 include/sysemu/kvm.h | 8 ++++++++
 2 files changed, 10 insertions(+)
accel/kvm/kvm-all.c +2 -0
index 801cff16a5..a0f3e6e493 100644
@@ -93,6 +93,7 @@ bool kvm_allowed;
 bool kvm_readonly_mem_allowed;
 bool kvm_vm_attributes_allowed;
 bool kvm_msi_use_devid;
+bool kvm_dirty_log_manual;
 static bool kvm_has_guest_debug;
 static int kvm_sstep_flags;
 static bool kvm_immediate_exit;
@@ -2537,6 +2538,7 @@ static int kvm_setup_dirty_ring(KVMState *s)
                             dirty_log_manual_caps);
                 s->manual_dirty_log_protect = 0;
             }
+            kvm_dirty_log_manual = true;
         }
     }
 
include/sysemu/kvm.h +8 -0
index c3a60b2890..f38f2818e1 100644
@@ -42,6 +42,7 @@ extern bool kvm_gsi_routing_allowed;
 extern bool kvm_gsi_direct_mapping;
 extern bool kvm_readonly_mem_allowed;
 extern bool kvm_msi_use_devid;
+extern bool kvm_dirty_log_manual;
 
 #define kvm_enabled()           (kvm_allowed)
 /**
@@ -143,6 +144,12 @@ extern bool kvm_msi_use_devid;
  */
 #define kvm_msi_devid_required() (kvm_msi_use_devid)
 
+/**
+ * kvm_dirty_log_manual_enabled:
+ * Returns: true if dirty logging manually was enabled.
+ */
+#define kvm_dirty_log_manual_enabled() (kvm_dirty_log_manual)
+
 #else
 
 #define kvm_enabled()           (0)
@@ -157,6 +164,7 @@ extern bool kvm_msi_use_devid;
 #define kvm_gsi_direct_mapping() (false)
 #define kvm_readonly_mem_enabled() (false)
 #define kvm_msi_devid_required() (false)
+#define kvm_dirty_log_manual_enabled() (false)
 
 #endif  /* CONFIG_KVM_IS_POSSIBLE */
 
Signature · 1 line
2.27.0
#2 lore raw

RFC PATCH 2/2 migration: Avoid doing RAMBlock dirty sync in the initial iteration

In reply to [email protected]

From: Hyman Huang <[email protected]>

KVM always returns 1 when userspace retrieves a dirty bitmap for the first time when KVM_DIRTY_LOG_INITIALLY_SET is enabled; in such scenario, the RAMBlock dirty sync of the initial iteration can be skipped.

---
 migration/cpu-throttle.c |  3 ++-
 migration/ram.c          | 30 +++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 4 deletions(-)
migration/cpu-throttle.c +2 -1
index 342681cdd4..06e3b1be78 100644
@@ -27,6 +27,7 @@
 #include "hw/core/cpu.h"
 #include "qemu/main-loop.h"
 #include "sysemu/cpus.h"
+#include "sysemu/kvm.h"
 #include "cpu-throttle.h"
 #include "migration.h"
 #include "migration-stats.h"
@@ -141,7 +142,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
      * effect on guest performance, therefore omit it to avoid
      * paying extra for the sync penalty.
      */
-    if (sync_cnt <= 1) {
+    if (sync_cnt <= (kvm_dirty_log_manual_enabled() ? 0 : 1)) {
         goto end;
     }
 
migration/ram.c +27 -3
index d284f63854..b312ebd69d 100644
@@ -2718,7 +2718,7 @@ static void ram_list_init_bitmaps(void)
 {
     MigrationState *ms = migrate_get_current();
     RAMBlock *block;
-    unsigned long pages;
+    unsigned long pages, clear_bmap_pages;
     uint8_t shift;
 
     /* Skip setting bitmap if there is no RAM */
@@ -2736,6 +2736,7 @@ static void ram_list_init_bitmaps(void)
 
         RAMBLOCK_FOREACH_NOT_IGNORED(block) {
             pages = block->max_length >> TARGET_PAGE_BITS;
+            clear_bmap_pages = clear_bmap_size(pages, shift);
             /*
              * The initial dirty bitmap for migration must be set with all
              * ones to make sure we'll migrate every guest RAM page to
@@ -2751,7 +2752,17 @@ static void ram_list_init_bitmaps(void)
                 block->file_bmap = bitmap_new(pages);
             }
             block->clear_bmap_shift = shift;
-            block->clear_bmap = bitmap_new(clear_bmap_size(pages, shift));
+            block->clear_bmap = bitmap_new(clear_bmap_pages);
+            /*
+             * Set the clear bitmap by default to enable dirty logging.
+             *
+             * Note that with KVM_DIRTY_LOG_INITIALLY_SET, dirty logging
+             * will be enabled gradually in small chunks using
+             * KVM_CLEAR_DIRTY_LOG
+             */
+            if (kvm_dirty_log_manual_enabled()) {
+                bitmap_set(block->clear_bmap, 0, clear_bmap_pages);
+            }
         }
     }
 }
@@ -2771,6 +2782,7 @@ static void migration_bitmap_clear_discarded_pages(RAMState *rs)
 
 static bool ram_init_bitmaps(RAMState *rs, Error **errp)
 {
+    Error *local_err = NULL;
     bool ret = true;
 
     qemu_mutex_lock_ramlist();
@@ -2783,7 +2795,19 @@ static bool ram_init_bitmaps(RAMState *rs, Error **errp)
             if (!ret) {
                 goto out_unlock;
             }
-            migration_bitmap_sync_precopy(false);
+
+            if (kvm_dirty_log_manual_enabled()) {
+                /*
+                 * Bypass the RAMBlock dirty sync and still publish a
+                 * notification.
+                 */
+                if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC,
+                            &local_err)) {
+                    error_report_err(local_err);
+                }
+            } else {
+                migration_bitmap_sync_precopy(false);
+            }
         }
     }
 out_unlock:
Signature · 1 line
2.27.0
#3 lore raw

Re: [PATCH RFC 2/2] migration: Avoid doing RAMBlock dirty sync in the initial iteration

Quoted 61 lines · On Wed, Oct 23, 2024 at 10:09:51AM +0800, [email protected] wrote:

From: Hyman Huang <[email protected]>

KVM always returns 1 when userspace retrieves a dirty bitmap for the first time when KVM_DIRTY_LOG_INITIALLY_SET is enabled; in such scenario, the RAMBlock dirty sync of the initial iteration can be skipped.

---
 migration/cpu-throttle.c |  3 ++-
 migration/ram.c          | 30 +++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 4 deletions(-)
migration/cpu-throttle.c +2 -1
index 342681cdd4..06e3b1be78 100644
@@ -27,6 +27,7 @@
 #include "hw/core/cpu.h"
 #include "qemu/main-loop.h"
 #include "sysemu/cpus.h"
+#include "sysemu/kvm.h"
 #include "cpu-throttle.h"
 #include "migration.h"
 #include "migration-stats.h"
@@ -141,7 +142,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
      * effect on guest performance, therefore omit it to avoid
      * paying extra for the sync penalty.
      */
-    if (sync_cnt <= 1) {
+    if (sync_cnt <= (kvm_dirty_log_manual_enabled() ? 0 : 1)) {
         goto end;
     }
 
migration/ram.c +3 -2
index d284f63854..b312ebd69d 100644
@@ -2718,7 +2718,7 @@ static void ram_list_init_bitmaps(void)
 {
     MigrationState *ms = migrate_get_current();
     RAMBlock *block;
-    unsigned long pages;
+    unsigned long pages, clear_bmap_pages;
     uint8_t shift;
 
     /* Skip setting bitmap if there is no RAM */
@@ -2736,6 +2736,7 @@ static void ram_list_init_bitmaps(void)
 
         RAMBLOCK_FOREACH_NOT_IGNORED(block) {
             pages = block->max_length >> TARGET_PAGE_BITS;
+            clear_bmap_pages = clear_bmap_size(pages, shift);
             /*
              * The initial dirty bitmap for migration must be set with all
              * ones to make sure we'll migrate every guest RAM page to
@@ -2751,7 +2752,17 @@ static void ram_list_init_bitmaps(void)
                 block->file_bmap = bitmap_new(pages);
             }
             block->clear_bmap_shift = shift;
-            block->clear_bmap = bitmap_new(clear_bmap_size(pages, shift));
+            block->clear_bmap = bitmap_new(clear_bmap_pages);

migration/ram.c static void ram_list_init_bitmaps(void) · line 2765

Quoted 10 lines
migration/ram.c +10 -0
+            /*
+             * Set the clear bitmap by default to enable dirty logging.
+             *
+             * Note that with KVM_DIRTY_LOG_INITIALLY_SET, dirty logging
+             * will be enabled gradually in small chunks using
+             * KVM_CLEAR_DIRTY_LOG
+             */
+            if (kvm_dirty_log_manual_enabled()) {
+                bitmap_set(block->clear_bmap, 0, clear_bmap_pages);
+            }

Why it needs to be relevant to whether DIRTY_LOG is enabled?

I wonder if we should always set clear_bmap to 1 unconditionally, as we always set bmap to all 1s by default.

Then we skip sync always during setup, dropping patch 1.

Quoted 35 lines
migration/ram.c +14 -1
         }
     }
 }
@@ -2771,6 +2782,7 @@ static void migration_bitmap_clear_discarded_pages(RAMState *rs)
 
 static bool ram_init_bitmaps(RAMState *rs, Error **errp)
 {
+    Error *local_err = NULL;
     bool ret = true;
 
     qemu_mutex_lock_ramlist();
@@ -2783,7 +2795,19 @@ static bool ram_init_bitmaps(RAMState *rs, Error **errp)
             if (!ret) {
                 goto out_unlock;
             }
-            migration_bitmap_sync_precopy(false);
+
+            if (kvm_dirty_log_manual_enabled()) {
+                /*
+                 * Bypass the RAMBlock dirty sync and still publish a
+                 * notification.
+                 */
+                if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC,
+                            &local_err)) {
+                    error_report_err(local_err);
+                }
+            } else {
+                migration_bitmap_sync_precopy(false);
+            }
         }
     }
 out_unlock:
Signature · 1 line
2.27.0
Signature · 1 line
Peter Xu
author
#4 lore raw

Re: [PATCH RFC 2/2] migration: Avoid doing RAMBlock dirty sync in the initial iteration

Quoted 75 lines · Peter Xu
Quoted 48 lines · On Wed, Oct 23, 2024 at 10:09:51AM +0800, [email protected] wrote:

From: Hyman Huang <[email protected]>

KVM always returns 1 when userspace retrieves a dirty bitmap for the first time when KVM_DIRTY_LOG_INITIALLY_SET is enabled; in such scenario, the RAMBlock dirty sync of the initial iteration can be skipped.

---
 migration/cpu-throttle.c |  3 ++-
 migration/ram.c          | 30 +++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 4 deletions(-)
migration/cpu-throttle.c +2 -1
index 342681cdd4..06e3b1be78 100644
@@ -27,6 +27,7 @@
 #include "hw/core/cpu.h"
 #include "qemu/main-loop.h"
 #include "sysemu/cpus.h"
+#include "sysemu/kvm.h"
 #include "cpu-throttle.h"
 #include "migration.h"
 #include "migration-stats.h"
@@ -141,7 +142,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
      * effect on guest performance, therefore omit it to avoid
      * paying extra for the sync penalty.
      */
-    if (sync_cnt <= 1) {
+    if (sync_cnt <= (kvm_dirty_log_manual_enabled() ? 0 : 1)) {
         goto end;
     }
 
migration/ram.c +1 -1
index d284f63854..b312ebd69d 100644
@@ -2718,7 +2718,7 @@ static void ram_list_init_bitmaps(void)
 {
     MigrationState *ms = migrate_get_current();
     RAMBlock *block;
-    unsigned long pages;
+    unsigned long pages, clear_bmap_pages;
     uint8_t shift;
 
     /* Skip setting bitmap if there is no RAM */

migration/ram.c static void ram_list_init_bitmaps(void) · line 2741

Quoted 6 lines
migration/ram.c +1 -0
@@ -2736,6 +2736,7 @@ static void ram_list_init_bitmaps(void)
 
         RAMBLOCK_FOREACH_NOT_IGNORED(block) {
             pages = block->max_length >> TARGET_PAGE_BITS;
+            clear_bmap_pages = clear_bmap_size(pages, shift);
             /*
              * The initial dirty bitmap for migration must be set with

all

migration/ram.c static void ram_list_init_bitmaps(void) · line 2754

Quoted 6 lines
migration/ram.c +0 -1
              * ones to make sure we'll migrate every guest RAM page to
@@ -2751,7 +2752,17 @@ static void ram_list_init_bitmaps(void)
                 block->file_bmap = bitmap_new(pages);
             }
             block->clear_bmap_shift = shift;
-            block->clear_bmap = bitmap_new(clear_bmap_size(pages,

shift));

migration/ram.c static void ram_list_init_bitmaps(void) · line 2765

Quoted 11 lines
migration/ram.c +11 -0
+            block->clear_bmap = bitmap_new(clear_bmap_pages);
+            /*
+             * Set the clear bitmap by default to enable dirty logging.
+             *
+             * Note that with KVM_DIRTY_LOG_INITIALLY_SET, dirty logging
+             * will be enabled gradually in small chunks using
+             * KVM_CLEAR_DIRTY_LOG
+             */
+            if (kvm_dirty_log_manual_enabled()) {
+                bitmap_set(block->clear_bmap, 0, clear_bmap_pages);
+            }
Quoted 5 lines

Why it needs to be relevant to whether DIRTY_LOG is enabled?

I wonder if we should always set clear_bmap to 1 unconditionally, as we always set bmap to all 1s by default.

OK, this works. We can drop it.

Quoted 3 lines

Then we skip sync always during setup, dropping patch 1.

IIUC, KVM initializes the slot->dirty_bitmap with 1 only when KVM_DIRTY_LOG_INITIALLY_SET is enabled, 0 otherwize. This means that if KVM does not support the KVM_DIRTY_LOG_INITIALLY_SET feature, userspace should do the first sync so that KVM could set the WP bit and clear the D-bit of the PTE.

Skipping first sync could handle this scenario?

Quoted 42 lines

migration/ram.c static void

Quoted 4 lines
migration/ram.c +0 -0
         }
     }
 }
@@ -2771,6 +2782,7 @@ static void

migration_bitmap_clear_discarded_pages(RAMState *rs)

migration/ram.c static bool ram_init_bitmaps(RAMState *rs, Error

Quoted 8 lines
migration/ram.c +1 -0
 static bool ram_init_bitmaps(RAMState *rs, Error **errp)
 {
+    Error *local_err = NULL;
     bool ret = true;
 
     qemu_mutex_lock_ramlist();
@@ -2783,7 +2795,19 @@ static bool ram_init_bitmaps(RAMState *rs, Error

**errp)

Quoted 21 lines
migration/ram.c +13 -2
             if (!ret) {
                 goto out_unlock;
             }
-            migration_bitmap_sync_precopy(false);
+
+            if (kvm_dirty_log_manual_enabled()) {
+                /*
+                 * Bypass the RAMBlock dirty sync and still publish a
+                 * notification.
+                 */
+                if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC,
+                            &local_err)) {
+                    error_report_err(local_err);
+                }
+            } else {
+                migration_bitmap_sync_precopy(false);
+            }
         }
     }
 out_unlock:
--
Quoted 2 lines

2.27.0

--
Peter Xu

Signature · 1 line
Best regards
#5 lore raw

Re: [PATCH RFC 2/2] migration: Avoid doing RAMBlock dirty sync in the initial iteration

Quoted 90 lines · Yong Huang
Quoted 75 lines · Peter Xu
Quoted 48 lines · On Wed, Oct 23, 2024 at 10:09:51AM +0800, [email protected] wrote:

From: Hyman Huang <[email protected]>

KVM always returns 1 when userspace retrieves a dirty bitmap for the first time when KVM_DIRTY_LOG_INITIALLY_SET is enabled; in such scenario, the RAMBlock dirty sync of the initial iteration can be skipped.

---
 migration/cpu-throttle.c |  3 ++-
 migration/ram.c          | 30 +++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 4 deletions(-)
migration/cpu-throttle.c +2 -1
index 342681cdd4..06e3b1be78 100644
@@ -27,6 +27,7 @@
 #include "hw/core/cpu.h"
 #include "qemu/main-loop.h"
 #include "sysemu/cpus.h"
+#include "sysemu/kvm.h"
 #include "cpu-throttle.h"
 #include "migration.h"
 #include "migration-stats.h"
@@ -141,7 +142,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
      * effect on guest performance, therefore omit it to avoid
      * paying extra for the sync penalty.
      */
-    if (sync_cnt <= 1) {
+    if (sync_cnt <= (kvm_dirty_log_manual_enabled() ? 0 : 1)) {
         goto end;
     }
 
migration/ram.c +1 -1
index d284f63854..b312ebd69d 100644
@@ -2718,7 +2718,7 @@ static void ram_list_init_bitmaps(void)
 {
     MigrationState *ms = migrate_get_current();
     RAMBlock *block;
-    unsigned long pages;
+    unsigned long pages, clear_bmap_pages;
     uint8_t shift;
 
     /* Skip setting bitmap if there is no RAM */

migration/ram.c static void ram_list_init_bitmaps(void) · line 2741

Quoted 6 lines
migration/ram.c +1 -0
@@ -2736,6 +2736,7 @@ static void ram_list_init_bitmaps(void)
 
         RAMBLOCK_FOREACH_NOT_IGNORED(block) {
             pages = block->max_length >> TARGET_PAGE_BITS;
+            clear_bmap_pages = clear_bmap_size(pages, shift);
             /*
              * The initial dirty bitmap for migration must be set with

all

migration/ram.c static void ram_list_init_bitmaps(void) · line 2754

Quoted 6 lines
migration/ram.c +0 -1
              * ones to make sure we'll migrate every guest RAM page to
@@ -2751,7 +2752,17 @@ static void ram_list_init_bitmaps(void)
                 block->file_bmap = bitmap_new(pages);
             }
             block->clear_bmap_shift = shift;
-            block->clear_bmap = bitmap_new(clear_bmap_size(pages,

shift));

migration/ram.c static void ram_list_init_bitmaps(void) · line 2765

Quoted 11 lines
migration/ram.c +11 -0
+            block->clear_bmap = bitmap_new(clear_bmap_pages);
+            /*
+             * Set the clear bitmap by default to enable dirty logging.
+             *
+             * Note that with KVM_DIRTY_LOG_INITIALLY_SET, dirty logging
+             * will be enabled gradually in small chunks using
+             * KVM_CLEAR_DIRTY_LOG
+             */
+            if (kvm_dirty_log_manual_enabled()) {
+                bitmap_set(block->clear_bmap, 0, clear_bmap_pages);
+            }
Quoted 5 lines

Why it needs to be relevant to whether DIRTY_LOG is enabled?

I wonder if we should always set clear_bmap to 1 unconditionally, as we always set bmap to all 1s by default.

OK, this works. We can drop it.

Quoted 3 lines

Then we skip sync always during setup, dropping patch 1.

Quoted 8 lines

IIUC, KVM initializes the slot->dirty_bitmap with 1 only when KVM_DIRTY_LOG_INITIALLY_SET is enabled, 0 otherwize. This means that if KVM does not support the KVM_DIRTY_LOG_INITIALLY_SET feature, userspace should do the first sync so that KVM could set the WP bit and clear the D-bit of the PTE.

Skipping first sync could handle this scenario?

Yes, the old kernels could be tricky (!CLEAR_LOG support), but I hope it's also working.

The thing is log_start() should also protect all pages if that's the case. For x86, that should corresponds to:

kvm_mmu_slot_apply_flags():

		/*
		 * Initially-all-set does not require write protecting any page,
		 * because they're all assumed to be dirty.
		 */
		if (kvm_dirty_log_manual_protect_and_init_set(kvm))
			return;

		if (READ_ONCE(eager_page_split))
			kvm_mmu_slot_try_split_huge_pages(kvm, new, PG_LEVEL_4K);

		if (kvm_x86_ops.cpu_dirty_log_size) {
			kvm_mmu_slot_leaf_clear_dirty(kvm, new);
			kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_2M);
		} else {
			kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_4K);
		}

In general, I think even if GET_DIRTY_LOG in setup() is omitted, all pages should still be wr-protected already right after log_start(). Then follow up log_clear()s will be noop, until the next sync which will reprotect every page again.

Please double check.

Thanks,

Signature · 1 line
Peter Xu
author
#6 lore raw

Re: [PATCH RFC 2/2] migration: Avoid doing RAMBlock dirty sync in the initial iteration

Quoted 125 lines · Peter Xu
Quoted 16 lines · Yong Huang
Quoted 14 lines · Peter Xu
Quoted 13 lines · On Wed, Oct 23, 2024 at 10:09:51AM +0800, [email protected] wrote:

From: Hyman Huang <[email protected]>

KVM always returns 1 when userspace retrieves a dirty bitmap for the first time when KVM_DIRTY_LOG_INITIALLY_SET is enabled; in such scenario, the RAMBlock dirty sync of the initial iteration can be skipped.

---
 migration/cpu-throttle.c |  3 ++-
 migration/ram.c          | 30 +++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 4 deletions(-)
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
migration/cpu-throttle.c +0 -0
index 342681cdd4..06e3b1be78 100644
@@ -27,6 +27,7 @@
Quoted 8 lines

migration/cpu-throttle.c void cpu_throttle_dirty_sync_timer_tick(void

Quoted 8 lines
migration/cpu-throttle.c +1 -0
 #include "hw/core/cpu.h"
 #include "qemu/main-loop.h"
 #include "sysemu/cpus.h"
+#include "sysemu/kvm.h"
 #include "cpu-throttle.h"
 #include "migration.h"
 #include "migration-stats.h"
@@ -141,7 +142,7 @@ void cpu_throttle_dirty_sync_timer_tick(void

*opaque)

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
migration/cpu-throttle.c +1 -1
      * effect on guest performance, therefore omit it to avoid
      * paying extra for the sync penalty.
      */
-    if (sync_cnt <= 1) {
+    if (sync_cnt <= (kvm_dirty_log_manual_enabled() ? 0 : 1)) {
         goto end;
     }
 
migration/ram.c +1 -1
index d284f63854..b312ebd69d 100644
@@ -2718,7 +2718,7 @@ static void ram_list_init_bitmaps(void)
 {
     MigrationState *ms = migrate_get_current();
     RAMBlock *block;
-    unsigned long pages;
+    unsigned long pages, clear_bmap_pages;
     uint8_t shift;
 
Quoted 8 lines
Quoted 8 lines

migration/ram.c static void ram_list_init_bitmaps(void) · line 2741

Quoted 8 lines
migration/ram.c +1 -0
     /* Skip setting bitmap if there is no RAM */
@@ -2736,6 +2736,7 @@ static void ram_list_init_bitmaps(void)
 
         RAMBLOCK_FOREACH_NOT_IGNORED(block) {
             pages = block->max_length >> TARGET_PAGE_BITS;
+            clear_bmap_pages = clear_bmap_size(pages, shift);
             /*
              * The initial dirty bitmap for migration must be set

with

Quoted 2 lines
Quoted 2 lines

all

Quoted 1 line
migration/ram.c +0 -0
              * ones to make sure we'll migrate every guest RAM page

to

Quoted 9 lines
Quoted 9 lines

migration/ram.c static void ram_list_init_bitmaps(void) · line 2754

Quoted 5 lines
migration/ram.c +0 -1
@@ -2751,7 +2752,17 @@ static void ram_list_init_bitmaps(void)
                 block->file_bmap = bitmap_new(pages);
             }
             block->clear_bmap_shift = shift;
-            block->clear_bmap = bitmap_new(clear_bmap_size(pages,

shift));

Quoted 3 lines
migration/ram.c +3 -0
+            block->clear_bmap = bitmap_new(clear_bmap_pages);
+            /*
+             * Set the clear bitmap by default to enable dirty

logging.

Quoted 2 lines
Quoted 2 lines

migration/ram.c static void ram_list_init_bitmaps(void) · line 2759

Quoted 2 lines
migration/ram.c +2 -0
+             *
+             * Note that with KVM_DIRTY_LOG_INITIALLY_SET, dirty

logging

Quoted 20 lines
Quoted 7 lines

migration/ram.c static void ram_list_init_bitmaps(void) · line 2765

Quoted 6 lines
migration/ram.c +6 -0
+             * will be enabled gradually in small chunks using
+             * KVM_CLEAR_DIRTY_LOG
+             */
+            if (kvm_dirty_log_manual_enabled()) {
+                bitmap_set(block->clear_bmap, 0, clear_bmap_pages);
+            }
Quoted 5 lines

Why it needs to be relevant to whether DIRTY_LOG is enabled?

I wonder if we should always set clear_bmap to 1 unconditionally, as we always set bmap to all 1s by default.

OK, this works. We can drop it.

Quoted 3 lines

Then we skip sync always during setup, dropping patch 1.

Quoted 8 lines

IIUC, KVM initializes the slot->dirty_bitmap with 1 only when KVM_DIRTY_LOG_INITIALLY_SET is enabled, 0 otherwize. This means that if KVM does not support the KVM_DIRTY_LOG_INITIALLY_SET feature, userspace should do the first sync so that KVM could set the WP bit and clear the D-bit of the PTE.

Skipping first sync could handle this scenario?

Yes, the old kernels could be tricky (!CLEAR_LOG support), but I hope it's also working.

The thing is log_start() should also protect all pages if that's the case. For x86, that should corresponds to:

kvm_mmu_slot_apply_flags():

/*
* Initially-all-set does not require write protecting any
page,

                 * because they're all assumed to be dirty.
                 */
                if (kvm_dirty_log_manual_protect_and_init_set(kvm))
                        return;

                if (READ_ONCE(eager_page_split))
                        kvm_mmu_slot_try_split_huge_pages(kvm, new,

PG_LEVEL_4K);

Quoted 9 lines
                if (kvm_x86_ops.cpu_dirty_log_size) {
                        kvm_mmu_slot_leaf_clear_dirty(kvm, new);
                        kvm_mmu_slot_remove_write_access(kvm, new,
PG_LEVEL_2M);
                } else {
                        kvm_mmu_slot_remove_write_access(kvm, new,
PG_LEVEL_4K);
                }

This path does the write protecting indeed. Thanks for pointing this out.

Quoted 6 lines

In general, I think even if GET_DIRTY_LOG in setup() is omitted, all pages should still be wr-protected already right after log_start(). Then follow up log_clear()s will be noop, until the next sync which will reprotect every page again.

Agree. I'll drop the patch 1 and simplify the patch 2 in the next version.

Thanks for the comments.

Quoted 9 lines

Please double check.

Thanks,

--
Peter Xu

Yong

Signature · 1 line
Best regards