[LTP] [PATCH v2 3/3] io_uring: Refactor io_uring01 to use common code

Sachin Sant sachinp@linux.ibm.com
Tue Mar 24 06:22:41 CET 2026


No functional change.

Refactor io_uring01 test case to use
- common definitions from io_uring_common.h
- remove duplicate structure definitions
- Replace manual munmap/close calls with io_uring_cleanup_queue()
  helper function
- Submit SQE using common helper

Signed-off-by: Sachin Sant <sachinp@linux.ibm.com>
---
 .../kernel/syscalls/io_uring/io_uring01.c     | 145 +++---------------
 1 file changed, 22 insertions(+), 123 deletions(-)

diff --git a/testcases/kernel/syscalls/io_uring/io_uring01.c b/testcases/kernel/syscalls/io_uring/io_uring01.c
index ab1ec00d6..4c64e562b 100644
--- a/testcases/kernel/syscalls/io_uring/io_uring01.c
+++ b/testcases/kernel/syscalls/io_uring/io_uring01.c
@@ -11,13 +11,7 @@
  * registered in the kernel for long term operation using io_uring_register().
  * This tests initiates I/O operations with the help of io_uring_enter().
  */
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <fcntl.h>
-#include "config.h"
-#include "tst_test.h"
-#include "lapi/io_uring.h"
+#include "io_uring_common.h"
 
 #define TEST_FILE "test_file"
 
@@ -32,96 +26,19 @@ static struct tcase {
 	{0, IORING_REGISTER_BUFFERS, IORING_OP_READ_FIXED},
 };
 
-struct io_sq_ring {
-	unsigned int *head;
-	unsigned int *tail;
-	unsigned int *ring_mask;
-	unsigned int *ring_entries;
-	unsigned int *flags;
-	unsigned int *array;
-};
-
-struct io_cq_ring {
-	unsigned int *head;
-	unsigned int *tail;
-	unsigned int *ring_mask;
-	unsigned int *ring_entries;
-	struct io_uring_cqe *cqes;
-};
-
-struct submitter {
-	int ring_fd;
-	struct io_sq_ring sq_ring;
-	struct io_uring_sqe *sqes;
-	struct io_cq_ring cq_ring;
-};
-
-static struct submitter sub_ring;
-static struct submitter *s = &sub_ring;
+static struct io_uring_submit s;
 static sigset_t sig;
 static struct iovec *iov;
 
-
-static void *sptr;
-static size_t sptr_size;
-static void *cptr;
-static size_t cptr_size;
-
-static int setup_io_uring_test(struct submitter *s, struct tcase *tc)
+static int setup_io_uring_test(struct io_uring_submit *s, struct tcase *tc)
 {
-	struct io_sq_ring *sring = &s->sq_ring;
-	struct io_cq_ring *cring = &s->cq_ring;
-	struct io_uring_params p;
+	int ret;
 
-	memset(&p, 0, sizeof(p));
-	p.flags |= tc->setup_flags;
-	s->ring_fd = io_uring_setup(QUEUE_DEPTH, &p);
-	if (s->ring_fd != -1) {
+	ret = io_uring_setup_queue(s, QUEUE_DEPTH, tc->setup_flags);
+	if (ret == 0)
 		tst_res(TPASS, "io_uring_setup() passed");
-	} else {
-		tst_res(TFAIL | TERRNO, "io_uring_setup() failed");
-		return 1;
-	}
-
-	sptr_size = p.sq_off.array + p.sq_entries * sizeof(unsigned int);
-
-	/* Submission queue ring buffer mapping */
-	sptr = SAFE_MMAP(0, sptr_size,
-			PROT_READ | PROT_WRITE,
-			MAP_SHARED | MAP_POPULATE,
-			s->ring_fd, IORING_OFF_SQ_RING);
-
-	/* Save global submission queue struct info */
-	sring->head = sptr + p.sq_off.head;
-	sring->tail = sptr + p.sq_off.tail;
-	sring->ring_mask = sptr + p.sq_off.ring_mask;
-	sring->ring_entries = sptr + p.sq_off.ring_entries;
-	sring->flags = sptr + p.sq_off.flags;
-	sring->array = sptr + p.sq_off.array;
-
-	/* Submission queue entries ring buffer mapping */
-	s->sqes = SAFE_MMAP(0, p.sq_entries *
-			sizeof(struct io_uring_sqe),
-			PROT_READ | PROT_WRITE,
-			MAP_SHARED | MAP_POPULATE,
-			s->ring_fd, IORING_OFF_SQES);
-
-	cptr_size = p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe);
-
-	/* Completion queue ring buffer mapping */
-	cptr = SAFE_MMAP(0, cptr_size,
-			PROT_READ | PROT_WRITE,
-			MAP_SHARED | MAP_POPULATE,
-			s->ring_fd, IORING_OFF_CQ_RING);
-
-	/* Save global completion queue struct info */
-	cring->head = cptr + p.cq_off.head;
-	cring->tail = cptr + p.cq_off.tail;
-	cring->ring_mask = cptr + p.cq_off.ring_mask;
-	cring->ring_entries = cptr + p.cq_off.ring_entries;
-	cring->cqes = cptr + p.cq_off.cqes;
 
-	return 0;
+	return ret;
 }
 
 static void check_buffer(char *buffer, size_t len)
@@ -139,7 +56,7 @@ static void check_buffer(char *buffer, size_t len)
 		tst_res(TPASS, "Buffer filled in correctly");
 }
 
-static void drain_uring_cq(struct submitter *s, unsigned int exp_events)
+static void drain_uring_cq(struct io_uring_submit *s, unsigned int exp_events)
 {
 	struct io_cq_ring *cring = &s->cq_ring;
 	unsigned int head = *cring->head;
@@ -175,12 +92,10 @@ static void drain_uring_cq(struct submitter *s, unsigned int exp_events)
 	        events, exp_events);
 }
 
-static int submit_to_uring_sq(struct submitter *s, struct tcase *tc)
+static int submit_to_uring_sq(struct io_uring_submit *s, struct tcase *tc)
 {
-	unsigned int index = 0, tail = 0, next_tail = 0;
-	struct io_sq_ring *sring = &s->sq_ring;
-	struct io_uring_sqe *sqe;
 	int ret;
+	int fd;
 
 	memset(iov->iov_base, 0, iov->iov_len);
 
@@ -193,26 +108,13 @@ static int submit_to_uring_sq(struct submitter *s, struct tcase *tc)
 		return 1;
 	}
 
-	int fd = SAFE_OPEN(TEST_FILE, O_RDONLY);
-
-	/* Submission queue entry addition to SQE ring buffer tail */
-	tail = *sring->tail;
-	next_tail = tail + 1;
-	index = tail & *s->sq_ring.ring_mask;
-	sqe = &s->sqes[index];
-	sqe->flags = 0;
-	sqe->fd = fd;
-	sqe->opcode = tc->enter_flags;
-	sqe->addr = (unsigned long)iov->iov_base;
-	sqe->len = BLOCK_SZ;
-	sqe->off = 0;
-	sqe->user_data = (unsigned long long)iov;
-	sring->array[index] = index;
-	tail = next_tail;
-
-	/* Kernel to notice the tail update */
-	if (*sring->tail != tail)
-		*sring->tail = tail;
+	fd = SAFE_OPEN(TEST_FILE, O_RDONLY);
+
+	/* Submit SQE using common helper */
+	io_uring_submit_sqe_internal(s, fd, tc->enter_flags,
+				     (unsigned long)iov->iov_base,
+				     BLOCK_SZ, 0,
+				     (unsigned long long)iov);
 
 	ret = io_uring_enter(s->ring_fd, 1, 1, IORING_ENTER_GETEVENTS, &sig);
 	if (ret == 1) {
@@ -229,23 +131,20 @@ static int submit_to_uring_sq(struct submitter *s, struct tcase *tc)
 
 static void cleanup_io_uring_test(void)
 {
-	io_uring_register(s->ring_fd, IORING_UNREGISTER_BUFFERS,
+	io_uring_register(s.ring_fd, IORING_UNREGISTER_BUFFERS,
 			  NULL, QUEUE_DEPTH);
-	SAFE_MUNMAP(s->sqes, sizeof(struct io_uring_sqe));
-	SAFE_MUNMAP(cptr, cptr_size);
-	SAFE_MUNMAP(sptr, sptr_size);
-	SAFE_CLOSE(s->ring_fd);
+	io_uring_cleanup_queue(&s, QUEUE_DEPTH);
 }
 
 static void run(unsigned int n)
 {
 	struct tcase *tc = &tcases[n];
 
-	if (setup_io_uring_test(s, tc))
+	if (setup_io_uring_test(&s, tc))
 		return;
 
-	if (!submit_to_uring_sq(s, tc))
-		drain_uring_cq(s, 1);
+	if (!submit_to_uring_sq(&s, tc))
+		drain_uring_cq(&s, 1);
 
 	cleanup_io_uring_test();
 }
-- 
2.39.1



More information about the ltp mailing list