1. 07 9月, 2022 1 次提交
  2. 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
  3. 24 8月, 2022 1 次提交
  4. 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
  5. 10 8月, 2022 1 次提交
  6. 23 7月, 2022 2 次提交
  7. 22 7月, 2022 1 次提交
  8. 21 7月, 2022 1 次提交
  9. 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
  10. 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
  11. 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
  12. 17 6月, 2022 4 次提交
  13. 03 6月, 2022 1 次提交
  14. 24 5月, 2022 4 次提交
  15. 21 5月, 2022 2 次提交
  16. 14 5月, 2022 1 次提交
  17. 12 5月, 2022 2 次提交
  18. 11 5月, 2022 4 次提交
  19. 28 4月, 2022 1 次提交
  20. 27 4月, 2022 1 次提交
  21. 26 4月, 2022 4 次提交
    • K
      bpf: Make BTF type match stricter for release arguments · 2ab3b380
      Kumar Kartikeya Dwivedi 提交于
      The current of behavior of btf_struct_ids_match for release arguments is
      that when type match fails, it retries with first member type again
      (recursively). Since the offset is already 0, this is akin to just
      casting the pointer in normal C, since if type matches it was just
      embedded inside parent sturct as an object. However, we want to reject
      cases for release function type matching, be it kfunc or BPF helpers.
      
      An example is the following:
      
      struct foo {
      	struct bar b;
      };
      
      struct foo *v = acq_foo();
      rel_bar(&v->b); // btf_struct_ids_match fails btf_types_are_same, then
      		// retries with first member type and succeeds, while
      		// it should fail.
      
      Hence, don't walk the struct and only rely on btf_types_are_same for
      strict mode. All users of strict mode must be dealing with zero offset
      anyway, since otherwise they would want the struct to be walked.
      Signed-off-by: NKumar Kartikeya Dwivedi <memxor@gmail.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220424214901.2743946-10-memxor@gmail.com
      2ab3b380
    • K
      bpf: Wire up freeing of referenced kptr · 14a324f6
      Kumar Kartikeya Dwivedi 提交于
      A destructor kfunc can be defined as void func(type *), where type may
      be void or any other pointer type as per convenience.
      
      In this patch, we ensure that the type is sane and capture the function
      pointer into off_desc of ptr_off_tab for the specific pointer offset,
      with the invariant that the dtor pointer is always set when 'kptr_ref'
      tag is applied to the pointer's pointee type, which is indicated by the
      flag BPF_MAP_VALUE_OFF_F_REF.
      
      Note that only BTF IDs whose destructor kfunc is registered, thus become
      the allowed BTF IDs for embedding as referenced kptr. Hence it serves
      the purpose of finding dtor kfunc BTF ID, as well acting as a check
      against the whitelist of allowed BTF IDs for this purpose.
      
      Finally, wire up the actual freeing of the referenced pointer if any at
      all available offsets, so that no references are leaked after the BPF
      map goes away and the BPF program previously moved the ownership a
      referenced pointer into it.
      
      The behavior is similar to BPF timers, where bpf_map_{update,delete}_elem
      will free any existing referenced kptr. The same case is with LRU map's
      bpf_lru_push_free/htab_lru_push_free functions, which are extended to
      reset unreferenced and free referenced kptr.
      
      Note that unlike BPF timers, kptr is not reset or freed when map uref
      drops to zero.
      Signed-off-by: NKumar Kartikeya Dwivedi <memxor@gmail.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220424214901.2743946-8-memxor@gmail.com
      14a324f6
    • K
      bpf: Adapt copy_map_value for multiple offset case · 4d7d7f69
      Kumar Kartikeya Dwivedi 提交于
      Since now there might be at most 10 offsets that need handling in
      copy_map_value, the manual shuffling and special case is no longer going
      to work. Hence, let's generalise the copy_map_value function by using
      a sorted array of offsets to skip regions that must be avoided while
      copying into and out of a map value.
      
      When the map is created, we populate the offset array in struct map,
      Then, copy_map_value uses this sorted offset array is used to memcpy
      while skipping timer, spin lock, and kptr. The array is allocated as
      in most cases none of these special fields would be present in map
      value, hence we can save on space for the common case by not embedding
      the entire object inside bpf_map struct.
      Signed-off-by: NKumar Kartikeya Dwivedi <memxor@gmail.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220424214901.2743946-6-memxor@gmail.com
      4d7d7f69
    • K
      bpf: Prevent escaping of kptr loaded from maps · 6efe152d
      Kumar Kartikeya Dwivedi 提交于
      While we can guarantee that even for unreferenced kptr, the object
      pointer points to being freed etc. can be handled by the verifier's
      exception handling (normal load patching to PROBE_MEM loads), we still
      cannot allow the user to pass these pointers to BPF helpers and kfunc,
      because the same exception handling won't be done for accesses inside
      the kernel. The same is true if a referenced pointer is loaded using
      normal load instruction. Since the reference is not guaranteed to be
      held while the pointer is used, it must be marked as untrusted.
      
      Hence introduce a new type flag, PTR_UNTRUSTED, which is used to mark
      all registers loading unreferenced and referenced kptr from BPF maps,
      and ensure they can never escape the BPF program and into the kernel by
      way of calling stable/unstable helpers.
      
      In check_ptr_to_btf_access, the !type_may_be_null check to reject type
      flags is still correct, as apart from PTR_MAYBE_NULL, only MEM_USER,
      MEM_PERCPU, and PTR_UNTRUSTED may be set for PTR_TO_BTF_ID. The first
      two are checked inside the function and rejected using a proper error
      message, but we still want to allow dereference of untrusted case.
      
      Also, we make sure to inherit PTR_UNTRUSTED when chain of pointers are
      walked, so that this flag is never dropped once it has been set on a
      PTR_TO_BTF_ID (i.e. trusted to untrusted transition can only be in one
      direction).
      
      In convert_ctx_accesses, extend the switch case to consider untrusted
      PTR_TO_BTF_ID in addition to normal PTR_TO_BTF_ID for PROBE_MEM
      conversion for BPF_LDX.
      Signed-off-by: NKumar Kartikeya Dwivedi <memxor@gmail.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220424214901.2743946-5-memxor@gmail.com
      6efe152d