• Andrii Nakryiko's avatar
    libbpf: use stable map placeholder FDs · dac645b9
    Andrii Nakryiko authored
    Move map creation to later during BPF object loading by pre-creating
    stable placeholder FDs (utilizing memfd_create()). Use dup2()
    syscall to then atomically make those placeholder FDs point to real
    kernel BPF map objects.
    
    This change allows to delay BPF map creation to after all the BPF
    program relocations. That, in turn, allows to delay BTF finalization and
    loading into kernel to after all the relocations as well. We'll take
    advantage of the latter in subsequent patches to allow libbpf to adjust
    BTF in a way that helps with BPF global function usage.
    
    Clean up a few places where we close map->fd, which now shouldn't
    happen, because map->fd should be a valid FD regardless of whether map
    was created or not. Surprisingly and nicely it simplifies a bunch of
    error handling code. If this change doesn't backfire, I'm tempted to
    pre-create such stable FDs for other entities (progs, maybe even BTF).
    We previously did some manipulations to make gen_loader work with fake
    map FDs, with stable map FDs this hack is not necessary for maps (we
    still have it for BTF, but I left it as is for now).
    Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240104013847.3875810-5-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    dac645b9
libbpf_internal.h 17.7 KB