Skip to content

Commit 2933ae6

Browse files
isilenceaxboe
authored andcommitted
io_uring/rsrc: refactor io_rsrc_node_switch
We use io_rsrc_node_switch() coupled with io_rsrc_node_switch_start() for a bunch of cases including initialising ctx->rsrc_node, i.e. by passing NULL instead of rsrc_data. Leave it to only deal with actual node changing. For that, first remove it from io_uring_create() and add a function allocating the first node. Then also remove all calls to io_rsrc_node_switch() from files/buffers register as we already have a node installed and it does essentially nothing. Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://lore.kernel.org/r/d146fe306ff98b1a5a60c997c252534f03d423d7.1681210788.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 13c2239 commit 2933ae6

3 files changed

Lines changed: 20 additions & 28 deletions

File tree

io_uring/io_uring.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,11 +3881,10 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
38813881
ret = io_sq_offload_create(ctx, p);
38823882
if (ret)
38833883
goto err;
3884-
/* always set a rsrc node */
3885-
ret = io_rsrc_node_switch_start(ctx);
3884+
3885+
ret = io_rsrc_init(ctx);
38863886
if (ret)
38873887
goto err;
3888-
io_rsrc_node_switch(ctx, NULL);
38893888

38903889
memset(&p->sq_off, 0, sizeof(p->sq_off));
38913890
p->sq_off.head = offsetof(struct io_rings, sq.head);

io_uring/rsrc.c

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
204204
}
205205
}
206206

207-
static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
207+
struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
208208
{
209209
struct io_rsrc_node *ref_node;
210210
struct io_cache_entry *entry;
@@ -231,23 +231,18 @@ void io_rsrc_node_switch(struct io_ring_ctx *ctx,
231231
struct io_rsrc_data *data_to_kill)
232232
__must_hold(&ctx->uring_lock)
233233
{
234-
WARN_ON_ONCE(io_alloc_cache_empty(&ctx->rsrc_node_cache));
235-
WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
234+
struct io_rsrc_node *node = ctx->rsrc_node;
235+
struct io_rsrc_node *backup = io_rsrc_node_alloc(ctx);
236236

237-
if (data_to_kill) {
238-
struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
239-
240-
rsrc_node->rsrc_data = data_to_kill;
241-
list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
242-
243-
data_to_kill->refs++;
244-
/* put master ref */
245-
io_put_rsrc_node(ctx, rsrc_node);
246-
ctx->rsrc_node = NULL;
247-
}
237+
if (WARN_ON_ONCE(!backup))
238+
return;
248239

249-
if (!ctx->rsrc_node)
250-
ctx->rsrc_node = io_rsrc_node_alloc(ctx);
240+
data_to_kill->refs++;
241+
node->rsrc_data = data_to_kill;
242+
list_add_tail(&node->node, &ctx->rsrc_ref_list);
243+
/* put master ref */
244+
io_put_rsrc_node(ctx, node);
245+
ctx->rsrc_node = backup;
251246
}
252247

253248
int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
@@ -921,9 +916,6 @@ int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
921916
return -EMFILE;
922917
if (nr_args > rlimit(RLIMIT_NOFILE))
923918
return -EMFILE;
924-
ret = io_rsrc_node_switch_start(ctx);
925-
if (ret)
926-
return ret;
927919
ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
928920
&ctx->file_data);
929921
if (ret)
@@ -978,7 +970,6 @@ int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
978970

979971
/* default it to the whole table */
980972
io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
981-
io_rsrc_node_switch(ctx, NULL);
982973
return 0;
983974
fail:
984975
__io_sqe_files_unregister(ctx);
@@ -1260,9 +1251,6 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
12601251
return -EBUSY;
12611252
if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
12621253
return -EINVAL;
1263-
ret = io_rsrc_node_switch_start(ctx);
1264-
if (ret)
1265-
return ret;
12661254
ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
12671255
if (ret)
12681256
return ret;
@@ -1300,8 +1288,6 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
13001288
ctx->buf_data = data;
13011289
if (ret)
13021290
__io_sqe_buffers_unregister(ctx);
1303-
else
1304-
io_rsrc_node_switch(ctx, NULL);
13051291
return ret;
13061292
}
13071293

io_uring/rsrc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ void io_rsrc_put_work(struct work_struct *work);
7474
void io_wait_rsrc_data(struct io_rsrc_data *data);
7575
void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
7676
int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
77+
struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
7778
int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
7879
struct io_rsrc_node *node, void *rsrc);
7980
void io_rsrc_node_switch(struct io_ring_ctx *ctx,
@@ -164,6 +165,12 @@ static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
164165
return &data->tags[table_idx][off];
165166
}
166167

168+
static inline int io_rsrc_init(struct io_ring_ctx *ctx)
169+
{
170+
ctx->rsrc_node = io_rsrc_node_alloc(ctx);
171+
return ctx->rsrc_node ? 0 : -ENOMEM;
172+
}
173+
167174
int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
168175
int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
169176

0 commit comments

Comments
 (0)