From: Jiri Olsa <jolsa@kernel.org>
To: Oleg Nesterov <oleg@redhat.com>,
Masami Hiramatsu <mhiramat@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Andrii Nakryiko <andrii@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-trace-kernel@vger.kernel.org, x86@kernel.org,
Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
Hao Luo <haoluo@google.com>, Steven Rostedt <rostedt@goodmis.org>,
Ingo Molnar <mingo@kernel.org>
Subject: [PATCH perf/core 07/11] libbpf: Add support to attach generic unique uprobe
Date: Tue, 2 Sep 2025 16:35:00 +0200 [thread overview]
Message-ID: <20250902143504.1224726-8-jolsa@kernel.org> (raw)
In-Reply-To: <20250902143504.1224726-1-jolsa@kernel.org>
Adding support to attach unique generic uprobe by adding the 'unique'
bool flag to struct bpf_uprobe_opts.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
tools/lib/bpf/libbpf.c | 29 ++++++++++++++++++++++++-----
tools/lib/bpf/libbpf.h | 4 +++-
2 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1f613a5f95b6..aac2bd4fb95e 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -11045,11 +11045,19 @@ static int determine_uprobe_retprobe_bit(void)
return parse_uint_from_file(file, "config:%d\n");
}
+static int determine_uprobe_unique_bit(void)
+{
+ const char *file = "/sys/bus/event_source/devices/uprobe/format/unique";
+
+ return parse_uint_from_file(file, "config:%d\n");
+}
+
#define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
#define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
- uint64_t offset, int pid, size_t ref_ctr_off)
+ uint64_t offset, int pid, size_t ref_ctr_off,
+ bool unique)
{
const size_t attr_sz = sizeof(struct perf_event_attr);
struct perf_event_attr attr;
@@ -11080,6 +11088,16 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
}
attr.config |= 1 << bit;
}
+ if (uprobe && unique) {
+ int bit = determine_uprobe_unique_bit();
+
+ if (bit < 0) {
+ pr_warn("failed to determine uprobe unique bit: %s\n",
+ errstr(bit));
+ return bit;
+ }
+ attr.config |= 1 << bit;
+ }
attr.size = attr_sz;
attr.type = type;
attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
@@ -11286,7 +11304,7 @@ int probe_kern_syscall_wrapper(int token_fd)
if (determine_kprobe_perf_type() >= 0) {
int pfd;
- pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0);
+ pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0, false);
if (pfd >= 0)
close(pfd);
@@ -11348,7 +11366,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
if (!legacy) {
pfd = perf_event_open_probe(false /* uprobe */, retprobe,
func_name, offset,
- -1 /* pid */, 0 /* ref_ctr_off */);
+ -1 /* pid */, 0 /* ref_ctr_off */, false /* unique */);
} else {
char probe_name[MAX_EVENT_NAME_LEN];
@@ -12251,7 +12269,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
struct bpf_link *link;
size_t ref_ctr_off;
int pfd, err;
- bool retprobe, legacy;
+ bool retprobe, legacy, unique;
const char *func_name;
if (!OPTS_VALID(opts, bpf_uprobe_opts))
@@ -12261,6 +12279,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
retprobe = OPTS_GET(opts, retprobe, false);
ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
+ unique = OPTS_GET(opts, unique, false);
if (!binary_path)
return libbpf_err_ptr(-EINVAL);
@@ -12321,7 +12340,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
if (!legacy) {
pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
- func_offset, pid, ref_ctr_off);
+ func_offset, pid, ref_ctr_off, unique);
} else {
char probe_name[MAX_EVENT_NAME_LEN];
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 13a10299331b..0a38dee1d9c1 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -701,9 +701,11 @@ struct bpf_uprobe_opts {
const char *func_name;
/* uprobe attach mode */
enum probe_attach_mode attach_mode;
+ /* create unique uprobe */
+ bool unique;
size_t :0;
};
-#define bpf_uprobe_opts__last_field attach_mode
+#define bpf_uprobe_opts__last_field unique
/**
* @brief **bpf_program__attach_uprobe()** attaches a BPF program
--
2.51.0
next prev parent reply other threads:[~2025-09-02 14:36 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-02 14:34 [PATCH perf/core 00/11] uprobes: Add " Jiri Olsa
2025-09-02 14:34 ` [PATCH perf/core 01/11] uprobes: Add unique flag to uprobe consumer Jiri Olsa
2025-09-02 15:11 ` Masami Hiramatsu
2025-09-03 6:35 ` Jiri Olsa
2025-09-03 10:49 ` Oleg Nesterov
2025-09-03 12:02 ` Jiri Olsa
2025-09-02 14:34 ` [PATCH perf/core 02/11] uprobes: Skip emulate/sstep on unique uprobe when ip is changed Jiri Olsa
2025-09-03 11:26 ` Oleg Nesterov
2025-09-03 18:20 ` Andrii Nakryiko
2025-09-03 19:57 ` Jiri Olsa
2025-09-03 19:50 ` Jiri Olsa
2025-09-04 8:49 ` Oleg Nesterov
2025-09-04 10:46 ` Jiri Olsa
2025-09-04 11:23 ` Oleg Nesterov
2025-09-04 15:01 ` Alexei Starovoitov
2025-09-04 18:06 ` Andrii Nakryiko
2025-09-04 18:55 ` Oleg Nesterov
2025-09-04 19:42 ` Andrii Nakryiko
2025-09-05 8:51 ` Oleg Nesterov
2025-09-02 14:34 ` [PATCH perf/core 03/11] perf: Add support to attach standard unique uprobe Jiri Olsa
2025-09-02 16:13 ` Alexei Starovoitov
2025-09-03 3:32 ` kernel test robot
2025-09-03 11:59 ` Oleg Nesterov
2025-09-03 13:03 ` Jiri Olsa
2025-09-03 13:22 ` Oleg Nesterov
2025-09-02 14:34 ` [PATCH perf/core 04/11] bpf: Add support to attach uprobe_multi " Jiri Olsa
2025-09-02 16:11 ` Alexei Starovoitov
2025-09-03 6:35 ` Jiri Olsa
2025-09-03 15:32 ` Alexei Starovoitov
2025-09-03 19:55 ` Jiri Olsa
2025-09-02 14:34 ` [PATCH perf/core 05/11] bpf: Allow uprobe program to change context registers Jiri Olsa
2025-09-02 14:34 ` [PATCH perf/core 06/11] libbpf: Add support to attach unique uprobe_multi uprobe Jiri Olsa
2025-09-02 14:35 ` Jiri Olsa [this message]
2025-09-03 18:26 ` [PATCH perf/core 07/11] libbpf: Add support to attach generic unique uprobe Andrii Nakryiko
2025-09-02 14:35 ` [PATCH perf/core 08/11] selftests/bpf: Add uprobe multi context registers changes test Jiri Olsa
2025-09-02 14:35 ` [PATCH perf/core 09/11] selftests/bpf: Add uprobe multi context ip register change test Jiri Olsa
2025-09-02 14:35 ` [PATCH perf/core 10/11] selftests/bpf: Add uprobe multi unique attach test Jiri Olsa
2025-09-02 14:35 ` [PATCH perf/core 11/11] selftests/bpf: Add uprobe " Jiri Olsa
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250902143504.1224726-8-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=mingo@kernel.org \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=songliubraving@fb.com \
--cc=x86@kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®