Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_TIMENS_H |
| 3 | #define _LINUX_TIMENS_H |
| 4 | |
| 5 | |
| 6 | #include <linux/sched.h> |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 7 | #include <linux/nsproxy.h> |
| 8 | #include <linux/ns_common.h> |
| 9 | #include <linux/err.h> |
| 10 | |
| 11 | struct user_namespace; |
| 12 | extern struct user_namespace init_user_ns; |
| 13 | |
Andrei Vagin | af993f5 | 2019-11-12 01:26:53 +0000 | [diff] [blame] | 14 | struct timens_offsets { |
| 15 | struct timespec64 monotonic; |
| 16 | struct timespec64 boottime; |
| 17 | }; |
| 18 | |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 19 | struct time_namespace { |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 20 | struct user_namespace *user_ns; |
| 21 | struct ucounts *ucounts; |
| 22 | struct ns_common ns; |
Andrei Vagin | af993f5 | 2019-11-12 01:26:53 +0000 | [diff] [blame] | 23 | struct timens_offsets offsets; |
Dmitry Safonov | afaa7b5 | 2019-11-12 01:27:12 +0000 | [diff] [blame] | 24 | struct page *vvar_page; |
| 25 | /* If set prevents changing offsets after any task joined namespace. */ |
| 26 | bool frozen_offsets; |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 27 | } __randomize_layout; |
| 28 | |
| 29 | extern struct time_namespace init_time_ns; |
| 30 | |
| 31 | #ifdef CONFIG_TIME_NS |
Dmitry Safonov | 70ddf65 | 2019-11-12 01:27:15 +0000 | [diff] [blame] | 32 | extern int vdso_join_timens(struct task_struct *task, |
| 33 | struct time_namespace *ns); |
Christian Brauner | 76c1288 | 2020-07-06 17:49:11 +0200 | [diff] [blame] | 34 | extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns); |
Dmitry Safonov | 70ddf65 | 2019-11-12 01:27:15 +0000 | [diff] [blame] | 35 | |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 36 | static inline struct time_namespace *get_time_ns(struct time_namespace *ns) |
| 37 | { |
Kirill Tkhai | 28c41ef | 2020-08-03 13:17:00 +0300 | [diff] [blame] | 38 | refcount_inc(&ns->ns.count); |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 39 | return ns; |
| 40 | } |
| 41 | |
| 42 | struct time_namespace *copy_time_ns(unsigned long flags, |
| 43 | struct user_namespace *user_ns, |
| 44 | struct time_namespace *old_ns); |
Kirill Tkhai | 28c41ef | 2020-08-03 13:17:00 +0300 | [diff] [blame] | 45 | void free_time_ns(struct time_namespace *ns); |
Hui Su | 5c62634 | 2020-11-18 00:17:50 +0800 | [diff] [blame] | 46 | void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk); |
Jann Horn | d6c494e | 2022-11-30 12:53:20 +0100 | [diff] [blame] | 47 | struct page *find_timens_vvar_page(struct vm_area_struct *vma); |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 48 | |
| 49 | static inline void put_time_ns(struct time_namespace *ns) |
| 50 | { |
Kirill Tkhai | 28c41ef | 2020-08-03 13:17:00 +0300 | [diff] [blame] | 51 | if (refcount_dec_and_test(&ns->ns.count)) |
| 52 | free_time_ns(ns); |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Andrei Vagin | 04a8682 | 2019-11-12 01:27:16 +0000 | [diff] [blame] | 55 | void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m); |
| 56 | |
| 57 | struct proc_timens_offset { |
| 58 | int clockid; |
| 59 | struct timespec64 val; |
| 60 | }; |
| 61 | |
| 62 | int proc_timens_set_offset(struct file *file, struct task_struct *p, |
| 63 | struct proc_timens_offset *offsets, int n); |
| 64 | |
Andrei Vagin | af993f5 | 2019-11-12 01:26:53 +0000 | [diff] [blame] | 65 | static inline void timens_add_monotonic(struct timespec64 *ts) |
| 66 | { |
| 67 | struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; |
| 68 | |
| 69 | *ts = timespec64_add(*ts, ns_offsets->monotonic); |
| 70 | } |
| 71 | |
| 72 | static inline void timens_add_boottime(struct timespec64 *ts) |
| 73 | { |
| 74 | struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; |
| 75 | |
| 76 | *ts = timespec64_add(*ts, ns_offsets->boottime); |
| 77 | } |
| 78 | |
Michael Weiß | 31909e3 | 2020-10-27 21:42:56 +0100 | [diff] [blame] | 79 | static inline u64 timens_add_boottime_ns(u64 nsec) |
| 80 | { |
| 81 | struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; |
| 82 | |
| 83 | return nsec + timespec64_to_ns(&ns_offsets->boottime); |
| 84 | } |
| 85 | |
| 86 | static inline void timens_sub_boottime(struct timespec64 *ts) |
| 87 | { |
| 88 | struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; |
| 89 | |
| 90 | *ts = timespec64_sub(*ts, ns_offsets->boottime); |
| 91 | } |
| 92 | |
Andrei Vagin | 89dd8ee | 2019-11-12 01:27:01 +0000 | [diff] [blame] | 93 | ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim, |
| 94 | struct timens_offsets *offsets); |
| 95 | |
| 96 | static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim) |
| 97 | { |
| 98 | struct time_namespace *ns = current->nsproxy->time_ns; |
| 99 | |
| 100 | if (likely(ns == &init_time_ns)) |
| 101 | return tim; |
| 102 | |
| 103 | return do_timens_ktime_to_host(clockid, tim, &ns->offsets); |
| 104 | } |
| 105 | |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 106 | #else |
Dmitry Safonov | 70ddf65 | 2019-11-12 01:27:15 +0000 | [diff] [blame] | 107 | static inline int vdso_join_timens(struct task_struct *task, |
| 108 | struct time_namespace *ns) |
| 109 | { |
| 110 | return 0; |
| 111 | } |
| 112 | |
Christian Brauner | 76c1288 | 2020-07-06 17:49:11 +0200 | [diff] [blame] | 113 | static inline void timens_commit(struct task_struct *tsk, |
| 114 | struct time_namespace *ns) |
| 115 | { |
| 116 | } |
| 117 | |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 118 | static inline struct time_namespace *get_time_ns(struct time_namespace *ns) |
| 119 | { |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | static inline void put_time_ns(struct time_namespace *ns) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | static inline |
| 128 | struct time_namespace *copy_time_ns(unsigned long flags, |
| 129 | struct user_namespace *user_ns, |
| 130 | struct time_namespace *old_ns) |
| 131 | { |
| 132 | if (flags & CLONE_NEWTIME) |
| 133 | return ERR_PTR(-EINVAL); |
| 134 | |
| 135 | return old_ns; |
| 136 | } |
| 137 | |
Hui Su | 5c62634 | 2020-11-18 00:17:50 +0800 | [diff] [blame] | 138 | static inline void timens_on_fork(struct nsproxy *nsproxy, |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 139 | struct task_struct *tsk) |
| 140 | { |
Hui Su | 5c62634 | 2020-11-18 00:17:50 +0800 | [diff] [blame] | 141 | return; |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Jann Horn | d6c494e | 2022-11-30 12:53:20 +0100 | [diff] [blame] | 144 | static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma) |
| 145 | { |
| 146 | return NULL; |
| 147 | } |
| 148 | |
Andrei Vagin | af993f5 | 2019-11-12 01:26:53 +0000 | [diff] [blame] | 149 | static inline void timens_add_monotonic(struct timespec64 *ts) { } |
| 150 | static inline void timens_add_boottime(struct timespec64 *ts) { } |
Michael Weiß | 31909e3 | 2020-10-27 21:42:56 +0100 | [diff] [blame] | 151 | |
| 152 | static inline u64 timens_add_boottime_ns(u64 nsec) |
| 153 | { |
| 154 | return nsec; |
| 155 | } |
| 156 | |
| 157 | static inline void timens_sub_boottime(struct timespec64 *ts) { } |
| 158 | |
Andrei Vagin | 89dd8ee | 2019-11-12 01:27:01 +0000 | [diff] [blame] | 159 | static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim) |
| 160 | { |
| 161 | return tim; |
| 162 | } |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 163 | #endif |
| 164 | |
Arnd Bergmann | b7a7ce1 | 2023-05-17 15:11:02 +0200 | [diff] [blame] | 165 | struct vdso_data *arch_get_vdso_data(void *vvar_page); |
| 166 | |
Andrei Vagin | 769071a | 2019-11-12 01:26:52 +0000 | [diff] [blame] | 167 | #endif /* _LINUX_TIMENS_H */ |