Skip to content
  1. May 11, 2022
  2. May 10, 2022
  3. May 09, 2022
    • Andrii Nakryiko's avatar
      selftests/bpf: Test libbpf's ringbuf size fix up logic · 7b3a0638
      Andrii Nakryiko authored
      
      
      Make sure we always excercise libbpf's ringbuf map size adjustment logic
      by specifying non-zero size that's definitely not a page size multiple.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-10-andrii@kernel.org
      7b3a0638
    • Andrii Nakryiko's avatar
      libbpf: Automatically fix up BPF_MAP_TYPE_RINGBUF size, if necessary · 0087a681
      Andrii Nakryiko authored
      
      
      Kernel imposes a pretty particular restriction on ringbuf map size. It
      has to be a power-of-2 multiple of page size. While generally this isn't
      hard for user to satisfy, sometimes it's impossible to do this
      declaratively in BPF source code or just plain inconvenient to do at
      runtime.
      
      One such example might be BPF libraries that are supposed to work on
      different architectures, which might not agree on what the common page
      size is.
      
      Let libbpf find the right size for user instead, if it turns out to not
      satisfy kernel requirements. If user didn't set size at all, that's most
      probably a mistake so don't upsize such zero size to one full page,
      though. Also we need to be careful about not overflowing __u32
      max_entries.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-9-andrii@kernel.org
      0087a681
    • Andrii Nakryiko's avatar
      libbpf: Provide barrier() and barrier_var() in bpf_helpers.h · f760d053
      Andrii Nakryiko authored
      
      
      Add barrier() and barrier_var() macros into bpf_helpers.h to be used by
      end users. While a bit advanced and specialized instruments, they are
      sometimes indispensable. Instead of requiring each user to figure out
      exact asm volatile incantations for themselves, provide them from
      bpf_helpers.h.
      
      Also remove conflicting definitions from selftests. Some tests rely on
      barrier_var() definition being nothing, those will still work as libbpf
      does the #ifndef/#endif guarding for barrier() and barrier_var(),
      allowing users to redefine them, if necessary.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-8-andrii@kernel.org
      f760d053
    • Andrii Nakryiko's avatar
      selftests/bpf: Add bpf_core_field_offset() tests · 785c3342
      Andrii Nakryiko authored
      
      
      Add test cases for bpf_core_field_offset() helper.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-7-andrii@kernel.org
      785c3342
    • Andrii Nakryiko's avatar
      libbpf: Complete field-based CO-RE helpers with field offset helper · 7715f549
      Andrii Nakryiko authored
      
      
      Add bpf_core_field_offset() helper to complete field-based CO-RE
      helpers. This helper can be useful for feature-detection and for some
      more advanced cases of field reading (e.g., reading flexible array members).
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-6-andrii@kernel.org
      7715f549
    • Andrii Nakryiko's avatar
      selftests/bpf: Use both syntaxes for field-based CO-RE helpers · 2a4ca46b
      Andrii Nakryiko authored
      
      
      Excercise both supported forms of bpf_core_field_exists() and
      bpf_core_field_size() helpers: variable-based field reference and
      type/field name-based one.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-5-andrii@kernel.org
      2a4ca46b
    • Andrii Nakryiko's avatar
      libbpf: Improve usability of field-based CO-RE helpers · 73d0280f
      Andrii Nakryiko authored
      
      
      Allow to specify field reference in two ways:
      
        - if user has variable of necessary type, they can use variable-based
          reference (my_var.my_field or my_var_ptr->my_field). This was the
          only supported syntax up till now.
        - now, bpf_core_field_exists() and bpf_core_field_size() support also
          specifying field in a fashion similar to offsetof() macro, by
          specifying type of the containing struct/union separately and field
          name separately: bpf_core_field_exists(struct my_type, my_field).
          This forms is quite often more convenient in practice and it matches
          type-based CO-RE helpers that support specifying type by its name
          without requiring any variables.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-4-andrii@kernel.org
      73d0280f
    • Andrii Nakryiko's avatar
      libbpf: Make __kptr and __kptr_ref unconditionally use btf_type_tag() attr · 8e2f618e
      Andrii Nakryiko authored
      It will be annoying and surprising for users of __kptr and __kptr_ref if
      libbpf silently ignores them just because Clang used for compilation
      didn't support btf_type_tag(). It's much better to get clear compiler
      error than debug BPF verifier failures later on.
      
      Fixes: ef89654f
      
       ("libbpf: Add kptr type tag macros to bpf_helpers.h")
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-3-andrii@kernel.org
      8e2f618e
    • Andrii Nakryiko's avatar
      selftests/bpf: Prevent skeleton generation race · 1e2666e0
      Andrii Nakryiko authored
      Prevent "classic" and light skeleton generation rules from stomping on
      each other's toes due to the use of the same <obj>.linked{1,2,3}.o
      naming pattern. There is no coordination and synchronizataion between
      .skel.h and .lskel.h rules, so they can easily overwrite each other's
      intermediate object files, leading to errors like:
      
        /bin/sh: line 1: 170928 Bus error               (core dumped)
        /data/users/andriin/linux/tools/testing/selftests/bpf/tools/sbin/bpftool gen skeleton
        /data/users/andriin/linux/tools/testing/selftests/bpf/test_ksyms_weak.linked3.o
        name test_ksyms_weak
        > /data/users/andriin/linux/tools/testing/selftests/bpf/test_ksyms_weak.skel.h
        make: *** [Makefile:507: /data/users/andriin/linux/tools/testing/selftests/bpf/test_ksyms_weak.skel.h] Error 135
        make: *** Deleting file '/data/users/andriin/linux/tools/testing/selftests/bpf/test_ksyms_weak.skel.h'
      
      Fix by using different suffix for light skeleton rule.
      
      Fixes: c48e51c8
      
       ("bpf: selftests: Add selftests for module kfunc support")
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220509004148.1801791-2-andrii@kernel.org
      1e2666e0
  4. Apr 29, 2022
    • Mykola Lysenko's avatar
      selftests/bpf: Fix two memory leaks in prog_tests · 20b87e7c
      Mykola Lysenko authored
      
      
      Fix log_fp memory leak in dispatch_thread_read_log.
      Remove obsolete log_fp clean-up code in dispatch_thread.
      
      Also, release memory of subtest_selector. This can be
      reproduced with -n 2/1 parameters.
      
      Signed-off-by: default avatarMykola Lysenko <mykolal@fb.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220428225744.1961643-1-mykolal@fb.com
      20b87e7c
    • Alexei Starovoitov's avatar
      Merge branch 'libbpf: allow to opt-out from BPF map creation' · a2c70dbc
      Alexei Starovoitov authored
      
      
      Andrii Nakryiko says:
      
      ====================
      
      Add bpf_map__set_autocreate() API which is a BPF map counterpart of
      bpf_program__set_autoload() and serves similar goal of allowing to build more
      flexible CO-RE applications. See patch #3 for example scenarios in which the
      need for such API came up previously.
      
      Patch #1 is a follow-up patch to previous patch set adding verifier log fixup
      logic, making sure bpf_core_format_spec()'s return result is used for
      something useful.
      
      Patch #2 is a small refactoring to avoid unnecessary verbose memory management
      around obj->maps array.
      
      Patch #3 adds and API and corresponding BPF verifier log fix up logic to
      provide human-comprehensible error message with useful details.
      
      Patch #4 adds a simple selftest validating both the API itself and libbpf's
      log fixup logic for it.
      ====================
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      a2c70dbc
    • Andrii Nakryiko's avatar
      selftests/bpf: Test bpf_map__set_autocreate() and related log fixup logic · 68964e15
      Andrii Nakryiko authored
      
      
      Add a subtest that excercises bpf_map__set_autocreate() API and
      validates that libbpf properly fixes up BPF verifier log with correct
      map information.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220428041523.4089853-5-andrii@kernel.org
      68964e15