From 4007c3d8c22a2025367953f4ee36ae106a69d855 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 29 Oct 2024 09:02:38 -0600 Subject: [PATCH] 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 --- io_uring/filetable.c | 10 +++------- io_uring/rsrc.c | 12 +++--------- io_uring/rsrc.h | 11 +++++++++++ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/io_uring/filetable.c b/io_uring/filetable.c index 7a9de6718b8a..45f005f5db42 100644 --- a/io_uring/filetable.c +++ b/io_uring/filetable.c @@ -58,7 +58,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file, u32 slot_index) __must_hold(&req->ctx->uring_lock) { - struct io_rsrc_node *node, *old_node; + struct io_rsrc_node *node; if (io_is_uring_fops(file)) return -EBADF; @@ -71,10 +71,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file, if (!node) return -ENOMEM; - old_node = io_rsrc_node_lookup(&ctx->file_table.data, slot_index); - if (old_node) - io_put_rsrc_node(old_node); - else + if (!io_reset_rsrc_node(&ctx->file_table.data, slot_index)) io_file_bitmap_set(&ctx->file_table, slot_index); 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); if (!node) return -EBADF; - io_put_rsrc_node(node); - ctx->file_table.data.nodes[offset] = NULL; + io_reset_rsrc_node(&ctx->file_table.data, offset); io_file_bitmap_clear(&ctx->file_table, offset); return 0; } diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 0380b2f4ed8d..378f33746457 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -181,7 +181,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, return -EINVAL; for (done = 0; done < nr_args; done++) { - struct io_rsrc_node *node; u64 tag = 0; 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; i = up->offset + done; - node = io_rsrc_node_lookup(&ctx->file_table.data, i); - if (node) { - io_put_rsrc_node(node); - ctx->file_table.data.nodes[i] = NULL; + if (io_reset_rsrc_node(&ctx->file_table.data, i)) io_file_bitmap_clear(&ctx->file_table, i); - } + if (fd != -1) { struct file *file = fget(fd); struct io_rsrc_node *node; @@ -279,9 +275,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, break; } i = array_index_nospec(up->offset + done, ctx->buf_table.nr); - if (ctx->buf_table.nodes[i]) - io_put_rsrc_node(ctx->buf_table.nodes[i]); - + io_reset_rsrc_node(&ctx->buf_table, i); ctx->buf_table.nodes[i] = node; if (tag) node->tag = tag; diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h index 82e08cbba02e..43b19e516f5f 100644 --- a/io_uring/rsrc.h +++ b/io_uring/rsrc.h @@ -84,6 +84,17 @@ static inline void io_put_rsrc_node(struct io_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) { if (req->rsrc_nodes[IORING_RSRC_FILE] != rsrc_empty_node) {