Skip to content

Commit d4e7dd4

Browse files
committed
Merge branch 'bpf-add-link_info-support-for-uprobe-multi-link'
Jiri Olsa says: ==================== bpf: Add link_info support for uprobe multi link hi, this patchset adds support to get bpf_link_info details for uprobe_multi links and adding support for bpftool link to display them. v4 changes: - move flags field up in bpf_uprobe_multi_link [Andrii] - include zero terminating byte in path_size [Andrii] - return d_path error directly [Yonghong] - use SEC(".probes") for semaphores [Yonghong] - fix ref_ctr_offsets leak in test [Yonghong] - other smaller fixes [Yonghong] thanks, jirka --- ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents cf97916 + a779569 commit d4e7dd4

File tree

10 files changed

+432
-39
lines changed

10 files changed

+432
-39
lines changed

include/uapi/linux/bpf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6562,6 +6562,16 @@ struct bpf_link_info {
65626562
__u32 flags;
65636563
__u64 missed;
65646564
} kprobe_multi;
6565+
struct {
6566+
__aligned_u64 path;
6567+
__aligned_u64 offsets;
6568+
__aligned_u64 ref_ctr_offsets;
6569+
__aligned_u64 cookies;
6570+
__u32 path_size; /* in/out: real path size on success, including zero byte */
6571+
__u32 count; /* in/out: uprobe_multi offsets/ref_ctr_offsets/cookies count */
6572+
__u32 flags;
6573+
__u32 pid;
6574+
} uprobe_multi;
65656575
struct {
65666576
__u32 type; /* enum bpf_perf_event_type */
65676577
__u32 :32;

kernel/trace/bpf_trace.c

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,6 +3033,7 @@ struct bpf_uprobe_multi_link;
30333033
struct bpf_uprobe {
30343034
struct bpf_uprobe_multi_link *link;
30353035
loff_t offset;
3036+
unsigned long ref_ctr_offset;
30363037
u64 cookie;
30373038
struct uprobe_consumer consumer;
30383039
};
@@ -3041,6 +3042,7 @@ struct bpf_uprobe_multi_link {
30413042
struct path path;
30423043
struct bpf_link link;
30433044
u32 cnt;
3045+
u32 flags;
30443046
struct bpf_uprobe *uprobes;
30453047
struct task_struct *task;
30463048
};
@@ -3082,9 +3084,79 @@ static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link)
30823084
kfree(umulti_link);
30833085
}
30843086

3087+
static int bpf_uprobe_multi_link_fill_link_info(const struct bpf_link *link,
3088+
struct bpf_link_info *info)
3089+
{
3090+
u64 __user *uref_ctr_offsets = u64_to_user_ptr(info->uprobe_multi.ref_ctr_offsets);
3091+
u64 __user *ucookies = u64_to_user_ptr(info->uprobe_multi.cookies);
3092+
u64 __user *uoffsets = u64_to_user_ptr(info->uprobe_multi.offsets);
3093+
u64 __user *upath = u64_to_user_ptr(info->uprobe_multi.path);
3094+
u32 upath_size = info->uprobe_multi.path_size;
3095+
struct bpf_uprobe_multi_link *umulti_link;
3096+
u32 ucount = info->uprobe_multi.count;
3097+
int err = 0, i;
3098+
long left;
3099+
3100+
if (!upath ^ !upath_size)
3101+
return -EINVAL;
3102+
3103+
if ((uoffsets || uref_ctr_offsets || ucookies) && !ucount)
3104+
return -EINVAL;
3105+
3106+
umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3107+
info->uprobe_multi.count = umulti_link->cnt;
3108+
info->uprobe_multi.flags = umulti_link->flags;
3109+
info->uprobe_multi.pid = umulti_link->task ?
3110+
task_pid_nr_ns(umulti_link->task, task_active_pid_ns(current)) : 0;
3111+
3112+
if (upath) {
3113+
char *p, *buf;
3114+
3115+
upath_size = min_t(u32, upath_size, PATH_MAX);
3116+
3117+
buf = kmalloc(upath_size, GFP_KERNEL);
3118+
if (!buf)
3119+
return -ENOMEM;
3120+
p = d_path(&umulti_link->path, buf, upath_size);
3121+
if (IS_ERR(p)) {
3122+
kfree(buf);
3123+
return PTR_ERR(p);
3124+
}
3125+
upath_size = buf + upath_size - p;
3126+
left = copy_to_user(upath, p, upath_size);
3127+
kfree(buf);
3128+
if (left)
3129+
return -EFAULT;
3130+
info->uprobe_multi.path_size = upath_size;
3131+
}
3132+
3133+
if (!uoffsets && !ucookies && !uref_ctr_offsets)
3134+
return 0;
3135+
3136+
if (ucount < umulti_link->cnt)
3137+
err = -ENOSPC;
3138+
else
3139+
ucount = umulti_link->cnt;
3140+
3141+
for (i = 0; i < ucount; i++) {
3142+
if (uoffsets &&
3143+
put_user(umulti_link->uprobes[i].offset, uoffsets + i))
3144+
return -EFAULT;
3145+
if (uref_ctr_offsets &&
3146+
put_user(umulti_link->uprobes[i].ref_ctr_offset, uref_ctr_offsets + i))
3147+
return -EFAULT;
3148+
if (ucookies &&
3149+
put_user(umulti_link->uprobes[i].cookie, ucookies + i))
3150+
return -EFAULT;
3151+
}
3152+
3153+
return err;
3154+
}
3155+
30853156
static const struct bpf_link_ops bpf_uprobe_multi_link_lops = {
30863157
.release = bpf_uprobe_multi_link_release,
30873158
.dealloc = bpf_uprobe_multi_link_dealloc,
3159+
.fill_link_info = bpf_uprobe_multi_link_fill_link_info,
30883160
};
30893161

30903162
static int uprobe_prog_run(struct bpf_uprobe *uprobe,
@@ -3172,7 +3244,6 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
31723244
{
31733245
struct bpf_uprobe_multi_link *link = NULL;
31743246
unsigned long __user *uref_ctr_offsets;
3175-
unsigned long *ref_ctr_offsets = NULL;
31763247
struct bpf_link_primer link_primer;
31773248
struct bpf_uprobe *uprobes = NULL;
31783249
struct task_struct *task = NULL;
@@ -3245,18 +3316,12 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
32453316
if (!uprobes || !link)
32463317
goto error_free;
32473318

3248-
if (uref_ctr_offsets) {
3249-
ref_ctr_offsets = kvcalloc(cnt, sizeof(*ref_ctr_offsets), GFP_KERNEL);
3250-
if (!ref_ctr_offsets)
3251-
goto error_free;
3252-
}
3253-
32543319
for (i = 0; i < cnt; i++) {
32553320
if (ucookies && __get_user(uprobes[i].cookie, ucookies + i)) {
32563321
err = -EFAULT;
32573322
goto error_free;
32583323
}
3259-
if (uref_ctr_offsets && __get_user(ref_ctr_offsets[i], uref_ctr_offsets + i)) {
3324+
if (uref_ctr_offsets && __get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) {
32603325
err = -EFAULT;
32613326
goto error_free;
32623327
}
@@ -3280,14 +3345,15 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
32803345
link->uprobes = uprobes;
32813346
link->path = path;
32823347
link->task = task;
3348+
link->flags = flags;
32833349

32843350
bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI,
32853351
&bpf_uprobe_multi_link_lops, prog);
32863352

32873353
for (i = 0; i < cnt; i++) {
32883354
err = uprobe_register_refctr(d_real_inode(link->path.dentry),
32893355
uprobes[i].offset,
3290-
ref_ctr_offsets ? ref_ctr_offsets[i] : 0,
3356+
uprobes[i].ref_ctr_offset,
32913357
&uprobes[i].consumer);
32923358
if (err) {
32933359
bpf_uprobe_unregister(&path, uprobes, i);
@@ -3299,11 +3365,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
32993365
if (err)
33003366
goto error_free;
33013367

3302-
kvfree(ref_ctr_offsets);
33033368
return bpf_link_settle(&link_primer);
33043369

33053370
error_free:
3306-
kvfree(ref_ctr_offsets);
33073371
kvfree(uprobes);
33083372
kfree(link);
33093373
if (task)

tools/bpf/bpftool/link.c

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,37 @@ show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
294294
jsonw_end_array(json_wtr);
295295
}
296296

297+
static __u64 *u64_to_arr(__u64 val)
298+
{
299+
return (__u64 *) u64_to_ptr(val);
300+
}
301+
302+
static void
303+
show_uprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
304+
{
305+
__u32 i;
306+
307+
jsonw_bool_field(json_wtr, "retprobe",
308+
info->uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN);
309+
jsonw_string_field(json_wtr, "path", (char *) u64_to_ptr(info->uprobe_multi.path));
310+
jsonw_uint_field(json_wtr, "func_cnt", info->uprobe_multi.count);
311+
jsonw_int_field(json_wtr, "pid", (int) info->uprobe_multi.pid);
312+
jsonw_name(json_wtr, "funcs");
313+
jsonw_start_array(json_wtr);
314+
315+
for (i = 0; i < info->uprobe_multi.count; i++) {
316+
jsonw_start_object(json_wtr);
317+
jsonw_uint_field(json_wtr, "offset",
318+
u64_to_arr(info->uprobe_multi.offsets)[i]);
319+
jsonw_uint_field(json_wtr, "ref_ctr_offset",
320+
u64_to_arr(info->uprobe_multi.ref_ctr_offsets)[i]);
321+
jsonw_uint_field(json_wtr, "cookie",
322+
u64_to_arr(info->uprobe_multi.cookies)[i]);
323+
jsonw_end_object(json_wtr);
324+
}
325+
jsonw_end_array(json_wtr);
326+
}
327+
297328
static void
298329
show_perf_event_kprobe_json(struct bpf_link_info *info, json_writer_t *wtr)
299330
{
@@ -465,6 +496,9 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
465496
case BPF_LINK_TYPE_KPROBE_MULTI:
466497
show_kprobe_multi_json(info, json_wtr);
467498
break;
499+
case BPF_LINK_TYPE_UPROBE_MULTI:
500+
show_uprobe_multi_json(info, json_wtr);
501+
break;
468502
case BPF_LINK_TYPE_PERF_EVENT:
469503
switch (info->perf_event.type) {
470504
case BPF_PERF_EVENT_EVENT:
@@ -674,6 +708,33 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info)
674708
}
675709
}
676710

711+
static void show_uprobe_multi_plain(struct bpf_link_info *info)
712+
{
713+
__u32 i;
714+
715+
if (!info->uprobe_multi.count)
716+
return;
717+
718+
if (info->uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN)
719+
printf("\n\turetprobe.multi ");
720+
else
721+
printf("\n\tuprobe.multi ");
722+
723+
printf("path %s ", (char *) u64_to_ptr(info->uprobe_multi.path));
724+
printf("func_cnt %u ", info->uprobe_multi.count);
725+
726+
if (info->uprobe_multi.pid)
727+
printf("pid %d ", info->uprobe_multi.pid);
728+
729+
printf("\n\t%-16s %-16s %-16s", "offset", "ref_ctr_offset", "cookies");
730+
for (i = 0; i < info->uprobe_multi.count; i++) {
731+
printf("\n\t0x%-16llx 0x%-16llx 0x%-16llx",
732+
u64_to_arr(info->uprobe_multi.offsets)[i],
733+
u64_to_arr(info->uprobe_multi.ref_ctr_offsets)[i],
734+
u64_to_arr(info->uprobe_multi.cookies)[i]);
735+
}
736+
}
737+
677738
static void show_perf_event_kprobe_plain(struct bpf_link_info *info)
678739
{
679740
const char *buf;
@@ -807,6 +868,9 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
807868
case BPF_LINK_TYPE_KPROBE_MULTI:
808869
show_kprobe_multi_plain(info);
809870
break;
871+
case BPF_LINK_TYPE_UPROBE_MULTI:
872+
show_uprobe_multi_plain(info);
873+
break;
810874
case BPF_LINK_TYPE_PERF_EVENT:
811875
switch (info->perf_event.type) {
812876
case BPF_PERF_EVENT_EVENT:
@@ -846,8 +910,10 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
846910

847911
static int do_show_link(int fd)
848912
{
913+
__u64 *ref_ctr_offsets = NULL, *offsets = NULL, *cookies = NULL;
849914
struct bpf_link_info info;
850915
__u32 len = sizeof(info);
916+
char path_buf[PATH_MAX];
851917
__u64 *addrs = NULL;
852918
char buf[PATH_MAX];
853919
int count;
@@ -889,6 +955,39 @@ static int do_show_link(int fd)
889955
goto again;
890956
}
891957
}
958+
if (info.type == BPF_LINK_TYPE_UPROBE_MULTI &&
959+
!info.uprobe_multi.offsets) {
960+
count = info.uprobe_multi.count;
961+
if (count) {
962+
offsets = calloc(count, sizeof(__u64));
963+
if (!offsets) {
964+
p_err("mem alloc failed");
965+
close(fd);
966+
return -ENOMEM;
967+
}
968+
info.uprobe_multi.offsets = ptr_to_u64(offsets);
969+
ref_ctr_offsets = calloc(count, sizeof(__u64));
970+
if (!ref_ctr_offsets) {
971+
p_err("mem alloc failed");
972+
free(offsets);
973+
close(fd);
974+
return -ENOMEM;
975+
}
976+
info.uprobe_multi.ref_ctr_offsets = ptr_to_u64(ref_ctr_offsets);
977+
cookies = calloc(count, sizeof(__u64));
978+
if (!cookies) {
979+
p_err("mem alloc failed");
980+
free(cookies);
981+
free(offsets);
982+
close(fd);
983+
return -ENOMEM;
984+
}
985+
info.uprobe_multi.cookies = ptr_to_u64(cookies);
986+
info.uprobe_multi.path = ptr_to_u64(path_buf);
987+
info.uprobe_multi.path_size = sizeof(path_buf);
988+
goto again;
989+
}
990+
}
892991
if (info.type == BPF_LINK_TYPE_PERF_EVENT) {
893992
switch (info.perf_event.type) {
894993
case BPF_PERF_EVENT_TRACEPOINT:
@@ -924,8 +1023,10 @@ static int do_show_link(int fd)
9241023
else
9251024
show_link_close_plain(fd, &info);
9261025

927-
if (addrs)
928-
free(addrs);
1026+
free(ref_ctr_offsets);
1027+
free(cookies);
1028+
free(offsets);
1029+
free(addrs);
9291030
close(fd);
9301031
return 0;
9311032
}

tools/include/uapi/linux/bpf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6562,6 +6562,16 @@ struct bpf_link_info {
65626562
__u32 flags;
65636563
__u64 missed;
65646564
} kprobe_multi;
6565+
struct {
6566+
__aligned_u64 path;
6567+
__aligned_u64 offsets;
6568+
__aligned_u64 ref_ctr_offsets;
6569+
__aligned_u64 cookies;
6570+
__u32 path_size; /* in/out: real path size on success, including zero byte */
6571+
__u32 count; /* in/out: uprobe_multi offsets/ref_ctr_offsets/cookies count */
6572+
__u32 flags;
6573+
__u32 pid;
6574+
} uprobe_multi;
65656575
struct {
65666576
__u32 type; /* enum bpf_perf_event_type */
65676577
__u32 :32;

tools/lib/bpf/elf.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ static int symbol_cmp(const void *a, const void *b)
407407
* size, that needs to be released by the caller.
408408
*/
409409
int elf_resolve_syms_offsets(const char *binary_path, int cnt,
410-
const char **syms, unsigned long **poffsets)
410+
const char **syms, unsigned long **poffsets,
411+
int st_type)
411412
{
412413
int sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
413414
int err = 0, i, cnt_done = 0;
@@ -438,7 +439,7 @@ int elf_resolve_syms_offsets(const char *binary_path, int cnt,
438439
struct elf_sym_iter iter;
439440
struct elf_sym *sym;
440441

441-
err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC);
442+
err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], st_type);
442443
if (err == -ENOENT)
443444
continue;
444445
if (err)

tools/lib/bpf/libbpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11447,7 +11447,7 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
1144711447
return libbpf_err_ptr(err);
1144811448
offsets = resolved_offsets;
1144911449
} else if (syms) {
11450-
err = elf_resolve_syms_offsets(path, cnt, syms, &resolved_offsets);
11450+
err = elf_resolve_syms_offsets(path, cnt, syms, &resolved_offsets, STT_FUNC);
1145111451
if (err < 0)
1145211452
return libbpf_err_ptr(err);
1145311453
offsets = resolved_offsets;

tools/lib/bpf/libbpf_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,8 @@ int elf_open(const char *binary_path, struct elf_fd *elf_fd);
594594
void elf_close(struct elf_fd *elf_fd);
595595

596596
int elf_resolve_syms_offsets(const char *binary_path, int cnt,
597-
const char **syms, unsigned long **poffsets);
597+
const char **syms, unsigned long **poffsets,
598+
int st_type);
598599
int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
599600
unsigned long **poffsets, size_t *pcnt);
600601

0 commit comments

Comments
 (0)