Skip to contentSkip to conversation

test/futex, test/waitid: futex and waitid cancel results

PATCH New agxr 1 participant 3 messages Oct 24 io_uring

lore
Share & tools
lore

agxr

author
#0 lore raw

cover test/futex, test/waitid: futex and waitid cancel results

Tests to go with the kernel series "io_uring/cancel: futex and waitid cancel results".

First patch checks that a CANCEL_ALL of three futex waits, and of three waitids, completes with 3. Kernels without the kernel series' patch 1 complete it with 1 and fail it.

Second patch lets a single futex or waitid cancel complete with 0, as the kernel series' patch 2 makes it, as well as with 1 as kernels so far do.

agxr (2):
test/futex, test/waitid: check the count of a CANCEL_ALL test/futex, test/waitid: accept 0 for a single futex or waitid cancel

 test/futex.c  | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 test/waitid.c | 28 +++++++++++++++++++---------
 2 files changed, 65 insertions(+), 10 deletions(-)

base-commit: 236c1525ff1b2ea5870ecada04951eb2d053275e

Signature · 1 line
2.56.0
author
#1 lore raw

PATCH 1/2 test/futex, test/waitid: check the count of a CANCEL_ALL

Cancel three futex waits, and three waitids, that share a user_data with one IORING_ASYNC_CANCEL_ALL. The cancel completes with 3, the number of requests it canceled.

Kernels that lack "io_uring/cancel: count all canceled futex and waitid requests on CANCEL_ALL" complete the cancel with 1.

---
 test/futex.c  | 44 ++++++++++++++++++++++++++++++++++++++++++++
 test/waitid.c | 26 +++++++++++++++++---------
 2 files changed, 61 insertions(+), 9 deletions(-)
test/futex.c +44 -0
index 4dc5a305..265f3675 100644
@@ -474,6 +474,44 @@ static int test_invalid(void)
 	return 0;
 }
 
+static int test_cancel_all(void)
+{
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+	struct io_uring ring;
+	unsigned int futex = 0;
+	int ret, i;
+
+	ret = io_uring_queue_init(8, &ring, 0);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < 3; i++) {
+		sqe = io_uring_get_sqe(&ring);
+		io_uring_prep_futex_wait(sqe, &futex, 0, FUTEX_BITSET_MATCH_ANY,
+					 FUTEX2_SIZE_U32, 0);
+		sqe->user_data = 1;
+	}
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_cancel64(sqe, 1, IORING_ASYNC_CANCEL_ALL);
+	sqe->user_data = 2;
+	io_uring_submit(&ring);
+
+	for (i = 0; i < 4; i++) {
+		ret = io_uring_wait_cqe(&ring, &cqe);
+		if (ret)
+			return ret;
+		if (cqe->user_data == 2 && cqe->res != 3) {
+			fprintf(stderr, "cancel all res %d\n", cqe->res);
+			return 1;
+		}
+		io_uring_cqe_seen(&ring, cqe);
+	}
+
+	io_uring_queue_exit(&ring);
+	return 0;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret;
@@ -579,5 +617,11 @@ int main(int argc, char *argv[])
 		return T_EXIT_FAIL;
 	}
 
+	ret = test_cancel_all();
+	if (ret) {
+		fprintf(stderr, "cancel_all failed\n");
+		return T_EXIT_FAIL;
+	}
+
 	return T_EXIT_PASS;
 }
test/waitid.c +17 -9
index a8c3488f..371b5e5e 100644
@@ -211,9 +211,9 @@ static int test_ready(struct io_uring *ring)
 }
 
 /*
- * Test cancelation of pending waitid
+ * Test cancelation of nr pending waitids
  */
-static int test_cancel(struct io_uring *ring)
+static int test_cancel(struct io_uring *ring, int nr)
 {
 	struct io_uring_sqe *sqe;
 	struct io_uring_cqe *cqe;
@@ -226,19 +226,21 @@ static int test_cancel(struct io_uring *ring)
 		exit(0);
 	}
 
-	sqe = io_uring_get_sqe(ring);
-	io_uring_prep_waitid(sqe, P_PID, pid, NULL, WEXITED, 0);
-	sqe->user_data = 1;
+	for (i = 0; i < nr; i++) {
+		sqe = io_uring_get_sqe(ring);
+		io_uring_prep_waitid(sqe, P_PID, pid, NULL, WEXITED, 0);
+		sqe->user_data = 1;
+	}
 
 	io_uring_submit(ring);
 
 	sqe = io_uring_get_sqe(ring);
-	io_uring_prep_cancel64(sqe, 1, 0);
+	io_uring_prep_cancel64(sqe, 1, nr > 1 ? IORING_ASYNC_CANCEL_ALL : 0);
 	sqe->user_data = 2;
 
 	io_uring_submit(ring);
 
-	for (i = 0; i < 2; i++) {
+	for (i = 0; i < nr + 1; i++) {
 		ret = io_uring_wait_cqe(ring, &cqe);
 		if (ret) {
 			fprintf(stderr, "cqe wait: %d\n", ret);
@@ -248,7 +250,7 @@ static int test_cancel(struct io_uring *ring)
 			fprintf(stderr, "cqe res: %d\n", cqe->res);
 			return T_EXIT_FAIL;
 		}
-		if (cqe->user_data == 2 && cqe->res != 1) {
+		if (cqe->user_data == 2 && cqe->res != nr) {
 			fprintf(stderr, "cqe res: %d\n", cqe->res);
 			return T_EXIT_FAIL;
 		}
@@ -510,12 +512,18 @@ int main(int argc, char *argv[])
 		return T_EXIT_FAIL;
 	}
 
-	ret = test_cancel(&ring);
+	ret = test_cancel(&ring, 1);
 	if (ret == T_EXIT_FAIL) {
 		fprintf(stderr, "test_cancel failed\n");
 		return T_EXIT_FAIL;
 	}
 
+	ret = test_cancel(&ring, 3);
+	if (ret == T_EXIT_FAIL) {
+		fprintf(stderr, "test_cancel all failed\n");
+		return T_EXIT_FAIL;
+	}
+
 	ret = test_invalid_infop(&ring);
 	if (ret == T_EXIT_FAIL) {
 		fprintf(stderr, "test_invalid_infop failed\n");
Signature · 1 line
2.56.0
author
#2 lore raw

PATCH 2/2 test/futex, test/waitid: accept 0 for a single futex or waitid cancel

In reply to agxr

A cancel of one request completes with 0, as io_uring_prep_cancel(3) documents. For a futex wait or a waitid, kernels so far complete it with 1, and the tests expect that. "io_uring/cancel: complete single futex or waitid cancel with 0" makes it 0. Accept both.

---
 test/futex.c  | 3 ++-
 test/waitid.c | 4 +++-
 2 files changed, 5 insertions(+), 2 deletions(-)
test/futex.c +2 -1
index 265f3675..1aadfc0b 100644
@@ -244,7 +244,8 @@ static int test_order(int vectored, int async)
 
 	reg.addr = 2;
 	ret = io_uring_register_sync_cancel(&ring, &reg);
-	if (ret != 1) {
+	/* older kernels return 1 */
+	if (ret != 0 && ret != 1) {
 		fprintf(stderr, "Failed to cancel pending futex wait: %d\n", ret);
 		return 1;
 	}
test/waitid.c +3 -1
index 371b5e5e..a3828e6c 100644
@@ -250,7 +250,9 @@ static int test_cancel(struct io_uring *ring, int nr)
 			fprintf(stderr, "cqe res: %d\n", cqe->res);
 			return T_EXIT_FAIL;
 		}
-		if (cqe->user_data == 2 && cqe->res != nr) {
+		/* older kernels complete a single cancel with 1 */
+		if (cqe->user_data == 2 && cqe->res != nr &&
+		    !(nr == 1 && cqe->res == 0)) {
 			fprintf(stderr, "cqe res: %d\n", cqe->res);
 			return T_EXIT_FAIL;
 		}
Signature · 1 line
2.56.0