Skip to content

Commit 8197b05

Browse files
isilenceaxboe
authored andcommitted
io_uring: fix put_kbuf without proper locking
io_put_kbuf_comp() should only be called while holding ->completion_lock, however there is no such assumption in io_clean_op() and thus it can corrupt ->io_buffer_comp. Take the lock there, and workaround the only user of io_clean_op() calling it with locks. Not the prettiest solution, but it's easier to refactor it for-next. Fixes: cc3cec8 ("io_uring: speedup provided buffer handling") Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://lore.kernel.org/r/743e2130b73ec6d48c4c5dd15db896c433431e6d.1648212967.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent ab0ac09 commit 8197b05

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

fs/io_uring.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,8 @@ static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list)
13381338

13391339
static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req)
13401340
{
1341+
lockdep_assert_held(&req->ctx->completion_lock);
1342+
13411343
if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
13421344
return 0;
13431345
return __io_put_kbuf(req, &req->ctx->io_buffers_comp);
@@ -2123,6 +2125,12 @@ static void __io_req_complete_post(struct io_kiocb *req, s32 res,
21232125
}
21242126
}
21252127
io_req_put_rsrc(req, ctx);
2128+
/*
2129+
* Selected buffer deallocation in io_clean_op() assumes that
2130+
* we don't hold ->completion_lock. Clean them here to avoid
2131+
* deadlocks.
2132+
*/
2133+
io_put_kbuf_comp(req);
21262134
io_dismantle_req(req);
21272135
io_put_task(req->task, 1);
21282136
wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
@@ -7126,8 +7134,11 @@ static __cold void io_drain_req(struct io_kiocb *req)
71267134

71277135
static void io_clean_op(struct io_kiocb *req)
71287136
{
7129-
if (req->flags & REQ_F_BUFFER_SELECTED)
7137+
if (req->flags & REQ_F_BUFFER_SELECTED) {
7138+
spin_lock(&req->ctx->completion_lock);
71307139
io_put_kbuf_comp(req);
7140+
spin_unlock(&req->ctx->completion_lock);
7141+
}
71317142

71327143
if (req->flags & REQ_F_NEED_CLEANUP) {
71337144
switch (req->opcode) {

0 commit comments

Comments
 (0)