Commit 600cf3f8 authored by Pavel Begunkov's avatar Pavel Begunkov Committed by Jens Axboe

io_uring: refactor *files_register()'s error paths

Don't keep repeating cleaning sequences in error paths, write it once
in the and use labels. It's less error prone and looks cleaner.
Signed-off-by: default avatarPavel Begunkov <asml.silence@gmail.com>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 5398ae69
...@@ -7282,10 +7282,9 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, ...@@ -7282,10 +7282,9 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
unsigned nr_args) unsigned nr_args)
{ {
__s32 __user *fds = (__s32 __user *) arg; __s32 __user *fds = (__s32 __user *) arg;
unsigned nr_tables; unsigned nr_tables, i;
struct file *file; struct file *file;
int fd, ret = 0; int fd, ret = -ENOMEM;
unsigned i;
struct fixed_file_ref_node *ref_node; struct fixed_file_ref_node *ref_node;
struct fixed_file_data *file_data; struct fixed_file_data *file_data;
...@@ -7307,45 +7306,32 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, ...@@ -7307,45 +7306,32 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
file_data->table = kcalloc(nr_tables, sizeof(file_data->table), file_data->table = kcalloc(nr_tables, sizeof(file_data->table),
GFP_KERNEL); GFP_KERNEL);
if (!file_data->table) { if (!file_data->table)
kfree(file_data); goto out_free;
return -ENOMEM;
}
if (percpu_ref_init(&file_data->refs, io_file_ref_kill, if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
kfree(file_data->table); goto out_free;
kfree(file_data);
return -ENOMEM;
}
if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) { if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
percpu_ref_exit(&file_data->refs); goto out_ref;
kfree(file_data->table);
kfree(file_data);
return -ENOMEM;
}
for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
struct fixed_file_table *table; struct fixed_file_table *table;
unsigned index; unsigned index;
ret = -EFAULT; if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
if (copy_from_user(&fd, &fds[i], sizeof(fd))) ret = -EFAULT;
break; goto out_fput;
}
/* allow sparse sets */ /* allow sparse sets */
if (fd == -1) { if (fd == -1)
ret = 0;
continue; continue;
}
table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
index = i & IORING_FILE_TABLE_MASK;
file = fget(fd); file = fget(fd);
ret = -EBADF; ret = -EBADF;
if (!file) if (!file)
break; goto out_fput;
/* /*
* Don't allow io_uring instances to be registered. If UNIX * Don't allow io_uring instances to be registered. If UNIX
...@@ -7356,28 +7342,13 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, ...@@ -7356,28 +7342,13 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
*/ */
if (file->f_op == &io_uring_fops) { if (file->f_op == &io_uring_fops) {
fput(file); fput(file);
break; goto out_fput;
} }
ret = 0; table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
index = i & IORING_FILE_TABLE_MASK;
table->files[index] = file; table->files[index] = file;
} }
if (ret) {
for (i = 0; i < ctx->nr_user_files; i++) {
file = io_file_from_index(ctx, i);
if (file)
fput(file);
}
for (i = 0; i < nr_tables; i++)
kfree(file_data->table[i].files);
percpu_ref_exit(&file_data->refs);
kfree(file_data->table);
kfree(file_data);
ctx->nr_user_files = 0;
return ret;
}
ctx->file_data = file_data; ctx->file_data = file_data;
ret = io_sqe_files_scm(ctx); ret = io_sqe_files_scm(ctx);
if (ret) { if (ret) {
...@@ -7397,6 +7368,21 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, ...@@ -7397,6 +7368,21 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
spin_unlock(&file_data->lock); spin_unlock(&file_data->lock);
percpu_ref_get(&file_data->refs); percpu_ref_get(&file_data->refs);
return ret; return ret;
out_fput:
for (i = 0; i < ctx->nr_user_files; i++) {
file = io_file_from_index(ctx, i);
if (file)
fput(file);
}
for (i = 0; i < nr_tables; i++)
kfree(file_data->table[i].files);
ctx->nr_user_files = 0;
out_ref:
percpu_ref_exit(&file_data->refs);
out_free:
kfree(file_data->table);
kfree(file_data);
return ret;
} }
static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment