Skip to content

Commit 6af3f48

Browse files
isilenceaxboe
authored andcommitted
io_uring: fix link traversal locking
WARNING: inconsistent lock state 5.16.0-rc2-syzkaller #0 Not tainted inconsistent {HARDIRQ-ON-W} -> {IN-HARDIRQ-W} usage. ffff888078e11418 (&ctx->timeout_lock ){?.+.}-{2:2} , at: io_timeout_fn+0x6f/0x360 fs/io_uring.c:5943 {HARDIRQ-ON-W} state was registered at: [...] spin_unlock_irq include/linux/spinlock.h:399 [inline] __io_poll_remove_one fs/io_uring.c:5669 [inline] __io_poll_remove_one fs/io_uring.c:5654 [inline] io_poll_remove_one+0x236/0x870 fs/io_uring.c:5680 io_poll_remove_all+0x1af/0x235 fs/io_uring.c:5709 io_ring_ctx_wait_and_kill+0x1cc/0x322 fs/io_uring.c:9534 io_uring_release+0x42/0x46 fs/io_uring.c:9554 __fput+0x286/0x9f0 fs/file_table.c:280 task_work_run+0xdd/0x1a0 kernel/task_work.c:164 exit_task_work include/linux/task_work.h:32 [inline] do_exit+0xc14/0x2b40 kernel/exit.c:832 674ee8e ("io_uring: correct link-list traversal locking") fixed a data race but introduced a possible deadlock and inconsistentcy in irq states. E.g. io_poll_remove_all() spin_lock_irq(timeout_lock) io_poll_remove_one() spin_lock/unlock_irq(poll_lock); spin_unlock_irq(timeout_lock) Another type of problem is freeing a request while holding ->timeout_lock, which may leads to a deadlock in io_commit_cqring() -> io_flush_timeouts() and other places. Having 3 nested locks is also too ugly. Add io_match_task_safe(), which would briefly take and release timeout_lock for race prevention inside, so the actuall request cancellation / free / etc. code doesn't have it taken. Reported-by: [email protected] Reported-by: [email protected] Reported-by: [email protected] Reported-by: [email protected] Reported-by: [email protected] Fixes: 674ee8e ("io_uring: correct link-list traversal locking") Cc: [email protected] # 5.15+ Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/397f7ebf3f4171f1abe41f708ac1ecb5766f0b68.1637937097.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent 617a894 commit 6af3f48

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

fs/io_uring.c

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,7 @@ static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
12781278

12791279
static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
12801280
bool cancel_all)
1281+
__must_hold(&req->ctx->timeout_lock)
12811282
{
12821283
struct io_kiocb *req;
12831284

@@ -1293,6 +1294,44 @@ static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
12931294
return false;
12941295
}
12951296

1297+
static bool io_match_linked(struct io_kiocb *head)
1298+
{
1299+
struct io_kiocb *req;
1300+
1301+
io_for_each_link(req, head) {
1302+
if (req->flags & REQ_F_INFLIGHT)
1303+
return true;
1304+
}
1305+
return false;
1306+
}
1307+
1308+
/*
1309+
* As io_match_task() but protected against racing with linked timeouts.
1310+
* User must not hold timeout_lock.
1311+
*/
1312+
static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
1313+
bool cancel_all)
1314+
{
1315+
bool matched;
1316+
1317+
if (task && head->task != task)
1318+
return false;
1319+
if (cancel_all)
1320+
return true;
1321+
1322+
if (head->flags & REQ_F_LINK_TIMEOUT) {
1323+
struct io_ring_ctx *ctx = head->ctx;
1324+
1325+
/* protect against races with linked timeouts */
1326+
spin_lock_irq(&ctx->timeout_lock);
1327+
matched = io_match_linked(head);
1328+
spin_unlock_irq(&ctx->timeout_lock);
1329+
} else {
1330+
matched = io_match_linked(head);
1331+
}
1332+
return matched;
1333+
}
1334+
12961335
static inline bool req_has_async_data(struct io_kiocb *req)
12971336
{
12981337
return req->flags & REQ_F_ASYNC_DATA;
@@ -5699,17 +5738,15 @@ static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx,
56995738
int posted = 0, i;
57005739

57015740
spin_lock(&ctx->completion_lock);
5702-
spin_lock_irq(&ctx->timeout_lock);
57035741
for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
57045742
struct hlist_head *list;
57055743

57065744
list = &ctx->cancel_hash[i];
57075745
hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5708-
if (io_match_task(req, tsk, cancel_all))
5746+
if (io_match_task_safe(req, tsk, cancel_all))
57095747
posted += io_poll_remove_one(req);
57105748
}
57115749
}
5712-
spin_unlock_irq(&ctx->timeout_lock);
57135750
spin_unlock(&ctx->completion_lock);
57145751

57155752
if (posted)
@@ -9565,19 +9602,8 @@ static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
95659602
{
95669603
struct io_kiocb *req = container_of(work, struct io_kiocb, work);
95679604
struct io_task_cancel *cancel = data;
9568-
bool ret;
95699605

9570-
if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
9571-
struct io_ring_ctx *ctx = req->ctx;
9572-
9573-
/* protect against races with linked timeouts */
9574-
spin_lock_irq(&ctx->timeout_lock);
9575-
ret = io_match_task(req, cancel->task, cancel->all);
9576-
spin_unlock_irq(&ctx->timeout_lock);
9577-
} else {
9578-
ret = io_match_task(req, cancel->task, cancel->all);
9579-
}
9580-
return ret;
9606+
return io_match_task_safe(req, cancel->task, cancel->all);
95819607
}
95829608

95839609
static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
@@ -9588,14 +9614,12 @@ static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
95889614
LIST_HEAD(list);
95899615

95909616
spin_lock(&ctx->completion_lock);
9591-
spin_lock_irq(&ctx->timeout_lock);
95929617
list_for_each_entry_reverse(de, &ctx->defer_list, list) {
9593-
if (io_match_task(de->req, task, cancel_all)) {
9618+
if (io_match_task_safe(de->req, task, cancel_all)) {
95949619
list_cut_position(&list, &ctx->defer_list, &de->list);
95959620
break;
95969621
}
95979622
}
9598-
spin_unlock_irq(&ctx->timeout_lock);
95999623
spin_unlock(&ctx->completion_lock);
96009624
if (list_empty(&list))
96019625
return false;

0 commit comments

Comments
 (0)