Skip to content

Commit 2c453a4

Browse files
committed
io_uring/tctx: have io_uring_alloc_task_context() return tctx
Instead of having io_uring_alloc_task_context() return an int and assign tsk->io_uring, just have it return the task context directly. This enables cleaner error handling in callers, which may have failure points post calling io_uring_alloc_task_context(). Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent f847bf6 commit 2c453a4

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

io_uring/sqpoll.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
458458
return -EINVAL;
459459
}
460460
if (ctx->flags & IORING_SETUP_SQPOLL) {
461+
struct io_uring_task *tctx;
461462
struct task_struct *tsk;
462463
struct io_sq_data *sqd;
463464
bool attached;
@@ -524,8 +525,13 @@ __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
524525
rcu_assign_pointer(sqd->thread, tsk);
525526
mutex_unlock(&sqd->lock);
526527

528+
ret = 0;
527529
get_task_struct(tsk);
528-
ret = io_uring_alloc_task_context(tsk, ctx);
530+
tctx = io_uring_alloc_task_context(tsk, ctx);
531+
if (!IS_ERR(tctx))
532+
tsk->io_uring = tctx;
533+
else
534+
ret = PTR_ERR(tctx);
529535
wake_up_new_task(tsk);
530536
if (ret)
531537
goto err;

io_uring/tctx.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,39 +74,38 @@ void __io_uring_free(struct task_struct *tsk)
7474
}
7575
}
7676

77-
__cold int io_uring_alloc_task_context(struct task_struct *task,
78-
struct io_ring_ctx *ctx)
77+
__cold struct io_uring_task *io_uring_alloc_task_context(struct task_struct *task,
78+
struct io_ring_ctx *ctx)
7979
{
8080
struct io_uring_task *tctx;
8181
int ret;
8282

8383
tctx = kzalloc_obj(*tctx);
8484
if (unlikely(!tctx))
85-
return -ENOMEM;
85+
return ERR_PTR(-ENOMEM);
8686

8787
ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8888
if (unlikely(ret)) {
8989
kfree(tctx);
90-
return ret;
90+
return ERR_PTR(ret);
9191
}
9292

9393
tctx->io_wq = io_init_wq_offload(ctx, task);
9494
if (IS_ERR(tctx->io_wq)) {
9595
ret = PTR_ERR(tctx->io_wq);
9696
percpu_counter_destroy(&tctx->inflight);
9797
kfree(tctx);
98-
return ret;
98+
return ERR_PTR(ret);
9999
}
100100

101101
tctx->task = task;
102102
xa_init(&tctx->xa);
103103
init_waitqueue_head(&tctx->wait);
104104
atomic_set(&tctx->in_cancel, 0);
105105
atomic_set(&tctx->inflight_tracked, 0);
106-
task->io_uring = tctx;
107106
init_llist_head(&tctx->task_list);
108107
init_task_work(&tctx->task_work, tctx_task_work);
109-
return 0;
108+
return tctx;
110109
}
111110

112111
int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
@@ -116,11 +115,11 @@ int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
116115
int ret;
117116

118117
if (unlikely(!tctx)) {
119-
ret = io_uring_alloc_task_context(current, ctx);
120-
if (unlikely(ret))
121-
return ret;
118+
tctx = io_uring_alloc_task_context(current, ctx);
119+
if (IS_ERR(tctx))
120+
return PTR_ERR(tctx);
122121

123-
tctx = current->io_uring;
122+
current->io_uring = tctx;
124123
if (ctx->int_flags & IO_RING_F_IOWQ_LIMITS_SET) {
125124
unsigned int limits[2] = { ctx->iowq_limits[0],
126125
ctx->iowq_limits[1], };

io_uring/tctx.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ struct io_tctx_node {
66
struct io_ring_ctx *ctx;
77
};
88

9-
int io_uring_alloc_task_context(struct task_struct *task,
10-
struct io_ring_ctx *ctx);
9+
struct io_uring_task *io_uring_alloc_task_context(struct task_struct *task,
10+
struct io_ring_ctx *ctx);
1111
void io_uring_del_tctx_node(unsigned long index);
1212
int __io_uring_add_tctx_node(struct io_ring_ctx *ctx);
1313
int __io_uring_add_tctx_node_from_submit(struct io_ring_ctx *ctx);

0 commit comments

Comments
 (0)