8cab76ec63
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: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/r/20221118015614.2013203-17-memxor@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
67 lines
2.1 KiB
C
67 lines
2.1 KiB
C
#ifndef __BPF_EXPERIMENTAL__
|
|
#define __BPF_EXPERIMENTAL__
|
|
|
|
#include <vmlinux.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_core_read.h>
|
|
|
|
/* Description
|
|
* Allocates an object of the type represented by 'local_type_id' in
|
|
* program BTF. User may use the bpf_core_type_id_local macro to pass the
|
|
* type ID of a struct in program BTF.
|
|
*
|
|
* The 'local_type_id' parameter must be a known constant.
|
|
* The 'meta' parameter is a hidden argument that is ignored.
|
|
* Returns
|
|
* A pointer to an object of the type corresponding to the passed in
|
|
* 'local_type_id', or NULL on failure.
|
|
*/
|
|
extern void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
|
|
|
|
/* Convenience macro to wrap over bpf_obj_new_impl */
|
|
#define bpf_obj_new(type) ((type *)bpf_obj_new_impl(bpf_core_type_id_local(type), NULL))
|
|
|
|
/* Description
|
|
* Free an allocated object. All fields of the object that require
|
|
* destruction will be destructed before the storage is freed.
|
|
*
|
|
* The 'meta' parameter is a hidden argument that is ignored.
|
|
* Returns
|
|
* Void.
|
|
*/
|
|
extern void bpf_obj_drop_impl(void *kptr, void *meta) __ksym;
|
|
|
|
/* Convenience macro to wrap over bpf_obj_drop_impl */
|
|
#define bpf_obj_drop(kptr) bpf_obj_drop_impl(kptr, NULL)
|
|
|
|
/* Description
|
|
* Add a new entry to the beginning of the BPF linked list.
|
|
* Returns
|
|
* Void.
|
|
*/
|
|
extern void bpf_list_push_front(struct bpf_list_head *head, struct bpf_list_node *node) __ksym;
|
|
|
|
/* Description
|
|
* Add a new entry to the end of the BPF linked list.
|
|
* Returns
|
|
* Void.
|
|
*/
|
|
extern void bpf_list_push_back(struct bpf_list_head *head, struct bpf_list_node *node) __ksym;
|
|
|
|
/* Description
|
|
* Remove the entry at the beginning of the BPF linked list.
|
|
* Returns
|
|
* Pointer to bpf_list_node of deleted entry, or NULL if list is empty.
|
|
*/
|
|
extern struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head) __ksym;
|
|
|
|
/* Description
|
|
* Remove the entry at the end of the BPF linked list.
|
|
* Returns
|
|
* Pointer to bpf_list_node of deleted entry, or NULL if list is empty.
|
|
*/
|
|
extern struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head) __ksym;
|
|
|
|
#endif
|