Skip to contentSkip to conversation

Add option to ignore O_NONBLOCK for retry purposes

PATCH New Jens Axboe 1 participant 2 messages Jun 18, 2021

Subsystems (2)
lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH v2 1/1 Add option to ignore O_NONBLOCK for retry purposes

Hi,

After thinking a bit more about this, I think this makes more sense to do as a setup flag. That means that libraries/applications using it can have this just in their setup path instead of the hot path, and it also means that we can save precious space in the sqe flags.

Hence v2 turns this from an sqe flag into a setup flag instead, and makes the behavior per ring rather than per sqe.

Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH 1/1 io_uring: add IORING_SETUP_IGNORE_ONONBLOCK flag

If a file has O_NONBLOCK set, then io_uring will not arm poll and wait for data/space if none is available. Instead, -EAGAIN is returned if an IO attempt is made on the file descriptor.

For library use cases, the library may not be in full control of the file descriptor, and hence cannot modify file flags through fcntl(2). Or even if it can, it'd be inefficient and require 3 system calls to check, set, and re-set.

If a ring is setup with IORING_SETUP_IGNORE_ONONBLOCK, that tells io_uring to ignore O_NONBLOCK and arm poll to wait for data/space instead, just like we would have if O_NONBLOCK wasn't set on the file descriptor.

---
 fs/io_uring.c                 | 8 +++++---
 include/uapi/linux/io_uring.h | 1 +
 2 files changed, 6 insertions(+), 3 deletions(-)
fs/io_uring.c +5 -3
index fc8637f591a6..214b8cd297cf 100644
@@ -2686,7 +2686,8 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 		return ret;
 
 	/* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
-	if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
+	if ((kiocb->ki_flags & IOCB_NOWAIT) ||
+	    ((file->f_flags & O_NONBLOCK) && !(ctx->flags & IORING_SETUP_IGNORE_ONONBLOCK)))
 		req->flags |= REQ_F_NOWAIT;
 
 	ioprio = READ_ONCE(sqe->ioprio);
@@ -4709,7 +4710,8 @@ static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
 	unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
 	int ret;
 
-	if (req->file->f_flags & O_NONBLOCK)
+	if ((req->file->f_flags & O_NONBLOCK) &&
+	    !(req->ctx->flags & IORING_SETUP_IGNORE_ONONBLOCK))
 		req->flags |= REQ_F_NOWAIT;
 
 	ret = __sys_accept4_file(req->file, file_flags, accept->addr,
@@ -9755,7 +9757,7 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
 	if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
 			IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
 			IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
-			IORING_SETUP_R_DISABLED))
+			IORING_SETUP_R_DISABLED | IORING_SETUP_IGNORE_ONONBLOCK))
 		return -EINVAL;
 
 	return  io_uring_create(entries, &p, params);
include/uapi/linux/io_uring.h +1 -0
index f1f9ac114b51..972fa742119b 100644
@@ -98,6 +98,7 @@ enum {
 #define IORING_SETUP_CLAMP	(1U << 4)	/* clamp SQ/CQ ring sizes */
 #define IORING_SETUP_ATTACH_WQ	(1U << 5)	/* attach to existing wq */
 #define IORING_SETUP_R_DISABLED	(1U << 6)	/* start with ring disabled */
+#define IORING_SETUP_IGNORE_ONONBLOCK	(1U << 7)	/* ignore O_NONBLOCK */
 
 enum {
 	IORING_OP_NOP,
Signature · 1 line
2.32.0