io_uring: clean file_data access in files_register
Keep file_data in a local var and replace with it complex references such as ctx->file_data. Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
parent
692d836351
commit
5398ae6985
@ -7099,13 +7099,13 @@ static int io_sqe_files_scm(struct io_ring_ctx *ctx)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
|
static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
|
||||||
unsigned nr_files)
|
unsigned nr_tables, unsigned nr_files)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < nr_tables; i++) {
|
for (i = 0; i < nr_tables; i++) {
|
||||||
struct fixed_file_table *table = &ctx->file_data->table[i];
|
struct fixed_file_table *table = &file_data->table[i];
|
||||||
unsigned this_files;
|
unsigned this_files;
|
||||||
|
|
||||||
this_files = min(nr_files, IORING_MAX_FILES_TABLE);
|
this_files = min(nr_files, IORING_MAX_FILES_TABLE);
|
||||||
@ -7120,7 +7120,7 @@ static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for (i = 0; i < nr_tables; i++) {
|
for (i = 0; i < nr_tables; i++) {
|
||||||
struct fixed_file_table *table = &ctx->file_data->table[i];
|
struct fixed_file_table *table = &file_data->table[i];
|
||||||
kfree(table->files);
|
kfree(table->files);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -7287,6 +7287,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
|
|||||||
int fd, ret = 0;
|
int fd, ret = 0;
|
||||||
unsigned i;
|
unsigned i;
|
||||||
struct fixed_file_ref_node *ref_node;
|
struct fixed_file_ref_node *ref_node;
|
||||||
|
struct fixed_file_data *file_data;
|
||||||
|
|
||||||
if (ctx->file_data)
|
if (ctx->file_data)
|
||||||
return -EBUSY;
|
return -EBUSY;
|
||||||
@ -7295,37 +7296,33 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
|
|||||||
if (nr_args > IORING_MAX_FIXED_FILES)
|
if (nr_args > IORING_MAX_FIXED_FILES)
|
||||||
return -EMFILE;
|
return -EMFILE;
|
||||||
|
|
||||||
ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
|
file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
|
||||||
if (!ctx->file_data)
|
if (!file_data)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
ctx->file_data->ctx = ctx;
|
file_data->ctx = ctx;
|
||||||
init_completion(&ctx->file_data->done);
|
init_completion(&file_data->done);
|
||||||
INIT_LIST_HEAD(&ctx->file_data->ref_list);
|
INIT_LIST_HEAD(&file_data->ref_list);
|
||||||
spin_lock_init(&ctx->file_data->lock);
|
spin_lock_init(&file_data->lock);
|
||||||
|
|
||||||
nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
|
nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
|
||||||
ctx->file_data->table = kcalloc(nr_tables,
|
file_data->table = kcalloc(nr_tables, sizeof(file_data->table),
|
||||||
sizeof(struct fixed_file_table),
|
|
||||||
GFP_KERNEL);
|
GFP_KERNEL);
|
||||||
if (!ctx->file_data->table) {
|
if (!file_data->table) {
|
||||||
kfree(ctx->file_data);
|
kfree(file_data);
|
||||||
ctx->file_data = NULL;
|
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (percpu_ref_init(&ctx->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(ctx->file_data->table);
|
kfree(file_data->table);
|
||||||
kfree(ctx->file_data);
|
kfree(file_data);
|
||||||
ctx->file_data = NULL;
|
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
|
if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) {
|
||||||
percpu_ref_exit(&ctx->file_data->refs);
|
percpu_ref_exit(&file_data->refs);
|
||||||
kfree(ctx->file_data->table);
|
kfree(file_data->table);
|
||||||
kfree(ctx->file_data);
|
kfree(file_data);
|
||||||
ctx->file_data = NULL;
|
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -7342,7 +7339,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
|
table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
|
||||||
index = i & IORING_FILE_TABLE_MASK;
|
index = i & IORING_FILE_TABLE_MASK;
|
||||||
file = fget(fd);
|
file = fget(fd);
|
||||||
|
|
||||||
@ -7372,16 +7369,16 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
|
|||||||
fput(file);
|
fput(file);
|
||||||
}
|
}
|
||||||
for (i = 0; i < nr_tables; i++)
|
for (i = 0; i < nr_tables; i++)
|
||||||
kfree(ctx->file_data->table[i].files);
|
kfree(file_data->table[i].files);
|
||||||
|
|
||||||
percpu_ref_exit(&ctx->file_data->refs);
|
percpu_ref_exit(&file_data->refs);
|
||||||
kfree(ctx->file_data->table);
|
kfree(file_data->table);
|
||||||
kfree(ctx->file_data);
|
kfree(file_data);
|
||||||
ctx->file_data = NULL;
|
|
||||||
ctx->nr_user_files = 0;
|
ctx->nr_user_files = 0;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx->file_data = file_data;
|
||||||
ret = io_sqe_files_scm(ctx);
|
ret = io_sqe_files_scm(ctx);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
io_sqe_files_unregister(ctx);
|
io_sqe_files_unregister(ctx);
|
||||||
@ -7394,11 +7391,11 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
|
|||||||
return PTR_ERR(ref_node);
|
return PTR_ERR(ref_node);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->file_data->cur_refs = &ref_node->refs;
|
file_data->cur_refs = &ref_node->refs;
|
||||||
spin_lock(&ctx->file_data->lock);
|
spin_lock(&file_data->lock);
|
||||||
list_add(&ref_node->node, &ctx->file_data->ref_list);
|
list_add(&ref_node->node, &file_data->ref_list);
|
||||||
spin_unlock(&ctx->file_data->lock);
|
spin_unlock(&file_data->lock);
|
||||||
percpu_ref_get(&ctx->file_data->refs);
|
percpu_ref_get(&file_data->refs);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user