1. 14 Dec, 2021 3 commits
    • Andrii Nakryiko's avatar
      libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF · e542f2c4
      Andrii Nakryiko authored
      The need to increase RLIMIT_MEMLOCK to do anything useful with BPF is
      one of the first extremely frustrating gotchas that all new BPF users go
      through and in some cases have to learn it a very hard way.
      
      Luckily, starting with upstream Linux kernel version 5.11, BPF subsystem
      dropped the dependency on memlock and uses memcg-based memory accounting
      instead. Unfortunately, detecting memcg-based BPF memory accounting is
      far from trivial (as can be evidenced by this patch), so in practice
      most BPF applications still do unconditional RLIMIT_MEMLOCK increase.
      
      As we move towards libbpf 1.0, it would be good to allow users to forget
      about RLIMIT_MEMLOCK vs memcg and let libbpf do the sensible adjustment
      automatically. This patch paves the way forward in this matter. Libbpf
      will do feature detection of memcg-based accounting, and if detected,
      will do nothing. But if the kernel is too old, just like BCC, libbpf
      will automatically increase RLIMIT_MEMLOCK on behalf of user
      application ([0]).
      
      As this is technically a breaking change, during the transition period
      applications have to opt into libbpf 1.0 mode by setting
      LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK bit when calling
      libbpf_set_strict_mode().
      
      Libbpf allows to control the exact amount of set RLIMIT_MEMLOCK limit
      with libbpf_set_memlock_rlim_max() API. Passing 0 will make libbpf do
      nothing with RLIMIT_MEMLOCK. libbpf_set_memlock_rlim_max() has to be
      called before the first bpf_prog_load(), bpf_btf_load(), or
      bpf_object__load() call, otherwise it has no effect and will return
      -EBUSY.
      
        [0] Closes: https://github.com/libbpf/libbpf/issues/369Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211214195904.1785155-2-andrii@kernel.org
      e542f2c4
    • Andrii Nakryiko's avatar
      libbpf: Add sane strncpy alternative and use it internally · 9fc205b4
      Andrii Nakryiko authored
      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: 2f6324a3 (libbpf: Support shared umems between queues and devices)
      Reported-by: default avatarEmmanuel Deloget <emmanuel.deloget@eho.link>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211211004043.2374068-1-andrii@kernel.org
      9fc205b4
    • Andrii Nakryiko's avatar
      libbpf: Fix potential uninit memory read · 4581e676
      Andrii Nakryiko authored
      In case of BPF_CORE_TYPE_ID_LOCAL we fill out target result explicitly.
      But targ_res itself isn't initialized in such a case, and subsequent
      call to bpf_core_patch_insn() might read uninitialized field (like
      fail_memsz_adjust in this case). So ensure that targ_res is
      zero-initialized for BPF_CORE_TYPE_ID_LOCAL case.
      
      This was reported by Coverity static analyzer.
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211214010032.3843804-1-andrii@kernel.org
      4581e676
  2. 13 Dec, 2021 13 commits
  3. 12 Dec, 2021 8 commits
  4. 11 Dec, 2021 13 commits
  5. 10 Dec, 2021 3 commits
    • Jakub Kicinski's avatar
      Merge https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next · be315829
      Jakub Kicinski authored
      Andrii Nakryiko says:
      
      ====================
      bpf-next 2021-12-10 v2
      
      We've added 115 non-merge commits during the last 26 day(s) which contain
      a total of 182 files changed, 5747 insertions(+), 2564 deletions(-).
      
      The main changes are:
      
      1) Various samples fixes, from Alexander Lobakin.
      
      2) BPF CO-RE support in kernel and light skeleton, from Alexei Starovoitov.
      
      3) A batch of new unified APIs for libbpf, logging improvements, version
         querying, etc. Also a batch of old deprecations for old APIs and various
         bug fixes, in preparation for libbpf 1.0, from Andrii Nakryiko.
      
      4) BPF documentation reorganization and improvements, from Christoph Hellwig
         and Dave Tucker.
      
      5) Support for declarative initialization of BPF_MAP_TYPE_PROG_ARRAY in
         libbpf, from Hengqi Chen.
      
      6) Verifier log fixes, from Hou Tao.
      
      7) Runtime-bounded loops support with bpf_loop() helper, from Joanne Koong.
      
      8) Extend branch record capturing to all platforms that support it,
         from Kajol Jain.
      
      9) Light skeleton codegen improvements, from Kumar Kartikeya Dwivedi.
      
      10) bpftool doc-generating script improvements, from Quentin Monnet.
      
      11) Two libbpf v0.6 bug fixes, from Shuyi Cheng and Vincent Minet.
      
      12) Deprecation warning fix for perf/bpf_counter, from Song Liu.
      
      13) MAX_TAIL_CALL_CNT unification and MIPS build fix for libbpf,
          from Tiezhu Yang.
      
      14) BTF_KING_TYPE_TAG follow-up fixes, from Yonghong Song.
      
      15) Selftests fixes and improvements, from Ilya Leoshkevich, Jean-Philippe
          Brucker, Jiri Olsa, Maxim Mikityanskiy, Tirthendu Sarkar, Yucong Sun,
          and others.
      
      * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: (115 commits)
        libbpf: Add "bool skipped" to struct bpf_map
        libbpf: Fix typo in btf__dedup@LIBBPF_0.0.2 definition
        bpftool: Switch bpf_object__load_xattr() to bpf_object__load()
        selftests/bpf: Remove the only use of deprecated bpf_object__load_xattr()
        selftests/bpf: Add test for libbpf's custom log_buf behavior
        selftests/bpf: Replace all uses of bpf_load_btf() with bpf_btf_load()
        libbpf: Deprecate bpf_object__load_xattr()
        libbpf: Add per-program log buffer setter and getter
        libbpf: Preserve kernel error code and remove kprobe prog type guessing
        libbpf: Improve logging around BPF program loading
        libbpf: Allow passing user log setting through bpf_object_open_opts
        libbpf: Allow passing preallocated log_buf when loading BTF into kernel
        libbpf: Add OPTS-based bpf_btf_load() API
        libbpf: Fix bpf_prog_load() log_buf logic for log_level 0
        samples/bpf: Remove unneeded variable
        bpf: Remove redundant assignment to pointer t
        selftests/bpf: Fix a compilation warning
        perf/bpf_counter: Use bpf_map_create instead of bpf_create_map
        samples: bpf: Fix 'unknown warning group' build warning on Clang
        samples: bpf: Fix xdp_sample_user.o linking with Clang
        ...
      ====================
      
      Link: https://lore.kernel.org/r/20211210234746.2100561-1-andrii@kernel.orgSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
      be315829
    • Shuyi Cheng's avatar
      libbpf: Add "bool skipped" to struct bpf_map · 229fae38
      Shuyi Cheng authored
      Fix error: "failed to pin map: Bad file descriptor, path:
      /sys/fs/bpf/_rodata_str1_1."
      
      In the old kernel, the global data map will not be created, see [0]. So
      we should skip the pinning of the global data map to avoid
      bpf_object__pin_maps returning error. Therefore, when the map is not
      created, we mark “map->skipped" as true and then check during relocation
      and during pinning.
      
      Fixes: 16e0c35c ("libbpf: Load global data maps lazily on legacy kernels")
      Signed-off-by: default avatarShuyi Cheng <chengshuyi@linux.alibaba.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      229fae38
    • Vincent Minet's avatar
      libbpf: Fix typo in btf__dedup@LIBBPF_0.0.2 definition · b69c5c07
      Vincent Minet authored
      The btf__dedup_deprecated name was misspelled in the definition of the
      compat symbol for btf__dedup. This leads it to be missing from the
      shared library.
      
      This fixes it.
      
      Fixes: 957d350a ("libbpf: Turn btf_dedup_opts into OPTS-based struct")
      Signed-off-by: default avatarVincent Minet <vincent@vincent-minet.net>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211210063112.80047-1-vincent@vincent-minet.net
      b69c5c07