xref: /linux-6.15/include/linux/time_namespace.h (revision af993f58)
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 } __randomize_layout;
27 
28 extern struct time_namespace init_time_ns;
29 
30 #ifdef CONFIG_TIME_NS
31 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
32 {
33 	kref_get(&ns->kref);
34 	return ns;
35 }
36 
37 struct time_namespace *copy_time_ns(unsigned long flags,
38 				    struct user_namespace *user_ns,
39 				    struct time_namespace *old_ns);
40 void free_time_ns(struct kref *kref);
41 int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
42 
43 static inline void put_time_ns(struct time_namespace *ns)
44 {
45 	kref_put(&ns->kref, free_time_ns);
46 }
47 
48 static inline void timens_add_monotonic(struct timespec64 *ts)
49 {
50 	struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
51 
52 	*ts = timespec64_add(*ts, ns_offsets->monotonic);
53 }
54 
55 static inline void timens_add_boottime(struct timespec64 *ts)
56 {
57 	struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
58 
59 	*ts = timespec64_add(*ts, ns_offsets->boottime);
60 }
61 
62 #else
63 static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
64 {
65 	return NULL;
66 }
67 
68 static inline void put_time_ns(struct time_namespace *ns)
69 {
70 }
71 
72 static inline
73 struct time_namespace *copy_time_ns(unsigned long flags,
74 				    struct user_namespace *user_ns,
75 				    struct time_namespace *old_ns)
76 {
77 	if (flags & CLONE_NEWTIME)
78 		return ERR_PTR(-EINVAL);
79 
80 	return old_ns;
81 }
82 
83 static inline int timens_on_fork(struct nsproxy *nsproxy,
84 				 struct task_struct *tsk)
85 {
86 	return 0;
87 }
88 
89 static inline void timens_add_monotonic(struct timespec64 *ts) { }
90 static inline void timens_add_boottime(struct timespec64 *ts) { }
91 #endif
92 
93 #endif /* _LINUX_TIMENS_H */
94