at master 4.2 kB view raw
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 12struct user_namespace; 13extern struct user_namespace init_user_ns; 14 15struct seq_file; 16struct vm_area_struct; 17 18struct timens_offsets { 19 struct timespec64 monotonic; 20 struct timespec64 boottime; 21}; 22 23struct 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 33extern struct time_namespace init_time_ns; 34 35#ifdef CONFIG_TIME_NS 36static inline struct time_namespace *to_time_ns(struct ns_common *ns) 37{ 38 return container_of(ns, struct time_namespace, ns); 39} 40void __init time_ns_init(void); 41extern int vdso_join_timens(struct task_struct *task, 42 struct time_namespace *ns); 43extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns); 44 45static inline struct time_namespace *get_time_ns(struct time_namespace *ns) 46{ 47 ns_ref_inc(ns); 48 return ns; 49} 50 51struct time_namespace *copy_time_ns(u64 flags, 52 struct user_namespace *user_ns, 53 struct time_namespace *old_ns); 54void free_time_ns(struct time_namespace *ns); 55void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk); 56struct page *find_timens_vvar_page(struct vm_area_struct *vma); 57 58static inline void put_time_ns(struct time_namespace *ns) 59{ 60 if (ns_ref_put(ns)) 61 free_time_ns(ns); 62} 63 64void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m); 65 66struct proc_timens_offset { 67 int clockid; 68 struct timespec64 val; 69}; 70 71int proc_timens_set_offset(struct file *file, struct task_struct *p, 72 struct proc_timens_offset *offsets, int n); 73 74static inline void timens_add_monotonic(struct timespec64 *ts) 75{ 76 struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets; 77 78 *ts = timespec64_add(*ts, ns_offsets->monotonic); 79} 80 81static inline void timens_add_boottime(struct timespec64 *ts) 82{ 83 struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets; 84 85 *ts = timespec64_add(*ts, ns_offsets->boottime); 86} 87 88static inline u64 timens_add_boottime_ns(u64 nsec) 89{ 90 struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets; 91 92 return nsec + timespec64_to_ns(&ns_offsets->boottime); 93} 94 95static inline void timens_sub_boottime(struct timespec64 *ts) 96{ 97 struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets; 98 99 *ts = timespec64_sub(*ts, ns_offsets->boottime); 100} 101 102ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim, 103 struct timens_offsets *offsets); 104 105static 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, &ns->offsets); 113} 114 115#else 116static inline void __init time_ns_init(void) 117{ 118} 119 120static inline int vdso_join_timens(struct task_struct *task, 121 struct time_namespace *ns) 122{ 123 return 0; 124} 125 126static inline void timens_commit(struct task_struct *tsk, 127 struct time_namespace *ns) 128{ 129} 130 131static inline struct time_namespace *get_time_ns(struct time_namespace *ns) 132{ 133 return NULL; 134} 135 136static inline void put_time_ns(struct time_namespace *ns) 137{ 138} 139 140static inline 141struct 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 151static inline void timens_on_fork(struct nsproxy *nsproxy, 152 struct task_struct *tsk) 153{ 154 return; 155} 156 157static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma) 158{ 159 return NULL; 160} 161 162static inline void timens_add_monotonic(struct timespec64 *ts) { } 163static inline void timens_add_boottime(struct timespec64 *ts) { } 164 165static inline u64 timens_add_boottime_ns(u64 nsec) 166{ 167 return nsec; 168} 169 170static inline void timens_sub_boottime(struct timespec64 *ts) { } 171 172static 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 */