s390/mm: shared zeropage + KVM fix and optimization
Subsystems (6)
cover s390/mm: shared zeropage + KVM fix and optimization
This series fixes one issue with uffd + shared zeropages on s390x and optimizes "ordinary" KVM guests to make use of shared zeropages again.
userfaultfd could currently end up mapping shared zeropages into processes that forbid shared zeropages. This only apples to s390x, relevant for handling PV guests and guests that use storage kets correctly. Fix it by placing a zeroed folio instead of the shared zeropage during UFFDIO_ZEROPAGE instead.
I stumbled over this issue while looking into a customer scenario that is using:
(1) Memory ballooning for dynamic resizing. Start a VM with, say, 100 GiB and inflate the balloon during boot to 60 GiB. The VM has ~40 GiB available and additional memory can be "fake hotplugged" to the VM later on demand by deflating the balloon. Actual memory overcommit is not desired, so physical memory would only be moved between VMs.
(2) Live migration of VMs between sites to evacuate servers in case of emergency.
Without the shared zeropage, during (2), the VM would suddenly consume 100 GiB on the migration source and destination. On the migration source, where we don't excpect memory overcommit, we could easilt end up crashing the VM during migration.
Independent of that, memory handed back to the hypervisor using "free page reporting" would end up consuming actual memory after the migration on the destination, not getting freed up until reused+freed again.
While there might be ways to optimize parts of this in QEMU, we really should just support the shared zeropage again for ordinary VMs.
We only expect legcy guests to make use of storage keys, so let's handle zeropages again when enabling storage keys or when enabling PV. To not break userfaultfd like we did in the past, don't zap the shared zeropages, but instead trigger unsharing faults, just like we do for unsharing KSM pages in break_ksm().
Unsharing faults will simply replace the shared zeropage by a zeroed anonymous folio. We can already trigger the same fault path using GUP, when trying to long-term pin a shared zeropage, but also when unmerging a KSM-placed zeropages, so this is nothing new.
Patch #1 tested on 86-64 by forcing mm_forbids_zeropage() to be 1, and running the uffd selftests.
Patch #2 tested on s390x: the live migration scenario now works as expected, and kvm-unit-tests that trigger usage of skeys work well, whereby I can see detection and unsharing of shared zeropages.
Based on current mm-unstable. Maybe at least the second patch should go via the s390x tree, I think patch #1 could go that route as well.
David Hildenbrand (2):
mm/userfaultfd: don't place zeropages when zeropages are disallowed s390/mm: re-enable the shared zeropage for !PV and !skeys KVM guests
arch/s390/include/asm/gmap.h | 2 +- arch/s390/include/asm/mmu.h | 5 + arch/s390/include/asm/mmu_context.h | 1 + arch/s390/include/asm/pgtable.h | 15 ++- arch/s390/kvm/kvm-s390.c | 4 +- arch/s390/mm/gmap.c | 163 +++++++++++++++++++++------- mm/userfaultfd.c | 35 ++++++ 7 files changed, 178 insertions(+), 47 deletions(-)
Signature · 1 line
2.43.2
PATCH 1/2 mm/userfaultfd: don't place zeropages when zeropages are disallowed
s390x must disable shared zeropages for processes running VMs, because the VMs could end up making use of "storage keys" or protected virtualization, which are incompatible with shared zeropages.
Yet, with userfaultfd it is possible to insert shared zeropages into such processes. Let's fallback to simply allocating a fresh zeroed anonymous folio and insert that instead.
mm_forbids_zeropage() was introduced in commit 593befa6ab74 ("mm: introduce mm_forbids_zeropage function"), briefly before userfaultfd went upstream.
Note that we don't want to fail the UFFDIO_ZEROPAGE request like we do for hugetlb, it would be rather unexpected. Further, we also cannot really indicated "not supported" to user space ahead of time: it could be that the MM disallows zeropages after userfaultfd was already registered.
--- mm/userfaultfd.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
mm/userfaultfd.c +35 -0
@@ -316,6 +316,38 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, goto out; } +static int mfill_atomic_pte_zeroed_folio (pmd_t *dst_pmd, +struct vm_area_struct *dst_vma,unsigned long dst_addr) +{ +struct folio *folio; +int ret; + + folio =vma_alloc_zeroed_movable_folio (dst_vma, dst_addr); +if (!folio) +return -ENOMEM ; + + ret = -ENOMEM ; +if (mem_cgroup_charge (folio, dst_vma->vm_mm,GFP_KERNEL )) + goto out_put; + +/* +* The memory barrier inside __folio_mark_uptodate makes sure that +* preceding stores to the page contents become visible before +* the set_pte_at() write. +*/ +__folio_mark_uptodate (folio); + + ret =mfill_atomic_install_pte (dst_pmd, dst_vma, dst_addr, + &folio->page, true,0 ); +if (ret) + goto out_put; + +return 0 ; +out_put: +folio_put (folio); +return ret; +} +static int mfill_atomic_pte_zeropage (pmd_t *dst_pmd,struct vm_area_struct *dst_vma,unsigned long dst_addr)
@@ -324,6 +356,9 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd,spinlock_t *ptl;int ret; +if (mm_forbids_zeropage (dst_vma->mm)) +return mfill_atomic_pte_zeroed_folio (dst_pmd, dst_vma, dst_addr); + _dst_pte =pte_mkspecial (pfn_pte (my_zero_pfn (dst_addr), dst_vma->vm_page_prot)); ret = -EAGAIN ;
Signature · 1 line
2.43.2
Re: [PATCH v1 1/2] mm/userfaultfd: don't place zeropages when zeropages are disallowed
Quoted 12 lines · David Hildenbrand
s390x must disable shared zeropages for processes running VMs, because the VMs could end up making use of "storage keys" or protected virtualization, which are incompatible with shared zeropages.
Yet, with userfaultfd it is possible to insert shared zeropages into such processes. Let's fallback to simply allocating a fresh zeroed anonymous folio and insert that instead.
mm_forbids_zeropage()was introduced in commit593befa6ab74("mm: introducemm_forbids_zeropagefunction"), briefly before userfaultfd went upstream.
Quoted 8 lines
Note that we don't want to fail the
UFFDIO_ZEROPAGErequest like we do for hugetlb, it would be rather unexpected. Further, we also cannot really indicated "not supported" to user space ahead of time: it could be that the MM disallows zeropages after userfaultfd was already registered.Fixes c1a4de99fada ("userfaultfd: mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation")Signed-off-by David Hildenbrand <[email protected]>
Still, a few comments below.
Quoted 9 lines
--- mm/userfaultfd.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)mm/userfaultfd.c +0
-0
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 323
Quoted 8 lines
mm/userfaultfd.c +5
-0@@ -316,6 +316,38 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, goto out; } +static int mfill_atomic_pte_zeroed_folio (pmd_t *dst_pmd, +struct vm_area_struct *dst_vma,unsigned long dst_addr) +{ +struct folio *folio; +int ret;
nitpick: we can set -ENOMEM here, then
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 327
Quoted 4 lines
mm/userfaultfd.c +4
-0+ + folio =vma_alloc_zeroed_movable_folio (dst_vma, dst_addr); +if (!folio) +return -ENOMEM ;
return ret;
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 329
Quoted 2 lines
mm/userfaultfd.c +2
-0+ + ret = -ENOMEM ;
drop.
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 337
Quoted 8 lines
mm/userfaultfd.c +8
-0+if (mem_cgroup_charge (folio, dst_vma->vm_mm,GFP_KERNEL )) + goto out_put; + +/* +* The memory barrier inside __folio_mark_uptodate makes sure that +* preceding stores to the page contents become visible before +* the set_pte_at() write. +*/
This comment doesn't apply. We can drop it.
Thanks,
Quoted 29 lines
mm/userfaultfd.c +16
-0+__folio_mark_uptodate (folio); + + ret =mfill_atomic_install_pte (dst_pmd, dst_vma, dst_addr, + &folio->page, true,0 ); +if (ret) + goto out_put; + +return 0 ; +out_put: +folio_put (folio); +return ret; +} +static int mfill_atomic_pte_zeropage (pmd_t *dst_pmd,struct vm_area_struct *dst_vma,unsigned long dst_addr)@@ -324,6 +356,9 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd,spinlock_t *ptl;int ret; +if (mm_forbids_zeropage (dst_vma->mm)) +return mfill_atomic_pte_zeroed_folio (dst_pmd, dst_vma, dst_addr); + _dst_pte =pte_mkspecial (pfn_pte (my_zero_pfn (dst_addr), dst_vma->vm_page_prot)); ret = -EAGAIN ;Signature · 1 line
2.43.2
Signature · 1 line
Peter Xu
Re: [PATCH v1 1/2] mm/userfaultfd: don't place zeropages when zeropages are disallowed
Quoted 53 lines · Peter Xu
Quoted 12 lines · David Hildenbrand
s390x must disable shared zeropages for processes running VMs, because the VMs could end up making use of "storage keys" or protected virtualization, which are incompatible with shared zeropages.
Yet, with userfaultfd it is possible to insert shared zeropages into such processes. Let's fallback to simply allocating a fresh zeroed anonymous folio and insert that instead.
mm_forbids_zeropage()was introduced in commit593befa6ab74("mm: introducemm_forbids_zeropagefunction"), briefly before userfaultfd went upstream.Quoted 8 lines
Note that we don't want to fail the
UFFDIO_ZEROPAGErequest like we do for hugetlb, it would be rather unexpected. Further, we also cannot really indicated "not supported" to user space ahead of time: it could be that the MM disallows zeropages after userfaultfd was already registered.Fixes c1a4de99fada ("userfaultfd: mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation")Signed-off-by David Hildenbrand <[email protected]>Reviewed-by Peter Xu <[email protected]>Still, a few comments below.
Quoted 9 lines
--- mm/userfaultfd.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)mm/userfaultfd.c +0
-0mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 323
Quoted 8 lines
mm/userfaultfd.c +5
-0@@ -316,6 +316,38 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, goto out; } +static int mfill_atomic_pte_zeroed_folio (pmd_t *dst_pmd, +struct vm_area_struct *dst_vma,unsigned long dst_addr) +{ +struct folio *folio; +int ret;nitpick: we can set
-ENOMEMhere, thenmm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 327
Quoted 4 lines
mm/userfaultfd.c +4
-0+ + folio =vma_alloc_zeroed_movable_folio (dst_vma, dst_addr); +if (!folio) +return -ENOMEM ;return ret;
Quoted 4 lines
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 329
Quoted 2 lines
mm/userfaultfd.c +2
-0+ + ret = -ENOMEM ;drop.
Sure!
Quoted 12 lines
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 337
Quoted 9 lines
mm/userfaultfd.c +8
-0+if (mem_cgroup_charge (folio, dst_vma->vm_mm,GFP_KERNEL )) + goto out_put; + +/* +* The memory barrier inside __folio_mark_uptodate makes sure that +* preceding stores to the page contents become visible before +* the set_pte_at() write. +*/ This comment doesn't apply. We can drop it.
I thought the same until I spotted that comment (where uffd originally copied this from I strongly assume) in do_anonymous_page().
"Preceding stores" here are: zeroing out the memory.
Thanks for the fast review!
Signature · 3 lines
Cheers, David / dhildenb
Peter Xu
reviewerRe: [PATCH v1 1/2] mm/userfaultfd: don't place zeropages when zeropages are disallowed
Quoted 73 lines · David Hildenbrand
Quoted 52 lines · Peter Xu
Quoted 12 lines · David Hildenbrand
s390x must disable shared zeropages for processes running VMs, because the VMs could end up making use of "storage keys" or protected virtualization, which are incompatible with shared zeropages.
Yet, with userfaultfd it is possible to insert shared zeropages into such processes. Let's fallback to simply allocating a fresh zeroed anonymous folio and insert that instead.
mm_forbids_zeropage()was introduced in commit593befa6ab74("mm: introducemm_forbids_zeropagefunction"), briefly before userfaultfd went upstream.Quoted 8 lines
Note that we don't want to fail the
UFFDIO_ZEROPAGErequest like we do for hugetlb, it would be rather unexpected. Further, we also cannot really indicated "not supported" to user space ahead of time: it could be that the MM disallows zeropages after userfaultfd was already registered.Fixes c1a4de99fada ("userfaultfd: mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation")Signed-off-by David Hildenbrand <[email protected]>Reviewed-by Peter Xu <[email protected]>Still, a few comments below.
Quoted 9 lines
--- mm/userfaultfd.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)mm/userfaultfd.c +0
-0mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 322
Quoted 7 lines
mm/userfaultfd.c +5
-0@@ -316,6 +316,38 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, goto out; } +static int mfill_atomic_pte_zeroed_folio (pmd_t *dst_pmd, +struct vm_area_struct *dst_vma,unsigned long dst_addr) +{ +struct folio *folio; +int ret;nitpick: we can set
-ENOMEMhere, thenmm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 327
Quoted 4 lines
mm/userfaultfd.c +4
-0+ + folio =vma_alloc_zeroed_movable_folio (dst_vma, dst_addr); +if (!folio) +return -ENOMEM ;return ret;
Quoted 4 lines
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 328
Quoted 2 lines
mm/userfaultfd.c +2
-0+ + ret = -ENOMEM ;drop.
Sure!
Quoted 12 lines
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 337
Quoted 9 lines
mm/userfaultfd.c +8
-0+if (mem_cgroup_charge (folio, dst_vma->vm_mm,GFP_KERNEL )) + goto out_put; + +/* +* The memory barrier inside __folio_mark_uptodate makes sure that +* preceding stores to the page contents become visible before +* the set_pte_at() write. +*/ This comment doesn't apply. We can drop it.
Quoted 4 lines
I thought the same until I spotted that comment (where uffd originally copied this from I strongly assume) in
do_anonymous_page()."Preceding stores" here are: zeroing out the memory.
Ah.. that's okay then.
Considering that userfault used to be pretty cautious on such ordering, as its specialty to involve many user updates on the page, would you mind we mention those details out?
/*
* __folio_mark_uptodate contains the memory barrier to make sure
* the page updates to the zero page will be visible before
* installing the pgtable entries. See do_anonymous_page(). */
Or anything better than my wordings.
Thanks!
Signature · 1 line
Peter Xu
Re: [PATCH v1 1/2] mm/userfaultfd: don't place zeropages when zeropages are disallowed
Quoted 85 lines · Peter Xu
Quoted 73 lines · David Hildenbrand
Quoted 52 lines · Peter Xu
Quoted 12 lines · David Hildenbrand
s390x must disable shared zeropages for processes running VMs, because the VMs could end up making use of "storage keys" or protected virtualization, which are incompatible with shared zeropages.
Yet, with userfaultfd it is possible to insert shared zeropages into such processes. Let's fallback to simply allocating a fresh zeroed anonymous folio and insert that instead.
mm_forbids_zeropage()was introduced in commit593befa6ab74("mm: introducemm_forbids_zeropagefunction"), briefly before userfaultfd went upstream.Quoted 8 lines
Note that we don't want to fail the
UFFDIO_ZEROPAGErequest like we do for hugetlb, it would be rather unexpected. Further, we also cannot really indicated "not supported" to user space ahead of time: it could be that the MM disallows zeropages after userfaultfd was already registered.Fixes c1a4de99fada ("userfaultfd: mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation")Signed-off-by David Hildenbrand <[email protected]>Reviewed-by Peter Xu <[email protected]>Still, a few comments below.
Quoted 9 lines
--- mm/userfaultfd.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)mm/userfaultfd.c +0
-0mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 322
Quoted 7 lines
mm/userfaultfd.c +5
-0@@ -316,6 +316,38 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, goto out; } +static int mfill_atomic_pte_zeroed_folio (pmd_t *dst_pmd, +struct vm_area_struct *dst_vma,unsigned long dst_addr) +{ +struct folio *folio; +int ret;nitpick: we can set
-ENOMEMhere, thenmm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 326
Quoted 4 lines
mm/userfaultfd.c +4
-0+ + folio =vma_alloc_zeroed_movable_folio (dst_vma, dst_addr); +if (!folio) +return -ENOMEM ;return ret;
Quoted 4 lines
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 328
Quoted 2 lines
mm/userfaultfd.c +2
-0+ + ret = -ENOMEM ;drop.
Sure!
Quoted 12 lines
mm/userfaultfd.c static int mfill_atomic_pte_copy(pmd_t *dst_pmd, · line 336
Quoted 9 lines
mm/userfaultfd.c +8
-0+if (mem_cgroup_charge (folio, dst_vma->vm_mm,GFP_KERNEL )) + goto out_put; + +/* +* The memory barrier inside __folio_mark_uptodate makes sure that +* preceding stores to the page contents become visible before +* the set_pte_at() write. +*/ This comment doesn't apply. We can drop it.
Quoted 4 lines
I thought the same until I spotted that comment (where uffd originally copied this from I strongly assume) in
do_anonymous_page()."Preceding stores" here are: zeroing out the memory.
Ah.. that's okay then.
Considering that userfault used to be pretty cautious on such ordering, as its specialty to involve many user updates on the page, would you mind we mention those details out?
Quoted 7 lines
/*
*__folio_mark_uptodatecontains the memory barrier to make sure
* the page updates to the zero page will be visible before
* installing the pgtable entries. Seedo_anonymous_page(). */Or anything better than my wordings.
Sure, I'd slightly reword it. The following on top:
mm/userfaultfd.c +4 -5
@@ -320,20 +320,19 @@ static int mfill_atomic_pte_zeroed_folio(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr) {struct folio *folio; -int ret; +int ret = -ENOMEM ; folio =vma_alloc_zeroed_movable_folio (dst_vma, dst_addr);if (!folio) -return -ENOMEM ; +return ret; - ret = -ENOMEM ;if (mem_cgroup_charge (folio, dst_vma->vm_mm,GFP_KERNEL )) goto out_put;/* * The memory barrier inside __folio_mark_uptodate makes sure that -* preceding stores to the page contents become visible before -* the set_pte_at() write. +* zeroing out the folio become visible before mapping the page +* using set_pte_at(). See do_anonymous_page(). */ __folio_mark_uptodate (folio);
Thanks!
Signature · 3 lines
Cheers, David / dhildenb
PATCH 2/2 s390/mm: re-enable the shared zeropage for !PV and !skeys KVM guests
In reply to David Hildenbrandcommit fa41ba0d08de ("s390/mm: avoid empty zero pages for KVM guests to avoid postcopy hangs") introduced an undesired side effect when combined with memory ballooning and VM migration: memory part of the inflated memory balloon will consume memory.
Assuming we have a 100GiB VM and inflated the balloon to 40GiB. Our VM will consume ~60GiB of memory. If we now trigger a VM migration, hypervisors like QEMU will read all VM memory. As s390x does not support the shared zeropage, we'll end up allocating for all previously-inflated memory part of the memory balloon: 50 GiB. So we might easily (unexpectedly) crash the VM on the migration source.
Even worse, hypervisors like QEMU optimize for zeropage migration to not consume memory on the migration destination: when migrating a "page full of zeroes", on the migration destination they check whether the target memory is already zero (by reading the destination memory) and avoid writing to the memory to not allocate memory: however, s390x will also allocate memory here, implying that also on the migration destination, we will end up allocating all previously-inflated memory part of the memory balloon.
This is especially bad if actual memory overcommit was not desired, when memory ballooning is used for dynamic VM memory resizing, setting aside some memory during boot that can be added later on demand. Alternatives like virtio-mem that would avoid this issue are not yet available on s390x.
There could be ways to optimize some cases in user space: before reading memory in an anonymous private mapping on the migration source, check via /proc/self/pagemap if anything is already populated. Similarly check on the migration destination before reading. While that would avoid populating tables full of shared zeropages on all architectures, it's harder to get right and performant, and requires user space changes.
Further, with posctopy live migration we must place a page, so there, "avoid touching memory to avoid allocating memory" is not really possible. (Note that a previously we would have falsely inserted shared zeropages into processes using UFFDIO_ZEROPAGE where mm_forbids_zeropage() would have actually forbidden it)
PV is currently incompatible with memory ballooning, and in the common case, KVM guests don't make use of storage keys. Instead of zapping zeropages when enabling storage keys / PV, that turned out to be problematic in the past, let's do exactly the same we do with KSM pages:
trigger unsharing faults to replace the shared zeropages by proper anonymous folios.
What about added latency when enabling storage kes? Having a lot of zeropages in applicable environments (PV, legacy guests, unittests) is unexpected. Further, KSM could today already unshare the zeropages and unmerging KSM pages when enabling storage kets would unshare the KSM-placed zeropages in the same way, resulting in the same latency.
--- arch/s390/include/asm/gmap.h | 2 +- arch/s390/include/asm/mmu.h | 5 + arch/s390/include/asm/mmu_context.h | 1 + arch/s390/include/asm/pgtable.h | 15 ++- arch/s390/kvm/kvm-s390.c | 4 +- arch/s390/mm/gmap.c | 163 +++++++++++++++++++++------- 6 files changed, 143 insertions(+), 47 deletions(-)
arch/s390/include/asm/gmap.h +1 -1
@@ -146,7 +146,7 @@ int gmap_mprotect_notify(struct gmap *, unsigned long start,void gmap_sync_dirty_log_pmd (struct gmap *gmap,unsigned long dirty_bitmap[4 ],unsigned long gaddr,unsigned long vmaddr); -int gmap_mark_unmergeable (void ); +int s390_disable_cow_sharing (void );void s390_unlist_old_asce (struct gmap *gmap);int s390_replace_asce (struct gmap *gmap);void s390_uv_destroy_pfns (unsigned long count,unsigned long *pfns);
arch/s390/include/asm/mmu.h +5 -0
@@ -32,6 +32,11 @@ typedef struct { unsigned int uses_skeys:1;/* The mmu context uses CMM. */ unsigned int uses_cmm:1; +/* +* The mmu context allows COW-sharing of memory pages (KSM, zeropage). +* Note that COW-sharing during fork() is currently always allowed. +*/ + unsigned int allow_cow_sharing:1;/* The gmaps associated with this context are allowed to use huge pages. */ unsigned int allow_gmap_hpage_1m:1; } mm_context_t;
arch/s390/include/asm/mmu_context.h +1 -0
@@ -35,6 +35,7 @@ static inline int init_new_context(struct task_struct *tsk, mm->context.has_pgste =0 ; mm->context.uses_skeys =0 ; mm->context.uses_cmm =0 ; + mm->context.allow_cow_sharing =1 ; mm->context.allow_gmap_hpage_1m =0 ;#endif switch (mm->context.asce_limit) {
arch/s390/include/asm/pgtable.h +12 -3
@@ -566,10 +566,19 @@ static inline pud_t set_pud_bit(pud_t pud, pgprot_t prot) }/* -* In the case that a guest uses storage keys -* faults should no longer be backed by zero pages +* As soon as the guest uses storage keys or enables PV, we deduplicate all +* mapped shared zeropages and prevent new shared zeropages from getting +* mapped. */ -#define mm_forbids_zeropage mm_has_pgste +static inline int mm_forbids_zeropage (struct mm_struct *mm) +{ +#ifdef CONFIG_PGSTE +if (!mm->context.allow_cow_sharing) +return 1 ; +#endif +return 0 ; +} +static inline int mm_uses_skeys (struct mm_struct *mm) {#ifdef CONFIG_PGSTE
arch/s390/kvm/kvm-s390.c +1 -3
@@ -2631,9 +2631,7 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)if (r)break ; -mmap_write_lock (current->mm); - r =gmap_mark_unmergeable (); -mmap_write_unlock (current->mm); + r =s390_disable_cow_sharing ();if (r)break ;
arch/s390/mm/gmap.c +123 -40
193 lines not on this page · raw message
Signature · 1 line
2.43.2
Re: [PATCH v1 2/2] s390/mm: re-enable the shared zeropage for !PV and !skeys KVM guests
Am 21.03.24 um 22:59 schrieb David Hildenbrand:
Quoted 47 lines
commit
fa41ba0d08de("s390/mm: avoid empty zero pages for KVM guests to avoid postcopy hangs") introduced an undesired side effect when combined with memory ballooning and VM migration: memory part of the inflated memory balloon will consume memory.Assuming we have a 100GiB VM and inflated the balloon to 40GiB. Our VM will consume ~60GiB of memory. If we now trigger a VM migration, hypervisors like QEMU will read all VM memory. As s390x does not support the shared zeropage, we'll end up allocating for all previously-inflated memory part of the memory balloon: 50 GiB. So we might easily (unexpectedly) crash the VM on the migration source.
Even worse, hypervisors like QEMU optimize for zeropage migration to not consume memory on the migration destination: when migrating a "page full of zeroes", on the migration destination they check whether the target memory is already zero (by reading the destination memory) and avoid writing to the memory to not allocate memory: however, s390x will also allocate memory here, implying that also on the migration destination, we will end up allocating all previously-inflated memory part of the memory balloon.
This is especially bad if actual memory overcommit was not desired, when memory ballooning is used for dynamic VM memory resizing, setting aside some memory during boot that can be added later on demand. Alternatives like virtio-mem that would avoid this issue are not yet available on s390x.
There could be ways to optimize some cases in user space: before reading memory in an anonymous private mapping on the migration source, check via
/proc/self/pagemapif anything is already populated. Similarly check on the migration destination before reading. While that would avoid populating tables full of shared zeropages on all architectures, it's harder to get right and performant, and requires user space changes.Further, with posctopy live migration we must place a page, so there, "avoid touching memory to avoid allocating memory" is not really possible. (Note that a previously we would have falsely inserted shared zeropages into processes using
UFFDIO_ZEROPAGEwheremm_forbids_zeropage()would have actually forbidden it)PV is currently incompatible with memory ballooning, and in the common case, KVM guests don't make use of storage keys. Instead of zapping zeropages when enabling storage keys / PV, that turned out to be problematic in the past, let's do exactly the same we do with KSM pages:
trigger unsharing faults to replace the shared zeropages by proper anonymous folios.
Quoted 7 lines
What about added latency when enabling storage kes? Having a lot of zeropages in applicable environments (PV, legacy guests, unittests) is unexpected. Further, KSM could today already unshare the zeropages and unmerging KSM pages when enabling storage kets would unshare the KSM-placed zeropages in the same way, resulting in the same latency.
Signed-off-by David Hildenbrand <[email protected]>
Nice work. Looks good to me and indeed it fixes the memory over-consumption that you mentioned.
(can also be seen with virsh managedsave; virsh start)
I guess its too invasive for stable, but I would say it is real fix.
Re: [PATCH v1 2/2] s390/mm: re-enable the shared zeropage for !PV and !skeys KVM guests
Quoted 56 lines · Christian Borntraeger
Am 21.03.24 um 22:59 schrieb David Hildenbrand:
Quoted 53 lines
commit
fa41ba0d08de("s390/mm: avoid empty zero pages for KVM guests to avoid postcopy hangs") introduced an undesired side effect when combined with memory ballooning and VM migration: memory part of the inflated memory balloon will consume memory.Assuming we have a 100GiB VM and inflated the balloon to 40GiB. Our VM will consume ~60GiB of memory. If we now trigger a VM migration, hypervisors like QEMU will read all VM memory. As s390x does not support the shared zeropage, we'll end up allocating for all previously-inflated memory part of the memory balloon: 50 GiB. So we might easily (unexpectedly) crash the VM on the migration source.
Even worse, hypervisors like QEMU optimize for zeropage migration to not consume memory on the migration destination: when migrating a "page full of zeroes", on the migration destination they check whether the target memory is already zero (by reading the destination memory) and avoid writing to the memory to not allocate memory: however, s390x will also allocate memory here, implying that also on the migration destination, we will end up allocating all previously-inflated memory part of the memory balloon.
This is especially bad if actual memory overcommit was not desired, when memory ballooning is used for dynamic VM memory resizing, setting aside some memory during boot that can be added later on demand. Alternatives like virtio-mem that would avoid this issue are not yet available on s390x.
There could be ways to optimize some cases in user space: before reading memory in an anonymous private mapping on the migration source, check via
/proc/self/pagemapif anything is already populated. Similarly check on the migration destination before reading. While that would avoid populating tables full of shared zeropages on all architectures, it's harder to get right and performant, and requires user space changes.Further, with posctopy live migration we must place a page, so there, "avoid touching memory to avoid allocating memory" is not really possible. (Note that a previously we would have falsely inserted shared zeropages into processes using
UFFDIO_ZEROPAGEwheremm_forbids_zeropage()would have actually forbidden it)PV is currently incompatible with memory ballooning, and in the common case, KVM guests don't make use of storage keys. Instead of zapping zeropages when enabling storage keys / PV, that turned out to be problematic in the past, let's do exactly the same we do with KSM pages:
trigger unsharing faults to replace the shared zeropages by proper anonymous folios.What about added latency when enabling storage kes? Having a lot of zeropages in applicable environments (PV, legacy guests, unittests) is unexpected. Further, KSM could today already unshare the zeropages and unmerging KSM pages when enabling storage kets would unshare the KSM-placed zeropages in the same way, resulting in the same latency.
Quoted 4 lines
Quoted 1 line
Signed-off-by David Hildenbrand <[email protected]>Nice work. Looks good to me and indeed it fixes the memory over-consumption that you mentioned.
Thanks for the very fast review and test!
Quoted 6 lines
Reviewed-by Christian Borntraeger <[email protected]>(can also be seen with virsh managedsave; virsh start)
I guess its too invasive for stable, but I would say it is real fix.
Should we add a Fixes: Tag? I refrained from doing so, treating this more like an optimization to restore the intended behavior at least as long as the VM does not use storage keys.
Signature · 3 lines
Cheers, David / dhildenb
Andrew Morton
maintainerQuoted 2 lines · On Thu, 21 Mar 2024 22:59:52 +0100 David Hildenbrand <[email protected]> wrote:
Based on current mm-unstable. Maybe at least the second patch should go via the s390x tree, I think patch #1 could go that route as well.
Taking both via the s390 tree is OK by me. I'll drop the mm.git copies if/when these turn up in the linux-next feed.
Heiko Carstens
maintainerQuoted 7 lines · Andrew Morton
Quoted 2 lines · On Thu, 21 Mar 2024 22:59:52 +0100 David Hildenbrand <[email protected]> wrote:
Based on current mm-unstable. Maybe at least the second patch should go via the s390x tree, I think patch #1 could go that route as well.
Taking both via the s390 tree is OK by me. I'll drop the mm.git copies if/when these turn up in the linux-next feed.
Considering the comments I would expect a v2 of this series at some time in the future.
Quoted 11 lines · Heiko Carstens
Quoted 7 lines · Andrew Morton
Quoted 2 lines · On Thu, 21 Mar 2024 22:59:52 +0100 David Hildenbrand <[email protected]> wrote:
Based on current mm-unstable. Maybe at least the second patch should go via the s390x tree, I think patch #1 could go that route as well.
Taking both via the s390 tree is OK by me. I'll drop the mm.git copies if/when these turn up in the linux-next feed.
Considering the comments I would expect a v2 of this series at some time in the future.
Yes, I'm still waiting for more feedback. I'll likely resend tomorrow.
Signature · 3 lines
Cheers, David / dhildenb