Commit 6e73dffb authored by Pavel Begunkov's avatar Pavel Begunkov Committed by Jens Axboe
Browse files

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: default avatarPavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/66ab0394e436f38437cf7c44676e1920d09687ad.1656154403.git.asml.silence@gmail.com


Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent e6130eba
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -233,6 +233,9 @@ struct io_ring_ctx {


	unsigned long		check_cq;
	unsigned long		check_cq;


	unsigned int		file_alloc_start;
	unsigned int		file_alloc_end;

	struct {
	struct {
		/*
		/*
		 * We cache a range of free CQEs we can use, once exhausted it
		 * We cache a range of free CQEs we can use, once exhausted it
+13 −0
Original line number Original line Diff line number Diff line
@@ -449,6 +449,9 @@ enum {
	/* sync cancelation API */
	/* sync cancelation API */
	IORING_REGISTER_SYNC_CANCEL		= 24,
	IORING_REGISTER_SYNC_CANCEL		= 24,


	/* register a range of fixed file slots for automatic slot allocation */
	IORING_REGISTER_FILE_ALLOC_RANGE	= 25,

	/* this goes last */
	/* this goes last */
	IORING_REGISTER_LAST
	IORING_REGISTER_LAST
};
};
@@ -595,4 +598,14 @@ struct io_uring_sync_cancel_reg {
	__u64				pad[4];
	__u64				pad[4];
};
};


/*
 * Argument for IORING_REGISTER_FILE_ALLOC_RANGE
 * The range is specified as [off, off + len)
 */
struct io_uring_file_index_range {
	__u32	off;
	__u32	len;
	__u64	resv;
};

#endif
#endif
+20 −4
Original line number Original line Diff line number Diff line
@@ -16,7 +16,7 @@
static int io_file_bitmap_get(struct io_ring_ctx *ctx)
static int io_file_bitmap_get(struct io_ring_ctx *ctx)
{
{
	struct io_file_table *table = &ctx->file_table;
	struct io_file_table *table = &ctx->file_table;
	unsigned long nr = ctx->nr_user_files;
	unsigned long nr = ctx->file_alloc_end;
	int ret;
	int ret;


	do {
	do {
@@ -24,11 +24,10 @@ static int io_file_bitmap_get(struct io_ring_ctx *ctx)
		if (ret != nr)
		if (ret != nr)
			return ret;
			return ret;


		if (!table->alloc_hint)
		if (table->alloc_hint == ctx->file_alloc_start)
			break;
			break;

		nr = table->alloc_hint;
		nr = table->alloc_hint;
		table->alloc_hint = 0;
		table->alloc_hint = ctx->file_alloc_start;
	} while (1);
	} while (1);


	return -ENFILE;
	return -ENFILE;
@@ -175,3 +174,20 @@ int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
	io_rsrc_node_switch(ctx, ctx->file_data);
	io_rsrc_node_switch(ctx, ctx->file_data);
	return 0;
	return 0;
}
}

int io_register_file_alloc_range(struct io_ring_ctx *ctx,
				 struct io_uring_file_index_range __user *arg)
{
	struct io_uring_file_index_range range;
	u32 end;

	if (copy_from_user(&range, arg, sizeof(range)))
		return -EFAULT;
	if (check_add_overflow(range.off, range.len, &end))
		return -EOVERFLOW;
	if (range.resv || end > ctx->nr_user_files)
		return -EINVAL;

	io_file_table_set_alloc_range(ctx, range.off, range.len);
	return 0;
}
+17 −3
Original line number Original line Diff line number Diff line
@@ -3,9 +3,7 @@
#define IOU_FILE_TABLE_H
#define IOU_FILE_TABLE_H


#include <linux/file.h>
#include <linux/file.h>

#include <linux/io_uring_types.h>
struct io_ring_ctx;
struct io_kiocb;


/*
/*
 * FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 0
 * 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,
				unsigned int file_slot);
				unsigned int file_slot);
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset);
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset);


int io_register_file_alloc_range(struct io_ring_ctx *ctx,
				 struct io_uring_file_index_range __user *arg);

unsigned int io_file_get_flags(struct file *file);
unsigned int io_file_get_flags(struct file *file);


static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
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,
	file_slot->file_ptr = file_ptr;
	file_slot->file_ptr = file_ptr;
}
}


static inline void io_reset_alloc_hint(struct io_ring_ctx *ctx)
{
	ctx->file_table.alloc_hint = ctx->file_alloc_start;
}

static inline void io_file_table_set_alloc_range(struct io_ring_ctx *ctx,
						 unsigned off, unsigned len)
{
	ctx->file_alloc_start = off;
	ctx->file_alloc_end = off + len;
	io_reset_alloc_hint(ctx);
}

#endif
#endif
+6 −0
Original line number Original line Diff line number Diff line
@@ -3866,6 +3866,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
			break;
			break;
		ret = io_sync_cancel(ctx, arg);
		ret = io_sync_cancel(ctx, arg);
		break;
		break;
	case IORING_REGISTER_FILE_ALLOC_RANGE:
		ret = -EINVAL;
		if (!arg || nr_args)
			break;
		ret = io_register_file_alloc_range(ctx, arg);
		break;
	default:
	default:
		ret = -EINVAL;
		ret = -EINVAL;
		break;
		break;
Loading