1. 25 Mar, 2023 1 commit
  2. 14 Mar, 2023 1 commit
  3. 11 Mar, 2023 1 commit
    • Dave Marchevsky's avatar
      bpf: Support __kptr to local kptrs · c8e18754
      Dave Marchevsky authored
      
      If a PTR_TO_BTF_ID type comes from program BTF - not vmlinux or module
      BTF - it must have been allocated by bpf_obj_new and therefore must be
      free'd with bpf_obj_drop. Such a PTR_TO_BTF_ID is considered a "local
      kptr" and is tagged with MEM_ALLOC type tag by bpf_obj_new.
      
      This patch adds support for treating __kptr-tagged pointers to "local
      kptrs" as having an implicit bpf_obj_drop destructor for referenced kptr
      acquire / release semantics. Consider the following example:
      
        struct node_data {
                long key;
                long data;
                struct bpf_rb_node node;
        };
      
        struct map_value {
                struct node_data __kptr *node;
        };
      
        struct {
                __uint(type, BPF_MAP_TYPE_ARRAY);
                __type(key, int);
                __type(value, struct map_value);
                __uint(max_entries, 1);
        } some_nodes SEC(".maps");
      
      If struct node_data had a matching definition in kernel BTF, the verifier would
      expect a destructor for the type to be registered. Since struct node_data does
      not match any type in kernel BTF, the verifier knows that there is no kfunc
      that provides a PTR_TO_BTF_ID to this type, and that such a PTR_TO_BTF_ID can
      only come from bpf_obj_new. So instead of searching for a registered dtor,
      a bpf_obj_drop dtor can be assumed.
      
      This allows the runtime to properly destruct such kptrs in
      bpf_obj_free_fields, which enables maps to clean up map_vals w/ such
      kptrs when going away.
      
      Implementation notes:
        * "kernel_btf" variable is renamed to "kptr_btf" in btf_parse_kptr.
          Before this patch, the variable would only ever point to vmlinux or
          module BTFs, but now it can point to some program BTF for local kptr
          type. It's later used to populate the (btf, btf_id) pair in kptr btf
          field.
        * It's necessary to btf_get the program BTF when populating btf_field
          for local kptr. btf_record_free later does a btf_put.
        * Behavior for non-local referenced kptrs is not modified, as
          bpf_find_btf_id helper only searches vmlinux and module BTFs for
          matching BTF type. If such a type is found, btf_field_kptr's btf will
          pass btf_is_kernel check, and the associated release function is
          some one-argument dtor. If btf_is_kernel check fails, associated
          release function is two-arg bpf_obj_drop_impl. Before this patch
          only btf_field_kptr's w/ kernel or module BTFs were created.
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      Link: https://lore.kernel.org/r/20230310230743.2320707-2-davemarchevsky@fb.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      c8e18754
  4. 09 Mar, 2023 1 commit
    • Andrii Nakryiko's avatar
      bpf: implement numbers iterator · 6018e1f4
      Andrii Nakryiko authored
      
      Implement the first open-coded iterator type over a range of integers.
      
      It's public API consists of:
        - bpf_iter_num_new() constructor, which accepts [start, end) range
          (that is, start is inclusive, end is exclusive).
        - bpf_iter_num_next() which will keep returning read-only pointer to int
          until the range is exhausted, at which point NULL will be returned.
          If bpf_iter_num_next() is kept calling after this, NULL will be
          persistently returned.
        - bpf_iter_num_destroy() destructor, which needs to be called at some
          point to clean up iterator state. BPF verifier enforces that iterator
          destructor is called at some point before BPF program exits.
      
      Note that `start = end = X` is a valid combination to setup an empty
      iterator. bpf_iter_num_new() will return 0 (success) for any such
      combination.
      
      If bpf_iter_num_new() detects invalid combination of input arguments, it
      returns error, resets iterator state to, effectively, empty iterator, so
      any subsequent call to bpf_iter_num_next() will keep returning NULL.
      
      BPF verifier has no knowledge that returned integers are in the
      [start, end) value range, as both `start` and `end` are not statically
      known and enforced: they are runtime values.
      
      While the implementation is pretty trivial, some care needs to be taken
      to avoid overflows and underflows. Subsequent selftests will validate
      correctness of [start, end) semantics, especially around extremes
      (INT_MIN and INT_MAX).
      
      Similarly to bpf_loop(), we enforce that no more than BPF_MAX_LOOPS can
      be specified.
      
      bpf_iter_num_{new,next,destroy}() is a logical evolution from bounded
      BPF loops and bpf_loop() helper and is the basis for implementing
      ergonomic BPF loops with no statically known or verified bounds.
      Subsequent patches implement bpf_for() macro, demonstrating how this can
      be wrapped into something that works and feels like a normal for() loop
      in C language.
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20230308184121.1165081-5-andrii@kernel.org
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      6018e1f4
  5. 03 Mar, 2023 3 commits
  6. 02 Mar, 2023 2 commits
  7. 01 Mar, 2023 3 commits
    • Joanne Koong's avatar
      bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr · 66e3a13e
      Joanne Koong authored
      
      Two new kfuncs are added, bpf_dynptr_slice and bpf_dynptr_slice_rdwr.
      The user must pass in a buffer to store the contents of the data slice
      if a direct pointer to the data cannot be obtained.
      
      For skb and xdp type dynptrs, these two APIs are the only way to obtain
      a data slice. However, for other types of dynptrs, there is no
      difference between bpf_dynptr_slice(_rdwr) and bpf_dynptr_data.
      
      For skb type dynptrs, the data is copied into the user provided buffer
      if any of the data is not in the linear portion of the skb. For xdp type
      dynptrs, the data is copied into the user provided buffer if the data is
      between xdp frags.
      
      If the skb is cloned and a call to bpf_dynptr_data_rdwr is made, then
      the skb will be uncloned (see bpf_unclone_prologue()).
      
      Please note that any bpf_dynptr_write() automatically invalidates any prior
      data slices of the skb dynptr. This is because the skb may be cloned or
      may need to pull its paged buffer into the head. As such, any
      bpf_dynptr_write() will automatically have its prior data slices
      invalidated, even if the write is to data in the skb head of an uncloned
      skb. Please note as well that any other helper calls that change the
      underlying packet buffer (eg bpf_skb_pull_data()) invalidates any data
      slices of the skb dynptr as well, for the same reasons.
      Signed-off-by: default avatarJoanne Koong <joannelkoong@gmail.com>
      Link: https://lore.kernel.org/r/20230301154953.641654-10-joannelkoong@gmail.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      66e3a13e
    • Joanne Koong's avatar
      bpf: Add xdp dynptrs · 05421aec
      Joanne Koong authored
      
      Add xdp dynptrs, which are dynptrs whose underlying pointer points
      to a xdp_buff. The dynptr acts on xdp data. xdp dynptrs have two main
      benefits. One is that they allow operations on sizes that are not
      statically known at compile-time (eg variable-sized accesses).
      Another is that parsing the packet data through dynptrs (instead of
      through direct access of xdp->data and xdp->data_end) can be more
      ergonomic and less brittle (eg does not need manual if checking for
      being within bounds of data_end).
      
      For reads and writes on the dynptr, this includes reading/writing
      from/to and across fragments. Data slices through the bpf_dynptr_data
      API are not supported; instead bpf_dynptr_slice() and
      bpf_dynptr_slice_rdwr() should be used.
      
      For examples of how xdp dynptrs can be used, please see the attached
      selftests.
      Signed-off-by: default avatarJoanne Koong <joannelkoong@gmail.com>
      Link: https://lore.kernel.org/r/20230301154953.641654-9-joannelkoong@gmail.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      05421aec
    • Joanne Koong's avatar
      bpf: Add skb dynptrs · b5964b96
      Joanne Koong authored
      
      Add skb dynptrs, which are dynptrs whose underlying pointer points
      to a skb. The dynptr acts on skb data. skb dynptrs have two main
      benefits. One is that they allow operations on sizes that are not
      statically known at compile-time (eg variable-sized accesses).
      Another is that parsing the packet data through dynptrs (instead of
      through direct access of skb->data and skb->data_end) can be more
      ergonomic and less brittle (eg does not need manual if checking for
      being within bounds of data_end).
      
      For bpf prog types that don't support writes on skb data, the dynptr is
      read-only (bpf_dynptr_write() will return an error)
      
      For reads and writes through the bpf_dynptr_read() and bpf_dynptr_write()
      interfaces, reading and writing from/to data in the head as well as from/to
      non-linear paged buffers is supported. Data slices through the
      bpf_dynptr_data API are not supported; instead bpf_dynptr_slice() and
      bpf_dynptr_slice_rdwr() (added in subsequent commit) should be used.
      
      For examples of how skb dynptrs can be used, please see the attached
      selftests.
      Signed-off-by: default avatarJoanne Koong <joannelkoong@gmail.com>
      Link: https://lore.kernel.org/r/20230301154953.641654-8-joannelkoong@gmail.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      b5964b96
  8. 28 Feb, 2023 1 commit
  9. 23 Feb, 2023 1 commit
  10. 14 Feb, 2023 2 commits
    • Dave Marchevsky's avatar
      bpf: Add bpf_rbtree_{add,remove,first} kfuncs · bd1279ae
      Dave Marchevsky authored
      
      This patch adds implementations of bpf_rbtree_{add,remove,first}
      and teaches verifier about their BTF_IDs as well as those of
      bpf_rb_{root,node}.
      
      All three kfuncs have some nonstandard component to their verification
      that needs to be addressed in future patches before programs can
      properly use them:
      
        * bpf_rbtree_add:     Takes 'less' callback, need to verify it
      
        * bpf_rbtree_first:   Returns ptr_to_node_type(off=rb_node_off) instead
                              of ptr_to_rb_node(off=0). Return value ref is
      			non-owning.
      
        * bpf_rbtree_remove:  Returns ptr_to_node_type(off=rb_node_off) instead
                              of ptr_to_rb_node(off=0). 2nd arg (node) is a
      			non-owning reference.
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      Link: https://lore.kernel.org/r/20230214004017.2534011-3-davemarchevsky@fb.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      bd1279ae
    • Dave Marchevsky's avatar
      bpf: Add basic bpf_rb_{root,node} support · 9c395c1b
      Dave Marchevsky authored
      
      This patch adds special BPF_RB_{ROOT,NODE} btf_field_types similar to
      BPF_LIST_{HEAD,NODE}, adds the necessary plumbing to detect the new
      types, and adds bpf_rb_root_free function for freeing bpf_rb_root in
      map_values.
      
      structs bpf_rb_root and bpf_rb_node are opaque types meant to
      obscure structs rb_root_cached rb_node, respectively.
      
      btf_struct_access will prevent BPF programs from touching these special
      fields automatically now that they're recognized.
      
      btf_check_and_fixup_fields now groups list_head and rb_root together as
      "graph root" fields and {list,rb}_node as "graph node", and does same
      ownership cycle checking as before. Note that this function does _not_
      prevent ownership type mixups (e.g. rb_root owning list_node) - that's
      handled by btf_parse_graph_root.
      
      After this patch, a bpf program can have a struct bpf_rb_root in a
      map_value, but not add anything to nor do anything useful with it.
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      Link: https://lore.kernel.org/r/20230214004017.2534011-2-davemarchevsky@fb.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      9c395c1b
  11. 01 Feb, 2023 1 commit
  12. 29 Dec, 2022 1 commit
  13. 19 Dec, 2022 3 commits
  14. 09 Dec, 2022 2 commits
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Use memmove for bpf_dynptr_{read,write} · 76d16077
      Kumar Kartikeya Dwivedi authored
      
      It may happen that destination buffer memory overlaps with memory dynptr
      points to. Hence, we must use memmove to correctly copy from dynptr to
      destination buffer, or source buffer to dynptr.
      
      This actually isn't a problem right now, as memcpy implementation falls
      back to memmove on detecting overlap and warns about it, but we
      shouldn't be relying on that.
      Acked-by: default avatarJoanne Koong <joannelkoong@gmail.com>
      Acked-by: default avatarDavid Vernet <void@manifault.com>
      Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/r/20221207204141.308952-7-memxor@gmail.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      76d16077
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Rework process_dynptr_func · 27060531
      Kumar Kartikeya Dwivedi authored
      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: 20571567
      
       ("bpf: Add bpf_user_ringbuf_drain() helper")
      Acked-by: default avatarJoanne Koong <joannelkoong@gmail.com>
      Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/r/20221207204141.308952-4-memxor@gmail.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      27060531
  15. 08 Dec, 2022 2 commits
  16. 07 Dec, 2022 1 commit
    • David Vernet's avatar
      bpf: Don't use rcu_users to refcount in task kfuncs · 156ed20d
      David Vernet authored
      A series of prior patches added some kfuncs that allow struct
      task_struct * objects to be used as kptrs. These kfuncs leveraged the
      'refcount_t rcu_users' field of the task for performing refcounting.
      This field was used instead of 'refcount_t usage', as we wanted to
      leverage the safety provided by RCU for ensuring a task's lifetime.
      
      A struct task_struct is refcounted by two different refcount_t fields:
      
      1. p->usage:     The "true" refcount field which task lifetime. The
      		 task is freed as soon as this refcount drops to 0.
      
      2. p->rcu_users: An "RCU users" refcount field which is statically
      		 initialized to 2, and is co-located in a union with
      		 a struct rcu_head field (p->rcu). p->rcu_users
      		 essentially encapsulates a single p->usage
      		 refcount, and when p->rcu_users goes to 0, an RCU
      		 callback is scheduled on the struct rcu_head which
      		 decrements the p->usage refcount.
      
      Our logic was that by using p->rcu_users, we would be able to use RCU to
      safely issue refcount_inc_not_zero() a task's rcu_users field to
      determine if a task could still be acquired, or was exiting.
      Unfortunately, this does not work due to p->rcu_users and p->rcu sharing
      a union. When p->rcu_users goes to 0, an RCU callback is scheduled to
      drop a single p->usage refcount, and because the fields share a union,
      the refcount immediately becomes nonzero again after the callback is
      scheduled.
      
      If we were to split the fields out of the union, this wouldn't be a
      problem. Doing so should also be rather non-controversial, as there are
      a number of places in struct task_struct that have padding which we
      could use to avoid growing the structure by splitting up the fields.
      
      For now, so as to fix the kfuncs to be correct, this patch instead
      updates bpf_task_acquire() and bpf_task_release() to use the p->usage
      field for refcounting via the get_task_struct() and put_task_struct()
      functions. Because we can no longer rely on RCU, the change also guts
      the bpf_task_acquire_not_zero() and bpf_task_kptr_get() functions
      pending a resolution on the above problem.
      
      In addition, the task fixes the kfunc and rcu_read_lock selftests to
      expect this new behavior.
      
      Fixes: 90660309 ("bpf: Add kfuncs for storing struct task_struct * as a kptr")
      Fixes: fca1aa75
      
       ("bpf: Handle MEM_RCU type properly")
      Reported-by: default avatarMatus Jokay <matus.jokay@stuba.sk>
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Link: https://lore.kernel.org/r/20221206210538.597606-1-void@manifault.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      156ed20d
  17. 04 Dec, 2022 1 commit
    • Yonghong Song's avatar
      bpf: Handle MEM_RCU type properly · fca1aa75
      Yonghong Song authored
      Commit 9bb00b28 ("bpf: Add kfunc bpf_rcu_read_lock/unlock()")
      introduced MEM_RCU and bpf_rcu_read_lock/unlock() support. In that
      commit, a rcu pointer is tagged with both MEM_RCU and PTR_TRUSTED
      so that it can be passed into kfuncs or helpers as an argument.
      
      Martin raised a good question in [1] such that the rcu pointer,
      although being able to accessing the object, might have reference
      count of 0. This might cause a problem if the rcu pointer is passed
      to a kfunc which expects trusted arguments where ref count should
      be greater than 0.
      
      This patch makes the following changes related to MEM_RCU pointer:
        - MEM_RCU pointer might be NULL (PTR_MAYBE_NULL).
        - Introduce KF_RCU so MEM_RCU ptr can be acquired with
          a KF_RCU tagged kfunc which assumes ref count of rcu ptr
          could be zero.
        - For mem access 'b = ptr->a', say 'ptr' is a MEM_RCU ptr, and
          'a' is tagged with __rcu as well. Let us mark 'b' as
          MEM_RCU | PTR_MAYBE_NULL.
      
       [1] https://lore.kernel.org/bpf/ac70f574-4023-664e-b711-e0d3b18117fd@linux.dev/
      
      Fixes: 9bb00b28
      
       ("bpf: Add kfunc bpf_rcu_read_lock/unlock()")
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221203184602.477272-1-yhs@fb.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      fca1aa75
  18. 24 Nov, 2022 3 commits
  19. 23 Nov, 2022 1 commit
  20. 22 Nov, 2022 2 commits
    • David Vernet's avatar
      bpf: Add bpf_cgroup_ancestor() kfunc · 5ca78670
      David Vernet authored
      
      struct cgroup * objects have a variably sized struct cgroup *ancestors[]
      field which stores pointers to their ancestor cgroups. If using a cgroup
      as a kptr, it can be useful to access these ancestors, but doing so
      requires variable offset accesses for PTR_TO_BTF_ID, which is currently
      unsupported.
      
      This is a very useful field to access for cgroup kptrs, as programs may
      wish to walk their ancestor cgroups when determining e.g. their
      proportional cpu.weight. So as to enable this functionality with cgroup
      kptrs before var_off is supported for PTR_TO_BTF_ID, this patch adds a
      bpf_cgroup_ancestor() kfunc which accesses the cgroup node on behalf of
      the caller, and acquires a reference on it. Once var_off is supported
      for PTR_TO_BTF_ID, and fields inside a struct can be marked as trusted
      so they retain the PTR_TRUSTED modifier when walked, this can be
      removed.
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Link: https://lore.kernel.org/r/20221122055458.173143-4-void@manifault.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      5ca78670
    • David Vernet's avatar
      bpf: Enable cgroups to be used as kptrs · fda01efc
      David Vernet authored
      
      Now that tasks can be used as kfuncs, and the PTR_TRUSTED flag is
      available for us to easily add basic acquire / get / release kfuncs, we
      can do the same for cgroups. This patch set adds the following kfuncs
      which enable using cgroups as kptrs:
      
      struct cgroup *bpf_cgroup_acquire(struct cgroup *cgrp);
      struct cgroup *bpf_cgroup_kptr_get(struct cgroup **cgrpp);
      void bpf_cgroup_release(struct cgroup *cgrp);
      
      A follow-on patch will add a selftest suite which validates these
      kfuncs.
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Link: https://lore.kernel.org/r/20221122055458.173143-2-void@manifault.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      fda01efc
  21. 20 Nov, 2022 5 commits
  22. 18 Nov, 2022 2 commits
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Introduce single ownership BPF linked list API · 8cab76ec
      Kumar Kartikeya Dwivedi authored
      
      Add a linked list API for use in BPF programs, where it expects
      protection from the bpf_spin_lock in the same allocation as the
      bpf_list_head. For now, only one bpf_spin_lock can be present hence that
      is assumed to be the one protecting the bpf_list_head.
      
      The following functions are added to kick things off:
      
      // Add node to beginning of list
      void bpf_list_push_front(struct bpf_list_head *head, struct bpf_list_node *node);
      
      // Add node to end of list
      void bpf_list_push_back(struct bpf_list_head *head, struct bpf_list_node *node);
      
      // Remove node at beginning of list and return it
      struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head);
      
      // Remove node at end of list and return it
      struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head);
      
      The lock protecting the bpf_list_head needs to be taken for all
      operations. The verifier ensures that the lock that needs to be taken is
      always held, and only the correct lock is taken for these operations.
      These checks are made statically by relying on the reg->id preserved for
      registers pointing into regions having both bpf_spin_lock and the
      objects protected by it. The comment over check_reg_allocation_locked in
      this change describes the logic in detail.
      
      Note that bpf_list_push_front and bpf_list_push_back are meant to
      consume the object containing the node in the 1st argument, however that
      specific mechanism is intended to not release the ref_obj_id directly
      until the bpf_spin_unlock is called. In this commit, nothing is done,
      but the next commit will be introducing logic to handle this case, so it
      has been left as is for now.
      
      bpf_list_pop_front and bpf_list_pop_back delete the first or last item
      of the list respectively, and return pointer to the element at the
      list_node offset. The user can then use container_of style macro to get
      the actual entry type. The verifier however statically knows the actual
      type, so the safety properties are still preserved.
      
      With these additions, programs can now manage their own linked lists and
      store their objects in them.
      Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/r/20221118015614.2013203-17-memxor@gmail.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      8cab76ec
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Introduce bpf_obj_drop · ac9f0605
      Kumar Kartikeya Dwivedi authored
      
      Introduce bpf_obj_drop, which is the kfunc used to free allocated
      objects (allocated using bpf_obj_new). Pairing with bpf_obj_new, it
      implicitly destructs the fields part of object automatically without
      user intervention.
      
      Just like the previous patch, btf_struct_meta that is needed to free up
      the special fields is passed as a hidden argument to the kfunc.
      
      For the user, a convenience macro hides over the kernel side kfunc which
      is named bpf_obj_drop_impl.
      
      Continuing the previous example:
      
      void prog(void) {
      	struct foo *f;
      
      	f = bpf_obj_new(typeof(*f));
      	if (!f)
      		return;
      	bpf_obj_drop(f);
      }
      Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/r/20221118015614.2013203-15-memxor@gmail.com
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      ac9f0605