1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
4 *
5 * 1. Block on a bitset futex and wait for timeout, the difference between
6 * normal futex and bitset futex is that that the later have absolute timeout.
7 * 2. Check that the futex waited for expected time.
8 */
9
10 #include "tst_test.h"
11 #include "tst_timer.h"
12 #include "futextest.h"
13
14 #define THRESHOLD_US 100000
15 #define DEFAULT_TIMEOUT_US 100010
16
17 static struct test_case_t {
18 clockid_t clk_id;
19 } tcases[] = {
20 { CLOCK_MONOTONIC },
21 { CLOCK_REALTIME }
22 };
23
24 static struct futex_test_variants variants[] = {
25 #if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
26 { .fntype = FUTEX_FN_FUTEX, .tstype = TST_KERN_OLD_TIMESPEC, .gettime = sys_clock_gettime, .desc = "syscall with old kernel spec"},
27 #endif
28
29 #if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
30 { .fntype = FUTEX_FN_FUTEX64, .tstype = TST_KERN_TIMESPEC, .gettime = sys_clock_gettime64, .desc = "syscall time64 with kernel spec"},
31 #endif
32 };
33
verify_futex_wait_bitset(long long wait_us,clock_t clk_id)34 static void verify_futex_wait_bitset(long long wait_us, clock_t clk_id)
35 {
36 struct futex_test_variants *tv = &variants[tst_variant];
37 struct tst_ts start, to, end;
38 futex_t futex = FUTEX_INITIALIZER;
39 u_int32_t bitset = 0xffffffff;
40 int flags = clk_id == CLOCK_REALTIME ? FUTEX_CLOCK_REALTIME : 0;
41
42 start.type = end.type = to.type = tv->tstype;
43
44 tst_res(TINFO, "testing futex_wait_bitset() timeout with %s",
45 clk_id == CLOCK_REALTIME ? "CLOCK_REALTIME" : "CLOCK_MONOTONIC");
46
47 tv->gettime(clk_id, tst_ts_get(&start));
48 to = tst_ts_add_us(start, wait_us);
49
50 TEST(futex_wait_bitset(tv->fntype, &futex, futex, &to, bitset, flags));
51
52 tv->gettime(clk_id, tst_ts_get(&end));
53
54 if (TST_RET != -1) {
55 tst_res(TFAIL, "futex_wait_bitset() returned %li, expected -1",
56 TST_RET);
57 return;
58 }
59
60 if (TST_ERR == ENOSYS) {
61 tst_res(TCONF,
62 "In this kernel, futex() does not support FUTEX_WAIT_BITSET operation");
63 return;
64 }
65
66 if (TST_ERR != ETIMEDOUT) {
67 tst_res(TFAIL | TTERRNO, "expected %s",
68 tst_strerrno(ETIMEDOUT));
69 return;
70 }
71
72 if (tst_ts_lt(end, to)) {
73 tst_res(TFAIL,
74 "futex_wait_bitset() woken up prematurely %llius, expected %llius",
75 tst_ts_diff_us(end, start), wait_us);
76 return;
77 }
78
79 if (tst_ts_diff_us(end, to) > THRESHOLD_US) {
80 tst_res(TFAIL,
81 "futex_wait_bitset() waited too long %llius, expected %llius",
82 tst_ts_diff_us(end, start), wait_us);
83 return;
84 }
85
86 tst_res(TPASS, "futex_wait_bitset() waited %llius, expected %llius",
87 tst_ts_diff_us(end, start), wait_us);
88 }
89
run(unsigned int n)90 static void run(unsigned int n)
91 {
92 verify_futex_wait_bitset(DEFAULT_TIMEOUT_US, tcases[n].clk_id);
93 }
94
setup(void)95 static void setup(void)
96 {
97 struct futex_test_variants *tv = &variants[tst_variant];
98
99 tst_res(TINFO, "Testing variant: %s", tv->desc);
100 futex_supported_by_kernel(tv->fntype);
101 }
102
103 static struct tst_test test = {
104 .setup = setup,
105 .test = run,
106 .tcnt = ARRAY_SIZE(tcases),
107 .test_variants = ARRAY_SIZE(variants),
108 };
109