If the id and user/creds match, return an existing io_wq if we can safely grab a reference to it. Signed-off-by: Jens Axboe <axboe@xxxxxxxxx> --- fs/io-wq.c | 42 +++++++++++++++++++++++++++++++++++++++++- fs/io-wq.h | 3 +++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/fs/io-wq.c b/fs/io-wq.c index 8cb0cff5f15c..a9985856033d 100644 --- a/fs/io-wq.c +++ b/fs/io-wq.c @@ -1024,7 +1024,7 @@ void io_wq_flush(struct io_wq *wq) } } -struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) +static struct io_wq *__io_wq_create(unsigned bounded, struct io_wq_data *data) { int ret = -ENOMEM, node, id; struct io_wq *wq; @@ -1107,6 +1107,41 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) return ERR_PTR(ret); } +struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) +{ + return __io_wq_create(bounded, data); +} + +/* + * Find and return io_wq with given id and grab a reference to it. + */ +struct io_wq *io_wq_create_id(unsigned bounded, struct io_wq_data *data, + unsigned int id) +{ + struct io_wq *wq, *ret = NULL; + + mutex_lock(&wq_lock); + list_for_each_entry(wq, &wq_list, wq_list) { + if (id != wq->id) + continue; + if (data->creds != wq->creds || data->user != wq->user) + continue; + if (data->get_work != wq->get_work || + data->put_work != wq->put_work) + continue; + if (!refcount_inc_not_zero(&wq->use_refs)) + continue; + ret = wq; + break; + } + mutex_unlock(&wq_lock); + + if (!ret) + ret = io_wq_create(bounded, data); + + return ret; +} + static bool io_wq_worker_wake(struct io_worker *worker, void *data) { wake_up_process(worker->task); @@ -1145,3 +1180,8 @@ void io_wq_destroy(struct io_wq *wq) __io_wq_destroy(wq); } } + +unsigned int io_wq_id(struct io_wq *wq) +{ + return wq->id; +} diff --git a/fs/io-wq.h b/fs/io-wq.h index 1cd039af8813..7abe2c56b535 100644 --- a/fs/io-wq.h +++ b/fs/io-wq.h @@ -98,7 +98,10 @@ struct io_wq_data { }; struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data); +struct io_wq *io_wq_create_id(unsigned bounded, struct io_wq_data *data, + unsigned int id); void io_wq_destroy(struct io_wq *wq); +unsigned int io_wq_id(struct io_wq *wq); void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work); void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val); -- 2.25.0