• Andrii Nakryiko's avatar
    libbpf: Allow passing preallocated log_buf when loading BTF into kernel · 1a190d1e
    Andrii Nakryiko authored
    Add libbpf-internal btf_load_into_kernel() that allows to pass
    preallocated log_buf and custom log_level to be passed into kernel
    during BPF_BTF_LOAD call. When custom log_buf is provided,
    btf_load_into_kernel() won't attempt an retry with automatically
    allocated internal temporary buffer to capture BTF validation log.
    
    It's important to note the relation between log_buf and log_level, which
    slightly deviates from stricter kernel logic. From kernel's POV, if
    log_buf is specified, log_level has to be > 0, and vice versa. While
    kernel has good reasons to request such "sanity, this, in practice, is
    a bit unconvenient and restrictive for libbpf's high-level bpf_object APIs.
    
    So libbpf will allow to set non-NULL log_buf and log_level == 0. This is
    fine and means to attempt to load BTF without logging requested, but if
    it failes, retry the load with custom log_buf and log_level 1. Similar
    logic will be implemented for program loading. In practice this means
    that users can provide custom log buffer just in case error happens, but
    not really request slower verbose logging all the time. This is also
    consistent with libbpf behavior when custom log_buf is not set: libbpf
    first tries to load everything with log_level=0, and only if error
    happens allocates internal log buffer and retries with log_level=1.
    
    Also, while at it, make BTF validation log more obvious and follow the log
    pattern libbpf is using for dumping BPF verifier log during
    BPF_PROG_LOAD. BTF loading resulting in an error will look like this:
    
    libbpf: BTF loading error: -22
    libbpf: -- BEGIN BTF LOAD LOG ---
    magic: 0xeb9f
    version: 1
    flags: 0x0
    hdr_len: 24
    type_off: 0
    type_len: 1040
    str_off: 1040
    str_len: 2063598257
    btf_total_size: 1753
    Total section length too long
    -- END BTF LOAD LOG --
    libbpf: Error loading .BTF into kernel: -22. BTF is optional, ignoring.
    
    This makes it much easier to find relevant parts in libbpf log output.
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211209193840.1248570-4-andrii@kernel.org
    1a190d1e
libbpf_internal.h 13.2 KB