Re: [PATCHv2 perf/core 08/22] uprobes/x86: Add mapping for optimized uprobe trampolines

From: Jiri Olsa
Date: Fri May 16 2025 - 03:48:27 EST


On Thu, May 15, 2025 at 10:22:51AM -0700, Andrii Nakryiko wrote:
> On Thu, May 15, 2025 at 5:13 AM Jiri Olsa <jolsa@xxxxxxxxxx> wrote:
> >
> > Adding support to add special mapping for user space trampoline with
> > following functions:
> >
> > uprobe_trampoline_get - find or add uprobe_trampoline
> > uprobe_trampoline_put - remove or destroy uprobe_trampoline
> >
> > The user space trampoline is exported as arch specific user space special
> > mapping through tramp_mapping, which is initialized in following changes
> > with new uprobe syscall.
> >
> > The uprobe trampoline needs to be callable/reachable from the probed address,
> > so while searching for available address we use is_reachable_by_call function
> > to decide if the uprobe trampoline is callable from the probe address.
> >
> > All uprobe_trampoline objects are stored in uprobes_state object and are
> > cleaned up when the process mm_struct goes down. Adding new arch hooks
> > for that, because this change is x86_64 specific.
> >
> > Locking is provided by callers in following changes.
> >
> > Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
> > ---
> > arch/x86/kernel/uprobes.c | 115 ++++++++++++++++++++++++++++++++++++++
> > include/linux/uprobes.h | 6 ++
> > kernel/events/uprobes.c | 10 ++++
> > kernel/fork.c | 1 +
> > 4 files changed, 132 insertions(+)
> >
>
> [...]
>
> > +static unsigned long find_nearest_page(unsigned long vaddr)
> > +{
> > + struct vm_unmapped_area_info info = {
> > + .length = PAGE_SIZE,
> > + .align_mask = ~PAGE_MASK,
> > + .flags = VM_UNMAPPED_AREA_TOPDOWN,
> > + .low_limit = 0,
>
> would this, technically, allow to allocate memory at NULL (0x0000)
> address? should this start at PAGE_SIZE?

of course I overlooked that, but looks like it's ok because of the
mmap_min_addr, which is used as the actual low limit.. anyway I think
it's better to be explicit, so I'll put the PAGE_SIZE in here

thanks,
jirka

>
> > + .high_limit = ULONG_MAX,
> > + };
> > + unsigned long limit, call_end = vaddr + 5;
> > +
> > + if (!check_add_overflow(call_end, INT_MIN, &limit))
> > + info.low_limit = limit;
> > + if (!check_add_overflow(call_end, INT_MAX, &limit))
> > + info.high_limit = limit;
> > + return vm_unmapped_area(&info);
> > +}
>
> [...]