Skip to content

Commit

Permalink
io_uring/sqpoll: fix io-wq affinity when IORING_SETUP_SQPOLL is used
Browse files Browse the repository at this point in the history
From: Jens Axboe <axboe@kernel.dk>

[ upstream commit ebdfefc ]

If we setup the ring with SQPOLL, then that polling thread has its
own io-wq setup. This means that if the application uses
IORING_REGISTER_IOWQ_AFF to set the io-wq affinity, we should not be
setting it for the invoking task, but rather the sqpoll task.

Add an sqpoll helper that parks the thread and updates the affinity,
and use that one if we're using SQPOLL.

Fixes: fe76421 ("io_uring: allow user configurable IO thread CPU affinity")
Cc: stable@vger.kernel.org # 5.10+
Link: axboe/liburing#884
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
isilence authored and gregkh committed Sep 19, 2023
1 parent 605d055 commit 9704cfc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
7 changes: 5 additions & 2 deletions io_uring/io-wq.c
Original file line number Diff line number Diff line change
Expand Up @@ -1350,13 +1350,16 @@ static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
return __io_wq_cpu_online(wq, cpu, false);
}

int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
{
int i;

if (!tctx || !tctx->io_wq)
return -EINVAL;

rcu_read_lock();
for_each_node(i) {
struct io_wqe *wqe = wq->wqes[i];
struct io_wqe *wqe = tctx->io_wq->wqes[i];

if (mask)
cpumask_copy(wqe->cpu_mask, mask);
Expand Down
2 changes: 1 addition & 1 deletion io_uring/io-wq.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void io_wq_put_and_exit(struct io_wq *wq);
void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
void io_wq_hash_work(struct io_wq_work *work, void *val);

int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask);
int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask);
int io_wq_max_workers(struct io_wq *wq, int *new_count);
bool io_wq_worker_stopped(void);

Expand Down
29 changes: 18 additions & 11 deletions io_uring/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3835,16 +3835,28 @@ static int io_register_enable_rings(struct io_ring_ctx *ctx)
return 0;
}

static __cold int __io_register_iowq_aff(struct io_ring_ctx *ctx,
cpumask_var_t new_mask)
{
int ret;

if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
ret = io_wq_cpu_affinity(current->io_uring, new_mask);
} else {
mutex_unlock(&ctx->uring_lock);
ret = io_sqpoll_wq_cpu_affinity(ctx, new_mask);
mutex_lock(&ctx->uring_lock);
}

return ret;
}

static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
void __user *arg, unsigned len)
{
struct io_uring_task *tctx = current->io_uring;
cpumask_var_t new_mask;
int ret;

if (!tctx || !tctx->io_wq)
return -EINVAL;

if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
return -ENOMEM;

Expand All @@ -3865,19 +3877,14 @@ static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
return -EFAULT;
}

ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
ret = __io_register_iowq_aff(ctx, new_mask);
free_cpumask_var(new_mask);
return ret;
}

static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
{
struct io_uring_task *tctx = current->io_uring;

if (!tctx || !tctx->io_wq)
return -EINVAL;

return io_wq_cpu_affinity(tctx->io_wq, NULL);
return __io_register_iowq_aff(ctx, NULL);
}

static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
Expand Down
15 changes: 15 additions & 0 deletions io_uring/sqpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,18 @@ __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
io_sq_thread_finish(ctx);
return ret;
}

__cold int io_sqpoll_wq_cpu_affinity(struct io_ring_ctx *ctx,
cpumask_var_t mask)
{
struct io_sq_data *sqd = ctx->sq_data;
int ret = -EINVAL;

if (sqd) {
io_sq_thread_park(sqd);
ret = io_wq_cpu_affinity(sqd->thread->io_uring, mask);
io_sq_thread_unpark(sqd);
}

return ret;
}
1 change: 1 addition & 0 deletions io_uring/sqpoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ void io_sq_thread_park(struct io_sq_data *sqd);
void io_sq_thread_unpark(struct io_sq_data *sqd);
void io_put_sq_data(struct io_sq_data *sqd);
int io_sqpoll_wait_sq(struct io_ring_ctx *ctx);
int io_sqpoll_wq_cpu_affinity(struct io_ring_ctx *ctx, cpumask_var_t mask);

0 comments on commit 9704cfc

Please sign in to comment.