Skip to content

Commit

Permalink
io_uring: add IORING_SETUP_SINGLE_ISSUER
Browse files Browse the repository at this point in the history
Add a new IORING_SETUP_SINGLE_ISSUER flag and the userspace visible part
of it, i.e. put limitations of submitters. Also, don't allow it together
with IOPOLL as we're not going to put it to good use.

Signed-off-by: Pavel Begunkov <[email protected]>
Link: https://lore.kernel.org/r/4bcc41ee467fdf04c8aab8baf6ce3ba21858c3d4.1655371007.git.asml.silence@gmail.com
Reviewed-by: Hao Xu <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
isilence authored and axboe committed Jul 25, 2022
1 parent 0ec6dca commit 97bbdc0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
5 changes: 4 additions & 1 deletion include/uapi/linux/io_uring.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ enum {
* IORING_SQ_TASKRUN in the sq ring flags. Not valid with COOP_TASKRUN.
*/
#define IORING_SETUP_TASKRUN_FLAG (1U << 9)

#define IORING_SETUP_SQE128 (1U << 10) /* SQEs are 128 byte */
#define IORING_SETUP_CQE32 (1U << 11) /* CQEs are 32 byte */
/*
* Only one task is allowed to submit requests
*/
#define IORING_SETUP_SINGLE_ISSUER (1U << 12)

enum io_uring_op {
IORING_OP_NOP,
Expand Down
7 changes: 5 additions & 2 deletions io_uring/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,8 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
io_destroy_buffers(ctx);
if (ctx->sq_creds)
put_cred(ctx->sq_creds);
if (ctx->submitter_task)
put_task_struct(ctx->submitter_task);

/* there are no registered resources left, nobody uses it */
if (ctx->rsrc_node)
Expand Down Expand Up @@ -3189,7 +3191,7 @@ static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
if (fd < 0)
return fd;

ret = io_uring_add_tctx_node(ctx);
ret = __io_uring_add_tctx_node(ctx, false);
if (ret) {
put_unused_fd(fd);
return ret;
Expand Down Expand Up @@ -3409,7 +3411,8 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL |
IORING_SETUP_COOP_TASKRUN | IORING_SETUP_TASKRUN_FLAG |
IORING_SETUP_SQE128 | IORING_SETUP_CQE32))
IORING_SETUP_SQE128 | IORING_SETUP_CQE32 |
IORING_SETUP_SINGLE_ISSUER))
return -EINVAL;

return io_uring_create(entries, &p, params);
Expand Down
1 change: 1 addition & 0 deletions io_uring/io_uring_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ struct io_ring_ctx {
/* Keep this last, we don't need it for the fast path */

struct io_restriction restrictions;
struct task_struct *submitter_task;

/* slow path rsrc auxilary data, used by update/register */
struct io_rsrc_node *rsrc_backup_node;
Expand Down
27 changes: 24 additions & 3 deletions io_uring/tctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,32 @@ __cold int io_uring_alloc_task_context(struct task_struct *task,
return 0;
}

int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
static int io_register_submitter(struct io_ring_ctx *ctx)
{
int ret = 0;

mutex_lock(&ctx->uring_lock);
if (!ctx->submitter_task)
ctx->submitter_task = get_task_struct(current);
else if (ctx->submitter_task != current)
ret = -EEXIST;
mutex_unlock(&ctx->uring_lock);

return ret;
}

int __io_uring_add_tctx_node(struct io_ring_ctx *ctx, bool submitter)
{
struct io_uring_task *tctx = current->io_uring;
struct io_tctx_node *node;
int ret;

if ((ctx->flags & IORING_SETUP_SINGLE_ISSUER) && submitter) {
ret = io_register_submitter(ctx);
if (ret)
return ret;
}

if (unlikely(!tctx)) {
ret = io_uring_alloc_task_context(current, ctx);
if (unlikely(ret))
Expand Down Expand Up @@ -133,7 +153,8 @@ int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
list_add(&node->ctx_node, &ctx->tctx_list);
mutex_unlock(&ctx->uring_lock);
}
tctx->last = ctx;
if (submitter)
tctx->last = ctx;
return 0;
}

Expand Down Expand Up @@ -241,7 +262,7 @@ int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg,
return -EINVAL;

mutex_unlock(&ctx->uring_lock);
ret = io_uring_add_tctx_node(ctx);
ret = __io_uring_add_tctx_node(ctx, false);
mutex_lock(&ctx->uring_lock);
if (ret)
return ret;
Expand Down
4 changes: 2 additions & 2 deletions io_uring/tctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct io_tctx_node {
int io_uring_alloc_task_context(struct task_struct *task,
struct io_ring_ctx *ctx);
void io_uring_del_tctx_node(unsigned long index);
int __io_uring_add_tctx_node(struct io_ring_ctx *ctx);
int __io_uring_add_tctx_node(struct io_ring_ctx *ctx, bool submitter);
void io_uring_clean_tctx(struct io_uring_task *tctx);

void io_uring_unreg_ringfd(void);
Expand All @@ -52,5 +52,5 @@ static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)

if (likely(tctx && tctx->last == ctx))
return 0;
return __io_uring_add_tctx_node(ctx);
return __io_uring_add_tctx_node(ctx, true);
}

0 comments on commit 97bbdc0

Please sign in to comment.