Skip to content

Commit 5398ae6

Browse files
isilenceaxboe
authored andcommitted
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>
1 parent 692d836 commit 5398ae6

1 file changed

Lines changed: 33 additions & 36 deletions

File tree

fs/io_uring.c

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7099,13 +7099,13 @@ static int io_sqe_files_scm(struct io_ring_ctx *ctx)
70997099
}
71007100
#endif
71017101

7102-
static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7103-
unsigned nr_files)
7102+
static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7103+
unsigned nr_tables, unsigned nr_files)
71047104
{
71057105
int i;
71067106

71077107
for (i = 0; i < nr_tables; i++) {
7108-
struct fixed_file_table *table = &ctx->file_data->table[i];
7108+
struct fixed_file_table *table = &file_data->table[i];
71097109
unsigned this_files;
71107110

71117111
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,
71207120
return 0;
71217121

71227122
for (i = 0; i < nr_tables; i++) {
7123-
struct fixed_file_table *table = &ctx->file_data->table[i];
7123+
struct fixed_file_table *table = &file_data->table[i];
71247124
kfree(table->files);
71257125
}
71267126
return 1;
@@ -7287,6 +7287,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
72877287
int fd, ret = 0;
72887288
unsigned i;
72897289
struct fixed_file_ref_node *ref_node;
7290+
struct fixed_file_data *file_data;
72907291

72917292
if (ctx->file_data)
72927293
return -EBUSY;
@@ -7295,37 +7296,33 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
72957296
if (nr_args > IORING_MAX_FIXED_FILES)
72967297
return -EMFILE;
72977298

7298-
ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7299-
if (!ctx->file_data)
7299+
file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7300+
if (!file_data)
73007301
return -ENOMEM;
7301-
ctx->file_data->ctx = ctx;
7302-
init_completion(&ctx->file_data->done);
7303-
INIT_LIST_HEAD(&ctx->file_data->ref_list);
7304-
spin_lock_init(&ctx->file_data->lock);
7302+
file_data->ctx = ctx;
7303+
init_completion(&file_data->done);
7304+
INIT_LIST_HEAD(&file_data->ref_list);
7305+
spin_lock_init(&file_data->lock);
73057306

73067307
nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7307-
ctx->file_data->table = kcalloc(nr_tables,
7308-
sizeof(struct fixed_file_table),
7309-
GFP_KERNEL);
7310-
if (!ctx->file_data->table) {
7311-
kfree(ctx->file_data);
7312-
ctx->file_data = NULL;
7308+
file_data->table = kcalloc(nr_tables, sizeof(file_data->table),
7309+
GFP_KERNEL);
7310+
if (!file_data->table) {
7311+
kfree(file_data);
73137312
return -ENOMEM;
73147313
}
73157314

7316-
if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
7315+
if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
73177316
PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7318-
kfree(ctx->file_data->table);
7319-
kfree(ctx->file_data);
7320-
ctx->file_data = NULL;
7317+
kfree(file_data->table);
7318+
kfree(file_data);
73217319
return -ENOMEM;
73227320
}
73237321

7324-
if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7325-
percpu_ref_exit(&ctx->file_data->refs);
7326-
kfree(ctx->file_data->table);
7327-
kfree(ctx->file_data);
7328-
ctx->file_data = NULL;
7322+
if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) {
7323+
percpu_ref_exit(&file_data->refs);
7324+
kfree(file_data->table);
7325+
kfree(file_data);
73297326
return -ENOMEM;
73307327
}
73317328

@@ -7342,7 +7339,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
73427339
continue;
73437340
}
73447341

7345-
table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7342+
table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
73467343
index = i & IORING_FILE_TABLE_MASK;
73477344
file = fget(fd);
73487345

@@ -7372,16 +7369,16 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
73727369
fput(file);
73737370
}
73747371
for (i = 0; i < nr_tables; i++)
7375-
kfree(ctx->file_data->table[i].files);
7372+
kfree(file_data->table[i].files);
73767373

7377-
percpu_ref_exit(&ctx->file_data->refs);
7378-
kfree(ctx->file_data->table);
7379-
kfree(ctx->file_data);
7380-
ctx->file_data = NULL;
7374+
percpu_ref_exit(&file_data->refs);
7375+
kfree(file_data->table);
7376+
kfree(file_data);
73817377
ctx->nr_user_files = 0;
73827378
return ret;
73837379
}
73847380

7381+
ctx->file_data = file_data;
73857382
ret = io_sqe_files_scm(ctx);
73867383
if (ret) {
73877384
io_sqe_files_unregister(ctx);
@@ -7394,11 +7391,11 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
73947391
return PTR_ERR(ref_node);
73957392
}
73967393

7397-
ctx->file_data->cur_refs = &ref_node->refs;
7398-
spin_lock(&ctx->file_data->lock);
7399-
list_add(&ref_node->node, &ctx->file_data->ref_list);
7400-
spin_unlock(&ctx->file_data->lock);
7401-
percpu_ref_get(&ctx->file_data->refs);
7394+
file_data->cur_refs = &ref_node->refs;
7395+
spin_lock(&file_data->lock);
7396+
list_add(&ref_node->node, &file_data->ref_list);
7397+
spin_unlock(&file_data->lock);
7398+
percpu_ref_get(&file_data->refs);
74027399
return ret;
74037400
}
74047401

0 commit comments

Comments
 (0)