1. 19 Apr, 2022 7 commits
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Ensure type tags precede modifiers in BTF · eb596b09
      Kumar Kartikeya Dwivedi authored
      It is guaranteed that for modifiers, clang always places type tags
      before other modifiers, and then the base type. We would like to rely on
      this guarantee inside the kernel to make it simple to parse type tags
      from BTF.
      
      However, a user would be allowed to construct a BTF without such
      guarantees. Hence, add a pass to check that in modifier chains, type
      tags only occur at the head of the chain, and then don't occur later in
      the chain.
      
      If we see a type tag, we can have one or more type tags preceding other
      modifiers that then never have another type tag. If we see other
      modifiers, all modifiers following them should never be a type tag.
      
      Instead of having to walk chains we verified previously, we can remember
      the last good modifier type ID which headed a good chain. At that point,
      we must have verified all other chains headed by type IDs less than it.
      This makes the verification process less costly, and it becomes a simple
      O(n) pass.
      Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/bpf/20220419164608.1990559-2-memxor@gmail.com
      eb596b09
    • Andrii Nakryiko's avatar
      selftests/bpf: Use non-autoloaded programs in few tests · 0d7fefeb
      Andrii Nakryiko authored
      Take advantage of new libbpf feature for declarative non-autoloaded BPF
      program SEC() definitions in few test that test single program at a time
      out of many available programs within the single BPF object.
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220419002452.632125-2-andrii@kernel.org
      0d7fefeb
    • Andrii Nakryiko's avatar
      libbpf: Support opting out from autoloading BPF programs declaratively · a3820c48
      Andrii Nakryiko authored
      Establish SEC("?abc") naming convention (i.e., adding question mark in
      front of otherwise normal section name) that allows to set corresponding
      program's autoload property to false. This is effectively just
      a declarative way to do bpf_program__set_autoload(prog, false).
      
      Having a way to do this declaratively in BPF code itself is useful and
      convenient for various scenarios. E.g., for testing, when BPF object
      consists of multiple independent BPF programs that each needs to be
      tested separately. Opting out all of them by default and then setting
      autoload to true for just one of them at a time simplifies testing code
      (see next patch for few conversions in BPF selftests taking advantage of
      this new feature).
      
      Another real-world use case is in libbpf-tools for cases when different
      BPF programs have to be picked depending on particulars of the host
      kernel due to various incompatible changes (like kernel function renames
      or signature change, or to pick kprobe vs fentry depending on
      corresponding kernel support for the latter). Marking all the different
      BPF program candidates as non-autoloaded declaratively makes this more
      obvious in BPF source code and allows simpler code in user-space code.
      
      When BPF program marked as SEC("?abc") it is otherwise treated just like
      SEC("abc") and bpf_program__section_name() reported will be "abc".
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220419002452.632125-1-andrii@kernel.org
      a3820c48
    • Yonghong Song's avatar
      selftests/bpf: Workaround a verifier issue for test exhandler · 44df171a
      Yonghong Song authored
      The llvm patch [1] enabled opaque pointer which caused selftest
      'exhandler' failure.
        ...
        ; work = task->task_works;
        7: (79) r1 = *(u64 *)(r6 +2120)       ; R1_w=ptr_callback_head(off=0,imm=0) R6_w=ptr_task_struct(off=0,imm=0)
        ; func = work->func;
        8: (79) r2 = *(u64 *)(r1 +8)          ; R1_w=ptr_callback_head(off=0,imm=0) R2_w=scalar()
        ; if (!work && !func)
        9: (4f) r1 |= r2
        math between ptr_ pointer and register with unbounded min value is not allowed
      
        below is insn 10 and 11
        10: (55) if r1 != 0 goto +5
        11: (18) r1 = 0 ll
        ...
      
      In llvm, the code generation of 'r1 |= r2' happened in codegen
      selectiondag phase due to difference of opaque pointer vs. non-opaque pointer.
      Without [1], the related code looks like:
        r2 = *(u64 *)(r6 + 2120)
        r1 = *(u64 *)(r2 + 8)
        if r2 != 0 goto +6 <LBB0_4>
        if r1 != 0 goto +5 <LBB0_4>
        r1 = 0 ll
        ...
      
      I haven't found a good way in llvm to fix this issue. So let us workaround the
      problem first so bpf CI won't be blocked.
      
        [1] https://reviews.llvm.org/D123300Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220419050900.3136024-1-yhs@fb.com
      44df171a
    • Yonghong Song's avatar
      selftests/bpf: Limit unroll_count for pyperf600 test · 8c89b5db
      Yonghong Song authored
      LLVM commit [1] changed loop pragma behavior such that
      full loop unroll is always honored with user pragma.
      Previously, unroll count also depends on the unrolled
      code size. For pyperf600, without [1], the loop unroll
      count is 150. With [1], the loop unroll count is 600.
      
      The unroll count of 600 caused the program size close to
      298k and this caused the following code is generated:
               0:       7b 1a 00 ff 00 00 00 00 *(u64 *)(r10 - 256) = r1
        ;       uint64_t pid_tgid = bpf_get_current_pid_tgid();
               1:       85 00 00 00 0e 00 00 00 call 14
               2:       bf 06 00 00 00 00 00 00 r6 = r0
        ;       pid_t pid = (pid_t)(pid_tgid >> 32);
               3:       bf 61 00 00 00 00 00 00 r1 = r6
               4:       77 01 00 00 20 00 00 00 r1 >>= 32
               5:       63 1a fc ff 00 00 00 00 *(u32 *)(r10 - 4) = r1
               6:       bf a2 00 00 00 00 00 00 r2 = r10
               7:       07 02 00 00 fc ff ff ff r2 += -4
        ;       PidData* pidData = bpf_map_lookup_elem(&pidmap, &pid);
               8:       18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 = 0 ll
              10:       85 00 00 00 01 00 00 00 call 1
              11:       bf 08 00 00 00 00 00 00 r8 = r0
        ;       if (!pidData)
              12:       15 08 15 e8 00 00 00 00 if r8 == 0 goto -6123 <LBB0_27588+0xffffffffffdae100>
      
      Note that insn 12 has a branch offset -6123 which is clearly illegal
      and will be rejected by the verifier. The negative offset is due to
      the branch range is greater than INT16_MAX.
      
      This patch changed the unroll count to be 150 to avoid above
      branch target insn out-of-range issue. Also the llvm is enhanced ([2])
      to assert if the branch target insn is out of INT16 range.
      
        [1] https://reviews.llvm.org/D119148
        [2] https://reviews.llvm.org/D123877Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220419043230.2928530-1-yhs@fb.com
      8c89b5db
    • Stanislav Fomichev's avatar
      bpf: Move rcu lock management out of BPF_PROG_RUN routines · 055eb955
      Stanislav Fomichev authored
      Commit 7d08c2c9 ("bpf: Refactor BPF_PROG_RUN_ARRAY family of macros
      into functions") switched a bunch of BPF_PROG_RUN macros to inline
      routines. This changed the semantic a bit. Due to arguments expansion
      of macros, it used to be:
      
      	rcu_read_lock();
      	array = rcu_dereference(cgrp->bpf.effective[atype]);
      	...
      
      Now, with with inline routines, we have:
      	array_rcu = rcu_dereference(cgrp->bpf.effective[atype]);
      	/* array_rcu can be kfree'd here */
      	rcu_read_lock();
      	array = rcu_dereference(array_rcu);
      
      I'm assuming in practice rcu subsystem isn't fast enough to trigger
      this but let's use rcu API properly.
      
      Also, rename to lower caps to not confuse with macros. Additionally,
      drop and expand BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY.
      
      See [1] for more context.
      
        [1] https://lore.kernel.org/bpf/CAKH8qBs60fOinFdxiiQikK_q0EcVxGvNTQoWvHLEUGbgcj1UYg@mail.gmail.com/T/#u
      
      v2
      - keep rcu locks inside by passing cgroup_bpf
      
      Fixes: 7d08c2c9 ("bpf: Refactor BPF_PROG_RUN_ARRAY family of macros into functions")
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Link: https://lore.kernel.org/bpf/20220414161233.170780-1-sdf@google.com
      055eb955
    • Mykola Lysenko's avatar
      selftests/bpf: Refactor prog_tests logging and test execution · 2324257d
      Mykola Lysenko authored
      This is a pre-req to add separate logging for each subtest in
      test_progs.
      
      Move all the mutable test data to the test_result struct.
      Move per-test init/de-init into the run_one_test function.
      Consolidate data aggregation and final log output in
      calculate_and_print_summary function.
      As a side effect, this patch fixes double counting of errors
      for subtests and possible duplicate output of subtest log
      on failures.
      
      Also, add prog_tests_framework.c test to verify some of the
      counting logic.
      
      As part of verification, confirmed that number of reported
      tests is the same before and after the change for both parallel
      and sequential test execution.
      Signed-off-by: default avatarMykola Lysenko <mykolal@fb.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220418222507.1726259-1-mykolal@fb.com
      2324257d
  2. 15 Apr, 2022 14 commits
  3. 13 Apr, 2022 3 commits
  4. 11 Apr, 2022 12 commits
  5. 09 Apr, 2022 1 commit
    • Jakub Kicinski's avatar
      Merge https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next · 34ba23b4
      Jakub Kicinski authored
      Daniel Borkmann says:
      
      ====================
      pull-request: bpf-next 2022-04-09
      
      We've added 63 non-merge commits during the last 9 day(s) which contain
      a total of 68 files changed, 4852 insertions(+), 619 deletions(-).
      
      The main changes are:
      
      1) Add libbpf support for USDT (User Statically-Defined Tracing) probes.
         USDTs are an abstraction built on top of uprobes, critical for tracing
         and BPF, and widely used in production applications, from Andrii Nakryiko.
      
      2) While Andrii was adding support for x86{-64}-specific logic of parsing
         USDT argument specification, Ilya followed-up with USDT support for s390
         architecture, from Ilya Leoshkevich.
      
      3) Support name-based attaching for uprobe BPF programs in libbpf. The format
         supported is `u[ret]probe/binary_path:[raw_offset|function[+offset]]`, e.g.
         attaching to libc malloc can be done in BPF via SEC("uprobe/libc.so.6:malloc")
         now, from Alan Maguire.
      
      4) Various load/store optimizations for the arm64 JIT to shrink the image
         size by using arm64 str/ldr immediate instructions. Also enable pointer
         authentication to verify return address for JITed code, from Xu Kuohai.
      
      5) BPF verifier fixes for write access checks to helper functions, e.g.
         rd-only memory from bpf_*_cpu_ptr() must not be passed to helpers that
         write into passed buffers, from Kumar Kartikeya Dwivedi.
      
      6) Fix overly excessive stack map allocation for its base map structure and
         buckets which slipped-in from cleanups during the rlimit accounting removal
         back then, from Yuntao Wang.
      
      7) Extend the unstable CT lookup helpers for XDP and tc/BPF to report netfilter
         connection tracking tuple direction, from Lorenzo Bianconi.
      
      8) Improve bpftool dump to show BPF program/link type names, Milan Landaverde.
      
      9) Minor cleanups all over the place from various others.
      
      * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: (63 commits)
        bpf: Fix excessive memory allocation in stack_map_alloc()
        selftests/bpf: Fix return value checks in perf_event_stackmap test
        selftests/bpf: Add CO-RE relos into linked_funcs selftests
        libbpf: Use weak hidden modifier for USDT BPF-side API functions
        libbpf: Don't error out on CO-RE relos for overriden weak subprogs
        samples, bpf: Move routes monitor in xdp_router_ipv4 in a dedicated thread
        libbpf: Allow WEAK and GLOBAL bindings during BTF fixup
        libbpf: Use strlcpy() in path resolution fallback logic
        libbpf: Add s390-specific USDT arg spec parsing logic
        libbpf: Make BPF-side of USDT support work on big-endian machines
        libbpf: Minor style improvements in USDT code
        libbpf: Fix use #ifdef instead of #if to avoid compiler warning
        libbpf: Potential NULL dereference in usdt_manager_attach_usdt()
        selftests/bpf: Uprobe tests should verify param/return values
        libbpf: Improve string parsing for uprobe auto-attach
        libbpf: Improve library identification for uprobe binary path resolution
        selftests/bpf: Test for writes to map key from BPF helpers
        selftests/bpf: Test passing rdonly mem to global func
        bpf: Reject writes for PTR_TO_MAP_KEY in check_helper_mem_access
        bpf: Check PTR_TO_MEM | MEM_RDONLY in check_helper_mem_access
        ...
      ====================
      
      Link: https://lore.kernel.org/r/20220408231741.19116-1-daniel@iogearbox.netSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
      34ba23b4
  6. 08 Apr, 2022 3 commits