migration/ram: avoid page population in ram_handle_zero via madvise
PATCH 1/1 migration/ram: avoid page population in ram_handle_zero via madvise
When the destination receives a zero page during precopy migration, ram_handle_zero() calls buffer_is_zero() which reads the page. For anonymous mmap this is benign (reads map to the shared zero page), but for memory-backend-memfd (mmap(MAP_SHARED) of a memfd) even a read commits a physical page in the tmpfs page cache.
As a result, after migration all zero pages of the guest are committed on the destination, turning a sparse RSS into a fully-populated one (see GitLab issue #2839: a 256 GB VM went from ~4 GB RSS before migration to ~256 GB after).
Add a bool can_discard parameter and call madvise(MADV_DONTNEED) when it is true. This releases tmpfs/anonymous pages back to the kernel's zero-page pool without reading the mapping at all. The madvise is issued before any read or write, preventing the initial page fault entirely.
Callers pass can_discard = !(block->flags & RAM_PREALLOC) so that backends with prealloc=on are unaffected: deliberately pre-faulted pages must not be discarded. On the destination side vCPUs are paused (RUN_STATE_INMIGRATE) while precopy pages are loaded, so madvise is race-free.
After migration for VM with 4GB RAM, the RSS on destination was reduced to 247 MB (vs 4148 MB before change), measured via VmRSS in /proc/$PID/status.
Relates-to: https://wiki.qemu.org/ToDo/LiveMigration#Avoid_page_population_when_page_is_not_populated
See-also: https://gitlab.com/qemu-project/qemu/-/issues/2839
--- migration/ram.c | 16 +++++++++++++--- migration/ram.h | 2 +- migration/rdma.c | 9 ++++++++- 3 files changed, 22 insertions(+), 5 deletions(-)
migration/ram.c +13 -3
@@ -3638,9 +3638,13 @@ static inline void *colo_cache_from_block_offset(RAMBlock *block,* * @host: host address for the zero page * @size: size of the zero page +* @can_discard: check whether RAMBlock was created with prealloc=on */ -void ram_handle_zero (void *host,uint64_t size) +void ram_handle_zero (void *host,uint64_t size,bool can_discard) { +if (can_discard &&qemu_madvise (host, size,QEMU_MADV_DONTNEED ) ==0 ) { +return ; + }if (!buffer_is_zero (host, size)) {memset (host,0 , size); }
@@ -4086,7 +4090,7 @@ static bool handle_zero_mapped_ram(RAMBlock *block, unsigned long from_bit_idx, block->idstr);return false; } -ram_handle_zero (host, size); +ram_handle_zero (host, size, !(block->flags &RAM_PREALLOC ));return true; }
@@ -4421,7 +4425,13 @@ static int ram_load_precopy(QEMUFile *f) ret = -EINVAL ;break ; } -ram_handle_zero (host,TARGET_PAGE_SIZE ); + { +ram_addr_t ram_offset; +RAMBlock *rb =qemu_ram_block_from_host (host, false, + &ram_offset); +bool can_discard = rb && !(rb->flags &RAM_PREALLOC ); +ram_handle_zero (host,TARGET_PAGE_SIZE , can_discard); + }break ;case RAM_SAVE_FLAG_PAGE :
migration/ram.h +1 -1
@@ -90,7 +90,7 @@ int ram_discard_range(const char *block_name, uint64_t start, size_t length);int ram_postcopy_incoming_init (MigrationIncomingState *mis,Error **errp);int ram_load_postcopy (QEMUFile *f,int channel); -void ram_handle_zero (void *host,uint64_t size); +void ram_handle_zero (void *host,uint64_t size,bool can_discard);void ram_transferred_add (uint64_t bytes);void ram_release_page (const char *rbname,uint64_t offset);
migration/rdma.c +8 -1
@@ -28,6 +28,7 @@#include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/module.h" +#include "system/ramblock.h" #include "qemu/rcu.h" #include "qemu/sockets.h" #include "qemu/bitmap.h"
@@ -3413,7 +3414,13 @@ int rdma_registration_handle(QEMUFile *f) comp->value); goto err; } -ram_handle_zero (host_addr, comp->length); + { +ram_addr_t ram_offset; +RAMBlock *rb =qemu_ram_block_from_host (host_addr, false, + &ram_offset); +bool can_discard = rb && !(rb->flags &RAM_PREALLOC ); +ram_handle_zero (host_addr, comp->length, can_discard); + }break ;case RDMA_CONTROL_REGISTER_FINISHED :
Signature · 1 line
2.43.0
Quoted 11 lines · Trieu Huynh
When the destination receives a zero page during precopy migration,
ram_handle_zero()callsbuffer_is_zero()which reads the page. For anonymous mmap this is benign (reads map to the shared zero page), but for memory-backend-memfd (mmap(MAP_SHARED)of a memfd) even a read commits a physical page in the tmpfs page cache.As a result, after migration all zero pages of the guest are committed on the destination, turning a sparse RSS into a fully-populated one (see GitLab issue #2839: a 256 GB VM went from ~4 GB RSS before migration to ~256 GB after).
Quoted 5 lines
Add a bool
can_discardparameter and callmadvise(MADV_DONTNEED)when it is true. This releases tmpfs/anonymous pages back to the kernel's zero-page pool without reading the mapping at all. The madvise is issued before any read or write, preventing the initial page fault entirely.
Trieu,
Thanks again for the patch.
Quoted 2 lines
Callers pass
can_discard= !(block->flags &RAM_PREALLOC) so that
I wonder if this will be enough. E.g. see ram_block_discard_range() where it special cased RAM_READONLY_FD.
Quoted 11 lines
backends with prealloc=on are unaffected: deliberately pre-faulted pages must not be discarded. On the destination side vCPUs are paused (
RUN_STATE_INMIGRATE) while precopy pages are loaded, so madvise is race-free.After migration for VM with 4GB RAM, the RSS on destination was reduced to 247 MB (vs 4148 MB before change), measured via VmRSS in
/proc/$PID/status.Relates-to: https://wiki.qemu.org/ToDo/LiveMigration#Avoid_page_population_when_page_is_not_populated
See-also: https://gitlab.com/qemu-project/qemu/-/issues/2839
Please always loop in people that were involved in the discussions. I'm adding Dan here.
Quoted 13 lines
Signed-off-by Trieu Huynh <[email protected]>--- migration/ram.c | 16 +++++++++++++--- migration/ram.h | 2 +- migration/rdma.c | 9 ++++++++- 3 files changed, 22 insertions(+), 5 deletions(-)migration/ram.c +0
-0
migration/ram.c static inline void *colo_cache_from_block_offset(RAMBlock *block, · line 3641
Quoted 4 lines
migration/ram.c +1
-0@@ -3638,9 +3638,13 @@ static inline void *colo_cache_from_block_offset(RAMBlock *block,* * @host: host address for the zero page * @size: size of the zero page +* @can_discard: check whether RAMBlock was created with prealloc=on
This is fine, but let's avoid mentioning "prealloc=on" details. It may not always be the case. As a helper function, we should normally describe what it implies when can_discard is set, not when it will be set.
migration/ram.c static inline void *colo_cache_from_block_offset(RAMBlock *block, · line 3645
Quoted 5 lines
migration/ram.c +2
-1*/ -void ram_handle_zero (void *host,uint64_t size) +void ram_handle_zero (void *host,uint64_t size,bool can_discard) { +if (can_discard &&qemu_madvise (host, size,QEMU_MADV_DONTNEED ) ==0 ) {
I also don't know if DONTNEED will always work as expected. E.g. when there're shared mappings and if the same page/folio is mapped elsewhere, DONTNEED may not be enough. Again, see ram_block_discard_range() on the cases where ramblock is shared, or fd>0.
I wonder if we should just pass in ramblock itself as parameter with offset to zero.. then we can do all the checks here and directly invoke ram_block_discard_range() when it's applicable.
Quoted 15 lines
migration/ram.c +3
-1+return ; + }if (!buffer_is_zero (host, size)) {memset (host,0 , size); }@@ -4086,7 +4090,7 @@ static bool handle_zero_mapped_ram(RAMBlock *block, unsigned long from_bit_idx, block->idstr);return false; } -ram_handle_zero (host, size); +ram_handle_zero (host, size, !(block->flags &RAM_PREALLOC ));return true; }
migration/ram.c static int ram_load_precopy(QEMUFile *f) · line 4432
Quoted 9 lines
migration/ram.c +5
-1@@ -4421,7 +4425,13 @@ static int ram_load_precopy(QEMUFile *f) ret = -EINVAL ;break ; } -ram_handle_zero (host,TARGET_PAGE_SIZE ); + { +ram_addr_t ram_offset; +RAMBlock *rb =qemu_ram_block_from_host (host, false, + &ram_offset); +bool can_discard = rb && !(rb->flags &RAM_PREALLOC );
IMHO there's no need to redo the lookup, we can extend the lifespan of the "block" variable right above when calculating the host address.
Quoted 31 lines
migration/ram.c +2
-0+ram_handle_zero (host,TARGET_PAGE_SIZE , can_discard); + }break ;case RAM_SAVE_FLAG_PAGE :migration/ram.h +1
-1@@ -90,7 +90,7 @@ int ram_discard_range(const char *block_name, uint64_t start, size_t length);int ram_postcopy_incoming_init (MigrationIncomingState *mis,Error **errp);int ram_load_postcopy (QEMUFile *f,int channel); -void ram_handle_zero (void *host,uint64_t size); +void ram_handle_zero (void *host,uint64_t size,bool can_discard);void ram_transferred_add (uint64_t bytes);void ram_release_page (const char *rbname,uint64_t offset);migration/rdma.c +1
-0@@ -28,6 +28,7 @@#include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/module.h" +#include "system/ramblock.h" #include "qemu/rcu.h" #include "qemu/sockets.h" #include "qemu/bitmap.h"
migration/rdma.c int rdma_registration_handle(QEMUFile *f) · line 3420
Quoted 8 lines
migration/rdma.c +4
-1@@ -3413,7 +3414,13 @@ int rdma_registration_handle(QEMUFile *f) comp->value); goto err; } -ram_handle_zero (host_addr, comp->length); + { +ram_addr_t ram_offset; +RAMBlock *rb =qemu_ram_block_from_host (host_addr, false, + &ram_offset);
Similar here, a slight difference is RDMA manages ramblocks with its own internal data structure, so I wonder if we want to simply cache the block pointer somewhere in qemu_rdma_init_one_block() if we want to make ram_handle_zero() take the ramblock ptr directly.
The other thing is I'm not yet sure if you have tested this code. Please keep that in mind whatever patches one posted need to be properly tested. It's definitely not good to send whatever not tested for merging. Here, this changes RDMA code path. You can have a look at our RDMA unit tests, but you'll also need to setup it. There's a helper you can use, see scripts/rdma-migration-helper.sh.
Quoted 9 lines
migration/rdma.c +3
-0+bool can_discard = rb && !(rb->flags &RAM_PREALLOC ); +ram_handle_zero (host_addr, comp->length, can_discard); + }break ;case RDMA_CONTROL_REGISTER_FINISHED :Signature · 1 line
2.43.0
Signature · 1 line
Peter Xu
Quoted 18 lines · Peter Xu
Quoted 11 lines · Trieu Huynh
When the destination receives a zero page during precopy migration,
ram_handle_zero()callsbuffer_is_zero()which reads the page. For anonymous mmap this is benign (reads map to the shared zero page), but for memory-backend-memfd (mmap(MAP_SHARED)of a memfd) even a read commits a physical page in the tmpfs page cache.As a result, after migration all zero pages of the guest are committed on the destination, turning a sparse RSS into a fully-populated one (see GitLab issue #2839: a 256 GB VM went from ~4 GB RSS before migration to ~256 GB after).
Quoted 5 lines
Add a bool
can_discardparameter and callmadvise(MADV_DONTNEED)when it is true. This releases tmpfs/anonymous pages back to the kernel's zero-page pool without reading the mapping at all. The madvise is issued before any read or write, preventing the initial page fault entirely.
Quoted 4 lines
Trieu,
Thanks again for the patch.
It's always my pleasure. Thanks.
Quoted 5 lines
Quoted 2 lines
Callers pass
can_discard= !(block->flags &RAM_PREALLOC) so thatI wonder if this will be enough. E.g. see
ram_block_discard_range()where it special casedRAM_READONLY_FD.
Worth cheking. Thanks for the point.
Quoted 10 lines
Quoted 10 lines
backends with prealloc=on are unaffected: deliberately pre-faulted pages must not be discarded. On the destination side vCPUs are paused (
RUN_STATE_INMIGRATE) while precopy pages are loaded, so madvise is race-free.After migration for VM with 4GB RAM, the RSS on destination was reduced to 247 MB (vs 4148 MB before change), measured via VmRSS in
/proc/$PID/status.
Quoted 6 lines
Quoted 2 lines
Relates-to: https://wiki.qemu.org/ToDo/LiveMigration#Avoid_page_population_when_page_is_not_populated
See-also: https://gitlab.com/qemu-project/qemu/-/issues/2839Please always loop in people that were involved in the discussions. I'm adding Dan here.
Noted, will add him and related people in v2.
Quoted 8 lines
Quoted 8 lines
Signed-off-by Trieu Huynh <[email protected]>--- migration/ram.c | 16 +++++++++++++--- migration/ram.h | 2 +- migration/rdma.c | 9 ++++++++- 3 files changed, 22 insertions(+), 5 deletions(-)
Quoted 13 lines
migration/ram.c static inline void *colo_cache_from_block_offset(RAMBlock *block, · line 3641
Quoted 9 lines
migration/ram.c +1
-0@@ -3638,9 +3638,13 @@ static inline void *colo_cache_from_block_offset(RAMBlock *block,* * @host: host address for the zero page * @size: size of the zero page +* @can_discard: check whether RAMBlock was created with prealloc=on This is fine, but let's avoid mentioning "prealloc=on" details. It may not always be the case. As a helper function, we should normally describe what it implies when
can_discardis set, not when it will be set.
Agreed. Will reword to focus on the behavior.
Quoted 12 lines
migration/ram.c static inline void *colo_cache_from_block_offset(RAMBlock *block, · line 3645
Quoted 6 lines
migration/ram.c +2
-1*/ -void ram_handle_zero (void *host,uint64_t size) +void ram_handle_zero (void *host,uint64_t size,bool can_discard) { +if (can_discard &&qemu_madvise (host, size,QEMU_MADV_DONTNEED ) ==0 ) {I also don't know if DONTNEED will always work as expected. E.g. when there're shared mappings and if the same page/folio is mapped elsewhere, DONTNEED may not be enough. Again, see
ram_block_discard_range()on the cases where ramblock is shared, or fd>0.
Quoted 3 lines
I wonder if we should just pass in ramblock itself as parameter with offset to zero.. then we can do all the checks here and directly invoke
ram_block_discard_range()when it's applicable.
That makes a lot of sense. I will refactor ram_handle_zero() to take the RAMBlock pointer and offset. This way, I can directly invoke ram_block_discard_range(), which already handles the complex edge cases.
Quoted 13 lines
Quoted 13 lines
migration/ram.c +3
-1+return ; + }if (!buffer_is_zero (host, size)) {memset (host,0 , size); }@@ -4086,7 +4090,7 @@ static bool handle_zero_mapped_ram(RAMBlock *block, unsigned long from_bit_idx, block->idstr);return false; } -ram_handle_zero (host, size); +ram_handle_zero (host, size, !(block->flags &RAM_PREALLOC ));
Quoted 15 lines
migration/ram.c static int ram_load_precopy(QEMUFile *f) · line 4432
Quoted 12 lines
migration/ram.c +5
-1return true; }@@ -4421,7 +4425,13 @@ static int ram_load_precopy(QEMUFile *f) ret = -EINVAL ;break ; } -ram_handle_zero (host,TARGET_PAGE_SIZE ); + { +ram_addr_t ram_offset; +RAMBlock *rb =qemu_ram_block_from_host (host, false, + &ram_offset); +bool can_discard = rb && !(rb->flags &RAM_PREALLOC );IMHO there's no need to redo the lookup, we can extend the lifespan of the "block" variable right above when calculating the host address.
Agreed.
Quoted 37 lines
Quoted 37 lines
migration/ram.c +2
-0+ram_handle_zero (host,TARGET_PAGE_SIZE , can_discard); + }break ;case RAM_SAVE_FLAG_PAGE :migration/ram.h +1
-1@@ -90,7 +90,7 @@ int ram_discard_range(const char *block_name, uint64_t start, size_t length);int ram_postcopy_incoming_init (MigrationIncomingState *mis,Error **errp);int ram_load_postcopy (QEMUFile *f,int channel); -void ram_handle_zero (void *host,uint64_t size); +void ram_handle_zero (void *host,uint64_t size,bool can_discard);void ram_transferred_add (uint64_t bytes);void ram_release_page (const char *rbname,uint64_t offset);migration/rdma.c +2
-1@@ -28,6 +28,7 @@#include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/module.h" +#include "system/ramblock.h" #include "qemu/rcu.h" #include "qemu/sockets.h" #include "qemu/bitmap.h" @@ -3413,7 +3414,13 @@ int rdma_registration_handle(QEMUFile *f) comp->value); goto err; } -ram_handle_zero (host_addr, comp->length); + {
Quoted 8 lines
migration/rdma.c int rdma_registration_handle(QEMUFile *f) · line 3420
Quoted 3 lines
migration/rdma.c +3
-0+ram_addr_t ram_offset; +RAMBlock *rb =qemu_ram_block_from_host (host_addr, false, + &ram_offset);Similar here, a slight difference is RDMA manages ramblocks with its own internal data structure, so I wonder if we want to simply cache the block pointer somewhere in
qemu_rdma_init_one_block()if we want to makeram_handle_zero()take the ramblock ptr directly.
Understood. I'll cache the RAMBlock pointer in the RDMA block structure during init rather than doing a lookup per zero page.
Quoted 7 lines
The other thing is I'm not yet sure if you have tested this code. Please keep that in mind whatever patches one posted need to be properly tested. It's definitely not good to send whatever not tested for merging. Here, this changes RDMA code path. You can have a look at our RDMA unit tests, but you'll also need to setup it. There's a helper you can use, see
scripts/rdma-migration-helper.sh.
You're right to be concerned. I haven't been able to test RDMA path since env.
Thanks for the point again. Will try to set up right env for validation avoid breaking that path and hopefully the patch can work with same behavior. Alternatively, I might think can split the patch so the core fix can be reviewed independently if needed.
Quoted 6 lines
Quoted 6 lines
migration/rdma.c +3
-0+bool can_discard = rb && !(rb->flags &RAM_PREALLOC ); +ram_handle_zero (host_addr, comp->length, can_discard); + }break ;
Quoted 8 lines
Quoted 4 lines
migration/rdma.c +0
-0case RDMA_CONTROL_REGISTER_FINISHED :Signature · 1 line
2.43.0Signature · 1 line
Peter Xu
Thanks for your kind review again.
BRs,
Trieu Huynh