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

libbpf: Add bpf_obj_get_opts()

Add an extensible variant of bpf_obj_get() capable of setting the
`file_flags` parameter.

This parameter is needed to enable unprivileged access to BPF maps.
Without a method like this, users must manually make the syscall.

Signed-off-by: Joe Burton <jevburton@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220729202727.3311806-1-jevburton.kernel@gmail.com

authored by

Joe Burton and committed by
Andrii Nakryiko
395fc4fa d0b80a9e

+21
+9
tools/lib/bpf/bpf.c
··· 579 579 580 580 int bpf_obj_get(const char *pathname) 581 581 { 582 + return bpf_obj_get_opts(pathname, NULL); 583 + } 584 + 585 + int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts) 586 + { 582 587 union bpf_attr attr; 583 588 int fd; 584 589 590 + if (!OPTS_VALID(opts, bpf_obj_get_opts)) 591 + return libbpf_err(-EINVAL); 592 + 585 593 memset(&attr, 0, sizeof(attr)); 586 594 attr.pathname = ptr_to_u64((void *)pathname); 595 + attr.file_flags = OPTS_GET(opts, file_flags, 0); 587 596 588 597 fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr)); 589 598 return libbpf_err_errno(fd);
+11
tools/lib/bpf/bpf.h
··· 270 270 __u32 *count, 271 271 const struct bpf_map_batch_opts *opts); 272 272 273 + struct bpf_obj_get_opts { 274 + size_t sz; /* size of this struct for forward/backward compatibility */ 275 + 276 + __u32 file_flags; 277 + 278 + size_t :0; 279 + }; 280 + #define bpf_obj_get_opts__last_field file_flags 281 + 273 282 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); 274 283 LIBBPF_API int bpf_obj_get(const char *pathname); 284 + LIBBPF_API int bpf_obj_get_opts(const char *pathname, 285 + const struct bpf_obj_get_opts *opts); 275 286 276 287 struct bpf_prog_attach_opts { 277 288 size_t sz; /* size of this struct for forward/backward compatibility */
+1
tools/lib/bpf/libbpf.map
··· 355 355 356 356 LIBBPF_1.0.0 { 357 357 global: 358 + bpf_obj_get_opts; 358 359 bpf_prog_query_opts; 359 360 bpf_program__attach_ksyscall; 360 361 btf__add_enum64;