On 9/17/26 18:22, Lorenzo Stoakes (ARM) wrote:
> GUP cannot be used for VMAs which set VMA_IO_BIT - because memory-mapped
> I/O must not be accessed on the user's behalf - or VMA_PFNMAP_BIT - because
> PFN maps have no folios which the kernel is permitted to access.

The interesting thing will be: what if we want to allow gup for COW'ed pages in
PFNMAP. We discussed that already, and it might be a bit harder to squeeze into
the new vma[_flags]_can_gup() checks that are also used elsewhere now.

Because GUP itself mostly already handles the VMA_PFNMAP_BIT, just has to be
taught about vm_normal_page() etc *properly* on all levels. I even have that on
my todo list.

Updating vma[_flags]_can_gup() won't be enough in that case (ignore
VMA_PFNMAP_BIT in cow-mappings), because suddenly we would unlock other code
paths that now depend on this helper.

Also, there is the secretmem thing I comment on below. I think this also belongs
in this helper but other unrelated users don't really want it.

Which makes me wonder whether vma[_flags]_can_gup() is actually the right
abstraction to use in some other cases that you convert below. For the actual
GUP users I think it's the right thing to do. But not for things that are
somewhat related to GUP (but actually different) I think you much rather want a
separate helper.

Long story short: I wonder whether vma[_flags]_can_gup() is the right
abstraction, especially for users that don't actually *use* gup but just want
slightly similar semantics (no VM_IO|VM_PFNMAP).

> 
> Rather than keeping these checks open-coded, abstract them to
> vma_flags_can_gup() and its VMA wrapper vma_can_gup().
> 
> A number of other places make the same check to decide whether a mapping
> can be populated or accessed as GUP would, so update those too.
> 
> While here, drop a reference to 'special' and replace a use of the
> deprecated VMA flags API in vma_dump_size().
> 
> No functional change intended.
> 
> Signed-off-by: Lorenzo Stoakes (ARM) <[email protected]>
> ---
>  fs/coredump.c      |  4 ++--
>  include/linux/mm.h | 29 +++++++++++++++++++++++++++++
>  mm/gup.c           |  7 +++----
>  mm/hmm.c           |  3 +--
>  mm/memory.c        | 14 ++++++++------
>  mm/mempolicy.c     |  3 ++-
>  6 files changed, 45 insertions(+), 15 deletions(-)
> 
> diff --git a/fs/coredump.c b/fs/coredump.c
> index fb21fb6703dd..9f729c594c47 100644
> --- a/fs/coredump.c
> +++ b/fs/coredump.c
> @@ -1616,8 +1616,8 @@ static unsigned long vma_dump_size(struct 
> vm_area_struct *vma,
>               return 0;
>       }
>  
> -     /* Do not dump I/O mapped devices or special mappings */
> -     if (vma->vm_flags & VM_IO)
> +     /* Do not dump memory-mapped I/O, which may have side effects on read. 
> */
> +     if (vma_test(vma, VMA_IO_BIT))
>               return 0;
>  

In the end, we use GUP to lookup the pages through get_dump_page().

I think we should just use the gup helper instead?

>       /* By default, dump shared memory if mapped from an anonymous file. */
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 5860a3b4dba9..1249e04d7b98 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1777,6 +1777,35 @@ static inline bool vma_is_persistent(const struct 
> vm_area_struct *vma)
>       return vma_flags_is_persistent(&vma->flags);
>  }
>  
> +/**
> + * vma_flags_can_gup() - Do the specified VMA flags permit GUP to access the
> + * mapping's pages?
> + * @flags: The VMA flags to test.
> + *
> + * GUP cannot obtain pages from a PFN map (VMA_PFNMAP_BIT), which may have no
> + * struct pages behind it, and must not provide access to memory-mapped I/O
> + * (VMA_IO_BIT).
> + *
> + * Returns: true if GUP may access pages from the mapping, otherwise false.
> + */
> +static inline bool vma_flags_can_gup(const vma_flags_t *flags)
> +{
> +     return !vma_flags_test_any(flags, VMA_IO_BIT, VMA_PFNMAP_BIT);
> +}

This only covers some things though. See check_vma_flags(): vma_is_secretmem()
is another case we universally reject and that is just simply incompatible. In
the future, it be an address space flag which we can have from the VMA. So we'd
want a vma_can_gup() helper but not necessarily a vma_flags_can_gup() helper.

I think this belongs into the vma_can_gup() helper.

Also, we should better clarify in the doc that other GUP flags will decide
whether GUP is actually allowed. the semantics are a bit vague right now "Do the
specified VMA flags permit GUP to access".

[...]

> diff --git a/mm/hmm.c b/mm/hmm.c
> index 2f1e98c6b644..e9569b82a1f0 100644
> --- a/mm/hmm.c
> +++ b/mm/hmm.c
> @@ -595,8 +595,7 @@ static int hmm_vma_walk_test(unsigned long start, 
> unsigned long end,
>       struct hmm_range *range = hmm_vma_walk->range;
>       struct vm_area_struct *vma = walk->vma;
>  
> -     if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) &&
> -         vma->vm_flags & VM_READ)
> +     if (vma_can_gup(vma) && vma_test(vma, VMA_READ_BIT))
>               return 0;

Where do we end up using gup? I don't think we do, because hmm essentially
implements an alternative to KVM-style GUP-fast usage.

So likely this wants a different helper.

>  
>       /*
> diff --git a/mm/memory.c b/mm/memory.c
> index 6c011979401a..338fce99e711 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -2417,11 +2417,11 @@ static bool vm_mixed_zeropage_allowed(struct 
> vm_area_struct *vma)
>        * be problematic as soon as the zeropage gets replaced by a different
>        * page due to vma->vm_ops->pfn_mkwrite, because what's mapped would
>        * now differ to what GUP looked up. FSDAX is incompatible to
> -      * FOLL_LONGTERM and VM_IO is incompatible to GUP completely (see
> -      * check_vma_flags).
> +      * FOLL_LONGTERM and memory-mapped I/O is incompatible to GUP completely
> +      * (see vma_can_gup()).
>        */
>       return vma->vm_ops && vma->vm_ops->pfn_mkwrite &&
> -            (vma_is_fsdax(vma) || vma->vm_flags & VM_IO);
> +            (vma_is_fsdax(vma) || vma_test(vma, VMA_IO_BIT));

This looks a bit misplaces in this patch. Also, not spelled out in the patch
description?

[...]

>  retry:
>       pgdp = pgd_offset(mm, address);
> @@ -7316,8 +7317,9 @@ static int __access_remote_vm(struct mm_struct *mm, 
> unsigned long addr,
>                       }
>  
>                       /*
> -                      * Check if this is a VM_IO | VM_PFNMAP VMA, which
> -                      * we can access using slightly different code.
> +                      * GUP failed, perhaps because this is a mapping it
> +                      * cannot handle (see vma_can_gup()) - such mappings may
> +                      * provide access via vm_ops->access() instead.
>                        */
>                       bytes = 0;
>  #ifdef CONFIG_HAVE_IOREMAP_PROT
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 044ffb4f4128..2fd759e348ca 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -2013,7 +2013,8 @@ SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
>  
>  bool vma_migratable(struct vm_area_struct *vma)
>  {
> -     if (vma->vm_flags & (VM_IO | VM_PFNMAP))
> +     /* Pages which GUP cannot obtain cannot be migrated either. */
> +     if (!vma_can_gup(vma))
>               return false;

It's slightly confusing, because we don't really use GUP (except in one scenario
for lookup_node).

-- 
Cheers,

David

Reply via email to