1. 17 9月, 2022 1 次提交
  2. 11 9月, 2022 1 次提交
  3. 08 9月, 2022 4 次提交
  4. 07 9月, 2022 1 次提交
  5. 26 8月, 2022 1 次提交
    • H
      bpf: Introduce cgroup iter · d4ccaf58
      Hao Luo 提交于
      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 the given cgroup.
      
      When attaching cgroup_iter, one can set a cgroup to the iter_link
      created from attaching. This cgroup is passed as a file descriptor
      or cgroup id and serves as the starting point of the walk. If no
      cgroup is specified, the starting point will be the root cgroup v2.
      
      For walking descendants, one can specify the order: either pre-order or
      post-order. For walking ancestors, the walk starts at the specified
      cgroup and ends at the root.
      
      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.
      
      Currently only one session is supported, which means, depending on the
      volume of data bpf program intends to send to user space, the number
      of cgroups that can be walked is limited. For example, given the current
      buffer size is 8 * PAGE_SIZE, if the program sends 64B data for each
      cgroup, assuming PAGE_SIZE is 4kb, the total number of cgroups that can
      be walked is 512. This is a limitation of cgroup_iter. If the output
      data is larger than the kernel buffer size, after all data in the
      kernel buffer is consumed by user space, the subsequent read() syscall
      will signal EOPNOTSUPP. In order to work around, the user may have to
      update their program to reduce the volume of data sent to output. For
      example, skip some uninteresting cgroups. In future, we may extend
      bpf_iter flags to allow customizing buffer size.
      Acked-by: NYonghong Song <yhs@fb.com>
      Acked-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NHao Luo <haoluo@google.com>
      Link: https://lore.kernel.org/r/20220824233117.1312810-2-haoluo@google.comSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      d4ccaf58
  6. 24 8月, 2022 1 次提交
  7. 19 8月, 2022 1 次提交
    • M
      bpf: net: Avoid sk_setsockopt() taking sk lock when called from bpf · 24426654
      Martin KaFai Lau 提交于
      Most of the code in bpf_setsockopt(SOL_SOCKET) are duplicated from
      the sk_setsockopt().  The number of supported optnames are
      increasing ever and so as the duplicated code.
      
      One issue in reusing sk_setsockopt() is that the bpf prog
      has already acquired the sk lock.  This patch adds a
      has_current_bpf_ctx() to tell if the sk_setsockopt() is called from
      a bpf prog.  The bpf prog calling bpf_setsockopt() is either running
      in_task() or in_serving_softirq().  Both cases have the current->bpf_ctx
      initialized.  Thus, the has_current_bpf_ctx() only needs to
      test !!current->bpf_ctx.
      
      This patch also adds sockopt_{lock,release}_sock() helpers
      for sk_setsockopt() to use.  These helpers will test
      has_current_bpf_ctx() before acquiring/releasing the lock.  They are
      in EXPORT_SYMBOL for the ipv6 module to use in a latter patch.
      
      Note on the change in sock_setbindtodevice().  sockopt_lock_sock()
      is done in sock_setbindtodevice() instead of doing the lock_sock
      in sock_bindtoindex(..., lock_sk = true).
      Reviewed-by: NStanislav Fomichev <sdf@google.com>
      Signed-off-by: NMartin KaFai Lau <kafai@fb.com>
      Link: https://lore.kernel.org/r/20220817061717.4175589-1-kafai@fb.comSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      24426654
  8. 10 8月, 2022 1 次提交
  9. 23 7月, 2022 2 次提交
  10. 22 7月, 2022 1 次提交
  11. 21 7月, 2022 1 次提交
  12. 13 7月, 2022 2 次提交
    • S
      bpf, x86: fix freeing of not-finalized bpf_prog_pack · 1d5f82d9
      Song Liu 提交于
      syzbot reported a few issues with bpf_prog_pack [1], [2]. This only happens
      with multiple subprogs. In jit_subprogs(), we first call bpf_int_jit_compile()
      on each sub program. And then, we call it on each sub program again. jit_data
      is not freed in the first call of bpf_int_jit_compile(). Similarly we don't
      call bpf_jit_binary_pack_finalize() in the first call of bpf_int_jit_compile().
      
      If bpf_int_jit_compile() failed for one sub program, we will call
      bpf_jit_binary_pack_finalize() for this sub program. However, we don't have a
      chance to call it for other sub programs. Then we will hit "goto out_free" in
      jit_subprogs(), and call bpf_jit_free on some subprograms that haven't got
      bpf_jit_binary_pack_finalize() yet.
      
      At this point, bpf_jit_binary_pack_free() is called and the whole 2MB page is
      freed erroneously.
      
      Fix this with a custom bpf_jit_free() for x86_64, which calls
      bpf_jit_binary_pack_finalize() if necessary. Also, with custom
      bpf_jit_free(), bpf_prog_aux->use_bpf_prog_pack is not needed any more,
      remove it.
      
      Fixes: 1022a549 ("bpf, x86_64: Use bpf_jit_binary_pack_alloc")
      [1] https://syzkaller.appspot.com/bug?extid=2f649ec6d2eea1495a8f
      [2] https://syzkaller.appspot.com/bug?extid=87f65c75f4a72db05445
      Reported-by: syzbot+2f649ec6d2eea1495a8f@syzkaller.appspotmail.com
      Reported-by: syzbot+87f65c75f4a72db05445@syzkaller.appspotmail.com
      Signed-off-by: NSong Liu <song@kernel.org>
      Link: https://lore.kernel.org/r/20220706002612.4013790-1-song@kernel.orgSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      1d5f82d9
    • R
      bpf: reparent bpf maps on memcg offlining · 4201d9ab
      Roman Gushchin 提交于
      The memory consumed by a bpf map is always accounted to the memory
      cgroup of the process which created the map. The map can outlive
      the memory cgroup if it's used by processes in other cgroups or
      is pinned on bpffs. In this case the map pins the original cgroup
      in the dying state.
      
      For other types of objects (slab objects, non-slab kernel allocations,
      percpu objects and recently LRU pages) there is a reparenting process
      implemented: on cgroup offlining charged objects are getting
      reassigned to the parent cgroup. Because all charges and statistics
      are fully recursive it's a fairly cheap operation.
      
      For efficiency and consistency with other types of objects, let's do
      the same for bpf maps. Fortunately thanks to the objcg API, the
      required changes are minimal.
      
      Please, note that individual allocations (slabs, percpu and large
      kmallocs) already have the reparenting mechanism. This commit adds
      it to the saved map->memcg pointer by replacing it to map->objcg.
      Because dying cgroups are not visible for a user and all charges are
      recursive, this commit doesn't bring any behavior changes for a user.
      
      v2:
        added a missing const qualifier
      Signed-off-by: NRoman Gushchin <roman.gushchin@linux.dev>
      Reviewed-by: NShakeel Butt <shakeelb@google.com>
      Link: https://lore.kernel.org/r/20220711162827.184743-1-roman.gushchin@linux.devSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      4201d9ab
  13. 30 6月, 2022 4 次提交
    • S
      bpf: expose bpf_{g,s}etsockopt to lsm cgroup · 9113d7e4
      Stanislav Fomichev 提交于
      I don't see how to make it nice without introducing btf id lists
      for the hooks where these helpers are allowed. Some LSM hooks
      work on the locked sockets, some are triggering early and
      don't grab any locks, so have two lists for now:
      
      1. LSM hooks which trigger under socket lock - minority of the hooks,
         but ideal case for us, we can expose existing BTF-based helpers
      2. LSM hooks which trigger without socket lock, but they trigger
         early in the socket creation path where it should be safe to
         do setsockopt without any locks
      3. The rest are prohibited. I'm thinking that this use-case might
         be a good gateway to sleeping lsm cgroup hooks in the future.
         We can either expose lock/unlock operations (and add tracking
         to the verifier) or have another set of bpf_setsockopt
         wrapper that grab the locks and might sleep.
      Reviewed-by: NMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: NStanislav Fomichev <sdf@google.com>
      Link: https://lore.kernel.org/r/20220628174314.1216643-7-sdf@google.comSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      9113d7e4
    • S
      bpf: minimize number of allocated lsm slots per program · c0e19f2c
      Stanislav Fomichev 提交于
      Previous patch adds 1:1 mapping between all 211 LSM hooks
      and bpf_cgroup program array. Instead of reserving a slot per
      possible hook, reserve 10 slots per cgroup for lsm programs.
      Those slots are dynamically allocated on demand and reclaimed.
      
      struct cgroup_bpf {
      	struct bpf_prog_array *    effective[33];        /*     0   264 */
      	/* --- cacheline 4 boundary (256 bytes) was 8 bytes ago --- */
      	struct hlist_head          progs[33];            /*   264   264 */
      	/* --- cacheline 8 boundary (512 bytes) was 16 bytes ago --- */
      	u8                         flags[33];            /*   528    33 */
      
      	/* XXX 7 bytes hole, try to pack */
      
      	struct list_head           storages;             /*   568    16 */
      	/* --- cacheline 9 boundary (576 bytes) was 8 bytes ago --- */
      	struct bpf_prog_array *    inactive;             /*   584     8 */
      	struct percpu_ref          refcnt;               /*   592    16 */
      	struct work_struct         release_work;         /*   608    72 */
      
      	/* size: 680, cachelines: 11, members: 7 */
      	/* sum members: 673, holes: 1, sum holes: 7 */
      	/* last cacheline: 40 bytes */
      };
      Reviewed-by: NMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: NStanislav Fomichev <sdf@google.com>
      Link: https://lore.kernel.org/r/20220628174314.1216643-5-sdf@google.comSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      c0e19f2c
    • S
      bpf: per-cgroup lsm flavor · 69fd337a
      Stanislav Fomichev 提交于
      Allow attaching to lsm hooks in the cgroup context.
      
      Attaching to per-cgroup LSM works exactly like attaching
      to other per-cgroup hooks. New BPF_LSM_CGROUP is added
      to trigger new mode; the actual lsm hook we attach to is
      signaled via existing attach_btf_id.
      
      For the hooks that have 'struct socket' or 'struct sock' as its first
      argument, we use the cgroup associated with that socket. For the rest,
      we use 'current' cgroup (this is all on default hierarchy == v2 only).
      Note that for some hooks that work on 'struct sock' we still
      take the cgroup from 'current' because some of them work on the socket
      that hasn't been properly initialized yet.
      
      Behind the scenes, we allocate a shim program that is attached
      to the trampoline and runs cgroup effective BPF programs array.
      This shim has some rudimentary ref counting and can be shared
      between several programs attaching to the same lsm hook from
      different cgroups.
      
      Note that this patch bloats cgroup size because we add 211
      cgroup_bpf_attach_type(s) for simplicity sake. This will be
      addressed in the subsequent patch.
      
      Also note that we only add non-sleepable flavor for now. To enable
      sleepable use-cases, bpf_prog_run_array_cg has to grab trace rcu,
      shim programs have to be freed via trace rcu, cgroup_bpf.effective
      should be also trace-rcu-managed + maybe some other changes that
      I'm not aware of.
      Reviewed-by: NMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: NStanislav Fomichev <sdf@google.com>
      Link: https://lore.kernel.org/r/20220628174314.1216643-4-sdf@google.comSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      69fd337a
    • S
      bpf: add bpf_func_t and trampoline helpers · af3f4134
      Stanislav Fomichev 提交于
      I'll be adding lsm cgroup specific helpers that grab
      trampoline mutex.
      
      No functional changes.
      Reviewed-by: NMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: NStanislav Fomichev <sdf@google.com>
      Link: https://lore.kernel.org/r/20220628174314.1216643-2-sdf@google.comSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      af3f4134
  14. 21 6月, 2022 1 次提交
    • E
      bpf: Inline calls to bpf_loop when callback is known · 1ade2371
      Eduard Zingerman 提交于
      Calls to `bpf_loop` are replaced with direct loops to avoid
      indirection. E.g. the following:
      
        bpf_loop(10, foo, NULL, 0);
      
      Is replaced by equivalent of the following:
      
        for (int i = 0; i < 10; ++i)
          foo(i, NULL);
      
      This transformation could be applied when:
      - callback is known and does not change during program execution;
      - flags passed to `bpf_loop` are always zero.
      
      Inlining logic works as follows:
      
      - During execution simulation function `update_loop_inline_state`
        tracks the following information for each `bpf_loop` call
        instruction:
        - is callback known and constant?
        - are flags constant and zero?
      - Function `optimize_bpf_loop` increases stack depth for functions
        where `bpf_loop` calls can be inlined and invokes `inline_bpf_loop`
        to apply the inlining. The additional stack space is used to spill
        registers R6, R7 and R8. These registers are used as loop counter,
        loop maximal bound and callback context parameter;
      
      Measurements using `benchs/run_bench_bpf_loop.sh` inside QEMU / KVM on
      i7-4710HQ CPU show a drop in latency from 14 ns/op to 2 ns/op.
      Signed-off-by: NEduard Zingerman <eddyz87@gmail.com>
      Acked-by: NSong Liu <songliubraving@fb.com>
      Link: https://lore.kernel.org/r/20220620235344.569325-4-eddyz87@gmail.comSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      1ade2371
  15. 17 6月, 2022 4 次提交
  16. 03 6月, 2022 1 次提交
  17. 24 5月, 2022 4 次提交
  18. 21 5月, 2022 2 次提交
  19. 14 5月, 2022 1 次提交
  20. 12 5月, 2022 2 次提交
  21. 11 5月, 2022 4 次提交