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@xxxxxxxxx> --- 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 1f22f183cdeb..717d5b806781 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 (IS_ERR(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 0924c53dd954..97673771a0fb 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -182,7 +182,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))) || @@ -198,12 +197,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; @@ -281,9 +277,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, err = PTR_ERR(node); break; } - 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 6952fb45f57a..abd214f303f5 100644 --- a/io_uring/rsrc.h +++ b/io_uring/rsrc.h @@ -85,6 +85,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) { -- 2.45.2