Skip to content

Commit 0d98c50

Browse files
Ming Leiaxboe
authored andcommitted
io_uring/rsrc: pass 'struct io_ring_ctx' reference to rsrc helpers
`io_rsrc_node` instance won't be shared among different io_uring ctxs, and its allocation 'ctx' is always same with the user's 'ctx', so it is safe to pass user 'ctx' reference to rsrc helpers. Even in io_clone_buffers(), `io_rsrc_node` instance is allocated actually for destination io_uring_ctx. Then io_rsrc_node_ctx() can be removed, and the 8 bytes `ctx` pointer will be removed from `io_rsrc_node` in the following patch. Signed-off-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent af0a2ff commit 0d98c50

File tree

5 files changed

+30
-35
lines changed

5 files changed

+30
-35
lines changed

io_uring/filetable.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,21 @@ static int io_file_bitmap_get(struct io_ring_ctx *ctx)
3636
return -ENFILE;
3737
}
3838

39-
bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
39+
bool io_alloc_file_tables(struct io_ring_ctx *ctx, struct io_file_table *table,
40+
unsigned nr_files)
4041
{
4142
if (io_rsrc_data_alloc(&table->data, nr_files))
4243
return false;
4344
table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
4445
if (table->bitmap)
4546
return true;
46-
io_rsrc_data_free(&table->data);
47+
io_rsrc_data_free(ctx, &table->data);
4748
return false;
4849
}
4950

50-
void io_free_file_tables(struct io_file_table *table)
51+
void io_free_file_tables(struct io_ring_ctx *ctx, struct io_file_table *table)
5152
{
52-
io_rsrc_data_free(&table->data);
53+
io_rsrc_data_free(ctx, &table->data);
5354
bitmap_free(table->bitmap);
5455
table->bitmap = NULL;
5556
}
@@ -71,7 +72,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
7172
if (!node)
7273
return -ENOMEM;
7374

74-
if (!io_reset_rsrc_node(&ctx->file_table.data, slot_index))
75+
if (!io_reset_rsrc_node(ctx, &ctx->file_table.data, slot_index))
7576
io_file_bitmap_set(&ctx->file_table, slot_index);
7677

7778
ctx->file_table.data.nodes[slot_index] = node;
@@ -130,7 +131,7 @@ int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
130131
node = io_rsrc_node_lookup(&ctx->file_table.data, offset);
131132
if (!node)
132133
return -EBADF;
133-
io_reset_rsrc_node(&ctx->file_table.data, offset);
134+
io_reset_rsrc_node(ctx, &ctx->file_table.data, offset);
134135
io_file_bitmap_clear(&ctx->file_table, offset);
135136
return 0;
136137
}

io_uring/filetable.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include <linux/io_uring_types.h>
77
#include "rsrc.h"
88

9-
bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);
10-
void io_free_file_tables(struct io_file_table *table);
9+
bool io_alloc_file_tables(struct io_ring_ctx *ctx, struct io_file_table *table, unsigned nr_files);
10+
void io_free_file_tables(struct io_ring_ctx *ctx, struct io_file_table *table);
1111

1212
int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
1313
struct file *file, unsigned int file_slot);

io_uring/rsrc.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type)
130130
return node;
131131
}
132132

133-
__cold void io_rsrc_data_free(struct io_rsrc_data *data)
133+
__cold void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *data)
134134
{
135135
if (!data->nr)
136136
return;
137137
while (data->nr--) {
138138
if (data->nodes[data->nr])
139-
io_put_rsrc_node(data->nodes[data->nr]);
139+
io_put_rsrc_node(ctx, data->nodes[data->nr]);
140140
}
141141
kvfree(data->nodes);
142142
data->nodes = NULL;
@@ -184,7 +184,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
184184
continue;
185185

186186
i = up->offset + done;
187-
if (io_reset_rsrc_node(&ctx->file_table.data, i))
187+
if (io_reset_rsrc_node(ctx, &ctx->file_table.data, i))
188188
io_file_bitmap_clear(&ctx->file_table, i);
189189

190190
if (fd != -1) {
@@ -266,7 +266,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
266266
node->tag = tag;
267267
}
268268
i = array_index_nospec(up->offset + done, ctx->buf_table.nr);
269-
io_reset_rsrc_node(&ctx->buf_table, i);
269+
io_reset_rsrc_node(ctx, &ctx->buf_table, i);
270270
ctx->buf_table.nodes[i] = node;
271271
if (ctx->compat)
272272
user_data += sizeof(struct compat_iovec);
@@ -442,10 +442,8 @@ int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
442442
return IOU_OK;
443443
}
444444

445-
void io_free_rsrc_node(struct io_rsrc_node *node)
445+
void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
446446
{
447-
struct io_ring_ctx *ctx = io_rsrc_node_ctx(node);
448-
449447
lockdep_assert_held(&ctx->uring_lock);
450448

451449
if (node->tag)
@@ -473,7 +471,7 @@ int io_sqe_files_unregister(struct io_ring_ctx *ctx)
473471
if (!ctx->file_table.data.nr)
474472
return -ENXIO;
475473

476-
io_free_file_tables(&ctx->file_table);
474+
io_free_file_tables(ctx, &ctx->file_table);
477475
io_file_table_set_alloc_range(ctx, 0, 0);
478476
return 0;
479477
}
@@ -494,7 +492,7 @@ int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
494492
return -EMFILE;
495493
if (nr_args > rlimit(RLIMIT_NOFILE))
496494
return -EMFILE;
497-
if (!io_alloc_file_tables(&ctx->file_table, nr_args))
495+
if (!io_alloc_file_tables(ctx, &ctx->file_table, nr_args))
498496
return -ENOMEM;
499497

500498
for (i = 0; i < nr_args; i++) {
@@ -551,7 +549,7 @@ int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
551549
{
552550
if (!ctx->buf_table.nr)
553551
return -ENXIO;
554-
io_rsrc_data_free(&ctx->buf_table);
552+
io_rsrc_data_free(ctx, &ctx->buf_table);
555553
return 0;
556554
}
557555

@@ -788,7 +786,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,
788786
if (ret) {
789787
kvfree(imu);
790788
if (node)
791-
io_put_rsrc_node(node);
789+
io_put_rsrc_node(ctx, node);
792790
node = ERR_PTR(ret);
793791
}
794792
kvfree(pages);
@@ -1018,7 +1016,7 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx
10181016
* old and new nodes at this point.
10191017
*/
10201018
if (arg->flags & IORING_REGISTER_DST_REPLACE)
1021-
io_rsrc_data_free(&ctx->buf_table);
1019+
io_rsrc_data_free(ctx, &ctx->buf_table);
10221020

10231021
/*
10241022
* ctx->buf_table should be empty now - either the contents are being
@@ -1042,7 +1040,7 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx
10421040
kfree(data.nodes[i]);
10431041
}
10441042
out_unlock:
1045-
io_rsrc_data_free(&data);
1043+
io_rsrc_data_free(ctx, &data);
10461044
mutex_unlock(&src_ctx->uring_lock);
10471045
mutex_lock(&ctx->uring_lock);
10481046
return ret;

io_uring/rsrc.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ struct io_imu_folio_data {
4545
};
4646

4747
struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type);
48-
void io_free_rsrc_node(struct io_rsrc_node *node);
49-
void io_rsrc_data_free(struct io_rsrc_data *data);
48+
void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node);
49+
void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *data);
5050
int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr);
5151

5252
int io_import_fixed(int ddir, struct iov_iter *iter,
@@ -76,40 +76,36 @@ static inline struct io_rsrc_node *io_rsrc_node_lookup(struct io_rsrc_data *data
7676
return NULL;
7777
}
7878

79-
static inline void io_put_rsrc_node(struct io_rsrc_node *node)
79+
static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
8080
{
8181
if (node && !--node->refs)
82-
io_free_rsrc_node(node);
82+
io_free_rsrc_node(ctx, node);
8383
}
8484

85-
static inline bool io_reset_rsrc_node(struct io_rsrc_data *data, int index)
85+
static inline bool io_reset_rsrc_node(struct io_ring_ctx *ctx,
86+
struct io_rsrc_data *data, int index)
8687
{
8788
struct io_rsrc_node *node = data->nodes[index];
8889

8990
if (!node)
9091
return false;
91-
io_put_rsrc_node(node);
92+
io_put_rsrc_node(ctx, node);
9293
data->nodes[index] = NULL;
9394
return true;
9495
}
9596

9697
static inline void io_req_put_rsrc_nodes(struct io_kiocb *req)
9798
{
9899
if (req->file_node) {
99-
io_put_rsrc_node(req->file_node);
100+
io_put_rsrc_node(req->ctx, req->file_node);
100101
req->file_node = NULL;
101102
}
102103
if (req->flags & REQ_F_BUF_NODE) {
103-
io_put_rsrc_node(req->buf_node);
104+
io_put_rsrc_node(req->ctx, req->buf_node);
104105
req->buf_node = NULL;
105106
}
106107
}
107108

108-
static inline struct io_ring_ctx *io_rsrc_node_ctx(struct io_rsrc_node *node)
109-
{
110-
return (struct io_ring_ctx *) (node->ctx_ptr & ~IORING_RSRC_TYPE_MASK);
111-
}
112-
113109
static inline int io_rsrc_node_type(struct io_rsrc_node *node)
114110
{
115111
return node->ctx_ptr & IORING_RSRC_TYPE_MASK;

io_uring/splice.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void io_splice_cleanup(struct io_kiocb *req)
5151
{
5252
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
5353

54-
io_put_rsrc_node(sp->rsrc_node);
54+
io_put_rsrc_node(req->ctx, sp->rsrc_node);
5555
}
5656

5757
static struct file *io_splice_get_file(struct io_kiocb *req,

0 commit comments

Comments
 (0)