1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
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/namespaces_constants.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
41 static struct time64_variants variants[] = {
42 { .clock_gettime = libc_clock_gettime, .ts_type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"},
43
44 #if (__NR_clock_gettime != __LTP__NR_INVALID_SYSCALL)
45 { .clock_gettime = sys_clock_gettime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
46 #endif
47
48 #if (__NR_clock_gettime64 != __LTP__NR_INVALID_SYSCALL)
49 { .clock_gettime = sys_clock_gettime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
50 #endif
51 };
52
child(struct time64_variants * tv,struct tcase * tc)53 static void child(struct time64_variants *tv, struct tcase *tc)
54 {
55 long long diff;
56
57 if (tv->clock_gettime(tc->clk_id, tst_ts_get(&then))) {
58 tst_res(TFAIL | TERRNO, "clock_gettime(%s) failed",
59 tst_clock_name(tc->clk_id));
60 return;
61 }
62
63 SAFE_SETNS(parent_ns, CLONE_NEWTIME);
64
65 if (tv->clock_gettime(tc->clk_id, tst_ts_get(&parent_then))) {
66 tst_res(TFAIL | TERRNO, "clock_gettime(%s) failed",
67 tst_clock_name(tc->clk_id));
68 return;
69 }
70
71 diff = tst_ts_diff_ms(then, now);
72
73 if (diff - tc->off * 1000 > 10) {
74 tst_res(TFAIL, "Wrong offset (%s) read %llims",
75 tst_clock_name(tc->clk_id), diff);
76 } else {
77 tst_res(TPASS, "Offset (%s) is correct %llims",
78 tst_clock_name(tc->clk_id), diff);
79 }
80
81 diff = tst_ts_diff_ms(parent_then, now);
82
83 if (diff > 10) {
84 tst_res(TFAIL, "Wrong offset (%s) read %llims",
85 tst_clock_name(tc->clk_id), diff);
86 } else {
87 tst_res(TPASS, "Offset (%s) is correct %llims",
88 tst_clock_name(tc->clk_id), diff);
89 }
90 }
91
verify_ns_clock(unsigned int n)92 static void verify_ns_clock(unsigned int n)
93 {
94 struct time64_variants *tv = &variants[tst_variant];
95 struct tcase *tc = &tcases[n];
96
97 SAFE_UNSHARE(CLONE_NEWTIME);
98
99 SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0",
100 tc->clk_off, tc->off);
101
102 if (tv->clock_gettime(tc->clk_id, tst_ts_get(&now))) {
103 tst_res(TFAIL | TERRNO, "%d clock_gettime(%s) failed",
104 __LINE__, tst_clock_name(tc->clk_id));
105 return;
106 }
107
108 if (!SAFE_FORK())
109 child(tv, tc);
110 }
111
setup(void)112 static void setup(void)
113 {
114 struct time64_variants *tv = &variants[tst_variant];
115
116 now.type = then.type = parent_then.type = tv->ts_type;
117 tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
118 parent_ns = SAFE_OPEN("/proc/self/ns/time_for_children", O_RDONLY);
119 }
120
cleanup(void)121 static void cleanup(void)
122 {
123 SAFE_CLOSE(parent_ns);
124 }
125
126 static struct tst_test test = {
127 .setup = setup,
128 .cleanup = cleanup,
129 .tcnt = ARRAY_SIZE(tcases),
130 .test = verify_ns_clock,
131 .test_variants = ARRAY_SIZE(variants),
132 .needs_root = 1,
133 .forks_child = 1,
134 .needs_kconfigs = (const char *[]) {
135 "CONFIG_TIME_NS=y",
136 NULL
137 }
138 };
139