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/kref.h> 8 #include <linux/nsproxy.h> 9 #include <linux/ns_common.h> 10 #include <linux/err.h> 11 12 struct user_namespace; 13 extern struct user_namespace init_user_ns; 14 15 struct timens_offsets { 16 struct timespec64 monotonic; 17 struct timespec64 boottime; 18 }; 19 20 struct time_namespace { 21 struct kref kref; 22 struct user_namespace *user_ns; 23 struct ucounts *ucounts; 24 struct ns_common ns; 25 struct timens_offsets offsets; 26 struct page *vvar_page; 27 /* If set prevents changing offsets after any task joined namespace. */ 28 bool frozen_offsets; 29 } __randomize_layout; 30 31 extern struct time_namespace init_time_ns; 32 33 #ifdef CONFIG_TIME_NS 34 static inline struct time_namespace *get_time_ns(struct time_namespace *ns) 35 { 36 kref_get(&ns->kref); 37 return ns; 38 } 39 40 struct time_namespace *copy_time_ns(unsigned long flags, 41 struct user_namespace *user_ns, 42 struct time_namespace *old_ns); 43 void free_time_ns(struct kref *kref); 44 int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk); 45 struct vdso_data *arch_get_vdso_data(void *vvar_page); 46 47 static inline void put_time_ns(struct time_namespace *ns) 48 { 49 kref_put(&ns->kref, free_time_ns); 50 } 51 52 static inline void timens_add_monotonic(struct timespec64 *ts) 53 { 54 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; 55 56 *ts = timespec64_add(*ts, ns_offsets->monotonic); 57 } 58 59 static inline void timens_add_boottime(struct timespec64 *ts) 60 { 61 struct timens_offsets *ns_offsets = ¤t->nsproxy->time_ns->offsets; 62 63 *ts = timespec64_add(*ts, ns_offsets->boottime); 64 } 65 66 ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim, 67 struct timens_offsets *offsets); 68 69 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim) 70 { 71 struct time_namespace *ns = current->nsproxy->time_ns; 72 73 if (likely(ns == &init_time_ns)) 74 return tim; 75 76 return do_timens_ktime_to_host(clockid, tim, &ns->offsets); 77 } 78 79 #else 80 static inline struct time_namespace *get_time_ns(struct time_namespace *ns) 81 { 82 return NULL; 83 } 84 85 static inline void put_time_ns(struct time_namespace *ns) 86 { 87 } 88 89 static inline 90 struct time_namespace *copy_time_ns(unsigned long flags, 91 struct user_namespace *user_ns, 92 struct time_namespace *old_ns) 93 { 94 if (flags & CLONE_NEWTIME) 95 return ERR_PTR(-EINVAL); 96 97 return old_ns; 98 } 99 100 static inline int timens_on_fork(struct nsproxy *nsproxy, 101 struct task_struct *tsk) 102 { 103 return 0; 104 } 105 106 static inline void timens_add_monotonic(struct timespec64 *ts) { } 107 static inline void timens_add_boottime(struct timespec64 *ts) { } 108 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim) 109 { 110 return tim; 111 } 112 #endif 113 114 #endif /* _LINUX_TIMENS_H */ 115