xref: /linux/include/linux/time_namespace.h (revision 18b19abc3709b109676ffd1f48dcd332c2e477d4)
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
to_time_ns(struct ns_common * ns)36 static inline struct time_namespace *to_time_ns(struct ns_common *ns)
37 {
38 	return container_of(ns, struct time_namespace, ns);
39 }
40 void __init time_ns_init(void);
41 extern int vdso_join_timens(struct task_struct *task,
42 			    struct time_namespace *ns);
43 extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
44 
get_time_ns(struct time_namespace * ns)45 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
46 {
47 	ns_ref_inc(ns);
48 	return ns;
49 }
50 
51 struct time_namespace *copy_time_ns(u64 flags,
52 				    struct user_namespace *user_ns,
53 				    struct time_namespace *old_ns);
54 void free_time_ns(struct time_namespace *ns);
55 void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
56 struct page *find_timens_vvar_page(struct vm_area_struct *vma);
57 
put_time_ns(struct time_namespace * ns)58 static inline void put_time_ns(struct time_namespace *ns)
59 {
60 	if (ns_ref_put(ns))
61 		free_time_ns(ns);
62 }
63 
64 void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
65 
66 struct proc_timens_offset {
67 	int			clockid;
68 	struct timespec64	val;
69 };
70 
71 int proc_timens_set_offset(struct file *file, struct task_struct *p,
72 			   struct proc_timens_offset *offsets, int n);
73 
timens_add_monotonic(struct timespec64 * ts)74 static inline void timens_add_monotonic(struct timespec64 *ts)
75 {
76 	struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
77 
78 	*ts = timespec64_add(*ts, ns_offsets->monotonic);
79 }
80 
timens_add_boottime(struct timespec64 * ts)81 static inline void timens_add_boottime(struct timespec64 *ts)
82 {
83 	struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
84 
85 	*ts = timespec64_add(*ts, ns_offsets->boottime);
86 }
87 
timens_add_boottime_ns(u64 nsec)88 static inline u64 timens_add_boottime_ns(u64 nsec)
89 {
90 	struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
91 
92 	return nsec + timespec64_to_ns(&ns_offsets->boottime);
93 }
94 
timens_sub_boottime(struct timespec64 * ts)95 static inline void timens_sub_boottime(struct timespec64 *ts)
96 {
97 	struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
98 
99 	*ts = timespec64_sub(*ts, ns_offsets->boottime);
100 }
101 
102 ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
103 				struct timens_offsets *offsets);
104 
timens_ktime_to_host(clockid_t clockid,ktime_t tim)105 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
106 {
107 	struct time_namespace *ns = current->nsproxy->time_ns;
108 
109 	if (likely(ns == &init_time_ns))
110 		return tim;
111 
112 	return do_timens_ktime_to_host(clockid, tim, &ns->offsets);
113 }
114 
115 #else
time_ns_init(void)116 static inline void __init time_ns_init(void)
117 {
118 }
119 
vdso_join_timens(struct task_struct * task,struct time_namespace * ns)120 static inline int vdso_join_timens(struct task_struct *task,
121 				   struct time_namespace *ns)
122 {
123 	return 0;
124 }
125 
timens_commit(struct task_struct * tsk,struct time_namespace * ns)126 static inline void timens_commit(struct task_struct *tsk,
127 				 struct time_namespace *ns)
128 {
129 }
130 
get_time_ns(struct time_namespace * ns)131 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
132 {
133 	return NULL;
134 }
135 
put_time_ns(struct time_namespace * ns)136 static inline void put_time_ns(struct time_namespace *ns)
137 {
138 }
139 
140 static inline
copy_time_ns(u64 flags,struct user_namespace * user_ns,struct time_namespace * old_ns)141 struct time_namespace *copy_time_ns(u64 flags,
142 				    struct user_namespace *user_ns,
143 				    struct time_namespace *old_ns)
144 {
145 	if (flags & CLONE_NEWTIME)
146 		return ERR_PTR(-EINVAL);
147 
148 	return old_ns;
149 }
150 
timens_on_fork(struct nsproxy * nsproxy,struct task_struct * tsk)151 static inline void timens_on_fork(struct nsproxy *nsproxy,
152 				 struct task_struct *tsk)
153 {
154 	return;
155 }
156 
find_timens_vvar_page(struct vm_area_struct * vma)157 static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
158 {
159 	return NULL;
160 }
161 
timens_add_monotonic(struct timespec64 * ts)162 static inline void timens_add_monotonic(struct timespec64 *ts) { }
timens_add_boottime(struct timespec64 * ts)163 static inline void timens_add_boottime(struct timespec64 *ts) { }
164 
timens_add_boottime_ns(u64 nsec)165 static inline u64 timens_add_boottime_ns(u64 nsec)
166 {
167 	return nsec;
168 }
169 
timens_sub_boottime(struct timespec64 * ts)170 static inline void timens_sub_boottime(struct timespec64 *ts) { }
171 
timens_ktime_to_host(clockid_t clockid,ktime_t tim)172 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
173 {
174 	return tim;
175 }
176 #endif
177 
178 #endif /* _LINUX_TIMENS_H */
179