mirror of
https://github.com/torvalds/linux.git
synced 2026-07-28 01:55:51 +02:00
bpf: refactor __bpf_list_add to take insertion point via **prev_ptr
Refactor __bpf_list_add to accept (node, head, struct list_head **prev_ptr, ..) instead of (node, head, bool tail, ..). Load prev from *prev_ptr after INIT_LIST_HEAD(h), so we never dereference an uninitialized h->prev when head was 0-initialized (e.g. push_back passes &h->prev). When prev is not the list head, validate that prev is in the list via its owner. Prepares for bpf_list_add(head, new, prev, ..) to insert after a given list node. Signed-off-by: Kaitao Cheng <chengkaitao@kylinos.cn> Reviewed-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20260521032306.97118-6-kaitao.cheng@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
187baa1096
commit
e6919ff67c
|
|
@ -2478,9 +2478,11 @@ __bpf_kfunc void *bpf_refcount_acquire_impl(void *p__refcounted_kptr, void *meta
|
|||
|
||||
static int __bpf_list_add(struct bpf_list_node_kern *node,
|
||||
struct bpf_list_head *head,
|
||||
bool tail, struct btf_record *rec, u64 off)
|
||||
struct list_head **prev_ptr,
|
||||
struct btf_record *rec, u64 off)
|
||||
{
|
||||
struct list_head *n = &node->list_head, *h = (void *)head;
|
||||
struct list_head *prev;
|
||||
|
||||
/* If list_head was 0-initialized by map, bpf_obj_init_field wasn't
|
||||
* called on its fields, so init here
|
||||
|
|
@ -2488,19 +2490,31 @@ static int __bpf_list_add(struct bpf_list_node_kern *node,
|
|||
if (unlikely(!h->next))
|
||||
INIT_LIST_HEAD(h);
|
||||
|
||||
prev = *prev_ptr;
|
||||
|
||||
/* When prev is not the list head, it must be a node in this list. */
|
||||
if (prev != h) {
|
||||
struct bpf_list_node_kern *prev_kn =
|
||||
container_of(prev, struct bpf_list_node_kern, list_head);
|
||||
|
||||
if (unlikely(READ_ONCE(prev_kn->owner) != head))
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* node->owner != NULL implies !list_empty(n), no need to separately
|
||||
* check the latter
|
||||
*/
|
||||
if (cmpxchg(&node->owner, NULL, BPF_PTR_POISON)) {
|
||||
/* Only called from BPF prog, no need to migrate_disable */
|
||||
__bpf_obj_drop_impl((void *)n - off, rec, false);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (cmpxchg(&node->owner, NULL, BPF_PTR_POISON))
|
||||
goto fail;
|
||||
|
||||
tail ? list_add_tail(n, h) : list_add(n, h);
|
||||
list_add(n, prev);
|
||||
WRITE_ONCE(node->owner, head);
|
||||
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
/* Only called from BPF prog, no need to migrate_disable */
|
||||
__bpf_obj_drop_impl((void *)n - off, rec, false);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -2521,8 +2535,9 @@ __bpf_kfunc int bpf_list_push_front(struct bpf_list_head *head,
|
|||
u64 off)
|
||||
{
|
||||
struct bpf_list_node_kern *n = (void *)node;
|
||||
struct list_head *h = (void *)head;
|
||||
|
||||
return __bpf_list_add(n, head, false, meta ? meta->record : NULL, off);
|
||||
return __bpf_list_add(n, head, &h, meta ? meta->record : NULL, off);
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_list_push_front_impl(struct bpf_list_head *head,
|
||||
|
|
@ -2550,8 +2565,9 @@ __bpf_kfunc int bpf_list_push_back(struct bpf_list_head *head,
|
|||
u64 off)
|
||||
{
|
||||
struct bpf_list_node_kern *n = (void *)node;
|
||||
struct list_head *h = (void *)head;
|
||||
|
||||
return __bpf_list_add(n, head, true, meta ? meta->record : NULL, off);
|
||||
return __bpf_list_add(n, head, &h->prev, meta ? meta->record : NULL, off);
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_list_push_back_impl(struct bpf_list_head *head,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user