* [PATCH] io_uring: add IOSQE_IGNORE_NONBLOCK flag
2021-06-18 19:19 [PATCH] Add option to ignore O_NONBLOCK for retry purposes Jens Axboe
@ 2021-06-18 19:19 ` Jens Axboe
0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2021-06-18 19:19 UTC (permalink / raw)
To: io-uring; +Cc: samuel, Jens Axboe
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.
Suggested-by: Samuel Williams <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
---
fs/io_uring.c | 11 ++++++++---
include/uapi/linux/io_uring.h | 3 +++
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/fs/io_uring.c b/fs/io_uring.c
index fc8637f591a6..2d42273bb50f 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -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,
diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h
index f1f9ac114b51..582eee6e898b 100644
--- a/include/uapi/linux/io_uring.h
+++ b/include/uapi/linux/io_uring.h
@@ -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
--
2.32.0
^ permalink raw reply related [flat|nested] 2+ messages in thread