Skip to contentSkip to conversation

segfault and not only fixes

PATCH In review Pavel Begunkov 2 participants 8 messages Feb 11, 2021 io_uring

lore
Share & tools
lore
#2 lore raw

PATCH 2/5 src/queue: control kernel enter with a var

In reply to Pavel Begunkov

We check twice for all entering conditions in _io_uring_get_cqe(), first to set flags, and the second to potentially break the loop. Save it into a need_enter var.

Also, don't set IORING_ENTER_GETEVENTS when there is already enough of events in the CQ.

---
 src/queue.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
src/queue.c +8 -4
index c2facfd..4fb4ea7 100644
@@ -92,6 +92,7 @@ static int _io_uring_get_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_pt
 	int err;
 
 	do {
+		bool need_enter = false;
 		bool cq_overflow_flush = false;
 		unsigned flags = 0;
 		unsigned nr_available;
@@ -107,12 +108,15 @@ static int _io_uring_get_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_pt
 			}
 			cq_overflow_flush = true;
 		}
-		if (data->wait_nr || cq_overflow_flush)
+		if (data->wait_nr > nr_available || cq_overflow_flush) {
 			flags = IORING_ENTER_GETEVENTS | data->get_flags;
-		if (data->submit)
+			need_enter = true;
+		}
+		if (data->submit) {
 			sq_ring_needs_enter(ring, &flags);
-		if (data->wait_nr <= nr_available && !data->submit &&
-		    !cq_overflow_flush)
+			need_enter = true;
+		}
+		if (!need_enter)
 			break;
 
 		ret = __sys_io_uring_enter2(ring->ring_fd, data->submit,
Signature · 1 line
2.24.0
#3 lore raw

PATCH 3/5 test/link-timeout: close pipes after yourself

In reply to Pavel Begunkov

Close pipe fds when we exit test_single_link_timeout().

---
 test/link-timeout.c | 2 ++
 1 file changed, 2 insertions(+)
test/link-timeout.c +2 -0
index a517c5e..5d8417f 100644
@@ -528,6 +528,8 @@ static int test_single_link_timeout(struct io_uring *ring, unsigned nsec)
 		io_uring_cqe_seen(ring, cqe);
 	}
 
+	close(fds[0]);
+	close(fds[1]);
 	return 0;
 err:
 	return 1;
Signature · 1 line
2.24.0
#4 lore raw

PATCH 4/5 test/sq-poll-share: don't ignore wait errors

In reply to Pavel Begunkov

Check io_uring_wait_cqe() result, it's not safe to poke into cqe on error.

---
 test/sq-poll-share.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
test/sq-poll-share.c +8 -1
index 0f25389..02b008e 100644
@@ -60,7 +60,14 @@ static int wait_io(struct io_uring *ring, int nr_ios)
 	struct io_uring_cqe *cqe;
 
 	while (nr_ios) {
-		io_uring_wait_cqe(ring, &cqe);
+		int ret = io_uring_wait_cqe(ring, &cqe);
+
+		if (ret == -EAGAIN) {
+			continue;
+		} else if (ret) {
+			fprintf(stderr, "io_uring_wait_cqe failed %i\n", ret);
+			return 1;
+		}
 		if (cqe->res != BS) {
 			fprintf(stderr, "Unexpected ret %d\n", cqe->res);
 			return 1;
Signature · 1 line
2.24.0
#0 lore raw

cover segfault and not only fixes

First 4 should be good and simple. 5/5 is my shot on the segfaults, take it with a grain of salt.

link-timeout failure is a separate beast, it's from the old times, and comes from the kernel's io_async_find_and_cancel() failing with ENOENT(?) when a linked-timeout sees its master but fails to cancel it, e.g. when the master is in IRQ or posting CQE.
Maybe we just need to fix the test.

Pavel Begunkov (5):
src/queue: don't re-wait for CQEs
src/queue: control kernel enter with a var
test/link-timeout: close pipes after yourself
test/sq-poll-share: don't ignore wait errors
src/queue: fix no-error with NULL cqe

 src/include/liburing.h |  4 +++-
 src/queue.c            | 22 +++++++++-------------
 test/link-timeout.c    |  2 ++
 test/sq-poll-share.c   |  9 ++++++++-
 4 files changed, 22 insertions(+), 15 deletions(-)
Signature · 1 line
2.24.0
#1 lore raw

PATCH 1/5 src/queue: don't re-wait for CQEs

If ret==to_submit then io_uring_enter did go to waiting path and we should not repeat it. And that was the intention of a post syscall data->submit check, but reshuffling spoiled it.

---
 src/queue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/queue.c +1 -1
index 8c394dd..c2facfd 100644
@@ -124,7 +124,7 @@ static int _io_uring_get_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_pt
 		}
 
 		data->submit -= ret;
-		if (ret == (int)data->submit) {
+		if (!data->submit) {
 			/*
 			 * When SETUP_IOPOLL is set, __sys_io_uring enter()
 			 * must be called to reap new completions but the call
Signature · 1 line
2.24.0
#5 lore raw

PATCH 5/5 src/queue: fix no-error with NULL cqe

In reply to Pavel Begunkov

Can happen that _io_uring_get_cqe() returns success without filled cqe, and it's ok, especially for wait_n=0. Fix up error code for functions that always want to have a valid CQE on success.

Also don't do cq_ring_needs_flush() check in a some weird place, mainstream it together with wait_nr testing.

---
 src/include/liburing.h |  4 +++-
 src/queue.c            | 10 +---------
 2 files changed, 4 insertions(+), 10 deletions(-)
src/include/liburing.h +3 -1
index 90403bc..27c5a14 100644
@@ -601,7 +601,9 @@ static inline int io_uring_wait_cqe_nr(struct io_uring *ring,
 				      struct io_uring_cqe **cqe_ptr,
 				      unsigned wait_nr)
 {
-	return __io_uring_get_cqe(ring, cqe_ptr, 0, wait_nr, NULL);
+	int ret = __io_uring_get_cqe(ring, cqe_ptr, 0, wait_nr, NULL);
+
+	return (ret || *cqe_ptr) ? ret : -EAGAIN;
 }
 
 /*
src/queue.c +1 -9
index 4fb4ea7..0b09a9c 100644
@@ -93,7 +93,6 @@ static int _io_uring_get_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_pt
 
 	do {
 		bool need_enter = false;
-		bool cq_overflow_flush = false;
 		unsigned flags = 0;
 		unsigned nr_available;
 		int ret;
@@ -101,14 +100,7 @@ static int _io_uring_get_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_pt
 		err = __io_uring_peek_cqe(ring, &cqe, &nr_available);
 		if (err)
 			break;
-		if (!cqe && !to_wait && !data->submit) {
-			if (!cq_ring_needs_flush(ring)) {
-				err = -EAGAIN;
-				break;
-			}
-			cq_overflow_flush = true;
-		}
-		if (data->wait_nr > nr_available || cq_overflow_flush) {
+		if (data->wait_nr > nr_available || cq_ring_needs_flush(ring)) {
 			flags = IORING_ENTER_GETEVENTS | data->get_flags;
 			need_enter = true;
 		}
Signature · 1 line
2.24.0
#6 lore raw
In reply to Pavel Begunkov
Quoted 8 lines · Pavel Begunkov

First 4 should be good and simple. 5/5 is my shot on the segfaults, take it with a grain of salt.

link-timeout failure is a separate beast, it's from the old times, and comes from the kernel's io_async_find_and_cancel() failing with ENOENT(?) when a linked-timeout sees its master but fails to cancel it, e.g. when the master is in IRQ or posting CQE.
Maybe we just need to fix the test.

Easily reproducible if you apply 3/5 and do

while (1) {
	int err = test_single_link_timeout(10);
	assert(err == 0);
}
Quoted 14 lines

Pavel Begunkov (5):
src/queue: don't re-wait for CQEs
src/queue: control kernel enter with a var
test/link-timeout: close pipes after yourself
test/sq-poll-share: don't ignore wait errors
src/queue: fix no-error with NULL cqe

 src/include/liburing.h |  4 +++-
 src/queue.c            | 22 +++++++++-------------
 test/link-timeout.c    |  2 ++
 test/sq-poll-share.c   |  9 ++++++++-
 4 files changed, 22 insertions(+), 15 deletions(-)
Signature · 1 line
Pavel Begunkov
maintainer
#7 lore raw
In reply to Pavel Begunkov
Quoted 8 lines · Pavel Begunkov

First 4 should be good and simple. 5/5 is my shot on the segfaults, take it with a grain of salt.

link-timeout failure is a separate beast, it's from the old times, and comes from the kernel's io_async_find_and_cancel() failing with ENOENT(?) when a linked-timeout sees its master but fails to cancel it, e.g. when the master is in IRQ or posting CQE.
Maybe we just need to fix the test.

1-4 look fine to me, I don't like 5. I've committed a different variant that I think better fixes the real issue of doing a return that's too early.

Signature · 1 line
Jens Axboe