Merge tag 'fbdev-for-6.4-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[linux-block.git] / include / linux / time_namespace.h
CommitLineData
769071ac
AV
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_TIMENS_H
3#define _LINUX_TIMENS_H
4
5
6#include <linux/sched.h>
769071ac
AV
7#include <linux/nsproxy.h>
8#include <linux/ns_common.h>
9#include <linux/err.h>
10
11struct user_namespace;
12extern struct user_namespace init_user_ns;
13
af993f58
AV
14struct timens_offsets {
15 struct timespec64 monotonic;
16 struct timespec64 boottime;
17};
18
769071ac 19struct time_namespace {
769071ac
AV
20 struct user_namespace *user_ns;
21 struct ucounts *ucounts;
22 struct ns_common ns;
af993f58 23 struct timens_offsets offsets;
afaa7b5a
DS
24 struct page *vvar_page;
25 /* If set prevents changing offsets after any task joined namespace. */
26 bool frozen_offsets;
769071ac
AV
27} __randomize_layout;
28
29extern struct time_namespace init_time_ns;
30
31#ifdef CONFIG_TIME_NS
70ddf651
DS
32extern int vdso_join_timens(struct task_struct *task,
33 struct time_namespace *ns);
76c12881 34extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
70ddf651 35
769071ac
AV
36static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
37{
28c41efd 38 refcount_inc(&ns->ns.count);
769071ac
AV
39 return ns;
40}
41
42struct time_namespace *copy_time_ns(unsigned long flags,
43 struct user_namespace *user_ns,
44 struct time_namespace *old_ns);
28c41efd 45void free_time_ns(struct time_namespace *ns);
5c62634f 46void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
64b302ab 47struct vdso_data *arch_get_vdso_data(void *vvar_page);
d6c494e8 48struct page *find_timens_vvar_page(struct vm_area_struct *vma);
769071ac
AV
49
50static inline void put_time_ns(struct time_namespace *ns)
51{
28c41efd
KT
52 if (refcount_dec_and_test(&ns->ns.count))
53 free_time_ns(ns);
769071ac
AV
54}
55
04a8682a
AV
56void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
57
58struct proc_timens_offset {
59 int clockid;
60 struct timespec64 val;
61};
62
63int proc_timens_set_offset(struct file *file, struct task_struct *p,
64 struct proc_timens_offset *offsets, int n);
65
af993f58
AV
66static inline void timens_add_monotonic(struct timespec64 *ts)
67{
68 struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
69
70 *ts = timespec64_add(*ts, ns_offsets->monotonic);
71}
72
73static inline void timens_add_boottime(struct timespec64 *ts)
74{
75 struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
76
77 *ts = timespec64_add(*ts, ns_offsets->boottime);
78}
79
31909e33
MW
80static inline u64 timens_add_boottime_ns(u64 nsec)
81{
82 struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
83
84 return nsec + timespec64_to_ns(&ns_offsets->boottime);
85}
86
87static inline void timens_sub_boottime(struct timespec64 *ts)
88{
89 struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
90
91 *ts = timespec64_sub(*ts, ns_offsets->boottime);
92}
93
89dd8eec
AV
94ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
95 struct timens_offsets *offsets);
96
97static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
98{
99 struct time_namespace *ns = current->nsproxy->time_ns;
100
101 if (likely(ns == &init_time_ns))
102 return tim;
103
104 return do_timens_ktime_to_host(clockid, tim, &ns->offsets);
105}
106
769071ac 107#else
70ddf651
DS
108static inline int vdso_join_timens(struct task_struct *task,
109 struct time_namespace *ns)
110{
111 return 0;
112}
113
76c12881
CB
114static inline void timens_commit(struct task_struct *tsk,
115 struct time_namespace *ns)
116{
117}
118
769071ac
AV
119static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
120{
121 return NULL;
122}
123
124static inline void put_time_ns(struct time_namespace *ns)
125{
126}
127
128static inline
129struct time_namespace *copy_time_ns(unsigned long flags,
130 struct user_namespace *user_ns,
131 struct time_namespace *old_ns)
132{
133 if (flags & CLONE_NEWTIME)
134 return ERR_PTR(-EINVAL);
135
136 return old_ns;
137}
138
5c62634f 139static inline void timens_on_fork(struct nsproxy *nsproxy,
769071ac
AV
140 struct task_struct *tsk)
141{
5c62634f 142 return;
769071ac
AV
143}
144
d6c494e8
JH
145static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
146{
147 return NULL;
148}
149
af993f58
AV
150static inline void timens_add_monotonic(struct timespec64 *ts) { }
151static inline void timens_add_boottime(struct timespec64 *ts) { }
31909e33
MW
152
153static inline u64 timens_add_boottime_ns(u64 nsec)
154{
155 return nsec;
156}
157
158static inline void timens_sub_boottime(struct timespec64 *ts) { }
159
89dd8eec
AV
160static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
161{
162 return tim;
163}
769071ac
AV
164#endif
165
166#endif /* _LINUX_TIMENS_H */