public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: Pavel Begunkov <asml.silence@gmail.com>
To: io-uring@vger.kernel.org
Cc: asml.silence@gmail.com, bpf@vger.kernel.org, axboe@kernel.dk,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>
Subject: [PATCH v9 10/10] io_uring/selftests: test BPF [un]registration
Date: Mon, 23 Feb 2026 14:10:21 +0000	[thread overview]
Message-ID: <6015650200dd82a41a50b0b23fd30f57b76a6203.1771855761.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1771855760.git.asml.silence@gmail.com>

Make sure BPF registration and unregistration leave io_uring in the
right state.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 tools/testing/selftests/io_uring/Makefile     |  2 +-
 .../testing/selftests/io_uring/common-defs.h  |  4 +
 tools/testing/selftests/io_uring/unreg.bpf.c  | 25 +++++
 tools/testing/selftests/io_uring/unreg.c      | 92 +++++++++++++++++++
 4 files changed, 122 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/io_uring/unreg.bpf.c
 create mode 100644 tools/testing/selftests/io_uring/unreg.c

diff --git a/tools/testing/selftests/io_uring/Makefile b/tools/testing/selftests/io_uring/Makefile
index 77df197449ef..37f50acdba37 100644
--- a/tools/testing/selftests/io_uring/Makefile
+++ b/tools/testing/selftests/io_uring/Makefile
@@ -3,7 +3,7 @@ include ../../../build/Build.include
 include ../../../scripts/Makefile.arch
 include ../../../scripts/Makefile.include
 
-TEST_GEN_PROGS := nops_loop overflow
+TEST_GEN_PROGS := nops_loop overflow unreg
 
 # override lib.mk's default rules
 OVERRIDE_TARGETS := 1
diff --git a/tools/testing/selftests/io_uring/common-defs.h b/tools/testing/selftests/io_uring/common-defs.h
index 948453c90375..9a44e0687436 100644
--- a/tools/testing/selftests/io_uring/common-defs.h
+++ b/tools/testing/selftests/io_uring/common-defs.h
@@ -24,4 +24,8 @@ struct nops_state {
 	int		reqs_left;
 };
 
+struct unreg_state {
+	unsigned	times_invoked;
+};
+
 #endif /* IOU_TOOLS_COMMON_DEFS_H */
diff --git a/tools/testing/selftests/io_uring/unreg.bpf.c b/tools/testing/selftests/io_uring/unreg.bpf.c
new file mode 100644
index 000000000000..e872915b09dd
--- /dev/null
+++ b/tools/testing/selftests/io_uring/unreg.bpf.c
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/types.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "vmlinux.h"
+#include "common-defs.h"
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
+
+SEC("struct_ops.s/unreg_loop_step")
+int BPF_PROG(unreg_loop_step, struct io_ring_ctx *ring,
+			      struct iou_loop_params *ls)
+{
+	struct unreg_state *us;
+
+	us = (void *)bpf_io_uring_get_region(ring, IOU_REGION_MEM, sizeof(*us));
+	if (us)
+		us->times_invoked++;
+	return IOU_LOOP_STOP;
+}
+
+SEC(".struct_ops.link")
+struct io_uring_bpf_ops unreg_ops = {
+	.loop_step = (void *)unreg_loop_step,
+};
diff --git a/tools/testing/selftests/io_uring/unreg.c b/tools/testing/selftests/io_uring/unreg.c
new file mode 100644
index 000000000000..43076681999f
--- /dev/null
+++ b/tools/testing/selftests/io_uring/unreg.c
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Test BPF registration / unregistration works and doesn't leave a dangling
+ * function pointer.
+ */
+#include <linux/stddef.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+
+#include <bpf/libbpf.h>
+#include <io_uring/mini_liburing.h>
+
+#include "helpers.h"
+#include "unreg.bpf.skel.h"
+
+static struct unreg *load_unreg(struct ring_ctx *ctx)
+{
+	struct unreg *skel;
+	int ret;
+
+	skel = unreg__open();
+	if (!skel) {
+		fprintf(stderr, "can't generate skeleton\n");
+		exit(1);
+	}
+
+	skel->struct_ops.unreg_ops->ring_fd = ctx->ring.ring_fd;
+
+	ret = unreg__load(skel);
+	if (ret) {
+		fprintf(stderr, "failed to load skeleton\n");
+		exit(1);
+	}
+
+	return skel;
+}
+
+int main()
+{
+	struct bpf_link *link1, *link2;
+	struct unreg *skel1, *skel2;
+	struct unreg_state *us;
+	struct ring_ctx ctx;
+
+	ring_ctx_create(&ctx, sizeof(struct unreg_state));
+	us = ctx.region;
+
+	skel1 = load_unreg(&ctx);
+	skel2 = load_unreg(&ctx);
+
+	link1 = bpf_map__attach_struct_ops(skel1->maps.unreg_ops);
+	if (!link1) {
+		fprintf(stderr, "failed to attach ops\n");
+		return 1;
+	}
+
+	ring_ctx_run(&ctx);
+	if (us->times_invoked != 1) {
+		fprintf(stderr, "failed to run BPF\n");
+		return 1;
+	}
+
+	/* remove the program and give the kernel time to actually destroy it */
+	bpf_link__destroy(link1);
+	unreg__destroy(skel1);
+	sleep(1);
+
+	ring_ctx_run(&ctx);
+	if (us->times_invoked != 1) {
+		fprintf(stderr, "Executed removed BPF\n");
+		return 1;
+	}
+
+	/* try to attach another program */
+	link2 = bpf_map__attach_struct_ops(skel2->maps.unreg_ops);
+	if (!link2) {
+		fprintf(stderr, "failed to reattach ops\n");
+		return 1;
+	}
+
+	ring_ctx_run(&ctx);
+	if (us->times_invoked != 2) {
+		fprintf(stderr, "failed to run reattached BPF\n");
+		return 1;
+	}
+
+	bpf_link__destroy(link2);
+	unreg__destroy(skel2);
+	ring_ctx_destroy(&ctx);
+	return 0;
+}
-- 
2.53.0


      parent reply	other threads:[~2026-02-23 14:10 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-23 14:10 [PATCH v9 00/10] BPF controlled io_uring Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 01/10] io_uring: introduce callback driven main loop Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 02/10] io_uring/bpf-ops: implement loop_step with BPF struct_ops Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 03/10] io_uring/bpf-ops: add kfunc helpers Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 04/10] io_uring/bpf-ops: implement bpf ops registration Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 05/10] io_uring: update tools uapi headers Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 06/10] io_uring/mini_liburing: add include guards Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 07/10] io_uring/mini_liburing: add io_uring_register() Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 08/10] selftests/io_uring: add BPF event loop example Pavel Begunkov
2026-02-23 14:10 ` [PATCH v9 09/10] io_uring/selftests: check loop CQ overflow handling Pavel Begunkov
2026-02-23 14:10 ` Pavel Begunkov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6015650200dd82a41a50b0b23fd30f57b76a6203.1771855761.git.asml.silence@gmail.com \
    --to=asml.silence@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=bpf@vger.kernel.org \
    --cc=io-uring@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox