Skip to content

Commit 2563d78

Browse files
mgjmgregkh
authored andcommitted
io_uring/kbuf: support min length left for incremental buffers
Commit 7deba79 upstream. Incrementally consumed buffer rings are generally fully consumed, but it's quite possible that the application has a minimum size it needs to meet to avoid truncation. Currently that minimum limit is 1 byte, but this should be a setting that is the hands of the application. For recvmsg multishot, a prime use case for incrementally consumed buffers, the application may get spurious -EFAULT returned at the end of an incrementally consumed buffer, as less space is available than the headers need. Grab a u32 field in struct io_uring_buf_reg, which the application can use to inform the kernel of the minimum size that should be available in an incrementally consumed buffer. If less than that is available, the current buffer is fully processed and the next one will be picked. Cc: stable@vger.kernel.org Fixes: ae98dbf ("io_uring/kbuf: add support for incremental buffer consumption") Link: axboe/liburing#1433 Signed-off-by: Martin Michaelis <code@mgjm.de> [axboe: write commit message, change io_buffer_list member name] Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de> Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 23900db commit 2563d78

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

include/uapi/linux/io_uring.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,8 @@ struct io_uring_buf_reg {
864864
__u32 ring_entries;
865865
__u16 bgid;
866866
__u16 flags;
867-
__u64 resv[3];
867+
__u32 min_left;
868+
__u32 resv[5];
868869
};
869870

870871
/* argument for IORING_REGISTER_PBUF_STATUS */

io_uring/kbuf.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len)
4747
this_len = min_t(u32, len, buf_len);
4848
buf_len -= this_len;
4949
/* Stop looping for invalid buffer length of 0 */
50-
if (buf_len || !this_len) {
51-
buf->addr = READ_ONCE(buf->addr) + this_len;
52-
buf->len = buf_len;
50+
if (buf_len > bl->min_left_sub_one || !this_len) {
51+
WRITE_ONCE(buf->addr, READ_ONCE(buf->addr) + this_len);
52+
WRITE_ONCE(buf->len, buf_len);
5353
return false;
5454
}
5555
buf->len = 0;
@@ -637,6 +637,10 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
637637
if (reg.ring_entries >= 65536)
638638
return -EINVAL;
639639

640+
/* minimum left byte count is a property of incremental buffers */
641+
if (!(reg.flags & IOU_PBUF_RING_INC) && reg.min_left)
642+
return -EINVAL;
643+
640644
bl = io_buffer_get_list(ctx, reg.bgid);
641645
if (bl) {
642646
/* if mapped buffer ring OR classic exists, don't allow */
@@ -684,6 +688,8 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
684688
bl->mask = reg.ring_entries - 1;
685689
bl->flags |= IOBL_BUF_RING;
686690
bl->buf_ring = br;
691+
if (reg.min_left)
692+
bl->min_left_sub_one = reg.min_left - 1;
687693
if (reg.flags & IOU_PBUF_RING_INC)
688694
bl->flags |= IOBL_INC;
689695
ret = io_buffer_add_list(ctx, bl, reg.bgid);

io_uring/kbuf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ struct io_buffer_list {
3434

3535
__u16 flags;
3636

37+
/*
38+
* minimum required amount to be left to reuse an incrementally
39+
* consumed buffer. If less than this is left at consumption time,
40+
* buffer is done and head is incremented to the next buffer.
41+
*/
42+
__u32 min_left_sub_one;
43+
3744
struct io_mapped_region region;
3845
};
3946

0 commit comments

Comments
 (0)