Re: [PATCH v4 02/20] mm: provide vma_[flags_]is_cow_mapping() and remove is_cow_mapping()
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Date: 2026-08-11 08:41:28
Also in:
amd-gfx, dri-devel, intel-xe, kvm, linux-fsdevel, linux-kselftest, linux-mm, linux-perf-users, linux-s390, lkml
On Mon, Aug 10, 2026 at 03:10:07PM -0700, Andrew Morton wrote:
On Mon, 10 Aug 2026 18:59:34 +0100 "Lorenzo Stoakes (ARM)" [off-list ref] wrote:quoted
Hi Andrew - would it be possible to insert the comments added below? Trying to avoid a respin if possible :)np, like this?
Great thanks! Just one minor edit below (being pedantic here but kinda what the comment needs I think :)
quoted hunk ↗ jump to hunk
From: Andrew Morton <akpm@linux-foundation.org> Subject: mm-provide-vma_is_cow_mapping-and-remove-is_cow_mapping-fix Date: Mon Aug 10 03:07:02 PM PDT 2026 add comments, per Lorenzo Link: https://lore.kernel.org/ann-vS6MYT9kuXqu@lucifer (local) Cc: "Lorenzo Stoakes (ARM)" <ljs@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> --- include/linux/mm.h | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)--- a/include/linux/mm.h~mm-provide-vma_is_cow_mapping-and-remove-is_cow_mapping-fix +++ a/include/linux/mm.h@@ -2271,17 +2271,74 @@ void unpin_user_pages(struct page **page void unpin_user_folio(struct folio *folio, unsigned long npages); void unpin_folios(struct folio **folios, unsigned long nfolios); +/** + * vma_flags_is_cow_mapping() - Do these VMA flags imply a CoW mapping? + * @flags: The VMA flags to check. + * + * Mappings which could be CoW'd (subject to Copy-On-Write faults) are + * described as CoW mappings. + * + * All mappings backed by anonymous folios (all anonymous mappings and most + * MAP_PRIVATE-file backed ranges) are CoW mappings. + * + * All other mappings (including all writable MAP_SHARED mappings) are + * non-CoW.
I ummed and ahhed over how to put this but now I think this could cause confusion so could you replace the paragraph with: * All other mappings (including all MAP_SHARED mappings) are non-CoW. It's _all_ MAP_SHARED mappings not just writable, I thought people would automatically assume read-only wasn't included plus drivers could in theory do crazy things (I have a pending series to fix that though) but it's clearer just to say all.
+ *
+ * The criteria are !VMA_SHARED_BIT, VMA_MAYWRITE_BIT.
+ *
+ * VMA_MAYWRITE_BIT is checked instead of VMA_WRITE_BIT to account for both
+ * future mprotect() calls which can render a read-only mapping writable, and
+ * GUP with FOLL_FORCE (e.g. ptrace) which can CoW a read-only mapping.
+ *
+ * - No anonymous mapping can ever clear VMA_MAYWRITE_BIT.
+ *
+ * - Writes to anonymous mappings do not immediately result in CoW faults but
+ * may do so after the process is forked or if a read is followed by a
+ * write.
+ *
+ * - Writes to MAP_PRIVATE file-backed mappings result in CoW faults and may
+ * do so again after fork.
+ *
+ * - MAP_SHARED mappings of a file opened read-only are transformed into
+ * VMA_MAYSHARE_BIT, !VMA_SHARED_BIT, !VMA_MAYWRITE_BIT mappings, so remain
+ * non-CoW.
+ *
+ * - Drivers may clear VMA_MAYWRITE_BIT but do so at mmap() time and cannot
+ * mark themselves anonymous. Having cleared this flag it is not valid for
+ * them to leave the VMA_WRITE_BIT flag set.
+ *
+ * As a consequence, the anonymous reverse mapping only tracks CoW mappings.
+ *
+ * Returns: true if the flags indicate a CoW mapping, otherwise false.
+ */
static inline bool vma_flags_is_cow_mapping(const vma_flags_t *flags)
{
return vma_flags_test(flags, VMA_MAYWRITE_BIT) &&
!vma_flags_test(flags, VMA_SHARED_BIT);
}
+/**
+ * vma_is_cow_mapping() - Is this VMA a CoW mapping?
+ * @desc: The VMA to check.
+ *
+ * See vma_flags_is_cow_mapping() for details.
+ *
+ * Returns: true if the VMA is a CoW mapping, otherwise false.
+ */
static inline bool vma_is_cow_mapping(const struct vm_area_struct *vma)
{
return vma_flags_is_cow_mapping(&vma->flags);
}
+/**
+ * vma_desc_is_cow_mapping() - Is this VMA descriptor a CoW mapping?
+ * @desc: The VMA descriptor to check.
+ *
+ * See vma_flags_is_cow_mapping() for details.
+ *
+ * Returns: true if the VMA descriptor describes a CoW mapping, otherwise
+ * false.
+ */
static inline bool vma_desc_is_cow_mapping(struct vm_area_desc *desc)
{
return vma_flags_is_cow_mapping(&desc->vma_flags);
_-- Cheers, Lorenzo