Skip to content

Commit 491381c

Browse files
committed
io_uring: fix up O_NONBLOCK handling for sockets
We've got two issues with the non-regular file handling for non-blocking IO: 1) We don't want to re-do a short read in full for a non-regular file, as we can't just read the data again. 2) For non-regular files that don't support non-blocking IO attempts, we need to punt to async context even if the file is opened as non-blocking. Otherwise the caller always gets -EAGAIN. Add two new request flags to handle these cases. One is just a cache of the inode S_ISREG() status, the other tells io_uring that we always need to punt this request to async context, even if REQ_F_NOWAIT is set. Cc: [email protected] Reported-by: Hrvoje Zeba <[email protected]> Tested-by: Hrvoje Zeba <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 6333ff6 commit 491381c

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

fs/io_uring.c

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ struct io_kiocb {
322322
#define REQ_F_FAIL_LINK 256 /* fail rest of links */
323323
#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
324324
#define REQ_F_TIMEOUT 1024 /* timeout request */
325+
#define REQ_F_ISREG 2048 /* regular file */
326+
#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
325327
u64 user_data;
326328
u32 result;
327329
u32 sequence;
@@ -914,26 +916,26 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
914916
return ret;
915917
}
916918

917-
static void kiocb_end_write(struct kiocb *kiocb)
919+
static void kiocb_end_write(struct io_kiocb *req)
918920
{
919-
if (kiocb->ki_flags & IOCB_WRITE) {
920-
struct inode *inode = file_inode(kiocb->ki_filp);
921+
/*
922+
* Tell lockdep we inherited freeze protection from submission
923+
* thread.
924+
*/
925+
if (req->flags & REQ_F_ISREG) {
926+
struct inode *inode = file_inode(req->file);
921927

922-
/*
923-
* Tell lockdep we inherited freeze protection from submission
924-
* thread.
925-
*/
926-
if (S_ISREG(inode->i_mode))
927-
__sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
928-
file_end_write(kiocb->ki_filp);
928+
__sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
929929
}
930+
file_end_write(req->file);
930931
}
931932

932933
static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
933934
{
934935
struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
935936

936-
kiocb_end_write(kiocb);
937+
if (kiocb->ki_flags & IOCB_WRITE)
938+
kiocb_end_write(req);
937939

938940
if ((req->flags & REQ_F_LINK) && res != req->result)
939941
req->flags |= REQ_F_FAIL_LINK;
@@ -945,7 +947,8 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
945947
{
946948
struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
947949

948-
kiocb_end_write(kiocb);
950+
if (kiocb->ki_flags & IOCB_WRITE)
951+
kiocb_end_write(req);
949952

950953
if ((req->flags & REQ_F_LINK) && res != req->result)
951954
req->flags |= REQ_F_FAIL_LINK;
@@ -1059,8 +1062,17 @@ static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
10591062
if (!req->file)
10601063
return -EBADF;
10611064

1062-
if (force_nonblock && !io_file_supports_async(req->file))
1063-
force_nonblock = false;
1065+
if (S_ISREG(file_inode(req->file)->i_mode))
1066+
req->flags |= REQ_F_ISREG;
1067+
1068+
/*
1069+
* If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1070+
* we know to async punt it even if it was opened O_NONBLOCK
1071+
*/
1072+
if (force_nonblock && !io_file_supports_async(req->file)) {
1073+
req->flags |= REQ_F_MUST_PUNT;
1074+
return -EAGAIN;
1075+
}
10641076

10651077
kiocb->ki_pos = READ_ONCE(sqe->off);
10661078
kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
@@ -1081,7 +1093,8 @@ static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
10811093
return ret;
10821094

10831095
/* don't allow async punt if RWF_NOWAIT was requested */
1084-
if (kiocb->ki_flags & IOCB_NOWAIT)
1096+
if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1097+
(req->file->f_flags & O_NONBLOCK))
10851098
req->flags |= REQ_F_NOWAIT;
10861099

10871100
if (force_nonblock)
@@ -1382,7 +1395,9 @@ static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
13821395
* need async punt anyway, so it's more efficient to do it
13831396
* here.
13841397
*/
1385-
if (force_nonblock && ret2 > 0 && ret2 < read_size)
1398+
if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1399+
(req->flags & REQ_F_ISREG) &&
1400+
ret2 > 0 && ret2 < read_size)
13861401
ret2 = -EAGAIN;
13871402
/* Catch -EAGAIN return for forced non-blocking submission */
13881403
if (!force_nonblock || ret2 != -EAGAIN) {
@@ -1447,7 +1462,7 @@ static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
14471462
* released so that it doesn't complain about the held lock when
14481463
* we return to userspace.
14491464
*/
1450-
if (S_ISREG(file_inode(file)->i_mode)) {
1465+
if (req->flags & REQ_F_ISREG) {
14511466
__sb_start_write(file_inode(file)->i_sb,
14521467
SB_FREEZE_WRITE, true);
14531468
__sb_writers_release(file_inode(file)->i_sb,
@@ -2282,7 +2297,13 @@ static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
22822297
int ret;
22832298

22842299
ret = __io_submit_sqe(ctx, req, s, force_nonblock);
2285-
if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
2300+
2301+
/*
2302+
* We async punt it if the file wasn't marked NOWAIT, or if the file
2303+
* doesn't support non-blocking read/write attempts
2304+
*/
2305+
if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2306+
(req->flags & REQ_F_MUST_PUNT))) {
22862307
struct io_uring_sqe *sqe_copy;
22872308

22882309
sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);

0 commit comments

Comments
 (0)