1. 03 Sep, 2022 1 commit
    • Martin KaFai Lau's avatar
      net: Change sock_getsockopt() to take the sk ptr instead of the sock ptr · ba74a760
      Martin KaFai Lau authored
      A latter patch refactors bpf_getsockopt(SOL_SOCKET) with the
      sock_getsockopt() to avoid code duplication and code
      drift between the two duplicates.
      
      The current sock_getsockopt() takes sock ptr as the argument.
      The very first thing of this function is to get back the sk ptr
      by 'sk = sock->sk'.
      
      bpf_getsockopt() could be called when the sk does not have
      the sock ptr created.  Meaning sk->sk_socket is NULL.  For example,
      when a passive tcp connection has just been established but has yet
      been accept()-ed.  Thus, it cannot use the sock_getsockopt(sk->sk_socket)
      or else it will pass a NULL ptr.
      
      This patch moves all sock_getsockopt implementation to the newly
      added sk_getsockopt().  The new sk_getsockopt() takes a sk ptr
      and immediately gets the sock ptr by 'sock = sk->sk_socket'
      
      The existing sock_getsockopt(sock) is changed to call
      sk_getsockopt(sock->sk).  All existing callers have both sock->sk
      and sk->sk_socket pointer.
      
      The latter patch will make bpf_getsockopt(SOL_SOCKET) call
      sk_getsockopt(sk) directly.  The bpf_getsockopt(SOL_SOCKET) does
      not use the optnames that require sk->sk_socket, so it will
      be safe.
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Link: https://lore.kernel.org/r/20220902002756.2887884-1-kafai@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      ba74a760
  2. 02 Sep, 2022 11 commits
  3. 01 Sep, 2022 7 commits
  4. 31 Aug, 2022 7 commits
  5. 30 Aug, 2022 1 commit
  6. 29 Aug, 2022 3 commits
    • James Hilliard's avatar
      selftests/bpf: Fix connect4_prog tcp/socket header type conflict · 2eb68040
      James Hilliard authored
      There is a potential for us to hit a type conflict when including
      netinet/tcp.h and sys/socket.h, we can replace both of these includes
      with linux/tcp.h and bpf_tcp_helpers.h to avoid this conflict.
      
      Fixes errors like the below when compiling with gcc BPF backend:
      
        In file included from /usr/include/netinet/tcp.h:91,
                         from progs/connect4_prog.c:11:
        /home/buildroot/opt/cross/lib/gcc/bpf/13.0.0/include/stdint.h:34:23: error: conflicting types for 'int8_t'; have 'char'
           34 | typedef __INT8_TYPE__ int8_t;
              |                       ^~~~~~
        In file included from /usr/include/x86_64-linux-gnu/sys/types.h:155,
                         from /usr/include/x86_64-linux-gnu/bits/socket.h:29,
                         from /usr/include/x86_64-linux-gnu/sys/socket.h:33,
                         from progs/connect4_prog.c:10:
        /usr/include/x86_64-linux-gnu/bits/stdint-intn.h:24:18: note: previous declaration of 'int8_t' with type 'int8_t' {aka 'signed char'}
           24 | typedef __int8_t int8_t;
              |                  ^~~~~~
        /home/buildroot/opt/cross/lib/gcc/bpf/13.0.0/include/stdint.h:43:24: error: conflicting types for 'int64_t'; have 'long int'
           43 | typedef __INT64_TYPE__ int64_t;
              |                        ^~~~~~~
        /usr/include/x86_64-linux-gnu/bits/stdint-intn.h:27:19: note: previous declaration of 'int64_t' with type 'int64_t' {aka 'long long int'}
           27 | typedef __int64_t int64_t;
              |                   ^~~~~~~
      Signed-off-by: default avatarJames Hilliard <james.hilliard1@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220829154710.3870139-1-james.hilliard1@gmail.com
      2eb68040
    • James Hilliard's avatar
      selftests/bpf: Fix bind{4,6} tcp/socket header type conflict · 3721359d
      James Hilliard authored
      There is a potential for us to hit a type conflict when including
      netinet/tcp.h with sys/socket.h, we can remove these as they are not
      actually needed.
      
      Fixes errors like the below when compiling with gcc BPF backend:
      
        In file included from /usr/include/netinet/tcp.h:91,
                         from progs/bind4_prog.c:10:
        /home/buildroot/opt/cross/lib/gcc/bpf/13.0.0/include/stdint.h:34:23: error: conflicting types for 'int8_t'; have 'char'
           34 | typedef __INT8_TYPE__ int8_t;
              |                       ^~~~~~
        In file included from /usr/include/x86_64-linux-gnu/sys/types.h:155,
                         from /usr/include/x86_64-linux-gnu/bits/socket.h:29,
                         from /usr/include/x86_64-linux-gnu/sys/socket.h:33,
                         from progs/bind4_prog.c:9:
        /usr/include/x86_64-linux-gnu/bits/stdint-intn.h:24:18: note: previous declaration of 'int8_t' with type 'int8_t' {aka 'signed char'}
           24 | typedef __int8_t int8_t;
              |                  ^~~~~~
        /home/buildroot/opt/cross/lib/gcc/bpf/13.0.0/include/stdint.h:43:24: error: conflicting types for 'int64_t'; have 'long int'
           43 | typedef __INT64_TYPE__ int64_t;
              |                        ^~~~~~~
        /usr/include/x86_64-linux-gnu/bits/stdint-intn.h:27:19: note: previous declaration of 'int64_t' with type 'int64_t' {aka 'long long int'}
           27 | typedef __int64_t int64_t;
              |                   ^~~~~~~
        make: *** [Makefile:537: /home/buildroot/bpf-next/tools/testing/selftests/bpf/bpf_gcc/bind4_prog.o] Error 1
      Signed-off-by: default avatarJames Hilliard <james.hilliard1@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220826052925.980431-1-james.hilliard1@gmail.com
      3721359d
    • Tiezhu Yang's avatar
      bpf, mips: No need to use min() to get MAX_TAIL_CALL_CNT · bbcf0f55
      Tiezhu Yang authored
      MAX_TAIL_CALL_CNT is 33, so min(MAX_TAIL_CALL_CNT, 0xffff) is always
      MAX_TAIL_CALL_CNT, it is better to use MAX_TAIL_CALL_CNT directly.
      
      At the same time, add BUILD_BUG_ON(MAX_TAIL_CALL_CNT > 0xffff) with a
      comment on why the assertion is there.
      Suggested-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Suggested-by: default avatarJohan Almbladh <johan.almbladh@anyfinetworks.com>
      Signed-off-by: default avatarTiezhu Yang <yangtiezhu@loongson.cn>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/1661742309-2320-1-git-send-email-yangtiezhu@loongson.cn
      bbcf0f55
  7. 27 Aug, 2022 2 commits
  8. 26 Aug, 2022 3 commits
  9. 25 Aug, 2022 5 commits
    • Hao Luo's avatar
      bpf: Add CGROUP prefix to cgroup_iter_order · d4ffb6f3
      Hao Luo authored
      bpf_cgroup_iter_order is globally visible but the entries do not have
      CGROUP prefix. As requested by Andrii, put a CGROUP in the names
      in bpf_cgroup_iter_order.
      
      This patch fixes two previous commits: one introduced the API and
      the other uses the API in bpf selftest (that is, the selftest
      cgroup_hierarchical_stats).
      
      I tested this patch via the following command:
      
        test_progs -t cgroup,iter,btf_dump
      
      Fixes: d4ccaf58 ("bpf: Introduce cgroup iter")
      Fixes: 88886309 ("selftests/bpf: add a selftest for cgroup hierarchical stats collection")
      Suggested-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Link: https://lore.kernel.org/r/20220825223936.1865810-1-haoluo@google.comSigned-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
      d4ffb6f3
    • Eyal Birger's avatar
      bpf/scripts: Assert helper enum value is aligned with comment order · 0a0d55ef
      Eyal Birger authored
      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: default avatarEyal Birger <eyal.birger@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Reviewed-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Link: https://lore.kernel.org/bpf/20220824181043.1601429-1-eyal.birger@gmail.com
      0a0d55ef
    • Lam Thai's avatar
      bpftool: Fix a wrong type cast in btf_dumper_int · 7184aef9
      Lam Thai authored
      When `data` points to a boolean value, casting it to `int *` is problematic
      and could lead to a wrong value being passed to `jsonw_bool`. Change the
      cast to `bool *` instead.
      
      Fixes: b12d6ec0 ("bpf: btf: add btf print functionality")
      Signed-off-by: default avatarLam Thai <lamthai@arista.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Reviewed-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Link: https://lore.kernel.org/bpf/20220824225859.9038-1-lamthai@arista.com
      7184aef9
    • Alexei Starovoitov's avatar
      Merge branch 'bpf: rstat: cgroup hierarchical' · eef3c3d3
      Alexei Starovoitov authored
      Hao Luo says:
      
      ====================
      
      This patch series allows for using bpf to collect hierarchical cgroup
      stats efficiently by integrating with the rstat framework. The rstat
      framework provides an efficient way to collect cgroup stats percpu and
      propagate them through the cgroup hierarchy.
      
      The stats are exposed to userspace in textual form by reading files in
      bpffs, similar to cgroupfs stats by using a cgroup_iter program.
      cgroup_iter is a type of bpf_iter. It walks over cgroups in four modes:
      - walking a cgroup's descendants in pre-order.
      - walking a cgroup's descendants in post-order.
      - walking a cgroup's ancestors.
      - process only a single object.
      
      When attaching cgroup_iter, one needs to set a cgroup to the iter_link
      created from attaching. This cgroup can be passed either as a file
      descriptor or a cgroup id. That cgroup serves as the starting point of
      the walk.
      
      One can also terminate the walk early by returning 1 from the iter
      program.
      
      Note that because walking cgroup hierarchy holds cgroup_mutex, the iter
      program is called with cgroup_mutex held.
      
      ** Background on rstat for stats collection **
      (I am using a subscriber analogy that is not commonly used)
      
      The rstat framework maintains a tree of cgroups that have updates and
      which cpus have updates. A subscriber to the rstat framework maintains
      their own stats. The framework is used to tell the subscriber when
      and what to flush, for the most efficient stats propagation. The
      workflow is as follows:
      
      - When a subscriber updates a cgroup on a cpu, it informs the rstat
        framework by calling cgroup_rstat_updated(cgrp, cpu).
      
      - When a subscriber wants to read some stats for a cgroup, it asks
        the rstat framework to initiate a stats flush (propagation) by calling
        cgroup_rstat_flush(cgrp).
      
      - When the rstat framework initiates a flush, it makes callbacks to
        subscribers to aggregate stats on cpus that have updates, and
        propagate updates to their parent.
      
      Currently, the main subscribers to the rstat framework are cgroup
      subsystems (e.g. memory, block). This patch series allow bpf programs to
      become subscribers as well.
      
      Patches in this series are organized as follows:
      * Patches 1-2 introduce cgroup_iter prog, and a selftest.
      * Patches 3-5 allow bpf programs to integrate with rstat by adding the
        necessary hook points and kfunc. A comprehensive selftest that
        demonstrates the entire workflow for using bpf and rstat to
        efficiently collect and output cgroup stats is added.
      ---
      Changelog:
      v8 -> v9:
      - Make UNSPEC (an invalid option) as the default order for cgroup_iter.
      - Use enum for specifying cgroup_iter order, instead of u32.
      - Add BPF_ITER_RESHCED to cgroup_iter.
      - Add cgroup_hierarchical_stats to s390x denylist.
      
      v7 -> v8:
      - Removed the confusing BPF_ITER_DEFAULT (Andrii)
      - s/SELF/SELF_ONLY/g
      - Fixed typo (e.g. outputing) (Andrii)
      - Use "descendants_pre", "descendants_post" etc. instead of "pre",
        "post" (Andrii)
      
      v6 -> v7:
      - Updated commit/comments in cgroup_iter for read() behavior (Yonghong)
      - Extracted BPF_ITER_SELF and other options out of cgroup_iter, so
        that they can be used in other iters. Also renamed them. (Andrii)
      - Supports both cgroup_fd and cgroup_id when specifying target cgroup.
        (Andrii)
      - Avoided using macro for formatting expected output in cgroup_iter
        selftest. (Andrii)
      - Applied 'static' on all vars and functions in cgroup_iter selftest.
        (Andrii)
      - Fixed broken buf reading in cgroup_iter selftest. (Andrii)
      - Switched to use bpf_link__destroy() unconditionally. (Andrii)
      - Removed 'volatile' for non-const global vars in selftests. (Andrii)
      - Started using bpf_core_enum_value() to get memory_cgrp_id. (Andrii)
      
      v5 -> v6:
      - Rebased on bpf-next
      - Tidy up cgroup_hierarchical_stats test (Andrii)
        * 'static' and 'inline'
        * avoid using libbpf_get_error()
        * string literals of cgroup paths.
      - Rename patch 8/8 to 'selftests/bpf' (Yonghong)
      - Fix cgroup_iter comments (e.g. PAGE_SIZE and uapi) (Yonghong)
      - Make sure further read() returns OK after previous read() finished
        properly (Yonghong)
      - Release cgroup_mutex before the last call of show() (Kumar)
      
      v4 -> v5:
      - Rebased on top of new kfunc flags infrastructure, updated patch 1 and
        patch 6 accordingly.
      - Added docs for sleepable kfuncs.
      
      v3 -> v4:
      - cgroup_iter:
        * reorder fields in bpf_link_info to avoid break uapi (Yonghong)
        * comment the behavior when cgroup_fd=0 (Yonghong)
        * comment on the limit of number of cgroups supported by cgroup_iter.
          (Yonghong)
      - cgroup_hierarchical_stats selftest:
        * Do not return -1 if stats are not found (causes overflow in userspace).
        * Check if child process failed to join cgroup.
        * Make buf and path arrays in get_cgroup_vmscan_delay() static.
        * Increase the test map sizes to accomodate cgroups that are not
          created by the test.
      
      v2 -> v3:
      - cgroup_iter:
        * Added conditional compilation of cgroup_iter.c in kernel/bpf/Makefile
          (kernel test) and dropped the !CONFIG_CGROUP patch.
        * Added validation of traversal_order when attaching (Yonghong).
        * Fixed previous wording "two modes" to "three modes" (Yonghong).
        * Fixed the btf_dump selftest broken by this patch (Yonghong).
        * Fixed ctx_arg_info[0] to use "PTR_TO_BTF_ID_OR_NULL" instead of
          "PTR_TO_BTF_ID", because the "cgroup" pointer passed to iter prog can
           be null.
      - Use __diag_push to eliminate __weak noinline warning in
        bpf_rstat_flush().
      - cgroup_hierarchical_stats selftest:
        * Added write_cgroup_file_parent() helper.
        * Added error handling for failed map updates.
        * Added null check for cgroup in vmscan_flush.
        * Fixed the signature of vmscan_[start/end].
        * Correctly return error code when attaching trace programs fail.
        * Make sure all links are destroyed correctly and not leaking in
          cgroup_hierarchical_stats selftest.
        * Use memory.reclaim instead of memory.high as a more reliable way to
          invoke reclaim.
        * Eliminated sleeps, the test now runs faster.
      
      v1 -> v2:
      - Redesign of cgroup_iter from v1, based on Alexei's idea [1]:
        * supports walking cgroup subtree.
        * supports walking ancestors of a cgroup. (Andrii)
        * supports terminating the walk early.
        * uses fd instead of cgroup_id as parameter for iter_link. Using fd is
          a convention in bpf.
        * gets cgroup's ref at attach time and deref at detach.
        * brought back cgroup1 support for cgroup_iter.
      - Squashed the patches adding the rstat flush hook points and kfuncs
        (Tejun).
      - Added a comment explaining why bpf_rstat_flush() needs to be weak
        (Tejun).
      - Updated the final selftest with the new cgroup_iter design.
      - Changed CHECKs in the selftest with ASSERTs (Yonghong, Andrii).
      - Removed empty line at the end of the selftest (Yonghong).
      - Renamed test files to cgroup_hierarchical_stats.c.
      - Reordered CGROUP_PATH params order to match struct declaration
        in the selftest (Michal).
      - Removed memory_subsys_enabled() and made sure memcg controller
        enablement checks make sense and are documented (Michal).
      
      RFC v2 -> v1:
      - Instead of introducing a new program type for rstat flushing, add an
        empty hook point, bpf_rstat_flush(), and use fentry bpf programs to
        attach to it and flush bpf stats.
      - Instead of using helpers, use kfuncs for rstat functions.
      - These changes simplify the patchset greatly, with minimal changes to
        uapi.
      
      RFC v1 -> RFC v2:
      - Instead of rstat flush programs attach to subsystems, they now attach
        to rstat (global flushers, not per-subsystem), based on discussions
        with Tejun. The first patch is entirely rewritten.
      - Pass cgroup pointers to rstat flushers instead of cgroup ids. This is
        much more flexibility and less likely to need a uapi update later.
      - rstat helpers are now only defined if CGROUP_CONFIG.
      - Most of the code is now only defined if CGROUP_CONFIG and
        CONFIG_BPF_SYSCALL.
      - Move rstat helper protos from bpf_base_func_proto() to
        tracing_prog_func_proto().
      - rstat helpers argument (cgroup pointer) is now ARG_PTR_TO_BTF_ID, not
        ARG_ANYTHING.
      - Rewrote the selftest to use the cgroup helpers.
      - Dropped bpf_map_lookup_percpu_elem (already added by Feng).
      - Dropped patch to support cgroup v1 for cgroup_iter.
      - Dropped patch to define some cgroup_put() when !CONFIG_CGROUP. The
        code that calls it is no longer compiled when !CONFIG_CGROUP.
      
      cgroup_iter was originally introduced in a different patch series[2].
      Hao and I agreed that it fits better as part of this series.
      RFC v1 of this patch series had the following changes from [2]:
      - Getting the cgroup's reference at the time at attaching, instead of
        at the time when iterating. (Yonghong)
      - Remove .init_seq_private and .fini_seq_private callbacks for
        cgroup_iter. They are not needed now. (Yonghong)
      
      [1] https://lore.kernel.org/bpf/20220520221919.jnqgv52k4ajlgzcl@MBP-98dd607d3435.dhcp.thefacebook.com/
      [2] https://lore.kernel.org/lkml/20220225234339.2386398-9-haoluo@google.com/
      
      Hao Luo (2):
        bpf: Introduce cgroup iter
        selftests/bpf: Test cgroup_iter.
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      eef3c3d3
    • Yosry Ahmed's avatar
      selftests/bpf: add a selftest for cgroup hierarchical stats collection · 88886309
      Yosry Ahmed authored
      Add a selftest that tests the whole workflow for collecting,
      aggregating (flushing), and displaying cgroup hierarchical stats.
      
      TL;DR:
      - Userspace program creates a cgroup hierarchy and induces memcg reclaim
        in parts of it.
      - Whenever reclaim happens, vmscan_start and vmscan_end update
        per-cgroup percpu readings, and tell rstat which (cgroup, cpu) pairs
        have updates.
      - When userspace tries to read the stats, vmscan_dump calls rstat to flush
        the stats, and outputs the stats in text format to userspace (similar
        to cgroupfs stats).
      - rstat calls vmscan_flush once for every (cgroup, cpu) pair that has
        updates, vmscan_flush aggregates cpu readings and propagates updates
        to parents.
      - Userspace program makes sure the stats are aggregated and read
        correctly.
      
      Detailed explanation:
      - The test loads tracing bpf programs, vmscan_start and vmscan_end, to
        measure the latency of cgroup reclaim. Per-cgroup readings are stored in
        percpu maps for efficiency. When a cgroup reading is updated on a cpu,
        cgroup_rstat_updated(cgroup, cpu) is called to add the cgroup to the
        rstat updated tree on that cpu.
      
      - A cgroup_iter program, vmscan_dump, is loaded and pinned to a file, for
        each cgroup. Reading this file invokes the program, which calls
        cgroup_rstat_flush(cgroup) to ask rstat to propagate the updates for all
        cpus and cgroups that have updates in this cgroup's subtree. Afterwards,
        the stats are exposed to the user. vmscan_dump returns 1 to terminate
        iteration early, so that we only expose stats for one cgroup per read.
      
      - An ftrace program, vmscan_flush, is also loaded and attached to
        bpf_rstat_flush. When rstat flushing is ongoing, vmscan_flush is invoked
        once for each (cgroup, cpu) pair that has updates. cgroups are popped
        from the rstat tree in a bottom-up fashion, so calls will always be
        made for cgroups that have updates before their parents. The program
        aggregates percpu readings to a total per-cgroup reading, and also
        propagates them to the parent cgroup. After rstat flushing is over, all
        cgroups will have correct updated hierarchical readings (including all
        cpus and all their descendants).
      
      - Finally, the test creates a cgroup hierarchy and induces memcg reclaim
        in parts of it, and makes sure that the stats collection, aggregation,
        and reading workflow works as expected.
      Signed-off-by: default avatarYosry Ahmed <yosryahmed@google.com>
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Link: https://lore.kernel.org/r/20220824233117.1312810-6-haoluo@google.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      88886309