Skip to content

Commit

Permalink
io_uring: abstract out provided buffer list selection
Browse files Browse the repository at this point in the history
In preparation for providing another way to select a buffer, move the
existing logic into a helper.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Jens Axboe committed May 9, 2022
1 parent b66e65f commit 149c69b
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions fs/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3448,29 +3448,41 @@ static int io_buffer_add_list(struct io_ring_ctx *ctx,
return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
}

static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
struct io_buffer_list *bl,
unsigned int issue_flags)
{
struct io_buffer *kbuf;

if (list_empty(&bl->buf_list))
return ERR_PTR(-ENOBUFS);

kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
list_del(&kbuf->list);
if (*len > kbuf->len)
*len = kbuf->len;
req->flags |= REQ_F_BUFFER_SELECTED;
req->kbuf = kbuf;
io_ring_submit_unlock(req->ctx, issue_flags);
return u64_to_user_ptr(kbuf->addr);
}

static void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
unsigned int issue_flags)
{
struct io_buffer *kbuf = req->kbuf;
struct io_ring_ctx *ctx = req->ctx;
struct io_buffer_list *bl;

io_ring_submit_lock(req->ctx, issue_flags);

bl = io_buffer_get_list(ctx, req->buf_index);
if (bl && !list_empty(&bl->buf_list)) {
kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
list_del(&kbuf->list);
if (*len > kbuf->len)
*len = kbuf->len;
req->flags |= REQ_F_BUFFER_SELECTED;
req->kbuf = kbuf;
if (unlikely(!bl)) {
io_ring_submit_unlock(req->ctx, issue_flags);
return u64_to_user_ptr(kbuf->addr);
return ERR_PTR(-ENOBUFS);
}

io_ring_submit_unlock(req->ctx, issue_flags);
return ERR_PTR(-ENOBUFS);
/* selection helpers drop the submit lock again, if needed */
return io_provided_buffer_select(req, len, bl, issue_flags);
}

#ifdef CONFIG_COMPAT
Expand Down

0 comments on commit 149c69b

Please sign in to comment.