1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Cyril Hrubis <[email protected]>
4 */
5
6 /*
7 * [Description]
8 *
9 * After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the
10 * process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC
11 * and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'.
12 *
13 * The child processes also switch to the initial parent namespace and checks
14 * that the offset is set to 0.
15 */
16
17 #define _GNU_SOURCE
18 #include "time64_variants.h"
19 #include "tst_safe_clocks.h"
20 #include "tst_timer.h"
21 #include "lapi/sched.h"
22
23 static struct tcase {
24 int clk_id;
25 int clk_off;
26 int off;
27 } tcases[] = {
28 {CLOCK_MONOTONIC, CLOCK_MONOTONIC, 10},
29 {CLOCK_BOOTTIME, CLOCK_BOOTTIME, 10},
30
31 {CLOCK_MONOTONIC, CLOCK_MONOTONIC, -10},
32 {CLOCK_BOOTTIME, CLOCK_BOOTTIME, -10},
33
34 {CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC, 100},
35 {CLOCK_MONOTONIC_COARSE, CLOCK_MONOTONIC, 100},
36 };
37
38 static struct tst_ts now, then, parent_then;
39 static int parent_ns;
40 static long long delta = 10;
41
42 static struct time64_variants variants[] = {
43 { .clock_gettime = libc_clock_gettime, .ts_type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"},
44
45 #if (__NR_clock_gettime != __LTP__NR_INVALID_SYSCALL)
46 { .clock_gettime = sys_clock_gettime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
47 #endif
48
49 #if (__NR_clock_gettime64 != __LTP__NR_INVALID_SYSCALL)
50 { .clock_gettime = sys_clock_gettime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
51 #endif
52 };
53
child(struct time64_variants * tv,struct tcase * tc)54 static void child(struct time64_variants *tv, struct tcase *tc)
55 {
56 long long diff;
57
58 if (tv->clock_gettime(tc->clk_id, tst_ts_get(&then))) {
59 tst_res(TFAIL | TERRNO, "clock_gettime(%s) failed",
60 tst_clock_name(tc->clk_id));
61 return;
62 }
63
64 SAFE_SETNS(parent_ns, CLONE_NEWTIME);
65
66 if (tv->clock_gettime(tc->clk_id, tst_ts_get(&parent_then))) {
67 tst_res(TFAIL | TERRNO, "clock_gettime(%s) failed",
68 tst_clock_name(tc->clk_id));
69 return;
70 }
71
72 diff = tst_ts_diff_ms(then, now);
73
74 if (diff - tc->off * 1000 > delta) {
75 tst_res(TFAIL, "Wrong offset (%s) read %llims",
76 tst_clock_name(tc->clk_id), diff);
77 } else {
78 tst_res(TPASS, "Offset (%s) is correct %llims",
79 tst_clock_name(tc->clk_id), diff);
80 }
81
82 diff = tst_ts_diff_ms(parent_then, now);
83
84 if (diff > delta) {
85 tst_res(TFAIL, "Wrong offset (%s) read %llims",
86 tst_clock_name(tc->clk_id), diff);
87 } else {
88 tst_res(TPASS, "Offset (%s) is correct %llims",
89 tst_clock_name(tc->clk_id), diff);
90 }
91 }
92
verify_ns_clock(unsigned int n)93 static void verify_ns_clock(unsigned int n)
94 {
95 struct time64_variants *tv = &variants[tst_variant];
96 struct tcase *tc = &tcases[n];
97
98 SAFE_UNSHARE(CLONE_NEWTIME);
99
100 SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0",
101 tc->clk_off, tc->off);
102
103 if (tv->clock_gettime(tc->clk_id, tst_ts_get(&now))) {
104 tst_res(TFAIL | TERRNO, "%d clock_gettime(%s) failed",
105 __LINE__, tst_clock_name(tc->clk_id));
106 return;
107 }
108
109 if (!SAFE_FORK())
110 child(tv, tc);
111 }
112
setup(void)113 static void setup(void)
114 {
115 struct time64_variants *tv = &variants[tst_variant];
116
117 if (tst_is_virt(VIRT_ANY)) {
118 tst_res(TINFO, "Running in a VM, multiply the delta by 10.");
119 delta *= 10;
120 }
121
122 now.type = then.type = parent_then.type = tv->ts_type;
123 tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
124 parent_ns = SAFE_OPEN("/proc/self/ns/time_for_children", O_RDONLY);
125 }
126
cleanup(void)127 static void cleanup(void)
128 {
129 SAFE_CLOSE(parent_ns);
130 }
131
132 static struct tst_test test = {
133 .setup = setup,
134 .cleanup = cleanup,
135 .tcnt = ARRAY_SIZE(tcases),
136 .test = verify_ns_clock,
137 .test_variants = ARRAY_SIZE(variants),
138 .needs_root = 1,
139 .forks_child = 1,
140 .needs_kconfigs = (const char *[]) {
141 "CONFIG_TIME_NS=y",
142 NULL
143 }
144 };
145