Skip to content

Commit 7e1b468

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
libbpf: Add uprobe multi link detection
Adding uprobe-multi link detection. It will be used later in bpf_program__attach_usdt function to check and use uprobe_multi link over standard uprobe links. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Link: https://lore.kernel.org/r/20230809083440.3209381-17-jolsa@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 5bfdd32 commit 7e1b468

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

tools/lib/bpf/libbpf.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4829,6 +4829,39 @@ static int probe_perf_link(void)
48294829
return link_fd < 0 && err == -EBADF;
48304830
}
48314831

4832+
static int probe_uprobe_multi_link(void)
4833+
{
4834+
LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
4835+
.expected_attach_type = BPF_TRACE_UPROBE_MULTI,
4836+
);
4837+
LIBBPF_OPTS(bpf_link_create_opts, link_opts);
4838+
struct bpf_insn insns[] = {
4839+
BPF_MOV64_IMM(BPF_REG_0, 0),
4840+
BPF_EXIT_INSN(),
4841+
};
4842+
int prog_fd, link_fd, err;
4843+
unsigned long offset = 0;
4844+
4845+
prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
4846+
insns, ARRAY_SIZE(insns), &load_opts);
4847+
if (prog_fd < 0)
4848+
return -errno;
4849+
4850+
/* Creating uprobe in '/' binary should fail with -EBADF. */
4851+
link_opts.uprobe_multi.path = "/";
4852+
link_opts.uprobe_multi.offsets = &offset;
4853+
link_opts.uprobe_multi.cnt = 1;
4854+
4855+
link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts);
4856+
err = -errno; /* close() can clobber errno */
4857+
4858+
if (link_fd >= 0)
4859+
close(link_fd);
4860+
close(prog_fd);
4861+
4862+
return link_fd < 0 && err == -EBADF;
4863+
}
4864+
48324865
static int probe_kern_bpf_cookie(void)
48334866
{
48344867
struct bpf_insn insns[] = {
@@ -4925,6 +4958,9 @@ static struct kern_feature_desc {
49254958
[FEAT_SYSCALL_WRAPPER] = {
49264959
"Kernel using syscall wrapper", probe_kern_syscall_wrapper,
49274960
},
4961+
[FEAT_UPROBE_MULTI_LINK] = {
4962+
"BPF multi-uprobe link support", probe_uprobe_multi_link,
4963+
},
49284964
};
49294965

49304966
bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)

tools/lib/bpf/libbpf_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ enum kern_feature_id {
355355
FEAT_BTF_ENUM64,
356356
/* Kernel uses syscall wrapper (CONFIG_ARCH_HAS_SYSCALL_WRAPPER) */
357357
FEAT_SYSCALL_WRAPPER,
358+
/* BPF multi-uprobe link support */
359+
FEAT_UPROBE_MULTI_LINK,
358360
__FEAT_CNT,
359361
};
360362

0 commit comments

Comments
 (0)