[PATCH perf/core 04/11] bpf: Add support to attach uprobe_multi unique uprobe

Jiri Olsa posted 11 patches 1 month ago
[PATCH perf/core 04/11] bpf: Add support to attach uprobe_multi unique uprobe
Posted by Jiri Olsa 1 month ago
Adding support to attach unique uprobe through uprobe multi link
interface.

Adding new BPF_F_UPROBE_MULTI_UNIQUE flag that denotes the unique
uprobe creation.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 include/uapi/linux/bpf.h       | 3 ++-
 kernel/trace/bpf_trace.c       | 4 +++-
 tools/include/uapi/linux/bpf.h | 3 ++-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 233de8677382..3de9eb469fe2 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1300,7 +1300,8 @@ enum {
  * BPF_TRACE_UPROBE_MULTI attach type to create return probe.
  */
 enum {
-	BPF_F_UPROBE_MULTI_RETURN = (1U << 0)
+	BPF_F_UPROBE_MULTI_RETURN = (1U << 0),
+	BPF_F_UPROBE_MULTI_UNIQUE = (1U << 1),
 };
 
 /* link_create.netfilter.flags used in LINK_CREATE command for
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 3ae52978cae6..0674d5ac7b55 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -3349,7 +3349,7 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 		return -EINVAL;
 
 	flags = attr->link_create.uprobe_multi.flags;
-	if (flags & ~BPF_F_UPROBE_MULTI_RETURN)
+	if (flags & ~(BPF_F_UPROBE_MULTI_RETURN|BPF_F_UPROBE_MULTI_UNIQUE))
 		return -EINVAL;
 
 	/*
@@ -3423,6 +3423,8 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 
 		uprobes[i].link = link;
 
+		if (flags & BPF_F_UPROBE_MULTI_UNIQUE)
+			uprobes[i].consumer.is_unique = true;
 		if (!(flags & BPF_F_UPROBE_MULTI_RETURN))
 			uprobes[i].consumer.handler = uprobe_multi_link_handler;
 		if (flags & BPF_F_UPROBE_MULTI_RETURN || is_uprobe_session(prog))
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 233de8677382..3de9eb469fe2 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1300,7 +1300,8 @@ enum {
  * BPF_TRACE_UPROBE_MULTI attach type to create return probe.
  */
 enum {
-	BPF_F_UPROBE_MULTI_RETURN = (1U << 0)
+	BPF_F_UPROBE_MULTI_RETURN = (1U << 0),
+	BPF_F_UPROBE_MULTI_UNIQUE = (1U << 1),
 };
 
 /* link_create.netfilter.flags used in LINK_CREATE command for
-- 
2.51.0
Re: [PATCH perf/core 04/11] bpf: Add support to attach uprobe_multi unique uprobe
Posted by Alexei Starovoitov 1 month ago
On Tue, Sep 2, 2025 at 7:38 AM Jiri Olsa <jolsa@kernel.org> wrote:
>
> Adding support to attach unique uprobe through uprobe multi link
> interface.
>
> Adding new BPF_F_UPROBE_MULTI_UNIQUE flag that denotes the unique
> uprobe creation.
>
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
>  include/uapi/linux/bpf.h       | 3 ++-
>  kernel/trace/bpf_trace.c       | 4 +++-
>  tools/include/uapi/linux/bpf.h | 3 ++-
>  3 files changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 233de8677382..3de9eb469fe2 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -1300,7 +1300,8 @@ enum {
>   * BPF_TRACE_UPROBE_MULTI attach type to create return probe.
>   */
>  enum {
> -       BPF_F_UPROBE_MULTI_RETURN = (1U << 0)
> +       BPF_F_UPROBE_MULTI_RETURN = (1U << 0),
> +       BPF_F_UPROBE_MULTI_UNIQUE = (1U << 1),

I second Masami's point. "exclusive" name fits better.
And once you use that name the "multi_exclusive"
part will not make sense.
How can an exclusive user of the uprobe be "multi" at the same time?
Like attaching to multiple uprobes and modifying regsiters
in all of them? Is it practical ?
It feels to me BPF_F_UPROBE_EXCLUSIVE should be targeting
one specific uprobe.
Re: [PATCH perf/core 04/11] bpf: Add support to attach uprobe_multi unique uprobe
Posted by Jiri Olsa 1 month ago
On Tue, Sep 02, 2025 at 09:11:22AM -0700, Alexei Starovoitov wrote:
> On Tue, Sep 2, 2025 at 7:38 AM Jiri Olsa <jolsa@kernel.org> wrote:
> >
> > Adding support to attach unique uprobe through uprobe multi link
> > interface.
> >
> > Adding new BPF_F_UPROBE_MULTI_UNIQUE flag that denotes the unique
> > uprobe creation.
> >
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> >  include/uapi/linux/bpf.h       | 3 ++-
> >  kernel/trace/bpf_trace.c       | 4 +++-
> >  tools/include/uapi/linux/bpf.h | 3 ++-
> >  3 files changed, 7 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> > index 233de8677382..3de9eb469fe2 100644
> > --- a/include/uapi/linux/bpf.h
> > +++ b/include/uapi/linux/bpf.h
> > @@ -1300,7 +1300,8 @@ enum {
> >   * BPF_TRACE_UPROBE_MULTI attach type to create return probe.
> >   */
> >  enum {
> > -       BPF_F_UPROBE_MULTI_RETURN = (1U << 0)
> > +       BPF_F_UPROBE_MULTI_RETURN = (1U << 0),
> > +       BPF_F_UPROBE_MULTI_UNIQUE = (1U << 1),
> 
> I second Masami's point. "exclusive" name fits better.
> And once you use that name the "multi_exclusive"
> part will not make sense.
> How can an exclusive user of the uprobe be "multi" at the same time?
> Like attaching to multiple uprobes and modifying regsiters
> in all of them? Is it practical ?

we can still attach single uprobe with uprobe_multi,
but for more uprobes it's probably not practical

> It till attach single uprobe with eels to me BPF_F_UPROBE_EXCLUSIVE should be targeting
> one specific uprobe.

do you mean to force single uprobe with this flag?

I understood 'BPF_F_UPROBE_MULTI_' flag prefix more as indication what link
it belongs to, but I'm ok with BPF_F_UPROBE_EXCLUSIVE

thanks,
jirka
Re: [PATCH perf/core 04/11] bpf: Add support to attach uprobe_multi unique uprobe
Posted by Alexei Starovoitov 4 weeks, 1 day ago
On Tue, Sep 2, 2025 at 11:35 PM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Tue, Sep 02, 2025 at 09:11:22AM -0700, Alexei Starovoitov wrote:
> > On Tue, Sep 2, 2025 at 7:38 AM Jiri Olsa <jolsa@kernel.org> wrote:
> > >
> > > Adding support to attach unique uprobe through uprobe multi link
> > > interface.
> > >
> > > Adding new BPF_F_UPROBE_MULTI_UNIQUE flag that denotes the unique
> > > uprobe creation.
> > >
> > > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > > ---
> > >  include/uapi/linux/bpf.h       | 3 ++-
> > >  kernel/trace/bpf_trace.c       | 4 +++-
> > >  tools/include/uapi/linux/bpf.h | 3 ++-
> > >  3 files changed, 7 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> > > index 233de8677382..3de9eb469fe2 100644
> > > --- a/include/uapi/linux/bpf.h
> > > +++ b/include/uapi/linux/bpf.h
> > > @@ -1300,7 +1300,8 @@ enum {
> > >   * BPF_TRACE_UPROBE_MULTI attach type to create return probe.
> > >   */
> > >  enum {
> > > -       BPF_F_UPROBE_MULTI_RETURN = (1U << 0)
> > > +       BPF_F_UPROBE_MULTI_RETURN = (1U << 0),
> > > +       BPF_F_UPROBE_MULTI_UNIQUE = (1U << 1),
> >
> > I second Masami's point. "exclusive" name fits better.
> > And once you use that name the "multi_exclusive"
> > part will not make sense.
> > How can an exclusive user of the uprobe be "multi" at the same time?
> > Like attaching to multiple uprobes and modifying regsiters
> > in all of them? Is it practical ?
>
> we can still attach single uprobe with uprobe_multi,
> but for more uprobes it's probably not practical
>
> > It till attach single uprobe with eels to me BPF_F_UPROBE_EXCLUSIVE should be targeting
> > one specific uprobe.
>
> do you mean to force single uprobe with this flag?
>
> I understood 'BPF_F_UPROBE_MULTI_' flag prefix more as indication what link
> it belongs to, but I'm ok with BPF_F_UPROBE_EXCLUSIVE

What is the use case for attaching the same bpf prog to multiple
uprobes and modifying their registers?
Re: [PATCH perf/core 04/11] bpf: Add support to attach uprobe_multi unique uprobe
Posted by Jiri Olsa 4 weeks, 1 day ago
On Wed, Sep 03, 2025 at 08:32:14AM -0700, Alexei Starovoitov wrote:
> On Tue, Sep 2, 2025 at 11:35 PM Jiri Olsa <olsajiri@gmail.com> wrote:
> >
> > On Tue, Sep 02, 2025 at 09:11:22AM -0700, Alexei Starovoitov wrote:
> > > On Tue, Sep 2, 2025 at 7:38 AM Jiri Olsa <jolsa@kernel.org> wrote:
> > > >
> > > > Adding support to attach unique uprobe through uprobe multi link
> > > > interface.
> > > >
> > > > Adding new BPF_F_UPROBE_MULTI_UNIQUE flag that denotes the unique
> > > > uprobe creation.
> > > >
> > > > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > > > ---
> > > >  include/uapi/linux/bpf.h       | 3 ++-
> > > >  kernel/trace/bpf_trace.c       | 4 +++-
> > > >  tools/include/uapi/linux/bpf.h | 3 ++-
> > > >  3 files changed, 7 insertions(+), 3 deletions(-)
> > > >
> > > > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> > > > index 233de8677382..3de9eb469fe2 100644
> > > > --- a/include/uapi/linux/bpf.h
> > > > +++ b/include/uapi/linux/bpf.h
> > > > @@ -1300,7 +1300,8 @@ enum {
> > > >   * BPF_TRACE_UPROBE_MULTI attach type to create return probe.
> > > >   */
> > > >  enum {
> > > > -       BPF_F_UPROBE_MULTI_RETURN = (1U << 0)
> > > > +       BPF_F_UPROBE_MULTI_RETURN = (1U << 0),
> > > > +       BPF_F_UPROBE_MULTI_UNIQUE = (1U << 1),
> > >
> > > I second Masami's point. "exclusive" name fits better.
> > > And once you use that name the "multi_exclusive"
> > > part will not make sense.
> > > How can an exclusive user of the uprobe be "multi" at the same time?
> > > Like attaching to multiple uprobes and modifying regsiters
> > > in all of them? Is it practical ?
> >
> > we can still attach single uprobe with uprobe_multi,
> > but for more uprobes it's probably not practical
> >
> > > It till attach single uprobe with eels to me BPF_F_UPROBE_EXCLUSIVE should be targeting
> > > one specific uprobe.
> >
> > do you mean to force single uprobe with this flag?
> >
> > I understood 'BPF_F_UPROBE_MULTI_' flag prefix more as indication what link
> > it belongs to, but I'm ok with BPF_F_UPROBE_EXCLUSIVE
> 
> What is the use case for attaching the same bpf prog to multiple
> uprobes and modifying their registers?

I dont have one.. but I guess you could have just one bpf program
doing the whatever override based on uprobe cookie?

I added the unique flag support in here because you can also
create just single uprobe with uprobe_multi interface

jirka