Commit Graph

53 Commits

Author SHA1 Message Date
Jerome Marchand 533d73b4e7 libbpf: Skip forward declaration when counting duplicated type names
Bugzilla: https://bugzilla.redhat.com/2120966

commit 4226961b0019b2e1612029e8950a9e911affc995
Author: Xu Kuohai <xukuohai@huawei.com>
Date:   Tue Mar 1 00:32:49 2022 -0500

    libbpf: Skip forward declaration when counting duplicated type names

    Currently if a declaration appears in the BTF before the definition, the
    definition is dumped as a conflicting name, e.g.:

        $ bpftool btf dump file vmlinux format raw | grep "'unix_sock'"
        [81287] FWD 'unix_sock' fwd_kind=struct
        [89336] STRUCT 'unix_sock' size=1024 vlen=14

        $ bpftool btf dump file vmlinux format c | grep "struct unix_sock"
        struct unix_sock;
        struct unix_sock___2 {	<--- conflict, the "___2" is unexpected
    		    struct unix_sock___2 *unix_sk;

    This causes a compilation error if the dump output is used as a header file.

    Fix it by skipping declaration when counting duplicated type names.

    Fixes: 351131b51c ("libbpf: add btf_dump API for BTF-to-C conversion")
    Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20220301053250.1464204-2-xukuohai@huawei.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:52 +02:00
Artem Savkov 8b7632b54c libbpf: Add sane strncpy alternative and use it internally
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 9fc205b413b3f3e9502fa92151fba63b91230454
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Dec 10 16:40:43 2021 -0800

    libbpf: Add sane strncpy alternative and use it internally

    strncpy() has a notoriously error-prone semantics which makes GCC
    complain about it a lot (and quite often completely completely falsely
    at that). Instead of pleasing GCC all the time (-Wno-stringop-truncation
    is unfortunately only supported by GCC, so it's a bit too messy to just
    enable it in Makefile), add libbpf-internal libbpf_strlcpy() helper
    which follows what FreeBSD's strlcpy() does and what most people would
    expect from strncpy(): copies up to N-1 first bytes from source string
    into destination string and ensures zero-termination afterwards.

    Replace all the relevant uses of strncpy/strncat/memcpy in libbpf with
    libbpf_strlcpy().

    This also fixes the issue reported by Emmanuel Deloget in xsk.c where
    memcpy() could access source string beyond its end.

    Fixes: 2f6324a393 (libbpf: Support shared umems between queues and devices)
    Reported-by: Emmanuel Deloget <emmanuel.deloget@eho.link>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20211211004043.2374068-1-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:48 +02:00
Artem Savkov 88a2fca700 libbpf: Support BTF_KIND_TYPE_TAG
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 2dc1e488e5cdfd937554ca81fd46ad874d244b3f
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Nov 11 17:26:14 2021 -0800

    libbpf: Support BTF_KIND_TYPE_TAG

    Add libbpf support for BTF_KIND_TYPE_TAG.

    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211112012614.1505315-1-yhs@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:36 +02:00
Artem Savkov bb33c493ab libbpf: Ensure btf_dump__new() and btf_dump_opts are future-proof
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 6084f5dc928f2ada4331ba9eda65542e94d86bc6
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Nov 10 21:36:19 2021 -0800

    libbpf: Ensure btf_dump__new() and btf_dump_opts are future-proof

    Change btf_dump__new() and corresponding struct btf_dump_ops structure
    to be extensible by using OPTS "framework" ([0]). Given we don't change
    the names, we use a similar approach as with bpf_prog_load(), but this
    time we ended up with two APIs with the same name and same number of
    arguments, so overloading based on number of arguments with
    ___libbpf_override() doesn't work.

    Instead, use "overloading" based on types. In this particular case,
    print callback has to be specified, so we detect which argument is
    a callback. If it's 4th (last) argument, old implementation of API is
    used by user code. If not, it must be 2nd, and thus new implementation
    is selected. The rest is handled by the same symbol versioning approach.

    btf_ext argument is dropped as it was never used and isn't necessary
    either. If in the future we'll need btf_ext, that will be added into
    OPTS-based struct btf_dump_opts.

    struct btf_dump_opts is reused for both old API and new APIs. ctx field
    is marked deprecated in v0.7+ and it's put at the same memory location
    as OPTS's sz field. Any user of new-style btf_dump__new() will have to
    set sz field and doesn't/shouldn't use ctx, as ctx is now passed along
    the callback as mandatory input argument, following the other APIs in
    libbpf that accept callbacks consistently.

    Again, this is quite ugly in implementation, but is done in the name of
    backwards compatibility and uniform and extensible future APIs (at the
    same time, sigh). And it will be gone in libbpf 1.0.

      [0] Closes: https://github.com/libbpf/libbpf/issues/283

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211111053624.190580-5-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:35 +02:00
Yauheni Kaliuta 87f52db921 libbpf: Use __BYTE_ORDER__
Bugzilla: http://bugzilla.redhat.com/2069045

commit 3930198dc9a0720a2b6561c67e55859ec51b73f9
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Tue Oct 26 03:08:27 2021 +0200

    libbpf: Use __BYTE_ORDER__
    
    Use the compiler-defined __BYTE_ORDER__ instead of the libc-defined
    __BYTE_ORDER for consistency.
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211026010831.748682-3-iii@linux.ibm.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:46 +03:00
Yauheni Kaliuta b16b2c8c85 libbpf: Add btf__type_cnt() and btf__raw_data() APIs
Bugzilla: http://bugzilla.redhat.com/2069045

commit 6a886de070fad850d6cb74a787c9ed017303d9ac
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Fri Oct 22 21:06:19 2021 +0800

    libbpf: Add btf__type_cnt() and btf__raw_data() APIs
    
    Add btf__type_cnt() and btf__raw_data() APIs and deprecate
    btf__get_nr_type() and btf__get_raw_data() since the old APIs
    don't follow the libbpf naming convention for getters which
    omit 'get' in the name (see [0]). btf__raw_data() is just an
    alias to the existing btf__get_raw_data(). btf__type_cnt()
    now returns the number of all types of the BTF object
    including 'void'.
    
      [0] Closes: https://github.com/libbpf/libbpf/issues/279
    
    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211022130623.1548429-2-hengqi.chen@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:44 +03:00
Yauheni Kaliuta b0f8acb5ef libbpf: Fix the use of aligned attribute
Bugzilla: http://bugzilla.redhat.com/2069045

commit fae1b05e6f0acf116f6450535b0e1c13051102d3
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Oct 22 12:25:02 2021 -0700

    libbpf: Fix the use of aligned attribute
    
    Building libbpf sources out of kernel tree (in Github repo) we run into
    compilation error due to unknown __aligned attribute. It must be coming
    from some kernel header, which is not available to Github sources. Use
    explicit __attribute__((aligned(16))) instead.
    
    Fixes: 961632d54163 ("libbpf: Fix dumping non-aligned __int128")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211022192502.2975553-1-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:44 +03:00
Yauheni Kaliuta f9e11cbec9 libbpf: Fix ptr_is_aligned() usages
Bugzilla: http://bugzilla.redhat.com/2069045

commit 632f96d2652e0d4188de837f88a977eedb55aee1
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Thu Oct 21 12:46:58 2021 +0200

    libbpf: Fix ptr_is_aligned() usages
    
    Currently ptr_is_aligned() takes size, and not alignment, as a
    parameter, which may be overly pessimistic e.g. for __i128 on s390,
    which must be only 8-byte aligned. Fix by using btf__align_of().
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211021104658.624944-2-iii@linux.ibm.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:43 +03:00
Yauheni Kaliuta 670b79821b libbpf: Fix dumping non-aligned __int128
Bugzilla: http://bugzilla.redhat.com/2069045

commit 961632d5416389a6f2e7478b674ac645c7e3ff01
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Oct 13 18:09:01 2021 +0200

    libbpf: Fix dumping non-aligned __int128
    
    Non-aligned integers are dumped as bitfields, which is supported for at
    most 64-bit integers. Fix by using the same trick as
    btf_dump_float_data(): copy non-aligned values to the local buffer.
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211013160902.428340-4-iii@linux.ibm.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:43 +03:00
Yauheni Kaliuta 69bf90c0da libbpf: Fix dumping big-endian bitfields
Bugzilla: http://bugzilla.redhat.com/2069045

commit c9e982b879465ca74e3593ce82808aa259265a71
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Oct 13 18:09:00 2021 +0200

    libbpf: Fix dumping big-endian bitfields
    
    On big-endian arches not only bytes, but also bits are numbered in
    reverse order (see e.g. S/390 ELF ABI Supplement, but this is also true
    for other big-endian arches as well).
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211013160902.428340-3-iii@linux.ibm.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:42 +03:00
Yauheni Kaliuta fdbdd94ffe bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG
Bugzilla: http://bugzilla.redhat.com/2069045

commit 223f903e9c832699f4e5f422281a60756c1c6cfe
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Oct 12 09:48:38 2021 -0700

    bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG
    
    Patch set [1] introduced BTF_KIND_TAG to allow tagging
    declarations for struct/union, struct/union field, var, func
    and func arguments and these tags will be encoded into
    dwarf. They are also encoded to btf by llvm for the bpf target.
    
    After BTF_KIND_TAG is introduced, we intended to use it
    for kernel __user attributes. But kernel __user is actually
    a type attribute. Upstream and internal discussion showed
    it is not a good idea to mix declaration attribute and
    type attribute. So we proposed to introduce btf_type_tag
    as a type attribute and existing btf_tag renamed to
    btf_decl_tag ([2]).
    
    This patch renamed BTF_KIND_TAG to BTF_KIND_DECL_TAG and some
    other declarations with *_tag to *_decl_tag to make it clear
    the tag is for declaration. In the future, BTF_KIND_TYPE_TAG
    might be introduced per [3].
    
     [1] https://lore.kernel.org/bpf/20210914223004.244411-1-yhs@fb.com/
     [2] https://reviews.llvm.org/D111588
     [3] https://reviews.llvm.org/D111199
    
    Fixes: b5ea834dde6b ("bpf: Support for new btf kind BTF_KIND_TAG")
    Fixes: 5b84bd10363e ("libbpf: Add support for BTF_KIND_TAG")
    Fixes: 5c07f2fec003 ("bpftool: Add support for BTF_KIND_TAG")
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211012164838.3345699-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:42 +03:00
Yauheni Kaliuta 0881d7268d libbpf: Add support for BTF_KIND_TAG
Bugzilla: http://bugzilla.redhat.com/2069045

commit 5b84bd10363e36ceb7c4c1ae749a3fc8adf8df45
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Sep 14 15:30:25 2021 -0700

    libbpf: Add support for BTF_KIND_TAG
    
    Add BTF_KIND_TAG support for parsing and dedup.
    Also added sanitization for BTF_KIND_TAG. If BTF_KIND_TAG is not
    supported in the kernel, sanitize it to INTs.
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210914223025.246687-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:16:12 +03:00
Jerome Marchand 5ec84ff32e libbpf: Fix compilation warning due to mismatched printf format
Bugzilla: https://bugzilla.redhat.com/2041365

commit dc37dc617fabfb1c3a16d49f5d8cc20e9e3608ca
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Feb 8 22:39:09 2022 -0800

    libbpf: Fix compilation warning due to mismatched printf format

    On ppc64le architecture __s64 is long int and requires %ld. Cast to
    ssize_t and use %zd to avoid architecture-specific specifiers.

    Fixes: 4172843ed4a3 ("libbpf: Fix signedness bug in btf_dump_array_data()")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220209063909.1268319-1-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:17:16 +02:00
Jerome Marchand db98a683b1 libbpf: Fix signedness bug in btf_dump_array_data()
Bugzilla: https://bugzilla.redhat.com/2041365

commit 4172843ed4a38f97084032f74f07b2037b5da3a6
Author: Dan Carpenter <dan.carpenter@oracle.com>
Date:   Tue Feb 8 10:15:52 2022 +0300

    libbpf: Fix signedness bug in btf_dump_array_data()

    The btf__resolve_size() function returns negative error codes so
    "elem_size" must be signed for the error handling to work.

    Fixes: 920d16af9b42 ("libbpf: BTF dumper support for typed data")
    Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20220208071552.GB10495@kili

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:17:16 +02:00
Jerome Marchand 47daeda65d libbpf: Silence uninitialized warning/error in btf_dump_dump_type_data
Bugzilla: https://bugzilla.redhat.com/2041365

commit 43174f0d4597325cb91f1f1f55263eb6e6101036
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Mon Nov 29 10:00:40 2021 +0000

    libbpf: Silence uninitialized warning/error in btf_dump_dump_type_data

    When compiling libbpf with gcc 4.8.5, we see:

      CC       staticobjs/btf_dump.o
    btf_dump.c: In function ‘btf_dump_dump_type_data.isra.24’:
    btf_dump.c:2296:5: error: ‘err’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      if (err < 0)
         ^
    cc1: all warnings being treated as errors
    make: *** [staticobjs/btf_dump.o] Error 1

    While gcc 4.8.5 is too old to build the upstream kernel, it's possible it
    could be used to build standalone libbpf which suffers from the same problem.
    Silence the error by initializing 'err' to 0.  The warning/error seems to be
    a false positive since err is set early in the function.  Regardless we
    shouldn't prevent libbpf from building for this.

    Fixes: 920d16af9b42 ("libbpf: BTF dumper support for typed data")
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1638180040-8037-1-git-send-email-alan.maguire@oracle.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:17:16 +02:00
Jerome Marchand e3520864ce libbpf: Propagate errors when retrieving enum value for typed data display
Bugzilla: http://bugzilla.redhat.com/2041365

commit 720c29fca9fb87c473148ff666b75314420cdda6
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Tue Jul 20 09:49:53 2021 +0100

    libbpf: Propagate errors when retrieving enum value for typed data display

    When retrieving the enum value associated with typed data during
    "is data zero?" checking in btf_dump_type_data_check_zero(), the
    return value of btf_dump_get_enum_value() is not passed to the caller
    if the function returns a non-zero (error) value.  Currently, 0
    is returned if the function returns an error.  We should instead
    propagate the error to the caller.

    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1626770993-11073-4-git-send-email-alan.maguire@oracle.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:34 +02:00
Jerome Marchand 6807f81f9e libbpf: Avoid use of __int128 in typed dump display
Bugzilla: http://bugzilla.redhat.com/2041365

commit a1d3cc3c5eca598cfabee3a35f30f34fbe2f709b
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Tue Jul 20 09:49:51 2021 +0100

    libbpf: Avoid use of __int128 in typed dump display

    __int128 is not supported for some 32-bit platforms (arm and i386).
    __int128 was used in carrying out computations on bitfields which
    aid display, but the same calculations could be done with __u64
    with the small effect of not supporting 128-bit bitfields.

    With these changes, a big-endian issue with casting 128-bit integers
    to 64-bit for enum bitfields is solved also, as we now use 64-bit
    integers for bitfield calculations.

    Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org>
    Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1626770993-11073-2-git-send-email-alan.maguire@oracle.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:34 +02:00
Jerome Marchand de17a73190 libbpf: Btf typed dump does not need to allocate dump data
Bugzilla: http://bugzilla.redhat.com/2041365

commit add192f81ab21b58471577c75e7be9c9add98223
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Fri Jul 16 23:46:57 2021 +0100

    libbpf: Btf typed dump does not need to allocate dump data

    By using the stack for this small structure, we avoid the need
    for freeing memory in error paths.

    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1626475617-25984-4-git-send-email-alan.maguire@oracle.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:33 +02:00
Jerome Marchand 48042baae0 libbpf: Fix compilation errors on ppc64le for btf dump typed data
Bugzilla: http://bugzilla.redhat.com/2041365

commit 04eb4dff6a64d842f7f2c85c7cb1affc5ab3ebc9
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Fri Jul 16 23:46:56 2021 +0100

    libbpf: Fix compilation errors on ppc64le for btf dump typed data

    __s64 can be defined as either long or long long, depending on the
    architecture. On ppc64le it's defined as long, giving this error:

     In file included from btf_dump.c:22:
    btf_dump.c: In function 'btf_dump_type_data_check_overflow':
    libbpf_internal.h:111:22: error: format '%lld' expects argument of
    type 'long long int', but argument 3 has type '__s64' {aka 'long int'}
    [-Werror=format=]
      111 |  libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__); \
          |                      ^~~~~~~~~~
    libbpf_internal.h:114:27: note: in expansion of macro '__pr'
      114 | #define pr_warn(fmt, ...) __pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
          |                           ^~~~
    btf_dump.c:1992:3: note: in expansion of macro 'pr_warn'
     1992 |   pr_warn("unexpected size [%lld] for id [%u]\n",
          |   ^~~~~~~
    btf_dump.c:1992:32: note: format string is defined here
     1992 |   pr_warn("unexpected size [%lld] for id [%u]\n",
          |                             ~~~^
          |                                |
          |                                long long int
          |                             %ld

    Cast to size_t and use %zu instead.

    Reported-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1626475617-25984-3-git-send-email-alan.maguire@oracle.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:33 +02:00
Jerome Marchand 1e1759dbe1 libbpf: Clarify/fix unaligned data issues for btf typed dump
Bugzilla: http://bugzilla.redhat.com/2041365

commit 8d44c3578b48d5f605eddcfd6a644e3944455a6b
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Fri Jul 16 23:46:55 2021 +0100

    libbpf: Clarify/fix unaligned data issues for btf typed dump

    If data is packed, data structures can store it outside of usual
    boundaries.  For example a 4-byte int can be stored on a unaligned
    boundary in a case like this:

    struct s {
            char f1;
            int f2;
    } __attribute((packed));

    ...the int is stored at an offset of one byte.  Some platforms have
    problems dereferencing data that is not aligned with its size, and
    code exists to handle most cases of this for BTF typed data display.
    However pointer display was missed, and a simple function to test if
    "ptr_is_aligned(data, data_sz)" would help clarify this code.

    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1626475617-25984-2-git-send-email-alan.maguire@oracle.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:33 +02:00
Jerome Marchand b65e1abf25 libbpf: BTF dumper support for typed data
Bugzilla: http://bugzilla.redhat.com/2041365

commit 920d16af9b42adfc8524d880b0e8dba66a6cb87d
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Thu Jul 15 16:15:24 2021 +0100

    libbpf: BTF dumper support for typed data

    Add a BTF dumper for typed data, so that the user can dump a typed
    version of the data provided.

    The API is

    int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
                                 void *data, size_t data_sz,
                                 const struct btf_dump_type_data_opts *opts);

    ...where the id is the BTF id of the data pointed to by the "void *"
    argument; for example the BTF id of "struct sk_buff" for a
    "struct skb *" data pointer.  Options supported are

     - a starting indent level (indent_lvl)
     - a user-specified indent string which will be printed once per
       indent level; if NULL, tab is chosen but any string <= 32 chars
       can be provided.
     - a set of boolean options to control dump display, similar to those
       used for BPF helper bpf_snprintf_btf().  Options are
            - compact : omit newlines and other indentation
            - skip_names: omit member names
            - emit_zeroes: show zero-value members

    Default output format is identical to that dumped by bpf_snprintf_btf(),
    for example a "struct sk_buff" representation would look like this:

    struct sk_buff){
            (union){
                    (struct){
                            .next = (struct sk_buff *)0xffffffffffffffff,
                            .prev = (struct sk_buff *)0xffffffffffffffff,
                    (union){
                            .dev = (struct net_device *)0xffffffffffffffff,
                            .dev_scratch = (long unsigned int)18446744073709551615,
                    },
            },
    ...

    If the data structure is larger than the *data_sz*
    number of bytes that are available in *data*, as much
    of the data as possible will be dumped and -E2BIG will
    be returned.  This is useful as tracers will sometimes
    not be able to capture all of the data associated with
    a type; for example a "struct task_struct" is ~16k.
    Being able to specify that only a subset is available is
    important for such cases.  On success, the amount of data
    dumped is returned.

    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1626362126-27775-2-git-send-email-alan.maguire@oracle.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:33 +02:00
Andrii Nakryiko e9fc3ce99b libbpf: Streamline error reporting for high-level APIs
Implement changes to error reporting for high-level libbpf APIs to make them
less surprising and less error-prone to users:
  - in all the cases when error happens, errno is set to an appropriate error
    value;
  - in libbpf 1.0 mode, all pointer-returning APIs return NULL on error and
    error code is communicated through errno; this applies both to APIs that
    already returned NULL before (so now they communicate more detailed error
    codes), as well as for many APIs that used ERR_PTR() macro and encoded
    error numbers as fake pointers.
  - in legacy (default) mode, those APIs that were returning ERR_PTR(err),
    continue doing so, but still set errno.

With these changes, errno can be always used to extract actual error,
regardless of legacy or libbpf 1.0 modes. This is utilized internally in
libbpf in places where libbpf uses it's own high-level APIs.
libbpf_get_error() is adapted to handle both cases completely transparently to
end-users (and is used by libbpf consistently as well).

More context, justification, and discussion can be found in "Libbpf: the road
to v1.0" document ([0]).

  [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210525035935.1461796-5-andrii@kernel.org
2021-05-25 17:32:35 -07:00
David S. Miller 241949e488 Merge https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Alexei Starovoitov says:

====================
pull-request: bpf-next 2021-03-24

The following pull-request contains BPF updates for your *net-next* tree.

We've added 37 non-merge commits during the last 15 day(s) which contain
a total of 65 files changed, 3200 insertions(+), 738 deletions(-).

The main changes are:

1) Static linking of multiple BPF ELF files, from Andrii.

2) Move drop error path to devmap for XDP_REDIRECT, from Lorenzo.

3) Spelling fixes from various folks.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
2021-03-25 16:30:46 -07:00
David S. Miller efd13b71a3 Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
Signed-off-by: David S. Miller <davem@davemloft.net>
2021-03-25 15:31:22 -07:00
Jean-Philippe Brucker 901ee1d750 libbpf: Fix BTF dump of pointer-to-array-of-struct
The vmlinux.h generated from BTF is invalid when building
drivers/phy/ti/phy-gmii-sel.c with clang:

vmlinux.h:61702:27: error: array type has incomplete element type ‘struct reg_field’
61702 |  const struct reg_field (*regfields)[3];
      |                           ^~~~~~~~~

bpftool generates a forward declaration for this struct regfield, which
compilers aren't happy about. Here's a simplified reproducer:

	struct inner {
		int val;
	};
	struct outer {
		struct inner (*ptr_to_array)[2];
	} A;

After build with clang -> bpftool btf dump c -> clang/gcc:
./def-clang.h:11:23: error: array has incomplete element type 'struct inner'
        struct inner (*ptr_to_array)[2];

Member ptr_to_array of struct outer is a pointer to an array of struct
inner. In the DWARF generated by clang, struct outer appears before
struct inner, so when converting BTF of struct outer into C, bpftool
issues a forward declaration to struct inner. With GCC the DWARF info is
reversed so struct inner gets fully defined.

That forward declaration is not sufficient when compilers handle an
array of the struct, even when it's only used through a pointer. Note
that we can trigger the same issue with an intermediate typedef:

	struct inner {
	        int val;
	};
	typedef struct inner inner2_t[2];
	struct outer {
	        inner2_t *ptr_to_array;
	} A;

Becomes:

	struct inner;
	typedef struct inner inner2_t[2];

And causes:

./def-clang.h:10:30: error: array has incomplete element type 'struct inner'
	typedef struct inner inner2_t[2];

To fix this, clear through_ptr whenever we encounter an intermediate
array, to make the inner struct part of a strong link and force full
declaration.

Fixes: 351131b51c ("libbpf: add btf_dump API for BTF-to-C conversion")
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210319112554.794552-2-jean-philippe@linaro.org
2021-03-19 14:14:44 -07:00
Andrii Nakryiko 3b029e06f6 libbpf: Rename internal memory-management helpers
Rename btf_add_mem() and btf_ensure_mem() helpers that abstract away details
of dynamically resizable memory to use libbpf_ prefix, as they are not
BTF-specific. No functional changes.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210318194036.3521577-4-andrii@kernel.org
2021-03-18 16:14:22 -07:00
Ilya Leoshkevich 22541a9eeb libbpf: Add BTF_KIND_FLOAT support
The logic follows that of BTF_KIND_INT most of the time. Sanitization
replaces BTF_KIND_FLOATs with equally-sized empty BTF_KIND_STRUCTs on
older kernels, for example, the following:

    [4] FLOAT 'float' size=4

becomes the following:

    [4] STRUCT '(anon)' size=4 vlen=0

With dwarves patch [1] and this patch, the older kernels, which were
failing with the floating-point-related errors, will now start working
correctly.

[1] https://github.com/iii-i/dwarves/commit/btf-kind-float-v2

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210226202256.116518-4-iii@linux.ibm.com
2021-03-04 17:58:15 -08:00
Andrii Nakryiko 9c6c5c48d7 libbpf: Make btf_dump work with modifiable BTF
Ensure that btf_dump can accommodate new BTF types being appended to BTF
instance after struct btf_dump was created. This came up during attemp to
use btf_dump for raw type dumping in selftests, but given changes are not
excessive, it's good to not have any gotchas in API usage, so I decided to
support such use case in general.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200929232843.1249318-2-andriin@fb.com
2020-09-30 12:30:22 -07:00
Andrii Nakryiko 7d9c71e10b libbpf: Extract generic string hashing function for reuse
Calculating a hash of zero-terminated string is a common need when using
hashmap, so extract it for reuse.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200926011357.2366158-5-andriin@fb.com
2020-09-28 17:27:31 -07:00
David S. Miller 150f29f5e6 Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says:

====================
pull-request: bpf-next 2020-09-01

The following pull-request contains BPF updates for your *net-next* tree.

There are two small conflicts when pulling, resolve as follows:

1) Merge conflict in tools/lib/bpf/libbpf.c between 88a8212028 ("libbpf: Factor
   out common ELF operations and improve logging") in bpf-next and 1e891e513e
   ("libbpf: Fix map index used in error message") in net-next. Resolve by taking
   the hunk in bpf-next:

        [...]
        scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
        data = elf_sec_data(obj, scn);
        if (!scn || !data) {
                pr_warn("elf: failed to get %s map definitions for %s\n",
                        MAPS_ELF_SEC, obj->path);
                return -EINVAL;
        }
        [...]

2) Merge conflict in drivers/net/ethernet/mellanox/mlx5/core/en/xsk/rx.c between
   9647c57b11 ("xsk: i40e: ice: ixgbe: mlx5: Test for dma_need_sync earlier for
   better performance") in bpf-next and e20f0dbf20 ("net/mlx5e: RX, Add a prefetch
   command for small L1_CACHE_BYTES") in net-next. Resolve the two locations by retaining
   net_prefetch() and taking xsk_buff_dma_sync_for_cpu() from bpf-next. Should look like:

        [...]
        xdp_set_data_meta_invalid(xdp);
        xsk_buff_dma_sync_for_cpu(xdp, rq->xsk_pool);
        net_prefetch(xdp->data);
        [...]

We've added 133 non-merge commits during the last 14 day(s) which contain
a total of 246 files changed, 13832 insertions(+), 3105 deletions(-).

The main changes are:

1) Initial support for sleepable BPF programs along with bpf_copy_from_user() helper
   for tracing to reliably access user memory, from Alexei Starovoitov.

2) Add BPF infra for writing and parsing TCP header options, from Martin KaFai Lau.

3) bpf_d_path() helper for returning full path for given 'struct path', from Jiri Olsa.

4) AF_XDP support for shared umems between devices and queues, from Magnus Karlsson.

5) Initial prep work for full BPF-to-BPF call support in libbpf, from Andrii Nakryiko.

6) Generalize bpf_sk_storage map & add local storage for inodes, from KP Singh.

7) Implement sockmap/hash updates from BPF context, from Lorenz Bauer.

8) BPF xor verification for scalar types & add BPF link iterator, from Yonghong Song.

9) Use target's prog type for BPF_PROG_TYPE_EXT prog verification, from Udip Pant.

10) Rework BPF tracing samples to use libbpf loader, from Daniel T. Lee.

11) Fix xdpsock sample to really cycle through all buffers, from Weqaar Janjua.

12) Improve type safety for tun/veth XDP frame handling, from Maciej Żenczykowski.

13) Various smaller cleanups and improvements all over the place.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
2020-09-01 13:22:59 -07:00
Andrii Nakryiko 85367030a6 libbpf: Centralize poisoning and poison reallocarray()
Most of libbpf source files already include libbpf_internal.h, so it's a good
place to centralize identifier poisoning. So move kernel integer type
poisoning there. And also add reallocarray to a poison list to prevent
accidental use of it. libbpf_reallocarray() should be used universally
instead.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200819013607.3607269-4-andriin@fb.com
2020-08-18 18:38:25 -07:00
Andrii Nakryiko 029258d7b2 libbpf: Remove any use of reallocarray() in libbpf
Re-implement glibc's reallocarray() for libbpf internal-only use.
reallocarray(), unfortunately, is not available in all versions of glibc, so
requires extra feature detection and using reallocarray() stub from
<tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates build
of libbpf unnecessarily and is just a maintenance burden. Instead, it's
trivial to implement libbpf-specific internal version and use it throughout
libbpf.

Which is what this patch does, along with converting some realloc() uses that
should really have been reallocarray() in the first place.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200819013607.3607269-2-andriin@fb.com
2020-08-18 18:38:25 -07:00
Andrii Nakryiko 3fb1a96a91 libbpf: Fix build on ppc64le architecture
On ppc64le we get the following warning:

  In file included from btf_dump.c:16:0:
  btf_dump.c: In function ‘btf_dump_emit_struct_def’:
  ../include/linux/kernel.h:20:17: error: comparison of distinct pointer types lacks a cast [-Werror]
    (void) (&_max1 == &_max2);  \
                   ^
  btf_dump.c:882:11: note: in expansion of macro ‘max’
      m_sz = max(0LL, btf__resolve_size(d->btf, m->type));
             ^~~

Fix by explicitly casting to __s64, which is a return type from
btf__resolve_size().

Fixes: 702eddc77a ("libbpf: Handle GCC built-in types for Arm NEON")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200818164456.1181661-1-andriin@fb.com
2020-08-18 10:02:52 -07:00
Andrii Nakryiko 44ad23dfbc libbpf: Handle BTF pointer sizes more carefully
With libbpf and BTF it is pretty common to have libbpf built for one
architecture, while BTF information was generated for a different architecture
(typically, but not always, BPF). In such case, the size of a pointer might
differ betweem architectures. libbpf previously was always making an
assumption that pointer size for BTF is the same as native architecture
pointer size, but that breaks for cases where libbpf is built as 32-bit
library, while BTF is for 64-bit architecture.

To solve this, add heuristic to determine pointer size by searching for `long`
or `unsigned long` integer type and using its size as a pointer size. Also,
allow to override the pointer size with a new API btf__set_pointer_size(), for
cases where application knows which pointer size should be used. User
application can check what libbpf "guessed" by looking at the result of
btf__pointer_size(). If it's not 0, then libbpf successfully determined a
pointer size, otherwise native arch pointer size will be used.

For cases where BTF is parsed from ELF file, use ELF's class (32-bit or
64-bit) to determine pointer size.

Fixes: 8a138aed4a ("bpf: btf: Add BTF support to libbpf")
Fixes: 351131b51c ("libbpf: add btf_dump API for BTF-to-C conversion")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200813204945.1020225-5-andriin@fb.com
2020-08-13 16:45:41 -07:00
Jean-Philippe Brucker 702eddc77a libbpf: Handle GCC built-in types for Arm NEON
When building Arm NEON (SIMD) code from lib/raid6/neon.uc, GCC emits
DWARF information using a base type "__Poly8_t", which is internal to
GCC and not recognized by Clang. This causes build failures when
building with Clang a vmlinux.h generated from an arm64 kernel that was
built with GCC.

	vmlinux.h:47284:9: error: unknown type name '__Poly8_t'
	typedef __Poly8_t poly8x16_t[16];
	        ^~~~~~~~~

The polyX_t types are defined as unsigned integers in the "Arm C
Language Extension" document (101028_Q220_00_en). Emit typedefs based on
standard integer types for the GCC internal types, similar to those
emitted by Clang.

Including linux/kernel.h to use ARRAY_SIZE() incidentally redefined
max(), causing a build bug due to different types, hence the seemingly
unrelated change.

Reported-by: Jakov Petrina <jakov.petrina@sartura.hr>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200812143909.3293280-1-jean-philippe@linaro.org
2020-08-12 18:11:51 -07:00
Andrii Nakryiko 50450fc716 libbpf: Make destructors more robust by handling ERR_PTR(err) cases
Most of libbpf "constructors" on failure return ERR_PTR(err) result encoded as
a pointer. It's a common mistake to eventually pass such malformed pointers
into xxx__destroy()/xxx__free() "destructors". So instead of fixing up
clean up code in selftests and user programs, handle such error pointers in
destructors themselves. This works beautifully for NULL pointers passed to
destructors, so might as well just work for error pointers.

Suggested-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20200729232148.896125-1-andriin@fb.com
2020-07-31 00:53:07 +02:00
Andrii Nakryiko 7c819e7013 libbpf: Support stripping modifiers for btf_dump
One important use case when emitting const/volatile/restrict is undesirable is
BPF skeleton generation of DATASEC layout. These are further memory-mapped and
can be written/read from user-space directly.

For important case of .rodata variables, bpftool strips away first-level
modifiers, to make their use on user-space side simple and not requiring extra
type casts to override compiler complaining about writing to const variables.

This logic works mostly fine, but breaks in some more complicated cases. E.g.:

    const volatile int params[10];

Because in BTF it's a chain of ARRAY -> CONST -> VOLATILE -> INT, bpftool
stops at ARRAY and doesn't strip CONST and VOLATILE. In skeleton this variable
will be emitted as is. So when used from user-space, compiler will complain
about writing to const array. This is problematic, as also mentioned in [0].

To solve this for arrays and other non-trivial cases (e.g., inner
const/volatile fields inside the struct), teach btf_dump to strip away any
modifier, when requested. This is done as an extra option on
btf_dump__emit_type_decl() API.

Reported-by: Anton Protopopov <a.s.protopopov@gmail.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200713232409.3062144-2-andriin@fb.com
2020-07-13 17:07:43 -07:00
Andrii Nakryiko 32022fd97e libbpf: Handle GCC noreturn-turned-volatile quirk
Handle a GCC quirk of emitting extra volatile modifier in DWARF (and
subsequently preserved in BTF by pahole) for function pointers marked as
__attribute__((noreturn)). This was the way to mark such functions before GCC
2.5 added noreturn attribute. Drop such func_proto modifiers, similarly to how
it's done for array (also to handle GCC quirk/bug).

Such volatile attribute is emitted by GCC only, so existing selftests can't
express such test. Simple repro is like this (compiled with GCC + BTF
generated by pahole):

  struct my_struct {
      void __attribute__((noreturn)) (*fn)(int);
  };
  struct my_struct a;

Without this fix, output will be:

struct my_struct {
    voidvolatile  (*fn)(int);
};

With the fix:

struct my_struct {
    void (*fn)(int);
};

Fixes: 351131b51c ("libbpf: add btf_dump API for BTF-to-C conversion")
Reported-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Link: https://lore.kernel.org/bpf/20200610052335.2862559-1-andriin@fb.com
2020-06-10 13:37:02 +02:00
Zou Wei a6bbdf2e75 libbpf: Remove unneeded semicolon in btf_dump_emit_type
Fixes the following coccicheck warning:

 tools/lib/bpf/btf_dump.c:661:4-5: Unneeded semicolon

Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Zou Wei <zou_wei@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/1588064829-70613-1-git-send-email-zou_wei@huawei.com
2020-04-28 21:47:47 +02:00
Andrii Nakryiko 7cb30aaab3 libbpf: Assume unsigned values for BTF_KIND_ENUM
Currently, BTF_KIND_ENUM type doesn't record whether enum values should be
interpreted as signed or unsigned. In Linux, most enums are unsigned, though,
so interpreting them as unsigned matches real world better.

Change btf_dump test case to test maximum 32-bit value, instead of negative
value.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200303003233.3496043-3-andriin@fb.com
2020-03-04 17:00:06 +01:00
Andrii Nakryiko 320a36063e libbpf: Fix handling of optional field_name in btf_dump__emit_type_decl
Internal functions, used by btf_dump__emit_type_decl(), assume field_name is
never going to be NULL. Ensure it's always the case.

Fixes: 9f81654eeb ("libbpf: Expose BTF-to-C type declaration emitting API")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200303180800.3303471-1-andriin@fb.com
2020-03-03 16:41:36 -08:00
Andrii Nakryiko bc0eb9a333 libbpf: Fix error handling bug in btf_dump__new
Fix missing jump to error handling in btf_dump__new, found by Coverity static
code analysis.

Fixes: 9f81654eeb ("libbpf: Expose BTF-to-C type declaration emitting API")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200117060801.1311525-2-andriin@fb.com
2020-01-17 08:33:17 -08:00
Andrii Nakryiko 1d1a3bcffe libbpf: Poison kernel-only integer types
It's been a recurring issue with types like u32 slipping into libbpf source
code accidentally. This is not detected during builds inside kernel source
tree, but becomes a compilation error in libbpf's Github repo. Libbpf is
supposed to use only __{s,u}{8,16,32,64} typedefs, so poison {s,u}{8,16,32,64}
explicitly in every .c file. Doing that in a bit more centralized way, e.g.,
inside libbpf_internal.h breaks selftests, which are both using kernel u32 and
libbpf_internal.h.

This patch also fixes a new u32 occurence in libbpf.c, added recently.

Fixes: 590a008882 ("bpf: libbpf: Add STRUCT_OPS support")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200110181916.271446-1-andriin@fb.com
2020-01-10 10:38:00 -08:00
Andrii Nakryiko 9f81654eeb libbpf: Expose BTF-to-C type declaration emitting API
Expose API that allows to emit type declaration and field/variable definition
(if optional field name is specified) in valid C syntax for any provided BTF
type. This is going to be used by bpftool when emitting data section layout as
a struct. As part of making this API useful in a stand-alone fashion, move
initialization of some of the internal btf_dump state to earlier phase.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191214014341.3442258-8-andriin@fb.com
2019-12-15 15:58:05 -08:00
Andrii Nakryiko 3d208f4ca1 libbpf: Expose btf__align_of() API
Expose BTF API that calculates type alignment requirements.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191214014341.3442258-7-andriin@fb.com
2019-12-15 15:58:05 -08:00
Kefeng Wang be18010ea2 tools, bpf: Rename pr_warning to pr_warn to align with kernel logging
For kernel logging macros, pr_warning() is completely removed and
replaced by pr_warn(). By using pr_warn() in tools/lib/bpf/ for
symmetry to kernel logging macros, we could eventually drop the
use of pr_warning() in the whole kernel tree.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191021055532.185245-1-wangkefeng.wang@huawei.com
2019-10-21 14:38:41 +02:00
Andrii Nakryiko e78dcbf414 libbpf: Handle invalid typedef emitted by old GCC
Old GCC versions are producing invalid typedef for __gnuc_va_list
pointing to void. Special-case this and emit valid:

typedef __builtin_va_list __gnuc_va_list;

Reported-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20191011032901.452042-1-andriin@fb.com
2019-10-11 23:11:55 +02:00
Andrii Nakryiko b4099769f3 libbpf: Fix struct end padding in btf_dump
Fix a case where explicit padding at the end of a struct is necessary
due to non-standart alignment requirements of fields (which BTF doesn't
capture explicitly).

Fixes: 351131b51c ("libbpf: add btf_dump API for BTF-to-C conversion")
Reported-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Tested-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20191008231009.2991130-2-andriin@fb.com
2019-10-09 15:38:36 -07:00
Andrii Nakryiko 39529a9948 libbpf: Teach btf_dumper to emit stand-alone anonymous enum definitions
BTF-to-C converter previously skipped anonymous enums in an assumption
that those are embedded in struct's field definitions. This is not
always the case and a lot of kernel constants are defined as part of
anonymous enums. This change fixes the logic by eagerly marking all
types as either referenced by any other type or not. This is enough to
distinguish two classes of anonymous enums and emit previously omitted
enum definitions.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20190925203745.3173184-1-andriin@fb.com
2019-09-26 14:38:29 +02:00
Andrii Nakryiko aef70a1f44 libbpf: fix false uninitialized variable warning
Some compilers emit warning for potential uninitialized next_id usage.
The code is correct, but control flow is too complicated for some
compilers to figure this out. Re-initialize next_id to satisfy
compiler.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-09-25 22:15:02 +02:00