public inbox for [email protected]
 help / color / mirror / Atom feed
From: Pavel Begunkov <[email protected]>
To: Jens Axboe <[email protected]>, [email protected]
Subject: [PATCH v2 2/2] Fix hang in io_uring_get_cqe() with iopoll
Date: Sun, 21 Jun 2020 19:14:26 +0300	[thread overview]
Message-ID: <ffc6ac4081c54b7c244ae46f76d6b46a9868871b.1592755912.git.asml.silence@gmail.com> (raw)
In-Reply-To: <[email protected]>

Because of need_resched() check, io_uring_enter() -> io_iopoll_check()
can return 0 even if @min_complete wasn't satisfied. If that's the
case, __io_uring_get_cqe() sets submit=0 and wait_nr=0, disabling
setting IORING_ENTER_GETEVENTS as well. So, it goes crazy calling
io_uring_enter() in a loop, not actually submitting nor polling.

Set @wait_nr based on actual number of CQEs ready. It doesn't manifest
extra CQEs if any, thus implements __io_uring_cq_ready() with relaxed
semantics.

Signed-off-by: Pavel Begunkov <[email protected]>
---
 src/queue.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/queue.c b/src/queue.c
index 14a0777..d824cfd 100644
--- a/src/queue.c
+++ b/src/queue.c
@@ -32,6 +32,19 @@ static inline bool sq_ring_needs_enter(struct io_uring *ring,
 	return false;
 }
 
+static inline unsigned int __io_uring_cq_ready(struct io_uring *ring)
+{
+	return io_uring_smp_load_relaxed(ring->cq.ktail) - *ring->cq.khead;
+}
+
+static inline unsigned int io_adjust_wait_nr(struct io_uring *ring,
+					     unsigned int to_wait)
+{
+	unsigned int ready = __io_uring_cq_ready(ring);
+
+	return (to_wait <= ready) ? 0 : (to_wait - ready);
+}
+
 int __io_uring_get_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr,
 		       unsigned submit, unsigned wait_nr, sigset_t *sigmask)
 {
@@ -60,7 +73,8 @@ int __io_uring_get_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr,
 			err = -errno;
 		} else if (ret == (int)submit) {
 			submit = 0;
-			wait_nr = 0;
+			if (to_wait)
+				wait_nr = io_adjust_wait_nr(ring, to_wait);
 		} else {
 			submit -= ret;
 		}
-- 
2.24.0


  parent reply	other threads:[~2020-06-21 16:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-21 16:14 [PATCH v2 0/2] Fix hang in io_uring_get_cqe() with iopoll Pavel Begunkov
2020-06-21 16:14 ` [PATCH v2 1/2] barriers: add load relaxed Pavel Begunkov
2020-06-21 16:14 ` Pavel Begunkov [this message]
2020-06-21 18:48 ` [PATCH v2 0/2] Fix hang in io_uring_get_cqe() with iopoll Jens Axboe
2020-06-21 19:54   ` Pavel Begunkov

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=ffc6ac4081c54b7c244ae46f76d6b46a9868871b.1592755912.git.asml.silence@gmail.com \
    [email protected] \
    [email protected] \
    [email protected] \
    /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