1. 29 Nov, 2021 1 commit
    • Hengqi Chen's avatar
      libbpf: Support static initialization of BPF_MAP_TYPE_PROG_ARRAY · 341ac5ff
      Hengqi Chen authored
      Support static initialization of BPF_MAP_TYPE_PROG_ARRAY with a
      syntax similar to map-in-map initialization ([0]):
      
          SEC("socket")
          int tailcall_1(void *ctx)
          {
              return 0;
          }
      
          struct {
              __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
              __uint(max_entries, 2);
              __uint(key_size, sizeof(__u32));
              __array(values, int (void *));
          } prog_array_init SEC(".maps") = {
              .values = {
                  [1] = (void *)&tailcall_1,
              },
          };
      
      Here's the relevant part of libbpf debug log showing what's
      going on with prog-array initialization:
      
      libbpf: sec '.relsocket': collecting relocation for section(3) 'socket'
      libbpf: sec '.relsocket': relo #0: insn #2 against 'prog_array_init'
      libbpf: prog 'entry': found map 0 (prog_array_init, sec 4, off 0) for insn #0
      libbpf: .maps relo #0: for 3 value 0 rel->r_offset 32 name 53 ('tailcall_1')
      libbpf: .maps relo #0: map 'prog_array_init' slot [1] points to prog 'tailcall_1'
      libbpf: map 'prog_array_init': created successfully, fd=5
      libbpf: map 'prog_array_init': slot [1] set to prog 'tailcall_1' fd=6
      
        [0] Closes: https://github.com/libbpf/libbpf/issues/354Signed-off-by: default avatarHengqi Chen <hengqi.chen@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211128141633.502339-2-hengqi.chen@gmail.com
      341ac5ff
  2. 26 Nov, 2021 1 commit
    • Tiezhu Yang's avatar
      bpf, mips: Fix build errors about __NR_bpf undeclared · e32cb12f
      Tiezhu Yang authored
      Add the __NR_bpf definitions to fix the following build errors for mips:
      
        $ cd tools/bpf/bpftool
        $ make
        [...]
        bpf.c:54:4: error: #error __NR_bpf not defined. libbpf does not support your arch.
         #  error __NR_bpf not defined. libbpf does not support your arch.
            ^~~~~
        bpf.c: In function ‘sys_bpf’:
        bpf.c:66:17: error: ‘__NR_bpf’ undeclared (first use in this function); did you mean ‘__NR_brk’?
          return syscall(__NR_bpf, cmd, attr, size);
                         ^~~~~~~~
                         __NR_brk
        [...]
        In file included from gen_loader.c:15:0:
        skel_internal.h: In function ‘skel_sys_bpf’:
        skel_internal.h:53:17: error: ‘__NR_bpf’ undeclared (first use in this function); did you mean ‘__NR_brk’?
          return syscall(__NR_bpf, cmd, attr, size);
                         ^~~~~~~~
                         __NR_brk
      
      We can see the following generated definitions:
      
        $ grep -r "#define __NR_bpf" arch/mips
        arch/mips/include/generated/uapi/asm/unistd_o32.h:#define __NR_bpf (__NR_Linux + 355)
        arch/mips/include/generated/uapi/asm/unistd_n64.h:#define __NR_bpf (__NR_Linux + 315)
        arch/mips/include/generated/uapi/asm/unistd_n32.h:#define __NR_bpf (__NR_Linux + 319)
      
      The __NR_Linux is defined in arch/mips/include/uapi/asm/unistd.h:
      
        $ grep -r "#define __NR_Linux" arch/mips
        arch/mips/include/uapi/asm/unistd.h:#define __NR_Linux	4000
        arch/mips/include/uapi/asm/unistd.h:#define __NR_Linux	5000
        arch/mips/include/uapi/asm/unistd.h:#define __NR_Linux	6000
      
      That is to say, __NR_bpf is:
      
        4000 + 355 = 4355 for mips o32,
        6000 + 319 = 6319 for mips n32,
        5000 + 315 = 5315 for mips n64.
      
      So use the GCC pre-defined macro _ABIO32, _ABIN32 and _ABI64 [1] to define
      the corresponding __NR_bpf.
      
      This patch is similar with commit bad1926d ("bpf, s390: fix build for
      libbpf and selftest suite").
      
        [1] https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/config/mips/mips.h#l549Signed-off-by: default avatarTiezhu Yang <yangtiezhu@loongson.cn>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/1637804167-8323-1-git-send-email-yangtiezhu@loongson.cn
      e32cb12f
  3. 25 Nov, 2021 18 commits
  4. 23 Nov, 2021 1 commit
  5. 19 Nov, 2021 4 commits
  6. 18 Nov, 2021 1 commit
  7. 17 Nov, 2021 7 commits
  8. 16 Nov, 2021 7 commits
    • Andrii Nakryiko's avatar
      selftests/bpf: Add uprobe triggering overhead benchmarks · d41bc48b
      Andrii Nakryiko authored
      Add benchmark to measure overhead of uprobes and uretprobes. Also have
      a baseline (no uprobe attached) benchmark.
      
      On my dev machine, baseline benchmark can trigger 130M user_target()
      invocations. When uprobe is attached, this falls to just 700K. With
      uretprobe, we get down to 520K:
      
        $ sudo ./bench trig-uprobe-base -a
        Summary: hits  131.289 ± 2.872M/s
      
        # UPROBE
        $ sudo ./bench -a trig-uprobe-without-nop
        Summary: hits    0.729 ± 0.007M/s
      
        $ sudo ./bench -a trig-uprobe-with-nop
        Summary: hits    1.798 ± 0.017M/s
      
        # URETPROBE
        $ sudo ./bench -a trig-uretprobe-without-nop
        Summary: hits    0.508 ± 0.012M/s
      
        $ sudo ./bench -a trig-uretprobe-with-nop
        Summary: hits    0.883 ± 0.008M/s
      
      So there is almost 2.5x performance difference between probing nop vs
      non-nop instruction for entry uprobe. And 1.7x difference for uretprobe.
      
      This means that non-nop uprobe overhead is around 1.4 microseconds for uprobe
      and 2 microseconds for non-nop uretprobe.
      
      For nop variants, uprobe and uretprobe overhead is down to 0.556 and
      1.13 microseconds, respectively.
      
      For comparison, just doing a very low-overhead syscall (with no BPF
      programs attached anywhere) gives:
      
        $ sudo ./bench trig-base -a
        Summary: hits    4.830 ± 0.036M/s
      
      So uprobes are about 2.67x slower than pure context switch.
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211116013041.4072571-1-andrii@kernel.org
      d41bc48b
    • Tiezhu Yang's avatar
      bpf: Change value of MAX_TAIL_CALL_CNT from 32 to 33 · ebf7f6f0
      Tiezhu Yang authored
      In the current code, the actual max tail call count is 33 which is greater
      than MAX_TAIL_CALL_CNT (defined as 32). The actual limit is not consistent
      with the meaning of MAX_TAIL_CALL_CNT and thus confusing at first glance.
      We can see the historical evolution from commit 04fd61ab ("bpf: allow
      bpf programs to tail-call other bpf programs") and commit f9dabe01
      ("bpf: Undo off-by-one in interpreter tail call count limit"). In order
      to avoid changing existing behavior, the actual limit is 33 now, this is
      reasonable.
      
      After commit 874be05f ("bpf, tests: Add tail call test suite"), we can
      see there exists failed testcase.
      
      On all archs when CONFIG_BPF_JIT_ALWAYS_ON is not set:
       # echo 0 > /proc/sys/net/core/bpf_jit_enable
       # modprobe test_bpf
       # dmesg | grep -w FAIL
       Tail call error path, max count reached jited:0 ret 34 != 33 FAIL
      
      On some archs:
       # echo 1 > /proc/sys/net/core/bpf_jit_enable
       # modprobe test_bpf
       # dmesg | grep -w FAIL
       Tail call error path, max count reached jited:1 ret 34 != 33 FAIL
      
      Although the above failed testcase has been fixed in commit 18935a72
      ("bpf/tests: Fix error in tail call limit tests"), it would still be good
      to change the value of MAX_TAIL_CALL_CNT from 32 to 33 to make the code
      more readable.
      
      The 32-bit x86 JIT was using a limit of 32, just fix the wrong comments and
      limit to 33 tail calls as the constant MAX_TAIL_CALL_CNT updated. For the
      mips64 JIT, use "ori" instead of "addiu" as suggested by Johan Almbladh.
      For the riscv JIT, use RV_REG_TCC directly to save one register move as
      suggested by Björn Töpel. For the other implementations, no function changes,
      it does not change the current limit 33, the new value of MAX_TAIL_CALL_CNT
      can reflect the actual max tail call count, the related tail call testcases
      in test_bpf module and selftests can work well for the interpreter and the
      JIT.
      
      Here are the test results on x86_64:
      
       # uname -m
       x86_64
       # echo 0 > /proc/sys/net/core/bpf_jit_enable
       # modprobe test_bpf test_suite=test_tail_calls
       # dmesg | tail -1
       test_bpf: test_tail_calls: Summary: 8 PASSED, 0 FAILED, [0/8 JIT'ed]
       # rmmod test_bpf
       # echo 1 > /proc/sys/net/core/bpf_jit_enable
       # modprobe test_bpf test_suite=test_tail_calls
       # dmesg | tail -1
       test_bpf: test_tail_calls: Summary: 8 PASSED, 0 FAILED, [8/8 JIT'ed]
       # rmmod test_bpf
       # ./test_progs -t tailcalls
       #142 tailcalls:OK
       Summary: 1/11 PASSED, 0 SKIPPED, 0 FAILED
      Signed-off-by: default avatarTiezhu Yang <yangtiezhu@loongson.cn>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Tested-by: default avatarJohan Almbladh <johan.almbladh@anyfinetworks.com>
      Tested-by: default avatarIlya Leoshkevich <iii@linux.ibm.com>
      Acked-by: default avatarBjörn Töpel <bjorn@kernel.org>
      Acked-by: default avatarJohan Almbladh <johan.almbladh@anyfinetworks.com>
      Acked-by: default avatarIlya Leoshkevich <iii@linux.ibm.com>
      Link: https://lore.kernel.org/bpf/1636075800-3264-1-git-send-email-yangtiezhu@loongson.cn
      ebf7f6f0
    • Quentin Monnet's avatar
      selftests/bpf: Configure dir paths via env in test_bpftool_synctypes.py · e12cd158
      Quentin Monnet authored
      Script test_bpftool_synctypes.py parses a number of files in the bpftool
      directory (or even elsewhere in the repo) to make sure that the list of
      types or options in those different files are consistent. Instead of
      having fixed paths, let's make the directories configurable through
      environment variable. This should make easier in the future to run the
      script in a different setup, for example on an out-of-tree bpftool
      mirror with a different layout.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211115225844.33943-4-quentin@isovalent.com
      e12cd158
    • Quentin Monnet's avatar
      bpftool: Update doc (use susbtitutions) and test_bpftool_synctypes.py · b6231815
      Quentin Monnet authored
      test_bpftool_synctypes.py helps detecting inconsistencies in bpftool
      between the different list of types and options scattered in the
      sources, the documentation, and the bash completion. For options that
      apply to all bpftool commands, the script had a hardcoded list of
      values, and would use them to check whether the man pages are
      up-to-date. When writing the script, it felt acceptable to have this
      list in order to avoid to open and parse bpftool's main.h every time,
      and because the list of global options in bpftool doesn't change so
      often.
      
      However, this is prone to omissions, and we recently added a new
      -l|--legacy option which was described in common_options.rst, but not
      listed in the options summary of each manual page. The script did not
      complain, because it keeps comparing the hardcoded list to the (now)
      outdated list in the header file.
      
      To address the issue, this commit brings the following changes:
      
      - Options that are common to all bpftool commands (--json, --pretty, and
        --debug) are moved to a dedicated file, and used in the definition of
        a RST substitution. This substitution is used in the sources of all
        the man pages.
      
      - This list of common options is updated, with the addition of the new
        -l|--legacy option.
      
      - The script test_bpftool_synctypes.py is updated to compare:
          - Options specific to a command, found in C files, for the
            interactive help messages, with the same specific options from the
            relevant man page for that command.
          - Common options, checked just once: the list in main.h is
            compared with the new list in substitutions.rst.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211115225844.33943-3-quentin@isovalent.com
      b6231815
    • Quentin Monnet's avatar
      bpftool: Add SPDX tags to RST documentation files · 43448428
      Quentin Monnet authored
      Most files in the kernel repository have a SPDX tags. The files that
      don't have such a tag (or another license boilerplate) tend to fall
      under the GPL-2.0 license. In the past, bpftool's Makefile (for example)
      has been marked as GPL-2.0 for that reason, when in fact all bpftool is
      dual-licensed.
      
      To prevent a similar confusion from happening with the RST documentation
      files for bpftool, let's explicitly mark all files as dual-licensed.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211115225844.33943-2-quentin@isovalent.com
      43448428
    • Yonghong Song's avatar
      selftests/bpf: Add a dedup selftest with equivalent structure types · 47461583
      Yonghong Song authored
      Without previous libbpf patch, the following error will occur:
      
        $ ./test_progs -t btf
        ...
        do_test_dedup:FAIL:check btf_dedup failed errno:-22#13/205 btf/dedup: btf_type_tag #5, struct:FAIL
      
      And the previous libbpf patch fixed the issue.
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211115163943.3922547-1-yhs@fb.com
      47461583
    • Yonghong Song's avatar
      libbpf: Fix a couple of missed btf_type_tag handling in btf.c · 69a055d5
      Yonghong Song authored
      Commit 2dc1e488 ("libbpf: Support BTF_KIND_TYPE_TAG") added the
      BTF_KIND_TYPE_TAG support. But to test vmlinux build with ...
      
        #define __user __attribute__((btf_type_tag("user")))
      
      ... I needed to sync libbpf repo and manually copy libbpf sources to
      pahole. To simplify process, I used BTF_KIND_RESTRICT to simulate the
      BTF_KIND_TYPE_TAG with vmlinux build as "restrict" modifier is barely
      used in kernel.
      
      But this approach missed one case in dedup with structures where
      BTF_KIND_RESTRICT is handled and BTF_KIND_TYPE_TAG is not handled in
      btf_dedup_is_equiv(), and this will result in a pahole dedup failure.
      This patch fixed this issue and a selftest is added in the subsequent
      patch to test this scenario.
      
      The other missed handling is in btf__resolve_size(). Currently the compiler
      always emit like PTR->TYPE_TAG->... so in practice we don't hit the missing
      BTF_KIND_TYPE_TAG handling issue with compiler generated code. But lets
      add case BTF_KIND_TYPE_TAG in the switch statement to be future proof.
      
      Fixes: 2dc1e488 ("libbpf: Support BTF_KIND_TYPE_TAG")
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20211115163937.3922235-1-yhs@fb.com
      69a055d5