BPF List
 help / color / mirror / Atom feed
* [PATCH] bpf: Require a bpf_spin_lock for bpf_list_head and rb_root
@ 2026-11-26  1:14 Quan Sun
  2026-11-26  1:44 ` bot+bpf-ci
  2026-11-26  2:41 ` Alexei Starovoitov
  0 siblings, 2 replies; 3+ messages in thread
From: Quan Sun @ 2026-11-26  1:14 UTC (permalink / raw)
  To: bpf, ast, daniel; +Cc: andrii, eddyz87, emxor, Quan Sun

Commit 0de2046137f9 ("bpf: Implement verifier support for rqspinlock")
relaxed the btf_parse_fields() check so that a bpf_list_head or
bpf_rb_root is accepted with only a bpf_res_spin_lock in the containing
type:

	if ((btf_record_has_field(rec, BPF_LIST_HEAD) ||
-	     btf_record_has_field(rec, BPF_RB_ROOT)) && rec->spin_lock_off < 0) {
+	     btf_record_has_field(rec, BPF_RB_ROOT)) &&
+		 (rec->spin_lock_off < 0 && rec->res_spin_lock_off < 0)) {

The release path was not updated to match.  bpf_obj_free_fields() still
only knows struct bpf_spin_lock:

	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);

so the first map of this shape that is freed splats:

  struct poc_graph {
  	struct bpf_res_spin_lock lock;
  	struct bpf_rb_root root __contains(poc_node, node);
  };

  WARNING: kernel/bpf/syscall.c:880 at bpf_obj_free_fields+0x321/0x350, CPU#3: kworker/u16:5/67
  Workqueue: events_unbound bpf_map_free_deferred
  RIP: 0010:bpf_obj_free_fields+0x321/0x350
  RDX: ffffffffffffffea
  Call stack:
   array_map_free+0xa4/0x230
   bpf_map_free_deferred+0x122/0x200
   process_one_work+0x53e/0xdd0

RDX is -22, i.e. rec->spin_lock_off == -EINVAL.  The WARN_ON_ONCE() is
the only thing standing between the release path and

	bpf_rb_root_free(field, field_ptr, obj + -22)

which would run __bpf_spin_lock_irqsave() on a pointer before the start
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.

Nothing in the tree relies on the relaxed form yet: no type in kernel/
or tools/testing/selftests/bpf/ has both a bpf_res_spin_lock and a
bpf_list_head/bpf_rb_root, and the commit being fixed came without a
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
syscall.c already use btf_record_has_field(rec, BPF_SPIN_LOCK) and are
unaffected.

With this patch the BTF of the map above is rejected, so a record with a
res-only guarded container cannot be created at all:

  libbpf: BTF loading error: -EINVAL

This change was drafted with an AI coding assistant during a BPF
subsystem audit. I reviewed the code, built the kernel and ran the
reproducer before and after, and take responsibility for it.

Fixes: 0de2046137f9 ("bpf: Implement verifier support for rqspinlock")
Signed-off-by: Quan Sun <m202672221@hust.edu.cn>
---

Reproduce: boot bpf-next master (8dfce13340ae, 7.4.0-rc3-g8dfce13340ae,
CONFIG_KASAN=y) in qemu and only create then close an ARRAY map whose
value type has a bpf_res_spin_lock and a bpf_rb_root with a
__contains() tag.  No BPF program and no bpf_rbtree_add() is needed:
bpf_map_free_deferred() hits the WARN on the plain free path.

 kernel/bpf/btf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index c4386a9a1ad..1008bc4c073 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -4302,8 +4302,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
 
 	/* bpf_{list_head, rb_node} require bpf_spin_lock */
 	if ((btf_record_has_field(rec, BPF_LIST_HEAD) ||
-	     btf_record_has_field(rec, BPF_RB_ROOT)) &&
-		 (rec->spin_lock_off < 0 && rec->res_spin_lock_off < 0)) {
+	     btf_record_has_field(rec, BPF_RB_ROOT)) && rec->spin_lock_off < 0) {
 		ret = -EINVAL;
 		goto end;
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] bpf: Require a bpf_spin_lock for bpf_list_head and rb_root
  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
  1 sibling, 0 replies; 3+ messages in thread
From: bot+bpf-ci @ 2026-11-26  1:44 UTC (permalink / raw)
  To: m202672221, bpf, ast, daniel
  Cc: andrii, eddyz87, emxor, m202672221, ast, andrii, daniel,
	martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 2998 bytes --]

> bpf: Require a bpf_spin_lock for bpf_list_head and rb_root

TL;DR: Requiring bpf_spin_lock for list/rb roots may break existing programs
that use bpf_res_spin_lock, which now fail BTF or program load; fixing the
free path instead is suggested.

> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c

[ ... ]

> @@ -4302,8 +4302,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
>
>  	/* bpf_{list_head, rb_node} require bpf_spin_lock */
>  	if ((btf_record_has_field(rec, BPF_LIST_HEAD) ||
> -	     btf_record_has_field(rec, BPF_RB_ROOT)) &&
> -		 (rec->spin_lock_off < 0 && rec->res_spin_lock_off < 0)) {
> +	     btf_record_has_field(rec, BPF_RB_ROOT)) && rec->spin_lock_off < 0) {
>  		ret = -EINVAL;
>  		goto end;

Does this reject layouts that load and verify today?  The commit being
fixed says:

  The bpf_res_spin_lock can also be used to protect objects that require
  lock protection for their kfuncs, like BPF rbtree and linked list.

It also changed check_reg_allocation_locked() so the graph-root lock check
accepts res locks:

  -	s = find_lock_state(env->cur_state, REF_TYPE_LOCK, id, ptr);
  +	s = find_lock_state(env->cur_state, REF_TYPE_LOCK | REF_TYPE_RES_LOCK | REF_TYPE_RES_LOCK_IRQ,
  +			    id, ptr);

That verifier code is still present (now REF_TYPE_LOCK_MASK) and this
patch does not touch it.

With this patch, a struct with a direct bpf_rb_root or bpf_list_head
member plus a bpf_res_spin_lock makes btf_parse_fields() return -EINVAL,
and btf_parse_struct_metas() treats that as fatal, so BPF_BTF_LOAD fails.

For a global-data layout with both in the same .data.X section,
map_check_btf() leaves map->record as ERR_PTR(-EINVAL), and verification
then fails in process_spin_lock() ("has no valid bpf_res_spin_lock") or in
__process_kf_arg_ptr_to_graph_root() ("not found at offset").

Before this patch both layouts loaded and passed verification.  The only
defect was at teardown, where bpf_obj_free_fields() hit
WARN_ON_ONCE(rec->spin_lock_off < 0) and skipped the root.

The commit message says "Nothing in the tree relies on the relaxed form
yet".  That covers in-tree users only.  Out-of-tree programs, for example
sched_ext schedulers written for v6.15 and later, could use the
combination, although I have not confirmed any do.

Would teaching the free path to use rec->res_spin_lock_off be an
alternative?  That would address the release-time WARN without rejecting
programs that load today.

If the restriction is kept, the verifier's res-lock acceptance for graph
roots becomes unreachable and could be cleaned up in the same series.  Should the
commit message also say that a feature documented in 0de2046137f9 is being
withdrawn?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45736338787

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] bpf: Require a bpf_spin_lock for bpf_list_head and rb_root
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-11-26  2:41 UTC (permalink / raw)
  To: Quan Sun, bpf, daniel; +Cc: andrii, eddyz87, memxor

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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-26  2:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox