Skip to contentSkip to conversation

selftests/bpf: libarena: Add initial data structures

PATCH Superseded Emil Tsalapatis 3 participants 11 messages Jun 4 – Jun 5

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

v3 cover selftests/bpf: libarena: Add initial data structures

Add two new data structures to libarena. These data structures initially resided in the sched-ext repo (https://github.com/sched-ext/scx) and have been adapted to the internal libarena build system. The data structures are:

- Red black tree: Fundamental tree data structure that can also serve as a base for more domain-specific data structures.

- Lev-Chase deque: Queue data structure that allows efficient work stealing, useful in scheduling scenarios.

The data structures are accompanied by selftests that are automatically discovered by the existing libarena test_progs selftest and incorporated in the CI.

CHANGELOG
=========

v2 -> v3 (https://lore.kernel.org/bpf/[email protected]/)

- Add workaround to handle LLVM 21 and GCC 15 assignment-to-memset promotions that are causing verification failures for arena programs (CI)
- Incorporate Sashiko feedback for cleanup edge cases (Sashiko)
- properly enforce store-load dependency between writing bottom and reading top ordering in owner_add() (Sashiko)
- Simplify some of the ordering semantics in spmc

v1 -> v2 (https://lore.kernel.org/bpf/[email protected]/):

- Rename tests from st_ to test_ (Alexei)
- Removed the freelist caches from the rbtrees, previously used to defer freeing (Alexei)
- Moved the type and function definitions to use the __arena identifier
- Removed the typecasts during function return and directly return __arena pointers (Alexei)
- Renamed queues to spmc queues to abstract away the algorithm (Alexei)
- Adjusted the memory barriers in the spmc queue
- Added multithreaded testing harness for libarena programs (Alexei)
- Added parallel selftest for queues (Alexei)
- Split least upper bound and exact find operations back into separate functions to prevent RB_DUPLICATE-related bug (AI)

Emil Tsalapatis (3):
selftests/bpf: libarena: Add rbtree data structure selftests/bpf: libarena: Add spmc queue data structure selftests/bpf: libarena: parallel test harness and spmc parallel
selftest

 .../bpf/libarena/include/libarena/rbtree.h    |   83 ++
 .../bpf/libarena/include/libarena/spmc.h      |   27 +
 .../bpf/libarena/include/libarena/userspace.h |    6 +
 .../selftests/test_parallel_spmc.bpf.c        |  664 +++++++++++
 .../bpf/libarena/selftests/test_rbtree.bpf.c  |  968 +++++++++++++++
 .../bpf/libarena/selftests/test_spmc.bpf.c    |  194 +++
 .../selftests/bpf/libarena/src/rbtree.bpf.c   | 1041 +++++++++++++++++
 .../selftests/bpf/libarena/src/spmc.bpf.c     |  234 ++++
 .../selftests/bpf/prog_tests/libarena.c       |  157 +++
 9 files changed, 3374 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/libarena/spmc.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/spmc.bpf.c
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

PATCH v3 1/3 selftests/bpf: libarena: Add rbtree data structure

Add a native red-black tree data structure to libarena. The data structure supports multiple APIs (key-value based, node based) with which users can query and modify it. The tree uses the libarena memory allocator to manage its data.

---
 .../bpf/libarena/include/libarena/rbtree.h    |   83 ++
 .../bpf/libarena/selftests/test_rbtree.bpf.c  |  968 +++++++++++++++
 .../selftests/bpf/libarena/src/rbtree.bpf.c   | 1041 +++++++++++++++++
 3 files changed, 2092 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c
tools/testing/selftests/bpf/libarena/include/libarena/rbtree.h new file +83 -0
new file mode 100644
index 000000000000..486428911d96
@@ -0,0 +1,83 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause */
+
+#pragma once
+
+#define RB_MAXLVL_PRINT (16)
+
+struct rbnode;
+
+struct rbnode {
+	struct rbnode __arena *parent;
+	union {
+		struct {
+			struct rbnode __arena *left;
+			struct rbnode __arena *right;
+		};
+
+		struct rbnode __arena *child[2];
+	};
+	uint64_t key;
+	/* Used as a linked list or to store KV pairs. */
+	union {
+		struct rbnode __arena *next;
+		uint64_t value;
+	};
+	bool is_red;
+};
+
+/*
+ * Does the rbtree allocate its own nodes, or do they get
+ * allocated by the caller?
+ */
+enum rbtree_alloc {
+	RB_ALLOC,
+	RB_NOALLOC,
+};
+
+/*
+ * Specify the behavior of rbtree insertions when the key is
+ * already present in the tree.
+ *
+ * RB_DEFAULT: Default behavior, reject the new insert.
+ *
+ * RB_UPDATE: Update the existing value in the rbtree.
+ * This updates the node itself, not just the value in
+ * 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.
+ */
+enum rbtree_insert_mode {
+	RB_DEFAULT,
+	RB_UPDATE,
+	RB_DUPLICATE,
+};
+
+struct rbtree {
+	struct rbnode __arena *root;
+	enum rbtree_alloc alloc;
+	enum rbtree_insert_mode insert;
+};
+
+#ifdef __BPF__
+struct rbtree __arena *rb_create(enum rbtree_alloc alloc, enum rbtree_insert_mode insert);
+
+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);
+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);
+
+int rb_insert_node(struct rbtree __arena *rbtree, struct rbnode __arena *node);
+int rb_remove_node(struct rbtree __arena *rbtree, struct rbnode __arena *node);
+
+struct rbnode __arena *rb_node_alloc(u64 key, u64 value);
+void rb_node_free(struct rbnode __arena *rbnode);
+
+int rb_integrity_check(struct rbtree __arena *rbtree);
+
+#endif /* __BPF__ */
tools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c new file +968 -0

968 lines not on this page · raw message

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +1041 -0

1,041 lines not on this page · raw message

Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH bpf-next v3 1/3] selftests/bpf: libarena: Add rbtree data structure

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] rb_destroy() unconditionally frees the rbtree handle even if node removal fails or the loop…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] rb_destroy() unconditionally frees the rbtree handle even if node removal fails or the loop terminates early, leaking all remaining nodes and risking a Use-After-Free.
- [Medium] rb_least() and rb_destroy() lack NULL pointer validation for the rbtree argument, leading to potential NULL pointer dereferences.
--

commit 73769b0f46630a26f3f9bdbf4c6e6ee81439114f
Author: Emil Tsalapatis <[email protected]>

selftests/bpf: libarena: Add rbtree data structure

This commit adds a native red-black tree data structure to libarena. The tree uses the libarena memory allocator to manage its data and provides a set of APIs for key-value and node-based operations.

Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +0 -0
new file mode 100644
index 0000000000000..979059e9860d6

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 53

Quoted 8 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +8 -0
+__weak
+int rb_destroy(struct rbtree __arena *rbtree)
+{
+	int ret = 0;
+
+	arena_subprog_init();
+
+	if (rbtree->alloc == RB_NOALLOC) {

[Severity: Medium]
Should there be a NULL check for rbtree before dereferencing it here? Most other APIs in this file include if (unlikely(!rbtree)) at the beginning, so it looks like it might be missing here.

Quoted 15 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +15 -0
+		/*
+		 * We cannot do anything about RB_NOALLOC nodes. The whole
+		 * point of RB_NOALLOC is that the nodes are directly owned
+		 * by the caller that allocates and inserts them. We could
+		 * unilaterally grab all nodes and free them anyway, but that
+		 * would almost certainly cause UAF as the callers keep accessing
+		 * the now freed nodes. Throw an error instead.
+		 */
+		if (rbtree->root) {
+			arena_stderr("WARNING: Trying to destroy RB_NOALLOC tree "
+					"without removing all nodes");
+			return -EBUSY;
+		}
+
+		goto out;

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 78

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +10 -0
+	}
+
+	while (rbtree->root && can_loop) {
+		ret = rb_remove(rbtree, rbtree->root->key);
+		if (ret)
+			break;
+	}
+
+out:
+	arena_free(rbtree);

[Severity: High]
If the loop breaks early because rb_remove() fails or can_loop is exhausted, does unconditionally freeing rbtree here orphan the remaining nodes?

Since ret retains the error code from rb_remove(), the caller will see an error, but the tree pointer will have been freed, potentially risking a use-after-free if the caller attempts any cleanup.

Quoted 2 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +2 -0
+	return ret;
+}

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 398

Quoted 4 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +4 -0
+__weak int rb_least(struct rbtree __arena *rbtree, u64 *key, u64 *value)
+{
+	struct rbnode __arena *least;
+	if (!rbtree->root)

[Severity: Medium]
Does this code need a NULL check for rbtree before checking rbtree->root? Similar to rb_destroy(), it appears to be missing the validation used in the rest of the exported functions.

Quoted 3 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +3 -0
+		return -ENOENT;
+
+	least = rbnode_least(rbtree->root);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH bpf-next v3 1/3] selftests/bpf: libarena: Add rbtree data structure

In reply to Emil Tsalapatis
Should this check rbtree for NULL before reading rbtree->alloc? The other exported entry points open with:
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +0 -0
new file mode 100644
index 000000000000..000000000000

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 53

Quoted 8 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +8 -0
+__weak
+int rb_destroy(struct rbtree __arena *rbtree)
+{
+	int ret = 0;
+
+	arena_subprog_init();
+
+	if (rbtree->alloc == RB_NOALLOC) {

Should this check rbtree for NULL before reading rbtree->alloc? The other exported entry points open with:

	if (unlikely(!rbtree))
		return -EINVAL;

so the missing guard here looks unintentional. The same question applies to rb_least() below.

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 80

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +10 -0
+	while (rbtree->root && can_loop) {
+		ret = rb_remove(rbtree, rbtree->root->key);
+		if (ret)
+			break;
+	}
+
+out:
+	arena_free(rbtree);
+	return ret;
+}

When rb_remove() returns an error the loop breaks and falls through to 'out:', where arena_free(rbtree) runs unconditionally and the non-zero ret is returned to the caller.

Can this leave the caller holding a freed rbtree? The caller sees an error suggesting the destroy did not finish, but the handle is already freed, so any follow-up access (for example a retry of rb_destroy()) would be a use-after-free. The nodes still in the tree are also leaked on this path.

Would freeing the handle only on the success path, or returning 0 once the handle is freed, make the contract consistent?

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 399

Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +5 -0
+__weak int rb_least(struct rbtree __arena *rbtree, u64 *key, u64 *value)
+{
+	struct rbnode __arena *least;
+	if (!rbtree->root)
+		return -ENOENT;

Same NULL question as rb_destroy(): this reads rbtree->root without first checking rbtree, unlike the other APIs. Should rb_least() also do:

	if (unlikely(!rbtree))
		return -EINVAL;

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 66

Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +5 -0
+		if (rbtree->root) {
+			arena_stderr("WARNING: Trying to destroy RB_NOALLOC tree "
+					"without removing all nodes");
+			return -EBUSY;
+		}

This isn't a bug, but in review of an earlier version it was suggested not to split string literals across lines since it makes them harder to grep for, and that going over 80 or 100 columns is fine. Could this warning string be kept on a single line?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26987200618

author reviewer
#4 lore raw

Re: [PATCH bpf-next v3 1/3] selftests/bpf: libarena: Add rbtree data structure

Quoted 69 lines · bot+bpf-ci
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +0 -0
new file mode 100644
index 000000000000..000000000000

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 53

Quoted 8 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +8 -0
+__weak
+int rb_destroy(struct rbtree __arena *rbtree)
+{
+	int ret = 0;
+
+	arena_subprog_init();
+
+	if (rbtree->alloc == RB_NOALLOC) {

Should this check rbtree for NULL before reading rbtree->alloc? The other exported entry points open with:

	if (unlikely(!rbtree))
		return -EINVAL;

so the missing guard here looks unintentional. The same question applies to rb_least() below.

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 80

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +10 -0
+	while (rbtree->root && can_loop) {
+		ret = rb_remove(rbtree, rbtree->root->key);
+		if (ret)
+			break;
+	}
+
+out:
+	arena_free(rbtree);
+	return ret;
+}

When rb_remove() returns an error the loop breaks and falls through to 'out:', where arena_free(rbtree) runs unconditionally and the non-zero ret is returned to the caller.

Can this leave the caller holding a freed rbtree? The caller sees an error suggesting the destroy did not finish, but the handle is already freed, so any follow-up access (for example a retry of rb_destroy()) would be a use-after-free. The nodes still in the tree are also leaked on this path.

Would freeing the handle only on the success path, or returning 0 once the handle is freed, make the contract consistent?

[ ... ]

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 399

Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +5 -0
+__weak int rb_least(struct rbtree __arena *rbtree, u64 *key, u64 *value)
+{
+	struct rbnode __arena *least;
+	if (!rbtree->root)
+		return -ENOENT;

Same NULL question as rb_destroy(): this reads rbtree->root without first checking rbtree, unlike the other APIs. Should rb_least() also do:

	if (unlikely(!rbtree))
		return -EINVAL;

[ ... ]

Quoted 11 lines

tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c line 66

Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/rbtree.bpf.c new file +5 -0
+		if (rbtree->root) {
+			arena_stderr("WARNING: Trying to destroy RB_NOALLOC tree "
+					"without removing all nodes");
+			return -EBUSY;
+		}

This isn't a bug, but in review of an earlier version it was suggested not to split string literals across lines since it makes them harder to grep for, and that going over 80 or 100 columns is fine. Could this warning string be kept on a single line?

Ack to the bot feedback (including the other two patches), the parallel selftest is also failing on s390.

Quoted 6 lines

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26987200618

author reviewer
#5 lore raw

PATCH v3 2/3 selftests/bpf: libarena: Add spmc queue data structure

In reply to Emil Tsalapatis

Expand libarena with a single producer multiple consumer deque data structure. This is a single producer, multiple consumer lockless structure that permits efficient work stealing. The structure is a Lev-Chase queue, so it is lock-free and wait-free.

The data structure exposes three main calls. two of them are available to the thread owning the queue and one available to all threads in the program:

spmc_owner_push(): Push an item to the top of the queue. spmc_owner_pop(): Pop an item from the top of the queue. spmc_steal(): Steal a thread from the bottom of the queue from any thread.

Note that the queue is not really FIFO for all consumers, since non-owners of the queue can only work steal from the bottom.

---
 .../bpf/libarena/include/libarena/spmc.h      |  27 ++
 .../bpf/libarena/selftests/test_spmc.bpf.c    | 194 +++++++++++++++
 .../selftests/bpf/libarena/src/spmc.bpf.c     | 234 ++++++++++++++++++
 3 files changed, 455 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/libarena/include/libarena/spmc.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/spmc.bpf.c
tools/testing/selftests/bpf/libarena/include/libarena/spmc.h new file +27 -0
new file mode 100644
index 000000000000..75611276ce13
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause */
+
+#pragma once
+
+struct spmc_arr;
+
+#define SPMC_ARR_BASESZ 128
+#define SPMC_ARR_ORDERS 10
+
+struct spmc_arr {
+	u64 __arena *data;
+	u64 order;
+};
+
+struct spmc {
+	volatile struct spmc_arr __arena *cur;
+	volatile u64 top;
+	volatile u64 bottom;
+	struct spmc_arr arr[SPMC_ARR_ORDERS];
+};
+
+int spmc_owned_add(struct spmc __arena *spmc, u64 val);
+int spmc_owned_remove(struct spmc __arena *spmc, u64 *val);
+int spmc_steal(struct spmc __arena *spmc, u64 *val);
+
+struct spmc __arena *spmc_create(void);
+int spmc_destroy(struct spmc __arena *spmc);
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +194 -0

194 lines not on this page · raw message

tools/testing/selftests/bpf/libarena/src/spmc.bpf.c new file +234 -0

234 lines not on this page · raw message

Signature · 1 line
2.54.0
#6 lore raw

Re: [PATCH bpf-next v3 2/3] selftests/bpf: libarena: Add spmc queue data structure

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Variadic format string specifier mismatch in selftest logging. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Variadic format string specifier mismatch in selftest logging.
--

commit 642caf26abcc9e080fc29e8529df46f84a968a90
Author: Emil Tsalapatis <[email protected]>

selftests/bpf: libarena: Add spmc queue data structure

Expand libarena with a single producer multiple consumer deque data structure. This lock-free and wait-free structure permits efficient work stealing. The queue exposes functions for pushing, popping, and stealing work items.

Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +0 -0
new file mode 100644
index 0000000000000..e458dd330906b

[ ... ]

Quoted 6 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +6 -0
+SEC("syscall")
+int test_spmc_remove_many(void)
+{
+	u64 val, newval;
+	int ret, i;
+	u64 expected;

[ ... ]

tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c line 147

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +10 -0
+	for (i = 0; i < 500 && can_loop; i++) {
+		ret = spmc_owned_remove(spmc, &newval);
+		if (ret) {
+			arena_stderr("%s:%d error %d\n", __func__, __LINE__, ret);
+			return 1;
+		}
+
+		expected = 500 - 1 - i;
+		if (newval != expected) {
+			arena_stderr("%s:%d expected %lu found %lu\n", __func__, __LINE__, expected, newval);

[Severity: Low]
Do these format specifiers match the variable types? Both expected and newval are declared as u64, but %lu is being used. Should these use %llu to avoid compiler warnings about format string mismatches?

Quoted 14 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +14 -0
+			return 1;
+		}
+	}
+
+	spmc_destroy(spmc);
+
+	return 0;
+}
+
+SEC("syscall")
+int test_spmc_steal_many(void)
+{
+	u64 val, newval;
+	int ret, i;

[ ... ]

tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c line 186

Quoted 9 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +9 -0
+	for (i = 0; i < 500 && can_loop; i++) {
+		ret = spmc_steal(spmc, &newval);
+		if (ret) {
+			arena_stderr("%s:%d error %d\n", __func__, __LINE__, ret);
+			return 1;
+		}
+
+		if (newval != i) {
+			arena_stderr("%s:%d expected %lu found %lu\n", __func__, __LINE__, i, newval);

[Severity: Low]
Does this format string specifier mismatch the arguments? The variable i is declared as an int, but is passed to a %lu specifier. Could this result in garbage output when printing?

Quoted 8 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +8 -0
+			return 1;
+		}
+	}
+
+	spmc_destroy(spmc);
+
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH bpf-next v3 2/3] selftests/bpf: libarena: Add spmc queue data structure

In reply to Emil Tsalapatis
Both expected and newval are declared as u64 here, but they are printed with %lu. Should these be %llu instead, to avoid format string warnings?
Quoted 6 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +0 -0
new file mode 100644
index 000000000000..e458dd330906
@@ -0,0 +1,194 @@

[ ... ]

Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +5 -0
+int test_spmc_remove_many(void)
+{
+	u64 val, newval;
+	int ret, i;
+	u64 expected;

[ ... ]

tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c line 147

Quoted 3 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +3 -0
+		expected = 500 - 1 - i;
+		if (newval != expected) {
+			arena_stderr("%s:%d expected %lu found %lu\n", __func__, __LINE__, expected, newval);

Both expected and newval are declared as u64 here, but they are printed with %lu. Should these be %llu instead, to avoid format string warnings?

This was raised on v3 by the Sashiko AI review but the line looks unchanged in this version:

https://lore.kernel.org/bpf/[email protected]

Quoted 3 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +3 -0
+			return 1;
+		}
+	}

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +4 -0
+int test_spmc_steal_many(void)
+{
+	u64 val, newval;
+	int ret, i;

[ ... ]

tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c line 186

Quoted 2 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +2 -0
+		if (newval != i) {
+			arena_stderr("%s:%d expected %lu found %lu\n", __func__, __LINE__, i, newval);

Here i is an int and newval is a u64, but both are passed to %lu. Could this print garbage, and should these be %d and %llu respectively?

This was also raised on v3 by the Sashiko AI review and the line looks unchanged in this version:

https://lore.kernel.org/bpf/[email protected]

tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c line 52

Quoted 3 lines
tools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c new file +3 -0
+			return 1;
+		}
+	}

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26987200618

author reviewer
#8 lore raw

PATCH v3 3/3 selftests/bpf: libarena: parallel test harness and spmc parallel selftest

In reply to Emil Tsalapatis

Add a parallel test for the SPMC Lev-Chase workstealing queue. The queue is built to be wait-free even when there are multiple consumers, and the parallel selftest provides a signal on whether the queue behaves correctly when stress tested.

To support the test, this patch includes a test harness for parallel selftests. The spmc selftest acts as an example of the naming and other conventions expected by the harness.

---
 .../bpf/libarena/include/libarena/userspace.h |   6 +
 .../selftests/test_parallel_spmc.bpf.c        | 664 ++++++++++++++++++
 .../selftests/bpf/prog_tests/libarena.c       | 157 +++++
 3 files changed, 827 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c
tools/testing/selftests/bpf/libarena/include/libarena/userspace.h +6 -0
index 88b68ac73cca..fc27a4bcf5d7 100644
@@ -32,6 +32,12 @@ static inline bool libarena_is_asan_test_prog(const char *name)
 	return strstr(name, "asan_test") == name;
 }
 
+static inline bool libarena_is_parallel_test_prog(const char *name)
+{
+	return strstr(name, "parallel_test") == name;
+}
+
+
 static inline int libarena_run_prog_args(int prog_fd, void *args, size_t argsize)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, opts);
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +664 -0

664 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/libarena.c +157 -0

169 lines not on this page · raw message

Signature · 1 line
2.54.0
#9 lore raw

Re: [PATCH bpf-next v3 3/3] selftests/bpf: libarena: parallel test harness and spmc parallel selftest

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Medium] Missing file descriptor validation for BPF programs retrieved via manual lookup. - [Medium] The…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Medium] Missing file descriptor validation for BPF programs retrieved via manual lookup.
- [Medium] The spmc_burst_owner_round function incorrectly ignores -ETIMEDOUT errors from spmc_wait_for_round_steals, masking failure root causes.
- [Medium] The owner thread silently ignores the error return value of spmc_singleval_tryconsume.
- [Medium] Time-Of-Check to Time-Of-Use (TOCTOU) race in spmc_resize_stealer causes spurious test failures.
- [Medium] Flaky test execution due to extremely short timeout in fixed-iteration spin-loops during cross-thread synchronization.
- [Low] spmc_wait_for_steals is defined but never used, resulting in dead code.
--

commit fc468b1c5ad015597983d43b0aaf22db02e08b96
Author: Emil Tsalapatis <[email protected]>
Subject: selftests/bpf: libarena: parallel test harness and spmc parallel selftest

This patch adds a parallel test for the SPMC Lev-Chase workstealing queue. The queue is built to be wait-free even when there are multiple consumers, and the parallel selftest provides a signal on whether the queue behaves correctly when stress tested.

Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +0 -0
new file mode 100644
index 0000000000000..987537c0f7912

[ ... ]

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 144

Quoted 11 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +11 -0
+__weak
+int spmc_quiesce_on_owner(u64 epoch)
+{
+	u64 i;
+
+	bpf_for(i, 0, TEST_SPMC_SYNC_SPINS) {
+		if (test_abort)
+			return -EINTR;
+		if (smp_load_acquire(&owner_epoch) >= epoch)
+			return 0;
+	}

[Severity: Medium]
Will this hardcoded spin loop timeout cause flaky test failures?

The timeout is fixed to 1U << 18 iterations. In BPF, this can execute in a fraction of a millisecond.

However, the userspace test runner sequentially spawns worker threads using pthread_create(). Can the scheduling and thread creation overhead take multiple milliseconds, causing early threads to exhaust the spin loop and abort with -ETIMEDOUT before all peer threads have fully entered the BPF execution context?

[ ... ]

Quoted 9 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +9 -0
+static int spmc_singleval_owner(void)
+{
+	int ret;
+	u64 i;
+
+	for (i = zero; i < TEST_SPMC_SINGLEVAL_ITERS && can_loop; i++) {
+		ret = spmc_quiesce_on_stealer(i);
+		if (ret)
+			goto err;

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 309

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +10 -0
+
+		ret = spmc_owned_add(spmc, i);
+		if (ret)
+			goto err;
+
+		__sync_fetch_and_add(&owner_epoch, 1);
+
+		spmc_singleval_tryconsume(i, false);
+
+		ret = spmc_quiesce_on_stealer(i + 1);

[Severity: Medium]
Does this silently ignore the error return value from spmc_singleval_tryconsume()?

If the tryconsume function encounters an error, such as a mismatched value indicating data corruption, it returns -EINVAL without automatically setting the test_abort flag.

Since the stealer thread correctly validates this return value, should the owner thread also check it to prevent the benchmark from continuing in a corrupted state?

[ ... ]

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 404

Quoted 8 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +8 -0
+__weak
+int spmc_wait_for_steals(u64 target)
+{
+	u64 i;
+
+	bpf_for(i, 0, TEST_SPMC_SYNC_SPINS) {
+		if (test_abort)
+			return -EINTR;

[Severity: Low]
Is this function needed?

It appears spmc_wait_for_steals() is fully defined but never called anywhere in the test file.

[ ... ]

Quoted 25 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +25 -0
+static int spmc_resize_stealer(void)
+{
+	u64 val;
+	int ret;
+
+	arena_subprog_init();
+
+	ret = spmc_quiesce_on_owner(1);
+	if (ret)
+		return ret;
+
+	__sync_fetch_and_add(&stealers_started, 1);
+
+	while (can_loop) {
+		spmc_waste_time();
+		if (test_abort)
+			return -EINTR;
+
+		ret = spmc_steal(spmc, &val);
+		if (!ret) {
+			ret = spmc_update_stats(val, false);
+			if (ret)
+				return ret;
+			continue;
+		}

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 504

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +10 -0
+
+		if (ret == -EAGAIN)
+			continue;
+
+		/* Only break if this was the last resize. */
+		if (ret == -ENOENT) {
+			if (READ_ONCE(owner_epoch) >= 2)
+				break;
+			continue;
+		}

[Severity: Medium]
Is there a Time-Of-Check to Time-Of-Use race condition here that could cause spurious test failures?

If spmc_steal() returns -ENOENT, the thread evaluates owner_epoch. However, if the thread is preempted after spmc_steal() finds the queue empty, the owner could add its remaining items and update owner_epoch to 2.

When the stealer resumes, it will read owner_epoch >= 2 and break the loop, leaving newly added items unconsumed. Should the stealer re-verify the queue state after observing the epoch change?

[ ... ]

Quoted 12 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +12 -0
+__weak int
+spmc_burst_owner_round(u64 round)
+{
+	u64 i, base, stolen, expected, val;
+	int ret;
+
+	base = round * TEST_SPMC_BURST_BURST;
+	round_steals = 0;
+
+	for (i = zero; i < TEST_SPMC_BURST_BURST && can_loop; i++) {
+		ret = spmc_owned_add(spmc, base + i);
+		if (ret)

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 568

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +10 -0
+			return ret;
+	}
+
+	__sync_fetch_and_add(&owner_epoch, 1);
+
+	ret = spmc_wait_for_round_steals(TEST_SPMC_BURST_STEAL_TARGET);
+	if (ret == -EINTR)
+		return ret;
+
+	__sync_fetch_and_add(&owner_epoch, 1);

[Severity: Medium]
Does this incorrectly ignore -ETIMEDOUT errors from spmc_wait_for_round_steals()?

If the stealers are too slow, the wait function returns -ETIMEDOUT and sets the global test_abort flag.

Because the check only explicitly looks for -EINTR, the owner will fall through on -ETIMEDOUT, incorrectly increment owner_epoch, and initiate another wait. Should this handle -ETIMEDOUT to properly catch the failure?

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +0 -0
index 81bdb084c2715..a42c1935eaf6e 100644

[ ... ]

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +6 -0
+static int run_libarena_parallel_fini(struct libarena *skel, const char *name,
+				      size_t prefixlen)
+{
+	char tdname[MAX_PARTEST_NAME];
+	struct bpf_program *fini_prog;
+	int ret;

tools/testing/selftests/bpf/prog_tests/libarena.c static void run_libarena_test(struct libarena *skel, struct bpf_program *prog, · line 57

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +10 -0
+
+	ret = snprintf(tdname, sizeof(tdname), "%.*s__fini", (int)prefixlen, name);
+	if (!ASSERT_LT(ret, sizeof(tdname), "partest fini name"))
+		return -ENAMETOOLONG;
+
+	fini_prog = bpf_object__find_program_by_name(skel->obj, tdname);
+	if (!ASSERT_TRUE(fini_prog, "partest fini prog"))
+		return -ENOENT;
+
+	ret = libarena_run_prog(bpf_program__fd(fini_prog));

[Severity: Medium]
Does this pass an unverified file descriptor to libarena_run_prog()?

According to the BPF selftests subsystem guidelines, manual lookup APIs like bpf_object__find_program_by_name() can return a valid bpf_program pointer even if the program failed to load, which results in a negative FD.

Should there be an explicit file descriptor validation (such as ASSERT_GE) before using the FD?

[ ... ]

Quoted 30 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +30 -0
+static int run_libarena_parallel_test_workers(struct libarena *skel,
+		const char *name, size_t prefixlen)
+{
+	pthread_t *threads = NULL, *tmp_threads;
+	char tdname[MAX_PARTEST_NAME];
+	struct bpf_program *tdprog;
+	uint32_t nthreads;
+	void *thread_ret;
+	int ret, err = 0;
+	int i;
+
+	for (nthreads = 0; nthreads < UINT_MAX; nthreads++) {
+		ret = snprintf(tdname, sizeof(tdname), "%.*s__%u", (int)prefixlen,
+			       name, nthreads);
+		if (!ASSERT_LT(ret, sizeof(tdname), "test worker name")) {
+			err = -ENAMETOOLONG;
+			break;
+		}
+
+		/*
+		 * We enumerate the worker threads for a given test with __0, __1,
+		 * and so on. The suffixes always start from 0 and are contiguous,
+		 * so if we don't find a program with the requested name we have
+		 * discovered all available worker programs.
+		 */
+		tdprog = bpf_object__find_program_by_name(skel->obj, tdname);
+		if (!tdprog)
+			break;
+
+		/* Bump the alloc array to accommodate the new thread. */

tools/testing/selftests/bpf/prog_tests/libarena.c static void run_libarena_test(struct libarena *skel, struct bpf_program *prog, · line 102

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +10 -0
+		tmp_threads = realloc(threads, (nthreads + 1) * sizeof(*threads));
+		if (!ASSERT_TRUE(tmp_threads, "realloc")) {
+			err = -ENOMEM;
+			break;
+		}
+		threads = tmp_threads;
+
+		ret = pthread_create(&threads[nthreads], NULL,
+				     run_libarena_parallel_prog,
+				     tdprog);

[Severity: Medium]
Similar to the fini program, run_libarena_parallel_prog() extracts the FD from this manually looked-up program and uses it. Does it need file descriptor validation to prevent passing a negative FD?

Additionally, pthread_create() is called here sequentially. Does this scheduling overhead cause the spin loops in the BPF programs to time out and abort, as mentioned earlier?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#10 lore raw

Re: [PATCH bpf-next v3 3/3] selftests/bpf: libarena: parallel test harness and spmc parallel selftest

In reply to Emil Tsalapatis
Does the owner thread need to validate the return value here? spmc_singleval_tryconsume() can return -EINVAL when it encounters a mismatched value indicating data corruption, but the function…
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +0 -0
new file mode 100644
index 000000000000..a703479f478f

[ ... ]

Quoted 1 line
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +0 -0
@@ -0,0 +1,664 @@

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +7 -0
+static int spmc_singleval_owner(void)
+{
+	int ret;
+	u64 i;
+
+	for (i = zero; i < TEST_SPMC_SINGLEVAL_ITERS && can_loop; i++) {
+		ret = spmc_quiesce_on_stealer(i);

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 307

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +10 -0
+		if (ret)
+			goto err;
+
+		ret = spmc_owned_add(spmc, i);
+		if (ret)
+			goto err;
+
+		__sync_fetch_and_add(&owner_epoch, 1);
+
+		spmc_singleval_tryconsume(i, false);

Does the owner thread need to validate the return value here? spmc_singleval_tryconsume() can return -EINVAL when it encounters a mismatched value indicating data corruption, but the function returns this error without automatically setting the test_abort flag. The stealer thread at spmc_singleval_stealer() correctly validates this return value, but the owner thread ignores it, which may allow the benchmark to continue in a corrupted state. Should the owner thread check for errors from spmc_singleval_tryconsume() to prevent the benchmark from continuing if data corruption is detected?

Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +5 -0
+
+		ret = spmc_quiesce_on_stealer(i + 1);
+		if (ret)
+			goto err;
+	}

[ ... ]

Quoted 23 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +23 -0
+static int spmc_resize_stealer(void)
+{
+	u64 val;
+	int ret;
+
+	arena_subprog_init();
+
+	ret = spmc_quiesce_on_owner(1);
+	if (ret)
+		return ret;
+
+	__sync_fetch_and_add(&stealers_started, 1);
+
+	while (can_loop) {
+		spmc_waste_time();
+		if (test_abort)
+			return -EINTR;
+
+		ret = spmc_steal(spmc, &val);
+		if (!ret) {
+			ret = spmc_update_stats(val, false);
+			if (ret)
+				return ret;

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 502

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +10 -0
+			continue;
+		}
+
+		if (ret == -EAGAIN)
+			continue;
+
+		/* Only break if this was the last resize. */
+		if (ret == -ENOENT) {
+			if (READ_ONCE(owner_epoch) >= 2)
+				break;

Is there a Time-Of-Check to Time-Of-Use race condition here? If spmc_steal() returns -ENOENT, the thread evaluates owner_epoch. However, if the thread is preempted after spmc_steal() finds the queue empty, the owner could add its remaining items and update owner_epoch to 2. When the stealer resumes, it will read owner_epoch >= 2 and break the loop, leaving newly added items unconsumed.

Should the stealer re-verify the queue state after observing the epoch change?

Quoted 6 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +6 -0
+			continue;
+		}
+
+		test_abort = true;
+		return ret;
+	}

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +10 -0
+__weak int
+spmc_burst_owner_round(u64 round)
+{
+	u64 i, base, stolen, expected, val;
+	int ret;
+
+	base = round * TEST_SPMC_BURST_BURST;
+	round_steals = 0;
+
+	for (i = zero; i < TEST_SPMC_BURST_BURST && can_loop; i++) {

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 566

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +10 -0
+		ret = spmc_owned_add(spmc, base + i);
+		if (ret)
+			return ret;
+	}
+
+	__sync_fetch_and_add(&owner_epoch, 1);
+
+	ret = spmc_wait_for_round_steals(TEST_SPMC_BURST_STEAL_TARGET);
+	if (ret == -EINTR)
+		return ret;

Should this check handle -ETIMEDOUT as well? If the stealers are too slow, spmc_wait_for_round_steals() returns -ETIMEDOUT and sets the global test_abort flag. Because this check only explicitly looks for -EINTR, the owner will fall through on -ETIMEDOUT, incorrectly increment owner_epoch, and initiate another wait, which may mask the failure root cause. Would it be clearer to handle -ETIMEDOUT explicitly to properly catch the failure?

Quoted 2 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +2 -0
+
+	__sync_fetch_and_add(&owner_epoch, 1);

[ ... ]

Quoted 6 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +6 -0
+__weak
+int spmc_wait_for_steals(u64 target)
+{
+	u64 i;
+
+	bpf_for(i, 0, TEST_SPMC_SYNC_SPINS) {

tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c line 412

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c new file +10 -0
+		if (test_abort)
+			return -EINTR;
+		if (READ_ONCE(steals) >= target)
+			return 0;
+	}
+
+	test_abort = true;
+
+	return -ETIMEDOUT;
+}

Is this function needed? It appears spmc_wait_for_steals() is fully defined but never called anywhere in the test file.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +0 -0
index 81bdb084c271..03348a95fb5c 100644

[ ... ]

tools/testing/selftests/bpf/prog_tests/libarena.c static void run_libarena_test(struct libarena *skel, struct bpf_program *prog, · line 35

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +6 -0
+static void *run_libarena_parallel_prog(void *arg)
+{
+	struct bpf_program *prog = arg;
+
+	return (void *)(long)libarena_run_prog(bpf_program__fd(prog));
+}

Does this need file descriptor validation for the BPF program? The FD extracted from the manually looked-up program is used without validation. According to the BPF selftests subsystem guidelines, manual lookup APIs like bpf_object__find_program_by_name() can return a valid bpf_program pointer even if the program failed to load, which results in a negative FD. Should there be an explicit file descriptor validation (such as ASSERT_GE) before using the FD to prevent passing a negative FD?

[ ... ]

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +6 -0
+static int run_libarena_parallel_fini(struct libarena *skel, const char *name,
+				      size_t prefixlen)
+{
+	char tdname[MAX_PARTEST_NAME];
+	struct bpf_program *fini_prog;
+	int ret;

tools/testing/selftests/bpf/prog_tests/libarena.c static void run_libarena_test(struct libarena *skel, struct bpf_program *prog, · line 57

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +10 -0
+
+	ret = snprintf(tdname, sizeof(tdname), "%.*s__fini", (int)prefixlen, name);
+	if (!ASSERT_LT(ret, sizeof(tdname), "partest fini name"))
+		return -ENAMETOOLONG;
+
+	fini_prog = bpf_object__find_program_by_name(skel->obj, tdname);
+	if (!ASSERT_TRUE(fini_prog, "partest fini prog"))
+		return -ENOENT;
+
+	ret = libarena_run_prog(bpf_program__fd(fini_prog));

Does this pass an unverified file descriptor to libarena_run_prog()? According to the BPF selftests subsystem guidelines, manual lookup APIs like bpf_object__find_program_by_name() can return a valid bpf_program pointer even if the program failed to load, which results in a negative FD. Should there be an explicit file descriptor validation (such as ASSERT_GE) before using the FD?

tools/testing/selftests/bpf/prog_tests/libarena.c static void run_libarena_test(struct libarena *skel, struct bpf_program *prog, · line 61

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/libarena.c +4 -0
+	ASSERT_OK(ret, tdname);
+
+	return ret;
+}

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26987200618