2021-12-03 13:28:28

by André Almeida

[permalink] [raw]
Subject: [PATCH RESEND 0/2] selftests: futex: Some improvements

This small patch series makes a futex_waitv() test more correct and test
the timeout for the new FUTEX_LOCK_PI2 opcode.

André Almeida (2):
selftests: futex: Make futex_waitv's invalid clockid test more robust
selftests: futex: Add FUTEX_LOCK_PI2 for timeout test

.../futex/functional/futex_wait_timeout.c | 14 +++++++++++++-
.../selftests/futex/functional/futex_waitv.c | 7 ++++++-
tools/testing/selftests/futex/include/futextest.h | 14 ++++++++++++++
3 files changed, 33 insertions(+), 2 deletions(-)

--
2.34.1



2021-12-03 13:28:37

by André Almeida

[permalink] [raw]
Subject: [PATCH RESEND 1/2] selftests: futex: Make futex_waitv's invalid clockid test more robust

If we use NULL for *waiters, we may be triggering a different error
path. Use a valid value for this arguments to make sure that the invalid
clockid is the one triggering the EINVAL return.

Signed-off-by: André Almeida <[email protected]>
---
tools/testing/selftests/futex/functional/futex_waitv.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/futex/functional/futex_waitv.c b/tools/testing/selftests/futex/functional/futex_waitv.c
index a94337f677e1..336b04dacd0e 100644
--- a/tools/testing/selftests/futex/functional/futex_waitv.c
+++ b/tools/testing/selftests/futex/functional/futex_waitv.c
@@ -217,12 +217,17 @@ int main(int argc, char *argv[])
}

/* Testing an invalid clockid */
+ waitv[0].uaddr = (uintptr_t)&futexes[0];
+ waitv[0].flags = FUTEX_PRIVATE_FLAG | FUTEX_32;
+ waitv[0].val = 0;
+ waitv[0].__reserved = 0;
+
if (clock_gettime(CLOCK_MONOTONIC, &to))
error("gettime64 failed\n", errno);

to.tv_sec++;

- res = futex_waitv(NULL, NR_FUTEXES, 0, &to, CLOCK_TAI);
+ res = futex_waitv(waitv, 1, 0, &to, CLOCK_TAI);
if (res == EINVAL) {
ksft_test_result_fail("futex_waitv private returned: %d %s\n",
res ? errno : res,
--
2.34.1


2021-12-03 13:28:44

by André Almeida

[permalink] [raw]
Subject: [PATCH RESEND 2/2] selftests: futex: Add FUTEX_LOCK_PI2 for timeout test

Since commit bf22a6976897 ("futex: Provide FUTEX_LOCK_PI2 to support clock
selection"), there's a new operation that supports CLOCK_MONOTONIC and
CLOCK_REALTIME. Test the timeout interface of FUTEX_LOCK_PI2 operation.

Signed-off-by: André Almeida <[email protected]>
---
.../futex/functional/futex_wait_timeout.c | 14 +++++++++++++-
tools/testing/selftests/futex/include/futextest.h | 14 ++++++++++++++
2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/futex/functional/futex_wait_timeout.c b/tools/testing/selftests/futex/functional/futex_wait_timeout.c
index 3651ce17beeb..8d0810eb6bf4 100644
--- a/tools/testing/selftests/futex/functional/futex_wait_timeout.c
+++ b/tools/testing/selftests/futex/functional/futex_wait_timeout.c
@@ -125,7 +125,7 @@ int main(int argc, char *argv[])
}

ksft_print_header();
- ksft_set_plan(9);
+ ksft_set_plan(11);
ksft_print_msg("%s: Block on a futex and wait for timeout\n",
basename(argv[0]));
ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
@@ -182,6 +182,18 @@ int main(int argc, char *argv[])
res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
test_timeout(res, &ret, "futex_lock_pi invalid timeout flag", ENOSYS);

+ /* FUTEX_LOCK_PI2 with CLOCK_REALTIME */
+ if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
+ return RET_FAIL;
+ res = futex_lock_pi2(&futex_pi, &to, 0, FUTEX_CLOCK_REALTIME);
+ test_timeout(res, &ret, "futex_lock_pi2 realtime", ETIMEDOUT);
+
+ /* FUTEX_LOCK_PI2 with CLOCK_MONOTONIC */
+ if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
+ return RET_FAIL;
+ res = futex_lock_pi2(&futex_pi, &to, 0, 0);
+ test_timeout(res, &ret, "futex_lock_pi2 monotonic", ETIMEDOUT);
+
/* futex_waitv with CLOCK_MONOTONIC */
if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
return RET_FAIL;
diff --git a/tools/testing/selftests/futex/include/futextest.h b/tools/testing/selftests/futex/include/futextest.h
index ddbcfc9b7bac..d5a04201cd1c 100644
--- a/tools/testing/selftests/futex/include/futextest.h
+++ b/tools/testing/selftests/futex/include/futextest.h
@@ -38,6 +38,9 @@ typedef volatile u_int32_t futex_t;
#ifndef FUTEX_CMP_REQUEUE_PI
#define FUTEX_CMP_REQUEUE_PI 12
#endif
+#ifndef FUTEX_PI2
+#define FUTEX_PI2 13
+#endif
#ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE
#define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX_WAIT_REQUEUE_PI | \
FUTEX_PRIVATE_FLAG)
@@ -124,6 +127,17 @@ futex_lock_pi(futex_t *uaddr, struct timespec *timeout, int detect,
return futex(uaddr, FUTEX_LOCK_PI, detect, timeout, NULL, 0, opflags);
}

+/**
+ * futex_lock_pi2() - block on uaddr as a PI mutex
+ * @detect: whether (1) or not (0) to perform deadlock detection
+ */
+static inline int
+futex_lock_pi2(futex_t *uaddr, struct timespec *timeout, int detect,
+ int opflags)
+{
+ return futex(uaddr, FUTEX_LOCK_PI2, detect, timeout, NULL, 0, opflags);
+}
+
/**
* futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter
*/
--
2.34.1