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 09/10] io_uring/selftests: check loop CQ overflow handling
Date: Mon, 23 Feb 2026 14:10:20 +0000	[thread overview]
Message-ID: <1538be8a738055bdb7a1a6493d8e4eca40c58a18.1771855761.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1771855760.git.asml.silence@gmail.com>

Make sure that CQ overflowing works well with loop execution. It adds a
BPF program that first submits enough requests to trigger overflows and
then empties the CQ.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 tools/testing/selftests/io_uring/Makefile     |  2 +-
 .../testing/selftests/io_uring/overflow.bpf.c | 51 +++++++++++++++++++
 tools/testing/selftests/io_uring/overflow.c   | 50 ++++++++++++++++++
 3 files changed, 102 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/io_uring/overflow.bpf.c
 create mode 100644 tools/testing/selftests/io_uring/overflow.c

diff --git a/tools/testing/selftests/io_uring/Makefile b/tools/testing/selftests/io_uring/Makefile
index 5b9518140f4c..77df197449ef 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
+TEST_GEN_PROGS := nops_loop overflow
 
 # override lib.mk's default rules
 OVERRIDE_TARGETS := 1
diff --git a/tools/testing/selftests/io_uring/overflow.bpf.c b/tools/testing/selftests/io_uring/overflow.bpf.c
new file mode 100644
index 000000000000..f347066e90ad
--- /dev/null
+++ b/tools/testing/selftests/io_uring/overflow.bpf.c
@@ -0,0 +1,51 @@
+/* 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";
+
+const volatile struct ring_info ri;
+static unsigned submitted;
+
+SEC("struct_ops.s/overflow_loop_step")
+int BPF_PROG(overflow_loop_step, struct io_ring_ctx *ring,
+				 struct iou_loop_params *ls)
+{
+	struct io_uring *sq_hdr, *cq_hdr;
+	struct io_uring_sqe *sqe;
+	void *rings;
+
+	sqe = (void *)bpf_io_uring_get_region(ring, IOU_REGION_SQ,
+				ri.sq_entries * sizeof(struct io_uring_sqe));
+	rings = (void *)bpf_io_uring_get_region(ring, IOU_REGION_CQ,
+				ri.cqes_offset + ri.cq_entries * sizeof(struct io_uring_cqe));
+	if (!rings || !sqe)
+		return IOU_LOOP_STOP;
+	sq_hdr = rings + ri.sq_hdr_offset;
+	cq_hdr = rings + ri.cq_hdr_offset;
+
+	/* keep submitting until we overrun the CQ and trigger an overflow */
+	if (submitted < 2 * ri.cq_entries) {
+		*sqe = (struct io_uring_sqe){};
+		sqe->opcode = IORING_OP_NOP;
+		sq_hdr->tail++;
+
+		bpf_io_uring_submit_sqes(ring, 1);
+		submitted++;
+		return IOU_LOOP_CONTINUE;
+	}
+
+	if (cq_hdr->tail == cq_hdr->head)
+		return IOU_LOOP_STOP;
+	/* Consume all queued CQEs and let io_uring to flush overflown CQEs */
+	cq_hdr->head = cq_hdr->tail;
+	return IOU_LOOP_CONTINUE;
+}
+
+SEC(".struct_ops.link")
+struct io_uring_bpf_ops overflow_ops = {
+	.loop_step = (void *)overflow_loop_step,
+};
diff --git a/tools/testing/selftests/io_uring/overflow.c b/tools/testing/selftests/io_uring/overflow.c
new file mode 100644
index 000000000000..724f3894d362
--- /dev/null
+++ b/tools/testing/selftests/io_uring/overflow.c
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Test that the loop handling logic around BPF doesn't deadlock on overflows.
+ */
+#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 "overflow.bpf.skel.h"
+
+int main()
+{
+	struct bpf_link *link;
+	struct overflow *skel;
+	struct ring_ctx ctx;
+	int ret;
+
+	ring_ctx_create(&ctx, 0);
+
+	skel = overflow__open();
+	if (!skel) {
+		fprintf(stderr, "can't generate skeleton\n");
+		exit(1);
+	}
+	skel->struct_ops.overflow_ops->ring_fd = ctx.ring.ring_fd;
+	skel->rodata->ri = ctx.ri;
+
+	ret = overflow__load(skel);
+	if (ret) {
+		fprintf(stderr, "failed to load skeleton\n");
+		exit(1);
+	}
+	link = bpf_map__attach_struct_ops(skel->maps.overflow_ops);
+	if (!link) {
+		fprintf(stderr, "failed to attach ops\n");
+		return 1;
+	}
+
+	ring_ctx_run(&ctx);
+
+	bpf_link__destroy(link);
+	overflow__destroy(skel);
+	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 ` Pavel Begunkov [this message]
2026-02-23 14:10 ` [PATCH v9 10/10] io_uring/selftests: test BPF [un]registration Pavel Begunkov

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=1538be8a738055bdb7a1a6493d8e4eca40c58a18.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