@samitouri / QOSamiQemu / commits / 3d0ad30fbf

target/arm/hvf, target/i386/hvf: Pass MR-relative offset to memory_region_set_dirty()

Both the arm and i386 hvf accelerators have the same bug in their dirty-page logging path: the address fed to memory_region_set_dirty() is computed as "<ipa,gpa>_page + xlat", but memory_region_set_dirty() expects an offset relative to the start of the resolved MemoryRegion. address_space_translate() already returns that offset in xlat, while ipa_page / gpa_page is the guest-physical (system address space) address. Adding the two together produces a bogus offset that for any non- trivial RAM size walks well past the end of the MemoryRegion's dirty bitmap. With dirty logging active (e.g. live migration on a guest with several GB of RAM), this triggers an out-of-bounds atomic write inside bitmap_set_atomic() and crashes the source QEMU as soon as the guest writes to RAM: Thread .. 'CPU N/HVF', stop reason = EXC_BAD_ACCESS ... bitmap_set_atomic at bitmap.c:213 physical_memory_set_dirty_range at physmem.c:1038 memory_region_set_dirty at memory.c:2191 hvf_handle_exception at hvf.c Fix it by passing only the MR-relative offset xlat. ipa_page / gpa_page is still the right argument to hvf_unprotect_dirty_range(), which works on the guest-physical address space. Signed-off-by: Scott J. Goldman <scottjgo@gmail.com> Link: https://lore.kernel.org/r/20260427232116.50586-2-scottjgo@gmail.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Scott J. Goldman committed Apr 27, 2026 at 16:21 UTC 3d0ad30fbf4f3dc711bf07293a8c270e7e94994e
2 files changed +2 -2
target/arm/hvf/hvf.c
+1 -1
@@ -2159,7 +2159,7 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
2159 assert(!mr->readonly);
2160
2161 if (memory_region_get_dirty_log_mask(mr)) {
2162 - memory_region_set_dirty(mr, ipa_page + xlat, page_size);
2162 + memory_region_set_dirty(mr, xlat, page_size);
2163 hvf_unprotect_dirty_range(ipa_page, page_size);
2164 }
2165
target/i386/hvf/hvf.c
+1 -1
@@ -146,7 +146,7 @@ static bool ept_emulation_fault(CPUState *cs, uint64_t gpa, uint64_t ept_qual)
146 if (write && memory_region_get_dirty_log_mask(mr)) {
147 uintptr_t page_size = qemu_real_host_page_size();
148
149 - memory_region_set_dirty(mr, gpa_page + xlat, page_size);
149 + memory_region_set_dirty(mr, xlat, page_size);
150 hvf_unprotect_dirty_range(gpa_page, page_size);
151 }
152