Skip to content

Commit eb2de94

Browse files
committed
io-wq: fix race around io_worker grabbing
There's a small window between lookup dropping the reference to the worker and calling wake_up_process() on the worker task, where the worker itself could have exited. We ensure that the worker struct itself is valid, but worker->task may very well be gone by the time we issue the wakeup. Fix the race by using a completion triggered by the reference going to zero, and having exit wait for that completion before proceeding. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 8b3e78b commit eb2de94

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

fs/io-wq.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ struct io_worker {
5656
const struct cred *cur_creds;
5757
const struct cred *saved_creds;
5858

59+
struct completion ref_done;
60+
5961
struct rcu_head rcu;
6062
};
6163

@@ -129,7 +131,7 @@ static bool io_worker_get(struct io_worker *worker)
129131
static void io_worker_release(struct io_worker *worker)
130132
{
131133
if (refcount_dec_and_test(&worker->ref))
132-
wake_up_process(worker->task);
134+
complete(&worker->ref_done);
133135
}
134136

135137
static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
@@ -157,14 +159,9 @@ static void io_worker_exit(struct io_worker *worker)
157159
struct io_wqe_acct *acct = io_wqe_get_acct(worker);
158160
unsigned flags;
159161

160-
/*
161-
* If we're not at zero, someone else is holding a brief reference
162-
* to the worker. Wait for that to go away.
163-
*/
164-
set_current_state(TASK_INTERRUPTIBLE);
165-
if (!refcount_dec_and_test(&worker->ref))
166-
schedule();
167-
__set_current_state(TASK_RUNNING);
162+
if (refcount_dec_and_test(&worker->ref))
163+
complete(&worker->ref_done);
164+
wait_for_completion(&worker->ref_done);
168165

169166
preempt_disable();
170167
current->flags &= ~PF_IO_WORKER;
@@ -615,6 +612,7 @@ static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
615612
worker->nulls_node.pprev = NULL;
616613
worker->wqe = wqe;
617614
spin_lock_init(&worker->lock);
615+
init_completion(&worker->ref_done);
618616

619617
refcount_inc(&wq->refs);
620618

@@ -724,6 +722,7 @@ static int io_wq_manager(void *data)
724722
io_wq_check_workers(wq);
725723

726724
if (refcount_dec_and_test(&wq->refs)) {
725+
wq->manager = NULL;
727726
complete(&wq->done);
728727
do_exit(0);
729728
}
@@ -734,6 +733,7 @@ static int io_wq_manager(void *data)
734733
io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
735734
rcu_read_unlock();
736735
}
736+
wq->manager = NULL;
737737
do_exit(0);
738738
}
739739

0 commit comments

Comments
 (0)