Re: [PATCH v4 02/20] mm: provide vma_[flags_]is_cow_mapping() and remove is_cow_mapping()

"Lorenzo Stoakes (ARM)" <[email protected]>
Newsgroups org.kernel.vger.linux-fsdevel,org.freedesktop.lists.amd-gfx,org.freedesktop.lists.dri-devel,org.freedesktop.lists.intel-xe,org.kernel.vger.kvm,org.kernel.vger.linux-kernel,org.kernel.vger.linux-kselftest,org.kernel.vger.linux-perf-users,org.kernel.vger.linux-s390,org.kernel.vger.linux-trace-kernel,org.kvack.linux-mm
Message-ID <ann-vS6MYT9kuXqu@lucifer>
Hi Andrew - would it be possible to insert the comments added below? Trying
to avoid a respin if possible :)

On Thu, Aug 06, 2026 at 09:21:27PM +0100, Lorenzo Stoakes (ARM) wrote:
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index df78847f5f07..a3368c542947 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -2271,17 +2271,20 @@ void unpin_user_pages(struct page **pages, unsigned long npages);
>  void unpin_user_folio(struct folio *folio, unsigned long npages);
>  void unpin_folios(struct folio **folios, unsigned long nfolios);
>
> -static inline bool is_cow_mapping(vm_flags_t flags)

 +/**
 + * 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.
 + *
 + * 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 (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
> +	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_desc_is_cow_mapping(struct vm_area_desc *desc)
> +static inline bool vma_is_cow_mapping(const struct vm_area_struct *vma)
>  {
> -	const vma_flags_t *flags = &desc->vma_flags;
> +	return vma_flags_is_cow_mapping(&vma->flags);
> +}
>
> -	return vma_flags_test(flags, VMA_MAYWRITE_BIT) &&
> -		!vma_flags_test(flags, VMA_SHARED_BIT);

 +/**
 + * 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);
>  }
>
>  #ifndef CONFIG_MMU

--
Cheers, Lorenzo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.