Skip to content

Commit 634d00d

Browse files
isilenceaxboe
authored andcommitted
io_uring: add full-fledged dynamic buffers support
Hook buffers into all rsrc infrastructure, including tagging and updates. Suggested-by: Bijan Mottahedeh <[email protected]> Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/119ed51d68a491dae87eb55fb467a47870c86aad.1619356238.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent bd54b6f commit 634d00d

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

fs/io_uring.c

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8114,8 +8114,8 @@ static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slo
81148114

81158115
static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
81168116
{
8117-
/* no updates yet, so not used */
8118-
WARN_ON_ONCE(1);
8117+
io_buffer_unmap(ctx, &prsrc->buf);
8118+
prsrc->buf = NULL;
81198119
}
81208120

81218121
static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
@@ -8359,7 +8359,7 @@ static int io_buffer_validate(struct iovec *iov)
83598359
}
83608360

83618361
static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8362-
unsigned int nr_args)
8362+
unsigned int nr_args, u64 __user *tags)
83638363
{
83648364
struct page *last_hpage = NULL;
83658365
struct io_rsrc_data *data;
@@ -8383,6 +8383,12 @@ static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
83838383
}
83848384

83858385
for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
8386+
u64 tag = 0;
8387+
8388+
if (tags && copy_from_user(&tag, &tags[i], sizeof(tag))) {
8389+
ret = -EFAULT;
8390+
break;
8391+
}
83868392
ret = io_copy_iov(ctx, &iov, arg, i);
83878393
if (ret)
83888394
break;
@@ -8394,6 +8400,7 @@ static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
83948400
&last_hpage);
83958401
if (ret)
83968402
break;
8403+
data->tags[i] = tag;
83978404
}
83988405

83998406
WARN_ON_ONCE(ctx->buf_data);
@@ -8406,6 +8413,62 @@ static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
84068413
return ret;
84078414
}
84088415

8416+
static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
8417+
struct io_uring_rsrc_update2 *up,
8418+
unsigned int nr_args)
8419+
{
8420+
u64 __user *tags = u64_to_user_ptr(up->tags);
8421+
struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
8422+
struct io_mapped_ubuf *imu;
8423+
struct page *last_hpage = NULL;
8424+
bool needs_switch = false;
8425+
__u32 done;
8426+
int i, err;
8427+
8428+
if (!ctx->buf_data)
8429+
return -ENXIO;
8430+
if (up->offset + nr_args > ctx->nr_user_bufs)
8431+
return -EINVAL;
8432+
8433+
for (done = 0; done < nr_args; done++) {
8434+
u64 tag = 0;
8435+
8436+
err = io_copy_iov(ctx, &iov, iovs, done);
8437+
if (err)
8438+
break;
8439+
if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
8440+
err = -EFAULT;
8441+
break;
8442+
}
8443+
8444+
i = array_index_nospec(up->offset + done, ctx->nr_user_bufs);
8445+
imu = ctx->user_bufs[i];
8446+
if (imu) {
8447+
err = io_queue_rsrc_removal(ctx->buf_data, up->offset + done,
8448+
ctx->rsrc_node, imu);
8449+
if (err)
8450+
break;
8451+
ctx->user_bufs[i] = NULL;
8452+
needs_switch = true;
8453+
}
8454+
8455+
if (iov.iov_base || iov.iov_len) {
8456+
err = io_buffer_validate(&iov);
8457+
if (err)
8458+
break;
8459+
err = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
8460+
&last_hpage);
8461+
if (err)
8462+
break;
8463+
ctx->buf_data->tags[up->offset + done] = tag;
8464+
}
8465+
}
8466+
8467+
if (needs_switch)
8468+
io_rsrc_node_switch(ctx, ctx->buf_data);
8469+
return done ? done : err;
8470+
}
8471+
84098472
static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
84108473
{
84118474
__s32 __user *fds = arg;
@@ -9807,6 +9870,8 @@ static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
98079870
switch (type) {
98089871
case IORING_RSRC_FILE:
98099872
return __io_sqe_files_update(ctx, up, nr_args);
9873+
case IORING_RSRC_BUFFER:
9874+
return __io_sqe_buffers_update(ctx, up, nr_args);
98109875
}
98119876
return -EINVAL;
98129877
}
@@ -9857,6 +9922,9 @@ static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
98579922
case IORING_RSRC_FILE:
98589923
return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
98599924
rr.nr, u64_to_user_ptr(rr.tags));
9925+
case IORING_RSRC_BUFFER:
9926+
return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
9927+
rr.nr, u64_to_user_ptr(rr.tags));
98609928
}
98619929
return -EINVAL;
98629930
}
@@ -9933,7 +10001,7 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
993310001

993410002
switch (opcode) {
993510003
case IORING_REGISTER_BUFFERS:
9936-
ret = io_sqe_buffers_register(ctx, arg, nr_args);
10004+
ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
993710005
break;
993810006
case IORING_UNREGISTER_BUFFERS:
993910007
ret = -EINVAL;

include/uapi/linux/io_uring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ struct io_uring_files_update {
314314

315315
enum {
316316
IORING_RSRC_FILE = 0,
317+
IORING_RSRC_BUFFER = 1,
317318
};
318319

319320
struct io_uring_rsrc_register {

0 commit comments

Comments
 (0)