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

io_uring: deduplicate file table slot calculation



Extract a helper io_fixed_file_slot() returning a place in our fixed
files table, so we don't hand-code it three times in the code.

Signed-off-by: default avatarPavel Begunkov <asml.silence@gmail.com>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 847595de
Loading
Loading
Loading
Loading
+19 −22
Original line number Original line Diff line number Diff line
@@ -7740,6 +7740,15 @@ static void io_rsrc_put_work(struct work_struct *work)
	}
	}
}
}


static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
					unsigned i)
{
	struct fixed_rsrc_table *table;

	table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
	return &table->files[i & IORING_FILE_TABLE_MASK];
}

static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
{
{
	struct fixed_rsrc_ref_node *ref_node;
	struct fixed_rsrc_ref_node *ref_node;
@@ -7808,6 +7817,7 @@ static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
	kfree(ref_node);
	kfree(ref_node);
}
}



static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
				 unsigned nr_args)
				 unsigned nr_args)
{
{
@@ -7840,9 +7850,6 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
		goto out_free;
		goto out_free;


	for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
	for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
		struct fixed_rsrc_table *table;
		unsigned index;

		if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
		if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
			ret = -EFAULT;
			ret = -EFAULT;
			goto out_fput;
			goto out_fput;
@@ -7867,9 +7874,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
			fput(file);
			fput(file);
			goto out_fput;
			goto out_fput;
		}
		}
		table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
		*io_fixed_file_slot(file_data, i) = file;
		index = i & IORING_FILE_TABLE_MASK;
		table->files[index] = file;
	}
	}


	ret = io_sqe_files_scm(ctx);
	ret = io_sqe_files_scm(ctx);
@@ -7972,7 +7977,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
{
{
	struct fixed_rsrc_data *data = ctx->file_data;
	struct fixed_rsrc_data *data = ctx->file_data;
	struct fixed_rsrc_ref_node *ref_node;
	struct fixed_rsrc_ref_node *ref_node;
	struct file *file;
	struct file *file, **file_slot;
	__s32 __user *fds;
	__s32 __user *fds;
	int fd, i, err;
	int fd, i, err;
	__u32 done;
	__u32 done;
@@ -7990,9 +7995,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,


	fds = u64_to_user_ptr(up->data);
	fds = u64_to_user_ptr(up->data);
	for (done = 0; done < nr_args; done++) {
	for (done = 0; done < nr_args; done++) {
		struct fixed_rsrc_table *table;
		unsigned index;

		err = 0;
		err = 0;
		if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
		if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
			err = -EFAULT;
			err = -EFAULT;
@@ -8002,14 +8004,13 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
			continue;
			continue;


		i = array_index_nospec(up->offset + done, ctx->nr_user_files);
		i = array_index_nospec(up->offset + done, ctx->nr_user_files);
		table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
		file_slot = io_fixed_file_slot(ctx->file_data, i);
		index = i & IORING_FILE_TABLE_MASK;

		if (table->files[index]) {
		if (*file_slot) {
			file = table->files[index];
			err = io_queue_file_removal(data, *file_slot);
			err = io_queue_file_removal(data, file);
			if (err)
			if (err)
				break;
				break;
			table->files[index] = NULL;
			*file_slot = NULL;
			needs_switch = true;
			needs_switch = true;
		}
		}
		if (fd != -1) {
		if (fd != -1) {
@@ -8031,13 +8032,12 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
				err = -EBADF;
				err = -EBADF;
				break;
				break;
			}
			}
			table->files[index] = file;
			err = io_sqe_file_register(ctx, file, i);
			err = io_sqe_file_register(ctx, file, i);
			if (err) {
			if (err) {
				table->files[index] = NULL;
				fput(file);
				fput(file);
				break;
				break;
			}
			}
			*file_slot = file;
		}
		}
	}
	}


@@ -9488,11 +9488,8 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
	seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
	seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
	seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
	seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
	for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
	for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
		struct fixed_rsrc_table *table;
		struct file *f = *io_fixed_file_slot(ctx->file_data, i);
		struct file *f;


		table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
		f = table->files[i & IORING_FILE_TABLE_MASK];
		if (f)
		if (f)
			seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
			seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
		else
		else