Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space
@ 2026-12-03 12:33 David Hildenbrand (Arm)
  2026-12-03 12:33 ` [PATCH v2 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
  2026-12-03 12:33 ` [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
  0 siblings, 2 replies; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-03 12:33 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan, Shakeel Butt,
	Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley, Paul Moore
  Cc: Chris Mason, linux-kernel, linux-trace-kernel, linux-perf-users,
	linux-mm, selinux

remap_pfn_range() stores the first PFN of CoW mappings in vm_pgoff, which
is exposed to user space as the file offset, for example through
/proc/pid/maps. Ever since commit 8be71e1ba093 ("selinux: mark status and
policy inodes as immutable, drop status mmap write checks"), this exposes
the PFN of the selinux status page to any process. Let's store it in the
anon page offset instead.

In patch #1, we teach uprobes to skip any mappings not managed by core
mm. So far, uprobes only skipped CoW PFNMAP mappings by accident,
because the lookup by file offset did not find them. This also fixes
existing issues with VM_MIXEDMAP mappings, such as cramfs XIP files.

In patch #2, we then stop storing the PFN of CoW PFNMAP mappings in
vm_pgoff and instead store it in the anon page offset that Lorenzo
introduced recently. In contrast to vm_pgoff, the anon page offset is
never exposed to user space.

As a side effect, such mappings are now found in the i_mmap tree by
their file offset, and cramfs direct mappings no longer report the
physical address in /proc/pid/maps.

I ran targeted tests (PFN leak, CoW behavior, fork, mremap) on x86-64 and
32bit arm (without LPAE -> no pte_special() support) and selftests on
x86-64.

I'll route this through my tree, likely after the next merge window.

---
Changes in v2:
- Collected tag (thanks!).
- "mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset"
  - Use vma_is_mm_managed() instead of vma_can_merge() in copy_vma().
  - Add vma_set_cow_pfnmap_pfn() and vma_cow_pfnmap_pfn().
  - Drop "unmergeable VMAs" comment from the vma_start_anon_pgoff() doc.
  - Adjust comments and the patch description.
- Link to v1: https://patch.msgid.link/20261128-cow_pfnmap_pgoff-v1-0-ad3733299f24@kernel.org

---
David Hildenbrand (Arm) (2):
      uprobes: skip mappings not managed by core mm
      mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset

 fs/cramfs/inode.c               |  7 +---
 include/linux/mm.h              |  9 +++--
 kernel/events/uprobes.c         | 30 ++++++++++-----
 mm/memory.c                     | 81 +++++++++++++++++++++++++----------------
 mm/vma.c                        |  6 +--
 mm/vma.h                        |  3 +-
 tools/testing/vma/include/dup.h |  3 +-
 7 files changed, 83 insertions(+), 56 deletions(-)
---
base-commit: c5941b1abeffba401f7f9cbc8c4f0c5da75dcba5
change-id: 20261128-cow_pfnmap_pgoff-2df69c81fd0c

Best regards,
--  
David Hildenbrand (Arm) <david@kernel.org>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v2 1/2] uprobes: skip mappings not managed by core mm
  2026-12-03 12:33 [PATCH v2 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space David Hildenbrand (Arm)
@ 2026-12-03 12:33 ` David Hildenbrand (Arm)
  2026-12-03 13:53   ` Oleg Nesterov
                     ` (2 more replies)
  2026-12-03 12:33 ` [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
  1 sibling, 3 replies; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-03 12:33 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan, Shakeel Butt,
	Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley, Paul Moore
  Cc: Chris Mason, linux-kernel, linux-trace-kernel, linux-perf-users,
	linux-mm, selinux

Uprobe writes breakpoints and updates the reference counters of SDT
markers through GUP, which refuses VM_IO and VM_PFNMAP mappings, and which
cannot access the parts of VM_MIXEDMAP mappings that are not backed by
"struct page".

However, valid_vma() and valid_ref_ctr_vma() never excluded such
mappings. For example, cramfs XIP file mappings use either
remap_pfn_range() or VM_MIXEDMAP mappings. For such file mappings,
enabling a uprobe fails with -EFAULT, and disabling a uprobe that was
enabled before such a mapping appeared fails with -EFAULT as well,
leaking the uprobe.

Further, since commit 83e3dc9a5d4d ("uprobes: simplify
find_active_uprobe_rcu() VMA checks"), the uprobe lookups no longer check
the VMA flags, assuming that a trap instruction in a VMA implies that we
installed a breakpoint there. But we never install breakpoints into
some VMAs, such as MAP_SHARED mappings. If the file itself contains a
trap instruction at the offset of a uprobe, executing it in such a VMA
will find the uprobe, and instead of delivering SIGTRAP, we let the task
loop on that trap instruction forever.

Let's fix it by only considering VMAs managed by core mm, using
vma_is_mm_managed().

Move the existing VMA flag checks in valid_vma() into a new helper
valid_vma_flags(), and call vma_is_mm_managed() from there.

Then, check valid_vma() also in find_active_uprobe_rcu(), and
valid_vma_flags() in find_active_uprobe_speculative(), essentially
reverting 83e3dc9a5d4d.

In valid_ref_ctr_vma(), only call vma_is_mm_managed(): using
valid_vma_flags() would be wrong as we don't need exec permissions.

vma_is_mm_managed() refuses VM_PFNMAP and VM_MIXEDMAP mappings, which
covers VM_IO mappings as well for anything we would probe. For example,
remap_pfn_range() sets VM_IO together with VM_PFNMAP.

This has low urgency and is likely not stable-worthy.

Fixes: 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Fixes: 83e3dc9a5d4d ("uprobes: simplify find_active_uprobe_rcu() VMA checks")
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Reported-by: Chris Mason <mason@kernel.org>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 kernel/events/uprobes.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 6015289fd92c6..a6c178f99ea11 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -124,6 +124,22 @@ static void uprobe_warn(struct task_struct *t, const char *msg)
 	pr_warn("uprobe: %s:%d failed to %s\n", t->comm, t->pid, msg);
 }
 
+static bool valid_vma_flags(struct vm_area_struct *vma, bool is_register)
+{
+	vma_flags_t forbidden = mk_vma_flags(VMA_HUGETLB_BIT,
+					     VMA_MAYSHARE_BIT);
+
+	/* We can only write breakpoints to mappings managed by core mm. */
+	if (!vma_is_mm_managed(vma))
+		return false;
+
+	if (is_register)
+		vma_flags_set(&forbidden, VMA_WRITE_BIT);
+
+	return vma_test(vma, VMA_MAYEXEC_BIT) &&
+	       !vma_test_any_mask(vma, forbidden);
+}
+
 /*
  * valid_vma: Verify if the specified vma is an executable vma
  * Relax restrictions while unregistering: vm_flags might have
@@ -134,14 +150,7 @@ static void uprobe_warn(struct task_struct *t, const char *msg)
  */
 static bool valid_vma(struct vm_area_struct *vma, bool is_register)
 {
-	vma_flags_t forbidden = mk_vma_flags(VMA_HUGETLB_BIT,
-					     VMA_MAYSHARE_BIT);
-
-	if (is_register)
-		vma_flags_set(&forbidden, VMA_WRITE_BIT);
-
-	return vma->vm_file && vma_test(vma, VMA_MAYEXEC_BIT) &&
-		!vma_test_any_mask(vma, forbidden);
+	return vma->vm_file && valid_vma_flags(vma, is_register);
 }
 
 static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
@@ -287,6 +296,7 @@ static bool valid_ref_ctr_vma(struct uprobe *uprobe,
 
 	return uprobe->ref_ctr_offset &&
 		vma->vm_file &&
+		vma_is_mm_managed(vma) &&
 		file_inode(vma->vm_file) == uprobe->inode &&
 		vma_test(vma, VMA_WRITE_BIT) && !vma_test(vma, VMA_SHARED_BIT) &&
 		vma->vm_start <= vaddr &&
@@ -2449,7 +2459,7 @@ static struct uprobe *find_active_uprobe_speculative(unsigned long bp_vaddr)
 	 * that whatever we speculatively found is correct
 	 */
 	vm_file = READ_ONCE(vma->vm_file);
-	if (!vm_file)
+	if (!vm_file || !valid_vma_flags(vma, false))
 		return NULL;
 
 	offset = (loff_t)(vma_start_pgoff(vma) << PAGE_SHIFT) +
@@ -2479,7 +2489,7 @@ static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swb
 	mmap_read_lock(mm);
 	vma = vma_lookup(mm, bp_vaddr);
 	if (vma) {
-		if (vma->vm_file) {
+		if (valid_vma(vma, false)) {
 			struct inode *inode = file_inode(vma->vm_file);
 			loff_t offset = vaddr_to_offset(vma, bp_vaddr);
 

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
  2026-12-03 12:33 [PATCH v2 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space David Hildenbrand (Arm)
  2026-12-03 12:33 ` [PATCH v2 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
@ 2026-12-03 12:33 ` David Hildenbrand (Arm)
  2026-12-03 13:47   ` Lorenzo Stoakes (ARM)
                     ` (2 more replies)
  1 sibling, 3 replies; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-03 12:33 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan, Shakeel Butt,
	Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley, Paul Moore
  Cc: Chris Mason, linux-kernel, linux-trace-kernel, linux-perf-users,
	linux-mm, selinux

remap_pfn_range() and its mmap_prepare variant overwrite vm_pgoff with the
first PFN of the range in CoW mappings, such that __vm_normal_page() can
reliably identify CoW'ed anon folios without arch support for pte_special.

However, vm_pgoff is also exposed to user space as the file offset,
through /proc/pid/maps, /proc/pid/smaps, PROCMAP_QUERY, PERF_RECORD_MMAP
and the NT_FILE note of core dumps, whereas /proc/pid/pagemap only
reports PFNs to CAP_SYS_ADMIN.

Consequently, such mappings can leak physical addresses to user space. In
particular, since commit 8be71e1ba093 ("selinux: mark status and policy
inodes as immutable, drop status mmap write checks"), a MAP_PRIVATE
mapping of /sys/fs/selinux/status can leak PFNs that way.

We don't want to expose the PFNs of mapped pages to user space.

Now that we have the anon page offset per VMA, let's fix it by storing
the PFN in there instead. That value is never exported to user space.

We already handle the anon page offset properly during VMA splits etc.
Only copy_vma() must be taught to not reset it for VMAs not managed by
core mm, which includes any PFNMAP mappings.

This has medium urgency. Do not CC stable, as backports would be tricky
either way and not worth it.

Fixes: 8be71e1ba093 ("selinux: mark status and policy inodes as immutable, drop status mmap write checks")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 fs/cramfs/inode.c               |  7 +---
 include/linux/mm.h              |  9 +++--
 mm/memory.c                     | 81 +++++++++++++++++++++++++----------------
 mm/vma.c                        |  6 +--
 mm/vma.h                        |  3 +-
 tools/testing/vma/include/dup.h |  3 +-
 6 files changed, 63 insertions(+), 46 deletions(-)

diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
index d4cd03f4f60d5..9b05250b2198c 100644
--- a/fs/cramfs/inode.c
+++ b/fs/cramfs/inode.c
@@ -402,12 +402,7 @@ static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
 	}
 
 	if (pages == vma_pages(vma)) {
-		/*
-		 * The entire vma is mappable. remap_pfn_range() will
-		 * make it distinguishable from a non-direct mapping
-		 * in /proc/<pid>/maps by substituting the file offset
-		 * with the actual physical address.
-		 */
+		/* The entire vma is mappable. */
 		ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
 				      pages * PAGE_SIZE, vma->vm_page_prot);
 	} else {
diff --git a/include/linux/mm.h b/include/linux/mm.h
index abd427c136409..778b1354f5d18 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1766,7 +1766,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
 	 * mapping not embodied in any attribute of the VMA.
 	 *
 	 * Additionally, private (CoW) PFN maps encode the source PFN of the
-	 * range in vma->vm_pgoff, which may otherwise cause spurious merges.
+	 * range in the anonymous page offset, which may otherwise cause
+	 * spurious merges.
 	 */
 	if (!vma_flags_is_mm_backed(flags))
 		return false;
@@ -4625,8 +4626,7 @@ static inline unsigned long vma_pages(const struct vm_area_struct *vma)
  * If @vma is a MAP_PRIVATE file-backed mapping, then this returns the
  * page offset within the file.
  *
- * Edge cases: nommu does not abide by these and CoW MAP_PRIVATE-pfnmap regions
- * have their page offset set to the first PFN in the range.
+ * Edge cases: nommu does not abide by these.
  *
  * Returns: The page offset of the start of @vma.
  */
@@ -4680,6 +4680,9 @@ static inline pgoff_t vma_last_pgoff(const struct vm_area_struct *vma)
  * This value is used for tracking MAP_PRIVATE file-backed mappings by their
  * anonymous page offset.
  *
+ * Edge cases: CoW MAP_PRIVATE-pfnmap regions have it set to the first PFN in
+ * the range.
+ *
  * Returns: The anonymous page offset of the start of @vma.
  */
 static inline pgoff_t vma_start_anon_pgoff(const struct vm_area_struct *vma)
diff --git a/mm/memory.c b/mm/memory.c
index 90fc7c3951247..d11074b461e14 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
 	print_bad_page_map(vma, addr, &entry, sizeof(entry), page, PGTABLE_LEVEL_PTE);
 }
 
+static inline void vma_set_cow_pfnmap_pfn(struct vm_area_struct *vma,
+		unsigned long pfn)
+{
+	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
+	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
+
+	/*
+	 * There's a horrible special case to handle copy-on-write
+	 * behaviour that some programs depend on. We mark the "original"
+	 * un-COW'ed pages by matching them up with the anonymous page
+	 * offset. See vm_normal_page() for details.
+	 */
+	vma_set_anon_pgoff(vma, pfn);
+}
+
+static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
+		unsigned long addr)
+{
+	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
+	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
+
+	return linear_anon_page_index(vma, addr);
+}
+
 /**
  * __vm_normal_page() - Get the "struct page" associated with a page table entry.
  * @vma: The VMA mapping the page table entry.
@@ -687,10 +711,10 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
  *
  * The way we recognize COWed pages within VM_PFNMAP mappings is through the
  * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
- * set, and the vm_pgoff will point to the first PFN mapped: thus every special
- * mapping will always honor the rule
+ * set, and the anonymous page offset will point to the first PFN mapped: thus
+ * every special mapping will always honor the rule
  *
- *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
+ *	pfn_of_page == vma_cow_pfnmap_pfn(vma, addr)
  *
  * And for normal mappings this is false.
  *
@@ -745,13 +769,11 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma,
 				if (!pfn_valid(pfn))
 					return NULL;
 			} else {
-				const pgoff_t index = linear_page_index(vma, addr);
-
 				/* Only CoW'ed anon folios are "normal". */
-				if (pfn == index)
-					return NULL;
 				if (!vma_is_cow_mapping(vma))
 					return NULL;
+				if (pfn == vma_cow_pfnmap_pfn(vma, addr))
+					return NULL;
 			}
 		}
 
@@ -3217,22 +3239,12 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
 	return 0;
 }
 
-static int get_remap_pgoff(bool is_cow, unsigned long addr,
-		unsigned long end, unsigned long vm_start, unsigned long vm_end,
-		unsigned long pfn, pgoff_t *vm_pgoff_p)
+static int check_cow_pfnmap_range(unsigned long addr, unsigned long end,
+		unsigned long vm_start, unsigned long vm_end)
 {
-	/*
-	 * There's a horrible special case to handle copy-on-write
-	 * behaviour that some programs depend on. We mark the "original"
-	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
-	 * See vm_normal_page() for details.
-	 */
-	if (is_cow) {
-		if (addr != vm_start || end != vm_end)
-			return -EINVAL;
-		*vm_pgoff_p = pfn;
-	}
-
+	/* We must span the entire VMA, see vma_set_cow_pfnmap_pfn(). */
+	if (addr != vm_start || end != vm_end)
+		return -EINVAL;
 	return 0;
 }
 
@@ -3370,17 +3382,18 @@ int remap_pfn_range_prepare(struct vm_area_desc *desc)
 	const struct mmap_action *action = &desc->action;
 	const unsigned long start = action->remap.start;
 	const unsigned long end = start + action->remap.size;
-	const unsigned long pfn = action->remap.start_pfn;
 	const bool is_cow = vma_desc_is_cow_mapping(desc);
 	int err;
 
 	if (!range_in_vma_desc(desc, start, end))
 		return -EFAULT;
 
-	err = get_remap_pgoff(is_cow, start, end, desc->start, desc->end, pfn,
-			      &desc->pgoff);
-	if (err)
-		return err;
+	if (is_cow) {
+		err = check_cow_pfnmap_range(start, end, desc->start,
+					     desc->end);
+		if (err)
+			return err;
+	}
 
 	vma_desc_set_flags_mask(desc, VMA_REMAP_FLAGS);
 	return 0;
@@ -3394,12 +3407,16 @@ static int remap_pfn_range_prepare_vma(struct vm_area_struct *vma,
 	const bool is_cow = vma_is_cow_mapping(vma);
 	int err;
 
-	err = get_remap_pgoff(is_cow, addr, end, vma->vm_start, vma->vm_end,
-			      pfn, &vma->vm_pgoff);
-	if (err)
-		return err;
+	if (is_cow) {
+		err = check_cow_pfnmap_range(addr, end, vma->vm_start,
+					     vma->vm_end);
+		if (err)
+			return err;
+	}
 
 	vma_set_flags_mask(vma, VMA_REMAP_FLAGS);
+	if (is_cow)
+		vma_set_cow_pfnmap_pfn(vma, pfn);
 	return 0;
 }
 
@@ -3436,6 +3453,8 @@ int remap_pfn_range_complete(struct vm_area_struct *vma,
 	const unsigned long size = action->remap.size;
 	const pgprot_t prot = action->remap.pgprot;
 
+	if (vma_is_cow_mapping(vma))
+		vma_set_cow_pfnmap_pfn(vma, pfn);
 	return do_remap_pfn_range(vma, start, pfn, size, prot);
 }
 
diff --git a/mm/vma.c b/mm/vma.c
index c82e9306b11ac..f99f50ca94777 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -2006,10 +2006,10 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
 	VMG_VMA_STATE(vmg, &vmi, NULL, vma, addr, addr + len);
 
 	/*
-	 * If a vma has not yet been faulted, update its anonymous pgoff to
-	 * match the new location to increase its chance of merging.
+	 * If an mm-managed vma has not yet been faulted, update its anonymous
+	 * pgoff to match the new location to increase its chance of merging.
 	 */
-	if (!vma_has_anon_rmap(vma)) {
+	if (!vma_has_anon_rmap(vma) && vma_is_mm_managed(vma)) {
 		anon_pgoff = addr >> PAGE_SHIFT;
 
 		if (vma_is_anonymous(vma)) {
diff --git a/mm/vma.h b/mm/vma.h
index a74143bd22746..8e506f5d15a79 100644
--- a/mm/vma.h
+++ b/mm/vma.h
@@ -290,8 +290,7 @@ static inline void assert_sane_pgoff(struct vm_area_struct *vma, pgoff_t pgoff)
 		return;
 	/*
 	 * File-backed VMAs have arbitrary page offset (either page offset into
-	 * file or for pfnmap the PFN of the start of the range or drivers may
-	 * set arbitrary page offset).
+	 * file or drivers may set arbitrary page offset).
 	 */
 	if (!vma_is_anonymous(vma))
 		return;
diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
index f9bafe4e20158..9e72d5f06e10e 100644
--- a/tools/testing/vma/include/dup.h
+++ b/tools/testing/vma/include/dup.h
@@ -1741,7 +1741,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
 	 * mapping not embodied in any attribute of the VMA.
 	 *
 	 * Additionally, private (CoW) PFN maps encode the source PFN of the
-	 * range in vma->vm_pgoff, which may otherwise cause spurious merges.
+	 * range in the anonymous page offset, which may otherwise cause
+	 * spurious merges.
 	 */
 	if (!vma_flags_is_mm_backed(flags))
 		return false;

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
  2026-12-03 12:33 ` [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
@ 2026-12-03 13:47   ` Lorenzo Stoakes (ARM)
  2026-12-04  6:03   ` YinFengwei
  2026-12-04 12:34   ` sashiko-bot
  2 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-03 13:47 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Michal Hocko, Andrew Morton,
	Suren Baghdasaryan, Shakeel Butt, Pedro Falcato, Jann Horn,
	Jan Kara, Stephen Smalley, Paul Moore, Chris Mason, linux-kernel,
	linux-trace-kernel, linux-perf-users, linux-mm, selinux

On Thu, Dec 03, 2026 at 01:33:25PM +0100, David Hildenbrand (Arm) wrote:
> remap_pfn_range() and its mmap_prepare variant overwrite vm_pgoff with the
> first PFN of the range in CoW mappings, such that __vm_normal_page() can
> reliably identify CoW'ed anon folios without arch support for pte_special.
>
> However, vm_pgoff is also exposed to user space as the file offset,
> through /proc/pid/maps, /proc/pid/smaps, PROCMAP_QUERY, PERF_RECORD_MMAP
> and the NT_FILE note of core dumps, whereas /proc/pid/pagemap only
> reports PFNs to CAP_SYS_ADMIN.
>
> Consequently, such mappings can leak physical addresses to user space. In
> particular, since commit 8be71e1ba093 ("selinux: mark status and policy
> inodes as immutable, drop status mmap write checks"), a MAP_PRIVATE
> mapping of /sys/fs/selinux/status can leak PFNs that way.
>
> We don't want to expose the PFNs of mapped pages to user space.
>
> Now that we have the anon page offset per VMA, let's fix it by storing
> the PFN in there instead. That value is never exported to user space.
>
> We already handle the anon page offset properly during VMA splits etc.
> Only copy_vma() must be taught to not reset it for VMAs not managed by
> core mm, which includes any PFNMAP mappings.
>
> This has medium urgency. Do not CC stable, as backports would be tricky
> either way and not worth it.
>
> Fixes: 8be71e1ba093 ("selinux: mark status and policy inodes as immutable, drop status mmap write checks")
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>

Thanks, this LGTM so:

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

Below are posted only pleased sounds :)

> ---
>  fs/cramfs/inode.c               |  7 +---
>  include/linux/mm.h              |  9 +++--
>  mm/memory.c                     | 81 +++++++++++++++++++++++++----------------
>  mm/vma.c                        |  6 +--
>  mm/vma.h                        |  3 +-
>  tools/testing/vma/include/dup.h |  3 +-
>  6 files changed, 63 insertions(+), 46 deletions(-)
>
> diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
> index d4cd03f4f60d5..9b05250b2198c 100644
> --- a/fs/cramfs/inode.c
> +++ b/fs/cramfs/inode.c
> @@ -402,12 +402,7 @@ static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
>  	}
>
>  	if (pages == vma_pages(vma)) {
> -		/*
> -		 * The entire vma is mappable. remap_pfn_range() will
> -		 * make it distinguishable from a non-direct mapping
> -		 * in /proc/<pid>/maps by substituting the file offset
> -		 * with the actual physical address.
> -		 */
> +		/* The entire vma is mappable. */

Love this simplification btw :)

>  		ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
>  				      pages * PAGE_SIZE, vma->vm_page_prot);
>  	} else {
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index abd427c136409..778b1354f5d18 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1766,7 +1766,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
>  	 * mapping not embodied in any attribute of the VMA.
>  	 *
>  	 * Additionally, private (CoW) PFN maps encode the source PFN of the
> -	 * range in vma->vm_pgoff, which may otherwise cause spurious merges.
> +	 * range in the anonymous page offset, which may otherwise cause
> +	 * spurious merges.
>  	 */
>  	if (!vma_flags_is_mm_backed(flags))
>  		return false;
> @@ -4625,8 +4626,7 @@ static inline unsigned long vma_pages(const struct vm_area_struct *vma)
>   * If @vma is a MAP_PRIVATE file-backed mapping, then this returns the
>   * page offset within the file.
>   *
> - * Edge cases: nommu does not abide by these and CoW MAP_PRIVATE-pfnmap regions
> - * have their page offset set to the first PFN in the range.
> + * Edge cases: nommu does not abide by these.
>   *
>   * Returns: The page offset of the start of @vma.
>   */
> @@ -4680,6 +4680,9 @@ static inline pgoff_t vma_last_pgoff(const struct vm_area_struct *vma)
>   * This value is used for tracking MAP_PRIVATE file-backed mappings by their
>   * anonymous page offset.
>   *
> + * Edge cases: CoW MAP_PRIVATE-pfnmap regions have it set to the first PFN in
> + * the range.
> + *
>   * Returns: The anonymous page offset of the start of @vma.
>   */
>  static inline pgoff_t vma_start_anon_pgoff(const struct vm_area_struct *vma)
> diff --git a/mm/memory.c b/mm/memory.c
> index 90fc7c3951247..d11074b461e14 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
>  	print_bad_page_map(vma, addr, &entry, sizeof(entry), page, PGTABLE_LEVEL_PTE);
>  }
>
> +static inline void vma_set_cow_pfnmap_pfn(struct vm_area_struct *vma,
> +		unsigned long pfn)
> +{
> +	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
> +	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
> +
> +	/*
> +	 * There's a horrible special case to handle copy-on-write
> +	 * behaviour that some programs depend on. We mark the "original"
> +	 * un-COW'ed pages by matching them up with the anonymous page
> +	 * offset. See vm_normal_page() for details.
> +	 */
> +	vma_set_anon_pgoff(vma, pfn);
> +}
> +
> +static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
> +		unsigned long addr)
> +{
> +	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
> +	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
> +
> +	return linear_anon_page_index(vma, addr);
> +}

Thanks, much appreicated! :)

> +
>  /**
>   * __vm_normal_page() - Get the "struct page" associated with a page table entry.
>   * @vma: The VMA mapping the page table entry.
> @@ -687,10 +711,10 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
>   *
>   * The way we recognize COWed pages within VM_PFNMAP mappings is through the
>   * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
> - * set, and the vm_pgoff will point to the first PFN mapped: thus every special
> - * mapping will always honor the rule
> + * set, and the anonymous page offset will point to the first PFN mapped: thus
> + * every special mapping will always honor the rule
>   *
> - *	pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
> + *	pfn_of_page == vma_cow_pfnmap_pfn(vma, addr)
>   *
>   * And for normal mappings this is false.
>   *
> @@ -745,13 +769,11 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma,
>  				if (!pfn_valid(pfn))
>  					return NULL;
>  			} else {
> -				const pgoff_t index = linear_page_index(vma, addr);
> -
>  				/* Only CoW'ed anon folios are "normal". */
> -				if (pfn == index)
> -					return NULL;
>  				if (!vma_is_cow_mapping(vma))
>  					return NULL;
> +				if (pfn == vma_cow_pfnmap_pfn(vma, addr))
> +					return NULL;

Great!

>  			}
>  		}
>
> @@ -3217,22 +3239,12 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
>  	return 0;
>  }
>
> -static int get_remap_pgoff(bool is_cow, unsigned long addr,
> -		unsigned long end, unsigned long vm_start, unsigned long vm_end,
> -		unsigned long pfn, pgoff_t *vm_pgoff_p)
> +static int check_cow_pfnmap_range(unsigned long addr, unsigned long end,
> +		unsigned long vm_start, unsigned long vm_end)
>  {
> -	/*
> -	 * There's a horrible special case to handle copy-on-write
> -	 * behaviour that some programs depend on. We mark the "original"
> -	 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
> -	 * See vm_normal_page() for details.
> -	 */
> -	if (is_cow) {
> -		if (addr != vm_start || end != vm_end)
> -			return -EINVAL;
> -		*vm_pgoff_p = pfn;
> -	}
> -
> +	/* We must span the entire VMA, see vma_set_cow_pfnmap_pfn(). */
> +	if (addr != vm_start || end != vm_end)
> +		return -EINVAL;

NIce.

>  	return 0;
>  }
>
> @@ -3370,17 +3382,18 @@ int remap_pfn_range_prepare(struct vm_area_desc *desc)
>  	const struct mmap_action *action = &desc->action;
>  	const unsigned long start = action->remap.start;
>  	const unsigned long end = start + action->remap.size;
> -	const unsigned long pfn = action->remap.start_pfn;
>  	const bool is_cow = vma_desc_is_cow_mapping(desc);
>  	int err;
>
>  	if (!range_in_vma_desc(desc, start, end))
>  		return -EFAULT;
>
> -	err = get_remap_pgoff(is_cow, start, end, desc->start, desc->end, pfn,
> -			      &desc->pgoff);
> -	if (err)
> -		return err;
> +	if (is_cow) {
> +		err = check_cow_pfnmap_range(start, end, desc->start,
> +					     desc->end);
> +		if (err)
> +			return err;
> +	}
>
>  	vma_desc_set_flags_mask(desc, VMA_REMAP_FLAGS);
>  	return 0;
> @@ -3394,12 +3407,16 @@ static int remap_pfn_range_prepare_vma(struct vm_area_struct *vma,
>  	const bool is_cow = vma_is_cow_mapping(vma);
>  	int err;
>
> -	err = get_remap_pgoff(is_cow, addr, end, vma->vm_start, vma->vm_end,
> -			      pfn, &vma->vm_pgoff);
> -	if (err)
> -		return err;
> +	if (is_cow) {
> +		err = check_cow_pfnmap_range(addr, end, vma->vm_start,
> +					     vma->vm_end);
> +		if (err)
> +			return err;
> +	}
>
>  	vma_set_flags_mask(vma, VMA_REMAP_FLAGS);
> +	if (is_cow)
> +		vma_set_cow_pfnmap_pfn(vma, pfn);
>  	return 0;
>  }
>
> @@ -3436,6 +3453,8 @@ int remap_pfn_range_complete(struct vm_area_struct *vma,
>  	const unsigned long size = action->remap.size;
>  	const pgprot_t prot = action->remap.pgprot;
>
> +	if (vma_is_cow_mapping(vma))
> +		vma_set_cow_pfnmap_pfn(vma, pfn);

Great!

>  	return do_remap_pfn_range(vma, start, pfn, size, prot);
>  }
>
> diff --git a/mm/vma.c b/mm/vma.c
> index c82e9306b11ac..f99f50ca94777 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -2006,10 +2006,10 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
>  	VMG_VMA_STATE(vmg, &vmi, NULL, vma, addr, addr + len);
>
>  	/*
> -	 * If a vma has not yet been faulted, update its anonymous pgoff to
> -	 * match the new location to increase its chance of merging.
> +	 * If an mm-managed vma has not yet been faulted, update its anonymous
> +	 * pgoff to match the new location to increase its chance of merging.
>  	 */
> -	if (!vma_has_anon_rmap(vma)) {
> +	if (!vma_has_anon_rmap(vma) && vma_is_mm_managed(vma)) {
>  		anon_pgoff = addr >> PAGE_SHIFT;
>
>  		if (vma_is_anonymous(vma)) {
> diff --git a/mm/vma.h b/mm/vma.h
> index a74143bd22746..8e506f5d15a79 100644
> --- a/mm/vma.h
> +++ b/mm/vma.h
> @@ -290,8 +290,7 @@ static inline void assert_sane_pgoff(struct vm_area_struct *vma, pgoff_t pgoff)
>  		return;
>  	/*
>  	 * File-backed VMAs have arbitrary page offset (either page offset into
> -	 * file or for pfnmap the PFN of the start of the range or drivers may
> -	 * set arbitrary page offset).
> +	 * file or drivers may set arbitrary page offset).
>  	 */
>  	if (!vma_is_anonymous(vma))
>  		return;
> diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
> index f9bafe4e20158..9e72d5f06e10e 100644
> --- a/tools/testing/vma/include/dup.h
> +++ b/tools/testing/vma/include/dup.h
> @@ -1741,7 +1741,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
>  	 * mapping not embodied in any attribute of the VMA.
>  	 *
>  	 * Additionally, private (CoW) PFN maps encode the source PFN of the
> -	 * range in vma->vm_pgoff, which may otherwise cause spurious merges.
> +	 * range in the anonymous page offset, which may otherwise cause
> +	 * spurious merges.
>  	 */
>  	if (!vma_flags_is_mm_backed(flags))
>  		return false;
>
> --
> 2.56.1
>

--
Cheers, Lorenzo


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm
  2026-12-03 12:33 ` [PATCH v2 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
@ 2026-12-03 13:53   ` Oleg Nesterov
  2026-12-04 21:17     ` Andrii Nakryiko
  2026-12-03 15:09   ` Lorenzo Stoakes (ARM)
  2026-12-04 21:30   ` Andrii Nakryiko
  2 siblings, 1 reply; 12+ messages in thread
From: Oleg Nesterov @ 2026-12-03 13:53 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Andrii Nakryiko, Nicolas Pitre, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Michal Hocko, Andrew Morton, Lorenzo Stoakes,
	Suren Baghdasaryan, Shakeel Butt, Pedro Falcato, Jann Horn,
	Jan Kara, Stephen Smalley, Paul Moore, Chris Mason, linux-kernel,
	linux-trace-kernel, linux-perf-users, linux-mm, selinux

On 12/03, David Hildenbrand (Arm) wrote:
>
> Further, since commit 83e3dc9a5d4d ("uprobes: simplify
> find_active_uprobe_rcu() VMA checks"), the uprobe lookups no longer check
> the VMA flags, assuming that a trap instruction in a VMA implies that we
> installed a breakpoint there. But we never install breakpoints into
> some VMAs, such as MAP_SHARED mappings. If the file itself contains a
> trap instruction at the offset of a uprobe, executing it in such a VMA
> will find the uprobe, and instead of delivering SIGTRAP, > we let the task
> loop on that trap instruction forever.

OK, I guess this can really happen if prepare_uprobe() wasn't called...

LGTM,

Acked-by: Oleg Nesterov <oleg@redhat.com>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm
  2026-12-03 12:33 ` [PATCH v2 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
  2026-12-03 13:53   ` Oleg Nesterov
@ 2026-12-03 15:09   ` Lorenzo Stoakes (ARM)
  2026-12-04 21:30   ` Andrii Nakryiko
  2 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-03 15:09 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Michal Hocko, Andrew Morton,
	Suren Baghdasaryan, Shakeel Butt, Pedro Falcato, Jann Horn,
	Jan Kara, Stephen Smalley, Paul Moore, Chris Mason, linux-kernel,
	linux-trace-kernel, linux-perf-users, linux-mm, selinux

On Thu, Dec 03, 2026 at 01:33:24PM +0100, David Hildenbrand (Arm) wrote:
> Uprobe writes breakpoints and updates the reference counters of SDT
> markers through GUP, which refuses VM_IO and VM_PFNMAP mappings, and which
> cannot access the parts of VM_MIXEDMAP mappings that are not backed by
> "struct page".
>
> However, valid_vma() and valid_ref_ctr_vma() never excluded such
> mappings. For example, cramfs XIP file mappings use either
> remap_pfn_range() or VM_MIXEDMAP mappings. For such file mappings,
> enabling a uprobe fails with -EFAULT, and disabling a uprobe that was
> enabled before such a mapping appeared fails with -EFAULT as well,
> leaking the uprobe.
>
> Further, since commit 83e3dc9a5d4d ("uprobes: simplify
> find_active_uprobe_rcu() VMA checks"), the uprobe lookups no longer check
> the VMA flags, assuming that a trap instruction in a VMA implies that we
> installed a breakpoint there. But we never install breakpoints into
> some VMAs, such as MAP_SHARED mappings. If the file itself contains a
> trap instruction at the offset of a uprobe, executing it in such a VMA
> will find the uprobe, and instead of delivering SIGTRAP, we let the task
> loop on that trap instruction forever.
>
> Let's fix it by only considering VMAs managed by core mm, using
> vma_is_mm_managed().
>
> Move the existing VMA flag checks in valid_vma() into a new helper
> valid_vma_flags(), and call vma_is_mm_managed() from there.
>
> Then, check valid_vma() also in find_active_uprobe_rcu(), and
> valid_vma_flags() in find_active_uprobe_speculative(), essentially
> reverting 83e3dc9a5d4d.
>
> In valid_ref_ctr_vma(), only call vma_is_mm_managed(): using
> valid_vma_flags() would be wrong as we don't need exec permissions.
>
> vma_is_mm_managed() refuses VM_PFNMAP and VM_MIXEDMAP mappings, which
> covers VM_IO mappings as well for anything we would probe. For example,
> remap_pfn_range() sets VM_IO together with VM_PFNMAP.
>
> This has low urgency and is likely not stable-worthy.
>
> Fixes: 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
> Fixes: 83e3dc9a5d4d ("uprobes: simplify find_active_uprobe_rcu() VMA checks")
> Cc: Andrii Nakryiko <andrii@kernel.org>
> Cc: Oleg Nesterov <oleg@redhat.com>
> Reported-by: Chris Mason <mason@kernel.org>
> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>

Looks sensible, but obviously defer to the uprobe folks, so:

Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

> ---
>  kernel/events/uprobes.c | 30 ++++++++++++++++++++----------
>  1 file changed, 20 insertions(+), 10 deletions(-)
>
> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index 6015289fd92c6..a6c178f99ea11 100644
> --- a/kernel/events/uprobes.c
> +++ b/kernel/events/uprobes.c
> @@ -124,6 +124,22 @@ static void uprobe_warn(struct task_struct *t, const char *msg)
>  	pr_warn("uprobe: %s:%d failed to %s\n", t->comm, t->pid, msg);
>  }
>
> +static bool valid_vma_flags(struct vm_area_struct *vma, bool is_register)
> +{
> +	vma_flags_t forbidden = mk_vma_flags(VMA_HUGETLB_BIT,
> +					     VMA_MAYSHARE_BIT);
> +
> +	/* We can only write breakpoints to mappings managed by core mm. */
> +	if (!vma_is_mm_managed(vma))
> +		return false;
> +
> +	if (is_register)
> +		vma_flags_set(&forbidden, VMA_WRITE_BIT);
> +
> +	return vma_test(vma, VMA_MAYEXEC_BIT) &&
> +	       !vma_test_any_mask(vma, forbidden);
> +}
> +
>  /*
>   * valid_vma: Verify if the specified vma is an executable vma
>   * Relax restrictions while unregistering: vm_flags might have
> @@ -134,14 +150,7 @@ static void uprobe_warn(struct task_struct *t, const char *msg)
>   */
>  static bool valid_vma(struct vm_area_struct *vma, bool is_register)
>  {
> -	vma_flags_t forbidden = mk_vma_flags(VMA_HUGETLB_BIT,
> -					     VMA_MAYSHARE_BIT);
> -
> -	if (is_register)
> -		vma_flags_set(&forbidden, VMA_WRITE_BIT);
> -
> -	return vma->vm_file && vma_test(vma, VMA_MAYEXEC_BIT) &&
> -		!vma_test_any_mask(vma, forbidden);
> +	return vma->vm_file && valid_vma_flags(vma, is_register);
>  }
>
>  static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
> @@ -287,6 +296,7 @@ static bool valid_ref_ctr_vma(struct uprobe *uprobe,
>
>  	return uprobe->ref_ctr_offset &&
>  		vma->vm_file &&
> +		vma_is_mm_managed(vma) &&
>  		file_inode(vma->vm_file) == uprobe->inode &&
>  		vma_test(vma, VMA_WRITE_BIT) && !vma_test(vma, VMA_SHARED_BIT) &&
>  		vma->vm_start <= vaddr &&
> @@ -2449,7 +2459,7 @@ static struct uprobe *find_active_uprobe_speculative(unsigned long bp_vaddr)
>  	 * that whatever we speculatively found is correct
>  	 */
>  	vm_file = READ_ONCE(vma->vm_file);
> -	if (!vm_file)
> +	if (!vm_file || !valid_vma_flags(vma, false))
>  		return NULL;
>
>  	offset = (loff_t)(vma_start_pgoff(vma) << PAGE_SHIFT) +
> @@ -2479,7 +2489,7 @@ static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swb
>  	mmap_read_lock(mm);
>  	vma = vma_lookup(mm, bp_vaddr);
>  	if (vma) {
> -		if (vma->vm_file) {
> +		if (valid_vma(vma, false)) {
>  			struct inode *inode = file_inode(vma->vm_file);
>  			loff_t offset = vaddr_to_offset(vma, bp_vaddr);
>
>
> --
> 2.56.1
>

--
Cheers, Lorenzo


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
  2026-12-03 12:33 ` [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
  2026-12-03 13:47   ` Lorenzo Stoakes (ARM)
@ 2026-12-04  6:03   ` YinFengwei
  2026-12-04 12:34   ` sashiko-bot
  2 siblings, 0 replies; 12+ messages in thread
From: YinFengwei @ 2026-12-04  6:03 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Michal Hocko, Andrew Morton,
	Lorenzo Stoakes, Suren Baghdasaryan, Shakeel Butt, Pedro Falcato,
	Jann Horn, Jan Kara, Stephen Smalley, Paul Moore, Chris Mason,
	linux-kernel, linux-trace-kernel, linux-perf-users, linux-mm,
	selinux

On Thu, Dec 03, 2026 at 01:33:25PM +0100, David Hildenbrand (Arm) wrote:
> remap_pfn_range() and its mmap_prepare variant overwrite vm_pgoff with the
> first PFN of the range in CoW mappings, such that __vm_normal_page() can
> reliably identify CoW'ed anon folios without arch support for pte_special.
> 
> However, vm_pgoff is also exposed to user space as the file offset,
> through /proc/pid/maps, /proc/pid/smaps, PROCMAP_QUERY, PERF_RECORD_MMAP
> and the NT_FILE note of core dumps, whereas /proc/pid/pagemap only
> reports PFNs to CAP_SYS_ADMIN.
> 
> Consequently, such mappings can leak physical addresses to user space. In
> particular, since commit 8be71e1ba093 ("selinux: mark status and policy
> inodes as immutable, drop status mmap write checks"), a MAP_PRIVATE
> mapping of /sys/fs/selinux/status can leak PFNs that way.
> 
> We don't want to expose the PFNs of mapped pages to user space.
> 
> Now that we have the anon page offset per VMA, let's fix it by storing
> the PFN in there instead. That value is never exported to user space.
> 
> We already handle the anon page offset properly during VMA splits etc.
> Only copy_vma() must be taught to not reset it for VMAs not managed by
> core mm, which includes any PFNMAP mappings.
> 
> This has medium urgency. Do not CC stable, as backports would be tricky
> either way and not worth it.
> 
> Fixes: 8be71e1ba093 ("selinux: mark status and policy inodes as immutable, drop status mmap write checks")
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>

Reviewed-by: Yin Fengwei <fengwei_yin@linux.alibaba.com>

Regards
Yin, Fengwei


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
  2026-12-03 12:33 ` [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
  2026-12-03 13:47   ` Lorenzo Stoakes (ARM)
  2026-12-04  6:03   ` YinFengwei
@ 2026-12-04 12:34   ` sashiko-bot
  2026-12-04 13:05     ` David Hildenbrand (Arm)
  2 siblings, 1 reply; 12+ messages in thread
From: sashiko-bot @ 2026-12-04 12:34 UTC (permalink / raw)
  To: David Hildenbrand (Arm), Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	James Clark, Masami Hiramatsu, Oleg Nesterov, Andrii Nakryiko,
	Nicolas Pitre, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Michal Hocko, Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan,
	Shakeel Butt, Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley,
	Paul Moore
  Cc: Chris Mason, linux-trace-kernel, linux-perf-users, linux-mm,
	selinux

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm: vma_cow_pfnmap_pfn() calls linear_anon_page_index() when
  vm_ops is NULL
--

commit d7059d46ab28e030b7a6940fa6aec3969604cdc8
Author: David Hildenbrand (Arm) <david@kernel.org>

mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset

This commit stores the base PFN of CoW VM_PFNMAP mappings in the VMA's
anonymous page offset instead of vma->vm_pgoff, avoiding leaking physical
addresses to user space.  It updates __vm_normal_page() and
remap_pfn_range() accordingly, and keeps copy_vma() from resetting the
anonymous page offset on non-mm-managed VMAs.

> diff --git a/mm/memory.c b/mm/memory.c
> index 90fc7c3951247..d11074b461e14 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
[ ... ]
> +static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
> +		unsigned long addr)
> +{
> +	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
> +	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
> +
> +	return linear_anon_page_index(vma, addr);
> +}

[Severity: Low]
Can linear_anon_page_index() trigger a spurious VM_WARN_ON_ONCE() here when
vma->vm_ops is temporarily NULL during an in-flight f_op->mmap() failure?

Legacy f_op->mmap() paths like drm_gem_shmem_mmap() clear vma->vm_ops on
imported dma-bufs and call dma_buf_mmap() -> system_heap_mmap() ->
remap_pfn_range().  If remap_pfn_range_notrack() fails partway through a
CoW mapping, zap_vma_range() runs before mmap_file() sets vma_dummy_vm_ops.

On architectures without CONFIG_ARCH_HAS_PTE_SPECIAL and with
CONFIG_DEBUG_VM enabled, zap_vma_range() reaches vma_cow_pfnmap_pfn() while
vma_is_anonymous(vma) is still true and anon_pgoff (the PFN) differs from
linear_page_index(vma, addr), firing the warning in linear_anon_page_index().

Since vma_cow_pfnmap_pfn() already asserts vma_is_cow_mapping(vma), calling
__linear_anon_page_index(vma, addr) directly would avoid this warning.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-cow_pfnmap_pgoff-v2-0-2e06735c100d@kernel.org?part=2


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
  2026-12-04 12:34   ` sashiko-bot
@ 2026-12-04 13:05     ` David Hildenbrand (Arm)
  2026-12-04 13:27       ` Lorenzo Stoakes (ARM)
  0 siblings, 1 reply; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-04 13:05 UTC (permalink / raw)
  To: sashiko-reviews, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	James Clark, Masami Hiramatsu, Oleg Nesterov, Andrii Nakryiko,
	Nicolas Pitre, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Michal Hocko, Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan,
	Shakeel Butt, Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley,
	Paul Moore
  Cc: Chris Mason, linux-trace-kernel, linux-perf-users, linux-mm,
	selinux

On 12/4/26 13:34, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Low] mm: vma_cow_pfnmap_pfn() calls linear_anon_page_index() when
>   vm_ops is NULL
> --
> 
> commit d7059d46ab28e030b7a6940fa6aec3969604cdc8
> Author: David Hildenbrand (Arm) <david@kernel.org>
> 
> mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
> 
> This commit stores the base PFN of CoW VM_PFNMAP mappings in the VMA's
> anonymous page offset instead of vma->vm_pgoff, avoiding leaking physical
> addresses to user space.  It updates __vm_normal_page() and
> remap_pfn_range() accordingly, and keeps copy_vma() from resetting the
> anonymous page offset on non-mm-managed VMAs.
> 
>> diff --git a/mm/memory.c b/mm/memory.c
>> index 90fc7c3951247..d11074b461e14 100644
>> --- a/mm/memory.c
>> +++ b/mm/memory.c
>> @@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
> [ ... ]
>> +static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
>> +		unsigned long addr)
>> +{
>> +	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
>> +	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
>> +
>> +	return linear_anon_page_index(vma, addr);
>> +}
> 
> [Severity: Low]
> Can linear_anon_page_index() trigger a spurious VM_WARN_ON_ONCE() here when
> vma->vm_ops is temporarily NULL during an in-flight f_op->mmap() failure?
> 
> Legacy f_op->mmap() paths like drm_gem_shmem_mmap() clear vma->vm_ops on
> imported dma-bufs and call dma_buf_mmap() -> system_heap_mmap() ->
> remap_pfn_range().  If remap_pfn_range_notrack() fails partway through a
> CoW mapping, zap_vma_range() runs before mmap_file() sets vma_dummy_vm_ops.
> 
> On architectures without CONFIG_ARCH_HAS_PTE_SPECIAL and with
> CONFIG_DEBUG_VM enabled, zap_vma_range() reaches vma_cow_pfnmap_pfn() while
> vma_is_anonymous(vma) is still true and anon_pgoff (the PFN) differs from
> linear_page_index(vma, addr), firing the warning in linear_anon_page_index().
> 
> Since vma_cow_pfnmap_pfn() already asserts vma_is_cow_mapping(vma), calling
> __linear_anon_page_index(vma, addr) directly would avoid this warning.
> 

Apparently this can happen when the stars align. Not sure if we care.

@Lorenzo, should I just use __linear_anon_page_index here?

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
  2026-12-04 13:05     ` David Hildenbrand (Arm)
@ 2026-12-04 13:27       ` Lorenzo Stoakes (ARM)
  0 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-04 13:27 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: sashiko-reviews, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	James Clark, Masami Hiramatsu, Oleg Nesterov, Andrii Nakryiko,
	Nicolas Pitre, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Michal Hocko, Andrew Morton, Suren Baghdasaryan, Shakeel Butt,
	Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley, Paul Moore,
	Chris Mason, linux-trace-kernel, linux-perf-users, linux-mm,
	selinux

On Fri, Dec 04, 2026 at 02:05:07PM +0100, David Hildenbrand (Arm) wrote:
> On 12/4/26 13:34, sashiko-bot@kernel.org wrote:
> > Thank you for your contribution! Sashiko AI review found 1 potential
> > issue to consider:
> > - [Low] mm: vma_cow_pfnmap_pfn() calls linear_anon_page_index() when
> >   vm_ops is NULL
> > --
> >
> > commit d7059d46ab28e030b7a6940fa6aec3969604cdc8
> > Author: David Hildenbrand (Arm) <david@kernel.org>
> >
> > mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
> >
> > This commit stores the base PFN of CoW VM_PFNMAP mappings in the VMA's
> > anonymous page offset instead of vma->vm_pgoff, avoiding leaking physical
> > addresses to user space.  It updates __vm_normal_page() and
> > remap_pfn_range() accordingly, and keeps copy_vma() from resetting the
> > anonymous page offset on non-mm-managed VMAs.
> >
> >> diff --git a/mm/memory.c b/mm/memory.c
> >> index 90fc7c3951247..d11074b461e14 100644
> >> --- a/mm/memory.c
> >> +++ b/mm/memory.c
> >> @@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
> > [ ... ]
> >> +static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma,
> >> +		unsigned long addr)
> >> +{
> >> +	VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT));
> >> +	VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
> >> +
> >> +	return linear_anon_page_index(vma, addr);
> >> +}
> >
> > [Severity: Low]
> > Can linear_anon_page_index() trigger a spurious VM_WARN_ON_ONCE() here when
> > vma->vm_ops is temporarily NULL during an in-flight f_op->mmap() failure?
> >
> > Legacy f_op->mmap() paths like drm_gem_shmem_mmap() clear vma->vm_ops on
> > imported dma-bufs and call dma_buf_mmap() -> system_heap_mmap() ->
> > remap_pfn_range().  If remap_pfn_range_notrack() fails partway through a
> > CoW mapping, zap_vma_range() runs before mmap_file() sets vma_dummy_vm_ops.
> >
> > On architectures without CONFIG_ARCH_HAS_PTE_SPECIAL and with
> > CONFIG_DEBUG_VM enabled, zap_vma_range() reaches vma_cow_pfnmap_pfn() while
> > vma_is_anonymous(vma) is still true and anon_pgoff (the PFN) differs from
> > linear_page_index(vma, addr), firing the warning in linear_anon_page_index().
> >
> > Since vma_cow_pfnmap_pfn() already asserts vma_is_cow_mapping(vma), calling
> > __linear_anon_page_index(vma, addr) directly would avoid this warning.
> >
>
> Apparently this can happen when the stars align. Not sure if we care.
>
> @Lorenzo, should I just use __linear_anon_page_index here?

Yeah that's fine maybe just add a comment to say /* Once in a blue moon < blah
blah what the AI said > */ or what not to explain why.

>
> --
> Cheers,
>
> David

--
Cheers, Lorenzo


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm
  2026-12-03 13:53   ` Oleg Nesterov
@ 2026-12-04 21:17     ` Andrii Nakryiko
  0 siblings, 0 replies; 12+ messages in thread
From: Andrii Nakryiko @ 2026-12-04 21:17 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: David Hildenbrand (Arm), Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	James Clark, Masami Hiramatsu, Andrii Nakryiko, Nicolas Pitre,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Andrew Morton, Lorenzo Stoakes, Suren Baghdasaryan, Shakeel Butt,
	Pedro Falcato, Jann Horn, Jan Kara, Stephen Smalley, Paul Moore,
	Chris Mason, linux-kernel, linux-trace-kernel, linux-perf-users,
	linux-mm, selinux

On Thu, Dec 3, 2026 at 5:53 AM Oleg Nesterov <oleg@redhat.com> wrote:
>
> On 12/03, David Hildenbrand (Arm) wrote:
> >
> > Further, since commit 83e3dc9a5d4d ("uprobes: simplify
> > find_active_uprobe_rcu() VMA checks"), the uprobe lookups no longer check
> > the VMA flags, assuming that a trap instruction in a VMA implies that we
> > installed a breakpoint there. But we never install breakpoints into
> > some VMAs, such as MAP_SHARED mappings. If the file itself contains a
> > trap instruction at the offset of a uprobe, executing it in such a VMA
> > will find the uprobe, and instead of delivering SIGTRAP, > we let the task
> > loop on that trap instruction forever.
>
> OK, I guess this can really happen if prepare_uprobe() wasn't called...
>
> LGTM,
>
> Acked-by: Oleg Nesterov <oleg@redhat.com>
>

LGTM as well

Acked-by: Andrii Nakryiko <andrii@kernel.org>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm
  2026-12-03 12:33 ` [PATCH v2 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
  2026-12-03 13:53   ` Oleg Nesterov
  2026-12-03 15:09   ` Lorenzo Stoakes (ARM)
@ 2026-12-04 21:30   ` Andrii Nakryiko
  2 siblings, 0 replies; 12+ messages in thread
From: Andrii Nakryiko @ 2026-12-04 21:30 UTC (permalink / raw)
  To: David Hildenbrand (Arm), Lorenzo Stoakes (Arm)
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Masami Hiramatsu,
	Oleg Nesterov, Andrii Nakryiko, Nicolas Pitre, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Michal Hocko, Andrew Morton,
	Suren Baghdasaryan, Shakeel Butt, Pedro Falcato, Jann Horn,
	Jan Kara, Stephen Smalley, Paul Moore, Chris Mason, linux-kernel,
	linux-trace-kernel, linux-perf-users, linux-mm, selinux

On Thu, Dec 3, 2026 at 4:33 AM David Hildenbrand (Arm) <david@kernel.org> wrote:
>
> Uprobe writes breakpoints and updates the reference counters of SDT
> markers through GUP, which refuses VM_IO and VM_PFNMAP mappings, and which
> cannot access the parts of VM_MIXEDMAP mappings that are not backed by
> "struct page".
>
> However, valid_vma() and valid_ref_ctr_vma() never excluded such
> mappings. For example, cramfs XIP file mappings use either
> remap_pfn_range() or VM_MIXEDMAP mappings. For such file mappings,
> enabling a uprobe fails with -EFAULT, and disabling a uprobe that was
> enabled before such a mapping appeared fails with -EFAULT as well,
> leaking the uprobe.
>
> Further, since commit 83e3dc9a5d4d ("uprobes: simplify
> find_active_uprobe_rcu() VMA checks"), the uprobe lookups no longer check
> the VMA flags, assuming that a trap instruction in a VMA implies that we
> installed a breakpoint there. But we never install breakpoints into
> some VMAs, such as MAP_SHARED mappings. If the file itself contains a
> trap instruction at the offset of a uprobe, executing it in such a VMA
> will find the uprobe, and instead of delivering SIGTRAP, we let the task
> loop on that trap instruction forever.
>
> Let's fix it by only considering VMAs managed by core mm, using
> vma_is_mm_managed().
>
> Move the existing VMA flag checks in valid_vma() into a new helper
> valid_vma_flags(), and call vma_is_mm_managed() from there.
>
> Then, check valid_vma() also in find_active_uprobe_rcu(), and
> valid_vma_flags() in find_active_uprobe_speculative(), essentially
> reverting 83e3dc9a5d4d.
>
> In valid_ref_ctr_vma(), only call vma_is_mm_managed(): using
> valid_vma_flags() would be wrong as we don't need exec permissions.
>
> vma_is_mm_managed() refuses VM_PFNMAP and VM_MIXEDMAP mappings, which
> covers VM_IO mappings as well for anything we would probe. For example,
> remap_pfn_range() sets VM_IO together with VM_PFNMAP.
>
> This has low urgency and is likely not stable-worthy.
>
> Fixes: 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
> Fixes: 83e3dc9a5d4d ("uprobes: simplify find_active_uprobe_rcu() VMA checks")
> Cc: Andrii Nakryiko <andrii@kernel.org>
> Cc: Oleg Nesterov <oleg@redhat.com>
> Reported-by: Chris Mason <mason@kernel.org>
> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
> ---
>  kernel/events/uprobes.c | 30 ++++++++++++++++++++----------
>  1 file changed, 20 insertions(+), 10 deletions(-)
>
> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index 6015289fd92c6..a6c178f99ea11 100644
> --- a/kernel/events/uprobes.c
> +++ b/kernel/events/uprobes.c
> @@ -124,6 +124,22 @@ static void uprobe_warn(struct task_struct *t, const char *msg)
>         pr_warn("uprobe: %s:%d failed to %s\n", t->comm, t->pid, msg);
>  }
>
> +static bool valid_vma_flags(struct vm_area_struct *vma, bool is_register)
> +{
> +       vma_flags_t forbidden = mk_vma_flags(VMA_HUGETLB_BIT,
> +                                            VMA_MAYSHARE_BIT);

so my AI asked me to point out that on GCC mk_vma_flags() is not
inlined nicely unlike with Clang, and that mm folks might want to look
into making this GCC-friendly.

just FYI. But also because modern AI is notorious overachiever, here's
what it proposed and "validated" (I loled at "one-word request for
David's v3"):


  Root cause. __mk_vma_flags() sets each bit in a loop with
__set_bit(). The kernel's bitop() macro only takes its foldable C path
when __builtin_constant_p() says both the bit number and the current
bitmap
  value are constants. GCC evaluates that before it fully unrolls the
loop, so the bit index isn't constant yet. It falls through to x86's
asm volatile("bts" ... "memory"), and that memory clobber blocks all
  later folding. Clang unrolls earlier, which is why it works there.
Lorenzo's commit message said mk_vma_flags() of three bits compiles to
movl $7, %eax. I couldn't reproduce that on GCC 11 or GCC 15 with
  kernel flags: anything with two or more bits ends up as runtime bts.

  Fix 1: in mm (include/linux/mm.h, Lorenzo's API)

  Use generic___set_bit() in vma_flags_set_flag() and add
unrolled_full (from <linux/unroll.h>) before the loop in
__mk_vma_flags(). Both are needed:

  ┌──────────────────────────┬────────────────────────────────────────────┬────────────────────────────┬──────────┐
  │         Variant          │                   GCC 11
   │           GCC 15           │ clang 22 │
  ├──────────────────────────┼────────────────────────────────────────────┼────────────────────────────┼──────────┤
  │ current                  │ runtime bts + stack canary
   │ runtime bts + stack canary │ constant │
  ├──────────────────────────┼────────────────────────────────────────────┼────────────────────────────┼──────────┤
  │ generic___set_bit() only │ 3+ bits: loop not unrolled
   │ constant                   │ constant │
  ├──────────────────────────┼────────────────────────────────────────────┼────────────────────────────┼──────────┤
  │ unrolled_full only       │ runtime vma_flags_set() still uses asm
bts │ constant                   │ constant │
  ├──────────────────────────┼────────────────────────────────────────────┼────────────────────────────┼──────────┤
  │ both                     │ constant
   │ constant                   │ constant │
  └──────────────────────────┴────────────────────────────────────────────┴────────────────────────────┴──────────┘

  - Safe: vma_flags_set_flag() is only ever used on local vma_flags_t
values inside __mk_vma_flags(), never on vma->flags, so plain C loses
nothing. KASAN/KCSAN still instrument the plain C store.
  - Impact: on a GCC 15 x86 defconfig vmlinux, text shrinks by about
7.2 KB across about 70 functions. The biggest wins are mm paths:
madvise_vma_behavior −734 bytes, mprotect_fixup −401, __mmap_region
−318,
    do_mmap −255, plus try_to_unmap_one, __zap_vma_range and dup_mmap.
vma_is_mm_managed() itself becomes a single test $imm, mem.
  - Builds: full vmlinux builds cleanly with GCC 11, GCC 15 and clang
22, with no warnings.
  - Not yet done: tools/testing/vma/include/dup.h has its own copy of
these helpers and should get the same change.
  - Prior reports: I found nothing on patchwork about this.

  Fix 2: in the uprobes patch

  Even with fix 1, GCC keeps valid_vma_flags() out of line. The
function is now only 13 instructions, but the hot path still pays a
call, a return thunk and register spills around it. Making it static
  __always_inline gives this, the same shape as clang:

  mov    0x20(%rax),%rcx
  test   $0x10000400,%ecx    ; PFNMAP|MIXEDMAP
  jne    ...
  mov    0x20(%rax),%rsi
  and    $0x40,%esi          ; MAYEXEC
  je     ...
  test   $0x400080,%ecx      ; HUGETLB|MAYSHARE
  jne    ...

  That's a one-word request for David's v3, and it's worth asking for
whether or not fix 1 lands. Without fix 1 it still removes the call
and the extra canary, but the runtime mask building stays.


diff --git a/include/linux/mm.h b/include/linux/mm.h
index 778b1354f5d1..5a0addee40a5 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3,6 +3,7 @@
 #define _LINUX_MM_H

 #include <linux/args.h>
+#include <linux/unroll.h>
 #include <linux/errno.h>
 #include <linux/mmdebug.h>
 #include <linux/gfp.h>
@@ -1027,13 +1028,20 @@ static inline void vm_flags_mod(struct
vm_area_struct *vma,
  __vm_flags_mod(vma, set, clear);
 }

-/* Set an individual VMA flag in flags, non-atomically. */
+/*
+ * Set an individual VMA flag in flags, non-atomically.
+ *
+ * Use generic___set_bit() rather than __set_bit(): on some architectures (e.g.
+ * x86) the latter falls back to inline asm with a memory clobber whenever the
+ * compiler can't prove the bitmap contents constant, which defeats constant
+ * folding in mk_vma_flags().
+ */
 static __always_inline void vma_flags_set_flag(vma_flags_t *flags,
  vma_flag_t bit)
 {
  unsigned long *bitmap = flags->__vma_flags;

- __set_bit((__force int)bit, bitmap);
+ generic___set_bit((__force int)bit, bitmap);
 }

 static __always_inline vma_flags_t __mk_vma_flags(vma_flags_t flags,
@@ -1041,6 +1049,8 @@ static __always_inline vma_flags_t
__mk_vma_flags(vma_flags_t flags,
 {
  int i;

+ /* Unroll so that constant bits fold into a constant mask. */
+ unrolled_full
  for (i = 0; i < count; i++)
  vma_flags_set_flag(&flags, bits[i]);
  return flags;

> +
> +       /* We can only write breakpoints to mappings managed by core mm. */
> +       if (!vma_is_mm_managed(vma))
> +               return false;
> +
> +       if (is_register)
> +               vma_flags_set(&forbidden, VMA_WRITE_BIT);
> +
> +       return vma_test(vma, VMA_MAYEXEC_BIT) &&
> +              !vma_test_any_mask(vma, forbidden);
> +}
> +

[...]

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-12-04 21:31 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 12:33 [PATCH v2 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space David Hildenbrand (Arm)
2026-12-03 12:33 ` [PATCH v2 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
2026-12-03 13:53   ` Oleg Nesterov
2026-12-04 21:17     ` Andrii Nakryiko
2026-12-03 15:09   ` Lorenzo Stoakes (ARM)
2026-12-04 21:30   ` Andrii Nakryiko
2026-12-03 12:33 ` [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
2026-12-03 13:47   ` Lorenzo Stoakes (ARM)
2026-12-04  6:03   ` YinFengwei
2026-12-04 12:34   ` sashiko-bot
2026-12-04 13:05     ` David Hildenbrand (Arm)
2026-12-04 13:27       ` Lorenzo Stoakes (ARM)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox