Skip to content

Commit bd54b6f

Browse files
Bijan Mottahedehaxboe
authored andcommitted
io_uring: implement fixed buffers registration similar to fixed files
Apply fixed_rsrc functionality for fixed buffers support. Signed-off-by: Bijan Mottahedeh <[email protected]> [rebase, remove multi-level tables, fix unregister on exit] Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/17035f4f75319dc92962fce4fc04bc0afb5a68dc.1619356238.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent eae071c commit bd54b6f

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

fs/io_uring.c

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ struct io_rsrc_put {
218218
union {
219219
void *rsrc;
220220
struct file *file;
221+
struct io_mapped_ubuf *buf;
221222
};
222223
};
223224

@@ -404,6 +405,7 @@ struct io_ring_ctx {
404405
unsigned nr_user_files;
405406

406407
/* if used, fixed mapped user buffers */
408+
struct io_rsrc_data *buf_data;
407409
unsigned nr_user_bufs;
408410
struct io_mapped_ubuf **user_bufs;
409411

@@ -5927,7 +5929,7 @@ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
59275929

59285930
printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
59295931
req->opcode);
5930-
return-EINVAL;
5932+
return -EINVAL;
59315933
}
59325934

59335935
static int io_req_prep_async(struct io_kiocb *req)
@@ -8110,19 +8112,36 @@ static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slo
81108112
*slot = NULL;
81118113
}
81128114

8113-
static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8115+
static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
81148116
{
8115-
unsigned int i;
8117+
/* no updates yet, so not used */
8118+
WARN_ON_ONCE(1);
8119+
}
81168120

8117-
if (!ctx->user_bufs)
8118-
return -ENXIO;
8121+
static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8122+
{
8123+
unsigned int i;
81198124

81208125
for (i = 0; i < ctx->nr_user_bufs; i++)
81218126
io_buffer_unmap(ctx, &ctx->user_bufs[i]);
81228127
kfree(ctx->user_bufs);
8128+
kfree(ctx->buf_data);
81238129
ctx->user_bufs = NULL;
8130+
ctx->buf_data = NULL;
81248131
ctx->nr_user_bufs = 0;
8125-
return 0;
8132+
}
8133+
8134+
static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8135+
{
8136+
int ret;
8137+
8138+
if (!ctx->buf_data)
8139+
return -ENXIO;
8140+
8141+
ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8142+
if (!ret)
8143+
__io_sqe_buffers_unregister(ctx);
8144+
return ret;
81268145
}
81278146

81288147
static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
@@ -8342,17 +8361,26 @@ static int io_buffer_validate(struct iovec *iov)
83428361
static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
83438362
unsigned int nr_args)
83448363
{
8364+
struct page *last_hpage = NULL;
8365+
struct io_rsrc_data *data;
83458366
int i, ret;
83468367
struct iovec iov;
8347-
struct page *last_hpage = NULL;
83488368

83498369
if (ctx->user_bufs)
83508370
return -EBUSY;
83518371
if (!nr_args || nr_args > UIO_MAXIOV)
83528372
return -EINVAL;
8353-
ret = io_buffers_map_alloc(ctx, nr_args);
8373+
ret = io_rsrc_node_switch_start(ctx);
83548374
if (ret)
83558375
return ret;
8376+
data = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, nr_args);
8377+
if (!data)
8378+
return -ENOMEM;
8379+
ret = io_buffers_map_alloc(ctx, nr_args);
8380+
if (ret) {
8381+
kfree(data);
8382+
return ret;
8383+
}
83568384

83578385
for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
83588386
ret = io_copy_iov(ctx, &iov, arg, i);
@@ -8368,9 +8396,13 @@ static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
83688396
break;
83698397
}
83708398

8371-
if (ret)
8372-
io_sqe_buffers_unregister(ctx);
8399+
WARN_ON_ONCE(ctx->buf_data);
83738400

8401+
ctx->buf_data = data;
8402+
if (ret)
8403+
__io_sqe_buffers_unregister(ctx);
8404+
else
8405+
io_rsrc_node_switch(ctx, NULL);
83748406
return ret;
83758407
}
83768408

@@ -8445,22 +8477,29 @@ static void io_req_caches_free(struct io_ring_ctx *ctx)
84458477
mutex_unlock(&ctx->uring_lock);
84468478
}
84478479

8480+
static bool io_wait_rsrc_data(struct io_rsrc_data *data)
8481+
{
8482+
if (!data)
8483+
return false;
8484+
if (!atomic_dec_and_test(&data->refs))
8485+
wait_for_completion(&data->done);
8486+
return true;
8487+
}
8488+
84488489
static void io_ring_ctx_free(struct io_ring_ctx *ctx)
84498490
{
84508491
io_sq_thread_finish(ctx);
8451-
io_sqe_buffers_unregister(ctx);
84528492

84538493
if (ctx->mm_account) {
84548494
mmdrop(ctx->mm_account);
84558495
ctx->mm_account = NULL;
84568496
}
84578497

84588498
mutex_lock(&ctx->uring_lock);
8459-
if (ctx->file_data) {
8460-
if (!atomic_dec_and_test(&ctx->file_data->refs))
8461-
wait_for_completion(&ctx->file_data->done);
8499+
if (io_wait_rsrc_data(ctx->buf_data))
8500+
__io_sqe_buffers_unregister(ctx);
8501+
if (io_wait_rsrc_data(ctx->file_data))
84628502
__io_sqe_files_unregister(ctx);
8463-
}
84648503
if (ctx->rings)
84658504
__io_cqring_overflow_flush(ctx, true);
84668505
mutex_unlock(&ctx->uring_lock);
@@ -9825,6 +9864,8 @@ static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
98259864
static bool io_register_op_must_quiesce(int op)
98269865
{
98279866
switch (op) {
9867+
case IORING_REGISTER_BUFFERS:
9868+
case IORING_UNREGISTER_BUFFERS:
98289869
case IORING_REGISTER_FILES:
98299870
case IORING_UNREGISTER_FILES:
98309871
case IORING_REGISTER_FILES_UPDATE:

0 commit comments

Comments
 (0)