bpf: A fq example similar to the kernel sch_fq.c implementation
Subsystems (7)
Martin KaFai Lau
reviewercover bpf: A fq example similar to the kernel sch_fq.c implementation
From: Martin KaFai Lau <[email protected]>
This patch is to show a fq implementation closer to the sch_fq.c and how it can be done in bpf.
The first part of the set adds a few rbtree and list traverse kfuncs.
The second part adds a bpf qdisc specific kfunc and then a new bpf_sch_fq.c implementation in the selftest.
The second part of the set depends on the bpf qdisc changes in the bpf-next/net. If bpf change is viable, it will be easier to merge the first part of the set with the bpf specific changes to the bpf-next/master first. The second part depending on the bpf qdisc piece can wait for the next cycle.
A few more bpf tests are still needed and a few things may still need to clean up in the bpf_sch_fq.c, so in RFC.
Martin KaFai Lau (12):
bpf: Check KF_bpf_rbtree_add_impl for the "case KF_ARG_PTR_TO_RB_NODE"
bpf: Simplify reg0 marking for the rbtree kfuncs that return a
bpf_rb_node pointer
bpf: Add bpf_rbtree_{root,left,right} kfunc
selftests/bpf: Adjust failure message in the rbtree_fail test
bpf: Allow refcounted bpf_rb_node used in
bpf_rbtree_{remove,left,right} selftests/bpf: Adjust test that does not allow refcounted node in
rbtree_remove
selftests/bpf: Add rbtree_search test
bpf: Simplify reg0 marking for the list kfuncs that return a
bpf_list_node pointer
bpf: Add bpf_list_{front,back} kfunc
selftests/bpf: Add test for bpf_list_{front,back}
bpf: net: Add a qdisc kfunc to set sk_pacing_status. selftests/bpf: A bpf fq implementation similar to the kernel sch_fq
kernel/bpf/helpers.c | 52 + kernel/bpf/verifier.c | 65 +- net/sched/bpf_qdisc.c | 14 + .../selftests/bpf/prog_tests/bpf_qdisc.c | 21 + .../selftests/bpf/prog_tests/linked_list.c | 2 + .../testing/selftests/bpf/prog_tests/rbtree.c | 6 + .../selftests/bpf/progs/bpf_qdisc_common.h | 97 +- .../selftests/bpf/progs/bpf_qdisc_fq.c | 2 - .../testing/selftests/bpf/progs/bpf_sch_fq.c | 1171 +++++++++++++++++ .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../selftests/bpf/progs/linked_list_peek.c | 104 ++ .../testing/selftests/bpf/progs/rbtree_fail.c | 26 +- .../selftests/bpf/progs/rbtree_search.c | 137 ++ 13 files changed, 1665 insertions(+), 33 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/bpf_sch_fq.c create mode 100644 tools/testing/selftests/bpf/progs/linked_list_peek.c create mode 100644 tools/testing/selftests/bpf/progs/rbtree_search.c
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 1/12 bpf: Check KF_bpf_rbtree_add_impl for the "case KF_ARG_PTR_TO_RB_NODE"
From: Martin KaFai Lau <[email protected]>
In a later patch, two new kfuncs will take the bpf_rb_node pointer arg.
struct bpf_rb_node *bpf_rbtree_left(struct bpf_rb_root *root,
struct bpf_rb_node *node); struct bpf_rb_node *bpf_rbtree_right(struct bpf_rb_root *root, struct bpf_rb_node *node);
In the check_kfunc_call, there is a "case KF_ARG_PTR_TO_RB_NODE" to check if the reg->type should be an allocated pointer or should be a non_owning_ref.
The later patch will need to ensure that the bpf_rb_node pointer passing to the new bpf_rbtree_{left,right} must be a non_owning_ref. This should be the same requirement as the existing bpf_rbtree_remove.
This patch swaps the current "if else" statement. Instead of checking the bpf_rbtree_remove, it checks the bpf_rbtree_add. Then the new bpf_rbtree_{left,right} will fall into the "else" case to make the later patch simpler. bpf_rbtree_add should be the only one that needs an allocated pointer.
This should be a no-op change considering there are only two kfunc(s) taking bpf_rb_node pointer arg, rbtree_add and rbtree_remove.
--- kernel/bpf/verifier.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
kernel/bpf/verifier.c +9 -9
@@ -13200,22 +13200,22 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_return ret;break ;case KF_ARG_PTR_TO_RB_NODE : -if (meta->func_id == special_kfunc_list[KF_bpf_rbtree_remove]) { -if (!type_is_non_owning_ref (reg->type) || reg->ref_obj_id) { -verbose (env,"rbtree_remove node input must be non-owning ref\n" ); +if (meta->func_id == special_kfunc_list[KF_bpf_rbtree_add_impl]) { +if (reg->type != (PTR_TO_BTF_ID |MEM_ALLOC )) { +verbose (env,"arg#%d expected pointer to allocated object\n" , i);return -EINVAL ; } -if (in_rbtree_lock_required_cb (env)) { -verbose (env,"rbtree_remove not allowed in rbtree cb\n" ); +if (!reg->ref_obj_id) { +verbose (env,"allocated object must be referenced\n" );return -EINVAL ; } }else { -if (reg->type != (PTR_TO_BTF_ID |MEM_ALLOC )) { -verbose (env,"arg#%d expected pointer to allocated object\n" , i); +if (!type_is_non_owning_ref (reg->type) || reg->ref_obj_id) { +verbose (env,"rbtree_remove node input must be non-owning ref\n" );return -EINVAL ; } -if (!reg->ref_obj_id) { -verbose (env,"allocated object must be referenced\n" ); +if (in_rbtree_lock_required_cb (env)) { +verbose (env,"rbtree_remove not allowed in rbtree cb\n" );return -EINVAL ; } }
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 2/12 bpf: Simplify reg0 marking for the rbtree kfuncs that return a bpf_rb_node pointer
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
The current rbtree kfunc, bpf_rbtree_{first, remove}, returns the bpf_rb_node pointer. The check_kfunc_call currently checks the kfunc btf_id instead of its return pointer type to decide
if it needs to do mark_reg_graph_node(reg0) and ref_set_non_owning(reg0).
The later patch will add bpf_rbtree_{root,left,right} that will also return a bpf_rb_node pointer. Instead of adding more kfunc btf_id checks to the "if" case, this patch changes the test to check the kfunc's return type. is_rbtree_node_type() function is added to test if a pointer type is a bpf_rb_node. The callers have already skipped the modifiers of the pointer type.
A note on the ref_set_non_owning(), although bpf_rbtree_remove() also returns a bpf_rb_node pointer, the bpf_rbtree_remove() has the KF_ACQUIRE flag. Thus, its reg0 will not become non-owning.
--- kernel/bpf/verifier.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +7 -3
@@ -11987,6 +11987,11 @@ static bool is_kfunc_arg_res_spin_lock(const struct btf *btf, const struct btf_preturn __is_kfunc_ptr_arg_type (btf, arg,KF_ARG_RES_SPIN_LOCK_ID ); } +static bool is_rbtree_node_type (const struct btf_type *t) +{ +return t ==btf_type_by_id (btf_vmlinux, kf_arg_btf_ids[KF_ARG_RB_NODE_ID ]); +} +static bool is_kfunc_arg_callback (struct bpf_verifier_env *env,const struct btf *btf,const struct btf_param *arg) {
@@ -13750,8 +13755,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,struct btf_field *field = meta.arg_list_head.field;mark_reg_graph_node (regs,BPF_REG_0 , &field->graph_root); - }else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_remove] || - meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) { + }else if (is_rbtree_node_type (ptr_type)) {struct btf_field *field = meta.arg_rbtree_root.field;mark_reg_graph_node (regs,BPF_REG_0 , &field->graph_root);
@@ -13881,7 +13885,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,if (is_kfunc_ret_null (&meta)) regs[BPF_REG_0 ].id = id; regs[BPF_REG_0 ].ref_obj_id = id; - }else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) { + }else if (is_rbtree_node_type (ptr_type)) {ref_set_non_owning (env, ®s[BPF_REG_0 ]); }
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 3/12 bpf: Add bpf_rbtree_{root,left,right} kfunc
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
In the kernel fq qdisc implementation, it requires to traverse a rbtree stored with the networking "flows".
In the later bpf selftests prog, the much simplified logic that uses the bpf_rbtree_{root,left,right} to traverse the tree is like:
struct fq_flow {
struct bpf_rb_node fq_node;
struct bpf_rb_node rate_node;
struct bpf_refcount refcount;
unsigned long sk_long;
};struct fq_flow_root {
struct bpf_spin_lock lock;
struct bpf_rb_root root __contains(fq_flow, fq_node);
};struct fq_flow *fq_classify(...)
{
struct bpf_rb_node *tofree[FQ_GC_MAX];
struct fq_flow_root *root;
struct fq_flow *gc_f, *f;
struct bpf_rb_node *p;
int i, fcnt = 0;
/* ... */
f = NULL;
bpf_spin_lock(&root->lock);
p = bpf_rbtree_root(&root->root);
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;
}
/* To be removed from the rbtree */
if (fcnt < FQ_GC_MAX && fq_gc_candidate(gc_f, jiffies_now))
tofree[fcnt++] = p;
if (gc_f->sk_long > sk_long)
p = bpf_rbtree_left(&root->root, p);
else
p = bpf_rbtree_right(&root->root, p);
}
/* remove from the rbtree */
for (i = 0; i < fcnt; i++) {
p = tofree[i];
tofree[i] = bpf_rbtree_remove(&root->root, p);
}
bpf_spin_unlock(&root->lock);
/* bpf_obj_drop the fq_flow(s) that have just been removed
* from the rbtree.
*/
for (i = 0; i < fcnt; i++) {
p = tofree[i];
if (p) {
gc_f = bpf_rb_entry(p, struct fq_flow, fq_node);
bpf_obj_drop(gc_f);
}
}
return f;}
The above simplified code needs to traverse the rbtree for two purposes,
1) find the flow with the desired sk_long value
2) while searching for the sk_long, collect flows that are the fq_gc_candidate. They will be removed from the rbtree.
This patch adds the bpf_rbtree_{root,left,right} kfunc to enable the rbtree traversal. The returned bpf_rb_node pointer will be a non-owning reference which is the same as the returned pointer of the exisiting bpf_rbtree_first kfunc.
--- kernel/bpf/helpers.c | 30 ++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 23 ++++++++++++++++++----- 2 files changed, 48 insertions(+), 5 deletions(-)
kernel/bpf/helpers.c +30 -0
@@ -2366,6 +2366,33 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root)return (struct bpf_rb_node *)rb_first_cached (r); } +__bpf_kfunc struct bpf_rb_node *bpf_rbtree_root(struct bpf_rb_root *root) +{ +struct rb_root_cached *r = (struct rb_root_cached *)root; + +return (struct bpf_rb_node *)r->rb_root.rb_node; +} + +__bpf_kfunc struct bpf_rb_node *bpf_rbtree_left(struct bpf_rb_root *root, struct bpf_rb_node *node) +{ +struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node; + +if (READ_ONCE (node_internal->owner) != root) +return NULL ; + +return (struct bpf_rb_node *)node_internal->rb_node.rb_left; +} + +__bpf_kfunc struct bpf_rb_node *bpf_rbtree_right(struct bpf_rb_root *root, struct bpf_rb_node *node) +{ +struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node; + +if (READ_ONCE (node_internal->owner) != root) +return NULL ; + +return (struct bpf_rb_node *)node_internal->rb_node.rb_right; +} +/** * bpf_task_acquire - Acquire a reference to a task. A task acquired by this * kfunc which is not stored in a map as a kptr, must be released by calling
@@ -3214,6 +3241,9 @@ BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_rbtree_remove, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_rbtree_add_impl) BTF_ID_FLAGS(func, bpf_rbtree_first, KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_rbtree_root, KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_rbtree_left, KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_rbtree_right, KF_RET_NULL)#ifdef CONFIG_CGROUPS BTF_ID_FLAGS (func, bpf_cgroup_acquire,KF_ACQUIRE |KF_RCU |KF_RET_NULL )
kernel/bpf/verifier.c +18 -5
@@ -12081,6 +12081,9 @@ enum special_kfunc_type { KF_bpf_rbtree_remove, KF_bpf_rbtree_add_impl, KF_bpf_rbtree_first, + KF_bpf_rbtree_root, + KF_bpf_rbtree_left, + KF_bpf_rbtree_right, KF_bpf_dynptr_from_skb, KF_bpf_dynptr_from_xdp, KF_bpf_dynptr_slice,
@@ -12121,6 +12124,9 @@ BTF_ID(func, bpf_rdonly_cast) BTF_ID(func, bpf_rbtree_remove) BTF_ID(func, bpf_rbtree_add_impl) BTF_ID(func, bpf_rbtree_first) +BTF_ID(func, bpf_rbtree_root) +BTF_ID(func, bpf_rbtree_left) +BTF_ID(func, bpf_rbtree_right)#ifdef CONFIG_NET BTF_ID(func, bpf_dynptr_from_skb)BTF_ID (func, bpf_dynptr_from_xdp)
@@ -12156,6 +12162,9 @@ BTF_ID(func, bpf_rcu_read_unlock) BTF_ID(func, bpf_rbtree_remove) BTF_ID(func, bpf_rbtree_add_impl) BTF_ID(func, bpf_rbtree_first) +BTF_ID(func, bpf_rbtree_root) +BTF_ID(func, bpf_rbtree_left) +BTF_ID(func, bpf_rbtree_right)#ifdef CONFIG_NET BTF_ID(func, bpf_dynptr_from_skb)BTF_ID (func, bpf_dynptr_from_xdp)
@@ -12591,7 +12600,10 @@ static bool is_bpf_rbtree_api_kfunc(u32 btf_id) {return btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl] || btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || - btf_id == special_kfunc_list[KF_bpf_rbtree_first]; + btf_id == special_kfunc_list[KF_bpf_rbtree_first] || + btf_id == special_kfunc_list[KF_bpf_rbtree_root] || + btf_id == special_kfunc_list[KF_bpf_rbtree_left] || + btf_id == special_kfunc_list[KF_bpf_rbtree_right]; }static bool is_bpf_iter_num_api_kfunc (u32 btf_id)
@@ -12691,7 +12703,9 @@ static bool check_kfunc_is_graph_node_api(struct bpf_verifier_env *env,break ;case BPF_RB_NODE : ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || - kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl]); + kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl] || + kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_left] || + kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_right]);break ;default : verbose(env, "verifier internal error: unexpected graph node argument type %s\n",
@@ -13216,15 +13230,14 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ } }else {if (!type_is_non_owning_ref (reg->type) || reg->ref_obj_id) { -verbose (env,"rbtree_remove node input must be non-owning ref\n" ); +verbose (env,"%s can only take non-owning bpf_rb_node pointer\n" , func_name);return -EINVAL ; }if (in_rbtree_lock_required_cb (env)) { -verbose (env,"rbtree_remove not allowed in rbtree cb\n" ); +verbose (env,"%s not allowed in rbtree cb\n" , func_name);return -EINVAL ; } } - ret =process_kf_arg_ptr_to_rbtree_node (env, reg, regno, meta);if (ret <0 )return ret;
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 4/12 selftests/bpf: Adjust failure message in the rbtree_fail test
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
Some of the failure messages in the rbtree_fail test. The message is now "bpf_rbtree_remove can only take non-owning bpf_rb_node pointer".
--- tools/testing/selftests/bpf/progs/rbtree_fail.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/progs/rbtree_fail.c +3 -3
@@ -69,7 +69,7 @@ long rbtree_api_nolock_first(void *ctx) } SEC("?tc") -__failure __msg("rbtree_remove node input must be non-owning ref") +__failure __msg("bpf_rbtree_remove can only take non-owning bpf_rb_node pointer") long rbtree_api_remove_unadded_node(void *ctx) {struct node_data *n, *m;
@@ -178,7 +178,7 @@ long rbtree_api_use_unchecked_remove_retval(void *ctx) } SEC("?tc") -__failure __msg("rbtree_remove node input must be non-owning ref") +__failure __msg("bpf_rbtree_remove can only take non-owning bpf_rb_node pointer") long rbtree_api_add_release_unlock_escape(void *ctx) {struct node_data *n;
@@ -202,7 +202,7 @@ long rbtree_api_add_release_unlock_escape(void *ctx) } SEC("?tc") -__failure __msg("rbtree_remove node input must be non-owning ref") +__failure __msg("bpf_rbtree_remove can only take non-owning bpf_rb_node pointer") long rbtree_api_first_release_unlock_escape(void *ctx) {struct bpf_rb_node *res;
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 5/12 bpf: Allow refcounted bpf_rb_node used in bpf_rbtree_{remove,left,right}
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
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 the later selftest, 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 the 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.
--- kernel/bpf/verifier.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +2 -2
@@ -13229,8 +13229,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_return -EINVAL ; } }else { -if (!type_is_non_owning_ref (reg->type) || reg->ref_obj_id) { -verbose (env,"%s can only take non-owning bpf_rb_node pointer\n" , func_name); +if (!type_is_non_owning_ref (reg->type) && !reg->ref_obj_id) { +verbose (env,"%s can only take non-owning or refcounted bpf_rb_node pointer\n" , func_name);return -EINVAL ; }if (in_rbtree_lock_required_cb (env)) {
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 6/12 selftests/bpf: Adjust test that does not allow refcounted node in rbtree_remove
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
rbtree_remove now allows refcounted node now. The rbtree_api_remove_unadded_node test needs to be adjusted.
First change, it does not expect a verifier's error now.
Second change, 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.
--- .../testing/selftests/bpf/progs/rbtree_fail.c | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-)
tools/testing/selftests/bpf/progs/rbtree_fail.c +14 -12
@@ -69,11 +69,11 @@ long rbtree_api_nolock_first(void *ctx) } SEC("?tc") -__failure __msg("bpf_rbtree_remove can only take non-owning bpf_rb_node pointer") +__retval(0)long rbtree_api_remove_unadded_node (void *ctx) {struct node_data *n, *m; -struct bpf_rb_node *res; +struct bpf_rb_node *res_n, *res_m; n =bpf_obj_new (typeof (*n));if (!n)
@@ -89,18 +89,20 @@ long rbtree_api_remove_unadded_node(void *ctx)bpf_rbtree_add (&groot, &n->node, less);/* This remove should pass verifier */ - res =bpf_rbtree_remove (&groot, &n->node); - n = container_of(res, struct node_data, node); + res_n =bpf_rbtree_remove (&groot, &n->node);/* This remove shouldn't, m isn't in an rbtree */ - res =bpf_rbtree_remove (&groot, &m->node); - m = container_of(res, struct node_data, node); + res_m =bpf_rbtree_remove (&groot, &m->node);bpf_spin_unlock (&glock); -if (n) -bpf_obj_drop (n); -if (m) -bpf_obj_drop (m); +bpf_obj_drop (m); +if (res_n) + bpf_obj_drop(container_of(res_n, struct node_data, node)); +if (res_m) { +bpf_obj_drop (container_of(res_m ,struct node_data ,node )); +return 2 ; + } +return 0 ; }
@@ -178,7 +180,7 @@ long rbtree_api_use_unchecked_remove_retval(void *ctx) } SEC("?tc") -__failure __msg("bpf_rbtree_remove can only take non-owning bpf_rb_node pointer") +__failure __msg("bpf_rbtree_remove can only take non-owning or refcounted bpf_rb_node pointer") long rbtree_api_add_release_unlock_escape(void *ctx) {struct node_data *n;
@@ -202,7 +204,7 @@ long rbtree_api_add_release_unlock_escape(void *ctx) } SEC("?tc") -__failure __msg("bpf_rbtree_remove can only take non-owning bpf_rb_node pointer") +__failure __msg("bpf_rbtree_remove can only take non-owning or refcounted bpf_rb_node pointer") long rbtree_api_first_release_unlock_escape(void *ctx) {struct bpf_rb_node *res;
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 7/12 selftests/bpf: Add rbtree_search test
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
This patch has a much simplified rbtree usage from the kernel sch_fq qdisc. It has a "struct node_data" which can be added to two different rbtrees which are ordered by different keys.
The test first populates both rbtrees. Then search for a lookup_key from the "groot0" rbtree. Once the lookup_key is found, that node refcount is taken. The node is then removed from another "groot1" rbtree.
While searching the lookup_key, the test will also try to remove all rbnodes in the path leading to the lookup_key.
--- .../testing/selftests/bpf/prog_tests/rbtree.c | 6 + .../selftests/bpf/progs/rbtree_search.c | 137 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/rbtree_search.c
tools/testing/selftests/bpf/prog_tests/rbtree.c +6 -0
@@ -8,6 +8,7 @@#include "rbtree_fail.skel.h" #include "rbtree_btf_fail__wrong_node_type.skel.h" #include "rbtree_btf_fail__add_wrong_type.skel.h" +#include "rbtree_search.skel.h" static void test_rbtree_add_nodes (void ) {
@@ -187,3 +188,8 @@ void test_rbtree_fail(void) {RUN_TESTS (rbtree_fail); } + +void test_rbtree_search (void ) +{ +RUN_TESTS (rbtree_search); +}
tools/testing/selftests/bpf/progs/rbtree_search.c new file +137 -0
137 lines not on this page · raw message
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 8/12 bpf: Simplify reg0 marking for the list kfuncs that return a bpf_list_node pointer
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
The next patch will add bpf_list_{front,back} kfuncs to peek the head and tail of a list. Both of them will return a 'struct bpf_list_node *'.
Follow the earlier change for rbtree, this patch checks the return btf type is a 'struct bpf_list_node' pointer instead of checking each kfuncs individually to decide if mark_reg_graph_node should be called. This will make the bpf_list_{front,back} kfunc addition easier in the later patch.
--- kernel/bpf/verifier.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +6 -2
@@ -11992,6 +11992,11 @@ static bool is_rbtree_node_type(const struct btf_type *t)return t ==btf_type_by_id (btf_vmlinux, kf_arg_btf_ids[KF_ARG_RB_NODE_ID ]); } +static bool is_list_node_type (const struct btf_type *t) +{ +return t ==btf_type_by_id (btf_vmlinux, kf_arg_btf_ids[KF_ARG_LIST_NODE_ID ]); +} +static bool is_kfunc_arg_callback (struct bpf_verifier_env *env,const struct btf *btf,const struct btf_param *arg) {
@@ -13763,8 +13768,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, insn_aux->kptr_struct_meta =btf_find_struct_meta (meta.arg_btf, meta.arg_btf_id); - }else if (meta.func_id == special_kfunc_list[KF_bpf_list_pop_front] || - meta.func_id == special_kfunc_list[KF_bpf_list_pop_back]) { + }else if (is_list_node_type (ptr_type)) {struct btf_field *field = meta.arg_list_head.field;mark_reg_graph_node (regs,BPF_REG_0 , &field->graph_root);
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 9/12 bpf: Add bpf_list_{front,back} kfunc
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
In the kernel fq qdisc implementation, it only needs to look at the fields of the first node in a list but does not always need to remove it from the list. It is more convenient to have a peek kfunc for the list. It works similar to the bpf_rbtree_first().
This patch adds bpf_list_{front,back} kfunc. The verifier is changed such that the kfunc returning "struct bpf_list_node *" will be marked as non-owning. The exception is the KF_ACQUIRE kfunc. The net effect is only the new bpf_list_{front,back} kfuncs will have its return pointer marked as non-owning.
--- kernel/bpf/helpers.c | 22 ++++++++++++++++++++++ kernel/bpf/verifier.c | 12 ++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-)
kernel/bpf/helpers.c +22 -0
@@ -2293,6 +2293,26 @@ __bpf_kfunc struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head)return __bpf_list_del (head, true); } +__bpf_kfunc struct bpf_list_node *bpf_list_front(struct bpf_list_head *head) +{ +struct list_head *h = (struct list_head *)head; + +if (list_empty (h) ||unlikely (!h->next)) +return NULL ; + +return (struct bpf_list_node *)h->next; +} + +__bpf_kfunc struct bpf_list_node *bpf_list_back(struct bpf_list_head *head) +{ +struct list_head *h = (struct list_head *)head; + +if (list_empty (h) ||unlikely (!h->next)) +return NULL ; + +return (struct bpf_list_node *)h->prev; +} + __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root,struct bpf_rb_node *node) {
@@ -3236,6 +3256,8 @@ BTF_ID_FLAGS(func, bpf_list_push_front_impl) BTF_ID_FLAGS(func, bpf_list_push_back_impl) BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_list_front, KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_list_back, KF_RET_NULL) BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE)BTF_ID_FLAGS (func, bpf_rbtree_remove,KF_ACQUIRE |KF_RET_NULL )
kernel/bpf/verifier.c +10 -2
@@ -12079,6 +12079,8 @@ enum special_kfunc_type { KF_bpf_list_push_back_impl, KF_bpf_list_pop_front, KF_bpf_list_pop_back, + KF_bpf_list_front, + KF_bpf_list_back, KF_bpf_cast_to_kern_ctx, KF_bpf_rdonly_cast, KF_bpf_rcu_read_lock,
@@ -12124,6 +12126,8 @@ BTF_ID(func, bpf_list_push_front_impl) BTF_ID(func, bpf_list_push_back_impl) BTF_ID(func, bpf_list_pop_front) BTF_ID(func, bpf_list_pop_back) +BTF_ID(func, bpf_list_front) +BTF_ID(func, bpf_list_back) BTF_ID(func, bpf_cast_to_kern_ctx) BTF_ID(func, bpf_rdonly_cast)BTF_ID (func, bpf_rbtree_remove)
@@ -12160,6 +12164,8 @@ BTF_ID(func, bpf_list_push_front_impl) BTF_ID(func, bpf_list_push_back_impl) BTF_ID(func, bpf_list_pop_front) BTF_ID(func, bpf_list_pop_back) +BTF_ID(func, bpf_list_front) +BTF_ID(func, bpf_list_back) BTF_ID(func, bpf_cast_to_kern_ctx) BTF_ID(func, bpf_rdonly_cast)BTF_ID (func, bpf_rcu_read_lock)
@@ -12598,7 +12604,9 @@ static bool is_bpf_list_api_kfunc(u32 btf_id)return btf_id == special_kfunc_list[KF_bpf_list_push_front_impl] || btf_id == special_kfunc_list[KF_bpf_list_push_back_impl] || btf_id == special_kfunc_list[KF_bpf_list_pop_front] || - btf_id == special_kfunc_list[KF_bpf_list_pop_back]; + btf_id == special_kfunc_list[KF_bpf_list_pop_back] || + btf_id == special_kfunc_list[KF_bpf_list_front] || + btf_id == special_kfunc_list[KF_bpf_list_back]; }static bool is_bpf_rbtree_api_kfunc (u32 btf_id)
@@ -13902,7 +13910,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,if (is_kfunc_ret_null (&meta)) regs[BPF_REG_0 ].id = id; regs[BPF_REG_0 ].ref_obj_id = id; - }else if (is_rbtree_node_type (ptr_type)) { + }else if (is_rbtree_node_type (ptr_type) ||is_list_node_type (ptr_type)) {ref_set_non_owning (env, ®s[BPF_REG_0 ]); }
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 10/12 selftests/bpf: Add test for bpf_list_{front,back}
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
This patch adds a test for the new bpf_list_{front,back} kfunc.
It also adds a test to ensure the non-owning node pointer cannot be used after unlock.
--- .../selftests/bpf/prog_tests/linked_list.c | 2 + .../selftests/bpf/progs/linked_list_peek.c | 104 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/linked_list_peek.c
tools/testing/selftests/bpf/prog_tests/linked_list.c +2 -0
@@ -7,6 +7,7 @@#include "linked_list.skel.h" #include "linked_list_fail.skel.h" +#include "linked_list_peek.skel.h" static char log_buf[1024 *1024 ];
@@ -804,4 +805,5 @@ void test_linked_list(void)test_linked_list_success (LIST_IN_LIST , false);test_linked_list_success (LIST_IN_LIST , true);test_linked_list_success (TEST_ALL , false); +RUN_TESTS (linked_list_peek); }
tools/testing/selftests/bpf/progs/linked_list_peek.c new file +104 -0
104 lines not on this page · raw message
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 11/12 bpf: net: Add a qdisc kfunc to set sk_pacing_status.
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
bpf_qdisc_set_sk_pacing() is added to set the sk_pacing_status. This can tell the tcp stack that the qdisc can do pacing and the tcp stack does not need to do its own internal pacing.
It is only allowed in the enqueue ops.
--- net/sched/bpf_qdisc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
net/sched/bpf_qdisc.c +14 -0
@@ -7,6 +7,7 @@#include <linux/filter.h> #include <net/pkt_sched.h> #include <net/pkt_cls.h> +#include <net/sock.h> #define QDISC_OP_IDX (op) (offsetof(struct Qdisc_ops, op) / sizeof(void (*)(void)))#define QDISC_MOFF_IDX (moff) (moff / sizeof(void (*)(void)))
@@ -214,6 +215,17 @@ __bpf_kfunc void bpf_qdisc_skb_drop(struct sk_buff *skb,__qdisc_drop (skb, (struct sk_buff **)to_free_list); } +__bpf_kfunc int bpf_qdisc_set_sk_pacing(struct sk_buff *skb, enum sk_pacing pacing) +{ +struct sock *sk = skb->sk; + +if (!sk ||sk_listener_or_tw (sk) || pacing !=SK_PACING_FQ ) +return -EINVAL ; + +smp_store_release (&sk->sk_pacing_status, pacing); +return 0 ; +} +/* bpf_qdisc_watchdog_schedule - Schedule a qdisc to a later time using a timer. * @sch: The qdisc to be scheduled. * @expire: The expiry time of the timer.
@@ -274,6 +286,7 @@ BTF_KFUNCS_START(qdisc_kfunc_ids) BTF_ID_FLAGS(func, bpf_skb_get_hash, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_kfree_skb, KF_RELEASE) BTF_ID_FLAGS(func, bpf_qdisc_skb_drop, KF_RELEASE) +BTF_ID_FLAGS(func, bpf_qdisc_set_sk_pacing, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_dynptr_from_skb, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_qdisc_watchdog_schedule, KF_TRUSTED_ARGS)BTF_ID_FLAGS (func, bpf_qdisc_init_prologue,KF_TRUSTED_ARGS )
@@ -290,6 +303,7 @@ BTF_SET_END(qdisc_common_kfunc_set) BTF_SET_START(qdisc_enqueue_kfunc_set) BTF_ID(func, bpf_qdisc_skb_drop) BTF_ID(func, bpf_qdisc_watchdog_schedule) +BTF_ID(func, bpf_qdisc_set_sk_pacing) BTF_SET_END(qdisc_enqueue_kfunc_set) BTF_SET_START(qdisc_dequeue_kfunc_set)
Signature · 1 line
2.47.1
Martin KaFai Lau
reviewerRFC PATCH 12/12 selftests/bpf: A bpf fq implementation similar to the kernel sch_fq
In reply to Martin KaFai LauFrom: Martin KaFai Lau <[email protected]>
This patch adds a fuller fq qdisc implementation that is comparable to the kernel fq implementation. The code is mostly borrowed from the sch_fq.c before the WRR addition. The WRR should be doable as a followup.
Some highlights:
* The current struct_ops does not support the qdisc_priv() concept. qdisc_priv() is the additional private data allocated by the qdisc subsystem at the end of a struct_ops object.
The patch is using a map-in-map approach to do the qdisc_priv. The outer map is an arraymap. When a qdisc instance starts, it grabs an available index (idx) in the ".init" ops. This idx will be the key to lookup the outer arraymap.
The inner map will then serve as the qdisc_priv which is the 'struct fq_sched_data'
* Each qdisc instance has a hash table of rbtrees. This patch also uses map-in-map to do this. The outer arraymap's key is the qdisc "idx". The inner map is the array of bpf_rb_root.
* With bpf_rbtree_{root,left,right} and bpf_list_{front,back}, the fq_classify/enqueue/dequeue should be more recognizable when comparing with the sch_fq.c. Like, searching the flow and doing gc.
* Most of the code deviation from sch_fq.c is because of the lock requirement and the refcount requirement.
veristat:
File Program Verdict Duration (us) Insns States Program size Jited size ---------------- -------------- ------- ------------- ----- ------ ------------ ---------- bpf_sch_fq.bpf.o bpf_fq_dequeue success 43043 1367 119 531 2798 bpf_sch_fq.bpf.o bpf_fq_destroy success 12414 543 54 232 1350 bpf_sch_fq.bpf.o bpf_fq_enqueue success 91888 4750 335 695 3645 bpf_sch_fq.bpf.o bpf_fq_init success 7439 149 11 123 897 bpf_sch_fq.bpf.o bpf_fq_reset success 12553 541 53 198 1189 ---------------- -------------- ------- ------------- ----- ------ ------------ ----------
--- .../selftests/bpf/prog_tests/bpf_qdisc.c | 21 + .../selftests/bpf/progs/bpf_qdisc_common.h | 97 +- .../selftests/bpf/progs/bpf_qdisc_fq.c | 2 - .../testing/selftests/bpf/progs/bpf_sch_fq.c | 1171 +++++++++++++++++ .../selftests/bpf/progs/bpf_tracing_net.h | 1 + 5 files changed, 1289 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/bpf_sch_fq.c
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +21 -0
@@ -7,6 +7,7 @@#include "network_helpers.h" #include "bpf_qdisc_fifo.skel.h" #include "bpf_qdisc_fq.skel.h" +#include "bpf_sch_fq.skel.h" #define LO_IFINDEX 1
@@ -88,6 +89,24 @@ static void test_fq(void)bpf_qdisc_fq__destroy (fq_skel); } +static void test_sch_fq (void ) +{ +struct bpf_sch_fq *skel; + + skel =bpf_sch_fq__open_and_load (); +if (!ASSERT_OK_PTR (skel,"bpf_sch_fq__open_and_load" )) +return ; + + skel->links.fq =bpf_map__attach_struct_ops (skel->maps.fq); +if (!ASSERT_OK_PTR (skel->links.fq,"bpf_map__attach_struct_ops" )) + goto done; + +do_test ("bpf_sch_fq" ); + +done: +bpf_sch_fq__destroy (skel); +} +static void test_qdisc_attach_to_mq (void ) { DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook,
@@ -171,6 +190,8 @@ void test_bpf_qdisc(void)test_fifo ();if (test__start_subtest ("fq" ))test_fq (); +if (test__start_subtest ("sch_fq" )) +test_sch_fq ();if (test__start_subtest ("attach to mq" ))test_qdisc_attach_to_mq ();if (test__start_subtest ("attach to non root" ))
tools/testing/selftests/bpf/progs/bpf_qdisc_common.h +96 -1
121 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c +0 -2
@@ -643,8 +643,6 @@ static int fq_remove_flows_in_list(u32 index, void *ctx)return 0 ; } -extern unsigned CONFIG_HZ __kconfig; -/* limit number of collected flows per round */ #define FQ_GC_MAX 8#define FQ_GC_AGE (3*CONFIG_HZ)
tools/testing/selftests/bpf/progs/bpf_sch_fq.c new file +1171 -0
1,171 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_tracing_net.h +1 -0
@@ -121,6 +121,7 @@#define ir_v6_rmt_addr req.__req_common.skc_v6_daddr#define ir_v6_loc_addr req.__req_common.skc_v6_rcv_saddr +#define sk_hash __sk_common.skc_hash#define sk_num __sk_common.skc_num#define sk_dport __sk_common.skc_dport#define sk_family __sk_common.skc_family
Signature · 1 line
2.47.1
Re: [RFC PATCH bpf-next 01/12] bpf: Check KF_bpf_rbtree_add_impl for the "case KF_ARG_PTR_TO_RB_NODE"
In reply to Martin KaFai LauQuoted 24 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
In a later patch, two new kfuncs will take the
bpf_rb_nodepointer arg.struct
bpf_rb_node*bpf_rbtree_left(structbpf_rb_root*root,struct bpf_rb_node *node); struct bpf_rb_node *bpf_rbtree_right(struct bpf_rb_root *root, struct bpf_rb_node *node);In the
check_kfunc_call, there is a "caseKF_ARG_PTR_TO_RB_NODE" to check if thereg->typeshould be an allocated pointer or should be anon_owning_ref.The later patch will need to ensure that the
bpf_rb_nodepointer passing to the new bpf_rbtree_{left,right} must be anon_owning_ref. This should be the same requirement as the existingbpf_rbtree_remove.This patch swaps the current "if else" statement. Instead of checking the
bpf_rbtree_remove, it checks thebpf_rbtree_add. Then the new bpf_rbtree_{left,right} will fall into the "else" case to make the later patch simpler.bpf_rbtree_addshould be the only one that needs an allocated pointer.
Quoted 5 lines
This should be a no-op change considering there are only two kfunc(s) taking
bpf_rb_nodepointer arg,rbtree_addandrbtree_remove.Signed-off-by Martin KaFai Lau <[email protected]>---
Re: [RFC PATCH bpf-next 02/12] bpf: Simplify reg0 marking for the rbtree kfuncs that return a bpf_rb_node pointer
In reply to Martin KaFai LauQuoted 15 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
The current rbtree kfunc, bpf_rbtree_{first, remove}, returns the
bpf_rb_nodepointer. Thecheck_kfunc_callcurrently checks the kfuncbtf_idinstead of its return pointer type to decide
if it needs to domark_reg_graph_node(reg0)andref_set_non_owning(reg0).The later patch will add bpf_rbtree_{root,left,right} that will also return a
bpf_rb_nodepointer. Instead of adding more kfuncbtf_idchecks to the "if" case, this patch changes the test to check the kfunc's return type.is_rbtree_node_type()function is added to test if a pointer type is abpf_rb_node. The callers have already skipped the modifiers of the pointer type.
Quoted 6 lines
A note on the
ref_set_non_owning(), althoughbpf_rbtree_remove()also returns abpf_rb_nodepointer, thebpf_rbtree_remove()has theKF_ACQUIREflag. Thus, its reg0 will not become non-owning.Signed-off-by Martin KaFai Lau <[email protected]>---
Re: [RFC PATCH bpf-next 03/12] bpf: Add bpf_rbtree_{root,left,right} kfunc
In reply to Martin KaFai LauQuoted 82 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
In the kernel fq qdisc implementation, it requires to traverse a rbtree stored with the networking "flows".
In the later bpf selftests prog, the much simplified logic that uses the bpf_rbtree_{root,left,right} to traverse the tree is like:
struct fq_flow { struct bpf_rb_node fq_node; struct bpf_rb_node rate_node; struct bpf_refcount refcount; unsigned long sk_long; };struct fq_flow_root { struct bpf_spin_lock lock; struct bpf_rb_root root __contains(fq_flow, fq_node); };struct
fq_flow*fq_classify(...){ struct bpf_rb_node *tofree[FQ_GC_MAX]; struct fq_flow_root *root; struct fq_flow *gc_f, *f; struct bpf_rb_node *p; int i, fcnt = 0; /* ... */ f = NULL; bpf_spin_lock(&root->lock); p = bpf_rbtree_root(&root->root); 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; } /* To be removed from the rbtree */ if (fcnt < FQ_GC_MAX && fq_gc_candidate(gc_f, jiffies_now)) tofree[fcnt++] = p; if (gc_f->sk_long > sk_long) p = bpf_rbtree_left(&root->root, p); else p = bpf_rbtree_right(&root->root, p); } /* remove from the rbtree */ for (i = 0; i < fcnt; i++) { p = tofree[i]; tofree[i] = bpf_rbtree_remove(&root->root, p); } bpf_spin_unlock(&root->lock); /* bpf_obj_drop the fq_flow(s) that have just been removed * from the rbtree. */ for (i = 0; i < fcnt; i++) { p = tofree[i]; if (p) { gc_f = bpf_rb_entry(p, struct fq_flow, fq_node); bpf_obj_drop(gc_f); } } return f;}
The above simplified code needs to traverse the rbtree for two purposes,
1) find the flow with the desiredsk_longvalue
2) while searching for thesk_long, collect flows that are thefq_gc_candidate. They will be removed from the rbtree.
Quoted 7 lines
This patch adds the bpf_rbtree_{root,left,right} kfunc to enable the rbtree traversal. The returned
bpf_rb_nodepointer will be a non-owning reference which is the same as the returned pointer of the exisitingbpf_rbtree_firstkfunc.Signed-off-by Martin KaFai Lau <[email protected]>---
Kumar Kartikeya Dwivedi
maintainerRe: [RFC PATCH bpf-next 04/12] selftests/bpf: Adjust failure message in the rbtree_fail test
In reply to Martin KaFai LauQuoted 8 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
Some of the failure messages in the
rbtree_failtest. The message is now "bpf_rbtree_removecan only take non-owningbpf_rb_nodepointer".Signed-off-by Martin KaFai Lau <[email protected]>---
You need to fold this patch into the previous one to preserve bisection.
Re: [RFC PATCH bpf-next 05/12] bpf: Allow refcounted bpf_rb_node used in bpf_rbtree_{remove,left,right}
In reply to Martin KaFai LauQuoted 45 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
The bpf_rbtree_{remove,left,right} requires the root's lock to be held. They also check the
node_internal->owneris still owned by that root before proceeding, so it is safe to allow refcountedbpf_rb_nodepointer to be used in these kfuncs.In the later selftest, 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 the 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); } }
Quoted 6 lines
bpf_rbtree_{left,right} do not need this change but are relaxed together with
bpf_rbtree_removeinstead of adding extra verifier logic to exclude these kfuncs.Signed-off-by Martin KaFai Lau <[email protected]>---
Kumar Kartikeya Dwivedi
maintainerRe: [RFC PATCH bpf-next 06/12] selftests/bpf: Adjust test that does not allow refcounted node in rbtree_remove
In reply to Martin KaFai LauQuoted 8 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
rbtree_removenow allows refcounted node now. Therbtree_api_remove_unadded_nodetest needs to be adjusted.First change, it does not expect a verifier's error now.
Quoted 8 lines
Second change, 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.
Signed-off-by Martin KaFai Lau <[email protected]>---
Looks good, but again, we should fold into the previous patch.
Kumar Kartikeya Dwivedi
maintainerRe: [RFC PATCH bpf-next 06/12] selftests/bpf: Adjust test that does not allow refcounted node in rbtree_remove
Quoted 14 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
rbtree_removenow allows refcounted node now. Therbtree_api_remove_unadded_nodetest needs to be adjusted.First change, it does not expect a verifier's error now.
Second change, the test now expects
bpf_rbtree_remove(&groot, &m->node)to return NULL. The test uses__retval(0)to ensure this NULL return value.
Quoted 6 lines
Quoted 4 lines
Some of the "only take non-owning..." failure messages are changed also.
Signed-off-by Martin KaFai Lau <[email protected]>---
Looks good, but again, we should fold into the previous patch.
It would also be good to ensure that we tag return values of root, left, right kfuncs correctly,
an easy way is to add three tests then try to pass them into bpf_this_cpu_ptr, then match
on the R1 type= in __msg.
Re: [RFC PATCH bpf-next 07/12] selftests/bpf: Add rbtree_search test
In reply to Martin KaFai LauQuoted 12 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
This patch has a much simplified rbtree usage from the kernel
sch_fqqdisc. It has a "structnode_data" which can be added to two different rbtrees which are ordered by different keys.The test first populates both rbtrees. Then search for a
lookup_keyfrom the "groot0" rbtree. Once thelookup_keyis found, that node refcount is taken. The node is then removed from another "groot1" rbtree.
Quoted 5 lines
While searching the
lookup_key, the test will also try to remove all rbnodes in the path leading to thelookup_key.Signed-off-by Martin KaFai Lau <[email protected]>---
Re: [RFC PATCH bpf-next 08/12] bpf: Simplify reg0 marking for the list kfuncs that return a bpf_list_node pointer
In reply to Martin KaFai LauQuoted 13 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
The next patch will add bpf_list_{front,back} kfuncs to peek the head and tail of a list. Both of them will return a 'struct
bpf_list_node*'.Follow the earlier change for rbtree, this patch checks the return btf type is a 'struct
bpf_list_node' pointer instead of checking each kfuncs individually to decide ifmark_reg_graph_nodeshould be called. This will make the bpf_list_{front,back} kfunc addition easier in the later patch.
Quoted 2 lines
Signed-off-by Martin KaFai Lau <[email protected]>---
Re: [RFC PATCH bpf-next 09/12] bpf: Add bpf_list_{front,back} kfunc
In reply to Martin KaFai LauQuoted 8 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
In the kernel fq qdisc implementation, it only needs to look at the fields of the first node in a list but does not always need to remove it from the list. It is more convenient to have a peek kfunc for the list. It works similar to the
bpf_rbtree_first().
Quoted 8 lines
This patch adds bpf_list_{front,back} kfunc. The verifier is changed such that the kfunc returning "struct
bpf_list_node*" will be marked as non-owning. The exception is theKF_ACQUIREkfunc. The net effect is only the new bpf_list_{front,back} kfuncs will have its return pointer marked as non-owning.Signed-off-by Martin KaFai Lau <[email protected]>---
Kumar Kartikeya Dwivedi
maintainerRe: [RFC PATCH bpf-next 10/12] selftests/bpf: Add test for bpf_list_{front,back}
In reply to Martin KaFai LauQuoted 70 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
This patch adds a test for the new bpf_list_{front,back} kfunc.
It also adds a test to ensure the non-owning node pointer cannot be used after unlock.
Signed-off-by Martin KaFai Lau <[email protected]>--- .../selftests/bpf/prog_tests/linked_list.c | 2 + .../selftests/bpf/progs/linked_list_peek.c | 104 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/linked_list_peek.ctools/testing/selftests/bpf/prog_tests/linked_list.c +2
-0@@ -7,6 +7,7 @@#include "linked_list.skel.h" #include "linked_list_fail.skel.h" +#include "linked_list_peek.skel.h" static char log_buf[1024 *1024 ];@@ -804,4 +805,5 @@ void test_linked_list(void)test_linked_list_success (LIST_IN_LIST , false);test_linked_list_success (LIST_IN_LIST , true);test_linked_list_success (TEST_ALL , false); +RUN_TESTS (linked_list_peek); }tools/testing/selftests/bpf/progs/linked_list_peek.c new file +31
-0@@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" +#include "bpf_experimental.h" + +struct node_data { +struct bpf_list_node l; +int key; +}; + +#define private (name) SEC(".data." #name) __hidden __attribute__((aligned(8))) +private(A) struct bpf_spin_lock glock; +private(A) struct bpf_list_head ghead __contains(node_data, l); + +#define list_entry (ptr, type, member) container_of(ptr, type, member) +#define NR_NODES 32 + +int zero =0 ; + +SEC("syscall") +__failure __msg("invalid mem access 'scalar'") +long list_peek_unlock_scalar_node(void *ctx) +{ +struct bpf_list_node *l_n; +struct node_data *n; + +bpf_spin_lock (&glock); + l_n =bpf_list_front (&ghead);
tools/testing/selftests/bpf/progs/linked_list_peek.c line 41
Quoted 10 lines
tools/testing/selftests/bpf/progs/linked_list_peek.c new file +10
-0+bpf_spin_unlock (&glock); + +if (l_n) { + n = list_entry(l_n, struct node_data, l); +if (n->key ==0 ) +return __LINE__; + } + +return 0 ; +}
Would be good to have tests explicitly asserting the type is non-owning ref (even though we indirectly do that by touching it after unlock, relying on invalidation logic.).
Quoted 3 lines
tools/testing/selftests/bpf/progs/linked_list_peek.c new file +1
-0+
Re: [RFC PATCH bpf-next 12/12] selftests/bpf: A bpf fq implementation similar to the kernel sch_fq
In reply to Martin KaFai LauQuoted 25 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
This patch adds a fuller fq qdisc implementation that is comparable to the kernel fq implementation. The code is mostly borrowed from the
sch_fq.cbefore the WRR addition. The WRR should be doable as a followup.Some highlights:
* The currentstruct_opsdoes not support theqdisc_priv()concept.qdisc_priv()is the additional private data allocated by the qdisc subsystem at the end of astruct_opsobject.The patch is using a map-in-map approach to do the
qdisc_priv. The outer map is an arraymap. When a qdisc instance starts, it grabs an available index (idx) in the ".init" ops. This idx will be the key to lookup the outer arraymap.The inner map will then serve as the
qdisc_privwhich is the 'structfq_sched_data'* Each qdisc instance has a hash table of rbtrees. This patch also uses map-in-map to do this. The outer arraymap's key is the qdisc "idx". The inner map is the array of
bpf_rb_root.
Quoted 6 lines
* With bpf_rbtree_{root,left,right} and bpf_list_{front,back}, the fq_classify/enqueue/dequeue should be more recognizable when comparing with the
sch_fq.c. Like, searching the flow and doing gc.* Most of the code deviation from
sch_fq.cis because of the lock requirement and the refcount requirement.
This is a very impressive bpf prog.
Quite amazing what qdisc-bpf can do.
Few questions:
Quoted 1 line
bpf_probe_read_kernel(&sk_long
Will the following work ?
*bpf_core_cast(skb->sk, long)
or if verifier needs struct type (I don't recall)
struct long_wrap {
long l;
};bpf_core_cast(skb->sk, struct long_wrap)->l
Quoted 1 line
bpf_spin_lock(&root->lock);
have you considered "if (bpf_res_spin_lock(&root->lock))" ? It can also protect rbtree and lists,
and allows arbitrary calls inside, so the algorithm might be easier to implement?
Martin KaFai Lau
reviewerRe: [RFC PATCH bpf-next 10/12] selftests/bpf: Add test for bpf_list_{front,back}
In reply to Kumar Kartikeya DwivediQuoted 77 lines · Kumar Kartikeya Dwivedi
Quoted 76 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
This patch adds a test for the new bpf_list_{front,back} kfunc.
It also adds a test to ensure the non-owning node pointer cannot be used after unlock.
Signed-off-by Martin KaFai Lau <[email protected]>--- .../selftests/bpf/prog_tests/linked_list.c | 2 + .../selftests/bpf/progs/linked_list_peek.c | 104 ++++++++++++++++++ 2 files changed, 106 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/linked_list_peek.ctools/testing/selftests/bpf/prog_tests/linked_list.c +2
-0@@ -7,6 +7,7 @@#include "linked_list.skel.h" #include "linked_list_fail.skel.h" +#include "linked_list_peek.skel.h" static char log_buf[1024 *1024 ];@@ -804,4 +805,5 @@ void test_linked_list(void)test_linked_list_success (LIST_IN_LIST , false);test_linked_list_success (LIST_IN_LIST , true);test_linked_list_success (TEST_ALL , false); +RUN_TESTS (linked_list_peek); }tools/testing/selftests/bpf/progs/linked_list_peek.c new file +37
-0@@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" +#include "bpf_experimental.h" + +struct node_data { +struct bpf_list_node l; +int key; +}; + +#define private (name) SEC(".data." #name) __hidden __attribute__((aligned(8))) +private(A) struct bpf_spin_lock glock; +private(A) struct bpf_list_head ghead __contains(node_data, l); + +#define list_entry (ptr, type, member) container_of(ptr, type, member) +#define NR_NODES 32 + +int zero =0 ; + +SEC("syscall") +__failure __msg("invalid mem access 'scalar'") +long list_peek_unlock_scalar_node(void *ctx) +{ +struct bpf_list_node *l_n; +struct node_data *n; + +bpf_spin_lock (&glock); + l_n =bpf_list_front (&ghead); +bpf_spin_unlock (&glock); + +if (l_n) { + n = list_entry(l_n, struct node_data, l); +if (n->key ==0 ) +return __LINE__;
Quoted 8 lines
tools/testing/selftests/bpf/progs/linked_list_peek.c line 41
Quoted 4 lines
tools/testing/selftests/bpf/progs/linked_list_peek.c new file +4
-0+ } + +return 0 ; +}Would be good to have tests explicitly asserting the type is non-owning ref (even though we indirectly do that by touching it after unlock, relying on invalidation logic.).
I will try to address the test suggestions in patch 6 and 10.
Thanks for the review!
Martin KaFai Lau
reviewerRe: [RFC PATCH bpf-next 12/12] selftests/bpf: A bpf fq implementation similar to the kernel sch_fq
In reply to Alexei StarovoitovQuoted 40 lines · Alexei Starovoitov
Quoted 25 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
This patch adds a fuller fq qdisc implementation that is comparable to the kernel fq implementation. The code is mostly borrowed from the
sch_fq.cbefore the WRR addition. The WRR should be doable as a followup.Some highlights:
* The currentstruct_opsdoes not support theqdisc_priv()concept.qdisc_priv()is the additional private data allocated by the qdisc subsystem at the end of astruct_opsobject.The patch is using a map-in-map approach to do the
qdisc_priv. The outer map is an arraymap. When a qdisc instance starts, it grabs an available index (idx) in the ".init" ops. This idx will be the key to lookup the outer arraymap.The inner map will then serve as the
qdisc_privwhich is the 'structfq_sched_data'* Each qdisc instance has a hash table of rbtrees. This patch also uses map-in-map to do this. The outer arraymap's key is the qdisc "idx". The inner map is the array of
bpf_rb_root.Quoted 6 lines
* With bpf_rbtree_{root,left,right} and bpf_list_{front,back}, the fq_classify/enqueue/dequeue should be more recognizable when comparing with the
sch_fq.c. Like, searching the flow and doing gc.* Most of the code deviation from
sch_fq.cis because of the lock requirement and the refcount requirement.This is a very impressive bpf prog.
Quite amazing what qdisc-bpf can do.Few questions:
Quoted 1 line
bpf_probe_read_kernel(&sk_long
Quoted 8 lines
Will the following work ?
*bpf_core_cast(skb->sk, long)or if verifier needs struct type (I don't recall)
struct long_wrap { long l; };
bpf_core_cast(skb->sk, struct long_wrap)->l
Ah. I didn't think about bpf_core_cast.
As you suspected, adding "struct long_wrap" to the kernel works.
The verifier enforces the bpf_rdonly_cast return type must be a pointer to a struct. Thus, a plain "long" pointer won't work. btf_struct_access() will need some changes to support long pointer. I haven't checked other places yet.
Quoted 4 lines
Quoted 2 lines
bpf_spin_lock(&root->lock);have you considered "if (
bpf_res_spin_lock(&root->lock))" ?
No but mostly because I started this set a little earlier, so it was not in my mind. Thanks for the suggestion.
Quoted 3 lines
It can also protect rbtree and lists,
and allows arbitrary calls inside, so the algorithm might be easier to implement?
Yes, it will make the implementation easier. e.g. it can call bpf_obj_{drop,new} without releasing the lock. It is a huge plus. I will use the res_spin_lock in the next revision.
Thanks for the review!