xref: /linux-6.15/include/linux/time_namespace.h (revision 28c41efd)
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 
11 struct user_namespace;
12 extern struct user_namespace init_user_ns;
13 
14 struct timens_offsets {
15 	struct timespec64 monotonic;
16 	struct timespec64 boottime;
17 };
18 
19 struct time_namespace {
20 	struct user_namespace	*user_ns;
21 	struct ucounts		*ucounts;
22 	struct ns_common	ns;
23 	struct timens_offsets	offsets;
24 	struct page		*vvar_page;
25 	/* If set prevents changing offsets after any task joined namespace. */
26 	bool			frozen_offsets;
27 } __randomize_layout;
28 
29 extern struct time_namespace init_time_ns;
30 
31 #ifdef CONFIG_TIME_NS
32 extern int vdso_join_timens(struct task_struct *task,
33 			    struct time_namespace *ns);
34 extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
35 
36 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
37 {
38 	refcount_inc(&ns->ns.count);
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);
45 void free_time_ns(struct time_namespace *ns);
46 int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
47 struct vdso_data *arch_get_vdso_data(void *vvar_page);
48 
49 static inline void put_time_ns(struct time_namespace *ns)
50 {
51 	if (refcount_dec_and_test(&ns->ns.count))
52 		free_time_ns(ns);
53 }
54 
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 
65 static inline void timens_add_monotonic(struct timespec64 *ts)
66 {
67 	struct timens_offsets *ns_offsets = &current->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 = &current->nsproxy->time_ns->offsets;
75 
76 	*ts = timespec64_add(*ts, ns_offsets->boottime);
77 }
78 
79 ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
80 				struct timens_offsets *offsets);
81 
82 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
83 {
84 	struct time_namespace *ns = current->nsproxy->time_ns;
85 
86 	if (likely(ns == &init_time_ns))
87 		return tim;
88 
89 	return do_timens_ktime_to_host(clockid, tim, &ns->offsets);
90 }
91 
92 #else
93 static inline int vdso_join_timens(struct task_struct *task,
94 				   struct time_namespace *ns)
95 {
96 	return 0;
97 }
98 
99 static inline void timens_commit(struct task_struct *tsk,
100 				 struct time_namespace *ns)
101 {
102 }
103 
104 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
105 {
106 	return NULL;
107 }
108 
109 static inline void put_time_ns(struct time_namespace *ns)
110 {
111 }
112 
113 static inline
114 struct time_namespace *copy_time_ns(unsigned long flags,
115 				    struct user_namespace *user_ns,
116 				    struct time_namespace *old_ns)
117 {
118 	if (flags & CLONE_NEWTIME)
119 		return ERR_PTR(-EINVAL);
120 
121 	return old_ns;
122 }
123 
124 static inline int timens_on_fork(struct nsproxy *nsproxy,
125 				 struct task_struct *tsk)
126 {
127 	return 0;
128 }
129 
130 static inline void timens_add_monotonic(struct timespec64 *ts) { }
131 static inline void timens_add_boottime(struct timespec64 *ts) { }
132 static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
133 {
134 	return tim;
135 }
136 #endif
137 
138 #endif /* _LINUX_TIMENS_H */
139