mm: access remote process memory under the per-VMA lock
Subsystems (11)
Rik van Riel
reviewerv3 cover mm: access remote process memory under the per-VMA lock
__access_remote_vm() holds mmap_read_lock() for the whole transfer. On large machines with large multi-threaded applications, the mmap_lock is often contended due to mixed accesses from readers and writers, like mmap and munmap. When a lock holder gets stuck, system monitoring software can get stuck behind that, resulting in a failure to log that the system is in trouble.
Take the per-VMA lock in __access_remote_vm() when the access falls entirely within a single VMA. Fall back to the mmap lock when the access crosses a VMA boundary, or when the page cannot be reached under the per-VMA lock: a dropped fault, a userfaultfd VMA, a hard error, or memory with no struct page that has to go through vma->vm_ops->access().
The bulk of the work is a new gup helper. __access_remote_vm() needs a single page from a VMA it has already looked up and locked, faulting it in when necessary, under either lock.
get_user_pages_remote() does not fit: it hard codes the mmap lock and re-derives the VMA. get_user_page_vma() walks the page tables, faults a missing page in, and returns it with a reference and the caller's lock still held.
The per-VMA path also closes a pre-existing gap. A COWed page in a VM_IO/VM_PFNMAP VMA has a struct page, but the old code routed it to ->access(), where generic_access_phys() ioremaps the PFN and ioremap of RAM is rejected, so the read came up short.
get_user_page_vma() now returns that page normally. Raw PFNs with no struct page still reach ->access() under the mmap lock, as before.
The series is arranged as:
1-2: untag the remote address in the VMA lookup without the mmap lock,
on x86 and riscv.
3: rename get_user_page_vma_remote() to get_user_page_lookup_vma(). 4: add get_user_page_vma(). 5: switch __access_remote_vm() to the per-VMA lock. 6: add selftest coverage.
Changes since v2 [1]. The per-VMA fast path is reworked to build on the GUP lookup+fault path instead of a custom walker, per David Hildenbrand's and Lorenzo Stoakes' review, and now faults pages in rather than only reading resident ones.
- Drop the folio_walk_start(FW_VMA_LOCKED) walk. Add get_user_page_vma() in mm/gup.c (patch 4): a trimmed __get_user_pages() that walks with follow_page_mask(), faults in with faultin_page(), and returns the page with the caller's lock held. Safe under the per-VMA lock because page tables are RCU-freed, so interrupts need not be disabled. (David)
- Fault pages in on the fast path. v2 fell back to the mmap lock for any not-present page; now it falls back only on -EAGAIN. (David)
- Turn the v2 READ_ONCE/WRITE_ONCE untag_mask change into an untagged_addr_remote_unlocked() helper (patch 1), and add the riscv pointer-masking equivalent (patch 2), which v2 did not cover.
- Read COWed pages in VM_IO/VM_PFNMAP VMAs; raw PFNs still fall back to
->access() under the mmap lock.
- Add the get_user_page_lookup_vma() rename (patch 3) and selftest coverage for the struct-page and raw-PFN paths (patch 6).
[1] https://lore.kernel.org/all/[email protected]/
Rik van Riel (6):
x86/mm: add untagged_addr_remote_unlocked()
riscv/mm: add untagged_addr_remote_unlocked()
mm: rename get_user_page_vma_remote() to get_user_page_lookup_vma() mm/gup: add get_user_page_vma() to fault in a page under a held lock
mm: use per-VMA lock in __access_remote_vm() for single-VMA accesses selftests/mm: cover /proc/pid/mem access to VM_PFNMAP memory
arch/arm64/kernel/mte.c | 2 +- arch/riscv/include/asm/mmu_context.h | 4 +- arch/riscv/include/asm/uaccess.h | 10 +- arch/riscv/kernel/process.c | 12 +- arch/x86/include/asm/mmu_context.h | 6 +- arch/x86/include/asm/uaccess_64.h | 14 ++- arch/x86/kernel/process_64.c | 4 +- arch/x86/kernel/uprobes.c | 2 +- include/linux/mm.h | 2 +- include/linux/uaccess.h | 7 ++ mm/gup.c | 156 ++++++++++++++++++++++---- mm/internal.h | 7 +- mm/memory.c | 171 ++++++++++++++++++++------- mm/rmap.c | 2 +- tools/testing/selftests/mm/pfnmap.c | 66 +++++++++++ 15 files changed, 375 insertions(+), 90 deletions(-)
base-commit: 0f26556c5eeea62cc934fa8938b148aa5844a6b6 --
2.47.0
Rik van Riel
reviewerRFC PATCH v3 1/6 x86/mm: add untagged_addr_remote_unlocked()
__access_remote_vm() reads another process's memory under the mmap lock. On large machines that lock is contended by tasks polling /proc/PID/cmdline, /proc/PID/environ, or calling process_vm_readv(), even though the memory they read is almost always resident and could be reached under the per-VMA lock instead.
Looking up the VMA first requires untagging the address. untagged_addr_remote() asserts the mmap lock only because it reads mm->context.untag_mask, which can race with the write in mm_enable_lam().
That mask is set once, when LAM is enabled, and never changes afterwards, so the read itself does not need the lock. It is already read without it, from the context switch path and /proc/PID/status.
Add untagged_addr_remote_unlocked() for callers that have not taken the mmap lock, and annotate access to mm->context.untag_mask with READ_ONCE() and WRITE_ONCE() so the existing lockless reads are explicit and KCSAN-clean. untagged_addr_remote() keeps its assertion and shares the same code.
--- arch/x86/include/asm/mmu_context.h | 6 +++--- arch/x86/include/asm/uaccess_64.h | 14 +++++++++++--- arch/x86/kernel/process_64.c | 4 ++-- include/linux/uaccess.h | 7 +++++++ 4 files changed, 23 insertions(+), 8 deletions(-)
arch/x86/include/asm/mmu_context.h +3 -3
@@ -100,18 +100,18 @@ static inline unsigned long mm_lam_cr3_mask(struct mm_struct *mm)static inline void dup_lam (struct mm_struct *oldmm,struct mm_struct *mm) { mm->context.lam_cr3_mask = oldmm->context.lam_cr3_mask; - mm->context.untag_mask = oldmm->context.untag_mask; +WRITE_ONCE (mm->context.untag_mask,READ_ONCE (oldmm->context.untag_mask)); }#define mm_untag_mask mm_untag_maskstatic inline unsigned long mm_untag_mask (struct mm_struct *mm) { -return mm->context.untag_mask; +return READ_ONCE (mm->context.untag_mask); }static inline void mm_reset_untag_mask (struct mm_struct *mm) { - mm->context.untag_mask =-1UL ; +WRITE_ONCE (mm->context.untag_mask,-1UL ); }#define arch_pgtable_dma_compat arch_pgtable_dma_compat
arch/x86/include/asm/uaccess_64.h +11 -3
@@ -39,18 +39,26 @@ static inline unsigned long __untagged_addr(unsigned long addr) (__force __typeof__(addr))__untagged_addr(__addr); \ }) +/* +* mm->context.untag_mask is set once, when LAM is enabled, and never +* changes afterwards, so it can be read without holding the mmap lock. +*/ static inline unsigned long __untagged_addr_remote (struct mm_struct *mm,unsigned long addr) { -mmap_assert_locked (mm); -return addr & (mm)->context.untag_mask; +return addr &READ_ONCE (mm->context.untag_mask); } -#define untagged_addr_remote (mm, addr) ({ \ +#define untagged_addr_remote_unlocked (mm, addr) ({ \ unsigned long __addr = (__force unsigned long)(addr); \ (__force __typeof__(addr))__untagged_addr_remote(mm, __addr); \ }) +#define untagged_addr_remote (mm, addr) ({ \ + mmap_assert_locked(mm); \ + untagged_addr_remote_unlocked(mm, addr); \ +}) +#endif #define valid_user_address (x) \
arch/x86/kernel/process_64.c +2 -2
@@ -814,7 +814,7 @@ static void enable_lam_func(void *__mm)static void mm_enable_lam (struct mm_struct *mm) { mm->context.lam_cr3_mask =X86_CR3_LAM_U57 ; - mm->context.untag_mask = ~GENMASK (62 ,57 ); +WRITE_ONCE (mm->context.untag_mask, ~GENMASK (62 ,57 ));/* * Even though the process must still be single-threaded at this
@@ -952,7 +952,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)#endif #ifdef CONFIG_ADDRESS_MASKING case ARCH_GET_UNTAG_MASK : -return put_user (task->mm->context.untag_mask, +return put_user (mm_untag_mask (task->mm), (unsigned long __user *)arg2);case ARCH_ENABLE_TAGGED_ADDR :return prctl_enable_tagged_addr (task->mm, arg2);
include/linux/uaccess.h +7 -0
@@ -34,6 +34,13 @@ })#endif +#ifndef untagged_addr_remote_unlocked +#define untagged_addr_remote_unlocked (mm, addr) ({ \ + (void)(mm); \ + untagged_addr(addr); \ +}) +#endif +#ifdef masked_user_access_begin#define can_do_masked_user_access () 1# ifndef masked_user_write_access_begin
Signature · 1 line
2.53.0-Meta
Rik van Riel
reviewerRFC PATCH v3 4/6 mm/gup: add get_user_page_vma() to fault in a page under a held lock
In reply to Rik van Riel__access_remote_vm() needs a single page from a VMA it has already looked up and locked, faulting it in when necessary, under either the mmap lock or the per-VMA lock. get_user_pages_remote() does not fit: it hard codes the mmap lock and re-looks-up the VMA, neither of which is wanted here.
Add get_user_page_vma(), a simplified __get_user_pages() that walks the page tables with follow_page_mask(), faults a missing page in with faultin_page(), and on success returns it with a reference and the caller's lock still held. Like __get_user_pages() it runs check_vma_flags(), so callers need not pre-check the VMA.
A VM_IO/VM_PFNMAP VMA is the exception to that check: it can still hold COWed pages that have a struct page, so follow_page_mask() is allowed to look for one. Memory with no struct page -- a raw PFN, or a present PFN reported as -EEXIST -- is returned as -EFAULT, so the caller can reach it through vma->vm_ops->access().
The caller sets FOLL_VMA_LOCK when it holds the per-VMA lock rather than the mmap lock, which reaches the fault code as FAULT_FLAG_VMA_LOCK.
Anything that cannot complete under the per-VMA lock -- a dropped fault, a userfaultfd VMA (uffd assumes current is the faulting task), a hard error, or ->access() memory -- releases the lock and returns -EAGAIN, so the caller retries under the mmap lock.
faultin_page() reports these retries as -EAGAIN for both lock types; only the mmap caller records the dropped lock in *locked. A VM_FAULT_ERROR that decodes to no errno warns and returns -EFAULT rather than BUG(), since the mmap-lock retry produces the definitive result.
--- mm/gup.c | 154 +++++++++++++++++++++++++++++++++++++++++++------- mm/internal.h | 6 +- 2 files changed, 139 insertions(+), 21 deletions(-)
mm/gup.c +134 -20
@@ -1082,7 +1082,13 @@ static int get_gate_page(struct mm_struct *mm, unsigned long address,/* * mmap_lock must be held on entry. If @flags has FOLL_UNLOCKABLE but not * FOLL_NOWAIT, the mmap_lock may be released. If it is, *@locked will be set -* to 0 and -EBUSY returned. +* to 0 and -EAGAIN returned. +* +* The return value does not depend on the lock type: a fault that made +* progress but needs a retry (VM_FAULT_RETRY / VM_FAULT_COMPLETED) is reported +* as -EAGAIN for both the mmap lock and the per-VMA lock (FOLL_VMA_LOCK). Only +* the *@locked side effect is lock-type specific, as the per-VMA lock path has +* no unlockable mmap_lock to drop. */ static int faultin_page(struct vm_area_struct *vma, unsigned long address, unsigned int flags, bool unshare,
@@ -1097,6 +1103,8 @@ static int faultin_page(struct vm_area_struct *vma, fault_flags |=FAULT_FLAG_WRITE ;if (flags &FOLL_REMOTE ) fault_flags |=FAULT_FLAG_REMOTE ; +if (flags &FOLL_VMA_LOCK ) + fault_flags |=FAULT_FLAG_VMA_LOCK ;if (flags &FOLL_UNLOCKABLE ) { fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_KILLABLE ;/*
@@ -1125,41 +1133,147 @@ static int faultin_page(struct vm_area_struct *vma, ret =handle_mm_fault (vma, address, fault_flags,NULL ); +/* +* A fully completed fault (VM_FAULT_COMPLETED) or one that needs a retry +* (VM_FAULT_RETRY) has released the lock it was holding. Report both as +* -EAGAIN so the caller retries: the mmap lock caller retakes it here, +* the per-VMA lock caller (FOLL_VMA_LOCK) falls back to the mmap lock. +* +* Dropping the mmap lock is recorded in *@locked. There is no such lock +* to drop under the per-VMA lock, where @locked is not used, so leave it +* alone in that case. +*/ if (ret &VM_FAULT_COMPLETED ) { -/* -* With FAULT_FLAG_RETRY_NOWAIT we'll never release the -* mmap lock in the page fault handler. Sanity check this. -*/ -WARN_ON_ONCE (fault_flags &FAULT_FLAG_RETRY_NOWAIT ); - *locked =0 ; - -/* -* We should do the same as VM_FAULT_RETRY, but let's not -* return -EBUSY since that's not reflecting the reality of -* what has happened - we've just fully completed a page -* fault, with the mmap lock released. Use -EAGAIN to show -* that we want to take the mmap lock _again_. -*/ +if (!(flags &FOLL_VMA_LOCK )) { +/* +* With FAULT_FLAG_RETRY_NOWAIT we'll never release the +* mmap lock in the page fault handler. Sanity check this. +*/ +WARN_ON_ONCE (fault_flags &FAULT_FLAG_RETRY_NOWAIT ); + *locked =0 ; + }return -EAGAIN ; }if (ret &VM_FAULT_ERROR ) {int err =vm_fault_to_errno (ret, flags); -if (err) -return err; -BUG (); +/* +* VM_FAULT_ERROR always decodes to an errno; a zero here would +* mean handle_mm_fault() returned an unexpected combination. +* Report -EFAULT rather than crash: under the per-VMA lock the +* mmap lock retry produces the definitive result. +*/ +VM_WARN_ON_ONCE (!err); +return err ? err : -EFAULT ; }if (ret &VM_FAULT_RETRY ) { -if (!(fault_flags &FAULT_FLAG_RETRY_NOWAIT )) +if (!(flags &FOLL_VMA_LOCK ) && + !(fault_flags &FAULT_FLAG_RETRY_NOWAIT )) *locked =0 ; -return -EBUSY ; +return -EAGAIN ; }return 0 ; } +/* +* get_user_page_vma - get one page from @vma, whose lock the caller already +* holds: the mmap lock, or (with FOLL_VMA_LOCK) the per-VMA lock. Walks the +* page tables, faulting the page in if needed, and on success returns it with +* a reference and the lock still held. +* +* Runs check_vma_flags() like __get_user_pages(), so callers need not pre-check +* the VMA; most rejections are returned as their error. A VM_IO/VM_PFNMAP VMA +* is the exception: a COWed page with a struct page is returned, while a raw +* PFN has none and yields -EFAULT, to be reached via vma->vm_ops->access(). +* +* Under FOLL_VMA_LOCK, anything that cannot be finished under the per-VMA lock +* (a dropped fault, userfaultfd, a hard error, or ->access() memory) releases +* the lock and returns -EAGAIN, so the caller retries under the mmap lock. +*/ +struct page *get_user_page_vma (struct vm_area_struct *vma,unsigned long addr, +unsigned int gup_flags) +{ +bool vma_locked = gup_flags &FOLL_VMA_LOCK ; +unsigned long page_mask; +struct page *page; +int locked =1 ; +bool pfnmap; +int ret; + +/* +* Validate the VMA up front, like __get_user_pages(). A VM_IO/VM_PFNMAP +* VMA is not rejected outright: it can hold COWed pages that have a +* struct page, so let follow_page_mask() look for one, and treat only +* its struct-page-less PFNs as unreachable. Any other rejection +* (secretmem, bad permissions, ...) is final. +*/ + ret =check_vma_flags (vma, gup_flags); +if (ret && !(vma->vm_flags & (VM_IO |VM_PFNMAP ))) + goto fail; + pfnmap = ret; + +for (;;) { +if (fatal_signal_pending (current)) { + ret = -EINTR ; + goto fail; + } +cond_resched (); + + page =follow_page_mask (vma, addr, + gup_flags |FOLL_TOUCH |FOLL_GET , + &page_mask); +if (!IS_ERR_OR_NULL (page)) +return page; + +/* +* No struct page: a raw PFN of a VM_IO/VM_PFNMAP VMA, whether +* seen by the up-front check (@pfnmap) or reported as -EEXIST +* for a present PFN. Return -EFAULT so the caller reaches it +* through vma->vm_ops->access(). +*/ +if (pfnmap ||PTR_ERR (page) == -EEXIST ) { + ret = -EFAULT ; + goto fail; + } +/* A hard error from the walk itself. */ +if (page &&PTR_ERR (page) != -EMLINK ) { + ret =PTR_ERR (page); + goto fail; + } + +/* +* The page is not present, or needs unsharing. A remote fault +* under the per-VMA lock cannot deliver userfaultfd (which +* assumes current is the faulting task), so fall back for those. +*/ +if (vma_locked &&userfaultfd_armed (vma)) { + ret = -EAGAIN ; + goto fail; + } + ret =faultin_page (vma, addr, gup_flags |FOLL_REMOTE |FOLL_GET , +PTR_ERR (page) == -EMLINK , &locked); +if (ret == -EAGAIN ) +return ERR_PTR (-EAGAIN );/* fault released the per-VMA lock */ +if (ret) + goto fail; + } + +fail: +/* +* Under the per-VMA lock the caller cannot reach ->access() or act on a +* hard error (both need the mmap lock), so release the lock and have it +* retry there; the mmap-lock pass produces the definitive error. +*/ +if (vma_locked) { +vma_end_read (vma); +return ERR_PTR (-EAGAIN ); + } +return ERR_PTR (ret); +} +/* * Writing to file-backed mappings which require folio dirty tracking using GUP * is a fundamentally broken operation, as kernel write access to GUP mappings
mm/internal.h +5 -1
@@ -1595,6 +1595,8 @@ struct vm_struct *__get_vm_area_node(unsigned long size,*/ int __must_check try_grab_folio (struct folio *folio,int refs,unsigned int flags); +struct page *get_user_page_vma (struct vm_area_struct *vma,unsigned long addr, +unsigned int gup_flags);/* * mm/huge_memory.c
@@ -1641,11 +1643,13 @@ enum {FOLL_UNLOCKABLE =1 <<21 ,/* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */ FOLL_MADV_POPULATE =1 <<22 , +/* caller holds the per-VMA lock, not the mmap lock */ + FOLL_VMA_LOCK = 1 << 23, };#define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \ FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \ - FOLL_MADV_POPULATE) + FOLL_MADV_POPULATE | FOLL_VMA_LOCK)/* * Indicates for which pages that are write-protected in the page table,
Signature · 1 line
2.53.0-Meta
Rik van Riel
reviewerRFC PATCH v3 5/6 mm: use per-VMA lock in __access_remote_vm() for single-VMA accesses
In reply to Rik van Riel__access_remote_vm() holds mmap_read_lock() for the whole transfer. On large machines that lock is contended by tasks reading /proc/PID/cmdline, /proc/PID/environ, or /proc/PID/mem, even though the memory they read is almost always resident.
Use the per-VMA lock to access a remote process's memory, provided that access is entirely within one VMA. Fall back to the mmap_lock if the access crosses VMA boundaries, or when get_user_page_vma() cannot finish the access under the per-VMA lock.
Walking the page tables under only the per-VMA lock is safe when page table pages are freed via RCU.
pte_offset_map() takes the RCU read lock, so a PTE page freed by a concurrent THP collapse stays valid for the walk; mid-collapse it returns NULL and the page is faulted in.
Higher level page tables cannot be freed under us, since munmap() and collapse take the VMA write lock that our read lock excludes.
get_user_page_vma() returns -EFAULT for memory with no struct page: the raw PFNs of a VM_IO/VM_PFNMAP VMA, ioremapped device memory reached through ptrace and /proc/PID/mem.
Handle that in the IS_ERR(page) branch by reaching it through vma->vm_ops->access(), under the mmap lock, via a new access_remote_vma_ops() helper -- as get_user_pages_remote() and the old ->access() fallback did before.
A COWed page in such a VMA has a struct page and is now returned normally by get_user_page_vma(). Previously it was routed to ->access() and could not be read, since generic_access_phys() ioremaps the PFN and ioremap of RAM is rejected.
--- mm/gup.c | 2 +- mm/internal.h | 1 + mm/memory.c | 169 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 126 insertions(+), 46 deletions(-)
mm/gup.c +1 -1
@@ -1311,7 +1311,7 @@ static bool writable_file_mapping_allowed(struct vm_area_struct *vma,return !vma_needs_dirty_tracking (vma); } -static int check_vma_flags (struct vm_area_struct *vma,unsigned long gup_flags) +int check_vma_flags (struct vm_area_struct *vma,unsigned long gup_flags) {vm_flags_t vm_flags = vma->vm_flags;int write = (gup_flags &FOLL_WRITE );
mm/internal.h +1 -0
@@ -1595,6 +1595,7 @@ struct vm_struct *__get_vm_area_node(unsigned long size,*/ int __must_check try_grab_folio (struct folio *folio,int refs,unsigned int flags); +int check_vma_flags (struct vm_area_struct *vma,unsigned long gup_flags);struct page *get_user_page_vma (struct vm_area_struct *vma,unsigned long addr,unsigned int gup_flags);
mm/memory.c +124 -45
207 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Rik van Riel
reviewerRFC PATCH v3 6/6 selftests/mm: cover /proc/pid/mem access to VM_PFNMAP memory
In reply to Rik van RielReading a VM_PFNMAP mapping through /proc/pid/mem exercises __access_remote_vm() two ways: a COWed page has a struct page and is returned by get_user_page_vma(), while a raw PFN has none and is reached through vma->vm_ops->access().
Add two tests to pfnmap.c, both reading VM_PFNMAP memory through /proc/self/mem.
procmem_cow_read maps the file MAP_PRIVATE and writable, writes to COW a page, then reads it back. Without the struct-page path in get_user_page_vma() this read is short: the access falls back to generic_access_phys(), which ioremaps the PFN, and ioremap of a COWed RAM page is rejected.
procmem_pfn_read reads a raw PFN back through ->access(). ioremap rejects RAM, so it runs only for genuine device memory and is skipped for the default /dev/mem System RAM target.
--- tools/testing/selftests/mm/pfnmap.c | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
tools/testing/selftests/mm/pfnmap.c +66 -0
@@ -31,6 +31,7 @@ static sigjmp_buf sigjmp_buf_env;static char *file ="/dev/mem" ;static off_t file_offset;static int fd; +static int target_is_ram;static void signal_handler (int sig) {
@@ -113,6 +114,7 @@ static void pfnmap_init(void)if (err)ksft_exit_skip ("Cannot find ram target in '/proc/iomem': %s\n" ,strerror (-err)); + target_is_ram =1 ; }else { file_offset =0 ; }
@@ -271,6 +273,70 @@ TEST_F(pfnmap, fork)ASSERT_EQ (ret,0 ); } +TEST_F(pfnmap, procmem_cow_read) +{ +char *priv, *buf; +ssize_t rc; +int mem_fd; + +/* +* A COWed page in a VM_PFNMAP mapping has a struct page, so reading it +* through /proc/self/mem -- __access_remote_vm() -> get_user_page_vma() +* -- returns it directly, instead of routing to vma->vm_ops->access(), +* which ioremaps the PFN and cannot reach a COWed RAM page. +* +* Map the file MAP_PRIVATE and writable, write to COW a page into anon +* memory, then read the page back through /proc/self/mem. +*/ + self->size2 = self->pagesize; + self->addr2 =mmap (NULL , self->size2,PROT_READ |PROT_WRITE , +MAP_PRIVATE , fd, file_offset); +if (self->addr2 ==MAP_FAILED ) +SKIP (return,"Cannot create a writable private pfnmap mapping" ); + priv = self->addr2; + +/* COW the page and stamp known bytes into the anon copy. */ + priv[0 ] =0x42 ; + priv[self->pagesize -1 ] =0x24 ; + + buf =malloc (self->pagesize); +ASSERT_NE (buf,NULL ); + + mem_fd =open ("/proc/self/mem" ,O_RDONLY ); +ASSERT_GE (mem_fd,0 ); + rc =pread (mem_fd, buf, self->pagesize, (off_t )(uintptr_t )priv); +close (mem_fd); + +ASSERT_EQ (rc, (ssize_t )self->pagesize); +EXPECT_EQ (buf[0 ],0x42 ); +EXPECT_EQ (buf[self->pagesize -1 ],0x24 ); + +free (buf); +} + +TEST_F(pfnmap, procmem_pfn_read) +{ +char buf[64 ]; +ssize_t rc; +int mem_fd; + +/* +* A raw PFN of a VM_IO/VM_PFNMAP mapping has no struct page, so +* __access_remote_vm() reaches it through vma->vm_ops->access() +* (generic_access_phys()). That ioremaps the PFN, which is rejected for +* RAM, so this only applies to genuine device memory. +*/ +if (target_is_ram) +SKIP (return,"Target is System RAM; ->access() cannot ioremap RAM" ); + + mem_fd =open ("/proc/self/mem" ,O_RDONLY ); +ASSERT_GE (mem_fd,0 ); + rc =pread (mem_fd, buf,sizeof (buf), (off_t )(uintptr_t )self->addr1); +close (mem_fd); + +ASSERT_EQ (rc, (ssize_t )sizeof (buf)); +} +int main (int argc,char **argv) {for (int i =1 ; i < argc; i++) {
Signature · 1 line
2.53.0-Meta
Rik van Riel
reviewerRFC PATCH v3 2/6 riscv/mm: add untagged_addr_remote_unlocked()
In reply to Rik van Riel__access_remote_vm() untags the remote address before looking up the VMA, now without holding the mmap lock. riscv defines untagged_addr_remote() but not untagged_addr_remote_unlocked(), so it falls back to the generic version, which untags with untagged_addr().
That reads current->mm, not the target mm, so a remote access to a process using pointer masking would untag with the wrong mask.
mm->context.pmlen is set only through PR_SET_TAGGED_ADDR_CTRL and is stable afterwards, so it can be read without the mmap lock, as it already is from untagged_addr() and mm_untag_mask().
Add untagged_addr_remote_unlocked(), which untags against the target mm, and annotate context.pmlen accesses with READ_ONCE() and WRITE_ONCE() so the lockless reads are explicit and KCSAN-clean. untagged_addr_remote() keeps its mmap_assert_locked() and shares the code.
--- arch/riscv/include/asm/mmu_context.h | 4 ++-- arch/riscv/include/asm/uaccess.h | 10 +++++++--- arch/riscv/kernel/process.c | 12 +++++++----- 3 files changed, 16 insertions(+), 10 deletions(-)
arch/riscv/include/asm/mmu_context.h +2 -2
@@ -21,7 +21,7 @@ static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next) {#ifdef CONFIG_RISCV_ISA_SUPM - next->context.pmlen =0 ; +WRITE_ONCE (next->context.pmlen,0 );#endif switch_mm (prev, next,NULL ); }
@@ -44,7 +44,7 @@ DECLARE_STATIC_KEY_FALSE(use_asid_allocator);#define mm_untag_mask mm_untag_maskstatic inline unsigned long mm_untag_mask (struct mm_struct *mm) { -return -1UL >> mm->context.pmlen; +return -1UL >>READ_ONCE (mm->context.pmlen); }#endif
arch/riscv/include/asm/uaccess.h +7 -3
@@ -16,7 +16,7 @@static inline unsigned long __untagged_addr_remote (struct mm_struct *mm,unsigned long addr) {if (riscv_has_extension_unlikely (RISCV_ISA_EXT_SUPM )) { -u8 pmlen = mm->context.pmlen; +u8 pmlen =READ_ONCE (mm->context.pmlen);/* Virtual addresses are sign-extended; physical addresses are zero-extended. */ if (IS_ENABLED (CONFIG_MMU ))
@@ -33,12 +33,16 @@ static inline unsigned long __untagged_addr_remote(struct mm_struct *mm, unsigne (__force __typeof__(addr))__untagged_addr_remote(current->mm, __addr); \ }) -#define untagged_addr_remote (mm, addr) ({ \ +#define untagged_addr_remote_unlocked (mm, addr) ({ \ unsigned long __addr = (__force unsigned long)(addr); \ - mmap_assert_locked(mm); \ (__force __typeof__(addr))__untagged_addr_remote(mm, __addr); \ }) +#define untagged_addr_remote (mm, addr) ({ \ + mmap_assert_locked(mm); \ + untagged_addr_remote_unlocked(mm, addr); \ +}) +#define access_ok (addr, size) likely(__access_ok(untagged_addr(addr), size))#else #define untagged_addr (addr) (addr)
arch/riscv/kernel/process.c +7 -5
@@ -357,13 +357,15 @@ long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg)if (mmap_write_lock_killable (mm))return -EINTR ; -if (test_bit (MM_CONTEXT_LOCK_PMLEN , &mm->context.flags) && mm->context.pmlen != pmlen) { -mmap_write_unlock (mm); -return -EBUSY ; +if (test_bit (MM_CONTEXT_LOCK_PMLEN , &mm->context.flags)) { +if (READ_ONCE (mm->context.pmlen) != pmlen) { +mmap_write_unlock (mm); +return -EBUSY ; + } }envcfg_update_bits (task,ENVCFG_PMM , pmm); - mm->context.pmlen = pmlen; +WRITE_ONCE (mm->context.pmlen, pmlen);mmap_write_unlock (mm);
@@ -394,7 +396,7 @@ long get_tagged_addr_ctrl(struct task_struct *task)break ; } -if (task->mm->context.pmlen) +if (READ_ONCE (task->mm->context.pmlen)) ret |=PR_TAGGED_ADDR_ENABLE ;return ret;
Signature · 1 line
2.53.0-Meta
Rik van Riel
reviewerRFC PATCH v3 3/6 mm: rename get_user_page_vma_remote() to get_user_page_lookup_vma()
In reply to Rik van Rielget_user_page_vma_remote() faults in the page at @addr in a remote mm and also looks up the VMA that covers it, handing both back to the caller.
This cleans up the name space for adding a get_user_page_vma() variant where the caller already has the vma.
--- arch/arm64/kernel/mte.c | 2 +- arch/x86/kernel/uprobes.c | 2 +- include/linux/mm.h | 2 +- mm/memory.c | 4 ++-- mm/rmap.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kernel/mte.c +1 -1
@@ -459,7 +459,7 @@ static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,struct vm_area_struct *vma;unsigned long tags, offset;void *maddr; -struct page *page =get_user_page_vma_remote (mm, addr, +struct page *page =get_user_page_lookup_vma (mm, addr, gup_flags, &vma);struct folio *folio;
arch/x86/kernel/uprobes.c +1 -1
@@ -1036,7 +1036,7 @@ static int copy_from_vaddr(struct mm_struct *mm, unsigned long vaddr, void *dst,struct vm_area_struct *vma;struct page *page; - page =get_user_page_vma_remote (mm, vaddr, gup_flags, &vma); + page =get_user_page_lookup_vma (mm, vaddr, gup_flags, &vma);if (IS_ERR (page))return PTR_ERR (page);uprobe_copy_from_page (page, vaddr, dst, len);
include/linux/mm.h +1 -1
@@ -3238,7 +3238,7 @@ long pin_user_pages_remote(struct mm_struct *mm,/* * Retrieves a single page alongside its VMA. Does not support FOLL_NOWAIT. */ -static inline struct page *get_user_page_vma_remote (struct mm_struct *mm, +static inline struct page *get_user_page_lookup_vma (struct mm_struct *mm,unsigned long addr,int gup_flags,struct vm_area_struct **vmap)
mm/memory.c +2 -2
@@ -7039,7 +7039,7 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,void *maddr;struct folio *folio;struct vm_area_struct *vma =NULL ; -struct page *page =get_user_page_vma_remote (mm, addr, +struct page *page =get_user_page_lookup_vma (mm, addr, gup_flags, &vma);if (IS_ERR (page)) {
@@ -7167,7 +7167,7 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr,struct page *page;struct vm_area_struct *vma =NULL ; - page =get_user_page_vma_remote (mm, addr, gup_flags, &vma); + page =get_user_page_lookup_vma (mm, addr, gup_flags, &vma);if (IS_ERR (page)) {/* * Treat as a total failure for now until we decide how
mm/rmap.c +1 -1
@@ -2838,7 +2838,7 @@ struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr,* (non-device-exclusive) PTE and issue a MMU_NOTIFY_EXCLUSIVE. */ retry: - page =get_user_page_vma_remote (mm, addr, + page =get_user_page_lookup_vma (mm, addr,FOLL_GET |FOLL_WRITE |FOLL_SPLIT_PMD , &vma);if (IS_ERR (page))
Signature · 1 line
2.53.0-Meta
Re: [PATCH RFC v3 1/6] x86/mm: add untagged_addr_remote_unlocked()
In reply to Rik van RielQuoted 14 lines · On Fri, 17 Jul 2026 13:00:31 -0400 Rik van Riel <[email protected]> wrote:
__access_remote_vm()reads another process's memory under the mmap lock. On large machines that lock is contended by tasks polling/proc/PID/cmdline,/proc/PID/environ, or callingprocess_vm_readv(), even though the memory they read is almost always resident and could be reached under the per-VMA lock instead.Looking up the VMA first requires untagging the address.
untagged_addr_remote()asserts the mmap lock only because it readsmm->context.untag_mask, which can race with the write inmm_enable_lam().That mask is set once, when LAM is enabled, and never changes afterwards, so the read itself does not need the lock. It is already read without it, from the context switch path and
/proc/PID/status.
Quoted 8 lines
Add
untagged_addr_remote_unlocked()for callers that have not taken the mmap lock, and annotate access tomm->context.untag_maskwithREAD_ONCE()andWRITE_ONCE()so the existing lockless reads are explicit and KCSAN-clean.untagged_addr_remote()keeps its assertion and shares the same code.Assisted-by Claude:claude-opus-4.8Acked-by Usama Arif <[email protected]>
Hi Rik,
Would you mind changing this to
in the next revision
Thanks!
Re: [PATCH RFC v3 2/6] riscv/mm: add untagged_addr_remote_unlocked()
In reply to Rik van RielQuoted 2 lines · On Fri, 17 Jul 2026 13:00:32 -0400 Rik van Riel <[email protected]> wrote:
__access_remote_vm()untags the remote address before looking up the VMA, now without holding the mmap lock. riscv definesuntagged_addr_remote()but
This describes behavior introduced in a later patch, not by this patch. At this point, __access_remote_vm() still acquires mmap_lock before untagging the address.
Quoted 10 lines
not
untagged_addr_remote_unlocked(), so it falls back to the generic version, which untags withuntagged_addr().That reads
current->mm, not the target mm, so a remote access to a process using pointer masking would untag with the wrong mask.
mm->context.pmlenis set only throughPR_SET_TAGGED_ADDR_CTRLand is stable afterwards, so it can be read without the mmap lock, as it already is fromuntagged_addr()andmm_untag_mask().
I think it might not be stable? set_tagged_addr_ctrl() can change it repeatedly until a CLONE_VM operation sets MM_CONTEXT_LOCK_PMLEN.
Quoted 104 lines
Add
untagged_addr_remote_unlocked(), which untags against the target mm, and annotate context.pmlen accesses withREAD_ONCE()andWRITE_ONCE()so the lockless reads are explicit and KCSAN-clean.untagged_addr_remote()keeps itsmmap_assert_locked()and shares the code.Assisted-by Claude:claude-opus-4.8Signed-off-by Rik van Riel <[email protected]>--- arch/riscv/include/asm/mmu_context.h | 4 ++-- arch/riscv/include/asm/uaccess.h | 10 +++++++--- arch/riscv/kernel/process.c | 12 +++++++----- 3 files changed, 16 insertions(+), 10 deletions(-)arch/riscv/include/asm/mmu_context.h +2
-2@@ -21,7 +21,7 @@ static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next) {#ifdef CONFIG_RISCV_ISA_SUPM - next->context.pmlen =0 ; +WRITE_ONCE (next->context.pmlen,0 );#endif switch_mm (prev, next,NULL ); }@@ -44,7 +44,7 @@ DECLARE_STATIC_KEY_FALSE(use_asid_allocator);#define mm_untag_mask mm_untag_maskstatic inline unsigned long mm_untag_mask (struct mm_struct *mm) { -return -1UL >> mm->context.pmlen; +return -1UL >>READ_ONCE (mm->context.pmlen); }#endif arch/riscv/include/asm/uaccess.h +7
-3@@ -16,7 +16,7 @@static inline unsigned long __untagged_addr_remote (struct mm_struct *mm,unsigned long addr) {if (riscv_has_extension_unlikely (RISCV_ISA_EXT_SUPM )) { -u8 pmlen = mm->context.pmlen; +u8 pmlen =READ_ONCE (mm->context.pmlen);/* Virtual addresses are sign-extended; physical addresses are zero-extended. */ if (IS_ENABLED (CONFIG_MMU ))@@ -33,12 +33,16 @@ static inline unsigned long __untagged_addr_remote(struct mm_struct *mm, unsigne (__force __typeof__(addr))__untagged_addr_remote(current->mm, __addr); \ }) -#define untagged_addr_remote (mm, addr) ({ \ +#define untagged_addr_remote_unlocked (mm, addr) ({ \ unsigned long __addr = (__force unsigned long)(addr); \ - mmap_assert_locked(mm); \ (__force __typeof__(addr))__untagged_addr_remote(mm, __addr); \ }) +#define untagged_addr_remote (mm, addr) ({ \ + mmap_assert_locked(mm); \ + untagged_addr_remote_unlocked(mm, addr); \ +}) +#define access_ok (addr, size) likely(__access_ok(untagged_addr(addr), size))#else #define untagged_addr (addr) (addr)arch/riscv/kernel/process.c +7
-5@@ -357,13 +357,15 @@ long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg)if (mmap_write_lock_killable (mm))return -EINTR ; -if (test_bit (MM_CONTEXT_LOCK_PMLEN , &mm->context.flags) && mm->context.pmlen != pmlen) { -mmap_write_unlock (mm); -return -EBUSY ; +if (test_bit (MM_CONTEXT_LOCK_PMLEN , &mm->context.flags)) { +if (READ_ONCE (mm->context.pmlen) != pmlen) { +mmap_write_unlock (mm); +return -EBUSY ; + } }envcfg_update_bits (task,ENVCFG_PMM , pmm); - mm->context.pmlen = pmlen; +WRITE_ONCE (mm->context.pmlen, pmlen);mmap_write_unlock (mm);@@ -394,7 +396,7 @@ long get_tagged_addr_ctrl(struct task_struct *task)break ; } -if (task->mm->context.pmlen) +if (READ_ONCE (task->mm->context.pmlen)) ret |=PR_TAGGED_ADDR_ENABLE ;return ret;Signature · 1 line
2.53.0-Meta
Re: [PATCH RFC v3 3/6] mm: rename get_user_page_vma_remote() to get_user_page_lookup_vma()
In reply to Rik van RielQuoted 4 lines · On Fri, 17 Jul 2026 13:00:33 -0400 Rik van Riel <[email protected]> wrote:
get_user_page_vma_remote()faults in the page at @addr in a remote mm and also looks up the VMA that covers it, handing both back to the caller.This cleans up the name space for adding a
get_user_page_vma()variant
nit: s/name space/namespace/
Quoted 4 lines
where the caller already has the vma.
Assisted-by Claude:claude-opus-4.8Signed-off-by Rik van Riel <[email protected]>
Quoted 8 lines
--- arch/arm64/kernel/mte.c | 2 +- arch/x86/kernel/uprobes.c | 2 +- include/linux/mm.h | 2 +- mm/memory.c | 4 ++-- mm/rmap.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
I think good to add no functional change intended in the patch.
Quoted 78 lines
arch/arm64/kernel/mte.c +1
-1@@ -459,7 +459,7 @@ static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,struct vm_area_struct *vma;unsigned long tags, offset;void *maddr; -struct page *page =get_user_page_vma_remote (mm, addr, +struct page *page =get_user_page_lookup_vma (mm, addr, gup_flags, &vma);struct folio *folio;arch/x86/kernel/uprobes.c +1
-1@@ -1036,7 +1036,7 @@ static int copy_from_vaddr(struct mm_struct *mm, unsigned long vaddr, void *dst,struct vm_area_struct *vma;struct page *page; - page =get_user_page_vma_remote (mm, vaddr, gup_flags, &vma); + page =get_user_page_lookup_vma (mm, vaddr, gup_flags, &vma);if (IS_ERR (page))return PTR_ERR (page);uprobe_copy_from_page (page, vaddr, dst, len);include/linux/mm.h +1
-1@@ -3238,7 +3238,7 @@ long pin_user_pages_remote(struct mm_struct *mm,/* * Retrieves a single page alongside its VMA. Does not support FOLL_NOWAIT. */ -static inline struct page *get_user_page_vma_remote (struct mm_struct *mm, +static inline struct page *get_user_page_lookup_vma (struct mm_struct *mm,unsigned long addr,int gup_flags,struct vm_area_struct **vmap)mm/memory.c +2
-2@@ -7039,7 +7039,7 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,void *maddr;struct folio *folio;struct vm_area_struct *vma =NULL ; -struct page *page =get_user_page_vma_remote (mm, addr, +struct page *page =get_user_page_lookup_vma (mm, addr, gup_flags, &vma);if (IS_ERR (page)) {@@ -7167,7 +7167,7 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr,struct page *page;struct vm_area_struct *vma =NULL ; - page =get_user_page_vma_remote (mm, addr, gup_flags, &vma); + page =get_user_page_lookup_vma (mm, addr, gup_flags, &vma);if (IS_ERR (page)) {/* * Treat as a total failure for now until we decide how mm/rmap.c +1
-1@@ -2838,7 +2838,7 @@ struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr,* (non-device-exclusive) PTE and issue a MMU_NOTIFY_EXCLUSIVE. */ retry: - page =get_user_page_vma_remote (mm, addr, + page =get_user_page_lookup_vma (mm, addr,FOLL_GET |FOLL_WRITE |FOLL_SPLIT_PMD , &vma);if (IS_ERR (page))Signature · 1 line
2.53.0-Meta
Re: [PATCH RFC v3 4/6] mm/gup: add get_user_page_vma() to fault in a page under a held lock
In reply to Rik van RielQuoted 44 lines · On Fri, 17 Jul 2026 13:00:34 -0400 Rik van Riel <[email protected]> wrote:
__access_remote_vm()needs a single page from a VMA it has already looked up and locked, faulting it in when necessary, under either the mmap lock or the per-VMA lock.get_user_pages_remote()does not fit: it hard codes the mmap lock and re-looks-up the VMA, neither of which is wanted here.Add
get_user_page_vma(), a simplified__get_user_pages()that walks the page tables withfollow_page_mask(), faults a missing page in withfaultin_page(), and on success returns it with a reference and the caller's lock still held. Like__get_user_pages()it runscheck_vma_flags(), so callers need not pre-check the VMA.A VM_IO/VM_PFNMAP VMA is the exception to that check: it can still hold COWed pages that have a struct page, so
follow_page_mask()is allowed to look for one. Memory with no struct page -- a raw PFN, or a present PFN reported as-EEXIST-- is returned as-EFAULT, so the caller can reach it throughvma->vm_ops->access().The caller sets
FOLL_VMA_LOCKwhen it holds the per-VMA lock rather than the mmap lock, which reaches the fault code asFAULT_FLAG_VMA_LOCK.Anything that cannot complete under the per-VMA lock -- a dropped fault, a userfaultfd VMA (uffd assumes current is the faulting task), a hard error, or ->access() memory -- releases the lock and returns
-EAGAIN, so the caller retries under the mmap lock.
faultin_page()reports these retries as-EAGAINfor both lock types; only the mmap caller records the dropped lock in *locked. AVM_FAULT_ERRORthat decodes to no errno warns and returns-EFAULTrather thanBUG(), since the mmap-lock retry produces the definitive result.Assisted-by Claude:claude-opus-4.8Signed-off-by Rik van Riel <[email protected]>--- mm/gup.c | 154 +++++++++++++++++++++++++++++++++++++++++++------- mm/internal.h | 6 +- 2 files changed, 139 insertions(+), 21 deletions(-)mm/gup.c +0
-0
mm/gup.c static int get_gate_page(struct mm_struct *mm, unsigned long address, · line 1083
Quoted 2 lines
mm/gup.c +0
-0@@ -1082,7 +1082,13 @@ static int get_gate_page(struct mm_struct *mm, unsigned long address,/* * mmap_lock must be held on entry. If @flags has FOLL_UNLOCKABLE but not
Does the above line also need to be changed? mmap_lock is not held on entry in faultin_page() if passing FOLL_VMA_LOCK, right?
Quoted 113 lines
mm/gup.c +62
-20* FOLL_NOWAIT, the mmap_lock may be released. If it is, *@locked will be set -* to 0 and -EBUSY returned. +* to 0 and -EAGAIN returned. +* +* The return value does not depend on the lock type: a fault that made +* progress but needs a retry (VM_FAULT_RETRY / VM_FAULT_COMPLETED) is reported +* as -EAGAIN for both the mmap lock and the per-VMA lock (FOLL_VMA_LOCK). Only +* the *@locked side effect is lock-type specific, as the per-VMA lock path has +* no unlockable mmap_lock to drop. */ static int faultin_page(struct vm_area_struct *vma, unsigned long address, unsigned int flags, bool unshare,@@ -1097,6 +1103,8 @@ static int faultin_page(struct vm_area_struct *vma, fault_flags |=FAULT_FLAG_WRITE ;if (flags &FOLL_REMOTE ) fault_flags |=FAULT_FLAG_REMOTE ; +if (flags &FOLL_VMA_LOCK ) + fault_flags |=FAULT_FLAG_VMA_LOCK ;if (flags &FOLL_UNLOCKABLE ) { fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_KILLABLE ;/* @@ -1125,41 +1133,147 @@ static int faultin_page(struct vm_area_struct *vma, ret =handle_mm_fault (vma, address, fault_flags,NULL ); +/* +* A fully completed fault (VM_FAULT_COMPLETED) or one that needs a retry +* (VM_FAULT_RETRY) has released the lock it was holding. Report both as +* -EAGAIN so the caller retries: the mmap lock caller retakes it here, +* the per-VMA lock caller (FOLL_VMA_LOCK) falls back to the mmap lock. +* +* Dropping the mmap lock is recorded in *@locked. There is no such lock +* to drop under the per-VMA lock, where @locked is not used, so leave it +* alone in that case. +*/ if (ret &VM_FAULT_COMPLETED ) { -/* -* With FAULT_FLAG_RETRY_NOWAIT we'll never release the -* mmap lock in the page fault handler. Sanity check this. -*/ -WARN_ON_ONCE (fault_flags &FAULT_FLAG_RETRY_NOWAIT ); - *locked =0 ; - -/* -* We should do the same as VM_FAULT_RETRY, but let's not -* return -EBUSY since that's not reflecting the reality of -* what has happened - we've just fully completed a page -* fault, with the mmap lock released. Use -EAGAIN to show -* that we want to take the mmap lock _again_. -*/ +if (!(flags &FOLL_VMA_LOCK )) { +/* +* With FAULT_FLAG_RETRY_NOWAIT we'll never release the +* mmap lock in the page fault handler. Sanity check this. +*/ +WARN_ON_ONCE (fault_flags &FAULT_FLAG_RETRY_NOWAIT ); + *locked =0 ; + }return -EAGAIN ; }if (ret &VM_FAULT_ERROR ) {int err =vm_fault_to_errno (ret, flags); -if (err) -return err; -BUG (); +/* +* VM_FAULT_ERROR always decodes to an errno; a zero here would +* mean handle_mm_fault() returned an unexpected combination. +* Report -EFAULT rather than crash: under the per-VMA lock the +* mmap lock retry produces the definitive result. +*/ +VM_WARN_ON_ONCE (!err); +return err ? err : -EFAULT ; }if (ret &VM_FAULT_RETRY ) { -if (!(fault_flags &FAULT_FLAG_RETRY_NOWAIT )) +if (!(flags &FOLL_VMA_LOCK ) && + !(fault_flags &FAULT_FLAG_RETRY_NOWAIT )) *locked =0 ; -return -EBUSY ; +return -EAGAIN ; }return 0 ; } +/* +* get_user_page_vma - get one page from @vma, whose lock the caller already +* holds: the mmap lock, or (with FOLL_VMA_LOCK) the per-VMA lock. Walks the +* page tables, faulting the page in if needed, and on success returns it with +* a reference and the lock still held. +* +* Runs check_vma_flags() like __get_user_pages(), so callers need not pre-check +* the VMA; most rejections are returned as their error. A VM_IO/VM_PFNMAP VMA +* is the exception: a COWed page with a struct page is returned, while a raw +* PFN has none and yields -EFAULT, to be reached via vma->vm_ops->access(). +* +* Under FOLL_VMA_LOCK, anything that cannot be finished under the per-VMA lock +* (a dropped fault, userfaultfd, a hard error, or ->access() memory) releases +* the lock and returns -EAGAIN, so the caller retries under the mmap lock. +*/ +struct page *get_user_page_vma (struct vm_area_struct *vma,unsigned long addr, +unsigned int gup_flags) +{ +bool vma_locked = gup_flags &FOLL_VMA_LOCK ; +unsigned long page_mask; +struct page *page; +int locked =1 ; +bool pfnmap; +int ret;
mm/gup.c static int faultin_page(struct vm_area_struct *vma, · line 1214
Quoted 10 lines
mm/gup.c +10
-0+ +/* +* Validate the VMA up front, like __get_user_pages(). A VM_IO/VM_PFNMAP +* VMA is not rejected outright: it can hold COWed pages that have a +* struct page, so let follow_page_mask() look for one, and treat only +* its struct-page-less PFNs as unreachable. Any other rejection +* (secretmem, bad permissions, ...) is final. +*/ + ret =check_vma_flags (vma, gup_flags); +if (ret && !(vma->vm_flags & (VM_IO |VM_PFNMAP )))
Do you need to somehow restructure check_vma_flags()?
The first thing that check_vma_flags() does is:
if (vm_flags & (VM_IO | VM_PFNMAP)) return -EFAULT;
check_vma_flags() returns before evaluating VM_READ, FOLL_FORCE, FOLL_ANON or the architecture permission check.
The code then ignores that early error and may return a COWed page through follow_page_mask(). A non-FOLL_FORCE caller can therefore read a COWed page from a PROT_NONE PFNMAP VMA.
Quoted 5 lines
mm/gup.c +5
-0+ goto fail; + pfnmap = ret; + +for (;;) { +if (fatal_signal_pending (current)) {
mm/gup.c static int faultin_page(struct vm_area_struct *vma, · line 1229
Quoted 10 lines
mm/gup.c +10
-0+ ret = -EINTR ; + goto fail; + } +cond_resched (); + + page =follow_page_mask (vma, addr, + gup_flags |FOLL_TOUCH |FOLL_GET , + &page_mask); +if (!IS_ERR_OR_NULL (page)) +return page;
__get_user_pages() does
flush_anon_page(vma, subpage, start + j * PAGE_SIZE); flush_dcache_page(subpage);
before returning the page. Do you need to that here as well above?
Quoted 82 lines
mm/gup.c +47
-0+ +/* +* No struct page: a raw PFN of a VM_IO/VM_PFNMAP VMA, whether +* seen by the up-front check (@pfnmap) or reported as -EEXIST +* for a present PFN. Return -EFAULT so the caller reaches it +* through vma->vm_ops->access(). +*/ +if (pfnmap ||PTR_ERR (page) == -EEXIST ) { + ret = -EFAULT ; + goto fail; + } +/* A hard error from the walk itself. */ +if (page &&PTR_ERR (page) != -EMLINK ) { + ret =PTR_ERR (page); + goto fail; + } + +/* +* The page is not present, or needs unsharing. A remote fault +* under the per-VMA lock cannot deliver userfaultfd (which +* assumes current is the faulting task), so fall back for those. +*/ +if (vma_locked &&userfaultfd_armed (vma)) { + ret = -EAGAIN ; + goto fail; + } + ret =faultin_page (vma, addr, gup_flags |FOLL_REMOTE |FOLL_GET , +PTR_ERR (page) == -EMLINK , &locked); +if (ret == -EAGAIN ) +return ERR_PTR (-EAGAIN );/* fault released the per-VMA lock */ +if (ret) + goto fail; + } + +fail: +/* +* Under the per-VMA lock the caller cannot reach ->access() or act on a +* hard error (both need the mmap lock), so release the lock and have it +* retry there; the mmap-lock pass produces the definitive error. +*/ +if (vma_locked) { +vma_end_read (vma); +return ERR_PTR (-EAGAIN ); + } +return ERR_PTR (ret); +} +/* * Writing to file-backed mappings which require folio dirty tracking using GUP * is a fundamentally broken operation, as kernel write access to GUP mappings mm/internal.h +5
-1@@ -1595,6 +1595,8 @@ struct vm_struct *__get_vm_area_node(unsigned long size,*/ int __must_check try_grab_folio (struct folio *folio,int refs,unsigned int flags); +struct page *get_user_page_vma (struct vm_area_struct *vma,unsigned long addr, +unsigned int gup_flags);/* * mm/huge_memory.c @@ -1641,11 +1643,13 @@ enum {FOLL_UNLOCKABLE =1 <<21 ,/* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */ FOLL_MADV_POPULATE =1 <<22 , +/* caller holds the per-VMA lock, not the mmap lock */ + FOLL_VMA_LOCK = 1 << 23, };#define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \ FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \ - FOLL_MADV_POPULATE) + FOLL_MADV_POPULATE | FOLL_VMA_LOCK)/* * Indicates for which pages that are write-protected in the page table, Signature · 1 line
2.53.0-Meta
Rik van Riel
reviewerRe: [PATCH RFC v3 2/6] riscv/mm: add untagged_addr_remote_unlocked()
In reply to Usama ArifQuoted 12 lines · Usama Arif
Quoted 7 lines · On Fri, 17 Jul 2026 13:00:32 -0400 Rik van Riel <[email protected]> wrote:
mm->context.pmlenis set only throughPR_SET_TAGGED_ADDR_CTRLand is stable
afterwards, so it can be read without the mmap lock, as it already is fromuntagged_addr()andmm_untag_mask().
Quoted 2 lines
I think it might not be stable?
set_tagged_addr_ctrl()can change it repeatedly until aCLONE_VMoperation setsMM_CONTEXT_LOCK_PMLEN.
You're right, ARM tagged addresses seem to work a little differently from x86 LAM.
It looks like on ARM with MTE, the top 8 bits of the virtual address are available as tags, meaning that address space cannot be used for VMAs.
For purposes of accessing process memory, this is a little more stable than LAM, because the number of bits we need to mask out of the address is always the same when MTE is in use.
At least, I think so. Am I overlooking anything?
I will clean up the changelog like you suggested.
Signature · 1 line
All Rights Reversed.
Rik van Riel
reviewerRe: [PATCH RFC v3 4/6] mm/gup: add get_user_page_vma() to fault in a page under a held lock
In reply to Usama ArifQuoted 25 lines · Usama Arif
Quoted 10 lines · On Fri, 17 Jul 2026 13:00:34 -0400 Rik van Riel <[email protected]> wrote:
(quoted diff) +2
-0+ /* + * Validate the VMA up front, like __get_user_pages(). AVM_IO/VM_PFNMAP
(quoted diff) +1
-0+ * VMA is not rejected outright: it can hold COWed pagesthat have a
(quoted diff) +1
-0+ * struct page, so let follow_page_mask() look for one,and treat only
(quoted diff) +1
-0+ * its struct-page-less PFNs as unreachable. Any otherrejection
mm/gup.c static int faultin_page(struct vm_area_struct *vma, · line 1214
Quoted 4 lines
mm/gup.c +4
-0+* (secretmem, bad permissions, ...) is final. +*/ + ret =check_vma_flags (vma, gup_flags); +if (ret && !(vma->vm_flags & (VM_IO |VM_PFNMAP )))Do you need to somehow restructure
check_vma_flags()?The first thing that
check_vma_flags()does is:if (vm_flags & (VM_IO | VM_PFNMAP)) return -EFAULT;
Quoted 6 lines
check_vma_flags()returns before evaluatingVM_READ,FOLL_FORCE,FOLL_ANONor the architecture permission check.The code then ignores that early error and may return a COWed page through
follow_page_mask(). A non-FOLL_FORCE caller can therefore read a COWed page from aPROT_NONEPFNMAP VMA.
In this code path, if we return an error to__access_remote_vm(), we end up falling back
to the ->access() hook, and will end up reading
the memory that's "under" that COW, instead of
the COWed page that is currently in the process
we are accessing.
In other words, I think the behavior of this
code is the way we want, even though the
implementation should be cleaned up.
How about we change check_vma_flags() so it
ignores specified flags?
check_vma_flags(vma, gup_flags, ignore_flags)
Then this one call to check_vma_flags can have
it ignore VM_IO | VM_PFNMAP, since we want to
access the COWed pages in those VMAs in this
code path, but fall back to ->access when
there are no COW pages.
Quoted 20 lines
Quoted 14 lines
mm/gup.c +12
-0+ goto fail; + pfnmap = ret; + +for (;;) { +if (fatal_signal_pending (current)) { + ret = -EINTR ; + goto fail; + } +cond_resched (); + + page = follow_page_mask(vma, addr, + gup_flags | FOLL_TOUCH |
FOLL_GET,mm/gup.c static int faultin_page(struct vm_area_struct *vma, · line 1229
Quoted 3 lines
mm/gup.c +3
-0+ &page_mask); +if (!IS_ERR_OR_NULL (page)) +return page;
__get_user_pages()does
Quoted 4 lines
flush_anon_page(vma, subpage, start + j * PAGE_SIZE); flush_dcache_page(subpage);before returning the page. Do you need to that here as well above?
I suppose on systems with virtually indexed caches we do need that.
Thanks for spotting that!
Now I also wonder if I could restructure get_user_page_vma_remote() to do the lookup before the get_user_pages_remote(), and have it then use the new get_user_page_vma() function, to avoid the double VMA lookup (why are we doing that, anyway?)
Quoted 1 line
Signature · 1 line
All Rights Reversed.
Re: [PATCH RFC v3 2/6] riscv/mm: add untagged_addr_remote_unlocked()
In reply to Rik van RielQuoted 13 lines · Rik van Riel
Quoted 12 lines · Usama Arif
Quoted 7 lines · On Fri, 17 Jul 2026 13:00:32 -0400 Rik van Riel <[email protected]> wrote:
mm->context.pmlenis set only throughPR_SET_TAGGED_ADDR_CTRLand is stable
afterwards, so it can be read without the mmap lock, as it already is fromuntagged_addr()andmm_untag_mask().
Quoted 4 lines
Quoted 2 lines
I think it might not be stable?
set_tagged_addr_ctrl()can change it repeatedly until aCLONE_VMoperation setsMM_CONTEXT_LOCK_PMLEN.You're right, ARM tagged addresses seem to work a
ah do you mean RISCV here?
Quoted 15 lines
little differently from x86 LAM.
It looks like on ARM with MTE, the top 8 bits of the virtual address are available as tags, meaning that address space cannot be used for VMAs.
For purposes of accessing process memory, this is a little more stable than LAM, because the number of bits we need to mask out of the address is always the same when MTE is in use.
At least, I think so. Am I overlooking anything?
I will clean up the changelog like you suggested.
Rik van Riel
reviewerRe: [PATCH RFC v3 2/6] riscv/mm: add untagged_addr_remote_unlocked()
Quoted 19 lines · Usama Arif
Quoted 16 lines · Rik van Riel
Quoted 15 lines · Usama Arif
Quoted 9 lines · On Fri, 17 Jul 2026 13:00:32 -0400 Rik van Riel <[email protected]> wrote:
mm->context.pmlenis set only throughPR_SET_TAGGED_ADDR_CTRLand
is stable
afterwards, so it can be read without the mmap lock, as it already
is fromuntagged_addr()andmm_untag_mask().
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
I think it might not be stable?
set_tagged_addr_ctrl()can change it
repeatedly until aCLONE_VMoperation setsMM_CONTEXT_LOCK_PMLEN.You're right, ARM tagged addresses seem to work a
ah do you mean RISCV here?
Ugh, I looked at the wrong one. RISCV is like x86, with a dynamic (though a limited number of options?) mask.
Having said that, won't a process have all of its VMAs in the masked-off area, so coming in from a remote process to a valid memory address should already give us a valid address?
Signature · 1 line
All Rights Reversed.
Re: [PATCH RFC v3 2/6] riscv/mm: add untagged_addr_remote_unlocked()
Quoted 28 lines · Rik van Riel
Quoted 19 lines · Usama Arif
Quoted 16 lines · Rik van Riel
Quoted 15 lines · Usama Arif
Quoted 9 lines · On Fri, 17 Jul 2026 13:00:32 -0400 Rik van Riel <[email protected]> wrote:
mm->context.pmlenis set only throughPR_SET_TAGGED_ADDR_CTRLand
is stable
afterwards, so it can be read without the mmap lock, as it already
is fromuntagged_addr()andmm_untag_mask().Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
I think it might not be stable?
set_tagged_addr_ctrl()can change it
repeatedly until aCLONE_VMoperation setsMM_CONTEXT_LOCK_PMLEN.You're right, ARM tagged addresses seem to work a
ah do you mean RISCV here?
Quoted 9 lines
Ugh, I looked at the wrong one. RISCV is like x86, with a dynamic (though a limited number of options?) mask.
Having said that, won't a process have all of its VMAs in the masked-off area, so coming in from a remote process to a valid memory address should already give us a valid address?
So my understanding from exploring this code is, and hopefully someone in CC from riscv can correct me, for example:
Tagged pointer: 0xabcd000012345678 PMLEN 16: 0x0000000012345678 PMLEN 7: 0xffcd000012345678
The target VMA might be at 0x12345678, but applying PMLEN 7 to that tagged pointer does not produce that address.
Previously, the order was:
Take mmap read lock.
Read pmlen.
Untag the address.
Look up the VMA.
Changing PMLEN takes the mmap write lock. The read and write operations were therefore serialized.
The new order is:
Read pmlen without mmap lock.
Untag the address.
Attempt the per-VMA lookup.
Possibly take mmap lock later.
Continue using the already-untagged address.
A concurrent PMLEN change can occur between those operations?
Rik van Riel
reviewerRe: [PATCH RFC v3 2/6] riscv/mm: add untagged_addr_remote_unlocked()
Quoted 24 lines · Usama Arif
So my understanding from exploring this code is, and hopefully someone
in CC from riscv can correct me, for example:Tagged pointer:
0xabcd000012345678
PMLEN 16:0x0000000012345678
PMLEN 7:0xffcd000012345678The target VMA might be at
0x12345678, but applying PMLEN 7 to that tagged pointer does not produce that address.Previously, the order was:
Take mmap read lock.
Read pmlen.
Untag the address.
Look up the VMA.Changing PMLEN takes the mmap write lock. The read and write operations were therefore serialized.
The new order is:
Quoted 7 lines
Read pmlen without mmap lock.
Untag the address.
Attempt the per-VMA lookup.
Possibly take mmap lock later.
Continue using the already-untagged address.A concurrent PMLEN change can occur between those operations?
I suppose it could, but what are the possible outcomes here?
- We fail to untag the address, the vma lookup fails, and we fail to access memory.
- The address is already untagged, maps to a VMA, and the access succeeds.
Are there any others?
The VMAs of the process need to be in the bottom part of the address space, right? The part where untagged addresses sit.
For things like /proc/<pid>/cmdline we should automatically get an address without any of the high bits set.
For things like ptrace peek / poke, BPF process accesses, and others, I really do not know if those could get tagged addresses...
What are the failures we need to protect against?
What if something comes in with a tagged address, but the process disables tagging while that something waits for the mmap_lock?
Does that reproduce the failure case, without any locking changes?
Quoted 3 lines
Signature · 1 line
All Rights Reversed.
Re: [PATCH RFC v3 2/6] riscv/mm: add untagged_addr_remote_unlocked()
Quoted 55 lines · Rik van Riel
Quoted 24 lines · Usama Arif
So my understanding from exploring this code is, and hopefully someone
in CC from riscv can correct me, for example:Tagged pointer:
0xabcd000012345678
PMLEN 16:0x0000000012345678
PMLEN 7:0xffcd000012345678The target VMA might be at
0x12345678, but applying PMLEN 7 to that tagged pointer does not produce that address.Previously, the order was:
Take mmap read lock.
Read pmlen.
Untag the address.
Look up the VMA.Changing PMLEN takes the mmap write lock. The read and write operations were therefore serialized.
The new order is:
Quoted 7 lines
Read pmlen without mmap lock.
Untag the address.
Attempt the per-VMA lookup.
Possibly take mmap lock later.
Continue using the already-untagged address.A concurrent PMLEN change can occur between those operations?
I suppose it could, but what are the possible outcomes here?
- We fail to untag the address, the vma lookup fails, and we fail to access memory.
- The address is already untagged, maps to a VMA, and the access succeeds.
Are there any others?
The VMAs of the process need to be in the bottom part of the address space, right? The part where untagged addresses sit.
For things like /proc/<pid>/cmdline we should automatically get an address without any of the high bits set.
For things like ptrace peek / poke, BPF process accesses, and others, I really do not know if those could get tagged addresses...
Quoted 5 lines
What are the failures we need to protect against?
What if something comes in with a tagged address, but the process disables tagging while that something waits for the
mmap_lock?
So I think the above question is what needs to be answered.
A tagged pointer can become invalid if PMLEN changes before the old mmap-locked lookup too.
The mmap lock only defined whether the lookup observed the old or new mode.
For VMA as you said, it should be ok. I don't know about others. I think it would be best to get input from RISC-V folks for this. Hopefully its ok..
If it is ok, then all that would be need to be done is to just remove in the commit message that pmlen is stable.
Quoted 3 lines
Does that reproduce the failure case, without any locking changes?
David Hildenbrand (Arm)
maintainerQuoted 13 lines · Rik van Riel
__access_remote_vm()holdsmmap_read_lock()for the whole transfer. On large machines with large multi-threaded applications, themmap_lockis often contended due to mixed accesses from readers and writers, like mmap and munmap. When a lock holder gets stuck, system monitoring software can get stuck behind that, resulting in a failure to log that the system is in trouble.Take the per-VMA lock in
__access_remote_vm()when the access falls entirely within a single VMA. Fall back to the mmap lock when the access crosses a VMA boundary, or when the page cannot be reached under the per-VMA lock: a dropped fault, a userfaultfd VMA, a hard error, or memory with no struct page that has to go throughvma->vm_ops->access().
Quoted 3 lines
The bulk of the work is a new gup helper.
__access_remote_vm()needs a single page from a VMA it has already looked up and locked, faulting it in when necessary, under either lock.
Yes, much better.
Quoted 5 lines
get_user_pages_remote()does not fit: it hard codes the mmap lock and re-derives the VMA.get_user_page_vma()walks the page tables, faults a missing page in, and returns it with a reference and the caller's lock still held.
Agreed, and that can be tackled later.
Quoted 5 lines
The per-VMA path also closes a pre-existing gap. A COWed page in a VM_IO/VM_PFNMAP VMA has a struct page, but the old code routed it to ->access(), where
generic_access_phys()ioremaps the PFN and ioremap of RAM is rejected, so the read came up short.
Yep. The GUP code rejected VM_IO/VM_PFNMAP for, though, because it did not properly handle vm_normal_page_pmd() etc so far.
Quoted 5 lines
get_user_page_vma()now returns that page normally. Raw PFNs with no struct page still reach ->access() under the mmap lock, as before.The series is arranged as:
It will take me a bit to dig into the details; I'll be out the remainder of the week. But the general direction sounds good.
Signature · 3 lines
Cheers, David
Rik van Riel
reviewerQuoted 11 lines · David Hildenbrand (Arm)
Quoted 8 lines · Rik van Riel
get_user_pages_remote()does not fit: it hard codes the mmap lock and
re-derives the VMA.get_user_page_vma()walks the page tables, faults a
missing page in, and returns it with a reference and the caller's lock
still held.
Quoted 1 line
Agreed, and that can be tackled later.
I'll stop making this series (much) bigger, then ;)
I added a few things for v4, but I can save other
stuff for follow-ups.
Quoted 9 lines
Quoted 8 lines
The per-VMA path also closes a pre-existing gap. A COWed page in a VM_IO/VM_PFNMAP VMA has a struct page, but the old code routed it to
->access(), wheregeneric_access_phys()ioremaps the PFN and ioremap of RAM
is rejected, so the read came up short.
Quoted 4 lines
Yep. The GUP code rejected VM_IO/VM_PFNMAP for, though, because it did not
properly handlevm_normal_page_pmd()etc so far.
Do we ever do a PMD sized COW on VM_IO / VM_PFNMAP VMAs?
In other words, is this something we should:
- leave out for now?
- fix now?
- fix later?
If we can fix it later, I'll add it to the list.
Signature · 1 line
All Rights Reversed.