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 --- 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 +#include +#include +#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 +#include +#include +#include + +#include +#include + +#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