kvm: Avoid COW if KVM MMU is asynchronous
PATCH 1/1 kvm: Avoid COW if KVM MMU is asynchronous
If the KVM MMU is asynchronous (kernel does not support MMU_NOTIFIER), we have to avoid COW for the guest memory. Otherwise we risk serious breakage when guest pages change there physical locations due to COW after fork. Seen when forking smbd during runtime via -smb.
---
exec.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
exec.c +14 -0
@@ -2477,6 +2477,20 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size) new_block->next = ram_blocks; ram_blocks = new_block; +if (kvm_enabled () && !kvm_has_sync_mmu ()) { +#ifdef MADV_DONTFORK +int ret =madvise (new_block->host, size,MADV_DONTFORK ); +if (ret) { +perror ("madvice" ); +exit (1 ); + } +#else +fprintf (stderr, +"Need MADV_DONTFORK in absence of synchronous KVM MMU\n" ); +exit (1 ); +#endif + } + phys_ram_dirty =qemu_realloc (phys_ram_dirty, (last_ram_offset + size) >>TARGET_PAGE_BITS ); memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS),
Quoted 23 lines · Jan Kiszka
If the KVM MMU is asynchronous (kernel does not support
MMU_NOTIFIER), we have to avoid COW for the guest memory. Otherwise we risk serious breakage when guest pages change there physical locations due to COW after fork. Seen when forking smbd during runtime via -smb.Signed-off-by Jan Kiszka <[email protected]>---
exec.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)exec.c +4
-0@@ -2477,6 +2477,20 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size) new_block->next = ram_blocks; ram_blocks = new_block; +if (kvm_enabled () && !kvm_has_sync_mmu ()) { +#ifdef MADV_DONTFORK +int ret =madvise (new_block->host, size,MADV_DONTFORK ); +if (ret) {
exec.c ram_addr_t qemu_ram_alloc(ram_addr_t size) · line 2493
Quoted 10 lines
exec.c +10
-0+perror ("madvice" ); + exit(1); + } +#else +fprintf (stderr, +"Need MADV_DONTFORK in absence of synchronous KVM MMU\n" ); +exit (1 ); +#endif + } +
Suggest wrapping in a function and hiding it deep inside kvm-all.c.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH v2 1/1 kvm: Avoid COW if KVM MMU is asynchronous
Quoted 2 lines · Avi Kivity
Suggest wrapping in a function and hiding it deep inside
kvm-all.c.
Done in v2:
---------->
If the KVM MMU is asynchronous (kernel does not support MMU_NOTIFIER), we have to avoid COW for the guest memory. Otherwise we risk serious breakage when guest pages change there physical locations due to COW after fork. Seen when forking smbd during runtime via -smb.
---
exec.c | 3 +++ kvm-all.c | 18 ++++++++++++++++++ kvm.h | 2 ++ 3 files changed, 23 insertions(+), 0 deletions(-)
exec.c +3 -0
@@ -2484,6 +2484,9 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size) last_ram_offset += size; +if (kvm_enabled ()) +kvm_setup_guest_memory (new_block->host, size); +return new_block->offset; }
kvm-all.c +18 -0
@@ -783,6 +783,24 @@ int kvm_has_sync_mmu(void)return 0 ; } +void kvm_setup_guest_memory (void *start,size_t size) +{ +if (!kvm_has_sync_mmu ()) { +#ifdef MADV_DONTFORK +int ret =madvise (start, size,MADV_DONTFORK ); + +if (ret) { +perror ("madvice" ); +exit (1 ); + } +#else +fprintf (stderr, +"Need MADV_DONTFORK in absence of synchronous KVM MMU\n" ); +exit (1 ); +#endif + } +} +#ifdef KVM_CAP_SET_GUEST_DEBUG struct kvm_sw_breakpoint *kvm_find_sw_breakpoint (CPUState *env,target_ulong pc)
kvm.h +2 -0
@@ -48,6 +48,8 @@ int kvm_log_stop(target_phys_addr_t phys_addr, ram_addr_t size);int kvm_has_sync_mmu (void ); +void kvm_setup_guest_memory (void *start,size_t size); +int kvm_coalesce_mmio_region (target_phys_addr_t start,ram_addr_t size);int kvm_uncoalesce_mmio_region (target_phys_addr_t start,ram_addr_t size);
Quoted 9 lines · Jan Kiszka
If the KVM MMU is asynchronous (kernel does not support
MMU_NOTIFIER), we have to avoid COW for the guest memory. Otherwise we risk serious breakage when guest pages change there physical locations due to COW after fork. Seen when forking smbd during runtime via -smb.last_ram_offset += size;
exec.c ram_addr_t qemu_ram_alloc(ram_addr_t size) · line 2491
Quoted 5 lines
exec.c +3
-0+if (kvm_enabled ()) +kvm_setup_guest_memory (new_block->host, size); +return new_block->offset; }
I feel a little bad about picking such small nits, but the if statement requires braces.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH v3 1/1 kvm: Avoid COW if KVM MMU is asynchronous
Quoted 16 lines · Avi Kivity
Quoted 9 lines · Jan Kiszka
If the KVM MMU is asynchronous (kernel does not support
MMU_NOTIFIER), we have to avoid COW for the guest memory. Otherwise we risk serious breakage when guest pages change there physical locations due to COW after fork. Seen when forking smbd during runtime via -smb.last_ram_offset += size;Review
Quoted 5 lines
(quoted diff) +3
-0+ if (kvm_enabled()) + kvm_setup_guest_memory(new_block->host, size); + return new_block->offset; }
Quoted 3 lines
I feel a little bad about picking such small nits, but the if statement requires braces.
No problem. I'm in "trivial editing mode" ATM anyway.
Thanks,
Jan
---------->
If the KVM MMU is asynchronous (kernel does not support MMU_NOTIFIER), we have to avoid COW for the guest memory. Otherwise we risk serious breakage when guest pages change there physical locations due to COW after fork. Seen when forking smbd during runtime via -smb.
---
exec.c | 3 +++ kvm-all.c | 18 ++++++++++++++++++ kvm.h | 2 ++ 3 files changed, 23 insertions(+), 0 deletions(-)
exec.c +3 -0
@@ -2484,6 +2484,9 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size) last_ram_offset += size; +if (kvm_enabled ()) { +kvm_setup_guest_memory (new_block->host, size); + }return new_block->offset; }
kvm-all.c +18 -0
@@ -783,6 +783,24 @@ int kvm_has_sync_mmu(void)return 0 ; } +void kvm_setup_guest_memory (void *start,size_t size) +{ +if (!kvm_has_sync_mmu ()) { +#ifdef MADV_DONTFORK +int ret =madvise (start, size,MADV_DONTFORK ); + +if (ret) { +perror ("madvice" ); +exit (1 ); + } +#else +fprintf (stderr, +"Need MADV_DONTFORK in absence of synchronous KVM MMU\n" ); +exit (1 ); +#endif + } +} +#ifdef KVM_CAP_SET_GUEST_DEBUG struct kvm_sw_breakpoint *kvm_find_sw_breakpoint (CPUState *env,target_ulong pc)
kvm.h +2 -0
@@ -48,6 +48,8 @@ int kvm_log_stop(target_phys_addr_t phys_addr, ram_addr_t size);int kvm_has_sync_mmu (void ); +void kvm_setup_guest_memory (void *start,size_t size); +int kvm_coalesce_mmio_region (target_phys_addr_t start,ram_addr_t size);int kvm_uncoalesce_mmio_region (target_phys_addr_t start,ram_addr_t size);