Skip to contentSkip to conversation

lapi/io_uring.h: Define constants that may be missing

PATCH In review Martin Doucha 2 participants 3 messages Feb 17, 2021 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lapi/io_uring.h: Define constants that may be missing

Early liburing headers did not include IOSQE_ASYNC and IOSQE_IO_HARDLINK. Check for their presence separately even if the system header file is present and define them if needed.

---
 include/lapi/io_uring.h | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
include/lapi/io_uring.h +11 -6
index 9d280f155..897ed7c64 100644
@@ -71,8 +71,6 @@ enum {
 	IOSQE_FIXED_FILE_BIT,
 	IOSQE_IO_DRAIN_BIT,
 	IOSQE_IO_LINK_BIT,
-	IOSQE_IO_HARDLINK_BIT,
-	IOSQE_ASYNC_BIT,
 };
 
 /*
@@ -84,10 +82,6 @@ enum {
 #define IOSQE_IO_DRAIN		(1U << IOSQE_IO_DRAIN_BIT)
 /* links next sqe */
 #define IOSQE_IO_LINK		(1U << IOSQE_IO_LINK_BIT)
-/* like LINK, but stronger */
-#define IOSQE_IO_HARDLINK	(1U << IOSQE_IO_HARDLINK_BIT)
-/* always go async */
-#define IOSQE_ASYNC		(1U << IOSQE_ASYNC_BIT)
 
 /*
  * io_uring_setup() flags
@@ -262,6 +256,17 @@ struct io_uring_probe {
 
 #endif /* IOSQE_FIXED_FILE */
 
+#ifndef IOSQE_IO_HADRLINK
+/* like LINK, but stronger */
+#define IOSQE_IO_HARDLINK_BIT	3
+#define IOSQE_IO_HARDLINK	(1U << IOSQE_IO_HARDLINK_BIT)
+#endif /* IOSQE_IO_HADRLINK */
+
+#ifndef IOSQE_ASYNC
+/* always go async */
+#define IOSQE_ASYNC_BIT		4
+#define IOSQE_ASYNC		(1U << IOSQE_ASYNC_BIT)
+#endif /* IOSQE_ASYNC */
 
 #ifndef HAVE_IO_URING_REGISTER
 static inline int io_uring_register(int fd, unsigned int opcode, void *arg,
Signature · 1 line
2.30.0
#1 lore raw

PATCH 2/2 syscalls/io_uring02: Use IOSQE_ASYNC when available

---
 .../kernel/syscalls/io_uring/io_uring02.c     | 70 ++++++++++++++++---
 1 file changed, 62 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/io_uring/io_uring02.c +62 -8
index 08f4a1714..cd90fbdc3 100644
@@ -79,11 +79,11 @@ static void setup(void)
 	SAFE_CHROOT(CHROOT_DIR);
 }
 
-static void run(void)
+static void drain_fallback(void)
 {
 	uint32_t i, count, tail;
 	int beef_found = 0;
-	struct io_uring_sqe *sqe_ptr;
+	struct io_uring_sqe *sqe_ptr = uring.sqr_entries;
 	const struct io_uring_cqe *cqe_ptr;
 
 	SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sockpair);
@@ -91,9 +91,6 @@ static void run(void)
 		32+sizeof(buf));
 	SAFE_FCNTL(sockpair[0], F_SETFL, O_NONBLOCK);
 
-	SAFE_IO_URING_INIT(512, &params, &uring);
-	sqe_ptr = uring.sqr_entries;
-
 	/* Add spam requests to force async processing of the real test */
 	for (i = 0, tail = *uring.sqr_tail; i < 255; i++, tail++, sqe_ptr++) {
 		memset(sqe_ptr, 0, sizeof(*sqe_ptr));
@@ -150,7 +147,7 @@ static void run(void)
 			tst_res(TFAIL | TTERRNO,
 				"Write outside chroot failed unexpectedly");
 		} else {
-			tst_res(TPASS,
+			tst_res(TPASS | TTERRNO,
 				"Write outside chroot failed as expected");
 		}
 	}
@@ -163,12 +160,69 @@ static void run(void)
 	if (count)
 		tst_res(TFAIL, "Wrong number of entries in completion queue");
 
-	/* iteration cleanup */
-	SAFE_IO_URING_CLOSE(&uring);
 	SAFE_CLOSE(sockpair[0]);
 	SAFE_CLOSE(sockpair[1]);
 }
 
+static void check_result(void)
+{
+	const struct io_uring_cqe *cqe_ptr;
+
+	cqe_ptr = uring.cqr_entries + (*uring.cqr_head & *uring.cqr_mask);
+	++*uring.cqr_head;
+	TST_ERR = -cqe_ptr->res;
+
+	if (cqe_ptr->user_data != BEEF_MARK) {
+		tst_res(TFAIL, "Unexpected entry in completion queue");
+		return;
+	}
+
+	if (cqe_ptr->res == -EINVAL) {
+		tst_res(TINFO, "IOSQE_ASYNC is not supported, using fallback");
+		drain_fallback();
+		return;
+	}
+
+	tst_res(TINFO, "IOSQE_ASYNC is supported");
+
+	if (cqe_ptr->res >= 0) {
+		tst_res(TFAIL, "Write outside chroot succeeded.");
+		return;
+	}
+
+	if (cqe_ptr->res != -ENOENT) {
+		tst_res(TFAIL | TTERRNO,
+			"Write outside chroot failed unexpectedly");
+		return;
+	}
+
+	tst_res(TPASS | TTERRNO, "Write outside chroot failed as expected");
+}
+
+static void run(void)
+{
+	uint32_t tail;
+	struct io_uring_sqe *sqe_ptr;
+
+	SAFE_IO_URING_INIT(512, &params, &uring);
+	sqe_ptr = uring.sqr_entries;
+	tail = *uring.sqr_tail;
+
+	memset(sqe_ptr, 0, sizeof(*sqe_ptr));
+	sqe_ptr->opcode = IORING_OP_SENDMSG;
+	sqe_ptr->flags = IOSQE_ASYNC;
+	sqe_ptr->fd = sendsock;
+	sqe_ptr->addr = (__u64)&beef_header;
+	sqe_ptr->user_data = BEEF_MARK;
+	uring.sqr_array[tail & *uring.sqr_mask] = 0;
+	tail++;
+
+	__atomic_store(uring.sqr_tail, &tail, __ATOMIC_RELEASE);
+	SAFE_IO_URING_ENTER(1, uring.fd, 1, 1, IORING_ENTER_GETEVENTS, NULL);
+	check_result();
+	SAFE_IO_URING_CLOSE(&uring);
+}
+
 static void cleanup(void)
 {
 	if (uring.fd >= 0)
Signature · 1 line
2.30.0
#2 lore raw

Re: [LTP] [PATCH 2/2] syscalls/io_uring02: Use IOSQE_ASYNC when available

Hi Martin,

[ Cc: io_uring folks ]

thanks a lot for your fix. Working well, patchset merged.

Kind regards,
Petr

Quoted 114 lines
---
 .../kernel/syscalls/io_uring/io_uring02.c     | 70 ++++++++++++++++---
 1 file changed, 62 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/io_uring/io_uring02.c +62 -8
index 08f4a1714..cd90fbdc3 100644
@@ -79,11 +79,11 @@ static void setup(void)
 	SAFE_CHROOT(CHROOT_DIR);
 }
 
-static void run(void)
+static void drain_fallback(void)
 {
 	uint32_t i, count, tail;
 	int beef_found = 0;
-	struct io_uring_sqe *sqe_ptr;
+	struct io_uring_sqe *sqe_ptr = uring.sqr_entries;
 	const struct io_uring_cqe *cqe_ptr;
 
 	SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sockpair);
@@ -91,9 +91,6 @@ static void run(void)
 		32+sizeof(buf));
 	SAFE_FCNTL(sockpair[0], F_SETFL, O_NONBLOCK);
 
-	SAFE_IO_URING_INIT(512, &params, &uring);
-	sqe_ptr = uring.sqr_entries;
-
 	/* Add spam requests to force async processing of the real test */
 	for (i = 0, tail = *uring.sqr_tail; i < 255; i++, tail++, sqe_ptr++) {
 		memset(sqe_ptr, 0, sizeof(*sqe_ptr));
@@ -150,7 +147,7 @@ static void run(void)
 			tst_res(TFAIL | TTERRNO,
 				"Write outside chroot failed unexpectedly");
 		} else {
-			tst_res(TPASS,
+			tst_res(TPASS | TTERRNO,
 				"Write outside chroot failed as expected");
 		}
 	}
@@ -163,12 +160,69 @@ static void run(void)
 	if (count)
 		tst_res(TFAIL, "Wrong number of entries in completion queue");
 
-	/* iteration cleanup */
-	SAFE_IO_URING_CLOSE(&uring);
 	SAFE_CLOSE(sockpair[0]);
 	SAFE_CLOSE(sockpair[1]);
 }
 
+static void check_result(void)
+{
+	const struct io_uring_cqe *cqe_ptr;
+
+	cqe_ptr = uring.cqr_entries + (*uring.cqr_head & *uring.cqr_mask);
+	++*uring.cqr_head;
+	TST_ERR = -cqe_ptr->res;
+
+	if (cqe_ptr->user_data != BEEF_MARK) {
+		tst_res(TFAIL, "Unexpected entry in completion queue");
+		return;
+	}
+
+	if (cqe_ptr->res == -EINVAL) {
+		tst_res(TINFO, "IOSQE_ASYNC is not supported, using fallback");
+		drain_fallback();
+		return;
+	}
+
+	tst_res(TINFO, "IOSQE_ASYNC is supported");
+
+	if (cqe_ptr->res >= 0) {
+		tst_res(TFAIL, "Write outside chroot succeeded.");
+		return;
+	}
+
+	if (cqe_ptr->res != -ENOENT) {
+		tst_res(TFAIL | TTERRNO,
+			"Write outside chroot failed unexpectedly");
+		return;
+	}
+
+	tst_res(TPASS | TTERRNO, "Write outside chroot failed as expected");
+}
+
+static void run(void)
+{
+	uint32_t tail;
+	struct io_uring_sqe *sqe_ptr;
+
+	SAFE_IO_URING_INIT(512, &params, &uring);
+	sqe_ptr = uring.sqr_entries;
+	tail = *uring.sqr_tail;
+
+	memset(sqe_ptr, 0, sizeof(*sqe_ptr));
+	sqe_ptr->opcode = IORING_OP_SENDMSG;
+	sqe_ptr->flags = IOSQE_ASYNC;
+	sqe_ptr->fd = sendsock;
+	sqe_ptr->addr = (__u64)&beef_header;
+	sqe_ptr->user_data = BEEF_MARK;
+	uring.sqr_array[tail & *uring.sqr_mask] = 0;
+	tail++;
+
+	__atomic_store(uring.sqr_tail, &tail, __ATOMIC_RELEASE);
+	SAFE_IO_URING_ENTER(1, uring.fd, 1, 1, IORING_ENTER_GETEVENTS, NULL);
+	check_result();
+	SAFE_IO_URING_CLOSE(&uring);
+}
+
 static void cleanup(void)
 {
 	if (uring.fd >= 0)