add MADV_DONTFORK to guest physical memory
PATCH 1/1 add MADV_DONTFORK to guest physical memory
From: Andrea Arcangeli <[email protected]>
All allocated guest physical memory shall be marked MADV_DONTFORK, otherwise fork will fail because of accounting issues preventing migration or netdev_add when the guest allocated more than half of host physical memory.
---
exec.c +3 -0
@@ -2861,6 +2861,9 @@ ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,#ifdef MADV_MERGEABLE madvise (new_block->host, size,MADV_MERGEABLE );#endif +#ifdef MADV_DONTFORK +madvise (new_block->host, size,MADV_DONTFORK ); +#endif } }
Quoted 7 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
All allocated guest physical memory shall be marked
MADV_DONTFORK, otherwise fork will fail because of accounting issues preventing migration ornetdev_addwhen the guest allocated more than half of host physical memory.Signed-off-by Andrea Arcangeli <[email protected]>
The madvise logic is just about to get changed now. Please look at Andreas' patches and coordinate with him there.
Alex
Am 15.09.2010 um 19:08 schrieb Andrea Arcangeli:
Quoted 14 lines
From: Andrea Arcangeli <[email protected]>
All allocated guest physical memory shall be marked
MADV_DONTFORK, otherwise fork will fail because of accounting issues preventing migration ornetdev_addwhen the guest allocated more than half of host physical memory.Signed-off-by Andrea Arcangeli <[email protected]>---
exec.c +0
-0
exec.c ram_addr_t qemu_ram_alloc_from_ptr(DeviceState · line 2867
Quoted 7 lines
exec.c +3
-0@@ -2861,6 +2861,9 @@ ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,#ifdef MADV_MERGEABLE madvise (new_block->host, size,MADV_MERGEABLE );#endif +#ifdef MADV_DONTFORK +madvise (new_block->host, size,MADV_DONTFORK ); +#endif
There is a pending patch which introduces qemu_madvise() and will eliminate the need for these #ifdefs, which was demanded by Blue. I'll cc you on v6.
Regards,
Andreas
Quoted 14 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
All allocated guest physical memory shall be marked
MADV_DONTFORK, otherwise fork will fail because of accounting issues preventing migration ornetdev_addwhen the guest allocated more than half of host physical memory.Signed-off-by Andrea Arcangeli <[email protected]>---
exec.c +0
-0
exec.c ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name, · line 2868
Quoted 8 lines
exec.c +3
-0@@ -2861,6 +2861,9 @@ ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,#ifdef MADV_MERGEABLE madvise (new_block->host, size,MADV_MERGEABLE );#endif +#ifdef MADV_DONTFORK +madvise (new_block->host, size,MADV_DONTFORK ); +#endif } }
Is this always a number of full host pages? If not, we'd get trouble when trying to call helpers.
Signature · 1 line
MST
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
All allocated guest physical memory shall be marked
MADV_DONTFORK, otherwise fork will fail because of accounting issues preventing migration ornetdev_addwhen the guest allocated more than half of host physical memory.Signed-off-by Andrea Arcangeli <[email protected]>---
Quoted 17 lines
exec.c ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name, · line 2868
Quoted 13 lines
exec.c +3
-0@@ -2861,6 +2861,9 @@ ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,#ifdef MADV_MERGEABLE madvise (new_block->host, size,MADV_MERGEABLE );#endif +#ifdef MADV_DONTFORK +madvise (new_block->host, size,MADV_DONTFORK ); +#endif } }Is this always a number of full host pages? If not, we'd get trouble when trying to call helpers.
How it can be not a number of full host pages? This is guest memory.
--
Gleb.
The bug is still there so I rediffed the old patch against current code.
On a related topic: could somebody give me advice on how to implement a command line (command line seems enough, the other option would be monitor command) to make the MADV_MERGEABLE conditional? I got KSM on THP working fine but KSM may decrease performance by increasing the number of copy on write and by splitting hugepages, so we'd like to be able to turn off KSM on a per-VM basis (not on the whole host, which of course we already can by setting /sys/kernel/mm/ksm/run to 0) so that high perf VMs will keep running at maximum speed with KSM off but others may still benefit from KSM. For that I need to make the below MADV_MERGEABLE madvise conditional to something and the code itself will be trivial, we've just to converge on a command line option (hopefully quickly ;).
======
Subject: avoid allocation failures during fork/exec for migrate/hotplug
From: Andrea Arcangeli <[email protected]>
Mark all guest physical memory as MADV_DONTFORK to avoid false positive allocation failures during fork in migrate/netdev hotplug.
---
exec.c +3 -0
@@ -2851,6 +2851,9 @@ ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name, new_block->host =qemu_vmalloc (size);#endif qemu_madvise (new_block->host, size,QEMU_MADV_MERGEABLE ); + +/* no allocation failures during fork/exec for migrate/hotplug */ +qemu_madvise (new_block->host, size,QEMU_MADV_DONTFORK ); } }
Quoted 15 lines · Andrea Arcangeli
The bug is still there so I rediffed the old patch against current code.
On a related topic: could somebody give me advice on how to implement a command line (command line seems enough, the other option would be monitor command) to make the
MADV_MERGEABLEconditional? I got KSM on THP working fine but KSM may decrease performance by increasing the number of copy on write and by splitting hugepages, so we'd like to be able to turn off KSM on a per-VM basis (not on the whole host, which of course we already can by setting/sys/kernel/mm/ksm/runto 0) so that high perf VMs will keep running at maximum speed with KSM off but others may still benefit from KSM. For that I need to make the belowMADV_MERGEABLEmadvise conditional to something and the code itself will be trivial, we've just to converge on a command line option (hopefully quickly ;).
There was a -mem_prealloc option added a while back to set MAP_POPULATE on memory mapped in via the -mem-path option. So an analogous -mem_nomerge option or something along that line seems reasonable for conditionally unsetting QEMU_MADV_MERGEABLE.
And for consistency you should probably make both your proposed changes for -mem-path'd memory as well.
Quoted 17 lines · Michael Roth
Quoted 15 lines · Andrea Arcangeli
The bug is still there so I rediffed the old patch against current code.
On a related topic: could somebody give me advice on how to implement a command line (command line seems enough, the other option would be monitor command) to make the
MADV_MERGEABLEconditional? I got KSM on THP working fine but KSM may decrease performance by increasing the number of copy on write and by splitting hugepages, so we'd like to be able to turn off KSM on a per-VM basis (not on the whole host, which of course we already can by setting/sys/kernel/mm/ksm/runto 0) so that high perf VMs will keep running at maximum speed with KSM off but others may still benefit from KSM. For that I need to make the belowMADV_MERGEABLEmadvise conditional to something and the code itself will be trivial, we've just to converge on a command line option (hopefully quickly ;).
Quoted 3 lines
There was a -mem_prealloc option added a while back to set
MAP_POPULATEon memory mapped in via the -mem-path option. So an analogous -mem_nomerge option or something along that line seems reasonable for conditionally unsettingQEMU_MADV_MERGEABLE.And for consistency you should probably make both your proposed changes for -mem-path'd memory as well.
Why not clean up all that mess and introduce a new -mem option that would just take all of the several options as parameters?
-mem size=512,populate=on,ksm=off
and default -m to something reasonable with the new syntax.
Alex
Hello everyone,
Quoted 22 lines · Alexander Graf
Quoted 19 lines · Michael Roth
Quoted 15 lines · Andrea Arcangeli
The bug is still there so I rediffed the old patch against current code.
On a related topic: could somebody give me advice on how to implement a command line (command line seems enough, the other option would be monitor command) to make the
MADV_MERGEABLEconditional? I got KSM on THP working fine but KSM may decrease performance by increasing the number of copy on write and by splitting hugepages, so we'd like to be able to turn off KSM on a per-VM basis (not on the whole host, which of course we already can by setting/sys/kernel/mm/ksm/runto 0) so that high perf VMs will keep running at maximum speed with KSM off but others may still benefit from KSM. For that I need to make the belowMADV_MERGEABLEmadvise conditional to something and the code itself will be trivial, we've just to converge on a command line option (hopefully quickly ;).There was a -mem_prealloc option added a while back to set
MAP_POPULATEon memory mapped in via the -mem-path option. So an analogous -mem_nomerge option or something along that line seems reasonable for conditionally unsettingQEMU_MADV_MERGEABLE.
Quoted 7 lines
Quoted 1 line
And for consistency you should probably make both your proposed changes for -mem-path'd memory as well.
Why not clean up all that mess and introduce a new -mem option that would just take all of the several options as parameters?
-mem size=512,populate=on,ksm=off
and default -m to something reasonable with the new syntax.
I'm neutral... so feel free to decide what I should implement ;).
One comment on combining -m ksm=off (or -mem_nomerge) with -mem-path. It seems unnecessary because ksm can't be turned on on VM_HUGETLB vmas (MADV_MERGEABLE will return -EINVAL) and mem-path only makes sense if used in combination with hugetlbfs (which sets VM_HUGETLB of course).
Quoted 33 lines · Andrea Arcangeli
Hello everyone,
Quoted 22 lines · Alexander Graf
Quoted 19 lines · Michael Roth
Quoted 15 lines · Andrea Arcangeli
The bug is still there so I rediffed the old patch against current code.
On a related topic: could somebody give me advice on how to implement a command line (command line seems enough, the other option would be monitor command) to make the
MADV_MERGEABLEconditional? I got KSM on THP working fine but KSM may decrease performance by increasing the number of copy on write and by splitting hugepages, so we'd like to be able to turn off KSM on a per-VM basis (not on the whole host, which of course we already can by setting/sys/kernel/mm/ksm/runto 0) so that high perf VMs will keep running at maximum speed with KSM off but others may still benefit from KSM. For that I need to make the belowMADV_MERGEABLEmadvise conditional to something and the code itself will be trivial, we've just to converge on a command line option (hopefully quickly ;).There was a -mem_prealloc option added a while back to set
MAP_POPULATEon memory mapped in via the -mem-path option. So an analogous -mem_nomerge option or something along that line seems reasonable for conditionally unsettingQEMU_MADV_MERGEABLE.Quoted 7 lines
Quoted 1 line
And for consistency you should probably make both your proposed changes for -mem-path'd memory as well.
Why not clean up all that mess and introduce a new -mem option that would just take all of the several options as parameters?
-mem size=512,populate=on,ksm=off
and default -m to something reasonable with the new syntax.
Quoted 7 lines
I'm neutral... so feel free to decide what I should implement ;).
One comment on combining -m ksm=off (or -mem_nomerge) with -mem-path. It seems unnecessary because ksm can't be turned on on
VM_HUGETLBvmas (MADV_MERGEABLEwill return-EINVAL) and mem-path only makes sense if used in combination with hugetlbfs (which setsVM_HUGETLBof course).
Sure, not all combinations make sense. But "-mem size=1G,path=/dev/shm/vm1.ram,populate=on" would make sense, no? TPH should go along the same lines here too. It'd just be a flag "tph" that defaults to on if available.
That way we could also do all the sanity checks in a single place. I really like the idea of combining memory management command line parameters into a single option :). In the end I'd assume it's Anthony's call though.
Alex
Quoted 23 lines · Alexander Graf
Quoted 20 lines · Michael Roth
Quoted 16 lines · Andrea Arcangeli
The bug is still there so I rediffed the old patch against current code.
On a related topic: could somebody give me advice on how to implement a command line (command line seems enough, the other option would be monitor command) to make the
MADV_MERGEABLEconditional? I got KSM on THP working fine but KSM may decrease performance by increasing the number of copy on write and by splitting hugepages, so we'd like to be able to turn off KSM on a per-VM basis (not on the whole host, which of course we already can by setting/sys/kernel/mm/ksm/runto 0) so that high perf VMs will keep running at maximum speed with KSM off but others may still benefit from KSM. For that I need to make the belowMADV_MERGEABLEmadvise conditional to something and the code itself will be trivial, we've just to converge on a command line option (hopefully quickly ;).There was a -mem_prealloc option added a while back to set
MAP_POPULATEon memory mapped in via the -mem-path option. So an analogous -mem_nomerge option or something along that line seems reasonable for conditionally unsettingQEMU_MADV_MERGEABLE.
Quoted 8 lines
Quoted 2 lines
And for consistency you should probably make both your proposed changes for -mem-path'd memory as well.
Why not clean up all that mess and introduce a new -mem option that would just take all of the several options as parameters?
-mem size=512,populate=on,ksm=off
and default -m to something reasonable with the new syntax.
Yeah, that does make sense. Maybe we should consider folding it in with the -numa option too?
Regards,
Anthony Liguori
Quoted 4 lines
Alex
Quoted 46 lines · Alexander Graf
Quoted 35 lines · Andrea Arcangeli
Hello everyone,
Quoted 23 lines · Alexander Graf
Quoted 20 lines · Michael Roth
Quoted 16 lines · Andrea Arcangeli
The bug is still there so I rediffed the old patch against current code.
On a related topic: could somebody give me advice on how to implement a command line (command line seems enough, the other option would be monitor command) to make the
MADV_MERGEABLEconditional? I got KSM on THP working fine but KSM may decrease performance by increasing the number of copy on write and by splitting hugepages, so we'd like to be able to turn off KSM on a per-VM basis (not on the whole host, which of course we already can by setting/sys/kernel/mm/ksm/runto 0) so that high perf VMs will keep running at maximum speed with KSM off but others may still benefit from KSM. For that I need to make the belowMADV_MERGEABLEmadvise conditional to something and the code itself will be trivial, we've just to converge on a command line option (hopefully quickly ;).There was a -mem_prealloc option added a while back to set
MAP_POPULATEon memory mapped in via the -mem-path option. So an analogous -mem_nomerge option or something along that line seems reasonable for conditionally unsettingQEMU_MADV_MERGEABLE.Quoted 8 lines
Quoted 2 lines
And for consistency you should probably make both your proposed changes for -mem-path'd memory as well.
Why not clean up all that mess and introduce a new -mem option that would just take all of the several options as parameters?
-mem size=512,populate=on,ksm=off
and default -m to something reasonable with the new syntax.
Quoted 8 lines
I'm neutral... so feel free to decide what I should implement ;).
One comment on combining -m ksm=off (or -mem_nomerge) with -mem-path. It seems unnecessary because ksm can't be turned on on
VM_HUGETLBvmas (MADV_MERGEABLEwill return-EINVAL) and mem-path only makes sense if used in combination with hugetlbfs (which setsVM_HUGETLBof course).
Quoted 4 lines
Sure, not all combinations make sense. But "-mem size=1G,path=/dev/shm/vm1.ram,populate=on" would make sense, no? TPH should go along the same lines here too. It'd just be a flag "tph" that defaults to on if available.
That way we could also do all the sanity checks in a single place. I really like the idea of combining memory management command line parameters into a single option :). In the end I'd assume it's Anthony's call though.
Where it's really helpful is in the ever elusive configuration file format. -mem becomes:
[mem]
size=1G
path=/dev/shm/vm1.ram
populate=on
Which is nice from a grouping perspective.
Regards,
Anthony Liguori
Quoted 3 lines
Alex
Quoted 2 lines · Alexander Graf
Sure, not all combinations make sense. But "-mem size=1G,path=/dev/shm/vm1.ram,populate=on" would make sense, no? TPH
I was referring to Michael's suggestion, sure many options make sense for mem-path too, in fact populate makes more sense for mem-path (considering if hugetlbfs allocation fails guest may die if libhugetlbfs isn't trapping the -ENOMEM and doing munmap creating hole and replacing hole with regular anoymous memory).
Quoted 2 lines
should go along the same lines here too. It'd just be a flag "tph" that defaults to on if available.
THP is already default on (thp only requires us to 2m align the virtual address of guest physical memory which we should do by default even for qemu no-kvm). We can add a thp=off switch on the same lines of ksm=off.
Quoted 4 lines
That way we could also do all the sanity checks in a single place. I really like the idea of combining memory management command line parameters into a single option :). In the end I'd assume it's Anthony's call though.
Ok, let me know ;). Sounds like a lot of trouble compared to -mem_nomerge, but I agree it may be worth it for the long term.
Thanks,
Andrea
Quoted 27 lines · Andrea Arcangeli
Hello everyone,
Quoted 24 lines · Alexander Graf
Quoted 20 lines · Michael Roth
Quoted 15 lines · Andrea Arcangeli
The bug is still there so I rediffed the old patch against current code.
On a related topic: could somebody give me advice on how to implement a command line (command line seems enough, the other option would be monitor command) to make the
MADV_MERGEABLEconditional? I got KSM on THP working fine but KSM may decrease performance by increasing the number of copy on write and by splitting hugepages, so we'd like to be able to turn off KSM on a per-VM basis (not on the whole host, which of course we already can by setting/sys/kernel/mm/ksm/runto 0) so that high perf VMs will keep running at maximum speed with KSM off but others may still benefit from KSM. For that I need to make the belowMADV_MERGEABLEmadvise conditional to something and the code itself will be trivial, we've just to converge on a command line option (hopefully quickly ;).There was a -mem_prealloc option added a while back to set
MAP_POPULATEon memory mapped in via the -mem-path option. So an analogous -mem_nomerge option or something along that line seems reasonable for conditionally unsettingQEMU_MADV_MERGEABLE.And for consistency you should probably make both your proposed changes for -mem-path'd memory as well.
Quoted 7 lines
Quoted 5 lines
Why not clean up all that mess and introduce a new -mem option that would just take all of the several options as parameters?
-mem size=512,populate=on,ksm=off
and default -m to something reasonable with the new syntax.
I'm neutral... so feel free to decide what I should implement ;).
Have to agree the consolidated approach definitely seems nicer.
Quoted 7 lines
One comment on combining -m ksm=off (or -mem_nomerge) with -mem-path. It seems unnecessary because ksm can't be turned on on
VM_HUGETLBvmas (MADV_MERGEABLEwill return-EINVAL) and mem-path only makes sense if used in combination with hugetlbfs (which setsVM_HUGETLBof course).
Yah you're right, but I've seen several discussions about using mempath for tmpfs/ram-backed files for things like numa/zram/etc so tend to think of it as something potentially more than just a hook for hugetlbfs, which is becoming less and less useful. But the MADV_DONTFORK stuff should still be immediately applicable.
Quoted 5 lines · Michael Roth
Yah you're right, but I've seen several discussions about using mempath for tmpfs/ram-backed files for things like numa/zram/etc so tend to think of it as something potentially more than just a hook for hugetlbfs, which is becoming less and less useful. But the
MADV_DONTFORKstuff should still be immediately applicable.
Yes, MADV_DONTFORK should be set all on all guest physical memory without options so I hope the new patch I just posted is fine to stop the spurious -ENOMEM failures in fork.
The ksm part should go incremental but 99% of it will be about changing the command line, making the madvise conditional will be trivial as well.
Quoted 10 lines · Andrea Arcangeli
Quoted 5 lines · Michael Roth
Yah you're right, but I've seen several discussions about using mempath for tmpfs/ram-backed files for things like numa/zram/etc so tend to think of it as something potentially more than just a hook for hugetlbfs, which is becoming less and less useful. But the
MADV_DONTFORKstuff should still be immediately applicable.Yes,
MADV_DONTFORKshould be set all on all guest physical memory without options so I hope the new patch I just posted is fine to stop the spurious-ENOMEMfailures in fork.
The patch in this thread? A couple paths still aren't covered when using -mem-path. Something like this should get them all:
exec.c +3 -0
@@ -2853,6 +2853,9 @@ ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,#endif qemu_madvise (new_block->host, size,QEMU_MADV_MERGEABLE ); } + +/* no allocation failures during fork/exec for migrate/hotplug */ +qemu_madvise (new_block->host, size,QEMU_MADV_DONTFORK ); }
Quoted 8 lines · Michael Roth
Quoted 7 lines · Andrea Arcangeli
Quoted 5 lines · Michael Roth
Yah you're right, but I've seen several discussions about using mempath for tmpfs/ram-backed files for things like numa/zram/etc so tend to think of it as something potentially more than just a hook for hugetlbfs, which is becoming less and less useful. But the
MADV_DONTFORKstuff should still be immediately applicable.
Quoted 6 lines
Quoted 3 lines
Yes,
MADV_DONTFORKshould be set all on all guest physical memory without options so I hope the new patch I just posted is fine to stop the spurious-ENOMEMfailures in fork.The patch in this thread? A couple paths still aren't covered when using -mem-path. Something like this should get them all:
Well the reason of MADV_DONTFORK is to avoid accounting issues with anonymous memory, mem-path don't have that issue as hugetlbfs skips the accounting (it has to because hugetlbfs are not always taken from the regular page allocator). It could be however considered a minor performance optimization.
Now you mention that you want to use -mem-path for other things too, so maybe that's why you need it there too. BTW, if you ever need it for more than hugetlbfs, I'm afraid this MAP_PRIVATE I see when mem_prealloc isn't set, is going to screw any other potential useful usage besides hugetlbfs, not exactly sure why it makes any sense to use MAP_PRIVATE there and not only MAP_SHARED.
Quoted 13 lines · Andrea Arcangeli
Quoted 12 lines · Michael Roth
Quoted 10 lines · Andrea Arcangeli
Quoted 5 lines · Michael Roth
Yah you're right, but I've seen several discussions about using mempath for tmpfs/ram-backed files for things like numa/zram/etc so tend to think of it as something potentially more than just a hook for hugetlbfs, which is becoming less and less useful. But the
MADV_DONTFORKstuff should still be immediately applicable.Yes,
MADV_DONTFORKshould be set all on all guest physical memory without options so I hope the new patch I just posted is fine to stop the spurious-ENOMEMfailures in fork.
Quoted 8 lines
Quoted 2 lines
The patch in this thread? A couple paths still aren't covered when using -mem-path. Something like this should get them all:
Well the reason of
MADV_DONTFORKis to avoid accounting issues with anonymous memory, mem-path don't have that issue as hugetlbfs skips the accounting (it has to because hugetlbfs are not always taken from the regular page allocator). It could be however considered a minor performance optimization.
That's one case, but there's also a wonky fallback in that path that defaults to the normal qemu_vmalloc():
if (mem_path) {
#if defined (__linux__) && !defined(TARGET_S390X)
new_block->host = file_ram_alloc(new_block, size, mem_path);
if (!new_block->host) {
new_block->host = qemu_vmalloc(size);
qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
}May make sense to only add coverage for that specific case though. If file_ram_alloc() is generalized we could deal with it then.
Quoted 6 lines
Now you mention that you want to use -mem-path for other things too, so maybe that's why you need it there too. BTW, if you ever need it for more than hugetlbfs, I'm afraid this
MAP_PRIVATEI see whenmem_preallocisn't set, is going to screw any other potential useful usage besides hugetlbfs, not exactly sure why it makes any sense to useMAP_PRIVATEthere and not onlyMAP_SHARED.
Not sure either...but if it's another hugetlbfs-ism it shouldn't matter since using mem-path for something other than hugetlbfs would ideally be configurable with a -mem path=/dev/shm/vm0.mem,hugetlbfs=off or something along that line, which wouldn't necessarily set MAP_PRIVATE.