Skip to content

Commit 4007c3d

Browse files
committed
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 <[email protected]>
1 parent 5f3829f commit 4007c3d

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

io_uring/filetable.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
5858
u32 slot_index)
5959
__must_hold(&req->ctx->uring_lock)
6060
{
61-
struct io_rsrc_node *node, *old_node;
61+
struct io_rsrc_node *node;
6262

6363
if (io_is_uring_fops(file))
6464
return -EBADF;
@@ -71,10 +71,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
7171
if (!node)
7272
return -ENOMEM;
7373

74-
old_node = io_rsrc_node_lookup(&ctx->file_table.data, slot_index);
75-
if (old_node)
76-
io_put_rsrc_node(old_node);
77-
else
74+
if (!io_reset_rsrc_node(&ctx->file_table.data, slot_index))
7875
io_file_bitmap_set(&ctx->file_table, slot_index);
7976

8077
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)
133130
node = io_rsrc_node_lookup(&ctx->file_table.data, offset);
134131
if (!node)
135132
return -EBADF;
136-
io_put_rsrc_node(node);
137-
ctx->file_table.data.nodes[offset] = NULL;
133+
io_reset_rsrc_node(&ctx->file_table.data, offset);
138134
io_file_bitmap_clear(&ctx->file_table, offset);
139135
return 0;
140136
}

io_uring/rsrc.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
181181
return -EINVAL;
182182

183183
for (done = 0; done < nr_args; done++) {
184-
struct io_rsrc_node *node;
185184
u64 tag = 0;
186185

187186
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,
197196
continue;
198197

199198
i = up->offset + done;
200-
node = io_rsrc_node_lookup(&ctx->file_table.data, i);
201-
if (node) {
202-
io_put_rsrc_node(node);
203-
ctx->file_table.data.nodes[i] = NULL;
199+
if (io_reset_rsrc_node(&ctx->file_table.data, i))
204200
io_file_bitmap_clear(&ctx->file_table, i);
205-
}
201+
206202
if (fd != -1) {
207203
struct file *file = fget(fd);
208204
struct io_rsrc_node *node;
@@ -279,9 +275,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
279275
break;
280276
}
281277
i = array_index_nospec(up->offset + done, ctx->buf_table.nr);
282-
if (ctx->buf_table.nodes[i])
283-
io_put_rsrc_node(ctx->buf_table.nodes[i]);
284-
278+
io_reset_rsrc_node(&ctx->buf_table, i);
285279
ctx->buf_table.nodes[i] = node;
286280
if (tag)
287281
node->tag = tag;

io_uring/rsrc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ static inline void io_put_rsrc_node(struct io_rsrc_node *node)
8484
io_free_rsrc_node(node);
8585
}
8686

87+
static inline bool io_reset_rsrc_node(struct io_rsrc_data *data, int index)
88+
{
89+
struct io_rsrc_node *node = data->nodes[index];
90+
91+
if (!node)
92+
return false;
93+
io_put_rsrc_node(node);
94+
data->nodes[index] = NULL;
95+
return true;
96+
}
97+
8798
static inline void io_req_put_rsrc_nodes(struct io_kiocb *req)
8899
{
89100
if (req->rsrc_nodes[IORING_RSRC_FILE] != rsrc_empty_node) {

0 commit comments

Comments
 (0)