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/kref.h>
8 #include <linux/nsproxy.h>
9 #include <linux/ns_common.h>
10 #include <linux/err.h>
12 struct user_namespace;
13 extern struct user_namespace init_user_ns;
15 struct timens_offsets {
16 struct timespec64 monotonic;
17 struct timespec64 boottime;
20 struct time_namespace {
22 struct user_namespace *user_ns;
23 struct ucounts *ucounts;
25 struct timens_offsets offsets;
26 struct page *vvar_page;
27 /* If set prevents changing offsets after any task joined namespace. */
31 extern struct time_namespace init_time_ns;
34 extern int vdso_join_timens(struct task_struct *task,
35 struct time_namespace *ns);
37 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
43 struct time_namespace *copy_time_ns(unsigned long flags,
44 struct user_namespace *user_ns,
45 struct time_namespace *old_ns);
46 void free_time_ns(struct kref *kref);
47 int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
48 struct vdso_data *arch_get_vdso_data(void *vvar_page);
50 static inline void put_time_ns(struct time_namespace *ns)
52 kref_put(&ns->kref, free_time_ns);
55 void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
57 struct proc_timens_offset {
59 struct timespec64 val;
62 int proc_timens_set_offset(struct file *file, struct task_struct *p,
63 struct proc_timens_offset *offsets, int n);
65 static inline void timens_add_monotonic(struct timespec64 *ts)
67 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
69 *ts = timespec64_add(*ts, ns_offsets->monotonic);
72 static inline void timens_add_boottime(struct timespec64 *ts)
74 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets;
76 *ts = timespec64_add(*ts, ns_offsets->boottime);
79 ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
80 struct timens_offsets *offsets);
82 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
84 struct time_namespace *ns = current->nsproxy->time_ns;
86 if (likely(ns == &init_time_ns))
89 return do_timens_ktime_to_host(clockid, tim, &ns->offsets);
93 static inline int vdso_join_timens(struct task_struct *task,
94 struct time_namespace *ns)
99 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
104 static inline void put_time_ns(struct time_namespace *ns)
109 struct time_namespace *copy_time_ns(unsigned long flags,
110 struct user_namespace *user_ns,
111 struct time_namespace *old_ns)
113 if (flags & CLONE_NEWTIME)
114 return ERR_PTR(-EINVAL);
119 static inline int timens_on_fork(struct nsproxy *nsproxy,
120 struct task_struct *tsk)
125 static inline void timens_add_monotonic(struct timespec64 *ts) { }
126 static inline void timens_add_boottime(struct timespec64 *ts) { }
127 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
133 #endif /* _LINUX_TIMENS_H */