Skip to content

Commit 65a6543

Browse files
Xiaoguang Wangaxboe
Xiaoguang Wang
authored andcommitted
io_uring: fix io_kiocb.flags modification race in IOPOLL mode
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]>
1 parent e697dee commit 65a6543

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

fs/io_uring.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ enum {
529529
REQ_F_INFLIGHT_BIT,
530530
REQ_F_CUR_POS_BIT,
531531
REQ_F_NOWAIT_BIT,
532-
REQ_F_IOPOLL_COMPLETED_BIT,
533532
REQ_F_LINK_TIMEOUT_BIT,
534533
REQ_F_TIMEOUT_BIT,
535534
REQ_F_ISREG_BIT,
@@ -574,8 +573,6 @@ enum {
574573
REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
575574
/* must not punt to workers */
576575
REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
577-
/* polled IO has completed */
578-
REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
579576
/* has linked timeout */
580577
REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
581578
/* timeout request */
@@ -640,6 +637,8 @@ struct io_kiocb {
640637
struct io_async_ctx *io;
641638
int cflags;
642639
u8 opcode;
640+
/* polled IO has completed */
641+
u8 iopoll_completed;
643642

644643
u16 buf_index;
645644

@@ -1798,7 +1797,7 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
17981797
* If we find a request that requires polling, break out
17991798
* and complete those lists first, if we have entries there.
18001799
*/
1801-
if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1800+
if (READ_ONCE(req->iopoll_completed)) {
18021801
list_move_tail(&req->list, &done);
18031802
continue;
18041803
}
@@ -1979,7 +1978,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
19791978
req_set_fail_links(req);
19801979
req->result = res;
19811980
if (res != -EAGAIN)
1982-
req->flags |= REQ_F_IOPOLL_COMPLETED;
1981+
WRITE_ONCE(req->iopoll_completed, 1);
19831982
}
19841983

19851984
/*
@@ -2012,7 +2011,7 @@ static void io_iopoll_req_issued(struct io_kiocb *req)
20122011
* For fast devices, IO may have already completed. If it has, add
20132012
* it to the front so we find it first.
20142013
*/
2015-
if (req->flags & REQ_F_IOPOLL_COMPLETED)
2014+
if (READ_ONCE(req->iopoll_completed))
20162015
list_add(&req->list, &ctx->poll_list);
20172016
else
20182017
list_add_tail(&req->list, &ctx->poll_list);
@@ -2140,6 +2139,7 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
21402139
kiocb->ki_flags |= IOCB_HIPRI;
21412140
kiocb->ki_complete = io_complete_rw_iopoll;
21422141
req->result = 0;
2142+
req->iopoll_completed = 0;
21432143
} else {
21442144
if (kiocb->ki_flags & IOCB_HIPRI)
21452145
return -EINVAL;

0 commit comments

Comments
 (0)