Commit Graph

24 Commits

Author SHA1 Message Date
Jerome Marchand 4355310b03 scripts/bpf_doc: Use silent mode when exec make cmd
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 5384cc0d1a88c27448a6a4e65b8abe6486de8012
Author: Hangbin Liu <liuhangbin@gmail.com>
Date:   Fri Mar 15 10:34:43 2024 +0800

    scripts/bpf_doc: Use silent mode when exec make cmd

    When getting kernel version via make, the result may be polluted by other
    output, like directory change info. e.g.

      $ export MAKEFLAGS="-w"
      $ make kernelversion
      make: Entering directory '/home/net'
      6.8.0
      make: Leaving directory '/home/net'

    This will distort the reStructuredText output and make latter rst2man
    failed like:

      [...]
      bpf-helpers.rst:20: (WARNING/2) Field list ends without a blank line; unexpected unindent.
      [...]

    Using silent mode would help. e.g.

      $ make -s --no-print-directory kernelversion
      6.8.0

    Fixes: fd0a38f9c37d ("scripts/bpf: Set version attribute for bpf-helpers(7) man page")
    Signed-off-by: Michael Hofmann <mhofmann@redhat.com>
    Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <qmo@kernel.org>
    Acked-by: Alejandro Colomar <alx@kernel.org>
    Link: https://lore.kernel.org/bpf/20240315023443.2364442-1-liuhangbin@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:16 +02:00
Jerome Marchand 678fc8a036 bpf: Generate const static pointers for kernel helpers
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit ff2071a7b7fd77908417603c4a785822939b3841
Author: Jose E. Marchesi <jose.marchesi@oracle.com>
Date:   Sat Jan 27 19:50:31 2024 +0100

    bpf: Generate const static pointers for kernel helpers

    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: Jose E. Marchesi <jose.marchesi@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/bpf/20240127185031.29854-1-jose.marchesi@oracle.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:05 +02:00
Viktor Malik f5a31e349f
bpf, scripts: Correct GPL license name
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit e37243b65d528a8a9f8b9a57a43885f8e8dfc15c
Author: Gianmarco Lusvardi <glusvardi@posteo.net>
Date:   Tue Feb 13 23:05:46 2024 +0000

    bpf, scripts: Correct GPL license name
    
    The bpf_doc script refers to the GPL as the "GNU Privacy License".
    I strongly suspect that the author wanted to refer to the GNU General
    Public License, under which the Linux kernel is released, as, to the
    best of my knowledge, there is no license named "GNU Privacy License".
    This patch corrects the license name in the script accordingly.
    
    Fixes: 56a092c895 ("bpf: add script and prepare bpf.h for new helpers documentation")
    Signed-off-by: Gianmarco Lusvardi <glusvardi@posteo.net>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20240213230544.930018-3-glusvardi@posteo.net

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 11:07:44 +02:00
Jerome Marchand 094290625a bpf, docs: Fix invalid escape sequence warnings in bpf_doc.py
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit 121fd33bf2d99007f8fe2a155c291a30baca3f52
Author: Vishal Chourasia <vishalc@linux.ibm.com>
Date:   Tue Aug 29 13:19:31 2023 +0530

    bpf, docs: Fix invalid escape sequence warnings in bpf_doc.py

    The script bpf_doc.py generates multiple SyntaxWarnings related to invalid
    escape sequences when executed with Python 3.12. These warnings do not appear
    in Python 3.10 and 3.11 and do not affect the kernel build, which completes
    successfully.

    This patch resolves these SyntaxWarnings by converting the relevant string
    literals to raw strings or by escaping backslashes. This ensures that
    backslashes are interpreted as literal characters, eliminating the warnings.

    Reported-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
    Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Tested-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20230829074931.2511204-1-vishalc@linux.ibm.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-15 09:29:02 +01:00
Artem Savkov 0f90cca2b9 bpf: Remove extra whitespace in SPDX tag for syscall/helpers man pages
Bugzilla: https://bugzilla.redhat.com/2221599

commit eafa92152e2ec6318e32b6ddda9c1d95d161000a
Author: Alejandro Colomar <alx@kernel.org>
Date:   Tue Apr 11 15:47:47 2023 +0100

    bpf: Remove extra whitespace in SPDX tag for syscall/helpers man pages
    
    There is an extra whitespace in the SPDX tag, before the license name,
    in the script for generating man pages for the bpf() syscall and the
    helpers. It has caused problems in Debian packaging, in the tool that
    autodetects licenses. Let's clean it up.
    
    Fixes: 5cb62b7598f2 ("bpf, docs: Use SPDX license identifier in bpf_doc.py")
    Signed-off-by: Alejandro Colomar <alx@kernel.org>
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20230411144747.66734-1-quentin@isovalent.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:27 +02:00
Jerome Marchand 7233b4a862 bpf_doc: Fix build error with older python versions
Bugzilla: https://bugzilla.redhat.com/2177177

commit 5fbea42387eba1c7517fcad79099df706def7054
Author: Michal Suchanek <msuchanek@suse.de>
Date:   Mon Jan 9 12:34:42 2023 +0100

    bpf_doc: Fix build error with older python versions

    The ability to subscript match result as an array is only available
    since python 3.6. Existing code in bpf_doc uses the older group()
    interface but commit 8a76145a2ec2 adds code using the new interface.

    Use the old interface consistently to avoid build error on older
    distributions like the below:

    + make -j48 -s -C /dev/shm/kbuild/linux.33946/current ARCH=powerpc HOSTCC=gcc CROSS_COMPILE=powerpc64-suse-linux- clean
    TypeError: '_sre.SRE_Match' object is not subscriptable

    Fixes: 8a76145a2ec2 ("bpf: explicitly define BPF_FUNC_xxx integer values")
    Signed-off-by: Michal Suchanek <msuchanek@suse.de>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20230109113442.20946-1-msuchanek@suse.de

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:20 +02:00
Jerome Marchand a59af7f5dd bpf: Rework process_dynptr_func
Bugzilla: https://bugzilla.redhat.com/2177177

commit 270605317366e4535d8d9fc3d9da1ad0fb3c9d45
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Thu Dec 8 02:11:37 2022 +0530

    bpf: Rework process_dynptr_func
    
    Recently, user ringbuf support introduced a PTR_TO_DYNPTR register type
    for use in callback state, because in case of user ringbuf helpers,
    there is no dynptr on the stack that is passed into the callback. To
    reflect such a state, a special register type was created.
    
    However, some checks have been bypassed incorrectly during the addition
    of this feature. First, for arg_type with MEM_UNINIT flag which
    initialize a dynptr, they must be rejected for such register type.
    Secondly, in the future, there are plans to add dynptr helpers that
    operate on the dynptr itself and may change its offset and other
    properties.
    
    In all of these cases, PTR_TO_DYNPTR shouldn't be allowed to be passed
    to such helpers, however the current code simply returns 0.
    
    The rejection for helpers that release the dynptr is already handled.
    
    For fixing this, we take a step back and rework existing code in a way
    that will allow fitting in all classes of helpers and have a coherent
    model for dealing with the variety of use cases in which dynptr is used.
    
    First, for ARG_PTR_TO_DYNPTR, it can either be set alone or together
    with a DYNPTR_TYPE_* constant that denotes the only type it accepts.
    
    Next, helpers which initialize a dynptr use MEM_UNINIT to indicate this
    fact. To make the distinction clear, use MEM_RDONLY flag to indicate
    that the helper only operates on the memory pointed to by the dynptr,
    not the dynptr itself. In C parlance, it would be equivalent to taking
    the dynptr as a point to const argument.
    
    When either of these flags are not present, the helper is allowed to
    mutate both the dynptr itself and also the memory it points to.
    Currently, the read only status of the memory is not tracked in the
    dynptr, but it would be trivial to add this support inside dynptr state
    of the register.
    
    With these changes and renaming PTR_TO_DYNPTR to CONST_PTR_TO_DYNPTR to
    better reflect its usage, it can no longer be passed to helpers that
    initialize a dynptr, i.e. bpf_dynptr_from_mem, bpf_ringbuf_reserve_dynptr.
    
    A note to reviewers is that in code that does mark_stack_slots_dynptr,
    and unmark_stack_slots_dynptr, we implicitly rely on the fact that
    PTR_TO_STACK reg is the only case that can reach that code path, as one
    cannot pass CONST_PTR_TO_DYNPTR to helpers that don't set MEM_RDONLY. In
    both cases such helpers won't be setting that flag.
    
    The next patch will add a couple of selftest cases to make sure this
    doesn't break.
    
    Fixes: 205715673844 ("bpf: Add bpf_user_ringbuf_drain() helper")
    Acked-by: Joanne Koong <joannelkoong@gmail.com>
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/r/20221207204141.308952-4-memxor@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:17 +02:00
Jerome Marchand dcf538d57d bpf: Implement cgroup storage available to non-cgroup-attached bpf progs
Bugzilla: https://bugzilla.redhat.com/2177177

Conflicts: Context change from missing commit 7f203bc89eb6 ("cgroup:
Replace cgroup->ancestor_ids[] with ->ancestors[]")

commit c4bcfb38a95edb1021a53f2d0356a78120ecfbe4
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Oct 25 21:28:50 2022 -0700

    bpf: Implement cgroup storage available to non-cgroup-attached bpf progs

    Similar to sk/inode/task storage, implement similar cgroup local storage.

    There already exists a local storage implementation for cgroup-attached
    bpf programs.  See map type BPF_MAP_TYPE_CGROUP_STORAGE and helper
    bpf_get_local_storage(). But there are use cases such that non-cgroup
    attached bpf progs wants to access cgroup local storage data. For example,
    tc egress prog has access to sk and cgroup. It is possible to use
    sk local storage to emulate cgroup local storage by storing data in socket.
    But this is a waste as it could be lots of sockets belonging to a particular
    cgroup. Alternatively, a separate map can be created with cgroup id as the key.
    But this will introduce additional overhead to manipulate the new map.
    A cgroup local storage, similar to existing sk/inode/task storage,
    should help for this use case.

    The life-cycle of storage is managed with the life-cycle of the
    cgroup struct.  i.e. the storage is destroyed along with the owning cgroup
    with a call to bpf_cgrp_storage_free() when cgroup itself
    is deleted.

    The userspace map operations can be done by using a cgroup fd as a key
    passed to the lookup, update and delete operations.

    Typically, the following code is used to get the current cgroup:
        struct task_struct *task = bpf_get_current_task_btf();
        ... task->cgroups->dfl_cgrp ...
    and in structure task_struct definition:
        struct task_struct {
            ....
            struct css_set __rcu            *cgroups;
            ....
        }
    With sleepable program, accessing task->cgroups is not protected by rcu_read_lock.
    So the current implementation only supports non-sleepable program and supporting
    sleepable program will be the next step together with adding rcu_read_lock
    protection for rcu tagged structures.

    Since map name BPF_MAP_TYPE_CGROUP_STORAGE has been used for old cgroup local
    storage support, the new map name BPF_MAP_TYPE_CGRP_STORAGE is used
    for cgroup storage available to non-cgroup-attached bpf programs. The old
    cgroup storage supports bpf_get_local_storage() helper to get the cgroup data.
    The new cgroup storage helper bpf_cgrp_storage_get() can provide similar
    functionality. While old cgroup storage pre-allocates storage memory, the new
    mechanism can also pre-allocate with a user space bpf_map_update_elem() call
    to avoid potential run-time memory allocation failure.
    Therefore, the new cgroup storage can provide all functionality w.r.t.
    the old one. So in uapi bpf.h, the old BPF_MAP_TYPE_CGROUP_STORAGE is alias to
    BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED to indicate the old cgroup storage can
    be deprecated since the new one can provide the same functionality.

    Acked-by: David Vernet <void@manifault.com>
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20221026042850.673791-1-yhs@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:58 +02:00
Jerome Marchand e99e5a42cd scripts/bpf_doc.py: update logic to not assume sequential enum values
Bugzilla: https://bugzilla.redhat.com/2177177

commit ce3e44a09dce74ca68fa56c23333378d936969b0
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Oct 5 21:24:52 2022 -0700

    scripts/bpf_doc.py: update logic to not assume sequential enum values
    
    Relax bpf_doc.py's expectation of all BPF_FUNC_xxx enumerators having
    sequential values increasing by one. Instead, only make sure that
    relative order of BPF helper descriptions in comments matches
    enumerators definitions order.
    
    Also additionally make sure that helper IDs are not duplicated.
    
    And also make sure that for cases when we have multiple descriptions for
    the same BPF helper (e.g., for bpf_get_socket_cookie()), all such
    descriptions are grouped together.
    
    Such checks should capture all the same (and more) issues in upstream
    UAPI headers, but also handle backported kernels correctly.
    
    Reported-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/r/20221006042452.2089843-2-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:51 +02:00
Jerome Marchand 511e42b7ff bpf: explicitly define BPF_FUNC_xxx integer values
Bugzilla: https://bugzilla.redhat.com/2177177

commit 8a76145a2ec2a81dfe34d7ac42e8c242f095e8c8
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Oct 5 21:24:51 2022 -0700

    bpf: explicitly define BPF_FUNC_xxx integer values
    
    Historically enum bpf_func_id's BPF_FUNC_xxx enumerators relied on
    implicit sequential values being assigned by compiler. This is
    convenient, as new BPF helpers are always added at the very end, but it
    also has its downsides, some of them being:
    
      - with over 200 helpers now it's very hard to know what's each helper's ID,
        which is often important to know when working with BPF assembly (e.g.,
        by dumping raw bpf assembly instructions with llvm-objdump -d
        command). it's possible to work around this by looking into vmlinux.h,
        dumping /sys/btf/kernel/vmlinux, looking at libbpf-provided
        bpf_helper_defs.h, etc. But it always feels like an unnecessary step
        and one should be able to quickly figure this out from UAPI header.
    
      - when backporting and cherry-picking only some BPF helpers onto older
        kernels it's important to be able to skip some enum values for helpers
        that weren't backported, but preserve absolute integer IDs to keep BPF
        helper IDs stable so that BPF programs stay portable across upstream
        and backported kernels.
    
    While neither problem is insurmountable, they come up frequently enough
    and are annoying enough to warrant improving the situation. And for the
    backporting the problem can easily go unnoticed for a while, especially
    if backport is done with people not very familiar with BPF subsystem overall.
    
    Anyways, it's easy to fix this by making sure that __BPF_FUNC_MAPPER
    macro provides explicit helper IDs. Unfortunately that would potentially
    break existing users that use UAPI-exposed __BPF_FUNC_MAPPER and are
    expected to pass macro that accepts only symbolic helper identifier
    (e.g., map_lookup_elem for bpf_map_lookup_elem() helper).
    
    As such, we need to introduce a new macro (___BPF_FUNC_MAPPER) which
    would specify both identifier and integer ID, but in such a way as to
    allow existing __BPF_FUNC_MAPPER be expressed in terms of new
    ___BPF_FUNC_MAPPER macro. And that's what this patch is doing. To avoid
    duplication and allow __BPF_FUNC_MAPPER stay *exactly* the same,
    ___BPF_FUNC_MAPPER accepts arbitrary "context" arguments, which can be
    used to pass any extra macros, arguments, and whatnot. In our case we
    use this to pass original user-provided macro that expects single
    argument and __BPF_FUNC_MAPPER is using it's own three-argument
    __BPF_FUNC_MAPPER_APPLY intermediate macro to impedance-match new and
    old "callback" macros.
    
    Once we resolve this, we use new ___BPF_FUNC_MAPPER to define enum
    bpf_func_id with explicit values. The other users of __BPF_FUNC_MAPPER
    in kernel (namely in kernel/bpf/disasm.c) are kept exactly the same both
    as demonstration that backwards compat works, but also to avoid
    unnecessary code churn.
    
    Note that new ___BPF_FUNC_MAPPER() doesn't forcefully insert comma
    between values, as that might not be appropriate in all possible cases
    where ___BPF_FUNC_MAPPER might be used by users. This doesn't reduce
    usability, as it's trivial to insert that comma inside "callback" macro.
    
    To validate all the manually specified IDs are exactly right, we used
    BTF to compare before and after values:
    
      $ bpftool btf dump file ~/linux-build/default/vmlinux | rg bpf_func_id -A 211 > after.txt
      $ git stash # stach UAPI changes
      $ make -j90
      ... re-building kernel without UAPI changes ...
      $ bpftool btf dump file ~/linux-build/default/vmlinux | rg bpf_func_id -A 211 > before.txt
      $ diff -u before.txt after.txt
      --- before.txt  2022-10-05 10:48:18.119195916 -0700
      +++ after.txt   2022-10-05 10:46:49.446615025 -0700
      @@ -1,4 +1,4 @@
      -[14576] ENUM 'bpf_func_id' encoding=UNSIGNED size=4 vlen=211
      +[9560] ENUM 'bpf_func_id' encoding=UNSIGNED size=4 vlen=211
              'BPF_FUNC_unspec' val=0
              'BPF_FUNC_map_lookup_elem' val=1
              'BPF_FUNC_map_update_elem' val=2
    
    As can be seen from diff above, the only thing that changed was resulting BTF
    type ID of ENUM bpf_func_id, not any of the enumerators, their names or integer
    values.
    
    The only other place that needed fixing was scripts/bpf_doc.py used to generate
    man pages and bpf_helper_defs.h header for libbpf and selftests. That script is
    tightly-coupled to exact shape of ___BPF_FUNC_MAPPER macro definition, so had
    to be trivially adapted.
    
    Cc: Quentin Monnet <quentin@isovalent.com>
    Reported-by: Andrea Terzolo <andrea.terzolo@polito.it>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
    Link: https://lore.kernel.org/r/20221006042452.2089843-1-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:51 +02:00
Artem Savkov dbbee687fc bpf/scripts: Assert helper enum value is aligned with comment order
Bugzilla: https://bugzilla.redhat.com/2166911

commit 0a0d55ef3e61d9f14e803cacb644fcc890f16774
Author: Eyal Birger <eyal.birger@gmail.com>
Date:   Wed Aug 24 21:10:43 2022 +0300

    bpf/scripts: Assert helper enum value is aligned with comment order
    
    The helper value is ABI as defined by enum bpf_func_id.
    As bpf_helper_defs.h is used for the userpace part, it must be consistent
    with this enum.
    
    Before this change the comments order was used by the bpf_doc script in
    order to set the helper values defined in the helpers file.
    
    When adding new helpers it is very puzzling when the userspace application
    breaks in weird places if the comment is inserted instead of appended -
    because the generated helper ABI is incorrect and shifted.
    
    This commit sets the helper value to the enum value.
    
    In addition it is currently the practice to have the comments appended
    and kept in the same order as the enum. As such, add an assertion
    validating the comment order is consistent with enum value.
    
    In case a different comments ordering is desired, this assertion can
    be lifted.
    
    Signed-off-by: Eyal Birger <eyal.birger@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220824181043.1601429-1-eyal.birger@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:04 +01:00
Artem Savkov 5f564693a7 scripts/bpf: Set date attribute for bpf-helpers(7) man page
Bugzilla: https://bugzilla.redhat.com/2166911

commit 92ec1cc3784a2a8a7a62596dcec4f2224b85dcf4
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Tue Aug 23 16:53:27 2022 +0100

    scripts/bpf: Set date attribute for bpf-helpers(7) man page
    
    The bpf-helpers(7) manual page shipped in the man-pages project is
    generated from the documentation contained in the BPF UAPI header, in
    the Linux repository, parsed by script/bpf_doc.py and then fed to
    rst2man.
    
    The man page should contain the date of last modification of the
    documentation. This commit adds the relevant date when generating the
    page.
    
    Before:
    
        $ ./scripts/bpf_doc.py helpers | rst2man | grep '\.TH'
        .TH BPF-HELPERS 7 "" "Linux v5.19-14022-g30d2a4d74e11" ""
    
    After:
    
        $ ./scripts/bpf_doc.py helpers | rst2man | grep '\.TH'
        .TH BPF-HELPERS 7 "2022-08-15" "Linux v5.19-14022-g30d2a4d74e11" ""
    
    We get the version by using "git log" to look for the commit date of the
    latest change to the section of the BPF header containing the
    documentation. If the command fails, we just skip the date field. and
    keep generating the page.
    
    Reported-by: Alejandro Colomar <alx.manpages@gmail.com>
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Alejandro Colomar <alx.manpages@gmail.com>
    Link: https://lore.kernel.org/bpf/20220823155327.98888-2-quentin@isovalent.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:02 +01:00
Artem Savkov 8be227a26b scripts/bpf: Set version attribute for bpf-helpers(7) man page
Bugzilla: https://bugzilla.redhat.com/2166911

commit fd0a38f9c37d539f5603f887cdb637a4e6e6944d
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Tue Aug 23 16:53:26 2022 +0100

    scripts/bpf: Set version attribute for bpf-helpers(7) man page
    
    The bpf-helpers(7) manual page shipped in the man-pages project is
    generated from the documentation contained in the BPF UAPI header, in
    the Linux repository, parsed by script/bpf_doc.py and then fed to
    rst2man.
    
    After a recent update of that page [0], Alejandro reported that the
    linter used to validate the man pages complains about the generated
    document [1]. The header for the page is supposed to contain some
    attributes that we do not set correctly with the script. This commit
    updates the "project and version" field. We discussed the format of
    those fields in [1] and [2].
    
    Before:
    
        $ ./scripts/bpf_doc.py helpers | rst2man | grep '\.TH'
        .TH BPF-HELPERS 7 "" "" ""
    
    After:
    
        $ ./scripts/bpf_doc.py helpers | rst2man | grep '\.TH'
        .TH BPF-HELPERS 7 "" "Linux v5.19-14022-g30d2a4d74e11" ""
    
    We get the version from "git describe", but if unavailable, we fall back
    on "make kernelversion". If none works, for example because neither git
    nore make are installed, we just set the field to "Linux" and keep
    generating the page.
    
    [0] https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/man7/bpf-helpers.7?id=19c7f78393f2b038e76099f87335ddf43a87f039
    [1] https://lore.kernel.org/all/20220823084719.13613-1-quentin@isovalent.com/t/#m58a418a318642c6428e14ce9bb84eba5183b06e8
    [2] https://lore.kernel.org/all/20220721110821.8240-1-alx.manpages@gmail.com/t/#m8e689a822e03f6e2530a0d6de9d128401916c5de
    
    Reported-by: Alejandro Colomar <alx.manpages@gmail.com>
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Alejandro Colomar <alx.manpages@gmail.com>
    Link: https://lore.kernel.org/bpf/20220823155327.98888-1-quentin@isovalent.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:02 +01:00
Artem Savkov ca5ca630d3 bpf, docs: Use SPDX license identifier in bpf_doc.py
Bugzilla: https://bugzilla.redhat.com/2137876

commit 5cb62b7598f2413f54650d5124d7863e23f82a44
Author: Alejandro Colomar <alx.manpages@gmail.com>
Date:   Thu Jul 21 13:08:22 2022 +0200

    bpf, docs: Use SPDX license identifier in bpf_doc.py
    
    The Linux man-pages project now uses SPDX tags, instead of the full
    license text.
    
    Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://www.kernel.org/doc/man-pages/licenses.html
    Link: https://spdx.org/licenses/Linux-man-pages-copyleft.html
    Link: https://lore.kernel.org/bpf/20220721110821.8240-1-alx.manpages@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:41 +01:00
Artem Savkov 56229aa3cb bpf: Add helpers to issue and check SYN cookies in XDP
Bugzilla: https://bugzilla.redhat.com/2137876

commit 33bf9885040c399cf6a95bd33216644126728e14
Author: Maxim Mikityanskiy <maximmi@nvidia.com>
Date:   Wed Jun 15 16:48:44 2022 +0300

    bpf: Add helpers to issue and check SYN cookies in XDP
    
    The new helpers bpf_tcp_raw_{gen,check}_syncookie_ipv{4,6} allow an XDP
    program to generate SYN cookies in response to TCP SYN packets and to
    check those cookies upon receiving the first ACK packet (the final
    packet of the TCP handshake).
    
    Unlike bpf_tcp_{gen,check}_syncookie these new helpers don't need a
    listening socket on the local machine, which allows to use them together
    with synproxy to accelerate SYN cookie generation.
    
    Signed-off-by: Maxim Mikityanskiy <maximmi@nvidia.com>
    Reviewed-by: Tariq Toukan <tariqt@nvidia.com>
    Link: https://lore.kernel.org/r/20220615134847.3753567-4-maximmi@nvidia.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:36 +01:00
Yauheni Kaliuta 251a3a0808 bpf: Add verifier support for dynptrs
Bugzilla: https://bugzilla.redhat.com/2120968

commit 97e03f521050c092919591e668107b3d69c5f426
Author: Joanne Koong <joannelkoong@gmail.com>
Date:   Mon May 23 14:07:07 2022 -0700

    bpf: Add verifier support for dynptrs
    
    This patch adds the bulk of the verifier work for supporting dynamic
    pointers (dynptrs) in bpf.
    
    A bpf_dynptr is opaque to the bpf program. It is a 16-byte structure
    defined internally as:
    
    struct bpf_dynptr_kern {
        void *data;
        u32 size;
        u32 offset;
    } __aligned(8);
    
    The upper 8 bits of *size* is reserved (it contains extra metadata about
    read-only status and dynptr type). Consequently, a dynptr only supports
    memory less than 16 MB.
    
    There are different types of dynptrs (eg malloc, ringbuf, ...). In this
    patchset, the most basic one, dynptrs to a bpf program's local memory,
    is added. For now only local memory that is of reg type PTR_TO_MAP_VALUE
    is supported.
    
    In the verifier, dynptr state information will be tracked in stack
    slots. When the program passes in an uninitialized dynptr
    (ARG_PTR_TO_DYNPTR | MEM_UNINIT), the stack slots corresponding
    to the frame pointer where the dynptr resides at are marked
    STACK_DYNPTR. For helper functions that take in initialized dynptrs (eg
    bpf_dynptr_read + bpf_dynptr_write which are added later in this
    patchset), the verifier enforces that the dynptr has been initialized
    properly by checking that their corresponding stack slots have been
    marked as STACK_DYNPTR.
    
    The 6th patch in this patchset adds test cases that the verifier should
    successfully reject, such as for example attempting to use a dynptr
    after doing a direct write into it inside the bpf program.
    
    Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/bpf/20220523210712.3641569-2-joannelkoong@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:06 +02:00
Yauheni Kaliuta 7dcd07e6dc bpf: Add bpf_skc_to_mptcp_sock_proto
Bugzilla: https://bugzilla.redhat.com/2120968

commit 3bc253c2e652cf5f12cd8c00d80d8ec55d67d1a7
Author: Geliang Tang <geliang.tang@suse.com>
Date:   Thu May 19 16:30:10 2022 -0700

    bpf: Add bpf_skc_to_mptcp_sock_proto
    
    This patch implements a new struct bpf_func_proto, named
    bpf_skc_to_mptcp_sock_proto. Define a new bpf_id BTF_SOCK_TYPE_MPTCP,
    and a new helper bpf_skc_to_mptcp_sock(), which invokes another new
    helper bpf_mptcp_sock_from_subflow() in net/mptcp/bpf.c to get struct
    mptcp_sock from a given subflow socket.
    
    v2: Emit BTF type, add func_id checks in verifier.c and bpf_trace.c,
    remove build check for CONFIG_BPF_JIT
    v5: Drop EXPORT_SYMBOL (Martin)
    
    Co-developed-by: Nicolas Rybowski <nicolas.rybowski@tessares.net>
    Co-developed-by: Matthieu Baerts <matthieu.baerts@tessares.net>
    Signed-off-by: Nicolas Rybowski <nicolas.rybowski@tessares.net>
    Signed-off-by: Matthieu Baerts <matthieu.baerts@tessares.net>
    Signed-off-by: Geliang Tang <geliang.tang@suse.com>
    Signed-off-by: Mat Martineau <mathew.j.martineau@linux.intel.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220519233016.105670-2-mathew.j.martineau@linux.intel.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:05 +02:00
Jerome Marchand e1e80231dc bpf/scripts: Raise an exception if the correct number of sycalls are not generated
Bugzilla: https://bugzilla.redhat.com/2120966

commit 0ba3929e5b3d3fda05d3b9c8d0d20a90a084c19e
Author: Usama Arif <usama.arif@bytedance.com>
Date:   Wed Jan 19 11:44:42 2022 +0000

    bpf/scripts: Raise an exception if the correct number of sycalls are not generated

    Currently the syscalls rst and subsequently man page are auto-generated
    using function documentation present in bpf.h. If the documentation for the
    syscall is missing or doesn't follow a specific format, then that syscall
    is not dumped in the auto-generated rst.

    This patch checks the number of syscalls documented within the header file
    with those present as part of the enum bpf_cmd and raises an Exception if
    they don't match. It is not needed with the currently documented upstream
    syscalls, but can help in debugging when developing new syscalls when
    there might be missing or misformatted documentation.

    The function helper_number_check is moved to the Printer parent
    class and renamed to elem_number_check as all the most derived children
    classes are using this function now.

    Signed-off-by: Usama Arif <usama.arif@bytedance.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220119114442.1452088-3-usama.arif@bytedance.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:40 +02:00
Jerome Marchand 9c15591f44 bpf/scripts: Make description and returns section for helpers/syscalls mandatory
Bugzilla: https://bugzilla.redhat.com/2120966

commit f1f3f67fd8ed6f512955bbbc76b04e9dc33ddeb6
Author: Usama Arif <usama.arif@bytedance.com>
Date:   Wed Jan 19 11:44:41 2022 +0000

    bpf/scripts: Make description and returns section for helpers/syscalls mandatory

    This  enforce a minimal formatting consistency for the documentation. The
    description and returns missing for a few helpers have also been added.

    Signed-off-by: Usama Arif <usama.arif@bytedance.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220119114442.1452088-2-usama.arif@bytedance.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:40 +02:00
Jerome Marchand 8855cfdf85 bpf/scripts: Raise an exception if the correct number of helpers are not generated
Bugzilla: https://bugzilla.redhat.com/2120966

commit 71a3cdf80c0d12aadca6f9b0f5a43883649bdbea
Author: Usama Arif <usama.arif@bytedance.com>
Date:   Wed Jan 12 11:49:53 2022 +0000

    bpf/scripts: Raise an exception if the correct number of helpers are not generated

    Currently bpf_helper_defs.h and the bpf helpers man page are auto-generated
    using function documentation present in bpf.h. If the documentation for the
    helper is missing or doesn't follow a specific format for e.g. if a function
    is documented as:
     * long bpf_kallsyms_lookup_name( const char *name, int name_sz, int flags, u64 *res )
    instead of
     * long bpf_kallsyms_lookup_name(const char *name, int name_sz, int flags, u64 *res)
    (notice the extra space at the start and end of function arguments)
    then that helper is not dumped in the auto-generated header and results in
    an invalid call during eBPF runtime, even if all the code specific to the
    helper is correct.

    This patch checks the number of functions documented within the header file
    with those present as part of #define __BPF_FUNC_MAPPER and raises an
    Exception if they don't match. It is not needed with the currently documented
    upstream functions, but can help in debugging when developing new helpers
    when there might be missing or misformatted documentation.

    Signed-off-by: Usama Arif <usama.arif@bytedance.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20220112114953.722380-1-usama.arif@bytedance.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:39 +02:00
Yauheni Kaliuta b7a8adb0f8 bpf: Add bpf_skc_to_unix_sock() helper
Bugzilla: http://bugzilla.redhat.com/2069045

Conflicts: context difference due to already applied
  5e0bc3082e2e ("bpf: Forbid bpf_ktime_get_coarse_ns and bpf_timer_* in tracing progs")

commit 9eeb3aa33ae005526f672b394c1791578463513f
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Thu Oct 21 21:47:51 2021 +0800

    bpf: Add bpf_skc_to_unix_sock() helper

    The helper is used in tracing programs to cast a socket
    pointer to a unix_sock pointer.
    The return value could be NULL if the casting is illegal.

    Suggested-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20211021134752.1223426-2-hengqi.chen@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:43 +03:00
Jerome Marchand 00ace47307 bpf: Introduce bpf timers.
Bugzilla: http://bugzilla.redhat.com/2041365

commit b00628b1c7d595ae5b544e059c27b1f5828314b4
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Wed Jul 14 17:54:09 2021 -0700

    bpf: Introduce bpf timers.

    Introduce 'struct bpf_timer { __u64 :64; __u64 :64; };' that can be embedded
    in hash/array/lru maps as a regular field and helpers to operate on it:

    // Initialize the timer.
    // First 4 bits of 'flags' specify clockid.
    // Only CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_BOOTTIME are allowed.
    long bpf_timer_init(struct bpf_timer *timer, struct bpf_map *map, int flags);

    // Configure the timer to call 'callback_fn' static function.
    long bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn);

    // Arm the timer to expire 'nsec' nanoseconds from the current time.
    long bpf_timer_start(struct bpf_timer *timer, u64 nsec, u64 flags);

    // Cancel the timer and wait for callback_fn to finish if it was running.
    long bpf_timer_cancel(struct bpf_timer *timer);

    Here is how BPF program might look like:
    struct map_elem {
        int counter;
        struct bpf_timer timer;
    };

    struct {
        __uint(type, BPF_MAP_TYPE_HASH);
        __uint(max_entries, 1000);
        __type(key, int);
        __type(value, struct map_elem);
    } hmap SEC(".maps");

    static int timer_cb(void *map, int *key, struct map_elem *val);
    /* val points to particular map element that contains bpf_timer. */

    SEC("fentry/bpf_fentry_test1")
    int BPF_PROG(test1, int a)
    {
        struct map_elem *val;
        int key = 0;

        val = bpf_map_lookup_elem(&hmap, &key);
        if (val) {
            bpf_timer_init(&val->timer, &hmap, CLOCK_REALTIME);
            bpf_timer_set_callback(&val->timer, timer_cb);
            bpf_timer_start(&val->timer, 1000 /* call timer_cb2 in 1 usec */, 0);
        }
    }

    This patch adds helper implementations that rely on hrtimers
    to call bpf functions as timers expire.
    The following patches add necessary safety checks.

    Only programs with CAP_BPF are allowed to use bpf_timer.

    The amount of timers used by the program is constrained by
    the memcg recorded at map creation time.

    The bpf_timer_init() helper needs explicit 'map' argument because inner maps
    are dynamic and not known at load time. While the bpf_timer_set_callback() is
    receiving hidden 'aux->prog' argument supplied by the verifier.

    The prog pointer is needed to do refcnting of bpf program to make sure that
    program doesn't get freed while the timer is armed. This approach relies on
    "user refcnt" scheme used in prog_array that stores bpf programs for
    bpf_tail_call. The bpf_timer_set_callback() will increment the prog refcnt which is
    paired with bpf_timer_cancel() that will drop the prog refcnt. The
    ops->map_release_uref is responsible for cancelling the timers and dropping
    prog refcnt when user space reference to a map reaches zero.
    This uref approach is done to make sure that Ctrl-C of user space process will
    not leave timers running forever unless the user space explicitly pinned a map
    that contained timers in bpffs.

    bpf_timer_init() and bpf_timer_set_callback() will return -EPERM if map doesn't
    have user references (is not held by open file descriptor from user space and
    not pinned in bpffs).

    The bpf_map_delete_elem() and bpf_map_update_elem() operations cancel
    and free the timer if given map element had it allocated.
    "bpftool map update" command can be used to cancel timers.

    The 'struct bpf_timer' is explicitly __attribute__((aligned(8))) because
    '__u64 :64' has 1 byte alignment of 8 byte padding.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
    Link: https://lore.kernel.org/bpf/20210715005417.78572-4-alexei.starovoitov@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:31 +02:00
Joe Stringer a67882a221 scripts/bpf: Add syscall commands printer
Add a new target to bpf_doc.py to support generating the list of syscall
commands directly from the UAPI headers. Assuming that developer
submissions keep the main header up to date, this should allow the man
pages to be automatically generated based on the latest API changes
rather than requiring someone to separately go back through the API and
describe each command.

Signed-off-by: Joe Stringer <joe@cilium.io>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210302171947.2268128-11-joe@cilium.io
2021-03-04 18:39:45 -08:00
Joe Stringer 923a932c98 scripts/bpf: Abstract eBPF API target parameter
Abstract out the target parameter so that upcoming commits, more than
just the existing "helpers" target can be called to generate specific
portions of docs from the eBPF UAPI headers.

Signed-off-by: Joe Stringer <joe@cilium.io>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210302171947.2268128-10-joe@cilium.io
2021-03-04 18:39:45 -08:00