io_uring/rsrc: add io_reset_rsrc_node() helper
authorJens Axboe <axboe@kernel.dk>
Tue, 29 Oct 2024 15:02:38 +0000 (09:02 -0600)
committerJens Axboe <axboe@kernel.dk>
Sat, 2 Nov 2024 21:45:30 +0000 (15:45 -0600)
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>
io_uring/filetable.c
io_uring/rsrc.c
io_uring/rsrc.h

index 7a9de6718b8a02000b07af5f5ad2e866dd006cb9..45f005f5db42e07de3f53b6789d8032cfde9016c 100644 (file)
@@ -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;
 }
index 0380b2f4ed8d7d482ea3d96c314947083d01ab07..378f337464572cec89fe273db9008a58c4730dd8 100644 (file)
@@ -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;
index 82e08cbba02e9172b2b282ad386aecfa58f92c32..43b19e516f5fd86b9f5a7eff7380f0639d1cc21f 100644 (file)
@@ -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) {