Skip to contentSkip to conversation

exec + timeout cancellation

PATCH Applied Pavel Begunkov 2 participants 4 messages Sep 11, 2021 io_uring

lore
Share & tools
lore
#0 lore raw

v2 cover exec + timeout cancellation

Add some infra to test exec(), hopefully we will get more tests using it. And also add a timeout test, which uses exec.

v2: rebase

Pavel Begunkov (2):
tests: add no-op executable for exec
tests: test timeout cancellation fails links

 .gitignore         |  1 +
 test/Makefile      |  2 ++
 test/exec-target.c |  4 +++
 test/timeout.c     | 84 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 91 insertions(+)
 create mode 100644 test/exec-target.c
Signature · 1 line
2.33.0
#1 lore raw

PATCH v2 1/2 tests: add no-op executable for exec

There are differences between close and exec from io_uring perspective, so we want to test exec as well. For that we need a program doing nothing to exec into.

---
 .gitignore         | 1 +
 test/Makefile      | 2 ++
 test/exec-target.c | 4 ++++
 3 files changed, 7 insertions(+)
 create mode 100644 test/exec-target.c
.gitignore +1 -0
index df0f740..0213bfa 100644
@@ -129,6 +129,7 @@
 /test/sqpoll-cancel-hang
 /test/testfile
 /test/submit-link-fail
+/test/exec-target
 /test/*.dmesg
 
 config-host.h
test/Makefile +2 -0
index 2313fcc..54ee730 100644
@@ -135,6 +135,7 @@ test_targets += \
 	wakeup-hang \
 	sendmsg_fs_cve \
 	rsrc_tags \
+	exec-target \
 	# EOL
 
 all_targets += $(test_targets)
@@ -276,6 +277,7 @@ test_srcs := \
 	wakeup-hang.c \
 	sendmsg_fs_cve.c \
 	rsrc_tags.c \
+	exec-target.c \
 	# EOL
 
 test_objs := $(patsubst %.c,%.ol,$(patsubst %.cc,%.ol,$(test_srcs)))
test/exec-target.c new file +4 -0
new file mode 100644
index 0000000..50bc2c9
@@ -0,0 +1,4 @@
+int main(int argc, char *argv[])
+{
+	return 0;
+}
Signature · 1 line
2.33.0
#2 lore raw

PATCH v2 2/2 tests: test timeout cancellation fails links

In reply to Pavel Begunkov

Test that we appropriately fail linked requests when we cancel a normal timeout.

---
 test/timeout.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
test/timeout.c +84 -0
index d2e4930..cef6846 100644
@@ -10,6 +10,8 @@
 #include <string.h>
 #include <fcntl.h>
 #include <sys/time.h>
+#include <sys/wait.h>
+#include <sys/types.h>
 
 #include "liburing.h"
 #include "../src/syscall.h"
@@ -1171,6 +1173,82 @@ err:
 	return 1;
 }
 
+static int test_timeout_link_cancel(void)
+{
+	struct io_uring ring;
+	struct io_uring_cqe *cqe;
+	pid_t p;
+	int ret, i, wstatus;
+
+	ret = io_uring_queue_init(8, &ring, 0);
+	if (ret) {
+		fprintf(stderr, "ring create failed: %d\n", ret);
+		return 1;
+	}
+
+	p = fork();
+	if (p == -1) {
+		fprintf(stderr, "fork() failed\n");
+		return 1;
+	}
+
+	if (p == 0) {
+		struct io_uring_sqe *sqe;
+		struct __kernel_timespec ts;
+		const char *prog_path = "./exec-target";
+
+		msec_to_ts(&ts, 10000);
+		sqe = io_uring_get_sqe(&ring);
+		io_uring_prep_timeout(sqe, &ts, 0, 0);
+		sqe->flags |= IOSQE_IO_LINK;
+		sqe->user_data = 0;
+
+		sqe = io_uring_get_sqe(&ring);
+		io_uring_prep_nop(sqe);
+		sqe->user_data = 1;
+
+		ret = io_uring_submit(&ring);
+		if (ret != 2) {
+			fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
+			exit(1);
+		}
+
+		/* trigger full cancellation */
+		ret = execl(prog_path, prog_path, NULL);
+		if (ret) {
+			fprintf(stderr, "exec failed %i\n", errno);
+			exit(1);
+		}
+		exit(0);
+	}
+
+	if (waitpid(p, &wstatus, 0) == (pid_t)-1) {
+		perror("waitpid()");
+		return 1;
+	}
+	if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) {
+		fprintf(stderr, "child failed %i\n", WEXITSTATUS(wstatus));
+		return 1;
+	}
+
+	for (i = 0; i < 2; ++i) {
+		ret = io_uring_wait_cqe(&ring, &cqe);
+		if (ret) {
+			fprintf(stderr, "wait_cqe=%d\n", ret);
+			return 1;
+		}
+		if (cqe->res != -ECANCELED) {
+			fprintf(stderr, "invalid result, user_data: %i res: %i\n",
+					(int)cqe->user_data, cqe->res);
+			return 1;
+		}
+		io_uring_cqe_seen(&ring, cqe);
+	}
+
+	io_uring_queue_exit(&ring);
+	return 0;
+}
+
 int main(int argc, char *argv[])
 {
 	struct io_uring ring, sqpoll_ring;
@@ -1348,6 +1426,12 @@ int main(int argc, char *argv[])
 		return ret;
 	}
 
+	ret = test_timeout_link_cancel();
+	if (ret) {
+		fprintf(stderr, "test_timeout_link_cancel failed\n");
+		return ret;
+	}
+
 	if (sqpoll)
 		io_uring_queue_exit(&sqpoll_ring);
 	return 0;
Signature · 1 line
2.33.0
maintainer Applied
#3 lore raw
In reply to Pavel Begunkov
Quoted 2 lines · Pavel Begunkov

Add some infra to test exec(), hopefully we will get more tests using it. And also add a timeout test, which uses exec.

Applied, thanks.

Signature · 1 line
Jens Axboe