Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Add uprobe multi link support to bpf_program__attach_usdt

Adding support for usdt_manager_attach_usdt to use uprobe_multi
link to attach to usdt probes.

The uprobe_multi support is detected before the usdt program is
loaded and its expected_attach_type is set accordingly.

If uprobe_multi support is detected the usdt_manager_attach_usdt
gathers uprobes info and calls bpf_program__attach_uprobe to
create all needed uprobes.

If uprobe_multi support is not detected the old behaviour stays.

Also adding usdt.s program section for sleepable usdt probes.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/r/20230809083440.3209381-18-jolsa@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Jiri Olsa and committed by
Alexei Starovoitov
5902da6d 7e1b4681

+82 -17
+11 -2
tools/lib/bpf/libbpf.c
··· 367 367 SEC_SLEEPABLE = 8, 368 368 /* BPF program support non-linear XDP buffer */ 369 369 SEC_XDP_FRAGS = 16, 370 + /* Setup proper attach type for usdt probes. */ 371 + SEC_USDT = 32, 370 372 }; 371 373 372 374 struct bpf_sec_def { ··· 6830 6828 if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS)) 6831 6829 opts->prog_flags |= BPF_F_XDP_HAS_FRAGS; 6832 6830 6831 + /* special check for usdt to use uprobe_multi link */ 6832 + if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) 6833 + prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI; 6834 + 6833 6835 if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) { 6834 6836 int btf_obj_fd = 0, btf_type_id = 0, err; 6835 6837 const char *attach_name; ··· 6902 6896 if (!insns || !insns_cnt) 6903 6897 return -EINVAL; 6904 6898 6905 - load_attr.expected_attach_type = prog->expected_attach_type; 6906 6899 if (kernel_supports(obj, FEAT_PROG_NAME)) 6907 6900 prog_name = prog->name; 6908 6901 load_attr.attach_prog_fd = prog->attach_prog_fd; ··· 6936 6931 insns = prog->insns; 6937 6932 insns_cnt = prog->insns_cnt; 6938 6933 } 6934 + 6935 + /* allow prog_prepare_load_fn to change expected_attach_type */ 6936 + load_attr.expected_attach_type = prog->expected_attach_type; 6939 6937 6940 6938 if (obj->gen_loader) { 6941 6939 bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name, ··· 8767 8759 SEC_DEF("uretprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), 8768 8760 SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), 8769 8761 SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), 8770 - SEC_DEF("usdt+", KPROBE, 0, SEC_NONE, attach_usdt), 8762 + SEC_DEF("usdt+", KPROBE, 0, SEC_USDT, attach_usdt), 8763 + SEC_DEF("usdt.s+", KPROBE, 0, SEC_USDT | SEC_SLEEPABLE, attach_usdt), 8771 8764 SEC_DEF("tc/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), /* alias for tcx */ 8772 8765 SEC_DEF("tc/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), /* alias for tcx */ 8773 8766 SEC_DEF("tcx/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE),
+71 -15
tools/lib/bpf/usdt.c
··· 250 250 251 251 bool has_bpf_cookie; 252 252 bool has_sema_refcnt; 253 + bool has_uprobe_multi; 253 254 }; 254 255 255 256 struct usdt_manager *usdt_manager_new(struct bpf_object *obj) ··· 285 284 */ 286 285 man->has_sema_refcnt = faccessat(AT_FDCWD, ref_ctr_sysfs_path, F_OK, AT_EACCESS) == 0; 287 286 287 + /* 288 + * Detect kernel support for uprobe multi link to be used for attaching 289 + * usdt probes. 290 + */ 291 + man->has_uprobe_multi = kernel_supports(obj, FEAT_UPROBE_MULTI_LINK); 288 292 return man; 289 293 } 290 294 ··· 814 808 long abs_ip; 815 809 struct bpf_link *link; 816 810 } *uprobes; 811 + 812 + struct bpf_link *multi_link; 817 813 }; 818 814 819 815 static int bpf_link_usdt_detach(struct bpf_link *link) ··· 824 816 struct usdt_manager *man = usdt_link->usdt_man; 825 817 int i; 826 818 819 + bpf_link__destroy(usdt_link->multi_link); 820 + 821 + /* When having multi_link, uprobe_cnt is 0 */ 827 822 for (i = 0; i < usdt_link->uprobe_cnt; i++) { 828 823 /* detach underlying uprobe link */ 829 824 bpf_link__destroy(usdt_link->uprobes[i].link); ··· 957 946 const char *usdt_provider, const char *usdt_name, 958 947 __u64 usdt_cookie) 959 948 { 949 + unsigned long *offsets = NULL, *ref_ctr_offsets = NULL; 960 950 int i, err, spec_map_fd, ip_map_fd; 961 951 LIBBPF_OPTS(bpf_uprobe_opts, opts); 962 952 struct hashmap *specs_hash = NULL; 963 953 struct bpf_link_usdt *link = NULL; 964 954 struct usdt_target *targets = NULL; 955 + __u64 *cookies = NULL; 965 956 struct elf_fd elf_fd; 966 957 size_t target_cnt; 967 958 ··· 1010 997 link->link.detach = &bpf_link_usdt_detach; 1011 998 link->link.dealloc = &bpf_link_usdt_dealloc; 1012 999 1013 - link->uprobes = calloc(target_cnt, sizeof(*link->uprobes)); 1014 - if (!link->uprobes) { 1015 - err = -ENOMEM; 1016 - goto err_out; 1000 + if (man->has_uprobe_multi) { 1001 + offsets = calloc(target_cnt, sizeof(*offsets)); 1002 + cookies = calloc(target_cnt, sizeof(*cookies)); 1003 + ref_ctr_offsets = calloc(target_cnt, sizeof(*ref_ctr_offsets)); 1004 + 1005 + if (!offsets || !ref_ctr_offsets || !cookies) { 1006 + err = -ENOMEM; 1007 + goto err_out; 1008 + } 1009 + } else { 1010 + link->uprobes = calloc(target_cnt, sizeof(*link->uprobes)); 1011 + if (!link->uprobes) { 1012 + err = -ENOMEM; 1013 + goto err_out; 1014 + } 1017 1015 } 1018 1016 1019 1017 for (i = 0; i < target_cnt; i++) { ··· 1065 1041 goto err_out; 1066 1042 } 1067 1043 1068 - opts.ref_ctr_offset = target->sema_off; 1069 - opts.bpf_cookie = man->has_bpf_cookie ? spec_id : 0; 1070 - uprobe_link = bpf_program__attach_uprobe_opts(prog, pid, path, 1071 - target->rel_ip, &opts); 1072 - err = libbpf_get_error(uprobe_link); 1073 - if (err) { 1074 - pr_warn("usdt: failed to attach uprobe #%d for '%s:%s' in '%s': %d\n", 1075 - i, usdt_provider, usdt_name, path, err); 1044 + if (man->has_uprobe_multi) { 1045 + offsets[i] = target->rel_ip; 1046 + ref_ctr_offsets[i] = target->sema_off; 1047 + cookies[i] = spec_id; 1048 + } else { 1049 + opts.ref_ctr_offset = target->sema_off; 1050 + opts.bpf_cookie = man->has_bpf_cookie ? spec_id : 0; 1051 + uprobe_link = bpf_program__attach_uprobe_opts(prog, pid, path, 1052 + target->rel_ip, &opts); 1053 + err = libbpf_get_error(uprobe_link); 1054 + if (err) { 1055 + pr_warn("usdt: failed to attach uprobe #%d for '%s:%s' in '%s': %d\n", 1056 + i, usdt_provider, usdt_name, path, err); 1057 + goto err_out; 1058 + } 1059 + 1060 + link->uprobes[i].link = uprobe_link; 1061 + link->uprobes[i].abs_ip = target->abs_ip; 1062 + link->uprobe_cnt++; 1063 + } 1064 + } 1065 + 1066 + if (man->has_uprobe_multi) { 1067 + LIBBPF_OPTS(bpf_uprobe_multi_opts, opts_multi, 1068 + .ref_ctr_offsets = ref_ctr_offsets, 1069 + .offsets = offsets, 1070 + .cookies = cookies, 1071 + .cnt = target_cnt, 1072 + ); 1073 + 1074 + link->multi_link = bpf_program__attach_uprobe_multi(prog, pid, path, 1075 + NULL, &opts_multi); 1076 + if (!link->multi_link) { 1077 + err = -errno; 1078 + pr_warn("usdt: failed to attach uprobe multi for '%s:%s' in '%s': %d\n", 1079 + usdt_provider, usdt_name, path, err); 1076 1080 goto err_out; 1077 1081 } 1078 1082 1079 - link->uprobes[i].link = uprobe_link; 1080 - link->uprobes[i].abs_ip = target->abs_ip; 1081 - link->uprobe_cnt++; 1083 + free(offsets); 1084 + free(ref_ctr_offsets); 1085 + free(cookies); 1082 1086 } 1083 1087 1084 1088 free(targets); ··· 1115 1063 return &link->link; 1116 1064 1117 1065 err_out: 1066 + free(offsets); 1067 + free(ref_ctr_offsets); 1068 + free(cookies); 1069 + 1118 1070 if (link) 1119 1071 bpf_link__destroy(&link->link); 1120 1072 free(targets);