[PATCH 39/39] mm/vma: introduce and use vma[_flags]_can_gup()

From: Lorenzo Stoakes (ARM)

Date: Tue Sep 08 2026 - 18:10:53 EST


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.

Rather than keeping these checks open-coded, abstract them to
vma_flags_can_gup() and its VMA wrapper vma_can_gup().

This is useful as there are a number of additional places within the kernel
that need to check whether a mapping can be accessed via GUP.

Therefore, update all such occurrences.

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) <ljs@xxxxxxxxxx>
---
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;

/* By default, dump shared memory if mapped from an anonymous file. */
diff --git a/include/linux/mm.h b/include/linux/mm.h
index b5784685272a..1902d4c77481 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 access pages belonging to mappings whose pages are not permitted
+ * to be accessed (VMA_PFNMAP_BIT) and must not manipulate or provide access to
+ * memory-mapped I/O ranges to users (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);
+}
+
+/**
+ * vma_can_gup() - May GUP obtain pages from @vma?
+ * @vma: The VMA to test.
+ *
+ * See vma_flags_can_gup() for details.
+ *
+ * Returns: true if GUP may access pages from the mapping, otherwise false.
+ */
+static inline bool vma_can_gup(const struct vm_area_struct *vma)
+{
+ return vma_flags_can_gup(&vma->flags);
+}
+
/**
* vma_kernel_pagesize - Default page size granularity for this VMA.
* @vma: The user mapping.
diff --git a/mm/gup.c b/mm/gup.c
index 66b306911703..f4d0cfcb602b 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1204,7 +1204,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
int foreign = (gup_flags & FOLL_REMOTE);
bool vma_anon = vma_is_anonymous(vma);

- if (vm_flags & (VM_IO | VM_PFNMAP))
+ if (!vma_can_gup(vma))
return -EFAULT;

if ((gup_flags & FOLL_ANON) && !vma_anon)
@@ -1955,7 +1955,7 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
* range with the first VMA. Also, skip undesirable VMA types.
*/
nend = min(end, vma->vm_end);
- if (vma->vm_flags & (VM_IO | VM_PFNMAP))
+ if (!vma_can_gup(vma))
continue;
if (nstart < vma->vm_start)
nstart = vma->vm_start;
@@ -2017,8 +2017,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
break;

/* protect what we can, including chardevs */
- if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
- !(vm_flags & vma->vm_flags))
+ if (!vma_can_gup(vma) || !(vm_flags & vma->vm_flags))
break;

if (pages) {
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;

/*
diff --git a/mm/memory.c b/mm/memory.c
index 9a38c7d4cc40..cb56d67b17ca 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));
}

static int validate_page_before_insert(struct vm_area_struct *vma,
@@ -7116,7 +7116,8 @@ int follow_pfnmap_start(struct follow_pfnmap_args *args)
if (unlikely(address < vma->vm_start || address >= vma->vm_end))
goto out;

- if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
+ /* Only mappings GUP cannot handle are followed here. */
+ if (vma_can_gup(vma))
goto out;
retry:
pgdp = pgd_offset(mm, address);
@@ -7310,8 +7311,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 aeb99c5933cb..ed444061631c 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -2011,7 +2011,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;

/*

--
2.55.0