]> git.baikalelectronics.ru Git - kernel.git/commit
io_uring: fix io_kiocb.flags modification race in IOPOLL mode
authorXiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
Thu, 11 Jun 2020 15:39:36 +0000 (23:39 +0800)
committerJens Axboe <axboe@kernel.dk>
Thu, 11 Jun 2020 15:45:21 +0000 (09:45 -0600)
commit3cd32c09802027b9c97d024c0fd9ae4562674244
tree21668dd9a1bb03b01ebc38eeb54367a8bcb4d485
parent8d0d1f278c83a614d80cf89146577d26aed657d8
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 <xiaoguang.wang@linux.alibaba.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
fs/io_uring.c