public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: Qingyue Zhang <chunzhennn@qq.com>
Cc: aftern00n@qq.com, io-uring@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] io_uring/kbuf: fix infinite loop in io_kbuf_inc_commit()
Date: Wed, 27 Aug 2025 20:58:12 -0600	[thread overview]
Message-ID: <8f4a4090-78ed-4cf1-bd73-7ae73fff8b90@kernel.dk> (raw)
In-Reply-To: <8ed15a17-618f-4277-afc3-934939292060@kernel.dk>

On 8/27/25 8:50 PM, Jens Axboe wrote:
> On 8/27/25 8:49 PM, Qingyue Zhang wrote:
>> On Wed, 27 Aug 2025 20:08:05 -0600, Jens Axboe wrote:
>>> I don't think there's anything wrong with the looping and stopping at
>>> the other end is of course a safe guard, but couldn't we just abort the
>>> loop if we see a 0 sized buffer? At that point we know the buffer is
>>> invalid, or the kernel is buggy, and it'd be saner to stop at that
>>> point. Something ala:
>>>
>>>
>>> diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
>>> index 394037d3f2f6..19a8bde5e1e1 100644
>>> --- a/io_uring/kbuf.c
>>> +++ b/io_uring/kbuf.c
>>> @@ -42,7 +42,8 @@ static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len)
>>>  		buf_len = READ_ONCE(buf->len);
>>>  		this_len = min_t(u32, len, buf_len);
>>>  		buf_len -= this_len;
>>> -		if (buf_len) {
>>> +		/* Stop looping for invalid buffer length of 0 */
>>> +		if (buf_len || !this_len) {
>>>  			buf->addr += this_len;
>>>  			buf->len = buf_len;
>>>  			return false;
>>
>> Good idea, it looks nice to me.
> 
> I'll send it out, I amended the commit message a bit too.

Oh, and let me know if you prefer any tags or whatever changed in that
patch.

Additionally, would you mind if I use your reproducer for a test case?
I turned it into the below. Will need a bit more to be a test case, but
it's pretty much there. Functionally it should be the same, it's just
using more idiomatic liburing rather than the lower level helpers.

And finally, thanks for the report and test case! Very useful.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <liburing.h>

#define BGID		0
#define RENTRIES	2
#define RMASK		(RENTRIES - 1)

int main(int argc, char *argv[])
{
	struct io_uring_buf_ring *br;
	struct io_uring_sqe *sqe;
	struct io_uring ring;
	void *send_buf;
	int ret, fds[2];

	io_uring_queue_init(1, &ring, IORING_SETUP_NO_SQARRAY);

	br = io_uring_setup_buf_ring(&ring, RENTRIES, BGID, IOU_PBUF_RING_INC, &ret);
	if (!br) {
		if (ret == -EINVAL)
			return 77;
		fprintf(stderr, "buf ring setup: %d\n", ret);
		return 1;
	}

	/*
	 * Use the buffer ring itself as the provided buffer address. Once the
	 * recv completes, it will have zero filled the buffer addr/len.
	 */
	io_uring_buf_ring_add(br, br, 4096, 0, RMASK, 0);
	io_uring_buf_ring_advance(br, 1);

	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds) < 0) {
		perror("socketpair");
		return 1;
	}

	/*
	 * Send zeroes, overwriting the buffer ring
	 */
	send_buf = calloc(1, 32);
	ret = send(fds[0], send_buf, 32, MSG_DONTWAIT);
	if (ret < 0) {
		perror("send");
		return 1;
	}

	/*
	 * Do recv, picking the first buffer. When buffer is picked,
	 * it's still fully valid. By the time it needs to get committed,
	 * it will have invalid addr/len fields (all zeroes from the recv)
	 */
	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_recv_multishot(sqe, fds[1], NULL, 0, 0);
	sqe->flags |= IOSQE_BUFFER_SELECT;
	sqe->buf_index = BGID;
	io_uring_submit_and_wait(&ring, 2);

	io_uring_free_buf_ring(&ring, br, RENTRIES, BGID);
	io_uring_queue_exit(&ring);
	return 0;
}

-- 
Jens Axboe

  reply	other threads:[~2025-08-28  2:58 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250827114339.367080-1-chunzhennn@qq.com>
2025-08-27 11:44 ` [PATCH 2/2] io_uring/kbuf: fix infinite loop in io_kbuf_inc_commit() Qingyue Zhang
2025-08-27 14:30   ` Jens Axboe
2025-08-27 21:45     ` Jens Axboe
2025-08-27 21:59       ` Keith Busch
2025-08-27 22:23         ` Jens Axboe
2025-08-28  1:36       ` Qingyue Zhang
2025-08-28  2:08         ` Jens Axboe
2025-08-28  2:49           ` Qingyue Zhang
2025-08-28  2:50             ` Jens Axboe
2025-08-28  2:58               ` Jens Axboe [this message]
2025-08-28  3:27                 ` Qingyue Zhang
2025-08-28 11:50                   ` Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8f4a4090-78ed-4cf1-bd73-7ae73fff8b90@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=aftern00n@qq.com \
    --cc=chunzhennn@qq.com \
    --cc=io-uring@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox