Skip to content

Commit 90a7592

Browse files
davidhildenbrandAlexander Gordeev
authored andcommitted
mm/userfaultfd: Do not 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 593befa ("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. [ agordeev: Fixed checkpatch complaints ] Fixes: c1a4de9 ("userfaultfd: mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation") Reviewed-by: Peter Xu <peterx@redhat.com> Link: https://lore.kernel.org/r/20240411161441.910170-2-david@redhat.com Signed-off-by: David Hildenbrand <david@redhat.com> Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com>
1 parent 39cd87c commit 90a7592

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

mm/userfaultfd.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,38 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd,
316316
goto out;
317317
}
318318

319+
static int mfill_atomic_pte_zeroed_folio(pmd_t *dst_pmd,
320+
struct vm_area_struct *dst_vma,
321+
unsigned long dst_addr)
322+
{
323+
struct folio *folio;
324+
int ret = -ENOMEM;
325+
326+
folio = vma_alloc_zeroed_movable_folio(dst_vma, dst_addr);
327+
if (!folio)
328+
return ret;
329+
330+
if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL))
331+
goto out_put;
332+
333+
/*
334+
* The memory barrier inside __folio_mark_uptodate makes sure that
335+
* zeroing out the folio become visible before mapping the page
336+
* using set_pte_at(). See do_anonymous_page().
337+
*/
338+
__folio_mark_uptodate(folio);
339+
340+
ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
341+
&folio->page, true, 0);
342+
if (ret)
343+
goto out_put;
344+
345+
return 0;
346+
out_put:
347+
folio_put(folio);
348+
return ret;
349+
}
350+
319351
static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd,
320352
struct vm_area_struct *dst_vma,
321353
unsigned long dst_addr)
@@ -324,6 +356,9 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd,
324356
spinlock_t *ptl;
325357
int ret;
326358

359+
if (mm_forbids_zeropage(dst_vma->vm_mm))
360+
return mfill_atomic_pte_zeroed_folio(dst_pmd, dst_vma, dst_addr);
361+
327362
_dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
328363
dst_vma->vm_page_prot));
329364
ret = -EAGAIN;

0 commit comments

Comments
 (0)