Skip to content

Commit 9704cfc

Browse files
isilencegregkh
authored andcommitted
io_uring/sqpoll: fix io-wq affinity when IORING_SETUP_SQPOLL is used
From: Jens Axboe <[email protected]> [ 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: [email protected] # 5.10+ Link: axboe/liburing#884 Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Pavel Begunkov <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 605d055 commit 9704cfc

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

io_uring/io-wq.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,13 +1350,16 @@ static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
13501350
return __io_wq_cpu_online(wq, cpu, false);
13511351
}
13521352

1353-
int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1353+
int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
13541354
{
13551355
int i;
13561356

1357+
if (!tctx || !tctx->io_wq)
1358+
return -EINVAL;
1359+
13571360
rcu_read_lock();
13581361
for_each_node(i) {
1359-
struct io_wqe *wqe = wq->wqes[i];
1362+
struct io_wqe *wqe = tctx->io_wq->wqes[i];
13601363

13611364
if (mask)
13621365
cpumask_copy(wqe->cpu_mask, mask);

io_uring/io-wq.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void io_wq_put_and_exit(struct io_wq *wq);
5050
void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
5151
void io_wq_hash_work(struct io_wq_work *work, void *val);
5252

53-
int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask);
53+
int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask);
5454
int io_wq_max_workers(struct io_wq *wq, int *new_count);
5555
bool io_wq_worker_stopped(void);
5656

io_uring/io_uring.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3835,16 +3835,28 @@ static int io_register_enable_rings(struct io_ring_ctx *ctx)
38353835
return 0;
38363836
}
38373837

3838+
static __cold int __io_register_iowq_aff(struct io_ring_ctx *ctx,
3839+
cpumask_var_t new_mask)
3840+
{
3841+
int ret;
3842+
3843+
if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
3844+
ret = io_wq_cpu_affinity(current->io_uring, new_mask);
3845+
} else {
3846+
mutex_unlock(&ctx->uring_lock);
3847+
ret = io_sqpoll_wq_cpu_affinity(ctx, new_mask);
3848+
mutex_lock(&ctx->uring_lock);
3849+
}
3850+
3851+
return ret;
3852+
}
3853+
38383854
static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
38393855
void __user *arg, unsigned len)
38403856
{
3841-
struct io_uring_task *tctx = current->io_uring;
38423857
cpumask_var_t new_mask;
38433858
int ret;
38443859

3845-
if (!tctx || !tctx->io_wq)
3846-
return -EINVAL;
3847-
38483860
if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
38493861
return -ENOMEM;
38503862

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

3868-
ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
3880+
ret = __io_register_iowq_aff(ctx, new_mask);
38693881
free_cpumask_var(new_mask);
38703882
return ret;
38713883
}
38723884

38733885
static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
38743886
{
3875-
struct io_uring_task *tctx = current->io_uring;
3876-
3877-
if (!tctx || !tctx->io_wq)
3878-
return -EINVAL;
3879-
3880-
return io_wq_cpu_affinity(tctx->io_wq, NULL);
3887+
return __io_register_iowq_aff(ctx, NULL);
38813888
}
38823889

38833890
static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,

io_uring/sqpoll.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,18 @@ __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
423423
io_sq_thread_finish(ctx);
424424
return ret;
425425
}
426+
427+
__cold int io_sqpoll_wq_cpu_affinity(struct io_ring_ctx *ctx,
428+
cpumask_var_t mask)
429+
{
430+
struct io_sq_data *sqd = ctx->sq_data;
431+
int ret = -EINVAL;
432+
433+
if (sqd) {
434+
io_sq_thread_park(sqd);
435+
ret = io_wq_cpu_affinity(sqd->thread->io_uring, mask);
436+
io_sq_thread_unpark(sqd);
437+
}
438+
439+
return ret;
440+
}

io_uring/sqpoll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ void io_sq_thread_park(struct io_sq_data *sqd);
2727
void io_sq_thread_unpark(struct io_sq_data *sqd);
2828
void io_put_sq_data(struct io_sq_data *sqd);
2929
int io_sqpoll_wait_sq(struct io_ring_ctx *ctx);
30+
int io_sqpoll_wq_cpu_affinity(struct io_ring_ctx *ctx, cpumask_var_t mask);

0 commit comments

Comments
 (0)