io_uring/rsrc: unify file and buffer resource tables

For files, there's nr_user_files/file_table/file_data, and buffers have
nr_user_bufs/user_bufs/buf_data. There's no reason why file_table and
file_data can't be the same thing, and ditto for the buffer side. That
gets rid of more io_ring_ctx state that's in two spots rather than just
being in one spot, as it should be. Put all the registered file data in
one locations, and ditto on the buffer front.

This also avoids having both io_rsrc_data->nodes being an allocated
array, and ->user_bufs[] or ->file_table.nodes. There's no reason to
have this information duplicated. Keep it in one spot, io_rsrc_data,
along with how many resources are available.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
Jens Axboe
2024-10-26 14:50:13 -06:00
parent f38f284764
commit 3597f2786b
15 changed files with 123 additions and 212 deletions

View File

@@ -38,25 +38,19 @@ static int io_file_bitmap_get(struct io_ring_ctx *ctx)
bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
{
table->nodes = kvmalloc_array(nr_files, sizeof(struct io_src_node *),
GFP_KERNEL_ACCOUNT | __GFP_ZERO);
if (unlikely(!table->nodes))
if (io_rsrc_data_alloc(&table->data, nr_files))
return false;
table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
if (unlikely(!table->bitmap)) {
kvfree(table->nodes);
return false;
}
return true;
if (table->bitmap)
return true;
io_rsrc_data_free(&table->data);
return false;
}
void io_free_file_tables(struct io_file_table *table)
{
kvfree(table->nodes);
io_rsrc_data_free(&table->data);
bitmap_free(table->bitmap);
table->nodes = NULL;
table->bitmap = NULL;
}
@@ -68,22 +62,22 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
if (io_is_uring_fops(file))
return -EBADF;
if (!ctx->file_data)
if (!ctx->file_table.data.nr)
return -ENXIO;
if (slot_index >= ctx->nr_user_files)
if (slot_index >= ctx->file_table.data.nr)
return -EINVAL;
node = io_rsrc_node_alloc(ctx, IORING_RSRC_FILE);
if (!node)
return -ENOMEM;
slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
if (ctx->file_table.nodes[slot_index])
io_put_rsrc_node(ctx->file_table.nodes[slot_index]);
slot_index = array_index_nospec(slot_index, ctx->file_table.data.nr);
if (ctx->file_table.data.nodes[slot_index])
io_put_rsrc_node(ctx->file_table.data.nodes[slot_index]);
else
io_file_bitmap_set(&ctx->file_table, slot_index);
ctx->file_table.nodes[slot_index] = node;
ctx->file_table.data.nodes[slot_index] = node;
io_fixed_file_set(node, file);
return 0;
}
@@ -129,16 +123,16 @@ int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
{
if (unlikely(!ctx->file_data))
if (unlikely(!ctx->file_table.data.nr))
return -ENXIO;
if (offset >= ctx->nr_user_files)
if (offset >= ctx->file_table.data.nr)
return -EINVAL;
offset = array_index_nospec(offset, ctx->nr_user_files);
if (!ctx->file_table.nodes[offset])
offset = array_index_nospec(offset, ctx->file_table.data.nr);
if (!ctx->file_table.data.nodes[offset])
return -EBADF;
io_put_rsrc_node(ctx->file_table.nodes[offset]);
ctx->file_table.nodes[offset] = NULL;
io_put_rsrc_node(ctx->file_table.data.nodes[offset]);
ctx->file_table.data.nodes[offset] = NULL;
io_file_bitmap_clear(&ctx->file_table, offset);
return 0;
}
@@ -153,7 +147,7 @@ int io_register_file_alloc_range(struct io_ring_ctx *ctx,
return -EFAULT;
if (check_add_overflow(range.off, range.len, &end))
return -EOVERFLOW;
if (range.resv || end > ctx->nr_user_files)
if (range.resv || end > ctx->file_table.data.nr)
return -EINVAL;
io_file_table_set_alloc_range(ctx, range.off, range.len);