Skip to content

Commit 3d6cb2e

Browse files
ljskernelgregkh
authored andcommitted
mm: introduce copy-on-fork VMAs and make VM_MAYBE_GUARD one
commit ab04b53 upstream. Gather all the VMA flags whose presence implies that page tables must be copied on fork into a single bitmap - VM_COPY_ON_FORK - and use this rather than specifying individual flags in vma_needs_copy(). We also add VM_MAYBE_GUARD to this list, as it being set on a VMA implies that there may be metadata contained in the page tables (that is - guard markers) which would will not and cannot be propagated upon fork. This was already being done manually previously in vma_needs_copy(), but this makes it very explicit, alongside VM_PFNMAP, VM_MIXEDMAP and VM_UFFD_WP all of which imply the same. Note that VM_STICKY flags ought generally to be marked VM_COPY_ON_FORK too - because equally a flag being VM_STICKY indicates that the VMA contains metadat that is not propagated by being faulted in - i.e. that the VMA metadata does not fully describe the VMA alone, and thus we must propagate whatever metadata there is on a fork. However, for maximum flexibility, we do not make this necessarily the case here. Link: https://lkml.kernel.org/r/5d41b24e7bc622cda0af92b6d558d7f4c0d1bc8c.1763460113.git.ljs@kernel.org Signed-off-by: Lorenzo Stoakes <ljs@kernel.org> Reviewed-by: Pedro Falcato <pfalcato@suse.de> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Acked-by: David Hildenbrand (Red Hat) <david@kernel.org> Cc: Andrei Vagin <avagin@gmail.com> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Barry Song <baohua@kernel.org> Cc: Dev Jain <dev.jain@arm.com> Cc: Jann Horn <jannh@google.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Lance Yang <lance.yang@linux.dev> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: "Masami Hiramatsu (Google)" <mhiramat@kernel.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: Nico Pache <npache@redhat.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Zi Yan <ziy@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Ahmed Elaidy <elaidya225@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 05cdec2 commit 3d6cb2e

3 files changed

Lines changed: 56 additions & 14 deletions

File tree

include/linux/mm.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,32 @@ extern unsigned int kobjsize(const void *objp);
538538
*/
539539
#define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY)
540540

541+
/*
542+
* Flags which should result in page tables being copied on fork. These are
543+
* flags which indicate that the VMA maps page tables which cannot be
544+
* reconsistuted upon page fault, so necessitate page table copying upon
545+
*
546+
* VM_PFNMAP / VM_MIXEDMAP - These contain kernel-mapped data which cannot be
547+
* reasonably reconstructed on page fault.
548+
*
549+
* VM_UFFD_WP - Encodes metadata about an installed uffd
550+
* write protect handler, which cannot be
551+
* reconstructed on page fault.
552+
*
553+
* We always copy pgtables when dst_vma has uffd-wp
554+
* enabled even if it's file-backed
555+
* (e.g. shmem). Because when uffd-wp is enabled,
556+
* pgtable contains uffd-wp protection information,
557+
* that's something we can't retrieve from page cache,
558+
* and skip copying will lose those info.
559+
*
560+
* VM_MAYBE_GUARD - Could contain page guard region markers which
561+
* by design are a property of the page tables
562+
* only and thus cannot be reconstructed on page
563+
* fault.
564+
*/
565+
#define VM_COPY_ON_FORK (VM_PFNMAP | VM_MIXEDMAP | VM_UFFD_WP | VM_MAYBE_GUARD)
566+
541567
/*
542568
* mapping from the currently active vm_flags protection bits (the
543569
* low four bits) to a page protection mask..

mm/memory.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,25 +1479,15 @@ copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
14791479
static bool
14801480
vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
14811481
{
1482+
if (src_vma->vm_flags & VM_COPY_ON_FORK)
1483+
return true;
14821484
/*
1483-
* Always copy pgtables when dst_vma has uffd-wp enabled even if it's
1484-
* file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable
1485-
* contains uffd-wp protection information, that's something we can't
1486-
* retrieve from page cache, and skip copying will lose those info.
1485+
* The presence of an anon_vma indicates an anonymous VMA has page
1486+
* tables which naturally cannot be reconstituted on page fault.
14871487
*/
1488-
if (userfaultfd_wp(dst_vma))
1489-
return true;
1490-
1491-
if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
1492-
return true;
1493-
14941488
if (src_vma->anon_vma)
14951489
return true;
14961490

1497-
/* Guard regions have modified page tables that require copying. */
1498-
if (src_vma->vm_flags & VM_MAYBE_GUARD)
1499-
return true;
1500-
15011491
/*
15021492
* Don't copy ptes where a page fault will fill them correctly. Fork
15031493
* becomes much lighter when there are big shared or private readonly

tools/testing/vma/vma_internal.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,32 @@ extern unsigned long dac_mmap_min_addr;
145145
*/
146146
#define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY)
147147

148+
/*
149+
* Flags which should result in page tables being copied on fork. These are
150+
* flags which indicate that the VMA maps page tables which cannot be
151+
* reconsistuted upon page fault, so necessitate page table copying upon
152+
*
153+
* VM_PFNMAP / VM_MIXEDMAP - These contain kernel-mapped data which cannot be
154+
* reasonably reconstructed on page fault.
155+
*
156+
* VM_UFFD_WP - Encodes metadata about an installed uffd
157+
* write protect handler, which cannot be
158+
* reconstructed on page fault.
159+
*
160+
* We always copy pgtables when dst_vma has uffd-wp
161+
* enabled even if it's file-backed
162+
* (e.g. shmem). Because when uffd-wp is enabled,
163+
* pgtable contains uffd-wp protection information,
164+
* that's something we can't retrieve from page cache,
165+
* and skip copying will lose those info.
166+
*
167+
* VM_MAYBE_GUARD - Could contain page guard region markers which
168+
* by design are a property of the page tables
169+
* only and thus cannot be reconstructed on page
170+
* fault.
171+
*/
172+
#define VM_COPY_ON_FORK (VM_PFNMAP | VM_MIXEDMAP | VM_UFFD_WP | VM_MAYBE_GUARD)
173+
148174
#define FIRST_USER_ADDRESS 0UL
149175
#define USER_PGTABLES_CEILING 0UL
150176

0 commit comments

Comments
 (0)