Skip to content

Commit 91f262e

Browse files
axboegregkh
authored andcommitted
io_uring/kbuf: always use READ_ONCE() to read ring provided buffer lengths
Commit 98b6fa6 upstream. Since the buffers are mapped from userspace, it is prudent to use READ_ONCE() to read the value into a local variable, and use that for any other actions taken. Having a stable read of the buffer length avoids worrying about it changing after checking, or being read multiple times. Similarly, the buffer may well change in between it being picked and being committed. Ensure the looping for incremental ring buffer commit stops if it hits a zero sized buffer, as no further progress can be made at that point. Fixes: ae98dbf ("io_uring/kbuf: add support for incremental buffer consumption") Link: https://lore.kernel.org/io-uring/tencent_000C02641F6250C856D0C26228DE29A3D30A@qq.com/ Reported-by: Qingyue Zhang <chunzhennn@qq.com> Reported-by: Suoxing Zhang <aftern00n@qq.com> Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c4dbca5 commit 91f262e

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

io_uring/kbuf.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@ static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len)
3636
{
3737
while (len) {
3838
struct io_uring_buf *buf;
39-
u32 this_len;
39+
u32 buf_len, this_len;
4040

4141
buf = io_ring_head_to_buf(bl->buf_ring, bl->head, bl->mask);
42-
this_len = min_t(u32, len, buf->len);
43-
buf->len -= this_len;
44-
if (buf->len) {
42+
buf_len = READ_ONCE(buf->len);
43+
this_len = min_t(u32, len, buf_len);
44+
buf_len -= this_len;
45+
/* Stop looping for invalid buffer length of 0 */
46+
if (buf_len || !this_len) {
4547
buf->addr += this_len;
48+
buf->len = buf_len;
4649
return false;
4750
}
51+
buf->len = 0;
4852
bl->head++;
4953
len -= this_len;
5054
}
@@ -167,6 +171,7 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len,
167171
__u16 tail, head = bl->head;
168172
struct io_br_sel sel = { };
169173
struct io_uring_buf *buf;
174+
u32 buf_len;
170175

171176
tail = smp_load_acquire(&br->tail);
172177
if (unlikely(tail == head))
@@ -176,8 +181,9 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len,
176181
req->flags |= REQ_F_BL_EMPTY;
177182

178183
buf = io_ring_head_to_buf(br, head, bl->mask);
179-
if (*len == 0 || *len > buf->len)
180-
*len = buf->len;
184+
buf_len = READ_ONCE(buf->len);
185+
if (*len == 0 || *len > buf_len)
186+
*len = buf_len;
181187
req->flags |= REQ_F_BUFFER_RING | REQ_F_BUFFERS_COMMIT;
182188
req->buf_index = buf->bid;
183189
sel.buf_list = bl;
@@ -274,7 +280,7 @@ static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg,
274280

275281
req->buf_index = buf->bid;
276282
do {
277-
u32 len = buf->len;
283+
u32 len = READ_ONCE(buf->len);
278284

279285
/* truncate end piece, if needed, for non partial buffers */
280286
if (len > arg->max_len) {

0 commit comments

Comments
 (0)