Create common routines to be used for both files/buffers registration. Signed-off-by: Bijan Mottahedeh <bijan.mottahedeh@xxxxxxxxxx> --- fs/io_uring.c | 66 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index a51d1e6..384ff3c 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -7287,14 +7287,10 @@ static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) spin_unlock_bh(&ctx->rsrc_ref_lock); } -static int io_sqe_files_unregister(struct io_ring_ctx *ctx) +static void io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, + struct io_ring_ctx *ctx) { - struct fixed_rsrc_data *data = ctx->file_data; struct fixed_rsrc_ref_node *ref_node = NULL; - unsigned nr_tables, i; - - if (!data) - return -ENXIO; io_rsrc_ref_lock(ctx); ref_node = data->node; @@ -7307,6 +7303,17 @@ static int io_sqe_files_unregister(struct io_ring_ctx *ctx) /* wait for all refs nodes to complete */ flush_delayed_work(&ctx->rsrc_put_work); wait_for_completion(&data->done); +} + +static int io_sqe_files_unregister(struct io_ring_ctx *ctx) +{ + struct fixed_rsrc_data *data = ctx->file_data; + unsigned nr_tables, i; + + if (!data) + return -ENXIO; + + io_rsrc_ref_quiesce(data, ctx); __io_sqe_files_unregister(ctx); nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); @@ -7736,6 +7743,17 @@ static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) kfree(ref_node); } +static void add_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node, + struct fixed_rsrc_data *data, + struct io_ring_ctx *ctx) +{ + io_rsrc_ref_lock(ctx); + list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); + data->node = ref_node; + io_rsrc_ref_unlock(ctx); + percpu_ref_get(&data->refs); +} + static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) { @@ -7818,11 +7836,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, return PTR_ERR(ref_node); } - file_data->node = ref_node; - io_rsrc_ref_lock(ctx); - list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); - io_rsrc_ref_unlock(ctx); - percpu_ref_get(&file_data->refs); + add_fixed_rsrc_ref_node(ref_node, file_data, ctx); return ret; out_fput: for (i = 0; i < ctx->nr_user_files; i++) { @@ -7911,10 +7925,7 @@ static void switch_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node, struct io_ring_ctx *ctx) { percpu_ref_kill(&data->node->refs); - io_rsrc_ref_lock(ctx); - list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); - data->node = ref_node; - io_rsrc_ref_unlock(ctx); + add_fixed_rsrc_ref_node(ref_node, data, ctx); } static int __io_sqe_files_update(struct io_ring_ctx *ctx, @@ -7992,10 +8003,9 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, up->offset++; } - if (needs_switch) { + if (needs_switch) switch_fixed_rsrc_ref_node(ref_node, data, ctx); - percpu_ref_get(&ctx->file_data->refs); - } else + else destroy_fixed_rsrc_ref_node(ref_node); return done ? done : err; @@ -8358,23 +8368,11 @@ static void io_buffers_map_free(struct io_ring_ctx *ctx) static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) { struct fixed_rsrc_data *data = ctx->buf_data; - struct fixed_rsrc_ref_node *ref_node = NULL; if (!data) return -ENXIO; - io_rsrc_ref_lock(ctx); - ref_node = data->node; - io_rsrc_ref_unlock(ctx); - if (ref_node) - percpu_ref_kill(&ref_node->refs); - - percpu_ref_kill(&data->refs); - - /* wait for all refs nodes to complete */ - flush_delayed_work(&ctx->rsrc_put_work); - wait_for_completion(&data->done); - + io_rsrc_ref_quiesce(data, ctx); io_buffers_unmap(ctx); io_buffers_map_free(ctx); @@ -8731,11 +8729,7 @@ static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, return PTR_ERR(ref_node); } - buf_data->node = ref_node; - io_rsrc_ref_lock(ctx); - list_add(&ref_node->node, &ctx->rsrc_ref_list); - io_rsrc_ref_unlock(ctx); - percpu_ref_get(&buf_data->refs); + add_fixed_rsrc_ref_node(ref_node, buf_data, ctx); return 0; } -- 1.8.3.1