| 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 | 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 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 74 | static inline void timens_add_monotonic(struct timespec64 *ts) | 
|---|
| 75 | { | 
|---|
| 76 | struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; | 
|---|
| 77 |  | 
|---|
| 78 | *ts = timespec64_add(lhs: *ts, rhs: ns_offsets->monotonic); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | static inline void timens_add_boottime(struct timespec64 *ts) | 
|---|
| 82 | { | 
|---|
| 83 | struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; | 
|---|
| 84 |  | 
|---|
| 85 | *ts = timespec64_add(lhs: *ts, rhs: ns_offsets->boottime); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | static inline u64 timens_add_boottime_ns(u64 nsec) | 
|---|
| 89 | { | 
|---|
| 90 | struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; | 
|---|
| 91 |  | 
|---|
| 92 | return nsec + timespec64_to_ns(ts: &ns_offsets->boottime); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | static inline void timens_sub_boottime(struct timespec64 *ts) | 
|---|
| 96 | { | 
|---|
| 97 | struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; | 
|---|
| 98 |  | 
|---|
| 99 | *ts = timespec64_sub(lhs: *ts, rhs: 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 |  | 
|---|
| 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, offsets: &ns->offsets); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | #else | 
|---|
| 116 | static inline void __init time_ns_init(void) | 
|---|
| 117 | { | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | static inline int vdso_join_timens(struct task_struct *task, | 
|---|
| 121 | struct time_namespace *ns) | 
|---|
| 122 | { | 
|---|
| 123 | return 0; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | static inline void timens_commit(struct task_struct *tsk, | 
|---|
| 127 | struct time_namespace *ns) | 
|---|
| 128 | { | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | static inline struct time_namespace *get_time_ns(struct time_namespace *ns) | 
|---|
| 132 | { | 
|---|
| 133 | return NULL; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | static inline void put_time_ns(struct time_namespace *ns) | 
|---|
| 137 | { | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | static inline | 
|---|
| 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 |  | 
|---|
| 151 | static inline void timens_on_fork(struct nsproxy *nsproxy, | 
|---|
| 152 | struct task_struct *tsk) | 
|---|
| 153 | { | 
|---|
| 154 | return; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma) | 
|---|
| 158 | { | 
|---|
| 159 | return NULL; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | static inline void timens_add_monotonic(struct timespec64 *ts) { } | 
|---|
| 163 | static inline void timens_add_boottime(struct timespec64 *ts) { } | 
|---|
| 164 |  | 
|---|
| 165 | static inline u64 timens_add_boottime_ns(u64 nsec) | 
|---|
| 166 | { | 
|---|
| 167 | return nsec; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | static inline void timens_sub_boottime(struct timespec64 *ts) { } | 
|---|
| 171 |  | 
|---|
| 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 |  | 
|---|