xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/timerfd/timerfd04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker 
4*49cdfc7eSAndroid Build Coastguard Worker   Copyright (c) 2020 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker    Test that timerfd adds correctly an offset with absolute expiration time.
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker    After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the
12*49cdfc7eSAndroid Build Coastguard Worker    process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC
13*49cdfc7eSAndroid Build Coastguard Worker    and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'.
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "time64_variants.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_clocks.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_timerfd.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define SLEEP_US 40000
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
27*49cdfc7eSAndroid Build Coastguard Worker 	int clk_id;
28*49cdfc7eSAndroid Build Coastguard Worker 	int clk_off;
29*49cdfc7eSAndroid Build Coastguard Worker 	int off;
30*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
31*49cdfc7eSAndroid Build Coastguard Worker 	{CLOCK_MONOTONIC, CLOCK_MONOTONIC, 10},
32*49cdfc7eSAndroid Build Coastguard Worker 	{CLOCK_BOOTTIME, CLOCK_BOOTTIME, 10},
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	{CLOCK_MONOTONIC, CLOCK_MONOTONIC, -10},
35*49cdfc7eSAndroid Build Coastguard Worker 	{CLOCK_BOOTTIME, CLOCK_BOOTTIME, -10},
36*49cdfc7eSAndroid Build Coastguard Worker };
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker static struct time64_variants variants[] = {
39*49cdfc7eSAndroid Build Coastguard Worker #if (__NR_timerfd_settime != __LTP__NR_INVALID_SYSCALL)
40*49cdfc7eSAndroid Build Coastguard Worker 	{ .clock_gettime = sys_clock_gettime, .tfd_settime = sys_timerfd_settime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
41*49cdfc7eSAndroid Build Coastguard Worker #endif
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker #if (__NR_timerfd_settime64 != __LTP__NR_INVALID_SYSCALL)
44*49cdfc7eSAndroid Build Coastguard Worker 	{ .clock_gettime = sys_clock_gettime64, .tfd_settime = sys_timerfd_settime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
45*49cdfc7eSAndroid Build Coastguard Worker #endif
46*49cdfc7eSAndroid Build Coastguard Worker };
47*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)48*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
verify_timerfd(unsigned int n)53*49cdfc7eSAndroid Build Coastguard Worker static void verify_timerfd(unsigned int n)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker 	struct time64_variants *tv = &variants[tst_variant];
56*49cdfc7eSAndroid Build Coastguard Worker 	struct tst_ts start, end;
57*49cdfc7eSAndroid Build Coastguard Worker 	struct tst_its it;
58*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	start.type = end.type = it.type = tv->ts_type;
61*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNSHARE(CLONE_NEWTIME);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0",
64*49cdfc7eSAndroid Build Coastguard Worker 	                 tc->clk_off, tc->off);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	if (tv->clock_gettime(tc->clk_id, tst_ts_get(&start))) {
67*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "clock_gettime(2) failed for clock %s",
68*49cdfc7eSAndroid Build Coastguard Worker 			tst_clock_name(tc->clk_id));
69*49cdfc7eSAndroid Build Coastguard Worker 		return;
70*49cdfc7eSAndroid Build Coastguard Worker 	}
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	end = tst_ts_add_us(start, 1000000 * tc->off + SLEEP_US);
73*49cdfc7eSAndroid Build Coastguard Worker 	tst_its_set_interval_sec(&it, 0);
74*49cdfc7eSAndroid Build Coastguard Worker 	tst_its_set_interval_nsec(&it, 0);
75*49cdfc7eSAndroid Build Coastguard Worker 	tst_its_set_value_from_ts(&it, end);
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	if (!SAFE_FORK()) {
78*49cdfc7eSAndroid Build Coastguard Worker 		uint64_t exp;
79*49cdfc7eSAndroid Build Coastguard Worker 		int fd = SAFE_TIMERFD_CREATE(tc->clk_id, 0);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 		if (tv->tfd_settime(fd, TFD_TIMER_ABSTIME, tst_its_get(&it), NULL)) {
82*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TERRNO, "timerfd_settime() failed");
83*49cdfc7eSAndroid Build Coastguard Worker 			return;
84*49cdfc7eSAndroid Build Coastguard Worker 		}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_READ(1, fd, &exp, sizeof(exp));
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 		if (exp != 1)
89*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "Got %llu expirations", (long long unsigned)exp);
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
92*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
93*49cdfc7eSAndroid Build Coastguard Worker 	}
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAIT(NULL);
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	if (tv->clock_gettime(tc->clk_id, tst_ts_get(&end))) {
98*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "clock_gettime(2) failed for clock %s",
99*49cdfc7eSAndroid Build Coastguard Worker 			tst_clock_name(tc->clk_id));
100*49cdfc7eSAndroid Build Coastguard Worker 		return;
101*49cdfc7eSAndroid Build Coastguard Worker 	}
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	long long diff = tst_ts_diff_us(end, start);
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	if (diff > 5 * SLEEP_US) {
106*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "timerfd %s slept too long %lli",
107*49cdfc7eSAndroid Build Coastguard Worker 		        tst_clock_name(tc->clk_id), diff);
108*49cdfc7eSAndroid Build Coastguard Worker 		return;
109*49cdfc7eSAndroid Build Coastguard Worker 	}
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	if (diff < SLEEP_US) {
112*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "timerfd %s slept too short %lli",
113*49cdfc7eSAndroid Build Coastguard Worker 		        tst_clock_name(tc->clk_id), diff);
114*49cdfc7eSAndroid Build Coastguard Worker 		return;
115*49cdfc7eSAndroid Build Coastguard Worker 	}
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "timerfd %s slept correctly %lli",
118*49cdfc7eSAndroid Build Coastguard Worker 		tst_clock_name(tc->clk_id), diff);
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
122*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
123*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_timerfd,
124*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = ARRAY_SIZE(variants),
125*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
126*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
127*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
128*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
129*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_TIME_NS=y",
130*49cdfc7eSAndroid Build Coastguard Worker 		NULL
131*49cdfc7eSAndroid Build Coastguard Worker 	}
132*49cdfc7eSAndroid Build Coastguard Worker };
133