Skip to content

Commit 3597f27

Browse files
committed
io_uring/rsrc: unify file and buffer resource tables
For files, there's nr_user_files/file_table/file_data, and buffers have nr_user_bufs/user_bufs/buf_data. There's no reason why file_table and file_data can't be the same thing, and ditto for the buffer side. That gets rid of more io_ring_ctx state that's in two spots rather than just being in one spot, as it should be. Put all the registered file data in one locations, and ditto on the buffer front. This also avoids having both io_rsrc_data->nodes being an allocated array, and ->user_bufs[] or ->file_table.nodes. There's no reason to have this information duplicated. Keep it in one spot, io_rsrc_data, along with how many resources are available. Signed-off-by: Jens Axboe <[email protected]>
1 parent f38f284 commit 3597f27

File tree

15 files changed

+123
-212
lines changed

15 files changed

+123
-212
lines changed

include/linux/io_uring_types.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ struct io_wq_work {
5555
int cancel_seq;
5656
};
5757

58+
struct io_rsrc_data {
59+
unsigned int nr;
60+
struct io_rsrc_node **nodes;
61+
};
62+
5863
struct io_file_table {
59-
struct io_rsrc_node **nodes;
64+
struct io_rsrc_data data;
6065
unsigned long *bitmap;
6166
unsigned int alloc_hint;
6267
};
@@ -276,9 +281,7 @@ struct io_ring_ctx {
276281
struct io_wq_work_list iopoll_list;
277282

278283
struct io_file_table file_table;
279-
struct io_rsrc_node **user_bufs;
280-
unsigned nr_user_files;
281-
unsigned nr_user_bufs;
284+
struct io_rsrc_data buf_table;
282285

283286
struct io_submit_state submit_state;
284287

@@ -366,10 +369,6 @@ struct io_ring_ctx {
366369
struct wait_queue_head poll_wq;
367370
struct io_restriction restrictions;
368371

369-
/* slow path rsrc auxilary data, used by update/register */
370-
struct io_rsrc_data *file_data;
371-
struct io_rsrc_data *buf_data;
372-
373372
u32 pers_next;
374373
struct xarray personalities;
375374

io_uring/cancel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ static int __io_sync_cancel(struct io_uring_task *tctx,
240240
/* fixed must be grabbed every time since we drop the uring_lock */
241241
if ((cd->flags & IORING_ASYNC_CANCEL_FD) &&
242242
(cd->flags & IORING_ASYNC_CANCEL_FD_FIXED)) {
243-
if (unlikely(fd >= ctx->nr_user_files))
243+
if (unlikely(fd >= ctx->file_table.data.nr))
244244
return -EBADF;
245-
fd = array_index_nospec(fd, ctx->nr_user_files);
245+
fd = array_index_nospec(fd, ctx->file_table.data.nr);
246246
cd->file = io_file_from_index(&ctx->file_table, fd);
247247
if (!cd->file)
248248
return -EBADF;

io_uring/fdinfo.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,18 @@ __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *file)
165165
seq_printf(m, "SqThreadCpu:\t%d\n", sq_cpu);
166166
seq_printf(m, "SqTotalTime:\t%llu\n", sq_total_time);
167167
seq_printf(m, "SqWorkTime:\t%llu\n", sq_work_time);
168-
seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
169-
for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
168+
seq_printf(m, "UserFiles:\t%u\n", ctx->file_table.data.nr);
169+
for (i = 0; has_lock && i < ctx->file_table.data.nr; i++) {
170170
struct file *f = io_file_from_index(&ctx->file_table, i);
171171

172172
if (f)
173173
seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
174174
else
175175
seq_printf(m, "%5u: <none>\n", i);
176176
}
177-
seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
178-
for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
179-
struct io_mapped_ubuf *buf = ctx->user_bufs[i]->buf;
177+
seq_printf(m, "UserBufs:\t%u\n", ctx->buf_table.nr);
178+
for (i = 0; has_lock && i < ctx->buf_table.nr; i++) {
179+
struct io_mapped_ubuf *buf = ctx->buf_table.nodes[i]->buf;
180180

181181
seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, buf->len);
182182
}

io_uring/filetable.c

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,19 @@ static int io_file_bitmap_get(struct io_ring_ctx *ctx)
3838

3939
bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
4040
{
41-
table->nodes = kvmalloc_array(nr_files, sizeof(struct io_src_node *),
42-
GFP_KERNEL_ACCOUNT | __GFP_ZERO);
43-
if (unlikely(!table->nodes))
41+
if (io_rsrc_data_alloc(&table->data, nr_files))
4442
return false;
45-
4643
table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
47-
if (unlikely(!table->bitmap)) {
48-
kvfree(table->nodes);
49-
return false;
50-
}
51-
52-
return true;
44+
if (table->bitmap)
45+
return true;
46+
io_rsrc_data_free(&table->data);
47+
return false;
5348
}
5449

5550
void io_free_file_tables(struct io_file_table *table)
5651
{
57-
kvfree(table->nodes);
52+
io_rsrc_data_free(&table->data);
5853
bitmap_free(table->bitmap);
59-
table->nodes = NULL;
6054
table->bitmap = NULL;
6155
}
6256

@@ -68,22 +62,22 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
6862

6963
if (io_is_uring_fops(file))
7064
return -EBADF;
71-
if (!ctx->file_data)
65+
if (!ctx->file_table.data.nr)
7266
return -ENXIO;
73-
if (slot_index >= ctx->nr_user_files)
67+
if (slot_index >= ctx->file_table.data.nr)
7468
return -EINVAL;
7569

7670
node = io_rsrc_node_alloc(ctx, IORING_RSRC_FILE);
7771
if (!node)
7872
return -ENOMEM;
7973

80-
slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
81-
if (ctx->file_table.nodes[slot_index])
82-
io_put_rsrc_node(ctx->file_table.nodes[slot_index]);
74+
slot_index = array_index_nospec(slot_index, ctx->file_table.data.nr);
75+
if (ctx->file_table.data.nodes[slot_index])
76+
io_put_rsrc_node(ctx->file_table.data.nodes[slot_index]);
8377
else
8478
io_file_bitmap_set(&ctx->file_table, slot_index);
8579

86-
ctx->file_table.nodes[slot_index] = node;
80+
ctx->file_table.data.nodes[slot_index] = node;
8781
io_fixed_file_set(node, file);
8882
return 0;
8983
}
@@ -129,16 +123,16 @@ int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
129123

130124
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
131125
{
132-
if (unlikely(!ctx->file_data))
126+
if (unlikely(!ctx->file_table.data.nr))
133127
return -ENXIO;
134-
if (offset >= ctx->nr_user_files)
128+
if (offset >= ctx->file_table.data.nr)
135129
return -EINVAL;
136130

137-
offset = array_index_nospec(offset, ctx->nr_user_files);
138-
if (!ctx->file_table.nodes[offset])
131+
offset = array_index_nospec(offset, ctx->file_table.data.nr);
132+
if (!ctx->file_table.data.nodes[offset])
139133
return -EBADF;
140-
io_put_rsrc_node(ctx->file_table.nodes[offset]);
141-
ctx->file_table.nodes[offset] = NULL;
134+
io_put_rsrc_node(ctx->file_table.data.nodes[offset]);
135+
ctx->file_table.data.nodes[offset] = NULL;
142136
io_file_bitmap_clear(&ctx->file_table, offset);
143137
return 0;
144138
}
@@ -153,7 +147,7 @@ int io_register_file_alloc_range(struct io_ring_ctx *ctx,
153147
return -EFAULT;
154148
if (check_add_overflow(range.off, range.len, &end))
155149
return -EOVERFLOW;
156-
if (range.resv || end > ctx->nr_user_files)
150+
if (range.resv || end > ctx->file_table.data.nr)
157151
return -EINVAL;
158152

159153
io_file_table_set_alloc_range(ctx, range.off, range.len);

io_uring/filetable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static inline struct file *io_slot_file(struct io_rsrc_node *node)
5252
static inline struct file *io_file_from_index(struct io_file_table *table,
5353
int index)
5454
{
55-
struct io_rsrc_node *node = table->nodes[index];
55+
struct io_rsrc_node *node = table->data.nodes[index];
5656

5757
if (node)
5858
return io_slot_file(node);

io_uring/io_uring.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,11 +1879,10 @@ inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
18791879
struct file *file = NULL;
18801880

18811881
io_ring_submit_lock(ctx, issue_flags);
1882-
1883-
if (unlikely((unsigned int)fd >= ctx->nr_user_files))
1882+
if (unlikely((unsigned int)fd >= ctx->file_table.data.nr))
18841883
goto out;
1885-
fd = array_index_nospec(fd, ctx->nr_user_files);
1886-
node = ctx->file_table.nodes[fd];
1884+
fd = array_index_nospec(fd, ctx->file_table.data.nr);
1885+
node = ctx->file_table.data.nodes[fd];
18871886
if (node) {
18881887
io_req_assign_rsrc_node(req, node);
18891888
req->flags |= io_slot_flags(node);

io_uring/msg_ring.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_fl
180180
int idx = msg->src_fd;
181181

182182
io_ring_submit_lock(ctx, issue_flags);
183-
if (likely(idx < ctx->nr_user_files)) {
184-
idx = array_index_nospec(idx, ctx->nr_user_files);
183+
if (likely(idx < ctx->file_table.data.nr)) {
184+
idx = array_index_nospec(idx, ctx->file_table.data.nr);
185185
file = io_file_from_index(&ctx->file_table, idx);
186186
if (file)
187187
get_file(file);

io_uring/net.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,9 +1347,9 @@ static int io_send_zc_import(struct io_kiocb *req, unsigned int issue_flags)
13471347

13481348
ret = -EFAULT;
13491349
io_ring_submit_lock(ctx, issue_flags);
1350-
if (sr->buf_index < ctx->nr_user_bufs) {
1351-
idx = array_index_nospec(sr->buf_index, ctx->nr_user_bufs);
1352-
node = ctx->user_bufs[idx];
1350+
if (sr->buf_index < ctx->buf_table.nr) {
1351+
idx = array_index_nospec(sr->buf_index, ctx->buf_table.nr);
1352+
node = ctx->buf_table.nodes[idx];
13531353
io_req_assign_rsrc_node(sr->notif, node);
13541354
ret = 0;
13551355
}

io_uring/nop.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ int io_nop(struct io_kiocb *req, unsigned int issue_flags)
6666

6767
ret = -EFAULT;
6868
io_ring_submit_lock(ctx, issue_flags);
69-
if (nop->buffer < ctx->nr_user_bufs) {
70-
idx = array_index_nospec(nop->buffer, ctx->nr_user_bufs);
71-
node = READ_ONCE(ctx->user_bufs[idx]);
69+
if (nop->buffer < ctx->buf_table.nr) {
70+
idx = array_index_nospec(nop->buffer, ctx->buf_table.nr);
71+
node = READ_ONCE(ctx->buf_table.nodes[idx]);
7272
io_req_assign_rsrc_node(req, node);
7373
ret = 0;
7474
}

io_uring/register.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,8 @@ SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
937937
mutex_lock(&ctx->uring_lock);
938938
ret = __io_uring_register(ctx, opcode, arg, nr_args);
939939
mutex_unlock(&ctx->uring_lock);
940-
trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret);
940+
trace_io_uring_register(ctx, opcode, ctx->file_table.data.nr,
941+
ctx->buf_table.nr, ret);
941942
if (!use_registered_ring)
942943
fput(file);
943944
return ret;

0 commit comments

Comments
 (0)