Add 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. Signed-off-by: Changwoo Min Reviewed-by: Alexei Starovoitov Reviewed-by: Emil Tsalapatis --- .../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(+) diff --git a/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h b/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h index 00e63b12d82e..788e25fcd7fd 100644 --- a/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h +++ b/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h @@ -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); 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 1e2a0fac1e4f..a92b00f799f8 100644 --- a/tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c +++ b/tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c @@ -285,6 +285,63 @@ __weak int test_rbtree_duplicate_fifo(void) return rb_destroy(rbtree); } +#define NEXT_NR_NODES 64 + +static struct rbnode __arena *next_nodes[NEXT_NR_NODES]; + +SEC("syscall") +__weak int test_rbtree_next(void) +{ + struct rbtree __arena *rbtree; + struct rbnode __arena *node; + u64 seed = 0xcafe, value; + int i; + + rbtree = rb_create(RB_NOALLOC, RB_DUPLICATE); + if (!rbtree) + return 1; + + /* Pseudo-random keys with repeats, so the walk crosses equal keys. */ + for (i = 0; i < NEXT_NR_NODES && can_loop; i++) { + /* LCG using Knuth's MMIX constants for determinism. */ + seed = seed * 6364136223846793005ULL + 1442695040888963407ULL; + node = rb_node_alloc((seed >> 33) % 16, i); + if (!node) + return 2; + + if (rb_insert_node(rbtree, node)) + return 3; + } + + /* Start the walk at the leftmost node. */ + node = rbtree->root; + while (node->left && can_loop) + node = node->left; + + for (i = 0; i < NEXT_NR_NODES && can_loop; i++) { + if (!node) + return 4; + + next_nodes[i] = node; + node = rb_next(node); + } + + if (node) + return 5; + + for (i = 0; i < NEXT_NR_NODES && can_loop; i++) { + if (rb_least(rbtree, NULL, &value) || value != next_nodes[i]->value) + return 6; + + if (rb_remove_node(rbtree, next_nodes[i])) + return 7; + + rb_node_free(next_nodes[i]); + } + + 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 831a1ef1c2a1..bf39cb2aa751 100644 --- a/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c +++ b/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c @@ -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, -- 2.56.0