selftests/bpf: libarena: Add ordered-walk and lockless-peek support to the rbtree
Subsystems (4)
Changwoo Min
reviewerv3 cover selftests/bpf: libarena: Add ordered-walk and lockless-peek support to the rbtree
A sched_ext scheduler can build a task queue on libarena's rbtree, keyed by vtime, in place of a vtime-ordered DSQ. Such a queue needs three things the tree lacks. This series adds them:
- Serve equal keys in insertion order, as vtime-ordered DSQs do (1/3).
- Add rb_next(), so that a consumer can walk past tasks it cannot run in key order (2/3).
- Cache the leftmost node and the node count, so that other CPUs can peek at the head and the length without the writer's lock, like scx_bpf_dsq_peek() and scx_bpf_dsq_nr_queued(). Add rb_init() and rb_fini() for a tree embedded in another object (3/3).
Each patch adds selftests, which run with:
$> ./test_progs -t 'libarena/test_rbtree*'
Changes in v3:
- Make the lockless readers static inline. A plain static function in a header fails -Wunused-function under -Werror in a file that does not call it (bot+bpf-ci, 3/3).
Changes in v2:
- Drop the comment on the insert helper and keep the name rbnode_least_upper_bound() (Emil, 1/3).
- Move rb_next() before the caching patch and into rbtree.bpf.c with the other traversal functions (Alexei, Emil, 2/3).
- Hand the head to its successor at the top of rb_node_remove(), before an RB_ALLOC tree frees the node, so that a lockless reader never sees a freed head. This drops the wrapper, was_first and the rename to rbnode_remove() (Alexei, 3/3).
- Drop the cache line alignment of the lockless fields, which showed no measurable benefit (Alexei, 3/3).
- Rename the lockless readers to rb_first_lockless(), rb_first_key_lockless() and rb_nr_nodes_lockless(), and document that a caller that dereferences the head must keep the node valid itself (Emil, 3/3).
- Drop __always_inline from static functions (Emil, 3/3).
- Add Reviewed-by tags from Alexei and Emil.
- Rebase on bpf-next.
v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/
Changwoo Min (3):
selftests/bpf: libarena: Order equal rbtree keys FIFO selftests/bpf: libarena: Add rb_next()
selftests/bpf: libarena: Cache the rbtree's leftmost node and count
.../bpf/libarena/include/libarena/rbtree.h | 57 +++- .../bpf/libarena/selftests/test_rbtree.bpf.c | 303 ++++++++++++++++++ .../selftests/bpf/libarena/src/rbtree.bpf.c | 120 +++++-- 3 files changed, 457 insertions(+), 23 deletions(-)
base-commit: 0fb18ce8a2e9de830433923cf65838933d615359
Signature · 1 line
2.56.0
Changwoo Min
reviewerPATCH v3 1/3 selftests/bpf: libarena: Order equal rbtree keys FIFO
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.
--- .../bpf/libarena/include/libarena/rbtree.h | 6 +-- .../bpf/libarena/selftests/test_rbtree.bpf.c | 37 +++++++++++++++++++ .../selftests/bpf/libarena/src/rbtree.bpf.c | 6 +-- 3 files changed, 43 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h +3 -3
@@ -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 ,
tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c +37 -0
@@ -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) {
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c +3 -3
@@ -171,7 +171,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 ;
@@ -278,8 +278,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;
Signature · 1 line
2.56.0
Changwoo Min
reviewerPATCH v3 2/3 selftests/bpf: libarena: Add rb_next()
In reply to Changwoo MinAdd rb_next(), the in-order successor through parent pointers.
Add a selftest that walks a tree with repeated keys and checks the walk against the order in which the head leaves the tree.
--- .../bpf/libarena/include/libarena/rbtree.h | 1 + .../bpf/libarena/selftests/test_rbtree.bpf.c | 57 +++++++++++++++++++ .../selftests/bpf/libarena/src/rbtree.bpf.c | 20 +++++++ 3 files changed, 78 insertions(+)
tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h +1 -0
@@ -71,6 +71,7 @@ int rb_find(struct rbtree __arena *rbtree, u64 key, u64 *value); int rb_print(struct rbtree __arena *rbtree); int rb_least(struct rbtree __arena *rbtree, u64 *key, u64 *value); int rb_pop(struct rbtree __arena *rbtree, u64 *key, u64 *value); +struct rbnode __arena *rb_next(struct rbnode __arena *node); int rb_insert_node(struct rbtree __arena *rbtree, struct rbnode __arena *node); int rb_remove_node(struct rbtree __arena *rbtree, struct rbnode __arena *node);
tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c +57 -0
63 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c +20 -0
@@ -413,6 +413,26 @@ __weak int rb_least(struct rbtree __arena *rbtree, u64 *key, u64 *value)return 0 ; } +/* +* Return the in-order successor of @node, or NULL if @node is the last one. +* Walking from the leftmost node with it visits the tree in key order, equal +* keys in insertion order. Call it under the writer's lock. +*/ +__weak +struct rbnode __arena *rb_next(struct rbnode __arena *node) +{ + struct rbnode __arena *parent; + +if (node->right) +return rbnode_least (node->right); + +/* Climb until we leave a left subtree; its parent comes next. */ +while ((parent = node->parent) && node == parent->right && can_loop) + node = parent; + +return parent; +} +/* * If we are referencing ourselves, a and b have a parent-child relation,
Signature · 1 line
2.56.0
Changwoo Min
reviewerPATCH v3 3/3 selftests/bpf: libarena: Cache the rbtree's leftmost node and count
In reply to Changwoo MinA run queue built on the tree needs lockless peeks at its head and length, like scx_bpf_dsq_peek() and scx_bpf_dsq_nr_queued() for sched_ext DSQs.
Keep the leftmost node and the node count in struct rbtree. An insert changes the head only when it links left of it, and removing the head hands it to its in-order successor before the tree changes. rb_least() and rb_pop() use the cached head. rb_first_lockless(), rb_first_key_lockless() and rb_nr_nodes_lockless() read them without the lock. The head pointer, published with release ordering, decides emptiness, and the key is read through it.
Add rb_init() for a tree embedded in another object, such as a queue that keeps its lock next to the tree, and rb_fini() to empty it, since rb_destroy() also frees the tree's memory.
Add selftests for the head and count across inserts, removals and updates, and for an embedded tree.
--- .../bpf/libarena/include/libarena/rbtree.h | 50 +++++ .../bpf/libarena/selftests/test_rbtree.bpf.c | 209 ++++++++++++++++++ .../selftests/bpf/libarena/src/rbtree.bpf.c | 94 ++++++-- 3 files changed, 336 insertions(+), 17 deletions(-)
tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h +50 -0
@@ -59,11 +59,28 @@ struct rbtree { struct rbnode __arena *root;enum rbtree_alloc alloc;enum rbtree_insert_mode insert; + +/* +* The leftmost node and the node count. The writer keeps them current, +* and readers may read them without the writer's lock (see +* rb_first_lockless() and friends). +*/ + struct rbnode __arena *first; +uint64_t nr; };#ifdef __BPF__ struct rbtree __arena *rb_create(enum rbtree_alloc alloc, enum rbtree_insert_mode insert); +/* +* Initialize and finalize a tree embedded in another arena object. +* rb_fini() frees the nodes of an RB_ALLOC tree and fails with -EBUSY on a +* non-empty RB_NOALLOC tree. It leaves the tree's memory to its owner. +*/ +int rb_init(struct rbtree __arena *rbtree, enum rbtree_alloc alloc, +enum rbtree_insert_mode insert); +int rb_fini(struct rbtree __arena *rbtree); + int rb_destroy(struct rbtree __arena *rbtree); int rb_insert(struct rbtree __arena *rbtree, u64 key, u64 value); int rb_remove(struct rbtree __arena *rbtree, u64 key);
@@ -81,4 +98,37 @@ void rb_node_free(struct rbnode __arena *rbnode); int rb_integrity_check(struct rbtree __arena *rbtree); +/* +* Lockless reads of the head and the node count. They need not be called +* under the writer's lock, so they are advisory: the tree may change before +* the caller acts on the result. The node rb_first_lockless() returns may +* have left the tree, or been freed in an RB_ALLOC tree, by the time the +* caller reads it. A caller that dereferences it must keep the node's memory +* valid itself, for example by embedding the node in an object that is freed +* only after an RCU grace period and reading it inside an RCU read-side +* critical section. The count is a separate read and need not agree with the +* head. +*/ +static inline struct rbnode __arena *rb_first_lockless(struct rbtree __arena *rbtree) +{ +return READ_ONCE (rbtree->first); +} + +/* -ENOENT if the tree is empty, 0 with the leftmost key in @key otherwise. */ +static inline int rb_first_key_lockless(struct rbtree __arena *rbtree, u64 *key) +{ + struct rbnode __arena *first = READ_ONCE(rbtree->first); + +if (!first) +return -ENOENT ; + + *key =READ_ONCE (first->key); +return 0 ; +} + +static inline u64 rb_nr_nodes_lockless(struct rbtree __arena *rbtree) +{ +return READ_ONCE (rbtree->nr); +} +#endif /* __BPF__ */
tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c +209 -0
221 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c +77 -17
170 lines not on this page · raw message
Signature · 1 line
2.56.0
Quoted 12 lines · Changwoo Min
A
sched_extscheduler can build a task queue on libarena's rbtree, keyed by vtime, in place of a vtime-ordered DSQ. Such a queue needs three things the tree lacks. This series adds them:- Serve equal keys in insertion order, as vtime-ordered DSQs do (1/3).
- Addrb_next(), so that a consumer can walk past tasks it cannot run in key order (2/3).
- Cache the leftmost node and the node count, so that other CPUs can peek at the head and the length without the writer's lock, likescx_bpf_dsq_peek()andscx_bpf_dsq_nr_queued(). Addrb_init()andrb_fini()for a tree embedded in another object (3/3).
Quoted 3 lines
Each patch adds selftests, which run with:
$> ./test_progs -t 'libarena/test_rbtree*'
Hi Changwoo,
feel free to keep my:
(though I didn't add them myself to v1's patches 2 and 3, I assume transcription error?)
Quoted 40 lines
Changes in v3:
- Make the lockless readers static inline. A plain static function in a header fails -Wunused-function under -Werror in a file that does not call it (bot+bpf-ci, 3/3).Changes in v2:
- Drop the comment on the insert helper and keep the namerbnode_least_upper_bound()(Emil, 1/3).
- Moverb_next()before the caching patch and intorbtree.bpf.cwith the other traversal functions (Alexei, Emil, 2/3).
- Hand the head to its successor at the top ofrb_node_remove(), before anRB_ALLOCtree frees the node, so that a lockless reader never sees a freed head. This drops the wrapper,was_firstand the rename torbnode_remove()(Alexei, 3/3).
- Drop the cache line alignment of the lockless fields, which showed no measurable benefit (Alexei, 3/3).
- Rename the lockless readers torb_first_lockless(),rb_first_key_lockless()andrb_nr_nodes_lockless(), and document that a caller that dereferences the head must keep the node valid itself (Emil, 3/3).
- Drop__always_inlinefrom static functions (Emil, 3/3).
- Add Reviewed-by tags from Alexei and Emil.
- Rebase on bpf-next.v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/Changwoo Min (3):
selftests/bpf: libarena: Order equal rbtree keys FIFO selftests/bpf: libarena: Addrb_next()
selftests/bpf: libarena: Cache the rbtree's leftmost node and count.../bpf/libarena/include/libarena/rbtree.h | 57 +++- .../bpf/libarena/selftests/test_rbtree.bpf.c | 303 ++++++++++++++++++ .../selftests/bpf/libarena/src/rbtree.bpf.c | 120 +++++-- 3 files changed, 457 insertions(+), 23 deletions(-)base-commit:
0fb18ce8a2e9de830433923cf65838933d615359