simplify SCM accounting
PATCH 2/5 io_uring: refactor __io_sqe_files_scm
In reply to Pavel Begunkov__io_sqe_files_scm() is now called only from one place passing a single file, so nr argument can be killed and __io_sqe_files_scm() simplified.
--- fs/io_uring.c | 48 +++++++++++++----------------------------------- 1 file changed, 13 insertions(+), 35 deletions(-)
fs/io_uring.c +13 -35
@@ -8597,12 +8597,12 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,* files because otherwise they can't form a loop and so are not interesting * for GC. */ -static int __io_sqe_files_scm (struct io_ring_ctx *ctx,int nr,int offset) +static int __io_sqe_files_scm (struct io_ring_ctx *ctx,int offset) { +struct file *file =io_file_from_index (ctx, offset);struct sock *sk = ctx->ring_sock->sk;struct scm_fp_list *fpl;struct sk_buff *skb; -int i, nr_files; fpl =kzalloc (sizeof (*fpl),GFP_KERNEL );if (!fpl)
@@ -8616,39 +8616,17 @@ static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) skb->sk = sk; - nr_files =0 ; fpl->user =get_uid (current_user ()); -for (i =0 ; i < nr; i++) { -struct file *file =io_file_from_index (ctx, i + offset); - -if (!file || !io_file_need_scm (file)) -continue ; - - fpl->fp[nr_files] =get_file (file); -unix_inflight (fpl->user, fpl->fp[nr_files]); - nr_files++; - } - -if (nr_files) { - fpl->max =SCM_MAX_FD ; - fpl->count = nr_files; -UNIXCB (skb).fp = fpl; - skb->destructor = unix_destruct_scm; -refcount_add (skb->truesize, &sk->sk_wmem_alloc); -skb_queue_head (&sk->sk_receive_queue, skb); - -for (i =0 ; i < nr; i++) { -struct file *file =io_file_from_index (ctx, i + offset); - -if (file &&io_file_need_scm (file)) -fput (file); - } - }else { -kfree_skb (skb); -free_uid (fpl->user); -kfree (fpl); - } - + fpl->fp[0 ] =get_file (file); +unix_inflight (fpl->user, file); + + fpl->max =SCM_MAX_FD ; + fpl->count =1 ; +UNIXCB (skb).fp = fpl; + skb->destructor = unix_destruct_scm; +refcount_add (skb->truesize, &sk->sk_wmem_alloc); +skb_queue_head (&sk->sk_receive_queue, skb); +fput (file); return 0; }#endif
@@ -8892,7 +8870,7 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, return 0; } -return __io_sqe_files_scm (ctx,1 , index); +return __io_sqe_files_scm (ctx, index);#else return 0 ;#endif
Signature · 1 line
2.35.1
PATCH 1/5 io_uring: uniform SCM accounting
In reply to Pavel BegunkovChannel all SCM accounting through io_sqe_file_register(), so we do it uniformely for updates and initial registration and can kill duplicated code. Registration might be slightly slower in some case, but first we skip most of SCM accounting now so it's not a problem. Moreover, it's nicer for an empty set registration as we don't even try to allocate skb for them anymore.
--- fs/io_uring.c | 93 ++++++++++++++------------------------------------- 1 file changed, 25 insertions(+), 68 deletions(-)
fs/io_uring.c +25 -68
@@ -8651,48 +8651,6 @@ static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) return 0; } - -/* -* If UNIX sockets are enabled, fd passing can cause a reference cycle which -* causes regular reference counting to break down. We rely on the UNIX -* garbage collection to take care of this problem for us. -*/ -static int io_sqe_files_scm (struct io_ring_ctx *ctx) -{ -unsigned left, total; -int ret =0 ; - - total =0 ; - left = ctx->nr_user_files; -while (left) { -unsigned this_files =min_t (unsigned, left,SCM_MAX_FD ); - - ret =__io_sqe_files_scm (ctx, this_files, total); -if (ret) -break ; - left -= this_files; - total += this_files; - } - -if (!ret) -return 0 ; - -while (total < ctx->nr_user_files) { -struct file *file =io_file_from_index (ctx, total); - -if (file) -fput (file); -io_fixed_file_slot (&ctx->file_table, total)->file_ptr =0 ; - total++; - } - -return ret; -} -#else -static int io_sqe_files_scm (struct io_ring_ctx *ctx) -{ -return 0 ; -}#endif static void io_rsrc_file_put (struct io_ring_ctx *ctx,struct io_rsrc_put *prsrc)
@@ -8813,6 +8771,9 @@ static void io_rsrc_put_work(struct work_struct *work) } } +static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file, +int index); +static int io_sqe_files_register (struct io_ring_ctx *ctx,void __user *arg,unsigned nr_args,u64 __user *tags) {
@@ -8837,27 +8798,31 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,if (ret)return ret; - ret = -ENOMEM ; -if (!io_alloc_file_tables (&ctx->file_table, nr_args)) - goto out_free; +if (!io_alloc_file_tables (&ctx->file_table, nr_args)) { +io_rsrc_data_free (ctx->file_data); + ctx->file_data =NULL ; +return -ENOMEM ; + }for (i =0 ; i < nr_args; i++, ctx->nr_user_files++) { +struct io_fixed_file *file_slot; +if (copy_from_user (&fd, &fds[i],sizeof (fd))) { ret = -EFAULT ; - goto out_fput; + goto fail; }/* allow sparse sets */ if (fd ==-1 ) { ret = -EINVAL ;if (unlikely (*io_get_tag_slot (ctx->file_data, i))) - goto out_fput; + goto fail;continue ; } file =fget (fd); ret = -EBADF ;if (unlikely (!file)) - goto out_fput; + goto fail;/* * Don't allow io_uring instances to be registered. If UNIX
@@ -8868,30 +8833,22 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,*/ if (file->f_op == &io_uring_fops) {fput (file); - goto out_fput; + goto fail; + } + file_slot =io_fixed_file_slot (&ctx->file_table, i); +io_fixed_file_set (file_slot, file); + ret =io_sqe_file_register (ctx, file, i); +if (ret) { + file_slot->file_ptr =0 ; +fput (file); + goto fail; } -io_fixed_file_set (io_fixed_file_slot (&ctx->file_table, i), file); - } - - ret =io_sqe_files_scm (ctx); -if (ret) { -__io_sqe_files_unregister (ctx); -return ret; }io_rsrc_node_switch (ctx,NULL ); -return ret; -out_fput: -for (i =0 ; i < ctx->nr_user_files; i++) { - file =io_file_from_index (ctx, i); -if (file) -fput (file); - } -io_free_file_tables (&ctx->file_table); - ctx->nr_user_files =0 ; -out_free: -io_rsrc_data_free (ctx->file_data); - ctx->file_data =NULL ; +return 0 ; +fail: +__io_sqe_files_unregister (ctx);return ret; }
Signature · 1 line
2.35.1
cover simplify SCM accounting
Just a refactoring series killing some lines of the SCM registration side and making it simpler overall.
Pavel Begunkov (5):
io_uring: uniform SCM accounting
io_uring: refactor __io_sqe_files_scm
io_uring: don't pass around fixed index for scm
io_uring: deduplicate SCM accounting
io_uring: rename io_sqe_file_register
fs/io_uring.c | 227 ++++++++++++++------------------------------------ 1 file changed, 63 insertions(+), 164 deletions(-)
Signature · 1 line
2.35.1
PATCH 3/5 io_uring: don't pass around fixed index for scm
There is an old API nuisance where io_uring's SCM accounting functions traverse fixed file tables and so requires them to be set in advance, which leads to some implicit rules of how io_sqe_file_register() should be used.
__io_sqe_files_scm() now works with only one file at a time, pass a file directly and get rid of all fixed table dereferencing inside. Clean io_sqe_file_register() callers.
--- fs/io_uring.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-)
fs/io_uring.c +14 -23
@@ -8597,9 +8597,8 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,* files because otherwise they can't form a loop and so are not interesting * for GC. */ -static int __io_sqe_files_scm (struct io_ring_ctx *ctx,int offset) +static int __io_sqe_files_scm (struct io_ring_ctx *ctx,struct file *file) { -struct file *file =io_file_from_index (ctx, offset);struct sock *sk = ctx->ring_sock->sk;struct scm_fp_list *fpl;struct sk_buff *skb;
@@ -8749,8 +8748,7 @@ static void io_rsrc_put_work(struct work_struct *work) } } -static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file, -int index); +static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file);static int io_sqe_files_register (struct io_ring_ctx *ctx,void __user *arg,unsigned nr_args,u64 __user *tags)
@@ -8813,14 +8811,13 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,fput (file); goto fail; } - file_slot =io_fixed_file_slot (&ctx->file_table, i); -io_fixed_file_set (file_slot, file); - ret =io_sqe_file_register (ctx, file, i); + ret =io_sqe_file_register (ctx, file);if (ret) { - file_slot->file_ptr =0 ;fput (file); goto fail; } + file_slot =io_fixed_file_slot (&ctx->file_table, i); +io_fixed_file_set (file_slot, file); }io_rsrc_node_switch (ctx,NULL );
@@ -8830,8 +8827,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } -static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file, -int index) +static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file) {#if defined(CONFIG_UNIX )struct sock *sock = ctx->ring_sock->sk;
@@ -8870,7 +8866,7 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, return 0; } -return __io_sqe_files_scm (ctx, index); +return __io_sqe_files_scm (ctx, file);#else return 0 ;#endif
@@ -8928,15 +8924,11 @@ static int io_install_fixed_file(struct io_kiocb *req, struct file *file, needs_switch = true; } - *io_get_tag_slot (ctx->file_data, slot_index) =0 ; -io_fixed_file_set (file_slot, file); - ret =io_sqe_file_register (ctx, file, slot_index); -if (ret) { - file_slot->file_ptr =0 ; - goto err; + ret =io_sqe_file_register (ctx, file); +if (!ret) { + *io_get_tag_slot (ctx->file_data, slot_index) =0 ; +io_fixed_file_set (file_slot, file); } - - ret =0 ; err:if (needs_switch)io_rsrc_node_switch (ctx, ctx->file_data);
@@ -9048,14 +9040,13 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, err = -EBADF ;break ; } - *io_get_tag_slot (data, i) = tag; -io_fixed_file_set (file_slot, file); - err =io_sqe_file_register (ctx, file, i); + err =io_sqe_file_register (ctx, file);if (err) { - file_slot->file_ptr =0 ;fput (file);break ; } + *io_get_tag_slot (data, i) = tag; +io_fixed_file_set (file_slot, file); } }
Signature · 1 line
2.35.1
PATCH 4/5 io_uring: deduplicate SCM accounting
In reply to Pavel BegunkovMerge io_sqe_file_register() and io_sqe_file_register(). The only real difference left between them is from where we get an skb.
--- fs/io_uring.c | 107 +++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 67 deletions(-)
fs/io_uring.c +40 -67
@@ -8589,7 +8589,6 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,return sqd; } -#if defined(CONFIG_UNIX )/* * Ensure the UNIX gc is aware of our file set, so we are certain that * the io_uring can be safely unregistered on process exit, even if we have
@@ -8597,38 +8596,59 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,* files because otherwise they can't form a loop and so are not interesting * for GC. */ -static int __io_sqe_files_scm (struct io_ring_ctx *ctx,struct file *file) +static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file) { +#if defined(CONFIG_UNIX )struct sock *sk = ctx->ring_sock->sk; +struct sk_buff_head *head = &sk->sk_receive_queue;struct scm_fp_list *fpl;struct sk_buff *skb; - fpl =kzalloc (sizeof (*fpl),GFP_KERNEL ); -if (!fpl) -return -ENOMEM ; +if (likely (!io_file_need_scm (file))) +return 0 ; + +/* +* See if we can merge this file into an existing skb SCM_RIGHTS +* file set. If there's no room, fall back to allocating a new skb +* and filling it in. +*/ +spin_lock_irq (&head->lock); + skb =skb_peek (head); +if (skb &&UNIXCB (skb).fp->count <SCM_MAX_FD ) +__skb_unlink (skb, head); +else + skb =NULL ; +spin_unlock_irq (&head->lock); - skb =alloc_skb (0 ,GFP_KERNEL );if (!skb) { -kfree (fpl); -return -ENOMEM ; - } + fpl =kzalloc (sizeof (*fpl),GFP_KERNEL ); +if (!fpl) +return -ENOMEM ; - skb->sk = sk; + skb =alloc_skb (0 ,GFP_KERNEL ); +if (!skb) { +kfree (fpl); +return -ENOMEM ; + } - fpl->user =get_uid (current_user ()); - fpl->fp[0 ] =get_file (file); -unix_inflight (fpl->user, file); + fpl->user =get_uid (current_user ()); + fpl->max =SCM_MAX_FD ; + fpl->count =0 ; - fpl->max =SCM_MAX_FD ; - fpl->count =1 ; -UNIXCB (skb).fp = fpl; - skb->destructor = unix_destruct_scm; -refcount_add (skb->truesize, &sk->sk_wmem_alloc); -skb_queue_head (&sk->sk_receive_queue, skb); +UNIXCB (skb).fp = fpl; + skb->sk = sk; + skb->destructor = unix_destruct_scm; +refcount_add (skb->truesize, &sk->sk_wmem_alloc); + } + + fpl =UNIXCB (skb).fp; + fpl->fp[fpl->count++] =get_file (file); +unix_inflight (fpl->user, file); +skb_queue_head (head, skb);fput (file); +#endif return 0 ; } -#endif static void io_rsrc_file_put (struct io_ring_ctx *ctx,struct io_rsrc_put *prsrc) {
@@ -8748,8 +8768,6 @@ static void io_rsrc_put_work(struct work_struct *work) } } -static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file); -static int io_sqe_files_register (struct io_ring_ctx *ctx,void __user *arg,unsigned nr_args,u64 __user *tags) {
@@ -8827,51 +8845,6 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } -static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file) -{ -#if defined(CONFIG_UNIX ) -struct sock *sock = ctx->ring_sock->sk; -struct sk_buff_head *head = &sock->sk_receive_queue; -struct sk_buff *skb; - -if (!io_file_need_scm (file)) -return 0 ; - -/* -* See if we can merge this file into an existing skb SCM_RIGHTS -* file set. If there's no room, fall back to allocating a new skb -* and filling it in. -*/ -spin_lock_irq (&head->lock); - skb =skb_peek (head); -if (skb) { -struct scm_fp_list *fpl =UNIXCB (skb).fp; - -if (fpl->count <SCM_MAX_FD ) { -__skb_unlink (skb, head); -spin_unlock_irq (&head->lock); - fpl->fp[fpl->count] =get_file (file); -unix_inflight (fpl->user, fpl->fp[fpl->count]); - fpl->count++; -spin_lock_irq (&head->lock); -__skb_queue_head (head, skb); - }else { - skb =NULL ; - } - } -spin_unlock_irq (&head->lock); - -if (skb) { -fput (file); -return 0 ; - } - -return __io_sqe_files_scm (ctx, file); -#else -return 0 ; -#endif -} -static int io_queue_rsrc_removal (struct io_rsrc_data *data,unsigned idx,struct io_rsrc_node *node,void *rsrc) {
Signature · 1 line
2.35.1
PATCH 5/5 io_uring: rename io_sqe_file_register
In reply to Pavel BegunkovRename io_sqe_file_register(), so the name better reflects what the function is doing.
--- fs/io_uring.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/io_uring.c +4 -4
@@ -8596,7 +8596,7 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,* files because otherwise they can't form a loop and so are not interesting * for GC. */ -static int io_sqe_file_register (struct io_ring_ctx *ctx,struct file *file) +static int io_scm_file_account (struct io_ring_ctx *ctx,struct file *file) {#if defined(CONFIG_UNIX )struct sock *sk = ctx->ring_sock->sk;
@@ -8829,7 +8829,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,fput (file); goto fail; } - ret =io_sqe_file_register (ctx, file); + ret =io_scm_file_account (ctx, file);if (ret) {fput (file); goto fail;
@@ -8897,7 +8897,7 @@ static int io_install_fixed_file(struct io_kiocb *req, struct file *file, needs_switch = true; } - ret =io_sqe_file_register (ctx, file); + ret =io_scm_file_account (ctx, file);if (!ret) { *io_get_tag_slot (ctx->file_data, slot_index) =0 ;io_fixed_file_set (file_slot, file);
@@ -9013,7 +9013,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, err = -EBADF ;break ; } - err =io_sqe_file_register (ctx, file); + err =io_scm_file_account (ctx, file);if (err) {fput (file);break ;
Signature · 1 line
2.35.1
Jens Axboe
maintainerQuoted 10 lines · Pavel Begunkov
Just a refactoring series killing some lines of the SCM registration side and making it simpler overall.
Pavel Begunkov (5):
io_uring: uniform SCM accounting
io_uring: refactor__io_sqe_files_scm
io_uring: don't pass around fixed index for scm
io_uring: deduplicate SCM accounting
io_uring: renameio_sqe_file_register
Quoted 2 lines
fs/io_uring.c | 227 ++++++++++++++------------------------------------ 1 file changed, 63 insertions(+), 164 deletions(-)
Looks good to me, and a nice cleanup.
Signature · 1 line
Jens Axboe
Quoted 11 lines · Pavel Begunkov
Just a refactoring series killing some lines of the SCM registration side and making it simpler overall.
Pavel Begunkov (5):
io_uring: uniform SCM accounting
io_uring: refactor__io_sqe_files_scm
io_uring: don't pass around fixed index for scm
io_uring: deduplicate SCM accounting
io_uring: renameio_sqe_file_register[...]
Applied, thanks!
[1/5] io_uring: uniform SCM accounting
commit: b302191999697ac6972c70d38d16a8a3e9546216
[2/5] io_uring: refactor __io_sqe_files_scm
commit: daee35f002ecabfaf6b9c6d5adc727b40aeaa048
[3/5] io_uring: don't pass around fixed index for scm
commit: 6c5f2c03659346f28334b3c757c8e752d96c41e2
[4/5] io_uring: deduplicate SCM accounting
commit: f8b9357ae7788235abc84ffc8fe1c66e8607aa47
[5/5] io_uring: rename io_sqe_file_register
commit: d9ed9fcf4bd6b8997d7e7533b650e18fa6273ed1Best regards,
Signature · 1 line
Jens Axboe