From nobody Tue Nov 24 11:21:47 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794391813; b=air/pOEcVEoYbUqPpia3Oc8iAILt0fKTcPmVjkCW5sZ545HAKxs2w+brJiFyCy34mNEo KaZLuD7OP8Q8MUmDSW57BAXJoXDBl9m7zhEUA523ccJ3a212Qk+OgVHC3Kh2I24qSgjT5 uFmp2Wkm4kLlPSk527QOhOcsnn/KE1BCOB1iTSTJ8CKRH76MhSbNaUEqZDzsJihvsRzvo CLqrWKXM6mrP1/YtPLCf5VgAJODRRACwSQzoncLh4S/oGhlNXvTte+1WZ/gHtddVL1pDA gwn3098BAlfwWNmZrojtm6ybNY1CRUZijnnQ2Fs7mr9eKTrNImok5r0bH1eJpp9aQJg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794391813; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=TKdv7edjmPjExE3RyciVP5KfNQ2HgIujnfELAufwRbY=; b=WG+ybpm5WllPDrKzle+BmlKQ33wYd1JyTB8fE9DNyrFDsGn10PS1Gt+UHeTGt9l7R6C0 eBCehs9Z5bEo+ugGoOZSwQXUHgqtFUmWJ0Gn/z3Dwpgvr85mWbdGKvcRt4Mc7siUn35Fq TAXKzkAZEXPdbCRRcTemddOtqP93tRapiPCRkOpiV0xXftiComaDkivM7WDXrWhxwZLVT OhH4C4ke25B5UQBU/3Z/LUXb4NpCxBazwMHiZMtIC+Xn9mMTNQcbX/nxhoDT5AIjqtWI6 vRg5s6kSZ1TGh3a9cDs8KF+6Tw3RMyLdk7817WVDqCC64gW333g6iCOtjJKmvGxbLBw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=lYMH0dCe; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6826606A31C; Wed, 11 Nov 2026 10:10:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A6826606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=lYMH0dCe DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:From:Reply-To; bh=TKdv7edjmPjExE3RyciVP5KfNQ2HgIujnfELAufwRbY=; t=1794391812; x=2109751812; b=lYMH0dCei7pknFhzgRYYfLo8lA58O0s5uUTC30SWtr9j425 aivLUSdgS+C2hYgM5fN3LZ5gGxdI6VXf+Yw8tdqpzpCsz0XYo42rBduleTus3xYxpHwAvYqXkVdFV 3DWmDOiZLnb2/eFLQLEokrsgZNbUF9eUEIngefQqHs6rM66ZWz3XjEr178id6hcXJyVppgLf3CpUQ wJZgu6fi5ILTC0/Uh72kc9tKU773Vgh4ITlUKLEtvN030MdLj8I9Nz8chX9i2o5o/3hXqIjES9U0S mmcsclOLX7rWIDfkMvGuKLM8GGFUPTDM/IM0MVsk3uPKdMDy+ah2m6X+o7s6ClEQ==; Received: from [58.29.145.179] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1xR5HH-008kZF-CK; Wed, 11 Nov 2026 11:10:04 +0100 From: Changwoo Min To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Shuah Khan , Emil Tsalapatis Cc: Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Ihor Solodrai , david.dai@linux.dev, Gavin Guo , 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 v3 1/3] selftests/bpf: libarena: Order equal rbtree keys FIFO Date: Wed, 11 Nov 2026 19:09:49 +0900 Message-ID: <20261111100951.230730-2-changwoo@igalia.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261111100951.230730-1-changwoo@igalia.com> References: <20261111100951.230730-1-changwoo@igalia.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Alexei Starovoitov Reviewed-by: Emil Tsalapatis --- .../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(-) 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); } =20 +#define FIFO_NR_NODES 64 +#define FIFO_NR_KEYS 4 + +SEC("syscall") +__weak int test_rbtree_duplicate_fifo(void) +{ + const u64 per_key =3D FIFO_NR_NODES / FIFO_NR_KEYS; + struct rbtree __arena *rbtree; + u64 key, value; + int ret, i; + + rbtree =3D rb_create(RB_ALLOC, RB_DUPLICATE); + if (!rbtree) + return 1; + + for (i =3D 0; i < FIFO_NR_NODES && can_loop; i++) { + ret =3D rb_insert(rbtree, i % FIFO_NR_KEYS, i); + if (ret) + return 2; + } + + for (i =3D 0; i < FIFO_NR_NODES && can_loop; i++) { + ret =3D rb_pop(rbtree, &key, &value); + if (ret) + return 3; + + /* All of key 0 in insertion order, then all of key 1, ... */ + if (key !=3D i / per_key) + return 4; + + if (value !=3D 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..831a1ef1c2a1 100644 --- a/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c +++ b/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c @@ -171,7 +171,7 @@ struct rbnode __arena *rbnode_least_upper_bound(struct = rbnode __arena *subtree, return NULL; =20 while (can_loop) { - dir =3D (key <=3D node->key) ? 0 : 1; + dir =3D (key < node->key) ? 0 : 1; =20 if (!node->child[dir]) break; @@ -278,8 +278,8 @@ int rb_node_insert(struct rbtree __arena *rbtree, } =20 node->parent =3D parent; - /* Also works if key =3D=3D parent->key. */ - if (key <=3D parent->key) + /* An equal key goes right, after the existing ones. */ + if (key < parent->key) parent->left =3D node; else parent->right =3D node; --=20 2.56.0 From nobody Tue Nov 24 11:21:47 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794391820; b=e1EBGYul6QYSuZoIOEKj96hhkhRlD7GWHM/xgGemP4QPpaS4WlTByCtSuvPv+EF5LMhE 8ebne9QjHz4yVX+br2IEvwB7N8HYZ1hd2zlY3MOOAzskwpJvVIDdYJprfn02+0++zHGhR JO32M55VnAC8aafTx9gPExOCuJzIx8DGPg4ev0+l0IUm9kSWBNyH45PJB2EDLMa3MmQys ns3DBrGy7aqm9tprLNqXZvlRSJ4MwO9zvU846QPKiHEOFrvM/Y5Su3x5stRDny4X7wQr5 wdZJ9KzNgoU4ummOljumXrkgyAPR/93Rvlbkh4HkXry96bexJGgQ1k3/k9v2IL1Hy8w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794391820; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Ms9BPHkoaP+qtvZ2CK2xTYT/SNrV2XAl7Rjaysi6YTs=; b=EQohumCwXVYhJEjpuZXPSAmvcA9u2aFK/kuFf/lUYWcFH6ITQ+wqRd11ffx579/qIsW8 7qnHDb4qZzZv7HR5675gHcffQQDiXk1RncXl9buSEV3zrC+tP4VcRpaKahzFYnq1o/uPV q333zoXpsAzHidgTGdYK3dNQrr9BqEr282FI0eSiCuHQuhHHy3zXFfx1damCzllbNQmmu 4J/sbj3/K2lBWnM9WNZHCho/5weMLaWv2LChwhJxQhKc4XSqODsBwM+GHx+tFwho0aT8g DHXPbP4xW82g6PSjsZMkWLDFjvzGRNvwpdlhlYr6ON1RQi9/2rAolRl9N+lneLy6awQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=CNTBXaBu; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADB3C606A33A; Wed, 11 Nov 2026 10:10:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org ADB3C606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=CNTBXaBu DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:From:Reply-To; bh=Ms9BPHkoaP+qtvZ2CK2xTYT/SNrV2XAl7Rjaysi6YTs=; t=1794391819; x=2109751819; b=CNTBXaBu6YFNMrpRfNxTawjbRS0xVL1ZEWjLxqFX28oC5fV cv5FX/499fx2XV2g3nubBuNfTg+DR5uPBGBq2gygAQwtkT0U2wDWkshCUYiyBKuGJUFevdJdMTDGt W7YvPpBzI9nlp8svBTsyVfIrjZuVWCisg0H/3IGIbe5KyhEHQEH6SbAuxIhXG4ooCYm8XeIAwE239 DI9Axn2UzjYk8Xw7I49xEJnNneo1OxPyhZtTKXxEzUyhcKhty+Y1HYJQms8TN3FjwZkBgtJ0uKjlw 2dqkWS5hMpHIb4oGdWxLoI+ijEuqRumPka2RatHCfGqjAC/Kemo4EAUzYTsrJung==; Received: from [58.29.145.179] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1xR5HN-008kaA-RO; Wed, 11 Nov 2026 11:10:10 +0100 From: Changwoo Min To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Shuah Khan , Emil Tsalapatis Cc: Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Ihor Solodrai , david.dai@linux.dev, Gavin Guo , 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 v3 2/3] selftests/bpf: libarena: Add rb_next() Date: Wed, 11 Nov 2026 19:09:50 +0900 Message-ID: <20261111100951.230730-3-changwoo@igalia.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261111100951.230730-1-changwoo@igalia.com> References: <20261111100951.230730-1-changwoo@igalia.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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); =20 int rb_insert_node(struct rbtree __arena *rbtree, struct rbnode __arena *n= ode); int rb_remove_node(struct rbtree __arena *rbtree, struct rbnode __arena *n= ode); 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); } =20 +#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 =3D 0xcafe, value; + int i; + + rbtree =3D rb_create(RB_NOALLOC, RB_DUPLICATE); + if (!rbtree) + return 1; + + /* Pseudo-random keys with repeats, so the walk crosses equal keys. */ + for (i =3D 0; i < NEXT_NR_NODES && can_loop; i++) { + /* LCG using Knuth's MMIX constants for determinism. */ + seed =3D seed * 6364136223846793005ULL + 1442695040888963407ULL; + node =3D 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 =3D rbtree->root; + while (node->left && can_loop) + node =3D node->left; + + for (i =3D 0; i < NEXT_NR_NODES && can_loop; i++) { + if (!node) + return 4; + + next_nodes[i] =3D node; + node =3D rb_next(node); + } + + if (node) + return 5; + + for (i =3D 0; i < NEXT_NR_NODES && can_loop; i++) { + if (rb_least(rbtree, NULL, &value) || value !=3D 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; } =20 +/* + * Return the in-order successor of @node, or NULL if @node is the last on= e. + * Walking from the leftmost node with it visits the tree in key order, eq= ual + * 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 =3D node->parent) && node =3D=3D parent->right && can_loop) + node =3D parent; + + return parent; +} + =20 /* * If we are referencing ourselves, a and b have a parent-child relation, --=20 2.56.0 From nobody Tue Nov 24 11:21:47 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794391827; b=pajwQifSkLwuEdwwG1nx3ocQVGqFcX5Iz057Sd/+dYcHDRjkOVbkpCoCh7rIrME4Z2pk DW4fXXQLsIwSJpx+ODOshEQIYXbBYibw1kPGhiClcr+BAolneJ1VuqGA1SWtYvI1SAmTl BsdHcY0IqtRlbvqO7OzzcCaAvB60uLHWfeb+obxfrOs/+wDy04gQRYe37p/bHqsgIByg8 JtWjuAwO7twXV8yv5YbJBwz0oM0kfIWsC0ToW0iOtH+vAvgTWDAulJm8AB4R2o7EXXXOg uv72VmOlPPuuuhBFdlVB5FHk5n+ZftkZRJyfSciY3nxyMSnWCdQgCAsq5xOx8S3hdhQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794391827; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=qm+QDt8crWU0I2ySj4ITQ4L2kOTJDBEFeviA4mgKr1k=; b=sfjuYpMPgeqsaTotdeFgg4o9XkOuCfledViDnvzkTUpcGcSZK0UhzocDDNbvDTcnToVX BASGF/kWo+fcShar+WFQwcGPAK1QtU8nBnkaIiNgkPFhxOKefTYyIhvscza1dIiAC/DLY 5H/lN0uz+/QN1OASc4pB4fU3+N3ape1kdiY2d2qvv4u3qIJUD4vqUIIeRJkoVc24WUrPq 70g7iz44MLumky5TuRYaDiCkYbNsjFuJeSk5j/LRv32qZC9JFOEEXmdRnUO7ce20SpKiO LrYg+f45iTDWfsLwril1E29py3RJBsKVTBzPGnljm5coSQyE5bHldTSMkocnzrhC26g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=nVSXrqoG; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE6B1606A884; Wed, 11 Nov 2026 10:10:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BE6B1606A884 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=nVSXrqoG DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:From:Reply-To; bh=qm+QDt8crWU0I2ySj4ITQ4L2kOTJDBEFeviA4mgKr1k=; t=1794391826; x=2109751826; b=nVSXrqoG10MtOniTQM8E4IX2eOlgd6wG9ubodWKvTQQIhYC DH3XSZCztO9xzEfGF4+8C0JCCawUIdd/C9MfNjljB0CrcbZHWSDoIEHhF7N0dzpvylHVjMPxHc4XL ctUyNSG60Ski5ktwRZNwfA1kPAGyxCRAnzHl+gc6Hv+Y11d7DguRTDf0hqixF+maunCoFDG6lz+/X 655n4wcuQPGQUNLA/NR2W1/gBflWxHrIApvbnwzX7u/HeT4yRImU+mJMNpK/707BE3Q4qta8s67TX 0+YTy8SUH0quXNhxDcdVuGYKDmPSBaz4iGyUmOOGJSv4G+pRswCcWpH0vSUUtluQ==; Received: from [58.29.145.179] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1xR5HT-008kau-O0; Wed, 11 Nov 2026 11:10:16 +0100 From: Changwoo Min To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Shuah Khan , Emil Tsalapatis Cc: Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Ihor Solodrai , david.dai@linux.dev, Gavin Guo , 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 v3 3/3] selftests/bpf: libarena: Cache the rbtree's leftmost node and count Date: Wed, 11 Nov 2026 19:09:51 +0900 Message-ID: <20261111100951.230730-4-changwoo@igalia.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261111100951.230730-1-changwoo@igalia.com> References: <20261111100951.230730-1-changwoo@igalia.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A 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. Signed-off-by: Changwoo Min Reviewed-by: Alexei Starovoitov Reviewed-by: Emil Tsalapatis --- .../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(-) diff --git a/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h= b/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h index 788e25fcd7fd..7b8afa38474b 100644 --- a/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h +++ b/tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h @@ -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; }; =20 #ifdef __BPF__ struct rbtree __arena *rb_create(enum rbtree_alloc alloc, enum rbtree_inse= rt_mode insert); =20 +/* + * 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); =20 int rb_integrity_check(struct rbtree __arena *rbtree); =20 +/* + * 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 befo= re + * 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 mem= ory + * valid itself, for example by embedding the node in an object that is fr= eed + * 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 __are= na *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 =3D READ_ONCE(rbtree->first); + + if (!first) + return -ENOENT; + + *key =3D READ_ONCE(first->key); + return 0; +} + +static inline u64 rb_nr_nodes_lockless(struct rbtree __arena *rbtree) +{ + return READ_ONCE(rbtree->nr); +} + #endif /* __BPF__ */ 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 a92b00f799f8..4da6f293e286 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,146 @@ __weak int test_rbtree_duplicate_fifo(void) return rb_destroy(rbtree); } =20 +/* + * Check the lockless head and count against a walk to the leftmost node, + * which does not use the cached head. + */ +__weak int rbtree_check_first(struct rbtree __arena *rbtree, u64 nr) +{ + struct rbnode __arena *least =3D rbtree->root; + u64 key; + + while (least && least->left && can_loop) + least =3D least->left; + + if (rb_first_lockless(rbtree) !=3D least) + return 1; + + if (rb_nr_nodes_lockless(rbtree) !=3D nr) + return 2; + + if (!least) + return rb_first_key_lockless(rbtree, &key) =3D=3D -ENOENT ? 0 : 3; + + if (rb_first_key_lockless(rbtree, &key) || key !=3D least->key) + return 4; + + return 0; +} + +#define FIRST_NR_NODES 64 + +static struct rbnode __arena *first_nodes[FIRST_NR_NODES]; + +SEC("syscall") +__weak int test_rbtree_first(void) +{ + struct rbtree __arena *rbtree; + struct rbnode __arena *node; + u64 seed =3D 12345, nr =3D 0; + int ret, i; + + rbtree =3D rb_create(RB_NOALLOC, RB_DUPLICATE); + if (!rbtree) + return 1; + + ret =3D rbtree_check_first(rbtree, 0); + if (ret) + return 10 + ret; + + /* Pseudo-random keys with repeats, so the head moves and gets ties. */ + for (i =3D 0; i < FIRST_NR_NODES && can_loop; i++) { + seed =3D seed * 6364136223846793005ULL + 1442695040888963407ULL; + node =3D rb_node_alloc((seed >> 33) % 32, i); + if (!node) + return 2; + + first_nodes[i] =3D node; + + ret =3D rb_insert_node(rbtree, node); + if (ret) + return 3; + + ret =3D rbtree_check_first(rbtree, ++nr); + if (ret) + return 20 + ret; + } + + /* Remove every other node in insertion order, head or not. */ + for (i =3D 0; i < FIRST_NR_NODES && can_loop; i +=3D 2) { + ret =3D rb_remove_node(rbtree, first_nodes[i]); + if (ret) + return 4; + + rb_node_free(first_nodes[i]); + + ret =3D rbtree_check_first(rbtree, --nr); + if (ret) + return 30 + ret; + } + + /* Remove the head of the remaining half, emptying the tree. */ + for (i =3D 0; i < FIRST_NR_NODES / 2 && can_loop; i++) { + node =3D rb_first_lockless(rbtree); + if (!node) + return 5; + + ret =3D rb_remove_node(rbtree, node); + if (ret) + return 6; + + rb_node_free(node); + + ret =3D rbtree_check_first(rbtree, --nr); + if (ret) + return 40 + ret; + } + + return rb_destroy(rbtree); +} + +SEC("syscall") +__weak int test_rbtree_first_update(void) +{ + struct rbtree __arena *rbtree; + struct rbnode __arena *node; + int ret; + + rbtree =3D rb_create(RB_ALLOC, RB_UPDATE); + if (!rbtree) + return 1; + + if (rb_insert(rbtree, 5, 1)) + return 2; + + if (rb_insert(rbtree, 5, 2)) + return 3; + + node =3D rb_first_lockless(rbtree); + if (!node || node->key !=3D 5 || node->value !=3D 2) + return 4; + + ret =3D rbtree_check_first(rbtree, 1); + if (ret) + return 10 + ret; + + if (rb_insert(rbtree, 3, 3)) + return 5; + + ret =3D rbtree_check_first(rbtree, 2); + if (ret) + return 20 + ret; + + if (rb_remove(rbtree, 3) || rb_remove(rbtree, 5)) + return 6; + + ret =3D rbtree_check_first(rbtree, 0); + if (ret) + return 30 + ret; + + return rb_destroy(rbtree); +} + #define NEXT_NR_NODES 64 =20 static struct rbnode __arena *next_nodes[NEXT_NR_NODES]; @@ -342,6 +482,75 @@ __weak int test_rbtree_next(void) return rb_destroy(rbtree); } =20 +struct rb_embed { + u64 pad; + struct rbtree tree; +}; + +SEC("syscall") +__weak int test_rbtree_embedded(void) +{ + struct rb_embed __arena *embed; + struct rbtree __arena *rbtree; + struct rbnode __arena *node; + int ret, i; + + embed =3D arena_malloc(sizeof(*embed)); + if (!embed) + return 1; + + rbtree =3D &embed->tree; + + /* Poison the fields rb_init() must set. */ + WRITE_ONCE(rbtree->root, (struct rbnode __arena *)~0ULL); + WRITE_ONCE(rbtree->first, (struct rbnode __arena *)~0ULL); + WRITE_ONCE(rbtree->nr, ~0ULL); + + if (rb_init(rbtree, RB_NOALLOC, RB_DUPLICATE)) + return 2; + + ret =3D rbtree_check_first(rbtree, 0); + if (ret) + return 10 + ret; + + for (i =3D 0; i < 4 && can_loop; i++) { + node =3D rb_node_alloc(4 - i, i); + if (!node) + return 3; + + if (rb_insert_node(rbtree, node)) + return 4; + + ret =3D rbtree_check_first(rbtree, i + 1); + if (ret) + return 20 + ret; + } + + if (rb_fini(rbtree) !=3D -EBUSY) + return 7; + + for (i =3D 0; i < 4 && can_loop; i++) { + node =3D rb_first_lockless(rbtree); + if (!node || node->key !=3D i + 1) + return 5; + + if (rb_remove_node(rbtree, node)) + return 6; + + rb_node_free(node); + + ret =3D rbtree_check_first(rbtree, 3 - i); + if (ret) + return 30 + ret; + } + + if (rb_fini(rbtree)) + return 8; + + arena_free(embed); + return 0; +} + 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 bf39cb2aa751..90fa0036e9d2 100644 --- a/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c +++ b/tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c @@ -15,14 +15,12 @@ static int rbnode_replace(struct rbtree __arena *rbtree, struct rbnode __arena *existing, struct rbnode __arena *replacement); =20 -struct rbtree __arena *rb_create(enum rbtree_alloc alloc, - enum rbtree_insert_mode insert) +__weak +int rb_init(struct rbtree __arena *rbtree, enum rbtree_alloc alloc, + enum rbtree_insert_mode insert) { - struct rbtree __arena *rbtree; - - rbtree =3D arena_malloc(sizeof(*rbtree)); if (unlikely(!rbtree)) - return NULL; + return -EINVAL; =20 /* * RB_UPDATE overwrites existing values in the nodes, but RB_NOALLOC @@ -32,19 +30,47 @@ struct rbtree __arena *rb_create(enum rbtree_alloc allo= c, */ if (alloc =3D=3D RB_NOALLOC && insert =3D=3D RB_UPDATE) { arena_stderr("WARNING: Cannot combine RB_NOALLOC and RB_UPDATE"); + return -EINVAL; + } + + WRITE_ONCE(rbtree->root, NULL); + WRITE_ONCE(rbtree->alloc, alloc); + WRITE_ONCE(rbtree->insert, insert); + WRITE_ONCE(rbtree->first, NULL); + WRITE_ONCE(rbtree->nr, 0); + + return 0; +} + +struct rbtree __arena *rb_create(enum rbtree_alloc alloc, + enum rbtree_insert_mode insert) +{ + struct rbtree __arena *rbtree; + + rbtree =3D arena_malloc(sizeof(*rbtree)); + if (unlikely(!rbtree)) + return NULL; + + if (rb_init(rbtree, alloc, insert)) { arena_free(rbtree); return NULL; } =20 - rbtree->alloc =3D alloc; - rbtree->insert =3D insert; - rbtree->root =3D NULL; - return rbtree; } =20 +static void rb_set_first(struct rbtree __arena *rbtree, + struct rbnode __arena *node) +{ + /* + * Publish the new leftmost node with release ordering, so a lockless + * reader that sees it also sees the node's fields. + */ + smp_store_release(&rbtree->first, node); +} + __weak -int rb_destroy(struct rbtree __arena *rbtree) +int rb_fini(struct rbtree __arena *rbtree) { int ret =3D 0; =20 @@ -63,11 +89,11 @@ int rb_destroy(struct rbtree __arena *rbtree) * the now freed nodes. Throw an error instead. */ if (rbtree->root) { - arena_stderr("WARNING: Destroying RB_NOALLOC tree with > 0 nodes"); + arena_stderr("WARNING: Finalizing RB_NOALLOC tree with > 0 nodes"); return -EBUSY; } =20 - goto out; + return 0; } =20 while (rbtree->root && can_loop) { @@ -76,11 +102,22 @@ int rb_destroy(struct rbtree __arena *rbtree) break; } =20 -out: - arena_free(rbtree); return ret; } =20 +__weak +int rb_destroy(struct rbtree __arena *rbtree) +{ + int ret; + + ret =3D rb_fini(rbtree); + if (ret) + return ret; + + arena_free(rbtree); + return 0; +} + static inline int rbnode_dir(struct rbnode __arena *node) { /* Arbitrarily choose a direction for the root. */ @@ -249,6 +286,8 @@ int rb_node_insert(struct rbtree __arena *rbtree, =20 if (!parent) { rbtree->root =3D node; + rb_set_first(rbtree, node); + WRITE_ONCE(rbtree->nr, 1); return 0; } =20 @@ -267,6 +306,10 @@ int rb_node_insert(struct rbtree __arena *rbtree, if (ret) return ret; =20 + /* Same key and place, so only the head's identity changes. */ + if (rbtree->first =3D=3D parent) + rb_set_first(rbtree, node); + if (rbtree->alloc =3D=3D RB_ALLOC) rb_node_free(parent); =20 @@ -284,6 +327,14 @@ int rb_node_insert(struct rbtree __arena *rbtree, else parent->right =3D node; =20 + /* + * The node is the new head only if it went left of the old one. The + * rotations below never change which node is leftmost. + */ + if (parent =3D=3D rbtree->first && parent->left =3D=3D node) + rb_set_first(rbtree, node); + WRITE_ONCE(rbtree->nr, rbtree->nr + 1); + while (can_loop) { parent =3D node->parent; if (!parent) @@ -404,7 +455,7 @@ __weak int rb_least(struct rbtree __arena *rbtree, u64 = *key, u64 *value) if (!rbtree->root) return -ENOENT; =20 - least =3D rbnode_least(rbtree->root); + least =3D rbtree->first; if (key) *key =3D least->key; if (value) @@ -601,6 +652,15 @@ int rb_node_remove(struct rbtree __arena *rbtree, bool is_red; int dir; =20 + /* + * Hand the head to its successor before the tree changes. The removal + * frees @node in an RB_ALLOC tree, so the head must not point to it + * by then. + */ + if (rbtree->first =3D=3D node) + rb_set_first(rbtree, rb_next(node)); + WRITE_ONCE(rbtree->nr, rbtree->nr - 1); + /* Both children present, replace with next largest key. */ if (node->left && node->right) { /* @@ -827,7 +887,7 @@ int rb_pop(struct rbtree __arena *rbtree, u64 *key, u64= *value) if (rbtree->alloc !=3D RB_ALLOC) return -EINVAL; =20 - node =3D rbnode_least(rbtree->root); + node =3D rbtree->first; if (unlikely(!node)) return -ENOENT; =20 --=20 2.56.0