Skip to content

Commit 5cccc68

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 2889d92 commit 5cccc68

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

include/uapi/linux/io_uring.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ struct io_uring_buf_reg {
758758
__u32 ring_entries;
759759
__u16 bgid;
760760
__u16 flags;
761-
__u64 resv[3];
761+
__u32 min_left;
762+
__u32 resv[5];
762763
};
763764

764765
/* argument for IORING_REGISTER_PBUF_STATUS */

io_uring/kbuf.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ 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) {
50+
if (buf_len > bl->min_left_sub_one || !this_len) {
5151
WRITE_ONCE(buf->addr, READ_ONCE(buf->addr) + this_len);
5252
WRITE_ONCE(buf->len, buf_len);
5353
return false;
@@ -727,6 +727,10 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
727727
if (reg.ring_entries >= 65536)
728728
return -EINVAL;
729729

730+
/* minimum left byte count is a property of incremental buffers */
731+
if (!(reg.flags & IOU_PBUF_RING_INC) && reg.min_left)
732+
return -EINVAL;
733+
730734
bl = io_buffer_get_list(ctx, reg.bgid);
731735
if (bl) {
732736
/* if mapped buffer ring OR classic exists, don't allow */
@@ -747,6 +751,8 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
747751
if (!ret) {
748752
bl->nr_entries = reg.ring_entries;
749753
bl->mask = reg.ring_entries - 1;
754+
if (reg.min_left)
755+
bl->min_left_sub_one = reg.min_left - 1;
750756
if (reg.flags & IOU_PBUF_RING_INC)
751757
bl->flags |= IOBL_INC;
752758

io_uring/kbuf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ struct io_buffer_list {
3838
__u16 flags;
3939

4040
atomic_t refs;
41+
42+
/*
43+
* minimum required amount to be left to reuse an incrementally
44+
* consumed buffer. If less than this is left at consumption time,
45+
* buffer is done and head is incremented to the next buffer.
46+
*/
47+
__u32 min_left_sub_one;
4148
};
4249

4350
struct io_buffer {

0 commit comments

Comments
 (0)