Skip to content

Commit 25e85a0

Browse files
Xiaoguang Wanggregkh
Xiaoguang Wang
authored andcommitted
io_uring: fix io_kiocb.flags modification race in IOPOLL mode
[ Upstream commit 65a6543 ] While testing io_uring in arm, we found sometimes io_sq_thread() keeps polling io requests even though there are not inflight io requests in block layer. After some investigations, found a possible race about io_kiocb.flags, see below race codes: 1) in the end of io_write() or io_read() req->flags &= ~REQ_F_NEED_CLEANUP; kfree(iovec); return ret; 2) in io_complete_rw_iopoll() if (res != -EAGAIN) req->flags |= REQ_F_IOPOLL_COMPLETED; In IOPOLL mode, io requests still maybe completed by interrupt, then above codes are not safe, concurrent modifications to req->flags, which is not protected by lock or is not atomic modifications. I also had disassemble io_complete_rw_iopoll() in arm: req->flags |= REQ_F_IOPOLL_COMPLETED; 0xffff000008387b18 <+76>: ldr w0, [x19,#104] 0xffff000008387b1c <+80>: orr w0, w0, #0x1000 0xffff000008387b20 <+84>: str w0, [x19,#104] Seems that the "req->flags |= REQ_F_IOPOLL_COMPLETED;" is load and modification, two instructions, which obviously is not atomic. To fix this issue, add a new iopoll_completed in io_kiocb to indicate whether io request is completed. Signed-off-by: Xiaoguang Wang <[email protected]> Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent a03e50c commit 25e85a0

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

fs/io_uring.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,6 @@ enum {
513513
REQ_F_INFLIGHT_BIT,
514514
REQ_F_CUR_POS_BIT,
515515
REQ_F_NOWAIT_BIT,
516-
REQ_F_IOPOLL_COMPLETED_BIT,
517516
REQ_F_LINK_TIMEOUT_BIT,
518517
REQ_F_TIMEOUT_BIT,
519518
REQ_F_ISREG_BIT,
@@ -556,8 +555,6 @@ enum {
556555
REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
557556
/* must not punt to workers */
558557
REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
559-
/* polled IO has completed */
560-
REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
561558
/* has linked timeout */
562559
REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
563560
/* timeout request */
@@ -618,6 +615,8 @@ struct io_kiocb {
618615
int cflags;
619616
bool needs_fixed_file;
620617
u8 opcode;
618+
/* polled IO has completed */
619+
u8 iopoll_completed;
621620

622621
u16 buf_index;
623622

@@ -1760,7 +1759,7 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
17601759
* If we find a request that requires polling, break out
17611760
* and complete those lists first, if we have entries there.
17621761
*/
1763-
if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1762+
if (READ_ONCE(req->iopoll_completed)) {
17641763
list_move_tail(&req->list, &done);
17651764
continue;
17661765
}
@@ -1941,7 +1940,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
19411940
req_set_fail_links(req);
19421941
req->result = res;
19431942
if (res != -EAGAIN)
1944-
req->flags |= REQ_F_IOPOLL_COMPLETED;
1943+
WRITE_ONCE(req->iopoll_completed, 1);
19451944
}
19461945

19471946
/*
@@ -1974,7 +1973,7 @@ static void io_iopoll_req_issued(struct io_kiocb *req)
19741973
* For fast devices, IO may have already completed. If it has, add
19751974
* it to the front so we find it first.
19761975
*/
1977-
if (req->flags & REQ_F_IOPOLL_COMPLETED)
1976+
if (READ_ONCE(req->iopoll_completed))
19781977
list_add(&req->list, &ctx->poll_list);
19791978
else
19801979
list_add_tail(&req->list, &ctx->poll_list);
@@ -2098,6 +2097,7 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
20982097
kiocb->ki_flags |= IOCB_HIPRI;
20992098
kiocb->ki_complete = io_complete_rw_iopoll;
21002099
req->result = 0;
2100+
req->iopoll_completed = 0;
21012101
} else {
21022102
if (kiocb->ki_flags & IOCB_HIPRI)
21032103
return -EINVAL;

0 commit comments

Comments
 (0)