From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Quan Sun" <m202672221@hust.edu.cn>, <bpf@vger.kernel.org>,
<daniel@iogearbox.net>
Cc: <andrii@kernel.org>, <eddyz87@gmail.com>, <memxor@gmail.com>
Subject: Re: [PATCH] bpf: Require a bpf_spin_lock for bpf_list_head and rb_root
Date: Thu, 26 Nov 2026 02:41:06 +0000 [thread overview]
Message-ID: <DN4SNHSNQ7E8.1Q1SW1CKDHLZS@gmail.com> (raw)
In-Reply-To: <b887fe56f02f8578e41cf0009da7638fb6f8ec0c.1795627805.git.m202672221@hust.edu.cn>
On Wed, Nov 25, 2026 at 05:14 PM Quan Sun <m202672221@hust.edu.cn> wrote:
> of the object and then treat whatever it finds there as a lock. The
> guard holds today only because btf_parse_fields() stores a negative
> errno in spin_lock_off; it is a load-bearing coincidence, not an
> invariant the code states anywhere.
It's not a coincidence. btf_parse_fields() has
rec->spin_lock_off = -EINVAL;
and "< 0" is the test for a missing field. Your hunk uses the same test.
The damage is different. "continue" skips the root, so all nodes leak.
A refcounted node keeps a stale node->owner, like before 4a7910ee060d.
> test for that combination. Restore the original requirement and let a
> follow-up teach bpf_{list_head,rb_root}_free() about rqspinlock if
> res-only guarding is actually wanted. The BPF_F_LOCK checks in
It's wanted. The log of 0de2046137f9 says so. So does the log of
commit 7b7b8b596010 ("bpf: Reject resilient lock operations in rbtree
callbacks").
check_reg_allocation_locked() accepts res locks since v6.15.
Such progs load and run today. With this patch they stop loading.
The bug is in the free path. Fix it there.
It doesn't need the lock. Update and delete don't free the roots since
commit a3a81d247651 ("bpf: Cancel special fields on map value recycle").
Only these callers of bpf_obj_free_fields() can see a root:
caller why no prog holds the lock
----------------------------- ----------------------------------
array_map_free() the map is freed next
htab_free_prealloced_fields() the map is freed next
htab_mem_dtor() kfree() of the element is next
__bpf_obj_drop_impl() the last owning reference is gone
The verifier takes a lock or a root from a map value or an owning
reference only. a3a81d247651 is in v7.2. Older trees need it first.
res_lock_roots_drop() should return -EINVAL today due to the stale owner.
With your patch the BTF of refcounted_kptr.c doesn't load.
The diff goes in place of your patch.
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4e9a4d1..d6b170f 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -664,10 +664,8 @@ void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
void bpf_timer_cancel_and_free(void *timer);
void bpf_wq_cancel_and_free(void *timer);
void bpf_task_work_cancel_and_free(void *timer);
-void bpf_list_head_free(const struct btf_field *field, void *list_head,
- struct bpf_spin_lock *spin_lock);
-void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
- struct bpf_spin_lock *spin_lock);
+void bpf_list_head_free(const struct btf_field *field, void *list_head);
+void bpf_rb_root_free(const struct btf_field *field, void *rb_root);
u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena);
u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena);
/*
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 937a0d7..2dd0164 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2301,39 +2301,22 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
}
EXPORT_SYMBOL_GPL(bpf_base_func_proto);
-void bpf_list_head_free(const struct btf_field *field, void *list_head,
- struct bpf_spin_lock *spin_lock)
+/*
+ * bpf_list_head_free() and bpf_rb_root_free() run when the object with the
+ * root is freed. No prog can lock it or reach the root, so they take no lock.
+ */
+void bpf_list_head_free(const struct btf_field *field, void *list_head)
{
- struct list_head *head = list_head, drain, *pos, *n;
+ struct list_head *head = list_head, *pos, *n;
BUILD_BUG_ON(sizeof(struct list_head) > sizeof(struct bpf_list_head));
BUILD_BUG_ON(__alignof__(struct list_head) > __alignof__(struct bpf_list_head));
- INIT_LIST_HEAD(&drain);
- /* Do the actual list draining outside the lock to not hold the lock for
- * too long, and also prevent deadlocks if tracing programs end up
- * executing on entry/exit of functions called inside the critical
- * 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);
- if (!head->next || list_empty(head))
- goto unlock;
+ if (!head->next)
+ return;
list_for_each_safe(pos, n, head) {
struct bpf_list_node_kern *node;
- node = container_of(pos, struct bpf_list_node_kern, list_head);
- WRITE_ONCE(node->owner, BPF_PTR_POISON);
- list_move_tail(pos, &drain);
- }
-unlock:
- INIT_LIST_HEAD(head);
- __bpf_spin_unlock_irqrestore(spin_lock);
-
- while (!list_empty(&drain)) {
- struct bpf_list_node_kern *node;
-
- pos = drain.next;
node = container_of(pos, struct bpf_list_node_kern, list_head);
list_del_init(pos);
/* Ensure __bpf_list_add() sees the node as unlinked. */
@@ -2359,8 +2342,7 @@ unlock:
pos && ({ n = rb_next_postorder(pos); 1; }); \
pos = n)
-void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
- struct bpf_spin_lock *spin_lock)
+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;
@@ -2370,14 +2352,8 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
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);
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_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) {
obj = pos;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ec8e787..b105328 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -872,14 +872,10 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
unpin_uptr_kaddr(*(void **)field_ptr);
break;
case BPF_LIST_HEAD:
- if (WARN_ON_ONCE(rec->spin_lock_off < 0))
- continue;
- bpf_list_head_free(field, field_ptr, obj + rec->spin_lock_off);
+ bpf_list_head_free(field, field_ptr);
break;
case BPF_RB_ROOT:
- if (WARN_ON_ONCE(rec->spin_lock_off < 0))
- continue;
- bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off);
+ bpf_rb_root_free(field, field_ptr);
break;
case BPF_LIST_NODE:
case BPF_RB_NODE:
diff --git a/tools/testing/selftests/bpf/progs/refcounted_kptr.c b/tools/testing/selftests/bpf/progs/refcounted_kptr.c
index cae00f7..24551dd 100644
--- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c
+++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c
@@ -757,6 +757,50 @@ int list_push_back_uninit_head(void *ctx)
return ret;
}
+struct res_roots {
+ struct bpf_res_spin_lock lock;
+ struct bpf_rb_root root __contains(node_data, r);
+ struct bpf_list_head head __contains(node_data, l);
+};
+
+SEC("tc")
+__description("res_lock_roots_drop: drop of the roots unlinks the node")
+__success __retval(0)
+int res_lock_roots_drop(void *ctx)
+{
+ struct node_data *n, *m;
+ struct res_roots *o;
+ int err;
+
+ o = bpf_obj_new(typeof(*o));
+ if (!o)
+ return -1;
+ n = bpf_obj_new(typeof(*n));
+ if (!n) {
+ bpf_obj_drop(o);
+ return -2;
+ }
+
+ err = bpf_res_spin_lock(&o->lock);
+ if (!err) {
+ m = bpf_refcount_acquire(n);
+ err |= bpf_rbtree_add(&o->root, &m->r, less);
+ m = bpf_refcount_acquire(n);
+ err |= bpf_list_push_front(&o->head, &m->l);
+ bpf_res_spin_unlock(&o->lock);
+ }
+ bpf_obj_drop(o);
+
+ /* Both fail when the node is still owned by the roots in 'o' */
+ m = bpf_refcount_acquire(n);
+ bpf_spin_lock(&lock);
+ err |= bpf_rbtree_add(&root, &m->r, less);
+ err |= bpf_list_push_front(&head, &n->l);
+ bpf_spin_unlock(&lock);
+
+ return err;
+}
+
SEC("?tc")
__failure __msg("bpf_spin_lock at off=32 must be held for bpf_list_head")
long list_del_without_lock_fail(void *ctx)
Kumar's address had typo in Cc...
Taget bpf-next.
pw-bot: cr
prev parent reply other threads:[~2026-11-26 2:41 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 1:14 [PATCH] bpf: Require a bpf_spin_lock for bpf_list_head and rb_root Quan Sun
2026-11-26 1:44 ` bot+bpf-ci
2026-11-26 2:41 ` Alexei Starovoitov [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DN4SNHSNQ7E8.1Q1SW1CKDHLZS@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=m202672221@hust.edu.cn \
--cc=memxor@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox