From: Leon Hwang <leon.hwang@linux.dev>
To: Chengkaitao <pilgrimtao@gmail.com>,
martin.lau@linux.dev, ast@kernel.org, daniel@iogearbox.net,
andrii@kernel.org, eddyz87@gmail.com, song@kernel.org,
yonghong.song@linux.dev, john.fastabend@gmail.com,
kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com,
jolsa@kernel.org, shuah@kernel.org, chengkaitao@kylinos.cn,
linux-kselftest@vger.kernel.org
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 2/6] selftests/bpf: Add test cases for bpf_list_del
Date: Wed, 4 Mar 2026 11:27:56 +0800 [thread overview]
Message-ID: <c13cc186-4692-4708-8185-e7117f6100e1@linux.dev> (raw)
In-Reply-To: <20260303135219.33726-3-pilgrimtao@gmail.com>
On 3/3/26 21:52, Chengkaitao wrote:
> From: Kaitao Cheng <chengkaitao@kylinos.cn>
>
> Add a node to both an rbtree and a list, retrieve the node from
> the rbtree, use the obtained node pointer to remove it from the
> list, and finally free the node.
>
> To verify the validity of bpf_list_del, also expect the verifier
> to reject calls to bpf_list_del made without holding the spin_lock.
>
To avoid the churn of renaming test_bpf_list_del() in patch #4, it would
be better to fold patches #2, #4, and #6 into one patch.
Thanks,
Leon
> Signed-off-by: Kaitao Cheng <chengkaitao@kylinos.cn>
> ---
> .../testing/selftests/bpf/bpf_experimental.h | 11 +++
> .../selftests/bpf/progs/refcounted_kptr.c | 71 +++++++++++++++++++
> 2 files changed, 82 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
> index 4b7210c318dd..54ec9d307fdc 100644
> --- a/tools/testing/selftests/bpf/bpf_experimental.h
> +++ b/tools/testing/selftests/bpf/bpf_experimental.h
> @@ -99,6 +99,17 @@ extern struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head) __ks
> */
> extern struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head) __ksym;
>
> +/* Description
> + * Remove 'node' from the BPF linked list with head 'head'.
> + * The node must be in the list. Caller receives ownership of the
> + * removed node and must release it with bpf_obj_drop.
> + * Returns
> + * Pointer to the removed bpf_list_node, or NULL if 'node' is NULL
> + * or not in the list.
> + */
> +extern struct bpf_list_node *bpf_list_del(struct bpf_list_head *head,
> + struct bpf_list_node *node) __ksym;
> +
> /* Description
> * Remove 'node' from rbtree with root 'root'
> * Returns
> diff --git a/tools/testing/selftests/bpf/progs/refcounted_kptr.c b/tools/testing/selftests/bpf/progs/refcounted_kptr.c
> index 1aca85d86aeb..ac7672cfefb8 100644
> --- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c
> +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c
> @@ -367,6 +367,77 @@ long insert_rbtree_and_stash__del_tree_##rem_tree(void *ctx) \
> INSERT_STASH_READ(true, "insert_stash_read: remove from tree");
> INSERT_STASH_READ(false, "insert_stash_read: don't remove from tree");
>
> +/* Insert node_data into both rbtree and list, remove from tree, then remove
> + * from list via bpf_list_del using the node obtained from the tree.
> + */
> +SEC("tc")
> +__description("test_bpf_list_del: remove an arbitrary node from the list")
> +__success __retval(0)
> +long test_bpf_list_del(void *ctx)
> +{
> + long err;
> + struct bpf_rb_node *rb;
> + struct bpf_list_node *l;
> + struct node_data *n;
> +
> + err = __insert_in_tree_and_list(&head, &root, &lock);
> + if (err)
> + return err;
> +
> + bpf_spin_lock(&lock);
> + rb = bpf_rbtree_first(&root);
> + if (!rb) {
> + bpf_spin_unlock(&lock);
> + return -4;
> + }
> +
> + rb = bpf_rbtree_remove(&root, rb);
> + if (!rb) {
> + bpf_spin_unlock(&lock);
> + return -5;
> + }
> +
> + n = container_of(rb, struct node_data, r);
> + l = bpf_list_del(&head, &n->l);
> + bpf_spin_unlock(&lock);
> + bpf_obj_drop(n);
> + if (!l)
> + return -6;
> +
> + bpf_obj_drop(container_of(l, struct node_data, l));
> + return 0;
> +}
> +
> +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)
> +{
> + struct bpf_rb_node *rb;
> + struct bpf_list_node *l;
> + struct node_data *n;
> +
> + bpf_spin_lock(&lock);
> + rb = bpf_rbtree_first(&root);
> + if (!rb) {
> + bpf_spin_unlock(&lock);
> + return -4;
> + }
> +
> + rb = bpf_rbtree_remove(&root, rb);
> + bpf_spin_unlock(&lock);
> + if (!rb)
> + return -5;
> +
> + n = container_of(rb, struct node_data, r);
> + l = bpf_list_del(&head, &n->l);
> + bpf_obj_drop(n);
> + if (!l)
> + return -6;
> +
> + bpf_obj_drop(container_of(l, struct node_data, l));
> + return 0;
> +}
> +
> SEC("tc")
> __success
> long rbtree_refcounted_node_ref_escapes(void *ctx)
next prev parent reply other threads:[~2026-03-04 3:28 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-03 13:52 [PATCH v4 0/6] bpf: Extend the bpf_list family of APIs Chengkaitao
2026-03-03 13:52 ` [PATCH v4 1/6] bpf: Introduce the bpf_list_del kfunc Chengkaitao
2026-03-04 3:27 ` Leon Hwang
2026-03-03 13:52 ` [PATCH v4 2/6] selftests/bpf: Add test cases for bpf_list_del Chengkaitao
2026-03-04 3:27 ` Leon Hwang [this message]
2026-03-03 13:52 ` [PATCH v4 3/6] bpf: add bpf_list_add_impl to insert node after a given list node Chengkaitao
2026-03-03 14:40 ` bot+bpf-ci
2026-03-04 3:29 ` Leon Hwang
2026-03-03 13:52 ` [PATCH v4 4/6] selftests/bpf: Add test case for bpf_list_add_impl Chengkaitao
2026-03-03 13:52 ` [PATCH v4 5/6] bpf: add bpf_list_is_first/last/empty kfuncs Chengkaitao
2026-03-04 3:30 ` Leon Hwang
2026-03-03 13:52 ` [PATCH v4 6/6] selftests/bpf: Add test cases for bpf_list_is_first/is_last/empty Chengkaitao
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=c13cc186-4692-4708-8185-e7117f6100e1@linux.dev \
--to=leon.hwang@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chengkaitao@kylinos.cn \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=pilgrimtao@gmail.com \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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