From: Changwoo Min <changwoo@igalia.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Shuah Khan <shuah@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
david.dai@linux.dev, Gavin Guo <gavinguo@igalia.com>,
bpf@vger.kernel.org, sched-ext@lists.linux.dev,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
changwoo@igalia.com, kernel-dev@igalia.com
Subject: [PATCH bpf-next 1/3] selftests/bpf: libarena: Order equal rbtree keys FIFO
Date: Wed, 28 Oct 2026 15:00:44 +0900 [thread overview]
Message-ID: <20261028060046.549097-2-changwoo@igalia.com> (raw)
In-Reply-To: <20261028060046.549097-1-changwoo@igalia.com>
An RB_DUPLICATE tree links a new key to the left of an equal one, so
rb_least() and rb_pop() return the newest of several equal keys. A
queue of tasks keyed by vtime should instead serve equal keys in
arrival order, as sched_ext's vtime-ordered DSQs do.
Send equal keys right on insert, as the kernel's rb_add() does. A new
node then lands after every node with an equal key. Rotations and
removals preserve the in-order sequence, so equal keys stay in
insertion order without a tie-break field.
Add a selftest that interleaves equal keys while the tree grows and
checks the pop order.
Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
.../bpf/libarena/include/libarena/rbtree.h | 6 +--
.../bpf/libarena/selftests/test_rbtree.bpf.c | 37 +++++++++++++++++++
.../selftests/bpf/libarena/src/rbtree.bpf.c | 16 +++++---
3 files changed, 51 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h b/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h
index 486428911d96..00e63b12d82e 100644
--- a/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h
+++ b/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h
@@ -45,9 +45,9 @@ enum rbtree_alloc {
* the existing node.
*
* RB_DUPLICATE: Allow nodes with identical keys in the rbtree.
- * Finding/popping/removing a key acts on any of the nodes
- * with the appropriate key - there is no ordering by time
- * of insertion.
+ * Nodes with identical keys are kept in insertion order, so
+ * rb_least() and rb_pop() return the oldest of them. Finding
+ * or removing by key acts on any of them.
*/
enum rbtree_insert_mode {
RB_DEFAULT,
diff --git a/tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c b/tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c
index 856c484a009a..1e2a0fac1e4f 100644
--- a/tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c
@@ -248,6 +248,43 @@ __weak int test_rbtree_duplicate(void)
return rb_destroy(rbtree);
}
+#define FIFO_NR_NODES 64
+#define FIFO_NR_KEYS 4
+
+SEC("syscall")
+__weak int test_rbtree_duplicate_fifo(void)
+{
+ const u64 per_key = FIFO_NR_NODES / FIFO_NR_KEYS;
+ struct rbtree __arena *rbtree;
+ u64 key, value;
+ int ret, i;
+
+ rbtree = rb_create(RB_ALLOC, RB_DUPLICATE);
+ if (!rbtree)
+ return 1;
+
+ for (i = 0; i < FIFO_NR_NODES && can_loop; i++) {
+ ret = rb_insert(rbtree, i % FIFO_NR_KEYS, i);
+ if (ret)
+ return 2;
+ }
+
+ for (i = 0; i < FIFO_NR_NODES && can_loop; i++) {
+ ret = rb_pop(rbtree, &key, &value);
+ if (ret)
+ return 3;
+
+ /* All of key 0 in insertion order, then all of key 1, ... */
+ if (key != i / per_key)
+ return 4;
+
+ if (value != key + FIFO_NR_KEYS * (i % per_key))
+ return 5;
+ }
+
+ return rb_destroy(rbtree);
+}
+
static inline int
clean_up_noalloc_tree(struct rbtree __arena *rbtree)
{
diff --git a/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c b/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c
index 7f0f6dc3e17d..e6c9430f734b 100644
--- a/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c
@@ -161,8 +161,14 @@ struct rbnode __arena *rbnode_find(struct rbnode __arena *subtree, u64 key)
return node;
}
+/*
+ * Find the node to link @key under. Smaller keys go left and everything else
+ * right, so a new key lands after every node with an equal key and equal keys
+ * stay in insertion order. Rotations and removals preserve the in-order
+ * sequence, so they keep that order too.
+ */
static
-struct rbnode __arena *rbnode_least_upper_bound(struct rbnode __arena *subtree, uint64_t key)
+struct rbnode __arena *rbnode_insert_parent(struct rbnode __arena *subtree, uint64_t key)
{
struct rbnode __arena *node = subtree;
int dir;
@@ -171,7 +177,7 @@ struct rbnode __arena *rbnode_least_upper_bound(struct rbnode __arena *subtree,
return NULL;
while (can_loop) {
- dir = (key <= node->key) ? 0 : 1;
+ dir = (key < node->key) ? 0 : 1;
if (!node->child[dir])
break;
@@ -255,7 +261,7 @@ int rb_node_insert(struct rbtree __arena *rbtree,
if (rbtree->insert != RB_DUPLICATE)
parent = rbnode_find(parent, key);
else
- parent = rbnode_least_upper_bound(parent, key);
+ parent = rbnode_insert_parent(parent, key);
if (key == parent->key && rbtree->insert != RB_DUPLICATE) {
if (rbtree->insert == RB_UPDATE) {
@@ -278,8 +284,8 @@ int rb_node_insert(struct rbtree __arena *rbtree,
}
node->parent = parent;
- /* Also works if key == parent->key. */
- if (key <= parent->key)
+ /* An equal key goes right, after the existing ones. */
+ if (key < parent->key)
parent->left = node;
else
parent->right = node;
--
2.56.0
next prev parent reply other threads:[~2026-10-28 6:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-28 6:00 [PATCH bpf-next 0/3] selftests/bpf: libarena: Add ordered-walk and lockless-peek support to the rbtree Changwoo Min
2026-10-28 6:00 ` Changwoo Min [this message]
2026-10-28 6:00 ` [PATCH bpf-next 2/3] selftests/bpf: libarena: Cache the rbtree's leftmost node and count Changwoo Min
2026-10-28 6:00 ` [PATCH bpf-next 3/3] selftests/bpf: libarena: Add rb_next() Changwoo Min
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=20261028060046.549097-2-changwoo@igalia.com \
--to=changwoo@igalia.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=david.dai@linux.dev \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=gavinguo@igalia.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=kernel-dev@igalia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=sched-ext@lists.linux.dev \
--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
all inboxes | Powered by JetHome®