io_uring/rsrc: add io_reset_rsrc_node() helper

Puts and reset an existing node in a slot, if one exists. Returns true
if a node was there, false if not. This helps cleanup some of the code
that does a lookup just to clear an existing node.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
Jens Axboe 2024-10-29 09:02:38 -06:00
parent 5f3829fdd6
commit 4007c3d8c2
3 changed files with 17 additions and 16 deletions

View File

@ -58,7 +58,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
u32 slot_index) u32 slot_index)
__must_hold(&req->ctx->uring_lock) __must_hold(&req->ctx->uring_lock)
{ {
struct io_rsrc_node *node, *old_node; struct io_rsrc_node *node;
if (io_is_uring_fops(file)) if (io_is_uring_fops(file))
return -EBADF; return -EBADF;
@ -71,10 +71,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
if (!node) if (!node)
return -ENOMEM; return -ENOMEM;
old_node = io_rsrc_node_lookup(&ctx->file_table.data, slot_index); if (!io_reset_rsrc_node(&ctx->file_table.data, slot_index))
if (old_node)
io_put_rsrc_node(old_node);
else
io_file_bitmap_set(&ctx->file_table, slot_index); io_file_bitmap_set(&ctx->file_table, slot_index);
ctx->file_table.data.nodes[slot_index] = node; ctx->file_table.data.nodes[slot_index] = node;
@ -133,8 +130,7 @@ int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
node = io_rsrc_node_lookup(&ctx->file_table.data, offset); node = io_rsrc_node_lookup(&ctx->file_table.data, offset);
if (!node) if (!node)
return -EBADF; return -EBADF;
io_put_rsrc_node(node); io_reset_rsrc_node(&ctx->file_table.data, offset);
ctx->file_table.data.nodes[offset] = NULL;
io_file_bitmap_clear(&ctx->file_table, offset); io_file_bitmap_clear(&ctx->file_table, offset);
return 0; return 0;
} }

View File

@ -181,7 +181,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
return -EINVAL; return -EINVAL;
for (done = 0; done < nr_args; done++) { for (done = 0; done < nr_args; done++) {
struct io_rsrc_node *node;
u64 tag = 0; u64 tag = 0;
if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
@ -197,12 +196,9 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
continue; continue;
i = up->offset + done; i = up->offset + done;
node = io_rsrc_node_lookup(&ctx->file_table.data, i); if (io_reset_rsrc_node(&ctx->file_table.data, i))
if (node) {
io_put_rsrc_node(node);
ctx->file_table.data.nodes[i] = NULL;
io_file_bitmap_clear(&ctx->file_table, i); io_file_bitmap_clear(&ctx->file_table, i);
}
if (fd != -1) { if (fd != -1) {
struct file *file = fget(fd); struct file *file = fget(fd);
struct io_rsrc_node *node; struct io_rsrc_node *node;
@ -279,9 +275,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
break; break;
} }
i = array_index_nospec(up->offset + done, ctx->buf_table.nr); i = array_index_nospec(up->offset + done, ctx->buf_table.nr);
if (ctx->buf_table.nodes[i]) io_reset_rsrc_node(&ctx->buf_table, i);
io_put_rsrc_node(ctx->buf_table.nodes[i]);
ctx->buf_table.nodes[i] = node; ctx->buf_table.nodes[i] = node;
if (tag) if (tag)
node->tag = tag; node->tag = tag;

View File

@ -84,6 +84,17 @@ static inline void io_put_rsrc_node(struct io_rsrc_node *node)
io_free_rsrc_node(node); io_free_rsrc_node(node);
} }
static inline bool io_reset_rsrc_node(struct io_rsrc_data *data, int index)
{
struct io_rsrc_node *node = data->nodes[index];
if (!node)
return false;
io_put_rsrc_node(node);
data->nodes[index] = NULL;
return true;
}
static inline void io_req_put_rsrc_nodes(struct io_kiocb *req) static inline void io_req_put_rsrc_nodes(struct io_kiocb *req)
{ {
if (req->rsrc_nodes[IORING_RSRC_FILE] != rsrc_empty_node) { if (req->rsrc_nodes[IORING_RSRC_FILE] != rsrc_empty_node) {