Skip to content

Commit 35e2470

Browse files
ljskernelakpm00
authored andcommitted
mm: do not copy page tables unnecessarily for VM_UFFD_WP
Commit ab04b53 ("mm: introduce copy-on-fork VMAs and make VM_MAYBE_GUARD one") aggregates flags checks in vma_needs_copy(), including VM_UFFD_WP. However in doing so, it incorrectly performed this check against src_vma. This check was done on the assumption that all relevant flags are copied upon fork. However the userfaultfd logic is very innovative in that it implements custom logic on fork in dup_userfaultfd(), including a rather well hidden case where lacking UFFD_FEATURE_EVENT_FORK causes VM_UFFD_WP to not be propagated to the destination VMA. And indeed, vma_needs_copy(), prior to this patch, did check this property on dst_vma, not src_vma. Since all the other relevant flags are copied on fork, we can simply fix this by checking against dst_vma. While we're here, we fix a comment against VM_COPY_ON_FORK (noting that it did indeed already reference dst_vma) to make it abundantly clear that we must check against the destination VMA. Link: https://lkml.kernel.org/r/20260114110006.1047071-1-lorenzo.stoakes@oracle.com Fixes: ab04b53 ("mm: introduce copy-on-fork VMAs and make VM_MAYBE_GUARD one") Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reported-by: Chris Mason <clm@meta.com> Closes: https://lore.kernel.org/all/20260113231257.3002271-1-clm@meta.com/ Acked-by: David Hildenbrand (Red Hat) <david@kernel.org> Acked-by: Pedro Falcato <pfalcato@suse.de> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 8ce720d commit 35e2470

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

include/linux/mm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,11 @@ enum {
608608
/*
609609
* Flags which should result in page tables being copied on fork. These are
610610
* flags which indicate that the VMA maps page tables which cannot be
611-
* reconsistuted upon page fault, so necessitate page table copying upon
611+
* reconsistuted upon page fault, so necessitate page table copying upon fork.
612+
*
613+
* Note that these flags should be compared with the DESTINATION VMA not the
614+
* source, as VM_UFFD_WP may not be propagated to destination, while all other
615+
* flags will be.
612616
*
613617
* VM_PFNMAP / VM_MIXEDMAP - These contain kernel-mapped data which cannot be
614618
* reasonably reconstructed on page fault.

mm/memory.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,11 @@ copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
14651465
static bool
14661466
vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
14671467
{
1468-
if (src_vma->vm_flags & VM_COPY_ON_FORK)
1468+
/*
1469+
* We check against dst_vma as while sane VMA flags will have been
1470+
* copied, VM_UFFD_WP may be set only on dst_vma.
1471+
*/
1472+
if (dst_vma->vm_flags & VM_COPY_ON_FORK)
14691473
return true;
14701474
/*
14711475
* The presence of an anon_vma indicates an anonymous VMA has page

0 commit comments

Comments
 (0)