Skip to content

Commit 6e73dff

Browse files
isilenceaxboe
authored andcommitted
io_uring: let to set a range for file slot allocation
From recently io_uring provides an option to allocate a file index for operation registering fixed files. However, it's utterly unusable with mixed approaches when for a part of files the userspace knows better where to place it, as it may race and users don't have any sane way to pick a slot and hoping it will not be taken. Let the userspace to register a range of fixed file slots in which the auto-allocation happens. The use case is splittting the fixed table in two parts, where on of them is used for auto-allocation and another for slot-specified operations. Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://lore.kernel.org/r/66ab0394e436f38437cf7c44676e1920d09687ad.1656154403.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent e6130eb commit 6e73dff

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-7
lines changed

include/linux/io_uring_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ struct io_ring_ctx {
233233

234234
unsigned long check_cq;
235235

236+
unsigned int file_alloc_start;
237+
unsigned int file_alloc_end;
238+
236239
struct {
237240
/*
238241
* We cache a range of free CQEs we can use, once exhausted it

include/uapi/linux/io_uring.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ enum {
449449
/* sync cancelation API */
450450
IORING_REGISTER_SYNC_CANCEL = 24,
451451

452+
/* register a range of fixed file slots for automatic slot allocation */
453+
IORING_REGISTER_FILE_ALLOC_RANGE = 25,
454+
452455
/* this goes last */
453456
IORING_REGISTER_LAST
454457
};
@@ -595,4 +598,14 @@ struct io_uring_sync_cancel_reg {
595598
__u64 pad[4];
596599
};
597600

601+
/*
602+
* Argument for IORING_REGISTER_FILE_ALLOC_RANGE
603+
* The range is specified as [off, off + len)
604+
*/
605+
struct io_uring_file_index_range {
606+
__u32 off;
607+
__u32 len;
608+
__u64 resv;
609+
};
610+
598611
#endif

io_uring/filetable.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@
1616
static int io_file_bitmap_get(struct io_ring_ctx *ctx)
1717
{
1818
struct io_file_table *table = &ctx->file_table;
19-
unsigned long nr = ctx->nr_user_files;
19+
unsigned long nr = ctx->file_alloc_end;
2020
int ret;
2121

2222
do {
2323
ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
2424
if (ret != nr)
2525
return ret;
2626

27-
if (!table->alloc_hint)
27+
if (table->alloc_hint == ctx->file_alloc_start)
2828
break;
29-
3029
nr = table->alloc_hint;
31-
table->alloc_hint = 0;
30+
table->alloc_hint = ctx->file_alloc_start;
3231
} while (1);
3332

3433
return -ENFILE;
@@ -175,3 +174,20 @@ int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
175174
io_rsrc_node_switch(ctx, ctx->file_data);
176175
return 0;
177176
}
177+
178+
int io_register_file_alloc_range(struct io_ring_ctx *ctx,
179+
struct io_uring_file_index_range __user *arg)
180+
{
181+
struct io_uring_file_index_range range;
182+
u32 end;
183+
184+
if (copy_from_user(&range, arg, sizeof(range)))
185+
return -EFAULT;
186+
if (check_add_overflow(range.off, range.len, &end))
187+
return -EOVERFLOW;
188+
if (range.resv || end > ctx->nr_user_files)
189+
return -EINVAL;
190+
191+
io_file_table_set_alloc_range(ctx, range.off, range.len);
192+
return 0;
193+
}

io_uring/filetable.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#define IOU_FILE_TABLE_H
44

55
#include <linux/file.h>
6-
7-
struct io_ring_ctx;
8-
struct io_kiocb;
6+
#include <linux/io_uring_types.h>
97

108
/*
119
* FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 0
@@ -33,6 +31,9 @@ int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
3331
unsigned int file_slot);
3432
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset);
3533

34+
int io_register_file_alloc_range(struct io_ring_ctx *ctx,
35+
struct io_uring_file_index_range __user *arg);
36+
3637
unsigned int io_file_get_flags(struct file *file);
3738

3839
static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
@@ -71,4 +72,17 @@ static inline void io_fixed_file_set(struct io_fixed_file *file_slot,
7172
file_slot->file_ptr = file_ptr;
7273
}
7374

75+
static inline void io_reset_alloc_hint(struct io_ring_ctx *ctx)
76+
{
77+
ctx->file_table.alloc_hint = ctx->file_alloc_start;
78+
}
79+
80+
static inline void io_file_table_set_alloc_range(struct io_ring_ctx *ctx,
81+
unsigned off, unsigned len)
82+
{
83+
ctx->file_alloc_start = off;
84+
ctx->file_alloc_end = off + len;
85+
io_reset_alloc_hint(ctx);
86+
}
87+
7488
#endif

io_uring/io_uring.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,6 +3866,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
38663866
break;
38673867
ret = io_sync_cancel(ctx, arg);
38683868
break;
3869+
case IORING_REGISTER_FILE_ALLOC_RANGE:
3870+
ret = -EINVAL;
3871+
if (!arg || nr_args)
3872+
break;
3873+
ret = io_register_file_alloc_range(ctx, arg);
3874+
break;
38693875
default:
38703876
ret = -EINVAL;
38713877
break;

io_uring/rsrc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,8 @@ int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
10121012
io_file_bitmap_set(&ctx->file_table, i);
10131013
}
10141014

1015+
/* default it to the whole table */
1016+
io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
10151017
io_rsrc_node_switch(ctx, NULL);
10161018
return 0;
10171019
fail:

0 commit comments

Comments
 (0)