Skip to contentSkip to conversation

system/physmem: Fix migration dirty bitmap coherency with TCG memory access

PATCH In review Philippe Mathieu-Daudé 4 participants 8 messages Mar 12, 2024 – Mar 13, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover system/physmem: Fix migration dirty bitmap coherency with TCG memory access

Respin of Nicholas patch, without introducing
tcg_cpu_physical_memory_dirty_bits_cleared(),
and split in more digestible parts.

Nicholas Piggin (2):
physmem: Factor cpu_physical_memory_dirty_bits_cleared() out
physmem: Fix migration dirty bitmap coherency with TCG memory access

Philippe Mathieu-Daudé (1):
physmem: Expose tlb_reset_dirty_range_all()

 include/exec/exec-all.h |  1 +
 include/exec/ram_addr.h | 12 ++++++++++++
 system/physmem.c        | 10 ++++------
 3 files changed, 17 insertions(+), 6 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/3 physmem: Expose tlb_reset_dirty_range_all()

In order to call tlb_reset_dirty_range_all() outside of system/physmem.c, expose its prototype.

---
 include/exec/exec-all.h | 1 +
 system/physmem.c        | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
include/exec/exec-all.h +1 -0
index ce36bb10d4..3e53501691 100644
@@ -655,6 +655,7 @@ static inline void mmap_unlock(void) {}
 
 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length);
 void tlb_set_dirty(CPUState *cpu, vaddr addr);
+void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length);
 
 MemoryRegionSection *
 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
system/physmem.c +1 -1
index 6cfb7a80ab..5441480ff0 100644
@@ -819,7 +819,7 @@ found:
     return block;
 }
 
-static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
+void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
 {
     CPUState *cpu;
     ram_addr_t start1;
Signature · 1 line
2.41.0
Acked-by on v2 1/3
#2 lore raw

Re: [PATCH-for-9.0 v2 1/3] physmem: Expose tlb_reset_dirty_range_all()

Quoted 3 lines · Philippe Mathieu-Daudé

In order to call tlb_reset_dirty_range_all() outside of system/physmem.c, expose its prototype.

Quoted 31 lines
---
 include/exec/exec-all.h | 1 +
 system/physmem.c        | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
include/exec/exec-all.h +1 -0
index ce36bb10d4..3e53501691 100644
@@ -655,6 +655,7 @@ static inline void mmap_unlock(void) {}
 
 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length);
 void tlb_set_dirty(CPUState *cpu, vaddr addr);
+void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length);
 
 MemoryRegionSection *
 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
system/physmem.c +1 -1
index 6cfb7a80ab..5441480ff0 100644
@@ -819,7 +819,7 @@ found:
     return block;
 }
 
-static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
+void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
 {
     CPUState *cpu;
     ram_addr_t start1;
#3 lore raw

PATCH v2 2/3 physmem: Factor cpu_physical_memory_dirty_bits_cleared() out

In reply to Philippe Mathieu-Daudé

From: Nicholas Piggin <[email protected]>

Message-ID: <[email protected]>

[PMD: Split patch in 2: part 1/2]
---
 include/exec/ram_addr.h | 9 +++++++++
 system/physmem.c        | 8 +++-----
 2 files changed, 12 insertions(+), 5 deletions(-)
include/exec/ram_addr.h +9 -0
index 90676093f5..b060ea9176 100644
@@ -25,6 +25,7 @@
 #include "sysemu/tcg.h"
 #include "exec/ramlist.h"
 #include "exec/ramblock.h"
+#include "exec/exec-all.h"
 
 extern uint64_t total_dirty_pages;
 
@@ -443,6 +444,14 @@ uint64_t cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap,
 }
 #endif /* not _WIN32 */
 
+static inline void cpu_physical_memory_dirty_bits_cleared(ram_addr_t start,
+                                                          ram_addr_t length)
+{
+    if (tcg_enabled()) {
+        tlb_reset_dirty_range_all(start, length);
+    }
+
+}
 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
                                               ram_addr_t length,
                                               unsigned client);
system/physmem.c +3 -5
index 5441480ff0..a4fe3d2bf8 100644
@@ -881,8 +881,8 @@ bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
         memory_region_clear_dirty_bitmap(ramblock->mr, mr_offset, mr_size);
     }
 
-    if (dirty && tcg_enabled()) {
-        tlb_reset_dirty_range_all(start, length);
+    if (dirty) {
+        cpu_physical_memory_dirty_bits_cleared(start, length);
     }
 
     return dirty;
@@ -929,9 +929,7 @@ DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty
         }
     }
 
-    if (tcg_enabled()) {
-        tlb_reset_dirty_range_all(start, length);
-    }
+    cpu_physical_memory_dirty_bits_cleared(start, length);
 
     memory_region_clear_dirty_bitmap(mr, offset, length);
 
Signature · 1 line
2.41.0
#4 lore raw

PATCH v2 3/3 physmem: Fix migration dirty bitmap coherency with TCG memory access

In reply to Philippe Mathieu-Daudé

From: Nicholas Piggin <[email protected]>

The fastpath in cpu_physical_memory_sync_dirty_bitmap() to test large aligned ranges forgot to bring the TCG TLB up to date after clearing some of the dirty memory bitmap bits. This can result in stores though the TCG TLB not setting the dirty memory bitmap and ultimately causes memory corruption / lost updates during migration from a TCG host.

Fix this by calling cpu_physical_memory_dirty_bits_cleared() when dirty bits have been cleared.

Fixes aa8dc044772 ("migration: synchronize memory bitmap 64bits at a time")

Message-ID: <[email protected]>

[PMD: Split patch in 2: part 2/2, slightly adapt description]
---
 include/exec/ram_addr.h | 3 +++
 1 file changed, 3 insertions(+)
include/exec/ram_addr.h +3 -0
index b060ea9176..de45ba7bc9 100644
@@ -513,6 +513,9 @@ uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlock *rb,
                 idx++;
             }
         }
+        if (num_dirty) {
+            cpu_physical_memory_dirty_bits_cleared(start, length);
+        }
 
         if (rb->clear_bmap) {
             /*
Signature · 1 line
2.41.0
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé

Respin of Nicholas patch, without introducing
tcg_cpu_physical_memory_dirty_bits_cleared(),
and split in more digestible parts.

Nicholas Piggin (2):
physmem: Factor cpu_physical_memory_dirty_bits_cleared() out
physmem: Fix migration dirty bitmap coherency with TCG memory access

Quoted 7 lines

Philippe Mathieu-Daudé (1):
physmem: Expose tlb_reset_dirty_range_all()

 include/exec/exec-all.h |  1 +
 include/exec/ram_addr.h | 12 ++++++++++++
 system/physmem.c        | 10 ++++------
 3 files changed, 17 insertions(+), 6 deletions(-)

Yes agree a better split than the single patch. Tentatively queued while waiting for any comments.

Signature · 1 line
Peter Xu
#6 lore raw
Quoted 12 lines · Peter Xu
Quoted 11 lines · Philippe Mathieu-Daudé

Respin of Nicholas patch, without introducing
tcg_cpu_physical_memory_dirty_bits_cleared(),
and split in more digestible parts.

Nicholas Piggin (2):
physmem: Factor cpu_physical_memory_dirty_bits_cleared() out
physmem: Fix migration dirty bitmap coherency with TCG memory access

Philippe Mathieu-Daudé (1):
physmem: Expose tlb_reset_dirty_range_all()

Quoted 7 lines
Quoted 4 lines
 include/exec/exec-all.h |  1 +
 include/exec/ram_addr.h | 12 ++++++++++++
 system/physmem.c        | 10 ++++------
 3 files changed, 17 insertions(+), 6 deletions(-)

Yes agree a better split than the single patch. Tentatively queued while waiting for any comments.

I've run into several other possible races / lost dirty tracking when stressing this stuff, but this one was the easiest to hit and most obvious and simple fix, so I think it's still good to go.

Also have a qtest test case that can reproduce this one so I'll send that after this is merged. It's really just the migration test case with value verification added in.

Thanks,
Nick