Skip to content

Commit 47cae0c

Browse files
Hao Xuaxboe
authored andcommitted
io-wq: fix IO_WORKER_F_FIXED issue in create_io_worker()
There may be cases like: A B spin_lock(wqe->lock) nr_workers is 0 nr_workers++ spin_unlock(wqe->lock) spin_lock(wqe->lock) nr_wokers is 1 nr_workers++ spin_unlock(wqe->lock) create_io_worker() acct->worker is 1 create_io_worker() acct->worker is 1 There should be one worker marked IO_WORKER_F_FIXED, but no one is. Fix this by introduce a new agrument for create_io_worker() to indicate if it is the first worker. Fixes: 3d4e4fa ("io-wq: fix no lock protection of acct->nr_worker") Signed-off-by: Hao Xu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 49e7f0c commit 47cae0c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

fs/io-wq.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ struct io_cb_cancel_data {
129129
bool cancel_all;
130130
};
131131

132-
static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
132+
static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first);
133133
static void io_wqe_dec_running(struct io_worker *worker);
134134

135135
static bool io_worker_get(struct io_worker *worker)
@@ -248,18 +248,20 @@ static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
248248
rcu_read_unlock();
249249

250250
if (!ret) {
251-
bool do_create = false;
251+
bool do_create = false, first = false;
252252

253253
raw_spin_lock_irq(&wqe->lock);
254254
if (acct->nr_workers < acct->max_workers) {
255255
atomic_inc(&acct->nr_running);
256256
atomic_inc(&wqe->wq->worker_refs);
257+
if (!acct->nr_workers)
258+
first = true;
257259
acct->nr_workers++;
258260
do_create = true;
259261
}
260262
raw_spin_unlock_irq(&wqe->lock);
261263
if (do_create)
262-
create_io_worker(wqe->wq, wqe, acct->index);
264+
create_io_worker(wqe->wq, wqe, acct->index, first);
263265
}
264266
}
265267

@@ -282,20 +284,22 @@ static void create_worker_cb(struct callback_head *cb)
282284
struct io_wq *wq;
283285
struct io_wqe *wqe;
284286
struct io_wqe_acct *acct;
285-
bool do_create = false;
287+
bool do_create = false, first = false;
286288

287289
cwd = container_of(cb, struct create_worker_data, work);
288290
wqe = cwd->wqe;
289291
wq = wqe->wq;
290292
acct = &wqe->acct[cwd->index];
291293
raw_spin_lock_irq(&wqe->lock);
292294
if (acct->nr_workers < acct->max_workers) {
295+
if (!acct->nr_workers)
296+
first = true;
293297
acct->nr_workers++;
294298
do_create = true;
295299
}
296300
raw_spin_unlock_irq(&wqe->lock);
297301
if (do_create) {
298-
create_io_worker(wq, cwd->wqe, cwd->index);
302+
create_io_worker(wq, wqe, cwd->index, first);
299303
} else {
300304
atomic_dec(&acct->nr_running);
301305
io_worker_ref_put(wq);
@@ -637,7 +641,7 @@ void io_wq_worker_sleeping(struct task_struct *tsk)
637641
raw_spin_unlock_irq(&worker->wqe->lock);
638642
}
639643

640-
static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
644+
static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first)
641645
{
642646
struct io_wqe_acct *acct = &wqe->acct[index];
643647
struct io_worker *worker;
@@ -678,7 +682,7 @@ static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
678682
worker->flags |= IO_WORKER_F_FREE;
679683
if (index == IO_WQ_ACCT_BOUND)
680684
worker->flags |= IO_WORKER_F_BOUND;
681-
if ((acct->nr_workers == 1) && (worker->flags & IO_WORKER_F_BOUND))
685+
if (first && (worker->flags & IO_WORKER_F_BOUND))
682686
worker->flags |= IO_WORKER_F_FIXED;
683687
raw_spin_unlock_irq(&wqe->lock);
684688
wake_up_new_task(tsk);

0 commit comments

Comments
 (0)