Put one buffer of 0x80000000 or 0xffffffff bytes in a ring and select it twice with a length of 0: by reads of a regular file, without and with IOSQE_ASYNC, and by recvs with IOSQE_ASYNC. These commit the buffer when it is selected, so the second request must fail with -ENOBUFS. From 6.12 on, without the kernel patch "io_uring/kbuf: keep ring buffer selection length within MAX_RW_COUNT", the second request gets the same buffer again and all six cases fail. Written and tested by Claude Code, I handled the review process. Signed-off-by: Hang Zhengyang --- test/buf-ring.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/test/buf-ring.c b/test/buf-ring.c index 924b5df5..30e4c450 100644 --- a/test/buf-ring.c +++ b/test/buf-ring.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "liburing.h" #include "helpers.h" @@ -401,9 +402,121 @@ static int test_running(int bgid, int entries, int loops, int use_mmap) return T_EXIT_PASS; } +/* + * A ring buffer of 2G or more, selected with a length of 0 and committed + * when it's selected (a regular file, or from io-wq), must still be + * consumed: the next selection must not hand out the same buffer again. + * Reads of a regular file, without and with IOSQE_ASYNC, and recvs with + * IOSQE_ASYNC, where the buffer is also committed at selection time. + */ +enum { LARGE_READ, LARGE_READ_ASYNC, LARGE_RECV_ASYNC, LARGE_NR }; + +static int test_large_len(unsigned int len, int mode) +{ + struct io_uring_buf_ring *br = NULL; + struct io_uring ring; + char name[] = ".buf-ring-large.XXXXXX"; + int ret, fd = -1, sv[2] = { -1, -1 }, i, res[2] = { }; + void *mem; + + /* only the pages that are written to get memory */ + mem = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + if (mem == MAP_FAILED) + return T_EXIT_SKIP; + + ret = t_create_ring(8, &ring, 0); + if (ret != T_SETUP_OK) { + munmap(mem, len); + return ret == T_SETUP_SKIP ? T_EXIT_SKIP : T_EXIT_FAIL; + } + + ret = T_EXIT_FAIL; + if (mode == LARGE_RECV_ASYNC) { + if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) { + perror("socketpair"); + goto out; + } + fd = sv[0]; + } else { + fd = mkstemp(name); + if (fd < 0) { + perror("mkstemp"); + goto out; + } + unlink(name); + if (write(fd, "0123456789", 10) != 10) { + perror("write"); + goto out; + } + } + + br = io_uring_setup_buf_ring(&ring, 1, 1, 0, &ret); + if (!br) { + fprintf(stderr, "buf ring setup failed: %d\n", ret); + ret = T_EXIT_FAIL; + goto out; + } + io_uring_buf_ring_add(br, mem, len, 1, io_uring_buf_ring_mask(1), 0); + io_uring_buf_ring_advance(br, 1); + + ret = T_EXIT_FAIL; + for (i = 0; i < 2; i++) { + struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); + struct io_uring_cqe *cqe; + + if (mode == LARGE_RECV_ASYNC) { + if (write(sv[1], "0123456789", 10) != 10) { + perror("write"); + goto out; + } + io_uring_prep_recv(sqe, fd, NULL, 0, 0); + } else { + io_uring_prep_read(sqe, fd, NULL, 0, 0); + } + sqe->flags |= IOSQE_BUFFER_SELECT; + if (mode != LARGE_READ) + sqe->flags |= IOSQE_ASYNC; + sqe->buf_group = 1; + io_uring_submit(&ring); + if (io_uring_wait_cqe(&ring, &cqe)) { + fprintf(stderr, "wait cqe failed\n"); + goto out; + } + res[i] = cqe->res; + io_uring_cqe_seen(&ring, cqe); + } + + if (res[0] != 10) + fprintf(stderr, "len %#x mode %d: first request %d\n", len, mode, res[0]); + else if (res[1] >= 0) + fprintf(stderr, "len %#x mode %d: buffer handed out twice, second request %d\n", + len, mode, res[1]); + else if (res[1] != -ENOBUFS) + fprintf(stderr, "len %#x mode %d: second request %d, want %d\n", + len, mode, res[1], -ENOBUFS); + else + ret = T_EXIT_PASS; +out: + if (br) + io_uring_free_buf_ring(&ring, br, 1, 1); + io_uring_queue_exit(&ring); + if (mode == LARGE_RECV_ASYNC) { + if (sv[0] >= 0) + close(sv[0]); + if (sv[1] >= 0) + close(sv[1]); + } else if (fd >= 0) { + close(fd); + } + munmap(mem, len); + return ret; +} + int main(int argc, char *argv[]) { int bgids[] = { 1, 127, -1 }; + int large_failed; int entries[] = {1, 32768, 4096, -1 }; int ret, i; @@ -470,6 +583,21 @@ int main(int argc, char *argv[]) } } + /* run every case, so each one shows whether it catches the bug */ + large_failed = 0; + for (i = 0; !no_buf_ring && i < 2 * LARGE_NR; i++) { + unsigned int len = (i & 1) ? 0xffffffffU : 0x80000000U; + + ret = test_large_len(len, i >> 1); + if (ret == T_EXIT_SKIP) + fprintf(stderr, "len %#x mode %d skipped\n", len, i >> 1); + else if (ret != T_EXIT_PASS) + large_failed = 1; + } + if (large_failed) { + fprintf(stderr, "test_large_len failed\n"); + return T_EXIT_FAIL; + } return T_EXIT_PASS; } base-commit: ed312b3c94f16aea679f689d4950284a954d3b5c -- 2.53.0