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

libbpf: add support for BPF cookie for raw_tp/tp_btf programs

Wire up BPF cookie passing or raw_tp and tp_btf programs, both in
low-level and high-level APIs.

Acked-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Message-ID: <20240319233852.1977493-5-andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
36ffb202 68ca5d4e

+53 -5
+14 -2
tools/lib/bpf/bpf.c
··· 785 785 if (!OPTS_ZEROED(opts, uprobe_multi)) 786 786 return libbpf_err(-EINVAL); 787 787 break; 788 + case BPF_TRACE_RAW_TP: 788 789 case BPF_TRACE_FENTRY: 789 790 case BPF_TRACE_FEXIT: 790 791 case BPF_MODIFY_RETURN: ··· 1174 1173 return bpf_obj_get_info_by_fd(link_fd, info, info_len); 1175 1174 } 1176 1175 1177 - int bpf_raw_tracepoint_open(const char *name, int prog_fd) 1176 + int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts) 1178 1177 { 1179 1178 const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint); 1180 1179 union bpf_attr attr; 1181 1180 int fd; 1182 1181 1182 + if (!OPTS_VALID(opts, bpf_raw_tp_opts)) 1183 + return libbpf_err(-EINVAL); 1184 + 1183 1185 memset(&attr, 0, attr_sz); 1184 - attr.raw_tracepoint.name = ptr_to_u64(name); 1185 1186 attr.raw_tracepoint.prog_fd = prog_fd; 1187 + attr.raw_tracepoint.name = ptr_to_u64(OPTS_GET(opts, tp_name, NULL)); 1188 + attr.raw_tracepoint.cookie = OPTS_GET(opts, cookie, 0); 1186 1189 1187 1190 fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz); 1188 1191 return libbpf_err_errno(fd); 1192 + } 1193 + 1194 + int bpf_raw_tracepoint_open(const char *name, int prog_fd) 1195 + { 1196 + LIBBPF_OPTS(bpf_raw_tp_opts, opts, .tp_name = name); 1197 + 1198 + return bpf_raw_tracepoint_open_opts(prog_fd, &opts); 1189 1199 } 1190 1200 1191 1201 int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts)
+9
tools/lib/bpf/bpf.h
··· 617 617 __u32 query_flags, __u32 *attach_flags, 618 618 __u32 *prog_ids, __u32 *prog_cnt); 619 619 620 + struct bpf_raw_tp_opts { 621 + size_t sz; /* size of this struct for forward/backward compatibility */ 622 + const char *tp_name; 623 + __u64 cookie; 624 + size_t :0; 625 + }; 626 + #define bpf_raw_tp_opts__last_field cookie 627 + 628 + LIBBPF_API int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts); 620 629 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); 621 630 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, 622 631 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
+17 -3
tools/lib/bpf/libbpf.c
··· 12309 12309 return libbpf_get_error(*link); 12310 12310 } 12311 12311 12312 - struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, 12313 - const char *tp_name) 12312 + struct bpf_link * 12313 + bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog, 12314 + const char *tp_name, 12315 + struct bpf_raw_tracepoint_opts *opts) 12314 12316 { 12317 + LIBBPF_OPTS(bpf_raw_tp_opts, raw_opts); 12315 12318 char errmsg[STRERR_BUFSIZE]; 12316 12319 struct bpf_link *link; 12317 12320 int prog_fd, pfd; 12321 + 12322 + if (!OPTS_VALID(opts, bpf_raw_tracepoint_opts)) 12323 + return libbpf_err_ptr(-EINVAL); 12318 12324 12319 12325 prog_fd = bpf_program__fd(prog); 12320 12326 if (prog_fd < 0) { ··· 12333 12327 return libbpf_err_ptr(-ENOMEM); 12334 12328 link->detach = &bpf_link__detach_fd; 12335 12329 12336 - pfd = bpf_raw_tracepoint_open(tp_name, prog_fd); 12330 + raw_opts.tp_name = tp_name; 12331 + raw_opts.cookie = OPTS_GET(opts, cookie, 0); 12332 + pfd = bpf_raw_tracepoint_open_opts(prog_fd, &raw_opts); 12337 12333 if (pfd < 0) { 12338 12334 pfd = -errno; 12339 12335 free(link); ··· 12345 12337 } 12346 12338 link->fd = pfd; 12347 12339 return link; 12340 + } 12341 + 12342 + struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, 12343 + const char *tp_name) 12344 + { 12345 + return bpf_program__attach_raw_tracepoint_opts(prog, tp_name, NULL); 12348 12346 } 12349 12347 12350 12348 static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
+11
tools/lib/bpf/libbpf.h
··· 760 760 const char *tp_name, 761 761 const struct bpf_tracepoint_opts *opts); 762 762 763 + struct bpf_raw_tracepoint_opts { 764 + size_t sz; /* size of this struct for forward/backward compatibility */ 765 + __u64 cookie; 766 + size_t :0; 767 + }; 768 + #define bpf_raw_tracepoint_opts__last_field cookie 769 + 763 770 LIBBPF_API struct bpf_link * 764 771 bpf_program__attach_raw_tracepoint(const struct bpf_program *prog, 765 772 const char *tp_name); 773 + LIBBPF_API struct bpf_link * 774 + bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog, 775 + const char *tp_name, 776 + struct bpf_raw_tracepoint_opts *opts); 766 777 767 778 struct bpf_trace_opts { 768 779 /* size of this struct, for forward/backward compatibility */
+2
tools/lib/bpf/libbpf.map
··· 410 410 411 411 LIBBPF_1.4.0 { 412 412 global: 413 + bpf_program__attach_raw_tracepoint_opts; 414 + bpf_raw_tracepoint_open_opts; 413 415 bpf_token_create; 414 416 btf__new_split; 415 417 btf_ext__raw_data;