io-wq: fix no lock protection of acct->nr_worker
authorHao Xu <haoxu@linux.alibaba.com>
Thu, 5 Aug 2021 10:05:37 +0000 (18:05 +0800)
committerJens Axboe <axboe@kernel.dk>
Fri, 6 Aug 2021 14:27:54 +0000 (08:27 -0600)
commit3d4e4face9c1548752a2891e98b38b100feee336
treed244adacc9c6e3f35303953ec242ba2a6cb47a20
parent83d6c39310b6d11199179f6384c2b0a415389597
io-wq: fix no lock protection of acct->nr_worker

There is an acct->nr_worker visit without lock protection. Think about
the case: two callers call io_wqe_wake_worker(), one is the original
context and the other one is an io-worker(by calling
io_wqe_enqueue(wqe, linked)), on two cpus paralelly, this may cause
nr_worker to be larger than max_worker.
Let's fix it by adding lock for it, and let's do nr_workers++ before
create_io_worker. There may be a edge cause that the first caller fails
to create an io-worker, but the second caller doesn't know it and then
quit creating io-worker as well:

say nr_worker = max_worker - 1
        cpu 0                        cpu 1
   io_wqe_wake_worker()          io_wqe_wake_worker()
      nr_worker < max_worker
      nr_worker++
      create_io_worker()         nr_worker == max_worker
         failed                  return
      return

But the chance of this case is very slim.

Fixes: 685fe7feedb9 ("io-wq: eliminate the need for a manager thread")
Signed-off-by: Hao Xu <haoxu@linux.alibaba.com>
[axboe: fix unconditional create_io_worker() call]
Signed-off-by: Jens Axboe <axboe@kernel.dk>
fs/io-wq.c