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. Suggested-by: Samuel Williams <samuel@xxxxxxxxxxxxx> Signed-off-by: Jens Axboe <axboe@xxxxxxxxx> --- fs/io_uring.c | 8 +++++--- include/uapi/linux/io_uring.h | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index fc8637f591a6..214b8cd297cf 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -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); diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index f1f9ac114b51..972fa742119b 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -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, -- 2.32.0