ctx wide rsrc nodes
v2 cover ctx wide rsrc nodes
The main idea here is to make make rsrc nodes (aka ref nodes) to be per ctx rather than per rsrc_data, that is a requirement for having multiple resource types. All the meat to it in 7/7.
Also rsrc API is complicated and too misuse. 1-6 may be considered to be preps but also together with 7/7 gradually make the API simpler and so more resilient.
v2: io_rsrc_node_destroy() last rsrc_node on ctx_free()
Pavel Begunkov (7):
io_uring: name rsrc bits consistently
io_uring: simplify io_rsrc_node_ref_zero
io_uring: use rsrc prealloc infra for files reg
io_uring: encapsulate rsrc node manipulations
io_uring: move rsrc_put callback into io_rsrc_data
io_uring: refactor io_queue_rsrc_removal()
io_uring: ctx-wide rsrc nodes
fs/io_uring.c | 237 +++++++++++++++++++++----------------------------- 1 file changed, 97 insertions(+), 140 deletions(-)
Signature · 1 line
2.24.0
PATCH v2 1/7 io_uring: name rsrc bits consistently
Keep resource related structs' and functions' naming consistent, in particular use "io_rsrc" prefix for everything.
--- fs/io_uring.c | 150 ++++++++++++++++++++++++-------------------------- 1 file changed, 71 insertions(+), 79 deletions(-)
fs/io_uring.c +71 -79
@@ -215,22 +215,22 @@ struct fixed_rsrc_table {struct file **files; }; -struct fixed_rsrc_ref_node { +struct io_rsrc_node {struct percpu_ref refs;struct list_head node;struct list_head rsrc_list; -struct fixed_rsrc_data *rsrc_data; +struct io_rsrc_data *rsrc_data;void (*rsrc_put)(struct io_ring_ctx *ctx,struct io_rsrc_put *prsrc);struct llist_node llist;bool done; }; -struct fixed_rsrc_data { +struct io_rsrc_data {struct fixed_rsrc_table *table;struct io_ring_ctx *ctx; -struct fixed_rsrc_ref_node *node; +struct io_rsrc_node *node;struct percpu_ref refs;struct completion done;bool quiesce;
@@ -389,7 +389,7 @@ struct io_ring_ctx {* readers must ensure that ->refs is alive as long as the file* is * used. Only updated through io_uring_register(2). */ -struct fixed_rsrc_data *file_data; +struct io_rsrc_data *file_data;unsigned nr_user_files;/* if used, fixed mapped user buffers */
@@ -443,7 +443,7 @@ struct io_ring_ctx {struct llist_head rsrc_put_llist;struct list_head rsrc_ref_list;spinlock_t rsrc_ref_lock; -struct fixed_rsrc_ref_node *rsrc_backup_node; +struct io_rsrc_node *rsrc_backup_node;struct io_restriction restrictions;
@@ -1012,9 +1012,8 @@ static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,struct task_struct *task, struct files_struct *files);static void io_uring_cancel_sqpoll (struct io_ring_ctx *ctx); -static void destroy_fixed_rsrc_ref_node (struct fixed_rsrc_ref_node *ref_node); -static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node ( -struct io_ring_ctx *ctx); +static void io_rsrc_node_destroy (struct io_rsrc_node *ref_node); +static struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx);static void io_ring_file_put (struct io_ring_ctx *ctx,struct io_rsrc_put *prsrc);static void io_cqring_fill_event (struct io_kiocb *req,long res);
@@ -1059,7 +1058,7 @@ EXPORT_SYMBOL(io_uring_get_socket);#define io_for_each_link (pos, head) \ for (pos = (head); pos; pos = pos->link) -static inline void io_set_resource_node (struct io_kiocb *req) +static inline void io_req_set_rsrc_node (struct io_kiocb *req) {struct io_ring_ctx *ctx = req->ctx;
@@ -6159,7 +6158,7 @@ static void io_wq_submit_work(struct io_wq_work *work)#endif #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) -static inline struct file **io_fixed_file_slot (struct fixed_rsrc_data *file_data, +static inline struct file **io_fixed_file_slot (struct io_rsrc_data *file_data,unsigned i) {struct fixed_rsrc_table *table;
@@ -6193,7 +6192,7 @@ static struct file *io_file_get(struct io_submit_state *state, file_ptr &= ~FFS_MASK ;/* mask in overlapping REQ_F and FFS bits */ req->flags |= (file_ptr <<REQ_F_ASYNC_READ_BIT ); -io_set_resource_node (req); +io_req_set_rsrc_node (req); }else {trace_io_uring_file_get (ctx, fd); file =__io_file_get (state, fd);
@@ -6943,9 +6942,8 @@ static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)static void io_rsrc_data_ref_zero (struct percpu_ref *ref) { -struct fixed_rsrc_data *data; + struct io_rsrc_data *data = container_of(ref, struct io_rsrc_data, refs); - data = container_of(ref, struct fixed_rsrc_data, refs);complete (&data->done); }
@@ -6959,20 +6957,20 @@ static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)spin_unlock_bh (&ctx->rsrc_ref_lock); } -static void io_sqe_rsrc_set_node (struct io_ring_ctx *ctx, -struct fixed_rsrc_data *rsrc_data, -struct fixed_rsrc_ref_node *ref_node) +static void io_rsrc_node_set (struct io_ring_ctx *ctx, +struct io_rsrc_data *rsrc_data, +struct io_rsrc_node *rsrc_node) {io_rsrc_ref_lock (ctx); - rsrc_data->node = ref_node; -list_add_tail (&ref_node->node, &ctx->rsrc_ref_list); + rsrc_data->node = rsrc_node; +list_add_tail (&rsrc_node->node, &ctx->rsrc_ref_list);io_rsrc_ref_unlock (ctx);percpu_ref_get (&rsrc_data->refs); } -static void io_sqe_rsrc_kill_node (struct io_ring_ctx *ctx,struct fixed_rsrc_data *data) +static void io_rsrc_node_kill (struct io_ring_ctx *ctx,struct io_rsrc_data *data) { -struct fixed_rsrc_ref_node *ref_node =NULL ; +struct io_rsrc_node *ref_node =NULL ;io_rsrc_ref_lock (ctx); ref_node = data->node;
@@ -6982,21 +6980,21 @@ static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_datpercpu_ref_kill (&ref_node->refs); } -static int io_rsrc_refnode_prealloc (struct io_ring_ctx *ctx) +static int io_rsrc_node_prealloc (struct io_ring_ctx *ctx) {if (ctx->rsrc_backup_node)return 0 ; - ctx->rsrc_backup_node =alloc_fixed_rsrc_ref_node (ctx); + ctx->rsrc_backup_node =io_rsrc_node_alloc (ctx);return ctx->rsrc_backup_node ?0 : -ENOMEM ; } -static struct fixed_rsrc_ref_node * -io_rsrc_refnode_get (struct io_ring_ctx *ctx, -struct fixed_rsrc_data *rsrc_data, -void (*rsrc_put)(struct io_ring_ctx *ctx, -struct io_rsrc_put *prsrc)) +static struct io_rsrc_node * +io_rsrc_node_get (struct io_ring_ctx *ctx, +struct io_rsrc_data *rsrc_data, +void (*rsrc_put)(struct io_ring_ctx *ctx, +struct io_rsrc_put *prsrc)) { -struct fixed_rsrc_ref_node *node = ctx->rsrc_backup_node; +struct io_rsrc_node *node = ctx->rsrc_backup_node;WARN_ON_ONCE (!node);
@@ -7006,12 +7004,12 @@ io_rsrc_refnode_get(struct io_ring_ctx *ctx,return node; } -static int io_rsrc_ref_quiesce (struct fixed_rsrc_data *data, +static int io_rsrc_ref_quiesce (struct io_rsrc_data *data,struct io_ring_ctx *ctx,void (*rsrc_put)(struct io_ring_ctx *ctx,struct io_rsrc_put *prsrc)) { -struct fixed_rsrc_ref_node *node; +struct io_rsrc_node *node;int ret;if (data->quiesce)
@@ -7019,10 +7017,10 @@ static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, data->quiesce = true; do { - ret = io_rsrc_refnode_prealloc(ctx); + ret = io_rsrc_node_prealloc(ctx); if (ret) break; - io_sqe_rsrc_kill_node(ctx, data); + io_rsrc_node_kill(ctx, data); percpu_ref_kill(&data->refs); flush_delayed_work(&ctx->rsrc_put_work);
@@ -7031,8 +7029,8 @@ static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,break ;percpu_ref_resurrect (&data->refs); - node =io_rsrc_refnode_get (ctx, data, rsrc_put); -io_sqe_rsrc_set_node (ctx, data, node); + node =io_rsrc_node_get (ctx, data, rsrc_put); +io_rsrc_node_set (ctx, data, node);reinit_completion (&data->done);mutex_unlock (&ctx->uring_lock);
@@ -7044,9 +7042,9 @@ static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,return ret; } -static struct fixed_rsrc_data *alloc_fixed_rsrc_data (struct io_ring_ctx *ctx) +static struct io_rsrc_data *io_rsrc_data_alloc (struct io_ring_ctx *ctx) { -struct fixed_rsrc_data *data; +struct io_rsrc_data *data; data =kzalloc (sizeof (*data),GFP_KERNEL );if (!data)
@@ -7062,7 +7060,7 @@ static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)return data; } -static void free_fixed_rsrc_data (struct fixed_rsrc_data *data) +static void io_rsrc_data_free (struct io_rsrc_data *data) {percpu_ref_exit (&data->refs);kfree (data->table);
@@ -7071,7 +7069,7 @@ static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)static int io_sqe_files_unregister (struct io_ring_ctx *ctx) { -struct fixed_rsrc_data *data = ctx->file_data; +struct io_rsrc_data *data = ctx->file_data;unsigned nr_tables, i;int ret;
@@ -7090,7 +7088,7 @@ static int io_sqe_files_unregister(struct io_ring_ctx *ctx) nr_tables =DIV_ROUND_UP (ctx->nr_user_files,IORING_MAX_FILES_TABLE );for (i =0 ; i < nr_tables; i++)kfree (data->table[i].files); -free_fixed_rsrc_data (data); +io_rsrc_data_free (data); ctx->file_data =NULL ; ctx->nr_user_files =0 ;return 0 ;
@@ -7319,7 +7317,7 @@ static int io_sqe_files_scm(struct io_ring_ctx *ctx) }#endif -static int io_sqe_alloc_file_tables (struct fixed_rsrc_data *file_data, +static int io_sqe_alloc_file_tables (struct io_rsrc_data *file_data,unsigned nr_tables,unsigned nr_files) {int i;
@@ -7409,9 +7407,9 @@ static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)#endif } -static void __io_rsrc_put_work (struct fixed_rsrc_ref_node *ref_node) +static void __io_rsrc_put_work (struct io_rsrc_node *ref_node) { -struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; +struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;struct io_ring_ctx *ctx = rsrc_data->ctx;struct io_rsrc_put *prsrc, *tmp;
@@ -7435,10 +7433,10 @@ static void io_rsrc_put_work(struct work_struct *work) node =llist_del_all (&ctx->rsrc_put_llist);while (node) { -struct fixed_rsrc_ref_node *ref_node; +struct io_rsrc_node *ref_node;struct llist_node *next = node->next; - ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); + ref_node = llist_entry(node, struct io_rsrc_node, llist);__io_rsrc_put_work (ref_node); node = next; }
@@ -7446,27 +7444,23 @@ static void io_rsrc_put_work(struct work_struct *work)static void io_rsrc_node_ref_zero (struct percpu_ref *ref) { -struct fixed_rsrc_ref_node *ref_node; -struct fixed_rsrc_data *data; -struct io_ring_ctx *ctx; + struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); +struct io_rsrc_data *data = node->rsrc_data; +struct io_ring_ctx *ctx = data->ctx;bool first_add = false;int delay =HZ ; - ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); - data = ref_node->rsrc_data; - ctx = data->ctx; -io_rsrc_ref_lock (ctx); - ref_node->done = true; + node->done = true;while (!list_empty (&ctx->rsrc_ref_list)) { - ref_node =list_first_entry (&ctx->rsrc_ref_list, - struct fixed_rsrc_ref_node, node); + node =list_first_entry (&ctx->rsrc_ref_list, + struct io_rsrc_node, node);/* recycle ref nodes in order */ -if (!ref_node->done) +if (!node->done)break ; -list_del (&ref_node->node); - first_add |=llist_add (&ref_node->llist, &ctx->rsrc_put_llist); +list_del (&node->node); + first_add |=llist_add (&node->llist, &ctx->rsrc_put_llist); }io_rsrc_ref_unlock (ctx);
@@ -7479,10 +7473,9 @@ static void io_rsrc_node_ref_zero(struct percpu_ref *ref)queue_delayed_work (system_wq, &ctx->rsrc_put_work, delay); } -static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node ( -struct io_ring_ctx *ctx) +static struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx) { -struct fixed_rsrc_ref_node *ref_node; +struct io_rsrc_node *ref_node; ref_node =kzalloc (sizeof (*ref_node),GFP_KERNEL );if (!ref_node)
@@ -7500,19 +7493,18 @@ static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( }static void init_fixed_file_ref_node (struct io_ring_ctx *ctx, -struct fixed_rsrc_ref_node *ref_node) +struct io_rsrc_node *ref_node) { ref_node->rsrc_data = ctx->file_data; ref_node->rsrc_put = io_ring_file_put; } -static void destroy_fixed_rsrc_ref_node (struct fixed_rsrc_ref_node *ref_node) +static void io_rsrc_node_destroy (struct io_rsrc_node *ref_node) {percpu_ref_exit (&ref_node->refs);kfree (ref_node); } -static int io_sqe_files_register (struct io_ring_ctx *ctx,void __user *arg,unsigned nr_args) {
@@ -7520,8 +7512,8 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,unsigned nr_tables, i;struct file *file;int fd, ret = -ENOMEM ; -struct fixed_rsrc_ref_node *ref_node; -struct fixed_rsrc_data *file_data; +struct io_rsrc_node *ref_node; +struct io_rsrc_data *file_data;if (ctx->file_data)return -EBUSY ;
@@ -7530,7 +7522,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,if (nr_args >IORING_MAX_FIXED_FILES )return -EMFILE ; - file_data =alloc_fixed_rsrc_data (ctx); + file_data =io_rsrc_data_alloc (ctx);if (!file_data)return -ENOMEM ; ctx->file_data = file_data;
@@ -7587,14 +7579,14 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } - ref_node =alloc_fixed_rsrc_ref_node (ctx); + ref_node =io_rsrc_node_alloc (ctx);if (!ref_node) {io_sqe_files_unregister (ctx);return -ENOMEM ; }init_fixed_file_ref_node (ctx, ref_node); -io_sqe_rsrc_set_node (ctx, file_data, ref_node); +io_rsrc_node_set (ctx, file_data, ref_node);return ret; out_fput:for (i =0 ; i < ctx->nr_user_files; i++) {
@@ -7606,7 +7598,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,kfree (file_data->table[i].files); ctx->nr_user_files =0 ; out_free: -free_fixed_rsrc_data (ctx->file_data); +io_rsrc_data_free (ctx->file_data); ctx->file_data =NULL ;return ret; }
@@ -7654,10 +7646,10 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,#endif } -static int io_queue_rsrc_removal (struct fixed_rsrc_data *data,void *rsrc) +static int io_queue_rsrc_removal (struct io_rsrc_data *data,void *rsrc) {struct io_rsrc_put *prsrc; -struct fixed_rsrc_ref_node *ref_node = data->node; +struct io_rsrc_node *ref_node = data->node; prsrc =kzalloc (sizeof (*prsrc),GFP_KERNEL );if (!prsrc)
@@ -7669,7 +7661,7 @@ static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)return 0 ; } -static inline int io_queue_file_removal (struct fixed_rsrc_data *data, +static inline int io_queue_file_removal (struct io_rsrc_data *data,struct file *file) {return io_queue_rsrc_removal (data, (void *)file);
@@ -7679,8 +7671,8 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, struct io_uring_rsrc_update *up, unsigned nr_args) { -struct fixed_rsrc_data *data = ctx->file_data; -struct fixed_rsrc_ref_node *ref_node; +struct io_rsrc_data *data = ctx->file_data; +struct io_rsrc_node *ref_node;struct file *file, **file_slot;__s32 __user *fds;int fd, i, err;
@@ -7691,7 +7683,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,return -EOVERFLOW ;if (done > ctx->nr_user_files)return -EINVAL ; - err =io_rsrc_refnode_prealloc (ctx); + err =io_rsrc_node_prealloc (ctx);if (err)return err;
@@ -7747,8 +7739,8 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,if (needs_switch) {percpu_ref_kill (&data->node->refs); - ref_node =io_rsrc_refnode_get (ctx, data, io_ring_file_put); -io_sqe_rsrc_set_node (ctx, data, ref_node); + ref_node =io_rsrc_node_get (ctx, data, io_ring_file_put); +io_rsrc_node_set (ctx, data, ref_node); }return done ? done : err; }
@@ -8421,7 +8413,7 @@ static void io_ring_ctx_free(struct io_ring_ctx *ctx)io_destroy_buffers (ctx);if (ctx->rsrc_backup_node) -destroy_fixed_rsrc_ref_node (ctx->rsrc_backup_node); +io_rsrc_node_destroy (ctx->rsrc_backup_node);#if defined(CONFIG_UNIX )if (ctx->ring_sock) {
Signature · 1 line
2.24.0
PATCH v2 2/7 io_uring: simplify io_rsrc_node_ref_zero
In reply to Pavel BegunkovReplace queue_delayed_work() with mod_delayed_work() in io_rsrc_node_ref_zero() as the later one can schedule a new work, and cleanup it further for better readability.
--- fs/io_uring.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
fs/io_uring.c +4 -8
@@ -7448,7 +7448,7 @@ static void io_rsrc_node_ref_zero(struct percpu_ref *ref)struct io_rsrc_data *data = node->rsrc_data;struct io_ring_ctx *ctx = data->ctx;bool first_add = false; -int delay =HZ ; +int delay;io_rsrc_ref_lock (ctx); node->done = true;
@@ -7464,13 +7464,9 @@ static void io_rsrc_node_ref_zero(struct percpu_ref *ref) }io_rsrc_ref_unlock (ctx); -if (percpu_ref_is_dying (&data->refs)) - delay =0 ; - -if (!delay) -mod_delayed_work (system_wq, &ctx->rsrc_put_work,0 ); -else if (first_add) -queue_delayed_work (system_wq, &ctx->rsrc_put_work, delay); + delay =percpu_ref_is_dying (&data->refs) ?0 :HZ ; +if (first_add || !delay) +mod_delayed_work (system_wq, &ctx->rsrc_put_work, delay); }static struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx)
Signature · 1 line
2.24.0
PATCH v2 3/7 io_uring: use rsrc prealloc infra for files reg
In reply to Pavel BegunkovKeep it consistent with update and use io_rsrc_node_prealloc() + io_rsrc_node_get() in io_sqe_files_register() as well, that will be used in future patches, not as error prone and allows to deduplicate rsrc_node init.
--- fs/io_uring.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-)
fs/io_uring.c +6 -15
@@ -7488,13 +7488,6 @@ static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)return ref_node; } -static void init_fixed_file_ref_node (struct io_ring_ctx *ctx, -struct io_rsrc_node *ref_node) -{ - ref_node->rsrc_data = ctx->file_data; - ref_node->rsrc_put = io_ring_file_put; -} -static void io_rsrc_node_destroy (struct io_rsrc_node *ref_node) {percpu_ref_exit (&ref_node->refs);
@@ -7507,7 +7500,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,__s32 __user *fds = (__s32 __user *) arg;unsigned nr_tables, i;struct file *file; -int fd, ret = -ENOMEM ; +int fd, ret;struct io_rsrc_node *ref_node;struct io_rsrc_data *file_data;
@@ -7517,12 +7510,16 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return -EINVAL ;if (nr_args >IORING_MAX_FIXED_FILES )return -EMFILE ; + ret =io_rsrc_node_prealloc (ctx); +if (ret) +return ret; file_data =io_rsrc_data_alloc (ctx);if (!file_data)return -ENOMEM ; ctx->file_data = file_data; + ret = -ENOMEM ; nr_tables =DIV_ROUND_UP (nr_args,IORING_MAX_FILES_TABLE ); file_data->table =kcalloc (nr_tables,sizeof (*file_data->table),GFP_KERNEL );
@@ -7575,13 +7572,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } - ref_node =io_rsrc_node_alloc (ctx); -if (!ref_node) { -io_sqe_files_unregister (ctx); -return -ENOMEM ; - } -init_fixed_file_ref_node (ctx, ref_node); - + ref_node =io_rsrc_node_get (ctx, ctx->file_data, io_ring_file_put);io_rsrc_node_set (ctx, file_data, ref_node);return ret; out_fput:
Signature · 1 line
2.24.0
PATCH v2 4/7 io_uring: encapsulate rsrc node manipulations
In reply to Pavel Begunkovio_rsrc_node_get() and io_rsrc_node_set() are always used together, merge them into one so most users don't even see io_rsrc_node and don't need to care about it.
It helped to catch io_sqe_files_register() inferring rsrc data argument for get and set differently, not a problem but a good sign.
--- fs/io_uring.c | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-)
fs/io_uring.c +13 -26
@@ -6959,8 +6959,17 @@ static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)static void io_rsrc_node_set (struct io_ring_ctx *ctx,struct io_rsrc_data *rsrc_data, -struct io_rsrc_node *rsrc_node) +void (*rsrc_put)(struct io_ring_ctx *ctx, +struct io_rsrc_put *prsrc)) { +struct io_rsrc_node *rsrc_node = ctx->rsrc_backup_node; + +WARN_ON_ONCE (!rsrc_node); + + ctx->rsrc_backup_node =NULL ; + rsrc_node->rsrc_data = rsrc_data; + rsrc_node->rsrc_put = rsrc_put; +io_rsrc_ref_lock (ctx); rsrc_data->node = rsrc_node;list_add_tail (&rsrc_node->node, &ctx->rsrc_ref_list);
@@ -6988,28 +6997,11 @@ static int io_rsrc_node_prealloc(struct io_ring_ctx *ctx)return ctx->rsrc_backup_node ?0 : -ENOMEM ; } -static struct io_rsrc_node * -io_rsrc_node_get (struct io_ring_ctx *ctx, -struct io_rsrc_data *rsrc_data, -void (*rsrc_put)(struct io_ring_ctx *ctx, -struct io_rsrc_put *prsrc)) -{ -struct io_rsrc_node *node = ctx->rsrc_backup_node; - -WARN_ON_ONCE (!node); - - ctx->rsrc_backup_node =NULL ; - node->rsrc_data = rsrc_data; - node->rsrc_put = rsrc_put; -return node; -} -static int io_rsrc_ref_quiesce (struct io_rsrc_data *data,struct io_ring_ctx *ctx,void (*rsrc_put)(struct io_ring_ctx *ctx,struct io_rsrc_put *prsrc)) { -struct io_rsrc_node *node;int ret;if (data->quiesce)
@@ -7029,8 +7021,7 @@ static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,break ;percpu_ref_resurrect (&data->refs); - node =io_rsrc_node_get (ctx, data, rsrc_put); -io_rsrc_node_set (ctx, data, node); +io_rsrc_node_set (ctx, data, rsrc_put);reinit_completion (&data->done);mutex_unlock (&ctx->uring_lock);
@@ -7501,7 +7492,6 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,unsigned nr_tables, i;struct file *file;int fd, ret; -struct io_rsrc_node *ref_node;struct io_rsrc_data *file_data;if (ctx->file_data)
@@ -7572,8 +7562,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } - ref_node =io_rsrc_node_get (ctx, ctx->file_data, io_ring_file_put); -io_rsrc_node_set (ctx, file_data, ref_node); +io_rsrc_node_set (ctx, file_data, io_ring_file_put);return ret; out_fput:for (i =0 ; i < ctx->nr_user_files; i++) {
@@ -7659,7 +7648,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, unsigned nr_args) {struct io_rsrc_data *data = ctx->file_data; -struct io_rsrc_node *ref_node;struct file *file, **file_slot;__s32 __user *fds;int fd, i, err;
@@ -7726,8 +7714,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,if (needs_switch) {percpu_ref_kill (&data->node->refs); - ref_node =io_rsrc_node_get (ctx, data, io_ring_file_put); -io_rsrc_node_set (ctx, data, ref_node); +io_rsrc_node_set (ctx, data, io_ring_file_put); }return done ? done : err; }
Signature · 1 line
2.24.0
PATCH v2 5/7 io_uring: move rsrc_put callback into io_rsrc_data
In reply to Pavel Begunkovio_rsrc_node's callback operates only on a single io_rsrc_data and only with its resources, so rsrc_put() callback is actually a property of io_rsrc_data. Move it there, it makes code much nicecr.
--- fs/io_uring.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)
fs/io_uring.c +14 -17
@@ -220,16 +220,17 @@ struct io_rsrc_node {struct list_head node;struct list_head rsrc_list;struct io_rsrc_data *rsrc_data; -void (*rsrc_put)(struct io_ring_ctx *ctx, -struct io_rsrc_put *prsrc);struct llist_node llist;bool done; }; +typedef void (rsrc_put_fn )(struct io_ring_ctx *ctx,struct io_rsrc_put *prsrc); +struct io_rsrc_data {struct fixed_rsrc_table *table;struct io_ring_ctx *ctx; +rsrc_put_fn *do_put;struct io_rsrc_node *node;struct percpu_ref refs;struct completion done;
@@ -6958,9 +6959,7 @@ static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) }static void io_rsrc_node_set (struct io_ring_ctx *ctx, -struct io_rsrc_data *rsrc_data, -void (*rsrc_put)(struct io_ring_ctx *ctx, -struct io_rsrc_put *prsrc)) +struct io_rsrc_data *rsrc_data) {struct io_rsrc_node *rsrc_node = ctx->rsrc_backup_node;
@@ -6968,7 +6967,6 @@ static void io_rsrc_node_set(struct io_ring_ctx *ctx, ctx->rsrc_backup_node =NULL ; rsrc_node->rsrc_data = rsrc_data; - rsrc_node->rsrc_put = rsrc_put;io_rsrc_ref_lock (ctx); rsrc_data->node = rsrc_node;
@@ -6997,10 +6995,7 @@ static int io_rsrc_node_prealloc(struct io_ring_ctx *ctx)return ctx->rsrc_backup_node ?0 : -ENOMEM ; } -static int io_rsrc_ref_quiesce (struct io_rsrc_data *data, -struct io_ring_ctx *ctx, -void (*rsrc_put)(struct io_ring_ctx *ctx, -struct io_rsrc_put *prsrc)) +static int io_rsrc_ref_quiesce (struct io_rsrc_data *data,struct io_ring_ctx *ctx) {int ret;
@@ -7021,7 +7016,7 @@ static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,break ;percpu_ref_resurrect (&data->refs); -io_rsrc_node_set (ctx, data, rsrc_put); +io_rsrc_node_set (ctx, data);reinit_completion (&data->done);mutex_unlock (&ctx->uring_lock);
@@ -7033,7 +7028,8 @@ static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,return ret; } -static struct io_rsrc_data *io_rsrc_data_alloc (struct io_ring_ctx *ctx) +static struct io_rsrc_data *io_rsrc_data_alloc (struct io_ring_ctx *ctx, +rsrc_put_fn *do_put) {struct io_rsrc_data *data;
@@ -7047,6 +7043,7 @@ static struct io_rsrc_data *io_rsrc_data_alloc(struct io_ring_ctx *ctx)return NULL ; } data->ctx = ctx; + data->do_put = do_put;init_completion (&data->done);return data; }
@@ -7071,7 +7068,7 @@ static int io_sqe_files_unregister(struct io_ring_ctx *ctx)*/ if (!data ||percpu_ref_is_dying (&data->refs))return -ENXIO ; - ret =io_rsrc_ref_quiesce (data, ctx, io_ring_file_put); + ret =io_rsrc_ref_quiesce (data, ctx);if (ret)return ret;
@@ -7406,7 +7403,7 @@ static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)list_for_each_entry_safe (prsrc, tmp, &ref_node->rsrc_list, list) {list_del (&prsrc->list); - ref_node->rsrc_put (ctx, prsrc); + rsrc_data->do_put (ctx, prsrc);kfree (prsrc); }
@@ -7504,7 +7501,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,if (ret)return ret; - file_data =io_rsrc_data_alloc (ctx); + file_data =io_rsrc_data_alloc (ctx, io_ring_file_put);if (!file_data)return -ENOMEM ; ctx->file_data = file_data;
@@ -7562,7 +7559,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } -io_rsrc_node_set (ctx, file_data, io_ring_file_put); +io_rsrc_node_set (ctx, file_data);return ret; out_fput:for (i =0 ; i < ctx->nr_user_files; i++) {
@@ -7714,7 +7711,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,if (needs_switch) {percpu_ref_kill (&data->node->refs); -io_rsrc_node_set (ctx, data, io_ring_file_put); +io_rsrc_node_set (ctx, data); }return done ? done : err; }
Signature · 1 line
2.24.0
PATCH v2 6/7 io_uring: refactor io_queue_rsrc_removal()
In reply to Pavel BegunkovPass rsrc_node into io_queue_rsrc_removal() explicitly. Just a simple preparation patch, makes following changes nicer.
--- fs/io_uring.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
fs/io_uring.c +4 -11
@@ -7619,27 +7619,20 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,#endif } -static int io_queue_rsrc_removal (struct io_rsrc_data *data,void *rsrc) +static int io_queue_rsrc_removal (struct io_rsrc_data *data, +struct io_rsrc_node *node,void *rsrc) {struct io_rsrc_put *prsrc; -struct io_rsrc_node *ref_node = data->node; prsrc =kzalloc (sizeof (*prsrc),GFP_KERNEL );if (!prsrc)return -ENOMEM ; prsrc->rsrc = rsrc; -list_add (&prsrc->list, &ref_node->rsrc_list); - +list_add (&prsrc->list, &node->rsrc_list);return 0 ; } -static inline int io_queue_file_removal (struct io_rsrc_data *data, -struct file *file) -{ -return io_queue_rsrc_removal (data, (void *)file); -} -static int __io_sqe_files_update (struct io_ring_ctx *ctx,struct io_uring_rsrc_update *up,unsigned nr_args)
@@ -7674,7 +7667,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,if (*file_slot) { file = (struct file *) ((unsigned long ) *file_slot &FFS_MASK ); - err =io_queue_file_removal (data, file); + err =io_queue_rsrc_removal (data, data->node, file);if (err)break ; *file_slot =NULL ;
Signature · 1 line
2.24.0
PATCH v2 7/7 io_uring: ctx-wide rsrc nodes
In reply to Pavel BegunkovIf we're going to ever support multiple types of resources we need shared rsrc nodes to not bloat requests, that is implemented in this patch. It also gives a nicer API and saves one pointer dereference in io_req_set_rsrc_node().
We may say that all requests bound to a resource belong to one and only one rsrc node, and considering that nodes are removed and recycled strictly in-order, this separates requests into generations, where generation are changed on each node switch (i.e. io_rsrc_node_switch()).
The API is simple, io_rsrc_node_switch() switches to a new generation if needed, and also optionally kills a passed in io_rsrc_data. Each call to io_rsrc_node_switch() have to be preceded with io_rsrc_node_switch_start(). The start function is idempotent and should not necessarily be followed by switch.
One difference is that once a node was set it will always retain a valid rsrc node, even on unregister. It may be a nuisance at the moment, but makes much sense for multiple types of resources. Another thing changed is that nodes are bound to/associated with a io_rsrc_data later just before killing (i.e. switching).
--- fs/io_uring.c | 73 ++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 36 deletions(-)
fs/io_uring.c +37 -36
@@ -231,7 +231,6 @@ struct io_rsrc_data {struct io_ring_ctx *ctx;rsrc_put_fn *do_put; -struct io_rsrc_node *node;struct percpu_ref refs;struct completion done;bool quiesce;
@@ -444,6 +443,7 @@ struct io_ring_ctx {struct llist_head rsrc_put_llist;struct list_head rsrc_ref_list;spinlock_t rsrc_ref_lock; +struct io_rsrc_node *rsrc_node;struct io_rsrc_node *rsrc_backup_node;struct io_restriction restrictions;
@@ -1064,7 +1064,7 @@ static inline void io_req_set_rsrc_node(struct io_kiocb *req)struct io_ring_ctx *ctx = req->ctx;if (!req->fixed_rsrc_refs) { - req->fixed_rsrc_refs = &ctx->file_data->node->refs; + req->fixed_rsrc_refs = &ctx->rsrc_node->refs;percpu_ref_get (req->fixed_rsrc_refs); } }
@@ -6958,36 +6958,32 @@ static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)spin_unlock_bh (&ctx->rsrc_ref_lock); } -static void io_rsrc_node_set (struct io_ring_ctx *ctx, -struct io_rsrc_data *rsrc_data) +static void io_rsrc_node_switch (struct io_ring_ctx *ctx, +struct io_rsrc_data *data_to_kill) { -struct io_rsrc_node *rsrc_node = ctx->rsrc_backup_node; +WARN_ON_ONCE (!ctx->rsrc_backup_node); +WARN_ON_ONCE (data_to_kill && !ctx->rsrc_node); -WARN_ON_ONCE (!rsrc_node); +if (data_to_kill) { +struct io_rsrc_node *rsrc_node = ctx->rsrc_node; - ctx->rsrc_backup_node =NULL ; - rsrc_node->rsrc_data = rsrc_data; + rsrc_node->rsrc_data = data_to_kill; +io_rsrc_ref_lock (ctx); +list_add_tail (&rsrc_node->node, &ctx->rsrc_ref_list); +io_rsrc_ref_unlock (ctx); -io_rsrc_ref_lock (ctx); - rsrc_data->node = rsrc_node; -list_add_tail (&rsrc_node->node, &ctx->rsrc_ref_list); -io_rsrc_ref_unlock (ctx); -percpu_ref_get (&rsrc_data->refs); -} - -static void io_rsrc_node_kill (struct io_ring_ctx *ctx,struct io_rsrc_data *data) -{ -struct io_rsrc_node *ref_node =NULL ; +percpu_ref_get (&data_to_kill->refs); +percpu_ref_kill (&rsrc_node->refs); + ctx->rsrc_node =NULL ; + } -io_rsrc_ref_lock (ctx); - ref_node = data->node; - data->node =NULL ; -io_rsrc_ref_unlock (ctx); -if (ref_node) -percpu_ref_kill (&ref_node->refs); +if (!ctx->rsrc_node) { + ctx->rsrc_node = ctx->rsrc_backup_node; + ctx->rsrc_backup_node =NULL ; + } } -static int io_rsrc_node_prealloc (struct io_ring_ctx *ctx) +static int io_rsrc_node_switch_start (struct io_ring_ctx *ctx) {if (ctx->rsrc_backup_node)return 0 ;
@@ -7004,10 +7000,11 @@ static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ct data->quiesce = true; do { - ret = io_rsrc_node_prealloc(ctx); + ret = io_rsrc_node_switch_start(ctx); if (ret) break; - io_rsrc_node_kill(ctx, data); + io_rsrc_node_switch(ctx, data); + percpu_ref_kill(&data->refs); flush_delayed_work(&ctx->rsrc_put_work);
@@ -7016,7 +7013,6 @@ static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctbreak ;percpu_ref_resurrect (&data->refs); -io_rsrc_node_set (ctx, data);reinit_completion (&data->done);mutex_unlock (&ctx->uring_lock);
@@ -7497,7 +7493,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return -EINVAL ;if (nr_args >IORING_MAX_FIXED_FILES )return -EMFILE ; - ret =io_rsrc_node_prealloc (ctx); + ret =io_rsrc_node_switch_start (ctx);if (ret)return ret;
@@ -7559,7 +7555,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } -io_rsrc_node_set (ctx, file_data); +io_rsrc_node_switch (ctx,NULL );return ret; out_fput:for (i =0 ; i < ctx->nr_user_files; i++) {
@@ -7648,7 +7644,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,return -EOVERFLOW ;if (done > ctx->nr_user_files)return -EINVAL ; - err =io_rsrc_node_prealloc (ctx); + err =io_rsrc_node_switch_start (ctx);if (err)return err;
@@ -7667,7 +7663,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,if (*file_slot) { file = (struct file *) ((unsigned long ) *file_slot &FFS_MASK ); - err =io_queue_rsrc_removal (data, data->node, file); + err =io_queue_rsrc_removal (data, ctx->rsrc_node, file);if (err)break ; *file_slot =NULL ;
@@ -7702,10 +7698,8 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, } } -if (needs_switch) { -percpu_ref_kill (&data->node->refs); -io_rsrc_node_set (ctx, data); - } +if (needs_switch) +io_rsrc_node_switch (ctx, data);return done ? done : err; }
@@ -8376,8 +8370,15 @@ static void io_ring_ctx_free(struct io_ring_ctx *ctx)io_eventfd_unregister (ctx);io_destroy_buffers (ctx); +/* there are no registered resources left, nobody uses it */ +if (ctx->rsrc_node) +io_rsrc_node_destroy (ctx->rsrc_node);if (ctx->rsrc_backup_node)io_rsrc_node_destroy (ctx->rsrc_backup_node); +flush_delayed_work (&ctx->rsrc_put_work); + +WARN_ON_ONCE (!list_empty (&ctx->rsrc_ref_list)); +WARN_ON_ONCE (!llist_empty (&ctx->rsrc_put_llist));#if defined(CONFIG_UNIX )if (ctx->ring_sock) {
Signature · 1 line
2.24.0