blob: 824d54e057eb6d4e94d19fde40046f5fd91317bd [file] [log] [blame]
Andrei Vagin769071a2019-11-12 01:26:52 +00001/* 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
12struct user_namespace;
13extern struct user_namespace init_user_ns;
14
Andrei Vaginaf993f52019-11-12 01:26:53 +000015struct timens_offsets {
16 struct timespec64 monotonic;
17 struct timespec64 boottime;
18};
19
Andrei Vagin769071a2019-11-12 01:26:52 +000020struct time_namespace {
21 struct kref kref;
22 struct user_namespace *user_ns;
23 struct ucounts *ucounts;
24 struct ns_common ns;
Andrei Vaginaf993f52019-11-12 01:26:53 +000025 struct timens_offsets offsets;
Dmitry Safonovafaa7b52019-11-12 01:27:12 +000026 struct page *vvar_page;
27 /* If set prevents changing offsets after any task joined namespace. */
28 bool frozen_offsets;
Andrei Vagin769071a2019-11-12 01:26:52 +000029} __randomize_layout;
30
31extern struct time_namespace init_time_ns;
32
33#ifdef CONFIG_TIME_NS
Dmitry Safonov70ddf652019-11-12 01:27:15 +000034extern int vdso_join_timens(struct task_struct *task,
35 struct time_namespace *ns);
36
Andrei Vagin769071a2019-11-12 01:26:52 +000037static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
38{
39 kref_get(&ns->kref);
40 return ns;
41}
42
43struct time_namespace *copy_time_ns(unsigned long flags,
44 struct user_namespace *user_ns,
45 struct time_namespace *old_ns);
46void free_time_ns(struct kref *kref);
47int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
Dmitry Safonov64b302a2019-11-12 01:27:10 +000048struct vdso_data *arch_get_vdso_data(void *vvar_page);
Andrei Vagin769071a2019-11-12 01:26:52 +000049
50static inline void put_time_ns(struct time_namespace *ns)
51{
52 kref_put(&ns->kref, free_time_ns);
53}
54
Andrei Vagin04a86822019-11-12 01:27:16 +000055void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
56
57struct proc_timens_offset {
58 int clockid;
59 struct timespec64 val;
60};
61
62int proc_timens_set_offset(struct file *file, struct task_struct *p,
63 struct proc_timens_offset *offsets, int n);
64
Andrei Vaginaf993f52019-11-12 01:26:53 +000065static 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
72static 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
Andrei Vagin89dd8ee2019-11-12 01:27:01 +000079ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
80 struct timens_offsets *offsets);
81
82static 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
Andrei Vagin769071a2019-11-12 01:26:52 +000092#else
Dmitry Safonov70ddf652019-11-12 01:27:15 +000093static inline int vdso_join_timens(struct task_struct *task,
94 struct time_namespace *ns)
95{
96 return 0;
97}
98
Andrei Vagin769071a2019-11-12 01:26:52 +000099static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
100{
101 return NULL;
102}
103
104static inline void put_time_ns(struct time_namespace *ns)
105{
106}
107
108static inline
109struct time_namespace *copy_time_ns(unsigned long flags,
110 struct user_namespace *user_ns,
111 struct time_namespace *old_ns)
112{
113 if (flags & CLONE_NEWTIME)
114 return ERR_PTR(-EINVAL);
115
116 return old_ns;
117}
118
119static inline int timens_on_fork(struct nsproxy *nsproxy,
120 struct task_struct *tsk)
121{
122 return 0;
123}
124
Andrei Vaginaf993f52019-11-12 01:26:53 +0000125static inline void timens_add_monotonic(struct timespec64 *ts) { }
126static inline void timens_add_boottime(struct timespec64 *ts) { }
Andrei Vagin89dd8ee2019-11-12 01:27:01 +0000127static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
128{
129 return tim;
130}
Andrei Vagin769071a2019-11-12 01:26:52 +0000131#endif
132
133#endif /* _LINUX_TIMENS_H */