From: Alexei Starovoitov bpf_spin_lock() saves the irq flags in the per-cpu variable irqsave_flags, and bpf_spin_unlock() restores them from there. One word per cpu is enough as long as a cpu takes no second bpf_spin_lock before it releases the first. bpf_list_head_free() and bpf_rb_root_free() take a second one. They lock the list or the tree that they free with __bpf_spin_lock_irqsave(), and they can run while a program holds a bpf_spin_lock of its own. bpf_list_add(), bpf_list_push_front(), bpf_list_push_back() and bpf_rbtree_add() drop the node when they fail. The drop frees a node that has no other reference, with its fields, and the node can have a list or a tree itself, like struct foo of the selftests' linked_list.h: bpf_spin_lock(&glock) irqsave_flags = on bpf_list_add(&ghead, foo, prev) prev is not in the list bpf_list_head_free(&foo->head) __bpf_spin_lock_irqsave() irqsave_flags = off bpf_spin_unlock(&glock) restores off The program returns with irqs off. Keep the flags of the two functions in a local variable, and take the lock with __bpf_spin_lock(). Both functions unlock before they drop the nodes, so nothing else runs between the save and the restore. A failed add drops the node since the commit below. Until bpf_list_add() existed, an add failed only for a node that a list or a tree still owned, with a reference of its own, so the drop freed the node only in a race with the release of that reference. bpf_list_add() fails for any node when @prev is not in the list. Fixes: d2dcc67df910 ("bpf: Migrate bpf_rbtree_add and bpf_list_push_{front,back} to possibly fail") Signed-off-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- kernel/bpf/helpers.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 6788489a2608..bc0dd36a2799 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2302,6 +2302,7 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head, struct bpf_spin_lock *spin_lock) { struct list_head *head = list_head, drain, *pos, *n; + unsigned long flags; BUILD_BUG_ON(sizeof(struct list_head) > sizeof(struct bpf_list_head)); BUILD_BUG_ON(__alignof__(struct list_head) > __alignof__(struct bpf_list_head)); @@ -2313,7 +2314,8 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head, * section, and end up doing map ops that call bpf_list_head_free for * the same map value again. */ - __bpf_spin_lock_irqsave(spin_lock); + local_irq_save(flags); + __bpf_spin_lock(spin_lock); if (!head->next || list_empty(head)) goto unlock; list_for_each_safe(pos, n, head) { @@ -2325,7 +2327,8 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head, } unlock: INIT_LIST_HEAD(head); - __bpf_spin_unlock_irqrestore(spin_lock); + __bpf_spin_unlock(spin_lock); + local_irq_restore(flags); while (!list_empty(&drain)) { struct bpf_list_node_kern *node; @@ -2362,19 +2365,22 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root, struct rb_root_cached orig_root, *root = rb_root; struct bpf_rb_node_kern *node; struct rb_node *pos, *n; + unsigned long flags; void *obj; BUILD_BUG_ON(sizeof(struct rb_root_cached) > sizeof(struct bpf_rb_root)); BUILD_BUG_ON(__alignof__(struct rb_root_cached) > __alignof__(struct bpf_rb_root)); - __bpf_spin_lock_irqsave(spin_lock); + local_irq_save(flags); + __bpf_spin_lock(spin_lock); orig_root = *root; bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) { node = rb_entry(pos, struct bpf_rb_node_kern, rb_node); WRITE_ONCE(node->owner, BPF_PTR_POISON); } *root = RB_ROOT_CACHED; - __bpf_spin_unlock_irqrestore(spin_lock); + __bpf_spin_unlock(spin_lock); + local_irq_restore(flags); bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) { obj = pos; base-commit: cbaaeff649b7aa09170cb31db001e5468c2726c0 -- 2.53.0