Re: [PATCH] io_uring: add io_uring_enter(2) fixed file support

Jens Axboe
2022-03-03T13:38:11Z

On 3/2/22 10:28 PM, Xiaoguang Wang wrote:
> IORING_REGISTER_FILES is a good feature to reduce fget/fput overhead for
> each IO we do on file, but still left one, which is io_uring_enter(2).
> In io_uring_enter(2), it still fget/fput io_ring fd. I have observed
> this overhead in some our internal oroutine implementations based on
> io_uring with low submit batch. To totally remove fget/fput overhead in
> io_uring, we may add a small struct file cache in io_uring_task and add
> a new IORING_ENTER_FIXED_FILE flag. Currently the capacity of this file
> cache is 16, wihcih I think it maybe enough, also not that this cache is
> per-thread.

Would indeed be nice to get rid of, can be a substantial amount of time
wasted in fdget/fdput. Does this resolve dependencies correctly if
someone passes the ring fd? Adding ring registration to test/ring-leak.c
from the liburing repo would be a useful exercise.

Comments below.

> @@ -8739,8 +8742,16 @@ static __cold int io_uring_alloc_task_context(struct task_struct *task,
> if (unlikely(!tctx))
> return -ENOMEM;
>
> + tctx->registered_files = kzalloc(sizeof(struct file *) * IO_RINGFD_REG_MAX,
> + GFP_KERNEL);

kcalloc()

> +static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx, bool locked);
> +
> +static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *arg)
> +{
> + struct io_uring_fd_reg reg;
> + struct io_uring_task *tctx;
> + struct file *file;
> + int ret;
> +
> + if (copy_from_user(&reg, arg, sizeof(struct io_uring_fd_reg)))
> + return -EFAULT;
> + if (reg.offset > IO_RINGFD_REG_MAX)
> + return -EINVAL;
> +
> + ret = io_uring_add_tctx_node(ctx, true);
> + if (unlikely(ret))
> + return ret;

Can we safely drop ctx->uring_lock around this call instead? The locked
argument is always kind of ugly, and you currently have a deadlock as
far as I can tell from io_uring_alloc_task_context() that now holds
ctx->uring_lock, and then calling io_init_wq_offload() which will grab
it again.

Why not just use io_uring_rsrc_update here rather than add a new type?

--
Jens Axboe