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