Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_TIMENS_H
3#define _LINUX_TIMENS_H
4
5
6#include <linux/sched.h>
7#include <linux/nsproxy.h>
8#include <linux/ns_common.h>
9#include <linux/err.h>
10#include <linux/time64.h>
11#include <linux/cleanup.h>
12
13struct user_namespace;
14extern struct user_namespace init_user_ns;
15
16struct seq_file;
17struct vm_area_struct;
18
19struct timens_offsets {
20 struct timespec64 monotonic;
21 struct timespec64 boottime;
22};
23
24struct time_namespace {
25 struct user_namespace *user_ns;
26 struct ucounts *ucounts;
27 struct ns_common ns;
28 struct timens_offsets offsets;
29#ifdef CONFIG_TIME_NS_VDSO
30 struct page *vvar_page;
31#endif
32 /* If set prevents changing offsets after any task joined namespace. */
33 bool frozen_offsets;
34} __randomize_layout;
35
36extern struct time_namespace init_time_ns;
37
38#ifdef CONFIG_TIME_NS
39static inline struct time_namespace *to_time_ns(struct ns_common *ns)
40{
41 return container_of(ns, struct time_namespace, ns);
42}
43void __init time_ns_init(void);
44
45static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
46{
47 ns_ref_inc(ns);
48 return ns;
49}
50
51struct time_namespace *copy_time_ns(u64 flags,
52 struct user_namespace *user_ns,
53 struct time_namespace *old_ns);
54void free_time_ns(struct time_namespace *ns);
55void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
56
57static inline void put_time_ns(struct time_namespace *ns)
58{
59 if (ns_ref_put(ns))
60 free_time_ns(ns);
61}
62
63void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
64
65struct proc_timens_offset {
66 int clockid;
67 struct timespec64 val;
68};
69
70int proc_timens_set_offset(struct file *file, struct task_struct *p,
71 struct proc_timens_offset *offsets, int n);
72
73static inline void timens_add_monotonic(struct timespec64 *ts)
74{
75 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
76
77 *ts = timespec64_add(*ts, ns_offsets->monotonic);
78}
79
80static inline void timens_add_boottime(struct timespec64 *ts)
81{
82 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
83
84 *ts = timespec64_add(*ts, ns_offsets->boottime);
85}
86
87static inline u64 timens_add_boottime_ns(u64 nsec)
88{
89 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
90
91 return nsec + timespec64_to_ns(&ns_offsets->boottime);
92}
93
94static inline void timens_sub_boottime(struct timespec64 *ts)
95{
96 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
97
98 *ts = timespec64_sub(*ts, ns_offsets->boottime);
99}
100
101ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
102 struct timens_offsets *offsets);
103
104static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
105{
106 struct time_namespace *ns = current->nsproxy->time_ns;
107
108 if (likely(ns == &init_time_ns))
109 return tim;
110
111 return do_timens_ktime_to_host(clockid, tim, &ns->offsets);
112}
113
114#else
115static inline void __init time_ns_init(void)
116{
117}
118
119static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
120{
121 return NULL;
122}
123
124static inline void put_time_ns(struct time_namespace *ns)
125{
126}
127
128static inline
129struct time_namespace *copy_time_ns(u64 flags,
130 struct user_namespace *user_ns,
131 struct time_namespace *old_ns)
132{
133 if (flags & CLONE_NEWTIME)
134 return ERR_PTR(-EINVAL);
135
136 return old_ns;
137}
138
139static inline void timens_on_fork(struct nsproxy *nsproxy,
140 struct task_struct *tsk)
141{
142 return;
143}
144
145static inline void timens_add_monotonic(struct timespec64 *ts) { }
146static inline void timens_add_boottime(struct timespec64 *ts) { }
147
148static inline u64 timens_add_boottime_ns(u64 nsec)
149{
150 return nsec;
151}
152
153static inline void timens_sub_boottime(struct timespec64 *ts) { }
154
155static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
156{
157 return tim;
158}
159#endif
160
161#ifdef CONFIG_TIME_NS_VDSO
162extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
163struct page *find_timens_vvar_page(struct vm_area_struct *vma);
164#else /* !CONFIG_TIME_NS_VDSO */
165static inline void timens_commit(struct task_struct *tsk, struct time_namespace *ns)
166{
167}
168
169static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
170{
171 return NULL;
172}
173#endif /* CONFIG_TIME_NS_VDSO */
174
175DEFINE_FREE(time_ns, struct time_namespace *, if (_T) put_time_ns(_T))
176
177#endif /* _LINUX_TIMENS_H */