Skip to content
  1. Feb 02, 2024
    • Andrii Nakryiko's avatar
      libbpf: Call memfd_create() syscall directly · 9fa5e1a1
      Andrii Nakryiko authored
      
      
      Some versions of Android do not implement memfd_create() wrapper in
      their libc implementation, leading to build failures ([0]). On the other
      hand, memfd_create() is available as a syscall on quite old kernels
      (3.17+, while bpf() syscall itself is available since 3.18+), so it is
      ok to assume that syscall availability and call into it with syscall()
      helper to avoid Android-specific workarounds.
      
      Validated in libbpf-bootstrap's CI ([1]).
      
        [0] https://github.com/libbpf/libbpf-bootstrap/actions/runs/7701003207/job/20986080319#step:5:83
        [1] https://github.com/libbpf/libbpf-bootstrap/actions/runs/7715988887/job/21031767212?pr=253
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Link: https://lore.kernel.org/bpf/20240201172027.604869-2-andrii@kernel.org
      9fa5e1a1
    • Matt Bobrowski's avatar
      bpf: Minor clean-up to sleepable_lsm_hooks BTF set · 1581e511
      Matt Bobrowski authored
      
      
      There's already one main CONFIG_SECURITY_NETWORK ifdef block within
      the sleepable_lsm_hooks BTF set. Consolidate this duplicated ifdef
      block as there's no need for it and all things guarded by it should
      remain in one place in this specific context.
      
      Signed-off-by: default avatarMatt Bobrowski <mattbobrowski@google.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/Zbt1smz43GDMbVU3@google.com
      1581e511
  2. Feb 01, 2024
    • Pu Lehui's avatar
      selftests/bpf: Enable inline bpf_kptr_xchg() test for RV64 · 994ff2f7
      Pu Lehui authored
      
      
      Enable inline bpf_kptr_xchg() test for RV64, and the test have passed as
      show below:
      
      Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
      
      Signed-off-by: default avatarPu Lehui <pulehui@huawei.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarBjörn Töpel <bjorn@kernel.org>
      Link: https://lore.kernel.org/bpf/20240130124659.670321-3-pulehui@huaweicloud.com
      994ff2f7
    • Pu Lehui's avatar
      riscv, bpf: Enable inline bpf_kptr_xchg() for RV64 · 69065aa1
      Pu Lehui authored
      
      
      RV64 JIT supports 64-bit BPF_XCHG atomic instructions. At the same time,
      the underlying implementation of xchg() and atomic64_xchg() in RV64 both
      are raw_xchg() that supported 64-bit. Therefore inline bpf_kptr_xchg()
      will have equivalent semantics. Let's inline it for better performance.
      
      Signed-off-by: default avatarPu Lehui <pulehui@huawei.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarBjörn Töpel <bjorn@kernel.org>
      Link: https://lore.kernel.org/bpf/20240130124659.670321-2-pulehui@huaweicloud.com
      69065aa1
    • Dave Thaler's avatar
      bpf, docs: Clarify which legacy packet instructions existed · 088a464e
      Dave Thaler authored
      
      
      As discussed on the BPF IETF mailing list (see link), this patch updates
      the "Legacy BPF Packet access instructions" section to clarify which
      instructions are deprecated (vs which were never defined and so are not
      deprecated).
      
      Signed-off-by: default avatarDave Thaler <dthaler1968@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Acked-by: default avatarDavid Vernet <void@manifault.com>
      Link: https://mailarchive.ietf.org/arch/msg/bpf/5LnnKm093cGpOmDI9TnLQLBXyys
      Link: https://lore.kernel.org/bpf/20240131033759.3634-1-dthaler1968@gmail.com
      088a464e
    • Eduard Zingerman's avatar
      libbpf: Remove unnecessary null check in kernel_supports() · 8263b338
      Eduard Zingerman authored
      After recent changes, Coverity complained about inconsistent null checks
      in kernel_supports() function:
      
          kernel_supports(const struct bpf_object *obj, ...)
          [...]
          // var_compare_op: Comparing obj to null implies that obj might be null
          if (obj && obj->gen_loader)
              return true;
      
          // var_deref_op: Dereferencing null pointer obj
          if (obj->token_fd)
              return feat_supported(obj->feat_cache, feat_id);
          [...]
      
      - The original null check was introduced by commit [0], which introduced
        a call `kernel_supports(NULL, ...)` in function bump_rlimit_memlock();
      - This call was refactored to use `feat_supported(NULL, ...)` in commit [1].
      
      Looking at all places where kernel_supports() is called:
      
      - There is either `obj->...` access before the call;
      - Or `obj` comes from `prog->obj` expression, where `prog` comes from
        enumeration of programs in `obj`;
      - Or `obj` comes from `prog->obj`, where `prog` is a parameter to one
        of the API functions:
        - bpf_program__attach_kprobe_opts;
        - bpf_program__attach_kprobe;
        - bpf_program__attach_ksyscall.
      
      Assuming correct API usage, it appears that `obj` can never be null when
      passed to kernel_supports(). Silence the Coverity warning by removing
      redundant null check.
      
        [0] e542f2c4 ("libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF")
        [1] d6dd1d49
      
       ("libbpf: Further decouple feature checking logic from bpf_object")
      
      Signed-off-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20240131212615.20112-1-eddyz87@gmail.com
      8263b338
    • Alexei Starovoitov's avatar
      Merge branch 'annotate-kfuncs-in-btf_ids-section' · b3d3e293
      Alexei Starovoitov authored
      
      
      Daniel Xu says:
      
      ====================
      Annotate kfuncs in .BTF_ids section
      
      === Description ===
      
      This is a bpf-treewide change that annotates all kfuncs as such inside
      .BTF_ids. This annotation eventually allows us to automatically generate
      kfunc prototypes from bpftool.
      
      We store this metadata inside a yet-unused flags field inside struct
      btf_id_set8 (thanks Kumar!). pahole will be taught where to look.
      
      More details about the full chain of events are available in commit 3's
      description.
      
      The accompanying pahole and bpftool changes can be viewed
      here on these "frozen" branches [0][1].
      
      [0]: https://github.com/danobi/pahole/tree/kfunc_btf-v3-mailed
      [1]: https://github.com/danobi/linux/tree/kfunc_bpftool-mailed
      
      === Changelog ===
      
      Changes from v3:
      * Rebase to bpf-next and add missing annotation on new kfunc
      
      Changes from v2:
      * Only WARN() for vmlinux kfuncs
      
      Changes from v1:
      * Move WARN_ON() up a call level
      * Also return error when kfunc set is not properly tagged
      * Use BTF_KFUNCS_START/END instead of flags
      * Rename BTF_SET8_KFUNC to BTF_SET8_KFUNCS
      ====================
      
      Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
      Link: https://lore.kernel.org/r/cover.1706491398.git.dxu@dxuuu.xyz
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      b3d3e293
    • Daniel Xu's avatar
      bpf: treewide: Annotate BPF kfuncs in BTF · 6f3189f3
      Daniel Xu authored
      
      
      This commit marks kfuncs as such inside the .BTF_ids section. The upshot
      of these annotations is that we'll be able to automatically generate
      kfunc prototypes for downstream users. The process is as follows:
      
      1. In source, use BTF_KFUNCS_START/END macro pair to mark kfuncs
      2. During build, pahole injects into BTF a "bpf_kfunc" BTF_DECL_TAG for
         each function inside BTF_KFUNCS sets
      3. At runtime, vmlinux or module BTF is made available in sysfs
      4. At runtime, bpftool (or similar) can look at provided BTF and
         generate appropriate prototypes for functions with "bpf_kfunc" tag
      
      To ensure future kfunc are similarly tagged, we now also return error
      inside kfunc registration for untagged kfuncs. For vmlinux kfuncs,
      we also WARN(), as initcall machinery does not handle errors.
      
      Signed-off-by: default avatarDaniel Xu <dxu@dxuuu.xyz>
      Acked-by: default avatarBenjamin Tissoires <bentiss@kernel.org>
      Link: https://lore.kernel.org/r/e55150ceecbf0a5d961e608941165c0bee7bc943.1706491398.git.dxu@dxuuu.xyz
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      6f3189f3
    • Daniel Xu's avatar
      bpf: btf: Add BTF_KFUNCS_START/END macro pair · a05e9042
      Daniel Xu authored
      
      
      This macro pair is functionally equivalent to BTF_SET8_START/END, except
      with BTF_SET8_KFUNCS flag set in the btf_id_set8 flags field. The next
      commit will codemod all kfunc set8s to this new variant such that all
      kfuncs are tagged as such in .BTF_ids section.
      
      Signed-off-by: default avatarDaniel Xu <dxu@dxuuu.xyz>
      Link: https://lore.kernel.org/r/d536c57c7c2af428686853cc7396b7a44faa53b7.1706491398.git.dxu@dxuuu.xyz
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      a05e9042
    • Daniel Xu's avatar
      bpf: btf: Support flags for BTF_SET8 sets · 79b47344
      Daniel Xu authored
      
      
      This commit adds support for flags on BTF_SET8s. struct btf_id_set8
      already supported 32 bits worth of flags, but was only used for
      alignment purposes before.
      
      We now use these bits to encode flags. The first use case is tagging
      kfunc sets with a flag so that pahole can recognize which
      BTF_ID_FLAGS(func, ..) are actual kfuncs.
      
      Signed-off-by: default avatarDaniel Xu <dxu@dxuuu.xyz>
      Link: https://lore.kernel.org/r/7bb152ec76d6c2c930daec88e995bf18484a5ebb.1706491398.git.dxu@dxuuu.xyz
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      79b47344
    • Manu Bretelle's avatar
      selftests/bpf: Disable IPv6 for lwt_redirect test · 2ef61296
      Manu Bretelle authored
      After a recent change in the vmtest runner, this test started failing
      sporadically.
      
      Investigation showed that this test was subject to race condition which
      got exacerbated after the vm runner change. The symptoms being that the
      logic that waited for an ICMPv4 packet is naive and will break if 5 or
      more non-ICMPv4 packets make it to tap0.
      When ICMPv6 is enabled, the kernel will generate traffic such as ICMPv6
      router solicitation...
      On a system with good performance, the expected ICMPv4 packet would very
      likely make it to the network interface promptly, but on a system with
      poor performance, those "guarantees" do not hold true anymore.
      
      Given that the test is IPv4 only, this change disable IPv6 in the test
      netns by setting `net.ipv6.conf.all.disable_ipv6` to 1.
      This essentially leaves "ping" as the sole generator of traffic in the
      network namespace.
      If this test was to be made IPv6 compatible, the logic in
      `wait_for_packet` would need to be modified.
      
      In more details...
      
      At a high level, the test does:
      - create a new namespace
      - in `setup_redirect_target` set up lo, tap0, and link_err interfaces as
        well as add 2 routes that attaches ingress/egress sections of
        `test_lwt_redirect.bpf.o` to the xmit path.
      - in `send_and_capture_test_packets` send an ICMP packet and read off
        the tap interface (using `wait_for_packet`) to check that a ICMP packet
        with the right size is read.
      
      `wait_for_packet` will try to read `max_retry` (5) times from the tap0
      fd looking for an ICMPv4 packet matching some criteria.
      
      The problem is that when we set up the `tap0` interface, because IPv6 is
      enabled by default, traffic such as Router solicitation is sent through
      tap0, as in:
      
        # tcpdump -r /tmp/lwt_redirect.pc
        reading from file /tmp/lwt_redirect.pcap, link-type EN10MB (Ethernet)
        04:46:23.578352 IP6 :: > ff02::1:ffc0:4427: ICMP6, neighbor solicitation, who has fe80::fcba:dff:fec0:4427, length 32
        04:46:23.659522 IP6 :: > ff02::16: HBH ICMP6, multicast listener report v2, 1 group record(s), length 28
        04:46:24.389169 IP 10.0.0.1 > 20.0.0.9: ICMP echo request, id 122, seq 1, length 108
        04:46:24.618599 IP6 fe80::fcba:dff:fec0:4427 > ff02::16: HBH ICMP6, multicast listener report v2, 1 group record(s), length 28
        04:46:24.619985 IP6 fe80::fcba:dff:fec0:4427 > ff02::2: ICMP6, router solicitation, length 16
        04:46:24.767326 IP6 fe80::fcba:dff:fec0:4427 > ff02::16: HBH ICMP6, multicast listener report v2, 1 group record(s), length 28
        04:46:28.936402 IP6 fe80::fcba:dff:fec0:4427 > ff02::2: ICMP6, router solicitation, length 16
      
      If `wait_for_packet` sees 5 non-ICMPv4 packets, it will return 0, which is what we see in:
      
        2024-01-31T03:51:25.0336992Z test_lwt_redirect_run:PASS:netns_create 0 nsec
        2024-01-31T03:51:25.0341309Z open_netns:PASS:malloc token 0 nsec
        2024-01-31T03:51:25.0344844Z open_netns:PASS:open /proc/self/ns/net 0 nsec
        2024-01-31T03:51:25.0350071Z open_netns:PASS:open netns fd 0 nsec
        2024-01-31T03:51:25.0353516Z open_netns:PASS:setns 0 nsec
        2024-01-31T03:51:25.0356560Z test_lwt_redirect_run:PASS:setns 0 nsec
        2024-01-31T03:51:25.0360140Z open_tuntap:PASS:open(/dev/net/tun) 0 nsec
        2024-01-31T03:51:25.0363822Z open_tuntap:PASS:ioctl(TUNSETIFF) 0 nsec
        2024-01-31T03:51:25.0367402Z open_tuntap:PASS:fcntl(O_NONBLOCK) 0 nsec
        2024-01-31T03:51:25.0371167Z setup_redirect_target:PASS:open_tuntap 0 nsec
        2024-01-31T03:51:25.0375180Z setup_redirect_target:PASS:if_nametoindex 0 nsec
        2024-01-31T03:51:25.0379929Z setup_redirect_target:PASS:ip link add link_err type dummy 0 nsec
        2024-01-31T03:51:25.0384874Z setup_redirect_target:PASS:ip link set lo up 0 nsec
        2024-01-31T03:51:25.0389678Z setup_redirect_target:PASS:ip addr add dev lo 10.0.0.1/32 0 nsec
        2024-01-31T03:51:25.0394814Z setup_redirect_target:PASS:ip link set link_err up 0 nsec
        2024-01-31T03:51:25.0399874Z setup_redirect_target:PASS:ip link set tap0 up 0 nsec
        2024-01-31T03:51:25.0407731Z setup_redirect_target:PASS:ip route add 10.0.0.0/24 dev link_err encap bpf xmit obj test_lwt_redirect.bpf.o sec redir_ingress 0 nsec
        2024-01-31T03:51:25.0419105Z setup_redirect_target:PASS:ip route add 20.0.0.0/24 dev link_err encap bpf xmit obj test_lwt_redirect.bpf.o sec redir_egress 0 nsec
        2024-01-31T03:51:25.0427209Z test_lwt_redirect_normal:PASS:setup_redirect_target 0 nsec
        2024-01-31T03:51:25.0431424Z ping_dev:PASS:if_nametoindex 0 nsec
        2024-01-31T03:51:25.0437222Z send_and_capture_test_packets:FAIL:wait_for_epacket unexpected wait_for_epacket: actual 0 != expected 1
        2024-01-31T03:51:25.0448298Z (/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c:175: errno: Success) test_lwt_redirect_normal egress test fails
        2024-01-31T03:51:25.0457124Z close_netns:PASS:setns 0 nsec
      
      When running in a VM which potential resource contrains, the odds that calling
      `ping` is not scheduled very soon after bringing `tap0` up increases,
      and with this the chances to get our ICMP packet pushed to position 6+
      in the network trace.
      
      To confirm this indeed solves the issue, I ran the test 100 times in a
      row with:
      
        errors=0
        successes=0
        for i in `seq 1 100`
        do
          ./test_progs -t lwt_redirect/lwt_redirect_normal
          if [ $? -eq 0 ]; then
            successes=$((successes+1))
          else
            errors=$((errors+1))
          fi
        done
        echo "successes: $successes/errors: $errors"
      
      While this test would at least fail a couple of time every 10 runs, here
      it ran 100 times with no error.
      
      Fixes: 43a7c3ef
      
       ("selftests/bpf: Add lwt_xmit tests for BPF_REDIRECT")
      Signed-off-by: default avatarManu Bretelle <chantr4@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20240131053212.2247527-1-chantr4@gmail.com
      2ef61296
  3. Jan 31, 2024
    • Martin KaFai Lau's avatar
      Merge branch 'libbpf: add bpf_core_cast() helper' · e4009250
      Martin KaFai Lau authored
      
      
      Andrii Nakryiko says:
      
      ====================
      Add bpf_core_cast(<ptr>, <type>) macro wrapper around bpf_rdonly_cast() kfunc
      to make it easier to use this functionality in BPF code. See patch #2 for
      BPF selftests conversions demonstrating improvements in code succinctness.
      ====================
      
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      e4009250
    • Andrii Nakryiko's avatar
      selftests/bpf: convert bpf_rdonly_cast() uses to bpf_core_cast() macro · ea9d5616
      Andrii Nakryiko authored
      
      
      Use more ergonomic bpf_core_cast() macro instead of bpf_rdonly_cast() in
      selftests code.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240130212023.183765-3-andrii@kernel.org
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      ea9d5616
    • Andrii Nakryiko's avatar
      libbpf: add bpf_core_cast() macro · 20d59ee5
      Andrii Nakryiko authored
      
      
      Add bpf_core_cast() macro that wraps bpf_rdonly_cast() kfunc. It's more
      ergonomic than kfunc, as it automatically extracts btf_id with
      bpf_core_type_id_kernel(), and works with type names. It also casts result
      to (T *) pointer. See the definition of the macro, it's self-explanatory.
      
      libbpf declares bpf_rdonly_cast() extern as __weak __ksym and should be
      safe to not conflict with other possible declarations in user code.
      
      But we do have a conflict with current BPF selftests that declare their
      externs with first argument as `void *obj`, while libbpf opts into more
      permissive `const void *obj`. This causes conflict, so we fix up BPF
      selftests uses in the same patch.
      
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240130212023.183765-2-andrii@kernel.org
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      20d59ee5
    • Alexei Starovoitov's avatar
      Merge branch 'trusted-ptr_to_btf_id-arg-support-in-global-subprogs' · 4d8ebe13
      Alexei Starovoitov authored
      
      
      Andrii Nakryiko says:
      
      ====================
      Trusted PTR_TO_BTF_ID arg support in global subprogs
      
      This patch set follows recent changes that added btf_decl_tag-based argument
      annotation support for global subprogs. This time we add ability to pass
      PTR_TO_BTF_ID (BTF-aware kernel pointers) arguments into global subprograms.
      We support explicitly trusted arguments only, for now.
      
      Patch #1 adds logic for arg:trusted tag support on the verifier side. Default
      semantic of such arguments is non-NULL, enforced on caller side. But patch #2
      adds arg:nullable tag that can be combined with arg:trusted to make callee
      explicitly do the NULL check, which helps implement "optional" PTR_TO_BTF_ID
      arguments.
      
      Patch #3 adds libbpf-side __arg_trusted and __arg_nullable macros.
      
      Patch #4 adds a bunch of tests validating __arg_trusted in combination with
      __arg_nullable.
      
      v2->v3:
        - went back to arg:nullable and __arg_nullable naming;
        - rebased on latest bpf-next after prepartory patches landed;
      v1->v2:
        - added fix up to type enforcement changes, landed earlier;
        - dropped bpf_core_cast() changes, will post them separately, as they now
          are not used in added tests;
        - dropped arg:untrusted support (Alexei);
        - renamed arg:nullable to arg:maybe_null (Alexei);
        - and also added task_struct___local flavor tests (Alexei).
      ====================
      
      Link: https://lore.kernel.org/r/20240130000648.2144827-1-andrii@kernel.org
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      4d8ebe13
    • Andrii Nakryiko's avatar
      selftests/bpf: add trusted global subprog arg tests · c381203e
      Andrii Nakryiko authored
      
      
      Add a bunch of test cases validating behavior of __arg_trusted and its
      combination with __arg_nullable tag. We also validate CO-RE flavor
      support by kernel for __arg_trusted args.
      
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240130000648.2144827-5-andrii@kernel.org
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      c381203e
    • Andrii Nakryiko's avatar
      libbpf: add __arg_trusted and __arg_nullable tag macros · d28bb1a8
      Andrii Nakryiko authored
      
      
      Add __arg_trusted to annotate global func args that accept trusted
      PTR_TO_BTF_ID arguments.
      
      Also add __arg_nullable to combine with __arg_trusted (and maybe other
      tags in the future) to force global subprog itself (i.e., callee) to do
      NULL checks, as opposed to default non-NULL semantics (and thus caller's
      responsibility to ensure non-NULL values).
      
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240130000648.2144827-4-andrii@kernel.org
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      d28bb1a8
    • Andrii Nakryiko's avatar
      bpf: add arg:nullable tag to be combined with trusted pointers · 8f2b44cd
      Andrii Nakryiko authored
      
      
      Add ability to mark arg:trusted arguments with optional arg:nullable
      tag to mark it as PTR_TO_BTF_ID_OR_NULL variant, which will allow
      callers to pass NULL, and subsequently will force global subprog's code
      to do NULL check. This allows to have "optional" PTR_TO_BTF_ID values
      passed into global subprogs.
      
      For now arg:nullable cannot be combined with anything else.
      
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240130000648.2144827-3-andrii@kernel.org
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      8f2b44cd
    • Andrii Nakryiko's avatar
      bpf: add __arg_trusted global func arg tag · e2b3c4ff
      Andrii Nakryiko authored
      
      
      Add support for passing PTR_TO_BTF_ID registers to global subprogs.
      Currently only PTR_TRUSTED flavor of PTR_TO_BTF_ID is supported.
      Non-NULL semantics is assumed, so caller will be forced to prove
      PTR_TO_BTF_ID can't be NULL.
      
      Note, we disallow global subprogs to destroy passed in PTR_TO_BTF_ID
      arguments, even the trusted one. We achieve that by not setting
      ref_obj_id when validating subprog code. This basically enforces (in
      Rust terms) borrowing semantics vs move semantics. Borrowing semantics
      seems to be a better fit for isolated global subprog validation
      approach.
      
      Implementation-wise, we utilize existing logic for matching
      user-provided BTF type to kernel-side BTF type, used by BPF CO-RE logic
      and following same matching rules. We enforce a unique match for types.
      
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240130000648.2144827-2-andrii@kernel.org
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      e2b3c4ff
  4. Jan 30, 2024
    • Jose E. Marchesi's avatar
      bpf: Move -Wno-compare-distinct-pointer-types to BPF_CFLAGS · 24219056
      Jose E. Marchesi authored
      
      
      Clang supports enabling/disabling certain conversion diagnostics via
      the -W[no-]compare-distinct-pointer-types command line options.
      Disabling this warning is required by some BPF selftests due to
      -Werror.  Until very recently GCC would emit these warnings
      unconditionally, which was a problem for gcc-bpf, but we added support
      for the command-line options to GCC upstream [1].
      
      This patch moves the -Wno-cmopare-distinct-pointer-types from
      CLANG_CFLAGS to BPF_CFLAGS in selftests/bpf/Makefile so the option
      is also used in gcc-bpf builds, not just in clang builds.
      
      Tested in bpf-next master.
      No regressions.
      
        [1] https://gcc.gnu.org/pipermail/gcc-patches/2023-August/627769.html
      
      Signed-off-by: default avatarJose E. Marchesi <jose.marchesi@oracle.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20240130113624.24940-1-jose.marchesi@oracle.com
      24219056
    • Jose E. Marchesi's avatar
      bpf: Build type-punning BPF selftests with -fno-strict-aliasing · 27a90b14
      Jose E. Marchesi authored
      
      
      A few BPF selftests perform type punning and they may break strict
      aliasing rules, which are exploited by both GCC and clang by default
      while optimizing.  This can lead to broken compiled programs.
      
      This patch disables strict aliasing for these particular tests, by
      mean of the -fno-strict-aliasing command line option.  This will make
      sure these tests are optimized properly even if some strict aliasing
      rule gets violated.
      
      After this patch, GCC is able to build all the selftests without
      warning about potential strict aliasing issue.
      
      bpf@vger discussion on strict aliasing and BPF selftests:
      https://lore.kernel.org/bpf/bae1205a-b6e5-4e46-8e20-520d7c327f7a@linux.dev/T/#t
      
      Tested in bpf-next master.
      No regressions.
      
      Signed-off-by: default avatarJose E. Marchesi <jose.marchesi@oracle.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/bae1205a-b6e5-4e46-8e20-520d7c327f7a@linux.dev
      Link: https://lore.kernel.org/bpf/20240130110343.11217-1-jose.marchesi@oracle.com
      27a90b14
    • Haiyue Wang's avatar
      bpf,token: Use BIT_ULL() to convert the bit mask · 6668e818
      Haiyue Wang authored
      
      
      Replace the '(1ULL << *)' with the macro BIT_ULL(nr).
      
      Signed-off-by: default avatarHaiyue Wang <haiyue.wang@intel.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20240127134901.3698613-1-haiyue.wang@intel.com
      6668e818
    • Jose E. Marchesi's avatar
      bpf: Generate const static pointers for kernel helpers · ff2071a7
      Jose E. Marchesi authored
      
      
      The generated bpf_helper_defs.h file currently contains definitions
      like this for the kernel helpers, which are static objects:
      
        static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
      
      These work well in both clang and GCC because both compilers do
      constant propagation with -O1 and higher optimization, resulting in
      `call 1' BPF instructions being generated, which are calls to kernel
      helpers.
      
      However, there is a discrepancy on how the -Wunused-variable
      warning (activated by -Wall) is handled in these compilers:
      
      - clang will not emit -Wunused-variable warnings for static variables
        defined in C header files, be them constant or not constant.
      
      - GCC will not emit -Wunused-variable warnings for _constant_ static
        variables defined in header files, but it will emit warnings for
        non-constant static variables defined in header files.
      
      There is no reason for these bpf_helpers_def.h pointers to not be
      declared constant, and it is actually desirable to do so, since their
      values are not to be changed.  So this patch modifies bpf_doc.py to
      generate prototypes like:
      
        static void *(* const bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
      
      This allows GCC to not error while compiling BPF programs with `-Wall
      -Werror', while still being able to detect and error on legitimate
      unused variables in the program themselves.
      
      This change doesn't impact the desired constant propagation in neither
      Clang nor GCC with -O1 and higher.  On the contrary, being declared as
      constant may increase the odds they get constant folded when
      used/referred to in certain circumstances.
      
      Tested in bpf-next master.
      No regressions.
      
      Signed-off-by: default avatarJose E. Marchesi <jose.marchesi@oracle.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/bpf/20240127185031.29854-1-jose.marchesi@oracle.com
      ff2071a7
    • Ian Rogers's avatar
      libbpf: Add some details for BTF parsing failures · f2e4040c
      Ian Rogers authored
      
      
      As CONFIG_DEBUG_INFO_BTF is default off the existing "failed to find
      valid kernel BTF" message makes diagnosing the kernel build issue somewhat
      cryptic. Add a little more detail with the hope of helping users.
      
      Before:
      ```
      libbpf: failed to find valid kernel BTF
      libbpf: Error loading vmlinux BTF: -3
      ```
      
      After not accessible:
      ```
      libbpf: kernel BTF is missing at '/sys/kernel/btf/vmlinux', was CONFIG_DEBUG_INFO_BTF enabled?
      libbpf: failed to find valid kernel BTF
      libbpf: Error loading vmlinux BTF: -3
      ```
      
      After not readable:
      ```
      libbpf: failed to read kernel BTF from (/sys/kernel/btf/vmlinux): -1
      ```
      
      Closes: https://lore.kernel.org/bpf/CAP-5=fU+DN_+Y=Y4gtELUsJxKNDDCOvJzPHvjUVaUoeFAzNnig@mail.gmail.com/
      
      Signed-off-by: default avatarIan Rogers <irogers@google.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20240125231840.1647951-1-irogers@google.com
      f2e4040c
    • Florian Lehner's avatar
      perf/bpf: Fix duplicate type check · aecaa3ed
      Florian Lehner authored
      
      
      Remove the duplicate check on type and unify result.
      
      Signed-off-by: default avatarFlorian Lehner <dev@der-flo.net>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarSong Liu <song@kernel.org>
      Link: https://lore.kernel.org/bpf/20240120150920.3370-1-dev@der-flo.net
      aecaa3ed
    • Jose E. Marchesi's avatar
      bpf: Use -Wno-error in certain tests when building with GCC · 646751d5
      Jose E. Marchesi authored
      
      
      Certain BPF selftests contain code that, albeit being legal C, trigger
      warnings in GCC that cannot be disabled.  This is the case for example
      for the tests
      
        progs/btf_dump_test_case_bitfields.c
        progs/btf_dump_test_case_namespacing.c
        progs/btf_dump_test_case_packing.c
        progs/btf_dump_test_case_padding.c
        progs/btf_dump_test_case_syntax.c
      
      which contain struct type declarations inside function parameter
      lists.  This is problematic, because:
      
      - The BPF selftests are built with -Werror.
      
      - The Clang and GCC compilers sometimes differ when it comes to handle
        warnings.  in the handling of warnings.  One compiler may emit
        warnings for code that the other compiles compiles silently, and one
        compiler may offer the possibility to disable certain warnings, while
        the other doesn't.
      
      In order to overcome this problem, this patch modifies the
      tools/testing/selftests/bpf/Makefile in order to:
      
      1. Enable the possibility of specifing per-source-file extra CFLAGS.
         This is done by defining a make variable like:
      
         <source-filename>-CFLAGS := <whateverflags>
      
         And then modifying the proper Make rule in order to use these flags
         when compiling <source-filename>.
      
      2. Use the mechanism above to add -Wno-error to CFLAGS for the
         following selftests:
      
         progs/btf_dump_test_case_bitfields.c
         progs/btf_dump_test_case_namespacing.c
         progs/btf_dump_test_case_packing.c
         progs/btf_dump_test_case_padding.c
         progs/btf_dump_test_case_syntax.c
      
         Note the corresponding -CFLAGS variables for these files are
         defined only if the selftests are being built with GCC.
      
      Note that, while compiler pragmas can generally be used to disable
      particular warnings per file, this 1) is only possible for warning
      that actually can be disabled in the command line, i.e. that have
      -Wno-FOO options, and 2) doesn't apply to -Wno-error.
      
      Tested in bpf-next master branch.
      No regressions.
      
      Signed-off-by: default avatarJose E. Marchesi <jose.marchesi@oracle.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20240127100702.21549-1-jose.marchesi@oracle.com
      646751d5
    • Martin KaFai Lau's avatar
      selftests/bpf: Remove "&>" usage in the selftests · fbaf59a9
      Martin KaFai Lau authored
      
      
      In s390, CI reported that the sock_iter_batch selftest
      hits this error very often:
      
      2024-01-26T16:56:49.3091804Z Bind /proc/self/ns/net -> /run/netns/sock_iter_batch_netns failed: No such file or directory
      2024-01-26T16:56:49.3149524Z Cannot remove namespace file "/run/netns/sock_iter_batch_netns": No such file or directory
      2024-01-26T16:56:49.3772213Z test_sock_iter_batch:FAIL:ip netns add sock_iter_batch_netns unexpected error: 256 (errno 0)
      
      It happens very often in s390 but Manu also noticed it happens very
      sparsely in other arch also.
      
      It turns out the default dash shell does not recognize "&>"
      as a redirection operator, so the command went to the background.
      In the sock_iter_batch selftest, the "ip netns delete" went
      into background and then race with the following "ip netns add"
      command.
      
      This patch replaces the "&> /dev/null" usage with ">/dev/null 2>&1"
      and does this redirection in the SYS_NOFAIL macro instead of doing
      it individually by its caller. The SYS_NOFAIL callers do not care
      about failure, so it is no harm to do this redirection even if
      some of the existing callers do not redirect to /dev/null now.
      
      It touches different test files, so I skipped the Fixes tags
      in this patch. Some of the changed tests do not use "&>"
      but they use the SYS_NOFAIL, so these tests are also
      changed to avoid doing its own redirection because
      SYS_NOFAIL does it internally now.
      
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Link: https://lore.kernel.org/r/20240127025017.950825-1-martin.lau@linux.dev
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      fbaf59a9
    • Andrii Nakryiko's avatar
      bpf: move arg:ctx type enforcement check inside the main logic loop · add9c58c
      Andrii Nakryiko authored
      
      
      Now that bpf and bpf-next trees converged and we don't run the risk of
      merge conflicts, move btf_validate_prog_ctx_type() into its most logical
      place inside the main logic loop.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240125205510.3642094-4-andrii@kernel.org
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      add9c58c
    • Andrii Nakryiko's avatar
      libbpf: fix __arg_ctx type enforcement for perf_event programs · 9eea8faf
      Andrii Nakryiko authored
      Adjust PERF_EVENT type enforcement around __arg_ctx to match exactly
      what kernel is doing.
      
      Fixes: 76ec90a9
      
       ("libbpf: warn on unexpected __arg_ctx type when rewriting BTF")
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240125205510.3642094-3-andrii@kernel.org
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      9eea8faf
    • Andrii Nakryiko's avatar
      libbpf: integrate __arg_ctx feature detector into kernel_supports() · 0e6d0a9d
      Andrii Nakryiko authored
      
      
      Now that feature detection code is in bpf-next tree, integrate __arg_ctx
      kernel-side support into kernel_supports() framework.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240125205510.3642094-2-andrii@kernel.org
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      0e6d0a9d
  5. Jan 29, 2024