libbpbpf: Check bpf_map/bpf_program fd validity
authorMykyta Yatsenko <yatsenko@meta.com>
Mon, 18 Mar 2024 13:18:08 +0000 (13:18 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 18 Mar 2024 20:45:11 +0000 (13:45 -0700)
commit7b30c296af6525571fc967f6a8661f6e1127369e
treeac27064d83f809fbec1e63ec44e67167d2622d5b
parent7f3edd0c72c3f7214f8f28495f2e6466348eb128
libbpbpf: Check bpf_map/bpf_program fd validity

libbpf creates bpf_program/bpf_map structs for each program/map that
user defines, but it allows to disable creating/loading those objects in
kernel, in that case they won't have associated file descriptor
(fd < 0). Such functionality is used for backward compatibility
with some older kernels.

Nothing prevents users from passing these maps or programs with no
kernel counterpart to libbpf APIs. This change introduces explicit
checks for kernel objects existence, aiming to improve visibility of
those edge cases and provide meaningful warnings to users.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240318131808.95959-1-yatsenko@meta.com
tools/lib/bpf/libbpf.c