system/physmem: Fix migration dirty bitmap coherency with TCG memory access
v2 cover system/physmem: Fix migration dirty bitmap coherency with TCG memory access
Respin of Nicholas patch, without introducingtcg_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
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
@@ -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
@@ -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
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 ofsystem/physmem.c, expose its prototype.
Quoted 31 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/exec/exec-all.h | 1 + system/physmem.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)include/exec/exec-all.h +1
-0@@ -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@@ -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;
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]>
--- include/exec/ram_addr.h | 9 +++++++++ system/physmem.c | 8 +++----- 2 files changed, 12 insertions(+), 5 deletions(-)
include/exec/ram_addr.h +9 -0
@@ -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
@@ -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
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.
Message-ID: <[email protected]>
--- include/exec/ram_addr.h | 3 +++ 1 file changed, 3 insertions(+)
include/exec/ram_addr.h +3 -0
@@ -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
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: Factorcpu_physical_memory_dirty_bits_cleared()out
physmem: Fix migration dirty bitmap coherency with TCG memory access
Quoted 7 lines
Philippe Mathieu-Daudé (1):
physmem: Exposetlb_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
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: Factorcpu_physical_memory_dirty_bits_cleared()out
physmem: Fix migration dirty bitmap coherency with TCG memory accessPhilippe Mathieu-Daudé (1):
physmem: Exposetlb_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
Quoted 6 lines · Philippe Mathieu-Daudé
Nicholas Piggin (2):
physmem: Factorcpu_physical_memory_dirty_bits_cleared()out
physmem: Fix migration dirty bitmap coherency with TCG memory accessPhilippe Mathieu-Daudé (1):
physmem: Exposetlb_reset_dirty_range_all()
r~