Skip to content

Commit

Permalink
io_uring: fix possible race condition against REQ_F_NEED_CLEANUP
Browse files Browse the repository at this point in the history
In io_read() or io_write(), when io request is submitted successfully,
it'll go through the below sequence:

    kfree(iovec);
    req->flags &= ~REQ_F_NEED_CLEANUP;
    return ret;

But clearing REQ_F_NEED_CLEANUP might be unsafe. The io request may
already have been completed, and then io_complete_rw_iopoll()
and io_complete_rw() will be called, both of which will also modify
req->flags if needed. This causes a race condition, with concurrent
non-atomic modification of req->flags.

To eliminate this race, in io_read() or io_write(), if io request is
submitted successfully, we don't remove REQ_F_NEED_CLEANUP flag. If
REQ_F_NEED_CLEANUP is set, we'll leave __io_req_aux_free() to the
iovec cleanup work correspondingly.

Cc: [email protected]
Signed-off-by: Xiaoguang Wang <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Xiaoguang Wang authored and axboe committed Jun 18, 2020
1 parent 56952e9 commit 6f2cc16
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions fs/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2670,8 +2670,8 @@ static int io_read(struct io_kiocb *req, bool force_nonblock)
}
}
out_free:
kfree(iovec);
req->flags &= ~REQ_F_NEED_CLEANUP;
if (!(req->flags & REQ_F_NEED_CLEANUP))
kfree(iovec);
return ret;
}

Expand Down Expand Up @@ -2793,8 +2793,8 @@ static int io_write(struct io_kiocb *req, bool force_nonblock)
}
}
out_free:
req->flags &= ~REQ_F_NEED_CLEANUP;
kfree(iovec);
if (!(req->flags & REQ_F_NEED_CLEANUP))
kfree(iovec);
return ret;
}

Expand Down

0 comments on commit 6f2cc16

Please sign in to comment.