Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20241107110149.890530-2-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Ming Lei and committed by
Jens Axboe
0d98c509 af0a2ffe

+30 -35
+7 -6
io_uring/filetable.c
··· 36 36 return -ENFILE; 37 37 } 38 38 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) 40 41 { 41 42 if (io_rsrc_data_alloc(&table->data, nr_files)) 42 43 return false; 43 44 table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT); 44 45 if (table->bitmap) 45 46 return true; 46 - io_rsrc_data_free(&table->data); 47 + io_rsrc_data_free(ctx, &table->data); 47 48 return false; 48 49 } 49 50 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) 51 52 { 52 - io_rsrc_data_free(&table->data); 53 + io_rsrc_data_free(ctx, &table->data); 53 54 bitmap_free(table->bitmap); 54 55 table->bitmap = NULL; 55 56 } ··· 72 71 if (!node) 73 72 return -ENOMEM; 74 73 75 - if (!io_reset_rsrc_node(&ctx->file_table.data, slot_index)) 74 + if (!io_reset_rsrc_node(ctx, &ctx->file_table.data, slot_index)) 76 75 io_file_bitmap_set(&ctx->file_table, slot_index); 77 76 78 77 ctx->file_table.data.nodes[slot_index] = node; ··· 131 130 node = io_rsrc_node_lookup(&ctx->file_table.data, offset); 132 131 if (!node) 133 132 return -EBADF; 134 - io_reset_rsrc_node(&ctx->file_table.data, offset); 133 + io_reset_rsrc_node(ctx, &ctx->file_table.data, offset); 135 134 io_file_bitmap_clear(&ctx->file_table, offset); 136 135 return 0; 137 136 }
+2 -2
io_uring/filetable.h
··· 6 6 #include <linux/io_uring_types.h> 7 7 #include "rsrc.h" 8 8 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); 11 11 12 12 int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags, 13 13 struct file *file, unsigned int file_slot);
+11 -13
io_uring/rsrc.c
··· 130 130 return node; 131 131 } 132 132 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) 134 134 { 135 135 if (!data->nr) 136 136 return; 137 137 while (data->nr--) { 138 138 if (data->nodes[data->nr]) 139 - io_put_rsrc_node(data->nodes[data->nr]); 139 + io_put_rsrc_node(ctx, data->nodes[data->nr]); 140 140 } 141 141 kvfree(data->nodes); 142 142 data->nodes = NULL; ··· 184 184 continue; 185 185 186 186 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)) 188 188 io_file_bitmap_clear(&ctx->file_table, i); 189 189 190 190 if (fd != -1) { ··· 266 266 node->tag = tag; 267 267 } 268 268 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); 270 270 ctx->buf_table.nodes[i] = node; 271 271 if (ctx->compat) 272 272 user_data += sizeof(struct compat_iovec); ··· 442 442 return IOU_OK; 443 443 } 444 444 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) 446 446 { 447 - struct io_ring_ctx *ctx = io_rsrc_node_ctx(node); 448 - 449 447 lockdep_assert_held(&ctx->uring_lock); 450 448 451 449 if (node->tag) ··· 471 473 if (!ctx->file_table.data.nr) 472 474 return -ENXIO; 473 475 474 - io_free_file_tables(&ctx->file_table); 476 + io_free_file_tables(ctx, &ctx->file_table); 475 477 io_file_table_set_alloc_range(ctx, 0, 0); 476 478 return 0; 477 479 } ··· 492 494 return -EMFILE; 493 495 if (nr_args > rlimit(RLIMIT_NOFILE)) 494 496 return -EMFILE; 495 - if (!io_alloc_file_tables(&ctx->file_table, nr_args)) 497 + if (!io_alloc_file_tables(ctx, &ctx->file_table, nr_args)) 496 498 return -ENOMEM; 497 499 498 500 for (i = 0; i < nr_args; i++) { ··· 549 551 { 550 552 if (!ctx->buf_table.nr) 551 553 return -ENXIO; 552 - io_rsrc_data_free(&ctx->buf_table); 554 + io_rsrc_data_free(ctx, &ctx->buf_table); 553 555 return 0; 554 556 } 555 557 ··· 786 788 if (ret) { 787 789 kvfree(imu); 788 790 if (node) 789 - io_put_rsrc_node(node); 791 + io_put_rsrc_node(ctx, node); 790 792 node = ERR_PTR(ret); 791 793 } 792 794 kvfree(pages); ··· 1016 1018 * old and new nodes at this point. 1017 1019 */ 1018 1020 if (arg->flags & IORING_REGISTER_DST_REPLACE) 1019 - io_rsrc_data_free(&ctx->buf_table); 1021 + io_rsrc_data_free(ctx, &ctx->buf_table); 1020 1022 1021 1023 /* 1022 1024 * ctx->buf_table should be empty now - either the contents are being ··· 1040 1042 kfree(data.nodes[i]); 1041 1043 } 1042 1044 out_unlock: 1043 - io_rsrc_data_free(&data); 1045 + io_rsrc_data_free(ctx, &data); 1044 1046 mutex_unlock(&src_ctx->uring_lock); 1045 1047 mutex_lock(&ctx->uring_lock); 1046 1048 return ret;
+9 -13
io_uring/rsrc.h
··· 45 45 }; 46 46 47 47 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); 50 50 int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr); 51 51 52 52 int io_import_fixed(int ddir, struct iov_iter *iter, ··· 76 76 return NULL; 77 77 } 78 78 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) 80 80 { 81 81 if (node && !--node->refs) 82 - io_free_rsrc_node(node); 82 + io_free_rsrc_node(ctx, node); 83 83 } 84 84 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) 86 87 { 87 88 struct io_rsrc_node *node = data->nodes[index]; 88 89 89 90 if (!node) 90 91 return false; 91 - io_put_rsrc_node(node); 92 + io_put_rsrc_node(ctx, node); 92 93 data->nodes[index] = NULL; 93 94 return true; 94 95 } ··· 97 96 static inline void io_req_put_rsrc_nodes(struct io_kiocb *req) 98 97 { 99 98 if (req->file_node) { 100 - io_put_rsrc_node(req->file_node); 99 + io_put_rsrc_node(req->ctx, req->file_node); 101 100 req->file_node = NULL; 102 101 } 103 102 if (req->flags & REQ_F_BUF_NODE) { 104 - io_put_rsrc_node(req->buf_node); 103 + io_put_rsrc_node(req->ctx, req->buf_node); 105 104 req->buf_node = NULL; 106 105 } 107 - } 108 - 109 - static inline struct io_ring_ctx *io_rsrc_node_ctx(struct io_rsrc_node *node) 110 - { 111 - return (struct io_ring_ctx *) (node->ctx_ptr & ~IORING_RSRC_TYPE_MASK); 112 106 } 113 107 114 108 static inline int io_rsrc_node_type(struct io_rsrc_node *node)
+1 -1
io_uring/splice.c
··· 51 51 { 52 52 struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice); 53 53 54 - io_put_rsrc_node(sp->rsrc_node); 54 + io_put_rsrc_node(req->ctx, sp->rsrc_node); 55 55 } 56 56 57 57 static struct file *io_splice_get_file(struct io_kiocb *req,