Skip to content

Commit 2ddef17

Browse files
Martin KaFai LauAlexei Starovoitov
authored andcommitted
bpf: Allow refcounted bpf_rb_node used in bpf_rbtree_{remove,left,right}
The bpf_rbtree_{remove,left,right} requires the root's lock to be held. They also check the node_internal->owner is still owned by that root before proceeding, so it is safe to allow refcounted bpf_rb_node pointer to be used in these kfuncs. In a bpf fq implementation which is much closer to the kernel fq, https://lore.kernel.org/bpf/[email protected]/, a networking flow (allocated by bpf_obj_new) can be added to two different rbtrees. There are cases that the flow is searched from one rbtree, held the refcount of the flow, and then removed from another rbtree: struct fq_flow { struct bpf_rb_node fq_node; struct bpf_rb_node rate_node; struct bpf_refcount refcount; unsigned long sk_long; }; int bpf_fq_enqueue(...) { /* ... */ bpf_spin_lock(&root->lock); while (can_loop) { /* ... */ if (!p) break; gc_f = bpf_rb_entry(p, struct fq_flow, fq_node); if (gc_f->sk_long == sk_long) { f = bpf_refcount_acquire(gc_f); break; } /* ... */ } bpf_spin_unlock(&root->lock); if (f) { bpf_spin_lock(&q->lock); bpf_rbtree_remove(&q->delayed, &f->rate_node); bpf_spin_unlock(&q->lock); } } bpf_rbtree_{left,right} do not need this change but are relaxed together with bpf_rbtree_remove instead of adding extra verifier logic to exclude these kfuncs. To avoid bi-sect failure, this patch also changes the selftests together. The "rbtree_api_remove_unadded_node" is not expecting verifier's error. The test now expects bpf_rbtree_remove(&groot, &m->node) to return NULL. The test uses __retval(0) to ensure this NULL return value. Some of the "only take non-owning..." failure messages are changed also. Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 9e3e66c commit 2ddef17

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

kernel/bpf/verifier.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13229,8 +13229,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1322913229
return -EINVAL;
1323013230
}
1323113231
} else {
13232-
if (!type_is_non_owning_ref(reg->type) || reg->ref_obj_id) {
13233-
verbose(env, "%s node input must be non-owning ref\n", func_name);
13232+
if (!type_is_non_owning_ref(reg->type) && !reg->ref_obj_id) {
13233+
verbose(env, "%s can only take non-owning or refcounted bpf_rb_node pointer\n", func_name);
1323413234
return -EINVAL;
1323513235
}
1323613236
if (in_rbtree_lock_required_cb(env)) {

tools/testing/selftests/bpf/progs/rbtree_fail.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ long rbtree_api_nolock_first(void *ctx)
6969
}
7070

7171
SEC("?tc")
72-
__failure __msg("rbtree_remove node input must be non-owning ref")
72+
__retval(0)
7373
long rbtree_api_remove_unadded_node(void *ctx)
7474
{
7575
struct node_data *n, *m;
76-
struct bpf_rb_node *res;
76+
struct bpf_rb_node *res_n, *res_m;
7777

7878
n = bpf_obj_new(typeof(*n));
7979
if (!n)
@@ -88,19 +88,20 @@ long rbtree_api_remove_unadded_node(void *ctx)
8888
bpf_spin_lock(&glock);
8989
bpf_rbtree_add(&groot, &n->node, less);
9090

91-
/* This remove should pass verifier */
92-
res = bpf_rbtree_remove(&groot, &n->node);
93-
n = container_of(res, struct node_data, node);
91+
res_n = bpf_rbtree_remove(&groot, &n->node);
9492

95-
/* This remove shouldn't, m isn't in an rbtree */
96-
res = bpf_rbtree_remove(&groot, &m->node);
97-
m = container_of(res, struct node_data, node);
93+
res_m = bpf_rbtree_remove(&groot, &m->node);
9894
bpf_spin_unlock(&glock);
9995

100-
if (n)
101-
bpf_obj_drop(n);
102-
if (m)
103-
bpf_obj_drop(m);
96+
bpf_obj_drop(m);
97+
if (res_n)
98+
bpf_obj_drop(container_of(res_n, struct node_data, node));
99+
if (res_m) {
100+
bpf_obj_drop(container_of(res_m, struct node_data, node));
101+
/* m was not added to the rbtree */
102+
return 2;
103+
}
104+
104105
return 0;
105106
}
106107

@@ -178,7 +179,7 @@ long rbtree_api_use_unchecked_remove_retval(void *ctx)
178179
}
179180

180181
SEC("?tc")
181-
__failure __msg("rbtree_remove node input must be non-owning ref")
182+
__failure __msg("bpf_rbtree_remove can only take non-owning or refcounted bpf_rb_node pointer")
182183
long rbtree_api_add_release_unlock_escape(void *ctx)
183184
{
184185
struct node_data *n;
@@ -202,7 +203,7 @@ long rbtree_api_add_release_unlock_escape(void *ctx)
202203
}
203204

204205
SEC("?tc")
205-
__failure __msg("rbtree_remove node input must be non-owning ref")
206+
__failure __msg("bpf_rbtree_remove can only take non-owning or refcounted bpf_rb_node pointer")
206207
long rbtree_api_first_release_unlock_escape(void *ctx)
207208
{
208209
struct bpf_rb_node *res;

0 commit comments

Comments
 (0)