Skip to content

Commit 5d329e1

Browse files
committed
io_uring: allow retry for O_NONBLOCK if async is supported
A common complaint is that using O_NONBLOCK files with io_uring can be a bit of a pain. Be a bit nicer and allow normal retry IFF the file does support async behavior. This makes it possible to use io_uring more reliably with O_NONBLOCK files, for use cases where it either isn't possible or feasible to modify the file flags. Cc: stable@vger.kernel.org Reported-and-tested-by: Dan Melnic <dmm@fb.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 9c7b0ba commit 5d329e1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

fs/io_uring.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,7 +2843,8 @@ static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
28432843
return __io_file_supports_nowait(req->file, rw);
28442844
}
28452845

2846-
static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2846+
static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2847+
int rw)
28472848
{
28482849
struct io_ring_ctx *ctx = req->ctx;
28492850
struct kiocb *kiocb = &req->rw.kiocb;
@@ -2865,8 +2866,13 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
28652866
if (unlikely(ret))
28662867
return ret;
28672868

2868-
/* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2869-
if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2869+
/*
2870+
* If the file is marked O_NONBLOCK, still allow retry for it if it
2871+
* supports async. Otherwise it's impossible to use O_NONBLOCK files
2872+
* reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2873+
*/
2874+
if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2875+
((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw)))
28702876
req->flags |= REQ_F_NOWAIT;
28712877

28722878
ioprio = READ_ONCE(sqe->ioprio);
@@ -3349,7 +3355,7 @@ static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
33493355
{
33503356
if (unlikely(!(req->file->f_mode & FMODE_READ)))
33513357
return -EBADF;
3352-
return io_prep_rw(req, sqe);
3358+
return io_prep_rw(req, sqe, READ);
33533359
}
33543360

33553361
/*
@@ -3542,7 +3548,7 @@ static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
35423548
{
35433549
if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
35443550
return -EBADF;
3545-
return io_prep_rw(req, sqe);
3551+
return io_prep_rw(req, sqe, WRITE);
35463552
}
35473553

35483554
static int io_write(struct io_kiocb *req, unsigned int issue_flags)

0 commit comments

Comments
 (0)