Re: [PATCH v2 1/2] uprobes: skip mappings not managed by core mm
From: Andrii Nakryiko
Date: Fri Dec 04 2026 - 16:31:22 EST
On Thu, Dec 3, 2026 at 4:33 AM David Hildenbrand (Arm) <david@xxxxxxxxxx> 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@xxxxxxxxxx>
> Cc: Oleg Nesterov <oleg@xxxxxxxxxx>
> Reported-by: Chris Mason <mason@xxxxxxxxxx>
> Acked-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> 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);
> +}
> +
[...]