Commit Graph

146 Commits

Author SHA1 Message Date
Jeff Moyer 63317c47dc net: skbuff: drop the word head from skb cache
JIRA: https://issues.redhat.com/browse/RHEL-64867

commit 025a785ff083729819dc82ac81baf190cb4aee5c
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Wed Feb 8 22:06:42 2023 -0800

    net: skbuff: drop the word head from skb cache
    
    skbuff_head_cache is misnamed (perhaps for historical reasons?)
    because it does not hold heads. Head is the buffer which skb->data
    points to, and also where shinfo lives. struct sk_buff is a metadata
    structure, not the head.
    
    Eric recently added skb_small_head_cache (which allocates actual
    head buffers), let that serve as an excuse to finally clean this up :)
    
    Leave the user-space visible name intact, it could possibly be uAPI.
    
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>
    Reviewed-by: Eric Dumazet <edumazet@google.com>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
2024-11-28 16:03:44 -05:00
Viktor Malik 380f5f2d92
bpf: Set run context for rawtp test_run callback
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit d0d1df8ba18abc57f28fb3bc053b2bf319367f2c
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Tue Jun 4 17:00:24 2024 +0200

    bpf: Set run context for rawtp test_run callback
    
    syzbot reported crash when rawtp program executed through the
    test_run interface calls bpf_get_attach_cookie helper or any
    other helper that touches task->bpf_ctx pointer.
    
    Setting the run context (task->bpf_ctx pointer) for test_run
    callback.
    
    Fixes: 7adfc6c9b315 ("bpf: Add bpf_get_attach_cookie() BPF helper to access bpf_cookie value")
    Reported-by: syzbot+3ab78ff125b7979e45f9@syzkaller.appspotmail.com
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Closes: https://syzkaller.appspot.com/bug?extid=3ab78ff125b7979e45f9
    Link: https://lore.kernel.org/bpf/20240604150024.359247-1-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-13 09:39:21 +01:00
Viktor Malik 57c548a158
bpf: add bpf_modify_return_test_tp() kfunc triggering tracepoint
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 3124591f686115aca25d772c2ccb7b1e202c3197
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Mar 26 09:21:50 2024 -0700

    bpf: add bpf_modify_return_test_tp() kfunc triggering tracepoint
    
    Add a simple bpf_modify_return_test_tp() kfunc, available to all program
    types, that is useful for various testing and benchmarking scenarios, as
    it allows to trigger most tracing BPF program types from BPF side,
    allowing to do complex testing and benchmarking scenarios.
    
    It is also attachable to for fmod_ret programs, making it a good and
    simple way to trigger fmod_ret program under test/benchmark.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240326162151.3981687-6-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:36 +01:00
Jerome Marchand 563e3eb7e7 bpf: treewide: Annotate BPF kfuncs in BTF
JIRA: https://issues.redhat.com/browse/RHEL-23649

Conflicts: Multiple conflicts due to missing kfuncs. All sections were
switched to use the new macro except bpf_mptcp_fmodret_ids which still
use BTF_SET8_* upstream. I don't know why. That might be an upstream
oversight.

commit 6f3189f38a3e995232e028a4c341164c4aca1b20
Author: Daniel Xu <dxu@dxuuu.xyz>
Date:   Sun Jan 28 18:24:08 2024 -0700

    bpf: treewide: Annotate BPF kfuncs in BTF

    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: Daniel Xu <dxu@dxuuu.xyz>
    Acked-by: Benjamin Tissoires <bentiss@kernel.org>
    Link: https://lore.kernel.org/r/e55150ceecbf0a5d961e608941165c0bee7bc943.1706491398.git.dxu@dxuuu.xyz
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:07 +02:00
Viktor Malik 5cc2464aa0
bpf: Fix dtor CFI
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit e4c00339891c074c76f626ac82981963cbba5332
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Fri Dec 15 10:12:22 2023 +0100

    bpf: Fix dtor CFI

    Ensure the various dtor functions match their prototype and retain
    their CFI signatures, since they don't have their address taken, they
    are prone to not getting CFI, making them impossible to call
    indirectly.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Link: https://lore.kernel.org/r/20231215092707.799451071@infradead.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 11:07:27 +02:00
Viktor Malik c05e0a4805
bpf: Fix a few selftest failures due to llvm18 change
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit b16904fd9f01b580db357ef2b1cc9e86d89576c2
Author: Yonghong Song <yonghong.song@linux.dev>
Date:   Sun Nov 26 21:03:42 2023 -0800

    bpf: Fix a few selftest failures due to llvm18 change
    
    With latest upstream llvm18, the following test cases failed:
    
      $ ./test_progs -j
      #13/2    bpf_cookie/multi_kprobe_link_api:FAIL
      #13/3    bpf_cookie/multi_kprobe_attach_api:FAIL
      #13      bpf_cookie:FAIL
      #77      fentry_fexit:FAIL
      #78/1    fentry_test/fentry:FAIL
      #78      fentry_test:FAIL
      #82/1    fexit_test/fexit:FAIL
      #82      fexit_test:FAIL
      #112/1   kprobe_multi_test/skel_api:FAIL
      #112/2   kprobe_multi_test/link_api_addrs:FAIL
      [...]
      #112     kprobe_multi_test:FAIL
      #356/17  test_global_funcs/global_func17:FAIL
      #356     test_global_funcs:FAIL
    
    Further analysis shows llvm upstream patch [1] is responsible for the above
    failures. For example, for function bpf_fentry_test7() in net/bpf/test_run.c,
    without [1], the asm code is:
    
      0000000000000400 <bpf_fentry_test7>:
         400: f3 0f 1e fa                   endbr64
         404: e8 00 00 00 00                callq   0x409 <bpf_fentry_test7+0x9>
         409: 48 89 f8                      movq    %rdi, %rax
         40c: c3                            retq
         40d: 0f 1f 00                      nopl    (%rax)
    
    ... and with [1], the asm code is:
    
      0000000000005d20 <bpf_fentry_test7.specialized.1>:
        5d20: e8 00 00 00 00                callq   0x5d25 <bpf_fentry_test7.specialized.1+0x5>
        5d25: c3                            retq
    
    ... and <bpf_fentry_test7.specialized.1> is called instead of <bpf_fentry_test7>
    and this caused test failures for #13/#77 etc. except #356.
    
    For test case #356/17, with [1] (progs/test_global_func17.c)), the main prog
    looks like:
    
      0000000000000000 <global_func17>:
           0:       b4 00 00 00 2a 00 00 00 w0 = 0x2a
           1:       95 00 00 00 00 00 00 00 exit
    
    ... which passed verification while the test itself expects a verification
    failure.
    
    Let us add 'barrier_var' style asm code in both places to prevent function
    specialization which caused selftests failure.
    
      [1] https://github.com/llvm/llvm-project/pull/72903
    
    Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20231127050342.1945270-1-yonghong.song@linux.dev

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:51:56 +02:00
Viktor Malik 60f43ea9fe
bpf: Move kernel test kfuncs to bpf_testmod
JIRA: https://issues.redhat.com/browse/RHEL-19647

Conflicts: context change because d84abd7ea6 ("selftests/bpf: add
           testcase for TRACING with 6+ arguments") was previously taken
           without this commit present.

commit 65eb006d85a2ac0b23464808099726bd826e9877
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Mon May 15 15:37:56 2023 +0200

    bpf: Move kernel test kfuncs to bpf_testmod

    Moving kernel test kfuncs into bpf_testmod kernel module, and adding
    necessary init calls and BTF IDs records.

    We need to keep following structs in kernel:
      struct prog_test_ref_kfunc
      struct prog_test_member (embedded in prog_test_ref_kfunc)

    The reason is because they need to be marked as rcu safe (check test
    prog mark_ref_as_untrusted_or_null) and such objects are being required
    to be defined only in kernel at the moment (see rcu_safe_kptr check
    in kernel).

    We need to keep also dtor functions for both objects in kernel:
      bpf_kfunc_call_test_release
      bpf_kfunc_call_memb_release

    We also keep the copy of these struct in bpf_testmod_kfunc.h, because
    other test functions use them. This is unfortunate, but this is just
    temporary solution until we are able to these structs them to bpf_testmod
    completely.

    As suggested by David adding bpf_testmod.ko make dependency for
    bpf programs, so they are rebuilt if we change the bpf_testmod.ko
    module.

    Also adding missing __bpf_kfunc to bpf_kfunc_call_test4 functions.

    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/r/20230515133756.1658301-11-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:51:30 +02:00
Petr Oros c2838bf4a9 bpf, test_run: fix crashes due to XDP frame overwriting/corruption
JIRA: https://issues.redhat.com/browse/RHEL-31941

Conflicts:
- added conflict resolution from upstream merge commit c2865b1122595e
  ("Daniel Borkmann says:")

Upstream commit(s):
commit e5995bc7e2ba1a0d444f806016d2e4ea91c032d0
Author: Alexander Lobakin <aleksander.lobakin@intel.com>
Date:   Thu Mar 16 18:50:50 2023 +0100

    bpf, test_run: fix crashes due to XDP frame overwriting/corruption

    syzbot and Ilya faced the splats when %XDP_PASS happens for bpf_test_run
    after skb PP recycling was enabled for {__,}xdp_build_skb_from_frame():

    BUG: kernel NULL pointer dereference, address: 0000000000000d28
    RIP: 0010:memset_erms+0xd/0x20 arch/x86/lib/memset_64.S:66
    [...]
    Call Trace:
     <TASK>
     __finalize_skb_around net/core/skbuff.c:321 [inline]
     __build_skb_around+0x232/0x3a0 net/core/skbuff.c:379
     build_skb_around+0x32/0x290 net/core/skbuff.c:444
     __xdp_build_skb_from_frame+0x121/0x760 net/core/xdp.c:622
     xdp_recv_frames net/bpf/test_run.c:248 [inline]
     xdp_test_run_batch net/bpf/test_run.c:334 [inline]
     bpf_test_run_xdp_live+0x1289/0x1930 net/bpf/test_run.c:362
     bpf_prog_test_run_xdp+0xa05/0x14e0 net/bpf/test_run.c:1418
    [...]

    This happens due to that it calls xdp_scrub_frame(), which nullifies
    xdpf->data. bpf_test_run code doesn't reinit the frame when the XDP
    program doesn't adjust head or tail. Previously, %XDP_PASS meant the
    page will be released from the pool and returned to the MM layer, but
    now it does return to the Pool with the nullified xdpf->data, which
    doesn't get reinitialized then.
    So, in addition to checking whether the head and/or tail have been
    adjusted, check also for a potential XDP frame corruption. xdpf->data
    is 100% affected and also xdpf->flags is the field closest to the
    metadata / frame start. Checking for these two should be enough for
    non-extreme cases.

    Fixes: 9c94bbf9a87b ("xdp: recycle Page Pool backed skbs built from XDP frames")
    Reported-by: syzbot+e1d1b65f7c32f2a86a9f@syzkaller.appspotmail.com
    Link: https://lore.kernel.org/bpf/000000000000f1985705f6ef2243@google.com
    Reported-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Link: https://lore.kernel.org/bpf/e07dd94022ad5731705891b9487cc9ed66328b94.camel@linux.ibm.com
    Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
    Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
    Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Link: https://lore.kernel.org/r/20230316175051.922550-2-aleksander.lobakin@intel.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Petr Oros <poros@redhat.com>
2024-05-16 19:27:57 +02:00
Petr Oros 905a8ceb41 bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES
JIRA: https://issues.redhat.com/browse/RHEL-31941

Upstream commit(s):
commit 294635a8165a31408a8b3a24f9c74849ca3d8701
Author: Alexander Lobakin <aleksander.lobakin@intel.com>
Date:   Fri Feb 24 17:36:07 2023 +0100

    bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES

    &xdp_buff and &xdp_frame are bound in a way that

    xdp_buff->data_hard_start == xdp_frame

    It's always the case and e.g. xdp_convert_buff_to_frame() relies on
    this.
    IOW, the following:

            for (u32 i = 0; i < 0xdead; i++) {
                    xdpf = xdp_convert_buff_to_frame(&xdp);
                    xdp_convert_frame_to_buff(xdpf, &xdp);
            }

    shouldn't ever modify @xdpf's contents or the pointer itself.
    However, "live packet" code wrongly treats &xdp_frame as part of its
    context placed *before* the data_hard_start. With such flow,
    data_hard_start is sizeof(*xdpf) off to the right and no longer points
    to the XDP frame.

    Instead of replacing `sizeof(ctx)` with `offsetof(ctx, xdpf)` in several
    places and praying that there are no more miscalcs left somewhere in the
    code, unionize ::frm with ::data in a flex array, so that both starts
    pointing to the actual data_hard_start and the XDP frame actually starts
    being a part of it, i.e. a part of the headroom, not the context.
    A nice side effect is that the maximum frame size for this mode gets
    increased by 40 bytes, as xdp_buff::frame_sz includes everything from
    data_hard_start (-> includes xdpf already) to the end of XDP/skb shared
    info.
    Also update %MAX_PKT_SIZE accordingly in the selftests code. Leave it
    hardcoded for 64 bit && 4k pages, it can be made more flexible later on.

    Minor: align `&head->data` with how `head->frm` is assigned for
    consistency.
    Minor #2: rename 'frm' to 'frame' in &xdp_page_head while at it for
    clarity.

    (was found while testing XDP traffic generator on ice, which calls
     xdp_convert_frame_to_buff() for each XDP frame)

    Fixes: b530e9e1063e ("bpf: Add "live packet" mode for XDP in BPF_PROG_RUN")
    Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
    Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
    Link: https://lore.kernel.org/r/20230224163607.2994755-1-aleksander.lobakin@intel.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Petr Oros <poros@redhat.com>
2024-05-16 19:27:56 +02:00
Petr Oros a4f7d9c153 Revert "bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES"
JIRA: https://issues.redhat.com/browse/RHEL-31941

Upstream commit(s):
commit 181127fb76e62d06ab17a75fd610129688612343
Author: Martin KaFai Lau <martin.lau@kernel.org>
Date:   Fri Feb 17 12:13:09 2023 -0800

    Revert "bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES"

    This reverts commit 6c20822fada1b8adb77fa450d03a0d449686a4a9.

    build bot failed on arch with different cache line size:
    https://lore.kernel.org/bpf/50c35055-afa9-d01e-9a05-ea5351280e4f@intel.com/

    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Petr Oros <poros@redhat.com>
2024-05-16 19:27:56 +02:00
Petr Oros 241792d1b3 bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES
JIRA: https://issues.redhat.com/browse/RHEL-31941

Upstream commit(s):
commit 6c20822fada1b8adb77fa450d03a0d449686a4a9
Author: Alexander Lobakin <aleksander.lobakin@intel.com>
Date:   Wed Feb 15 19:54:40 2023 +0100

    bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES

    &xdp_buff and &xdp_frame are bound in a way that

    xdp_buff->data_hard_start == xdp_frame

    It's always the case and e.g. xdp_convert_buff_to_frame() relies on
    this.
    IOW, the following:

            for (u32 i = 0; i < 0xdead; i++) {
                    xdpf = xdp_convert_buff_to_frame(&xdp);
                    xdp_convert_frame_to_buff(xdpf, &xdp);
            }

    shouldn't ever modify @xdpf's contents or the pointer itself.
    However, "live packet" code wrongly treats &xdp_frame as part of its
    context placed *before* the data_hard_start. With such flow,
    data_hard_start is sizeof(*xdpf) off to the right and no longer points
    to the XDP frame.

    Instead of replacing `sizeof(ctx)` with `offsetof(ctx, xdpf)` in several
    places and praying that there are no more miscalcs left somewhere in the
    code, unionize ::frm with ::data in a flex array, so that both starts
    pointing to the actual data_hard_start and the XDP frame actually starts
    being a part of it, i.e. a part of the headroom, not the context.
    A nice side effect is that the maximum frame size for this mode gets
    increased by 40 bytes, as xdp_buff::frame_sz includes everything from
    data_hard_start (-> includes xdpf already) to the end of XDP/skb shared
    info.
    Also update %MAX_PKT_SIZE accordingly in the selftests code. Leave it
    hardcoded for 64 bit && 4k pages, it can be made more flexible later on.

    Minor: align `&head->data` with how `head->frm` is assigned for
    consistency.
    Minor #2: rename 'frm' to 'frame' in &xdp_page_head while at it for
    clarity.

    (was found while testing XDP traffic generator on ice, which calls
     xdp_convert_frame_to_buff() for each XDP frame)

    Fixes: b530e9e1063e ("bpf: Add "live packet" mode for XDP in BPF_PROG_RUN")
    Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
    Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
    Link: https://lore.kernel.org/r/20230215185440.4126672-1-aleksander.lobakin@intel.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Petr Oros <poros@redhat.com>
2024-05-16 19:27:56 +02:00
Lucas Zampieri 0fece7220e
Merge: CNB95: net: add <net/rps.h> and <net/netdev_rx_queue.h> headers
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/3991

JIRA: https://issues.redhat.com/browse/RHEL-31916
Tested: Just built... no real functional change

Commits:
```
1e5c647c3f6d ("rfs: annotate lockless accesses to sk->sk_rxhash")
5c3b74a92aa2 ("rfs: annotate lockless accesses to RFS sock flow table")
49e47a5b6145 ("net: move struct netdev_rx_queue out of netdevice.h")
490a79faf95e ("net: introduce include/net/rps.h")
```

Signed-off-by: Ivan Vecera <ivecera@redhat.com>

Approved-by: Sabrina Dubroca <sdubroca@redhat.com>
Approved-by: José Ignacio Tornos Martínez <jtornosm@redhat.com>

Merged-by: Lucas Zampieri <lzampier@redhat.com>
2024-04-17 10:14:55 -03:00
Ivan Vecera 139012e61c net: move struct netdev_rx_queue out of netdevice.h
JIRA: https://issues.redhat.com/browse/RHEL-31916

Conflicts:
* include/linux/netdevice.h
  Adjusted due to KABI reservations made by RHEL
  commit 3b3a52715a ("net: exclude BPF/XDP from kABI")

commit 49e47a5b6145d86c30022fe0e949bbb24bae28ba
Author: Jakub Kicinski <kuba@kernel.org>
Date:   Wed Aug 2 18:02:29 2023 -0700

    net: move struct netdev_rx_queue out of netdevice.h

    struct netdev_rx_queue is touched in only a few places
    and having it defined in netdevice.h brings in the dependency
    on xdp.h, because struct xdp_rxq_info gets embedded in
    struct netdev_rx_queue.

    In prep for removal of xdp.h from netdevice.h move all
    the netdev_rx_queue stuff to a new header.

    We could technically break the new header up to avoid
    the sysfs.h include but it's so rarely included it
    doesn't seem to be worth it at this point.

    Reviewed-by: Amritha Nambiar <amritha.nambiar@intel.com>
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>
    Acked-by: Jesper Dangaard Brouer <hawk@kernel.org>
    Link: https://lore.kernel.org/r/20230803010230.1755386-3-kuba@kernel.org
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Ivan Vecera <ivecera@redhat.com>
2024-04-05 16:03:26 +02:00
Artem Savkov 1e9cbbe0f6 bpf: Add __bpf_kfunc_{start,end}_defs macros
JIRA: https://issues.redhat.com/browse/RHEL-23643

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Conflicts: missing xdp commits, missing vma_task iterator

commit 391145ba2accc48b596f3d438af1a6255b62a555
Author: Dave Marchevsky <davemarchevsky@fb.com>
Date:   Tue Oct 31 14:56:24 2023 -0700

    bpf: Add __bpf_kfunc_{start,end}_defs macros

    BPF kfuncs are meant to be called from BPF programs. Accordingly, most
    kfuncs are not called from anywhere in the kernel, which the
    -Wmissing-prototypes warning is unhappy about. We've peppered
    __diag_ignore_all("-Wmissing-prototypes", ... everywhere kfuncs are
    defined in the codebase to suppress this warning.

    This patch adds two macros meant to bound one or many kfunc definitions.
    All existing kfunc definitions which use these __diag calls to suppress
    -Wmissing-prototypes are migrated to use the newly-introduced macros.
    A new __diag_ignore_all - for "-Wmissing-declarations" - is added to the
    __bpf_kfunc_start_defs macro based on feedback from Andrii on an earlier
    version of this patch [0] and another recent mailing list thread [1].

    In the future we might need to ignore different warnings or do other
    kfunc-specific things. This change will make it easier to make such
    modifications for all kfunc defs.

      [0]: https://lore.kernel.org/bpf/CAEf4BzaE5dRWtK6RPLnjTW-MW9sx9K3Fn6uwqCTChK2Dcb1Xig@mail.gmail.com/
      [1]: https://lore.kernel.org/bpf/ZT+2qCc%2FaXep0%2FLf@krava/

    Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Cc: Jiri Olsa <olsajiri@gmail.com>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: David Vernet <void@manifault.com>
    Acked-by: Yafang Shao <laoar.shao@gmail.com>
    Link: https://lore.kernel.org/r/20231031215625.2343848-1-davemarchevsky@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 11:23:42 +01:00
Scott Weaver 692770de9f Merge: BPF: update to 6.6
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/3461

Rebase BPF to 6.6

JIRA: https://issues.redhat.com/browse/RHEL-10691

Omitted-fix: 7ac5c53e0073 ("bpf: Use c->unit_size to select target cache during free")
    The issue only affects the SLAB allocator.

Omitted-fix: 8b2efe51ba85 ("bpf: Limit the number of uprobes when attaching program to multiple uprobes")
    The issue isn't critical. I already opened RHEL-21411 to make sure the fix isn't lost.

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>

Approved-by: Wander Lairson Costa <wander@redhat.com>
Approved-by: Jiri Benc <jbenc@redhat.com>
Approved-by: Viktor Malik <vmalik@redhat.com>
Approved-by: Artem Savkov <asavkov@redhat.com>

Signed-off-by: Scott Weaver <scweaver@redhat.com>
2024-01-12 13:40:43 -05:00
Jerome Marchand 7a2d165c35 bpf: Prevent inlining of bpf_fentry_test7()
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit 32337c0a28242f725c2c499c15100d67a4133050
Author: Yonghong Song <yonghong.song@linux.dev>
Date:   Sat Aug 26 13:08:43 2023 -0700

    bpf: Prevent inlining of bpf_fentry_test7()

    With latest clang18, I hit test_progs failures for the following test:

      #13/2    bpf_cookie/multi_kprobe_link_api:FAIL
      #13/3    bpf_cookie/multi_kprobe_attach_api:FAIL
      #13      bpf_cookie:FAIL
      #75      fentry_fexit:FAIL
      #76/1    fentry_test/fentry:FAIL
      #76      fentry_test:FAIL
      #80/1    fexit_test/fexit:FAIL
      #80      fexit_test:FAIL
      #110/1   kprobe_multi_test/skel_api:FAIL
      #110/2   kprobe_multi_test/link_api_addrs:FAIL
      #110/3   kprobe_multi_test/link_api_syms:FAIL
      #110/4   kprobe_multi_test/attach_api_pattern:FAIL
      #110/5   kprobe_multi_test/attach_api_addrs:FAIL
      #110/6   kprobe_multi_test/attach_api_syms:FAIL
      #110     kprobe_multi_test:FAIL

    For example, for #13/2, the error messages are:

      [...]
      kprobe_multi_test_run:FAIL:kprobe_test7_result unexpected kprobe_test7_result: actual 0 != expected 1
      [...]
      kprobe_multi_test_run:FAIL:kretprobe_test7_result unexpected kretprobe_test7_result: actual 0 != expected 1

    clang17 does not have this issue.

    Further investigation shows that kernel func bpf_fentry_test7(), used in
    the above tests, is inlined by the compiler although it is marked as
    noinline.

      int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg)
      {
            return (long)arg;
      }

    It is known that for simple functions like the above (e.g. just returning
    a constant or an input argument), the clang compiler may still do inlining
    for a noinline function. Adding 'asm volatile ("")' in the beginning of the
    bpf_fentry_test7() can prevent inlining.

    Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Tested-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/bpf/20230826200843.2210074-1-yonghong.song@linux.dev

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-15 09:29:02 +01:00
Jerome Marchand d84abd7ea6 selftests/bpf: add testcase for TRACING with 6+ arguments
JIRA: https://issues.redhat.com/browse/RHEL-10691

Conflicts: Context change from missing commit 65eb006d85a2 ("bpf: Move
kernel test kfuncs to bpf_testmod")

commit 5e9cf77d81f9ba51019157dc55a3b73dc89a7cf4
Author: Menglong Dong <imagedong@tencent.com>
Date:   Thu Jul 13 12:07:38 2023 +0800

    selftests/bpf: add testcase for TRACING with 6+ arguments

    Add fentry_many_args.c and fexit_many_args.c to test the fentry/fexit
    with 7/11 arguments. As this feature is not supported by arm64 yet, we
    disable these testcases for arm64 in DENYLIST.aarch64. We can combine
    them with fentry_test.c/fexit_test.c when arm64 is supported too.

    Correspondingly, add bpf_testmod_fentry_test7() and
    bpf_testmod_fentry_test11() to bpf_testmod.c

    Meanwhile, add bpf_modify_return_test2() to test_run.c to test the
    MODIFY_RETURN with 7 arguments.

    Add bpf_testmod_test_struct_arg_7/bpf_testmod_test_struct_arg_7 in
    bpf_testmod.c to test the struct in the arguments.

    And the testcases passed on x86_64:

    ./test_progs -t fexit
    Summary: 5/14 PASSED, 0 SKIPPED, 0 FAILED

    ./test_progs -t fentry
    Summary: 3/2 PASSED, 0 SKIPPED, 0 FAILED

    ./test_progs -t modify_return
    Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

    ./test_progs -t tracing_struct
    Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

    Signed-off-by: Menglong Dong <imagedong@tencent.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20230713040738.1789742-4-imagedong@tencent.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-15 09:28:52 +01:00
Jerome Marchand e6189eef06 selftests/bpf: Add test to exercise typedef walking
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit 2597a25cb865b17c1c9c2a0dcf4925e9d1a696bb
Author: Stanislav Fomichev <sdf@google.com>
Date:   Mon Jun 26 14:25:22 2023 -0700

    selftests/bpf: Add test to exercise typedef walking

    Add new bpf_fentry_test_sinfo with skb_shared_info argument and try to
    access frags.

    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20230626212522.2414485-2-sdf@google.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-14 15:22:23 +01:00
Petr Oros 8333fb8ac8 page_pool: split types and declarations from page_pool.h
JIRA: https://issues.redhat.com/browse/RHEL-16983

Conflicts:
- net/core/skbuff.c:
   adjusted context conflict due to missing 78476d315e1905 ("mctp: Add flow
   extension to skb")
- drivers/net/ethernet/hisilicon/hns3/hns3_enet.h:
   adjusted context conflict due to missing 87a9b2fd9288c5 ("net: hns3: add
   support for TX push mode")
- drivers/net/ethernet/mediatek/mtk_eth_soc.[c|h]
   Chunks ommited due to lack of page_pool support in driver. Missing
   upstream commit 23233e577ef973 ("net: ethernet: mtk_eth_soc: rely on
   page_pool for single page buffers")
- drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c
   adjusted context conflict due to missing 67f245c2ec0af1 ("mlx5:
   bpf_xdp_metadata_rx_hash add xdp rss hash type")
- drivers/net/ethernet/microsoft/mana/mana_en.c
   adjusted context conflict due to missing 92272ec4107ef4 ("eth: add
   missing xdp.h includes in drivers")
- drivers/net/veth.c
   Chunks ommited due to missing 0ebab78cbcbfd6 ("net: veth: add page_pool
   for page recycling")
- Unmerged path's (missing in rhel):
   drivers/net/ethernet/engleder/tsnep_main.c,
   drivers/net/ethernet/microchip/lan966x/lan966x_fdma.c,
   drivers/net/ethernet/microchip/lan966x/lan966x_main.h,
   drivers/net/ethernet/wangxun/libwx/wx_lib.c

Upstream commit(s):
commit a9ca9f9ceff382b58b488248f0c0da9e157f5d06
Author: Yunsheng Lin <linyunsheng@huawei.com>
Date:   Fri Aug 4 20:05:24 2023 +0200

    page_pool: split types and declarations from page_pool.h

    Split types and pure function declarations from page_pool.h
    and add them in page_page/types.h, so that C sources can
    include page_pool.h and headers should generally only include
    page_pool/types.h as suggested by jakub.
    Rename page_pool.h to page_pool/helpers.h to have both in
    one place.

    Signed-off-by: Yunsheng Lin <linyunsheng@huawei.com>
    Suggested-by: Jakub Kicinski <kuba@kernel.org>
    Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
    Reviewed-by: Alexander Duyck <alexanderduyck@fb.com>
    Link: https://lore.kernel.org/r/20230804180529.2483231-2-aleksander.lobakin@intel.com
    [Jakub: change microsoft/mana, fix kdoc paths in Documentation]
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Petr Oros <poros@redhat.com>
2023-11-30 19:11:24 +01:00
Ivan Vecera b4aa21f5ad net: introduce and use skb_frag_fill_page_desc()
JIRA: https://issues.redhat.com/browse/RHEL-12625

Conflicts:
* drivers/net/ethernet/freescale/enetc/enetc.c
- context due to missing 8feb020f92a5 ("net: ethernet: enetc: unlock
  XDP_REDIRECT for XDP non-linear buffers")
* drivers/net/ethernet/fungible/funeth/funeth_rx.c
  - removed hunk for non-existing file
* drivers/net/ethernet/marvell/mvneta.c
  - context due to missing 76a676947b56 ("net: mvneta: update frags bit
    before passing the xdp buffer to eBPF layer")
* drivers/net/ethernet/mellanox/mlx5/core/en_rx.c
  - adjusted due to missing 27602319e328 ("net/mlx5e: RX, Take shared
    info fragment addition into a function")

commit b51f4113ebb02011f0ca86abc3134b28d2071b6a
Author: Yunsheng Lin <linyunsheng@huawei.com>
Date:   Thu May 11 09:12:12 2023 +0800

    net: introduce and use skb_frag_fill_page_desc()

    Most users use __skb_frag_set_page()/skb_frag_off_set()/
    skb_frag_size_set() to fill the page desc for a skb frag.

    Introduce skb_frag_fill_page_desc() to do that.

    net/bpf/test_run.c does not call skb_frag_off_set() to
    set the offset, "copy_from_user(page_address(page), ...)"
    and 'shinfo' being part of the 'data' kzalloced in
    bpf_test_init() suggest that it is assuming offset to be
    initialized as zero, so call skb_frag_fill_page_desc()
    with offset being zero for this case.

    Also, skb_frag_set_page() is not used anymore, so remove
    it.

    Signed-off-by: Yunsheng Lin <linyunsheng@huawei.com>
    Reviewed-by: Leon Romanovsky <leonro@nvidia.com>
    Reviewed-by: Simon Horman <simon.horman@corigine.com>
    Signed-off-by: David S. Miller <davem@davemloft.net>

Signed-off-by: Ivan Vecera <ivecera@redhat.com>
2023-10-11 12:38:04 +02:00
Artem Savkov bf1a980461 bpf: add test_run support for netfilter program type
Bugzilla: https://bugzilla.redhat.com/2221599

commit 2b99ef22e0d237e08bfc437e7d051f78f352aeb2
Author: Florian Westphal <fw@strlen.de>
Date:   Fri Apr 21 19:02:59 2023 +0200

    bpf: add test_run support for netfilter program type
    
    add glue code so a bpf program can be run using userspace-provided
    netfilter state and packet/skb.
    
    Default is to use ipv4:output hook point, but this can be overridden by
    userspace.  Userspace provided netfilter state is restricted, only hook and
    protocol families can be overridden and only to ipv4/ipv6.
    
    Signed-off-by: Florian Westphal <fw@strlen.de>
    Link: https://lore.kernel.org/r/20230421170300.24115-7-fw@strlen.de
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:33 +02:00
Artem Savkov 69396a0fd0 bpf: Remove bpf_kfunc_call_test_kptr_get() test kfunc
Bugzilla: https://bugzilla.redhat.com/2221599

commit 09b501d905217a38f03c0f07d5a66e0b5c8c1644
Author: David Vernet <void@manifault.com>
Date:   Sun Apr 16 03:49:26 2023 -0500

    bpf: Remove bpf_kfunc_call_test_kptr_get() test kfunc
    
    We've managed to improve the UX for kptrs significantly over the last 9
    months. All of the prior main use cases, struct bpf_cpumask *, struct
    task_struct *, and struct cgroup *, have all been updated to be
    synchronized mainly using RCU. In other words, their KF_ACQUIRE kfunc
    calls are all KF_RCU, and the pointers themselves are MEM_RCU and can be
    accessed in an RCU read region in BPF.
    
    In a follow-on change, we'll be removing the KF_KPTR_GET kfunc flag.
    This patch prepares for that by removing the
    bpf_kfunc_call_test_kptr_get() kfunc, and all associated selftests.
    
    Signed-off-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/r/20230416084928.326135-2-void@manifault.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:30 +02:00
Artem Savkov d5e68d82f5 selftests/bpf: Add test to access u32 ptr argument in tracing program
Bugzilla: https://bugzilla.redhat.com/2221599

commit 75dcef8d3609d0b1d3497d6ed4809096513e0b83
Author: Feng Zhou <zhoufeng.zf@bytedance.com>
Date:   Mon Apr 10 16:59:08 2023 +0800

    selftests/bpf: Add test to access u32 ptr argument in tracing program
    
    Adding verifier test for accessing u32 pointer argument in
    tracing programs.
    
    The test program loads 1nd argument of bpf_fentry_test9 function
    which is u32 pointer and checks that verifier allows that.
    
    Co-developed-by: Chengming Zhou <zhouchengming@bytedance.com>
    Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
    Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/bpf/20230410085908.98493-3-zhoufeng.zf@bytedance.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:29 +02:00
Artem Savkov d5cf6bcc12 bpf: Remove now-unnecessary NULL checks for KF_RELEASE kfuncs
Bugzilla: https://bugzilla.redhat.com/2221599

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit fb2211a57c110b4ced3cb7f8570bd7246acf2d04
Author: David Vernet <void@manifault.com>
Date:   Sat Mar 25 16:31:45 2023 -0500

    bpf: Remove now-unnecessary NULL checks for KF_RELEASE kfuncs

    Now that we're not invoking kfunc destructors when the kptr in a map was
    NULL, we no longer require NULL checks in many of our KF_RELEASE kfuncs.
    This patch removes those NULL checks.

    Signed-off-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/r/20230325213144.486885-3-void@manifault.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:25 +02:00
Artem Savkov 59a436371a bpf: Treat KF_RELEASE kfuncs as KF_TRUSTED_ARGS
Bugzilla: https://bugzilla.redhat.com/2221599

commit 6c831c4684124a544f73f7c9b83bc7b2eb0b23d3
Author: David Vernet <void@manifault.com>
Date:   Sat Mar 25 16:31:46 2023 -0500

    bpf: Treat KF_RELEASE kfuncs as KF_TRUSTED_ARGS
    
    KF_RELEASE kfuncs are not currently treated as having KF_TRUSTED_ARGS,
    even though they have a superset of the requirements of KF_TRUSTED_ARGS.
    Like KF_TRUSTED_ARGS, KF_RELEASE kfuncs require a 0-offset argument, and
    don't allow NULL-able arguments. Unlike KF_TRUSTED_ARGS which require
    _either_ an argument with ref_obj_id > 0, _or_ (ref->type &
    BPF_REG_TRUSTED_MODIFIERS) (and no unsafe modifiers allowed), KF_RELEASE
    only allows for ref_obj_id > 0.  Because KF_RELEASE today doesn't
    automatically imply KF_TRUSTED_ARGS, some of these requirements are
    enforced in different ways that can make the behavior of the verifier
    feel unpredictable. For example, a KF_RELEASE kfunc with a NULL-able
    argument will currently fail in the verifier with a message like, "arg#0
    is ptr_or_null_ expected ptr_ or socket" rather than "Possibly NULL
    pointer passed to trusted arg0". Our intention is the same, but the
    semantics are different due to implemenetation details that kfunc authors
    and BPF program writers should not need to care about.
    
    Let's make the behavior of the verifier more consistent and intuitive by
    having KF_RELEASE kfuncs imply the presence of KF_TRUSTED_ARGS. Our
    eventual goal is to have all kfuncs assume KF_TRUSTED_ARGS by default
    anyways, so this takes us a step in that direction.
    
    Note that it does not make sense to assume KF_TRUSTED_ARGS for all
    KF_ACQUIRE kfuncs. KF_ACQUIRE kfuncs can have looser semantics than
    KF_RELEASE, with e.g. KF_RCU | KF_RET_NULL. We may want to have
    KF_ACQUIRE imply KF_TRUSTED_ARGS _unless_ KF_RCU is specified, but that
    can be left to another patch set, and there are no such subtleties to
    address for KF_RELEASE.
    
    Signed-off-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/r/20230325213144.486885-4-void@manifault.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:20 +02:00
Artem Savkov 7a5e1fbc36 bpf/selftests: Test fentry attachment to shadowed functions
Bugzilla: https://bugzilla.redhat.com/2221599

commit aa3d65de4b9004d799f97700751a86d3ebd7d5f9
Author: Viktor Malik <vmalik@redhat.com>
Date:   Fri Mar 10 08:41:00 2023 +0100

    bpf/selftests: Test fentry attachment to shadowed functions
    
    Adds a new test that tries to attach a program to fentry of two
    functions of the same name, one located in vmlinux and the other in
    bpf_testmod.
    
    To avoid conflicts with existing tests, a new function
    "bpf_fentry_shadow_test" was created both in vmlinux and in bpf_testmod.
    
    The previous commit fixed a bug which caused this test to fail. The
    verifier would always use the vmlinux function's address as the target
    trampoline address, hence trying to create two trampolines for a single
    address, which is forbidden.
    
    The test (similarly to other fentry/fexit tests) is not working on arm64
    at the moment.
    
    Signed-off-by: Viktor Malik <vmalik@redhat.com>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/5fe2f364190b6f79b085066ed7c5989c5bc475fa.1678432753.git.vmalik@redhat.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:17 +02:00
Artem Savkov 86812a862c bpf: Introduce kptr_rcu.
Bugzilla: https://bugzilla.redhat.com/2221599

Conflicts: issing commit 7f203bc89eb6 ("cgroup: Replace
cgroup->ancestor_ids[] with ->ancestors[]")

commit 20c09d92faeefb8536f705d3a4629e0dc314c8a1
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Thu Mar 2 20:14:43 2023 -0800

    bpf: Introduce kptr_rcu.

    The life time of certain kernel structures like 'struct cgroup' is protected by RCU.
    Hence it's safe to dereference them directly from __kptr tagged pointers in bpf maps.
    The resulting pointer is MEM_RCU and can be passed to kfuncs that expect KF_RCU.
    Derefrence of other kptr-s returns PTR_UNTRUSTED.

    For example:
    struct map_value {
       struct cgroup __kptr *cgrp;
    };

    SEC("tp_btf/cgroup_mkdir")
    int BPF_PROG(test_cgrp_get_ancestors, struct cgroup *cgrp_arg, const char *path)
    {
      struct cgroup *cg, *cg2;

      cg = bpf_cgroup_acquire(cgrp_arg); // cg is PTR_TRUSTED and ref_obj_id > 0
      bpf_kptr_xchg(&v->cgrp, cg);

      cg2 = v->cgrp; // This is new feature introduced by this patch.
      // cg2 is PTR_MAYBE_NULL | MEM_RCU.
      // When cg2 != NULL, it's a valid cgroup, but its percpu_ref could be zero

      if (cg2)
        bpf_cgroup_ancestor(cg2, level); // safe to do.
    }

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Tejun Heo <tj@kernel.org>
    Acked-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/bpf/20230303041446.3630-4-alexei.starovoitov@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:10 +02:00
Felix Maurer 25333e5eef bpf: Disable bh in bpf_test_run for xdp and tc prog
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2178930

commit af2d0d09eabe98b01bf02b236e381edae4209778
Author: Martin KaFai Lau <martin.lau@kernel.org>
Date:   Thu Feb 16 16:41:47 2023 -0800

    bpf: Disable bh in bpf_test_run for xdp and tc prog

    Some of the bpf helpers require bh disabled. eg. The bpf_fib_lookup
    helper that will be used in a latter selftest. In particular, it
    calls ___neigh_lookup_noref that expects the bh disabled.

    This patch disables bh before calling bpf_prog_run[_xdp], so
    the testing prog can also use those helpers.

    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20230217004150.2980689-2-martin.lau@linux.dev

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-06-14 10:44:26 +02:00
Felix Maurer 189bc40654 bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2178930

commit 294635a8165a31408a8b3a24f9c74849ca3d8701
Author: Alexander Lobakin <aleksander.lobakin@intel.com>
Date:   Fri Feb 24 17:36:07 2023 +0100

    bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES

    &xdp_buff and &xdp_frame are bound in a way that

    xdp_buff->data_hard_start == xdp_frame

    It's always the case and e.g. xdp_convert_buff_to_frame() relies on
    this.
    IOW, the following:

            for (u32 i = 0; i < 0xdead; i++) {
                    xdpf = xdp_convert_buff_to_frame(&xdp);
                    xdp_convert_frame_to_buff(xdpf, &xdp);
            }

    shouldn't ever modify @xdpf's contents or the pointer itself.
    However, "live packet" code wrongly treats &xdp_frame as part of its
    context placed *before* the data_hard_start. With such flow,
    data_hard_start is sizeof(*xdpf) off to the right and no longer points
    to the XDP frame.

    Instead of replacing `sizeof(ctx)` with `offsetof(ctx, xdpf)` in several
    places and praying that there are no more miscalcs left somewhere in the
    code, unionize ::frm with ::data in a flex array, so that both starts
    pointing to the actual data_hard_start and the XDP frame actually starts
    being a part of it, i.e. a part of the headroom, not the context.
    A nice side effect is that the maximum frame size for this mode gets
    increased by 40 bytes, as xdp_buff::frame_sz includes everything from
    data_hard_start (-> includes xdpf already) to the end of XDP/skb shared
    info.
    Also update %MAX_PKT_SIZE accordingly in the selftests code. Leave it
    hardcoded for 64 bit && 4k pages, it can be made more flexible later on.

    Minor: align `&head->data` with how `head->frm` is assigned for
    consistency.
    Minor #2: rename 'frm' to 'frame' in &xdp_page_head while at it for
    clarity.

    (was found while testing XDP traffic generator on ice, which calls
     xdp_convert_frame_to_buff() for each XDP frame)

    Fixes: b530e9e1063e ("bpf: Add "live packet" mode for XDP in BPF_PROG_RUN")
    Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
    Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
    Link: https://lore.kernel.org/r/20230224163607.2994755-1-aleksander.lobakin@intel.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-06-13 22:45:50 +02:00
Felix Maurer fdb9296e50 Revert "bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES"
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2178930

commit 181127fb76e62d06ab17a75fd610129688612343
Author: Martin KaFai Lau <martin.lau@kernel.org>
Date:   Fri Feb 17 12:13:09 2023 -0800

    Revert "bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES"

    This reverts commit 6c20822fada1b8adb77fa450d03a0d449686a4a9.

    build bot failed on arch with different cache line size:
    https://lore.kernel.org/bpf/50c35055-afa9-d01e-9a05-ea5351280e4f@intel.com/

    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-06-13 22:45:49 +02:00
Felix Maurer 91c181951d bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2178930

commit 6c20822fada1b8adb77fa450d03a0d449686a4a9
Author: Alexander Lobakin <aleksander.lobakin@intel.com>
Date:   Wed Feb 15 19:54:40 2023 +0100

    bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES

    &xdp_buff and &xdp_frame are bound in a way that

    xdp_buff->data_hard_start == xdp_frame

    It's always the case and e.g. xdp_convert_buff_to_frame() relies on
    this.
    IOW, the following:

            for (u32 i = 0; i < 0xdead; i++) {
                    xdpf = xdp_convert_buff_to_frame(&xdp);
                    xdp_convert_frame_to_buff(xdpf, &xdp);
            }

    shouldn't ever modify @xdpf's contents or the pointer itself.
    However, "live packet" code wrongly treats &xdp_frame as part of its
    context placed *before* the data_hard_start. With such flow,
    data_hard_start is sizeof(*xdpf) off to the right and no longer points
    to the XDP frame.

    Instead of replacing `sizeof(ctx)` with `offsetof(ctx, xdpf)` in several
    places and praying that there are no more miscalcs left somewhere in the
    code, unionize ::frm with ::data in a flex array, so that both starts
    pointing to the actual data_hard_start and the XDP frame actually starts
    being a part of it, i.e. a part of the headroom, not the context.
    A nice side effect is that the maximum frame size for this mode gets
    increased by 40 bytes, as xdp_buff::frame_sz includes everything from
    data_hard_start (-> includes xdpf already) to the end of XDP/skb shared
    info.
    Also update %MAX_PKT_SIZE accordingly in the selftests code. Leave it
    hardcoded for 64 bit && 4k pages, it can be made more flexible later on.

    Minor: align `&head->data` with how `head->frm` is assigned for
    consistency.
    Minor #2: rename 'frm' to 'frame' in &xdp_page_head while at it for
    clarity.

    (was found while testing XDP traffic generator on ice, which calls
     xdp_convert_frame_to_buff() for each XDP frame)

    Fixes: b530e9e1063e ("bpf: Add "live packet" mode for XDP in BPF_PROG_RUN")
    Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
    Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
    Link: https://lore.kernel.org/r/20230215185440.4126672-1-aleksander.lobakin@intel.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-06-13 22:45:49 +02:00
Viktor Malik 3a0ff46ce3 selftests/bpf: Add testcase for static kfunc with unused arg
Bugzilla: https://bugzilla.redhat.com/2178930

commit 6aed15e330bfec6a423f40582b2a8b53d9ce1757
Author: David Vernet <void@manifault.com>
Date:   Wed Feb 1 11:30:16 2023 -0600

    selftests/bpf: Add testcase for static kfunc with unused arg
    
    kfuncs are allowed to be static, or not use one or more of their
    arguments. For example, bpf_xdp_metadata_rx_hash() in net/core/xdp.c is
    meant to be implemented by drivers, with the default implementation just
    returning -EOPNOTSUPP. As described in [0], such kfuncs can have their
    arguments elided, which can cause BTF encoding to be skipped. The new
    __bpf_kfunc macro should address this, and this patch adds a selftest
    which verifies that a static kfunc with at least one unused argument can
    still be encoded and invoked by a BPF program.
    
    Signed-off-by: David Vernet <void@manifault.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20230201173016.342758-5-void@manifault.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:45:20 +02:00
Viktor Malik 23c9904275 bpf: Add __bpf_kfunc tag to all kfuncs
Bugzilla: https://bugzilla.redhat.com/2178930

commit 400031e05adfcef9e80eca80bdfc3f4b63658be4
Author: David Vernet <void@manifault.com>
Date:   Wed Feb 1 11:30:15 2023 -0600

    bpf: Add __bpf_kfunc tag to all kfuncs

    Now that we have the __bpf_kfunc tag, we should use add it to all
    existing kfuncs to ensure that they'll never be elided in LTO builds.

    Signed-off-by: David Vernet <void@manifault.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/bpf/20230201173016.342758-4-void@manifault.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:45:20 +02:00
Felix Maurer 58671712a5 bpf: XDP metadata RX kfuncs
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2178930
Conflicts:
- include/linux/netdevice.h: Context difference due to missing 97dc7cd92ac6
  ("ptp: Support late timestamp determination")

commit 3d76a4d3d4e591af3e789698affaad88a5a8e8ab
Author: Stanislav Fomichev <sdf@google.com>
Date:   Thu Jan 19 14:15:26 2023 -0800

    bpf: XDP metadata RX kfuncs

    Define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible
    XDP metatada kfuncs. Not all devices have to implement them. If kfunc is not
    supported by the target device, the default implementation is called instead.
    The verifier, at load time, replaces a call to the generic kfunc with a call
    to the per-device one. Per-device kfunc pointers are stored in separate
    struct xdp_metadata_ops.

    Cc: John Fastabend <john.fastabend@gmail.com>
    Cc: David Ahern <dsahern@gmail.com>
    Cc: Martin KaFai Lau <martin.lau@linux.dev>
    Cc: Jakub Kicinski <kuba@kernel.org>
    Cc: Willem de Bruijn <willemb@google.com>
    Cc: Jesper Dangaard Brouer <brouer@redhat.com>
    Cc: Anatoly Burakov <anatoly.burakov@intel.com>
    Cc: Alexander Lobakin <alexandr.lobakin@intel.com>
    Cc: Magnus Karlsson <magnus.karlsson@gmail.com>
    Cc: Maryam Tahhan <mtahhan@redhat.com>
    Cc: xdp-hints@xdp-project.net
    Cc: netdev@vger.kernel.org
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/r/20230119221536.3349901-8-sdf@google.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-06-13 22:45:14 +02:00
Viktor Malik 5ab30f543d selftests/bpf: Add a sign-extension test for kfuncs
Bugzilla: https://bugzilla.redhat.com/2178930

commit be6b5c10ecc4014446e5c807d6a69c5a7cc1c497
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Sat Jan 28 01:06:33 2023 +0100

    selftests/bpf: Add a sign-extension test for kfuncs
    
    s390x ABI requires the caller to zero- or sign-extend the arguments.
    eBPF already deals with zero-extension (by definition of its ABI), but
    not with sign-extension.
    
    Add a test to cover that potentially problematic area.
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Link: https://lore.kernel.org/r/20230128000650.1516334-15-iii@linux.ibm.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:44:59 +02:00
Jiri Benc 4c362dc0ad skbuff: Introduce slab_build_skb()
Bugzilla: https://bugzilla.redhat.com/2177177

Conflicts:
- Only the networking core changes backported. Drivers will be updated
  at their own pace.
- Removed WARN_ONCE on old API usage to allow gradual change of drivers.

Omitted-fix: 8c495270845d ("bnx2x: use the right build_skb() helper")

commit ce098da1497c6dee9589fce2c61d1910f4fcf0e7
Author: Kees Cook <keescook@chromium.org>
Date:   Wed Dec 7 22:02:59 2022 -0800

    skbuff: Introduce slab_build_skb()

    syzkaller reported:

      BUG: KASAN: slab-out-of-bounds in __build_skb_around+0x235/0x340 net/core/skbuff.c:294
      Write of size 32 at addr ffff88802aa172c0 by task syz-executor413/5295

    For bpf_prog_test_run_skb(), which uses a kmalloc()ed buffer passed to
    build_skb().

    When build_skb() is passed a frag_size of 0, it means the buffer came
    from kmalloc. In these cases, ksize() is used to find its actual size,
    but since the allocation may not have been made to that size, actually
    perform the krealloc() call so that all the associated buffer size
    checking will be correctly notified (and use the "new" pointer so that
    compiler hinting works correctly). Split this logic out into a new
    interface, slab_build_skb(), but leave the original 0 checking for now
    to catch any stragglers.

    Reported-by: syzbot+fda18eaa8c12534ccb3b@syzkaller.appspotmail.com
    Link: https://groups.google.com/g/syzkaller-bugs/c/UnIKxTtU5-0/m/-wbXinkgAQAJ
    Fixes: 38931d8989b5 ("mm: Make ksize() a reporting-only function")
    Cc: Pavel Begunkov <asml.silence@gmail.com>
    Cc: pepsipu <soopthegoop@gmail.com>
    Cc: syzbot+fda18eaa8c12534ccb3b@syzkaller.appspotmail.com
    Cc: Vlastimil Babka <vbabka@suse.cz>
    Cc: kasan-dev <kasan-dev@googlegroups.com>
    Cc: Andrii Nakryiko <andrii@kernel.org>
    Cc: ast@kernel.org
    Cc: Daniel Borkmann <daniel@iogearbox.net>
    Cc: Hao Luo <haoluo@google.com>
    Cc: Jesper Dangaard Brouer <hawk@kernel.org>
    Cc: John Fastabend <john.fastabend@gmail.com>
    Cc: jolsa@kernel.org
    Cc: KP Singh <kpsingh@kernel.org>
    Cc: martin.lau@linux.dev
    Cc: Stanislav Fomichev <sdf@google.com>
    Cc: song@kernel.org
    Cc: Yonghong Song <yhs@fb.com>
    Signed-off-by: Kees Cook <keescook@chromium.org>
    Link: https://lore.kernel.org/r/20221208060256.give.994-kees@kernel.org
    Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Signed-off-by: Jiri Benc <jbenc@redhat.com>
2023-04-28 11:43:23 +02:00
Jerome Marchand 6af9035ad8 bpf: do not rely on ALLOW_ERROR_INJECTION for fmod_ret
Bugzilla: https://bugzilla.redhat.com/2177177

commit 5b481acab4ce017fda8166fa9428511da41109e5
Author: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Date:   Tue Dec 6 15:59:32 2022 +0100

    bpf: do not rely on ALLOW_ERROR_INJECTION for fmod_ret
    
    The current way of expressing that a non-bpf kernel component is willing
    to accept that bpf programs can be attached to it and that they can change
    the return value is to abuse ALLOW_ERROR_INJECTION.
    This is debated in the link below, and the result is that it is not a
    reasonable thing to do.
    
    Reuse the kfunc declaration structure to also tag the kernel functions
    we want to be fmodret. This way we can control from any subsystem which
    functions are being modified by bpf without touching the verifier.
    
    Link: https://lore.kernel.org/all/20221121104403.1545f9b5@gandalf.local.home/
    Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
    Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
    Acked-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/r/20221206145936.922196-2-benjamin.tissoires@redhat.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:16 +02:00
Felix Maurer 45b2a79767 bpf, test_run: Propagate bpf_flow_dissect's retval to user's bpf_attr.test.retval
Bugzilla: https://bugzilla.redhat.com/2166911

commit 5deedfbee84278da3b76fb7176dc3742f56eb370
Author: Shmulik Ladkani <shmulik.ladkani@gmail.com>
Date:   Sun Aug 21 14:35:18 2022 +0300

    bpf, test_run: Propagate bpf_flow_dissect's retval to user's bpf_attr.test.retval
    
    Formerly, a boolean denoting whether bpf_flow_dissect returned BPF_OK
    was set into 'bpf_attr.test.retval'.
    
    Augment this, so users can check the actual return code of the dissector
    program under test.
    
    Existing prog_tests/flow_dissector*.c tests were correspondingly changed
    to check against each test's expected retval.
    
    Also, tests' resulting 'flow_keys' are verified only in case the expected
    retval is BPF_OK. This allows adding new tests that expect non BPF_OK.
    
    Signed-off-by: Shmulik Ladkani <shmulik.ladkani@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Stanislav Fomichev <sdf@google.com>
    Acked-by: John Fastabend <john.fastabend@gmail.com>
    Link: https://lore.kernel.org/bpf/20220821113519.116765-4-shmulik.ladkani@gmail.com

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-03-06 14:54:33 +01:00
Felix Maurer e10f4efe42 flow_dissector: Make 'bpf_flow_dissect' return the bpf program retcode
Bugzilla: https://bugzilla.redhat.com/2166911

commit 0ba985024ae7db226776725d9aa436b5c1c9fca2
Author: Shmulik Ladkani <shmulik.ladkani@gmail.com>
Date:   Sun Aug 21 14:35:16 2022 +0300

    flow_dissector: Make 'bpf_flow_dissect' return the bpf program retcode
    
    Let 'bpf_flow_dissect' callers know the BPF program's retcode and act
    accordingly.
    
    Signed-off-by: Shmulik Ladkani <shmulik.ladkani@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Stanislav Fomichev <sdf@google.com>
    Acked-by: John Fastabend <john.fastabend@gmail.com>
    Link: https://lore.kernel.org/bpf/20220821113519.116765-2-shmulik.ladkani@gmail.com

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-03-06 14:54:33 +01:00
Artem Savkov 2bdb0839c6 bpf, test_run: Fix alignment problem in bpf_prog_test_run_skb()
Bugzilla: https://bugzilla.redhat.com/2166911

commit d3fd203f36d46aa29600a72d57a1b61af80e4a25
Author: Baisong Zhong <zhongbaisong@huawei.com>
Date:   Wed Nov 2 16:16:20 2022 +0800

    bpf, test_run: Fix alignment problem in bpf_prog_test_run_skb()
    
    We got a syzkaller problem because of aarch64 alignment fault
    if KFENCE enabled. When the size from user bpf program is an odd
    number, like 399, 407, etc, it will cause the struct skb_shared_info's
    unaligned access. As seen below:
    
      BUG: KFENCE: use-after-free read in __skb_clone+0x23c/0x2a0 net/core/skbuff.c:1032
    
      Use-after-free read at 0xffff6254fffac077 (in kfence-#213):
       __lse_atomic_add arch/arm64/include/asm/atomic_lse.h:26 [inline]
       arch_atomic_add arch/arm64/include/asm/atomic.h:28 [inline]
       arch_atomic_inc include/linux/atomic-arch-fallback.h:270 [inline]
       atomic_inc include/asm-generic/atomic-instrumented.h:241 [inline]
       __skb_clone+0x23c/0x2a0 net/core/skbuff.c:1032
       skb_clone+0xf4/0x214 net/core/skbuff.c:1481
       ____bpf_clone_redirect net/core/filter.c:2433 [inline]
       bpf_clone_redirect+0x78/0x1c0 net/core/filter.c:2420
       bpf_prog_d3839dd9068ceb51+0x80/0x330
       bpf_dispatcher_nop_func include/linux/bpf.h:728 [inline]
       bpf_test_run+0x3c0/0x6c0 net/bpf/test_run.c:53
       bpf_prog_test_run_skb+0x638/0xa7c net/bpf/test_run.c:594
       bpf_prog_test_run kernel/bpf/syscall.c:3148 [inline]
       __do_sys_bpf kernel/bpf/syscall.c:4441 [inline]
       __se_sys_bpf+0xad0/0x1634 kernel/bpf/syscall.c:4381
    
      kfence-#213: 0xffff6254fffac000-0xffff6254fffac196, size=407, cache=kmalloc-512
    
      allocated by task 15074 on cpu 0 at 1342.585390s:
       kmalloc include/linux/slab.h:568 [inline]
       kzalloc include/linux/slab.h:675 [inline]
       bpf_test_init.isra.0+0xac/0x290 net/bpf/test_run.c:191
       bpf_prog_test_run_skb+0x11c/0xa7c net/bpf/test_run.c:512
       bpf_prog_test_run kernel/bpf/syscall.c:3148 [inline]
       __do_sys_bpf kernel/bpf/syscall.c:4441 [inline]
       __se_sys_bpf+0xad0/0x1634 kernel/bpf/syscall.c:4381
       __arm64_sys_bpf+0x50/0x60 kernel/bpf/syscall.c:4381
    
    To fix the problem, we adjust @size so that (@size + @hearoom) is a
    multiple of SMP_CACHE_BYTES. So we make sure the struct skb_shared_info
    is aligned to a cache line.
    
    Fixes: 1cf1cae963 ("bpf: introduce BPF_PROG_TEST_RUN command")
    Signed-off-by: Baisong Zhong <zhongbaisong@huawei.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Cc: Eric Dumazet <edumazet@google.com>
    Link: https://lore.kernel.org/bpf/20221102081620.1465154-1-zhongbaisong@huawei.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:22 +01:00
Artem Savkov 8d92f8ef2d selftests/bpf: Add tests for kfunc returning a memory pointer
Bugzilla: https://bugzilla.redhat.com/2166911

commit 22ed8d5a46520ef0f060e7c0ee91f1cc6f684400
Author: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Date:   Tue Sep 6 17:13:03 2022 +0200

    selftests/bpf: Add tests for kfunc returning a memory pointer
    
    We add 2 new kfuncs that are following the RET_PTR_TO_MEM
    capability from the previous commit.
    Then we test them in selftests:
    the first tests are testing valid case, and are not failing,
    and the later ones are actually preventing the program to be loaded
    because they are wrong.
    
    To work around that, we mark the failing ones as not autoloaded
    (with SEC("?tc")), and we manually enable them one by one, ensuring
    the verifier rejects them.
    
    Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
    Link: https://lore.kernel.org/r/20220906151303.2780789-8-benjamin.tissoires@redhat.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:10 +01:00
Artem Savkov cbacbe3dff selftests/bpf: add test for accessing ctx from syscall program type
Bugzilla: https://bugzilla.redhat.com/2166911

commit fb66223a244f252273995557b23e0fa53092e92c
Author: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Date:   Tue Sep 6 17:13:00 2022 +0200

    selftests/bpf: add test for accessing ctx from syscall program type
    
    We need to also export the kfunc set to the syscall program type,
    and then add a couple of eBPF programs that are testing those calls.
    
    The first one checks for valid access, and the second one is OK
    from a static analysis point of view but fails at run time because
    we are trying to access outside of the allocated memory.
    
    Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
    Link: https://lore.kernel.org/r/20220906151303.2780789-5-benjamin.tissoires@redhat.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:10 +01:00
Artem Savkov aa1482f22a selftests/bpf: add destructive kfunc test
Bugzilla: https://bugzilla.redhat.com/2166911

commit e338945816754a1c362f606b8e2029f2c023e51c
Author: Artem Savkov <asavkov@redhat.com>
Date:   Wed Aug 10 08:59:05 2022 +0200

    selftests/bpf: add destructive kfunc test
    
    Add a test checking that programs calling destructive kfuncs can only do
    so if they have CAP_SYS_BOOT capabilities.
    
    Signed-off-by: Artem Savkov <asavkov@redhat.com>
    Link: https://lore.kernel.org/r/20220810065905.475418-4-asavkov@redhat.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:00 +01:00
Artem Savkov 0a46cc5e87 bpf: Allow calling bpf_prog_test kfuncs in tracing programs
Bugzilla: https://bugzilla.redhat.com/2137876

commit 1f0752628e76bb3a02109dbd980381f27060ba92
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Tue Aug 9 23:30:31 2022 +0200

    bpf: Allow calling bpf_prog_test kfuncs in tracing programs
    
    In addition to TC hook, enable these in tracing programs so that they
    can be used in selftests.
    
    Acked-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/r/20220809213033.24147-2-memxor@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:46 +01:00
Artem Savkov d581951eb0 bpf: Add support for forcing kfunc args to be trusted
Bugzilla: https://bugzilla.redhat.com/2137876

commit 56e948ffc098a780fefb6c1784a3a2c7b81100a1
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Thu Jul 21 15:42:36 2022 +0200

    bpf: Add support for forcing kfunc args to be trusted
    
    Teach the verifier to detect a new KF_TRUSTED_ARGS kfunc flag, which
    means each pointer argument must be trusted, which we define as a
    pointer that is referenced (has non-zero ref_obj_id) and also needs to
    have its offset unchanged, similar to how release functions expect their
    argument. This allows a kfunc to receive pointer arguments unchanged
    from the result of the acquire kfunc.
    
    This is required to ensure that kfunc that operate on some object only
    work on acquired pointers and not normal PTR_TO_BTF_ID with same type
    which can be obtained by pointer walking. The restrictions applied to
    release arguments also apply to trusted arguments. This implies that
    strict type matching (not deducing type by recursively following members
    at offset) and OBJ_RELEASE offset checks (ensuring they are zero) are
    used for trusted pointer arguments.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/r/20220721134245.2450-5-memxor@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:42 +01:00
Artem Savkov dbfa384357 bpf: Switch to new kfunc flags infrastructure
Bugzilla: https://bugzilla.redhat.com/2137876

commit a4703e3184320d6e15e2bc81d2ccf1c8c883f9d1
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Thu Jul 21 15:42:35 2022 +0200

    bpf: Switch to new kfunc flags infrastructure
    
    Instead of populating multiple sets to indicate some attribute and then
    researching the same BTF ID in them, prepare a single unified BTF set
    which indicates whether a kfunc is allowed to be called, and also its
    attributes if any at the same time. Now, only one call is needed to
    perform the lookup for both kfunc availability and its attributes.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/r/20220721134245.2450-4-memxor@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:42 +01:00
Felix Maurer a2fc436a09 bpf: Move skb->len == 0 checks into __bpf_redirect
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2137876

commit 114039b342014680911c35bd6b72624180fd669a
Author: Stanislav Fomichev <sdf@google.com>
Date:   Mon Nov 21 10:03:39 2022 -0800

    bpf: Move skb->len == 0 checks into __bpf_redirect

    To avoid potentially breaking existing users.

    Both mac/no-mac cases have to be amended; mac_header >= network_header
    is not enough (verified with a new test, see next patch).

    Fixes: fd1894224407 ("bpf: Don't redirect packets with invalid pkt_len")
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/r/20221121180340.1983627-1-sdf@google.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-01-05 15:46:40 +01:00
Felix Maurer 36840e1543 bpf: Don't redirect packets with invalid pkt_len
Bugzilla: https://bugzilla.redhat.com/2137876
Conflicts: We only take the fix in net/bpf/test_run.c, because the other
changes caused regressions all over the stack. We rely on these changes
being present upstream and bugs found with them being fixed there.

commit fd1894224407c484f652ad456e1ce423e89bb3eb
Author: Zhengchao Shao <shaozhengchao@huawei.com>
Date:   Fri Jul 15 19:55:59 2022 +0800

    bpf: Don't redirect packets with invalid pkt_len

    Syzbot found an issue [1]: fq_codel_drop() try to drop a flow whitout any
    skbs, that is, the flow->head is null.
    The root cause, as the [2] says, is because that bpf_prog_test_run_skb()
    run a bpf prog which redirects empty skbs.
    So we should determine whether the length of the packet modified by bpf
    prog or others like bpf_prog_test is valid before forwarding it directly.

    LINK: [1] https://syzkaller.appspot.com/bug?id=0b84da80c2917757915afa89f7738a9d16ec96c5
    LINK: [2] https://www.spinics.net/lists/netdev/msg777503.html

    Reported-by: syzbot+7a12909485b94426aceb@syzkaller.appspotmail.com
    Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
    Reviewed-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/r/20220715115559.139691-1-shaozhengchao@huawei.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
2023-01-05 15:46:40 +01:00
Artem Savkov 6c02dd8089 bpf, test_run: Remove unnecessary prog type checks
Bugzilla: https://bugzilla.redhat.com/2137876

commit 988d0d5899248b758d2f2eae3b57708fe78a8618
Author: Daniel Xu <dxu@dxuuu.xyz>
Date:   Sun May 29 15:15:41 2022 -0500

    bpf, test_run: Remove unnecessary prog type checks
    
    These checks were effectively noops b/c there's only one way these
    functions get called: through prog_ops dispatching. And since there's no
    other callers, we can be sure that `prog` is always the correct type.
    
    Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/0a9aaac329f76ddb17df1786b001117823ffefa5.1653855302.git.dxu@dxuuu.xyz

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:29 +01:00
Yauheni Kaliuta ed4c742acc bpf: Prepare prog_test_struct kfuncs for runtime tests
Bugzilla: https://bugzilla.redhat.com/2120968

commit 5cdccadcac2612f947ebc26ad7023dfb7e8871f9
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Thu May 12 01:16:52 2022 +0530

    bpf: Prepare prog_test_struct kfuncs for runtime tests
    
    In an effort to actually test the refcounting logic at runtime, add a
    refcount_t member to prog_test_ref_kfunc and use it in selftests to
    verify and test the whole logic more exhaustively.
    
    The kfunc calls for prog_test_member do not require runtime refcounting,
    as they are only used for verifier selftests, not during runtime
    execution. Hence, their implementation now has a WARN_ON_ONCE as it is
    not meant to be reachable code at runtime. It is strictly used in tests
    triggering failure cases in the verifier. bpf_kfunc_call_memb_release is
    called from map free path, since prog_test_member is embedded in map
    value for some verifier tests, so we skip WARN_ON_ONCE for it.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/r/20220511194654.765705-3-memxor@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:03 +02:00