Skip to content

Commit c605c39

Browse files
committed
Merge tag 'io_uring-5.15-2021-09-11' of git://git.kernel.dk/linux-block
Pull io_uring fixes from Jens Axboe: - Fix an off-by-one in a BUILD_BUG_ON() check. Not a real issue right now as we have plenty of flags left, but could become one. (Hao) - Fix lockdep issue introduced in this merge window (me) - Fix a few issues with the worker creation (me, Pavel, Qiang) - Fix regression with wq_has_sleeper() for IOPOLL (Pavel) - Timeout link error propagation fix (Pavel) * tag 'io_uring-5.15-2021-09-11' of git://git.kernel.dk/linux-block: io_uring: fix off-by-one in BUILD_BUG_ON check of __REQ_F_LAST_BIT io_uring: fail links of cancelled timeouts io-wq: fix memory leak in create_io_worker() io-wq: fix silly logic error in io_task_work_match() io_uring: drop ctx->uring_lock before acquiring sqd->lock io_uring: fix missing mb() before waitqueue_active io-wq: fix cancellation on create-worker failure
2 parents c0f7e49 + 32c2d33 commit c605c39

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

fs/io-wq.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ static void create_worker_cont(struct callback_head *cb)
709709
}
710710
raw_spin_unlock(&wqe->lock);
711711
io_worker_ref_put(wqe->wq);
712+
kfree(worker);
712713
return;
713714
}
714715

@@ -725,6 +726,7 @@ static void io_workqueue_create(struct work_struct *work)
725726
if (!io_queue_worker_create(worker, acct, create_worker_cont)) {
726727
clear_bit_unlock(0, &worker->create_state);
727728
io_worker_release(worker);
729+
kfree(worker);
728730
}
729731
}
730732

@@ -759,6 +761,7 @@ static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
759761
if (!IS_ERR(tsk)) {
760762
io_init_new_worker(wqe, worker, tsk);
761763
} else if (!io_should_retry_thread(PTR_ERR(tsk))) {
764+
kfree(worker);
762765
goto fail;
763766
} else {
764767
INIT_WORK(&worker->work, io_workqueue_create);
@@ -832,6 +835,11 @@ static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
832835
wq_list_add_after(&work->list, &tail->list, &acct->work_list);
833836
}
834837

838+
static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
839+
{
840+
return work == data;
841+
}
842+
835843
static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
836844
{
837845
struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
@@ -844,7 +852,6 @@ static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
844852
*/
845853
if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
846854
(work->flags & IO_WQ_WORK_CANCEL)) {
847-
run_cancel:
848855
io_run_cancel(work, wqe);
849856
return;
850857
}
@@ -864,15 +871,22 @@ static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
864871
bool did_create;
865872

866873
did_create = io_wqe_create_worker(wqe, acct);
867-
if (unlikely(!did_create)) {
868-
raw_spin_lock(&wqe->lock);
869-
/* fatal condition, failed to create the first worker */
870-
if (!acct->nr_workers) {
871-
raw_spin_unlock(&wqe->lock);
872-
goto run_cancel;
873-
}
874-
raw_spin_unlock(&wqe->lock);
874+
if (likely(did_create))
875+
return;
876+
877+
raw_spin_lock(&wqe->lock);
878+
/* fatal condition, failed to create the first worker */
879+
if (!acct->nr_workers) {
880+
struct io_cb_cancel_data match = {
881+
.fn = io_wq_work_match_item,
882+
.data = work,
883+
.cancel_all = false,
884+
};
885+
886+
if (io_acct_cancel_pending_work(wqe, acct, &match))
887+
raw_spin_lock(&wqe->lock);
875888
}
889+
raw_spin_unlock(&wqe->lock);
876890
}
877891
}
878892

@@ -1122,7 +1136,7 @@ static bool io_task_work_match(struct callback_head *cb, void *data)
11221136
{
11231137
struct io_worker *worker;
11241138

1125-
if (cb->func != create_worker_cb || cb->func != create_worker_cont)
1139+
if (cb->func != create_worker_cb && cb->func != create_worker_cont)
11261140
return false;
11271141
worker = container_of(cb, struct io_worker, create_work);
11281142
return worker->wqe->wq == data;
@@ -1143,9 +1157,14 @@ static void io_wq_exit_workers(struct io_wq *wq)
11431157

11441158
while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
11451159
struct io_worker *worker;
1160+
struct io_wqe_acct *acct;
11461161

11471162
worker = container_of(cb, struct io_worker, create_work);
1148-
atomic_dec(&worker->wqe->acct[worker->create_index].nr_running);
1163+
acct = io_wqe_get_acct(worker);
1164+
atomic_dec(&acct->nr_running);
1165+
raw_spin_lock(&worker->wqe->lock);
1166+
acct->nr_workers--;
1167+
raw_spin_unlock(&worker->wqe->lock);
11491168
io_worker_ref_put(wq);
11501169
clear_bit_unlock(0, &worker->create_state);
11511170
io_worker_release(worker);

fs/io_uring.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,8 @@ static void io_kill_timeout(struct io_kiocb *req, int status)
14821482
struct io_timeout_data *io = req->async_data;
14831483

14841484
if (hrtimer_try_to_cancel(&io->timer) != -1) {
1485+
if (status)
1486+
req_set_fail(req);
14851487
atomic_set(&req->ctx->cq_timeouts,
14861488
atomic_read(&req->ctx->cq_timeouts) + 1);
14871489
list_del_init(&req->timeout.list);
@@ -1619,8 +1621,11 @@ static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
16191621

16201622
static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
16211623
{
1624+
/* see waitqueue_active() comment */
1625+
smp_mb();
1626+
16221627
if (ctx->flags & IORING_SETUP_SQPOLL) {
1623-
if (wq_has_sleeper(&ctx->cq_wait))
1628+
if (waitqueue_active(&ctx->cq_wait))
16241629
wake_up_all(&ctx->cq_wait);
16251630
}
16261631
if (io_should_trigger_evfd(ctx))
@@ -10550,7 +10555,14 @@ static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
1055010555
if (ctx->flags & IORING_SETUP_SQPOLL) {
1055110556
sqd = ctx->sq_data;
1055210557
if (sqd) {
10558+
/*
10559+
* Observe the correct sqd->lock -> ctx->uring_lock
10560+
* ordering. Fine to drop uring_lock here, we hold
10561+
* a ref to the ctx.
10562+
*/
10563+
mutex_unlock(&ctx->uring_lock);
1055310564
mutex_lock(&sqd->lock);
10565+
mutex_lock(&ctx->uring_lock);
1055410566
tctx = sqd->thread->io_uring;
1055510567
}
1055610568
} else {
@@ -10853,7 +10865,7 @@ static int __init io_uring_init(void)
1085310865
BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
1085410866

1085510867
BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
10856-
BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
10868+
BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
1085710869

1085810870
req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
1085910871
SLAB_ACCOUNT);

0 commit comments

Comments
 (0)