exec: fix migration with devices that use address_space_rw
PATCH 1/1 exec: fix migration with devices that use address_space_rw
Devices that use address_space_rw to write large areas to memory (as opposed to address_space_map/unmap) were broken with respect to migration since fe680d0 (exec: Limit translation limiting in address_space_translate to xen, 2014-05-07). Such devices include IDE CD-ROMs.
The reason is that invalidate_and_set_dirty (called by address_space_rw but not address_space_map/unmap) was only setting the dirty bit for the first page in the translation.
To fix this, introduce cpu_physical_memory_set_dirty_range_nocode that is the same as cpu_physical_memory_set_dirty_range except it does not muck with the DIRTY_MEMORY_CODE bitmap. This function can be used if the caller invalidates translations with tb_invalidate_phys_page_range.
There is another difference between cpu_physical_memory_set_dirty_range and cpu_physical_memory_set_dirty_flag; the former includes a call to xen_modified_memory. This is handled separately in invalidate_and_set_dirty, and is not needed in other callers of cpu_physical_memory_set_dirty_range_nocode, so leave it alone.
Just one nit: now that invalidate_and_set_dirty takes care of handling multiple pages, there is no need for address_space_unmap to wrap it in a loop. In fact that loop would now be O(n^2).
--- exec.c | 20 ++++---------------- include/exec/ram_addr.h | 11 +++++++++++ 2 files changed, 15 insertions(+), 16 deletions(-)
exec.c +4 -16
@@ -1568,8 +1568,7 @@ static void notdirty_mem_write(void *opaque, hwaddr ram_addr,default :abort (); } -cpu_physical_memory_set_dirty_flag (ram_addr,DIRTY_MEMORY_MIGRATION ); -cpu_physical_memory_set_dirty_flag (ram_addr,DIRTY_MEMORY_VGA ); +cpu_physical_memory_set_dirty_range_nocode (ram_addr, size);/* we remove the notdirty callback only if the code has been flushed */ if (!cpu_physical_memory_is_clean (ram_addr)) {
@@ -1978,8 +1977,7 @@ static void invalidate_and_set_dirty(hwaddr addr,/* invalidate code */ tb_invalidate_phys_page_range (addr, addr + length,0 );/* set dirty bit */ -cpu_physical_memory_set_dirty_flag (addr,DIRTY_MEMORY_VGA ); -cpu_physical_memory_set_dirty_flag (addr,DIRTY_MEMORY_MIGRATION ); +cpu_physical_memory_set_dirty_range_nocode (addr, length); }xen_modified_memory (addr, length); }
@@ -2335,15 +2333,7 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, mr =qemu_ram_addr_from_host (buffer, &addr1);assert (mr !=NULL );if (is_write) { -while (access_len) { -unsigned l; - l =TARGET_PAGE_SIZE ; -if (l > access_len) - l = access_len; -invalidate_and_set_dirty (addr1, l); - addr1 += l; - access_len -= l; - } +invalidate_and_set_dirty (addr1, access_len); }if (xen_enabled ()) {xen_invalidate_map_cache_entry (buffer);
@@ -2581,9 +2571,7 @@ void stl_phys_notdirty(AddressSpace *as, hwaddr addr, uint32_t val)/* invalidate code */ tb_invalidate_phys_page_range (addr1, addr1 +4 ,0 );/* set dirty bit */ -cpu_physical_memory_set_dirty_flag (addr1, -DIRTY_MEMORY_MIGRATION ); -cpu_physical_memory_set_dirty_flag (addr1,DIRTY_MEMORY_VGA ); +cpu_physical_memory_set_dirty_range_nocode (addr1,4 ); } } }
include/exec/ram_addr.h +11 -0
@@ -71,6 +71,17 @@ static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr,set_bit (addr >>TARGET_PAGE_BITS , ram_list.dirty_memory[client]); } +static inline void cpu_physical_memory_set_dirty_range_nocode (ram_addr_t start, +ram_addr_t length) +{ +unsigned long end, page; + + end =TARGET_PAGE_ALIGN (start + length) >>TARGET_PAGE_BITS ; + page = start >>TARGET_PAGE_BITS ; +bitmap_set (ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION ], page, end - page); +bitmap_set (ram_list.dirty_memory[DIRTY_MEMORY_VGA ], page, end - page); +} +static inline void cpu_physical_memory_set_dirty_range (ram_addr_t start,ram_addr_t length) {
Signature · 1 line
1.8.3.1
Quoted 21 lines · Paolo Bonzini
Devices that use
address_space_rwto write large areas to memory (as opposed to address_space_map/unmap) were broken with respect to migration since fe680d0 (exec: Limit translation limiting inaddress_space_translateto xen, 2014-05-07). Such devices include IDE CD-ROMs.The reason is that
invalidate_and_set_dirty(called byaddress_space_rwbut not address_space_map/unmap) was only setting the dirty bit for the first page in the translation.To fix this, introduce
cpu_physical_memory_set_dirty_range_nocodethat is the same ascpu_physical_memory_set_dirty_rangeexcept it does not muck with theDIRTY_MEMORY_CODEbitmap. This function can be used if the caller invalidates translations withtb_invalidate_phys_page_range.There is another difference between
cpu_physical_memory_set_dirty_rangeandcpu_physical_memory_set_dirty_flag; the former includes a call toxen_modified_memory. This is handled separately ininvalidate_and_set_dirty, and is not needed in other callers ofcpu_physical_memory_set_dirty_range_nocode, so leave it alone.
Quoted 6 lines
Just one nit: now that
invalidate_and_set_dirtytakes care of handling multiple pages, there is no need foraddress_space_unmapto wrap it in a loop. In fact that loop would now be O(n^1).Reported-by Dave Gilbert <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>
Quoted 81 lines
--- exec.c | 20 ++++---------------- include/exec/ram_addr.h | 11 +++++++++++ 2 files changed, 15 insertions(+), 16 deletions(-)exec.c +4
-16@@ -1568,8 +1568,7 @@ static void notdirty_mem_write(void *opaque, hwaddr ram_addr,default :abort (); } -cpu_physical_memory_set_dirty_flag (ram_addr,DIRTY_MEMORY_MIGRATION ); -cpu_physical_memory_set_dirty_flag (ram_addr,DIRTY_MEMORY_VGA ); +cpu_physical_memory_set_dirty_range_nocode (ram_addr, size);/* we remove the notdirty callback only if the code has been flushed */ if (!cpu_physical_memory_is_clean (ram_addr)) {@@ -1978,8 +1977,7 @@ static void invalidate_and_set_dirty(hwaddr addr,/* invalidate code */ tb_invalidate_phys_page_range (addr, addr + length,0 );/* set dirty bit */ -cpu_physical_memory_set_dirty_flag (addr,DIRTY_MEMORY_VGA ); -cpu_physical_memory_set_dirty_flag (addr,DIRTY_MEMORY_MIGRATION ); +cpu_physical_memory_set_dirty_range_nocode (addr, length); }xen_modified_memory (addr, length); }@@ -2335,15 +2333,7 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, mr =qemu_ram_addr_from_host (buffer, &addr1);assert (mr !=NULL );if (is_write) { -while (access_len) { -unsigned l; - l =TARGET_PAGE_SIZE ; -if (l > access_len) - l = access_len; -invalidate_and_set_dirty (addr1, l); - addr1 += l; - access_len -= l; - } +invalidate_and_set_dirty (addr1, access_len); }if (xen_enabled ()) {xen_invalidate_map_cache_entry (buffer);@@ -2581,9 +2571,7 @@ void stl_phys_notdirty(AddressSpace *as, hwaddr addr, uint32_t val)/* invalidate code */ tb_invalidate_phys_page_range (addr1, addr1 +4 ,0 );/* set dirty bit */ -cpu_physical_memory_set_dirty_flag (addr1, -DIRTY_MEMORY_MIGRATION ); -cpu_physical_memory_set_dirty_flag (addr1,DIRTY_MEMORY_VGA ); +cpu_physical_memory_set_dirty_range_nocode (addr1,4 ); } } }include/exec/ram_addr.h +11
-0@@ -71,6 +71,17 @@ static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr,set_bit (addr >>TARGET_PAGE_BITS , ram_list.dirty_memory[client]); } +static inline void cpu_physical_memory_set_dirty_range_nocode (ram_addr_t start, +ram_addr_t length) +{ +unsigned long end, page; + + end =TARGET_PAGE_ALIGN (start + length) >>TARGET_PAGE_BITS ; + page = start >>TARGET_PAGE_BITS ; +bitmap_set (ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION ], page, end - page); +bitmap_set (ram_list.dirty_memory[DIRTY_MEMORY_VGA ], page, end - page); +} +static inline void cpu_physical_memory_set_dirty_range (ram_addr_t start,ram_addr_t length) {Signature · 1 line
1.8.3.1
Quoted 21 lines · Paolo Bonzini
Devices that use
address_space_rwto write large areas to memory (as opposed to address_space_map/unmap) were broken with respect to migration since fe680d0 (exec: Limit translation limiting inaddress_space_translateto xen, 2014-05-07). Such devices include IDE CD-ROMs.The reason is that
invalidate_and_set_dirty(called byaddress_space_rwbut not address_space_map/unmap) was only setting the dirty bit for the first page in the translation.To fix this, introduce
cpu_physical_memory_set_dirty_range_nocodethat is the same ascpu_physical_memory_set_dirty_rangeexcept it does not muck with theDIRTY_MEMORY_CODEbitmap. This function can be used if the caller invalidates translations withtb_invalidate_phys_page_range.There is another difference between
cpu_physical_memory_set_dirty_rangeandcpu_physical_memory_set_dirty_flag; the former includes a call toxen_modified_memory. This is handled separately ininvalidate_and_set_dirty, and is not needed in other callers ofcpu_physical_memory_set_dirty_range_nocode, so leave it alone.
Quoted 6 lines
Just one nit: now that
invalidate_and_set_dirtytakes care of handling multiple pages, there is no need foraddress_space_unmapto wrap it in a loop. In fact that loop would now be O(n^2).Reported-by Dave Gilbert <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>
Quoted 21 lines · Paolo Bonzini
Devices that use
address_space_rwto write large areas to memory (as opposed to address_space_map/unmap) were broken with respect to migration since fe680d0 (exec: Limit translation limiting inaddress_space_translateto xen, 2014-05-07). Such devices include IDE CD-ROMs.The reason is that
invalidate_and_set_dirty(called byaddress_space_rwbut not address_space_map/unmap) was only setting the dirty bit for the first page in the translation.To fix this, introduce
cpu_physical_memory_set_dirty_range_nocodethat is the same ascpu_physical_memory_set_dirty_rangeexcept it does not muck with theDIRTY_MEMORY_CODEbitmap. This function can be used if the caller invalidates translations withtb_invalidate_phys_page_range.There is another difference between
cpu_physical_memory_set_dirty_rangeandcpu_physical_memory_set_dirty_flag; the former includes a call toxen_modified_memory. This is handled separately ininvalidate_and_set_dirty, and is not needed in other callers ofcpu_physical_memory_set_dirty_range_nocode, so leave it alone.
Quoted 6 lines
Just one nit: now that
invalidate_and_set_dirtytakes care of handling multiple pages, there is no need foraddress_space_unmapto wrap it in a loop. In fact that loop would now be O(n^2).Reported-by Dave Gilbert <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>
Paolo, are you doing the pull for this, or should I do it?
Thanks, Juan.