1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TIMENS_H
3 #define _LINUX_TIMENS_H
6 #include <linux/sched.h>
7 #include <linux/nsproxy.h>
8 #include <linux/ns_common.h>
11 struct user_namespace;
12 extern struct user_namespace init_user_ns;
14 struct timens_offsets {
15 struct timespec64 monotonic;
16 struct timespec64 boottime;
19 struct time_namespace {
20 struct user_namespace *user_ns;
21 struct ucounts *ucounts;
23 struct timens_offsets offsets;
24 struct page *vvar_page;
25 /* If set prevents changing offsets after any task joined namespace. */
29 extern struct time_namespace init_time_ns;
32 extern int vdso_join_timens(struct task_struct *task,
33 struct time_namespace *ns);
34 extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
36 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
38 refcount_inc(&ns->ns.count);
42 struct time_namespace *copy_time_ns(unsigned long flags,
43 struct user_namespace *user_ns,
44 struct time_namespace *old_ns);
45 void free_time_ns(struct time_namespace *ns);
46 void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
47 struct vdso_data *arch_get_vdso_data(void *vvar_page);
48 struct page *find_timens_vvar_page(struct vm_area_struct *vma);
50 static inline void put_time_ns(struct time_namespace *ns)
52 if (refcount_dec_and_test(&ns->ns.count))
56 void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
58 struct proc_timens_offset {
60 struct timespec64 val;
63 int proc_timens_set_offset(struct file *file, struct task_struct *p,
64 struct proc_timens_offset *offsets, int n);
66 static inline void timens_add_monotonic(struct timespec64 *ts)
68 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
70 *ts = timespec64_add(*ts, ns_offsets->monotonic);
73 static inline void timens_add_boottime(struct timespec64 *ts)
75 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
77 *ts = timespec64_add(*ts, ns_offsets->boottime);
80 static inline u64 timens_add_boottime_ns(u64 nsec)
82 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
84 return nsec + timespec64_to_ns(&ns_offsets->boottime);
87 static inline void timens_sub_boottime(struct timespec64 *ts)
89 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
91 *ts = timespec64_sub(*ts, ns_offsets->boottime);
94 ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
95 struct timens_offsets *offsets);
97 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
99 struct time_namespace *ns = current->nsproxy->time_ns;
101 if (likely(ns == &init_time_ns))
104 return do_timens_ktime_to_host(clockid, tim, &ns->offsets);
108 static inline int vdso_join_timens(struct task_struct *task,
109 struct time_namespace *ns)
114 static inline void timens_commit(struct task_struct *tsk,
115 struct time_namespace *ns)
119 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
124 static inline void put_time_ns(struct time_namespace *ns)
129 struct time_namespace *copy_time_ns(unsigned long flags,
130 struct user_namespace *user_ns,
131 struct time_namespace *old_ns)
133 if (flags & CLONE_NEWTIME)
134 return ERR_PTR(-EINVAL);
139 static inline void timens_on_fork(struct nsproxy *nsproxy,
140 struct task_struct *tsk)
145 static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
150 static inline void timens_add_monotonic(struct timespec64 *ts) { }
151 static inline void timens_add_boottime(struct timespec64 *ts) { }
153 static inline u64 timens_add_boottime_ns(u64 nsec)
158 static inline void timens_sub_boottime(struct timespec64 *ts) { }
160 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
166 #endif /* _LINUX_TIMENS_H */