Skip to contentSkip to conversation

Add option to ignore O_NONBLOCK for retry purposes

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

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

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

Hi,

Just a minor behavioral tweak that makes non-blocking file descriptors easier to deal with.

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

PATCH 1/1 io_uring: add IOSQE_IGNORE_NONBLOCK 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.

IOSQE_IGNORE_NONBLOCK provides a way to tell 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                 | 11 ++++++++---
 include/uapi/linux/io_uring.h |  3 +++
 2 files changed, 11 insertions(+), 3 deletions(-)
fs/io_uring.c +8 -3
index fc8637f591a6..2d42273bb50f 100644
@@ -108,7 +108,7 @@
 
 #define SQE_VALID_FLAGS	(IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK|	\
 				IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
-				IOSQE_BUFFER_SELECT)
+				IOSQE_BUFFER_SELECT | IOSQE_IGNORE_NONBLOCK)
 #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
 				REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS)
 
@@ -706,6 +706,7 @@ enum {
 	REQ_F_HARDLINK_BIT	= IOSQE_IO_HARDLINK_BIT,
 	REQ_F_FORCE_ASYNC_BIT	= IOSQE_ASYNC_BIT,
 	REQ_F_BUFFER_SELECT_BIT	= IOSQE_BUFFER_SELECT_BIT,
+	REQ_F_IGNORE_NONBLOCK_BIT	= IOSQE_IGNORE_NONBLOCK_BIT,
 
 	/* first byte is taken by user flags, shift it to not overlap */
 	REQ_F_FAIL_BIT		= 8,
@@ -743,6 +744,8 @@ enum {
 	REQ_F_FORCE_ASYNC	= BIT(REQ_F_FORCE_ASYNC_BIT),
 	/* IOSQE_BUFFER_SELECT */
 	REQ_F_BUFFER_SELECT	= BIT(REQ_F_BUFFER_SELECT_BIT),
+	/* IOSQE_IGNORE_NONBLOCK */
+	REQ_F_IGNORE_NONBLOCK	= BIT(REQ_F_IGNORE_NONBLOCK_BIT),
 
 	/* fail rest of links */
 	REQ_F_FAIL		= BIT(REQ_F_FAIL_BIT),
@@ -2686,7 +2689,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) && !(req->flags & REQ_F_IGNORE_NONBLOCK)))
 		req->flags |= REQ_F_NOWAIT;
 
 	ioprio = READ_ONCE(sqe->ioprio);
@@ -4709,7 +4713,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->flags & REQ_F_IGNORE_NONBLOCK))
 		req->flags |= REQ_F_NOWAIT;
 
 	ret = __sys_accept4_file(req->file, file_flags, accept->addr,
include/uapi/linux/io_uring.h +3 -0
index f1f9ac114b51..582eee6e898b 100644
@@ -70,6 +70,7 @@ enum {
 	IOSQE_IO_HARDLINK_BIT,
 	IOSQE_ASYNC_BIT,
 	IOSQE_BUFFER_SELECT_BIT,
+	IOSQE_IGNORE_NONBLOCK_BIT,
 };
 
 /*
@@ -87,6 +88,8 @@ enum {
 #define IOSQE_ASYNC		(1U << IOSQE_ASYNC_BIT)
 /* select buffer from sqe->buf_group */
 #define IOSQE_BUFFER_SELECT	(1U << IOSQE_BUFFER_SELECT_BIT)
+/* ignore if file has O_NONBLOCK set */
+#define IOSQE_IGNORE_NONBLOCK	(1U << IOSQE_IGNORE_NONBLOCK_BIT)
 
 /*
  * io_uring_setup() flags
Signature · 1 line
2.32.0