Skip to content

Commit 769071a

Browse files
avaginKAGA-KOKO
authored andcommitted
ns: Introduce Time Namespace
Time Namespace isolates clock values. The kernel provides access to several clocks CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, etc. CLOCK_REALTIME System-wide clock that measures real (i.e., wall-clock) time. CLOCK_MONOTONIC Clock that cannot be set and represents monotonic time since some unspecified starting point. CLOCK_BOOTTIME Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended. For many users, the time namespace means the ability to changes date and time in a container (CLOCK_REALTIME). Providing per namespace notions of CLOCK_REALTIME would be complex with a massive overhead, but has a dubious value. But in the context of checkpoint/restore functionality, monotonic and boottime clocks become interesting. Both clocks are monotonic with unspecified starting points. These clocks are widely used to measure time slices and set timers. After restoring or migrating processes, it has to be guaranteed that they never go backward. In an ideal case, the behavior of these clocks should be the same as for a case when a whole system is suspended. All this means that it is required to set CLOCK_MONOTONIC and CLOCK_BOOTTIME clocks, which can be achieved by adding per-namespace offsets for clocks. A time namespace is similar to a pid namespace in the way how it is created: unshare(CLONE_NEWTIME) system call creates a new time namespace, but doesn't set it to the current process. Then all children of the process will be born in the new time namespace, or a process can use the setns() system call to join a namespace. This scheme allows setting clock offsets for a namespace, before any processes appear in it. All available clone flags have been used, so CLONE_NEWTIME uses the highest bit of CSIGNAL. It means that it can be used only with the unshare() and the clone3() system calls. [ tglx: Adjusted paragraph about clone3() to reality and massaged the changelog a bit. ] Co-developed-by: Dmitry Safonov <[email protected]> Signed-off-by: Andrei Vagin <[email protected]> Signed-off-by: Dmitry Safonov <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://criu.org/Time_namespace Link: https://lists.openvz.org/pipermail/criu/2018-June/041504.html Link: https://lore.kernel.org/r/[email protected]
1 parent c966533 commit 769071a

File tree

12 files changed

+361
-10
lines changed

12 files changed

+361
-10
lines changed

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13214,6 +13214,8 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/core
1321413214
S: Maintained
1321513215
F: fs/timerfd.c
1321613216
F: include/linux/timer*
13217+
F: include/linux/time_namespace.h
13218+
F: kernel/time_namespace.c
1321713219
F: kernel/time/*timer*
1321813220

1321913221
POWER MANAGEMENT CORE

fs/proc/namespaces.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ static const struct proc_ns_operations *ns_entries[] = {
3333
#ifdef CONFIG_CGROUPS
3434
&cgroupns_operations,
3535
#endif
36+
#ifdef CONFIG_TIME_NS
37+
&timens_operations,
38+
&timens_for_children_operations,
39+
#endif
3640
};
3741

3842
static const char *proc_ns_get_link(struct dentry *dentry,

include/linux/nsproxy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ struct nsproxy {
3535
struct mnt_namespace *mnt_ns;
3636
struct pid_namespace *pid_ns_for_children;
3737
struct net *net_ns;
38+
struct time_namespace *time_ns;
39+
struct time_namespace *time_ns_for_children;
3840
struct cgroup_namespace *cgroup_ns;
3941
};
4042
extern struct nsproxy init_nsproxy;

include/linux/proc_ns.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extern const struct proc_ns_operations pidns_for_children_operations;
3232
extern const struct proc_ns_operations userns_operations;
3333
extern const struct proc_ns_operations mntns_operations;
3434
extern const struct proc_ns_operations cgroupns_operations;
35+
extern const struct proc_ns_operations timens_operations;
36+
extern const struct proc_ns_operations timens_for_children_operations;
3537

3638
/*
3739
* We always define these enumerators
@@ -43,6 +45,7 @@ enum {
4345
PROC_USER_INIT_INO = 0xEFFFFFFDU,
4446
PROC_PID_INIT_INO = 0xEFFFFFFCU,
4547
PROC_CGROUP_INIT_INO = 0xEFFFFFFBU,
48+
PROC_TIME_INIT_INO = 0xEFFFFFFAU,
4649
};
4750

4851
#ifdef CONFIG_PROC_FS

include/linux/time_namespace.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 time_namespace {
16+
struct kref kref;
17+
struct user_namespace *user_ns;
18+
struct ucounts *ucounts;
19+
struct ns_common ns;
20+
} __randomize_layout;
21+
22+
extern struct time_namespace init_time_ns;
23+
24+
#ifdef CONFIG_TIME_NS
25+
static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
26+
{
27+
kref_get(&ns->kref);
28+
return ns;
29+
}
30+
31+
struct time_namespace *copy_time_ns(unsigned long flags,
32+
struct user_namespace *user_ns,
33+
struct time_namespace *old_ns);
34+
void free_time_ns(struct kref *kref);
35+
int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
36+
37+
static inline void put_time_ns(struct time_namespace *ns)
38+
{
39+
kref_put(&ns->kref, free_time_ns);
40+
}
41+
42+
#else
43+
static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
44+
{
45+
return NULL;
46+
}
47+
48+
static inline void put_time_ns(struct time_namespace *ns)
49+
{
50+
}
51+
52+
static inline
53+
struct time_namespace *copy_time_ns(unsigned long flags,
54+
struct user_namespace *user_ns,
55+
struct time_namespace *old_ns)
56+
{
57+
if (flags & CLONE_NEWTIME)
58+
return ERR_PTR(-EINVAL);
59+
60+
return old_ns;
61+
}
62+
63+
static inline int timens_on_fork(struct nsproxy *nsproxy,
64+
struct task_struct *tsk)
65+
{
66+
return 0;
67+
}
68+
69+
#endif
70+
71+
#endif /* _LINUX_TIMENS_H */

include/linux/user_namespace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum ucount_type {
4545
UCOUNT_NET_NAMESPACES,
4646
UCOUNT_MNT_NAMESPACES,
4747
UCOUNT_CGROUP_NAMESPACES,
48+
UCOUNT_TIME_NAMESPACES,
4849
#ifdef CONFIG_INOTIFY_USER
4950
UCOUNT_INOTIFY_INSTANCES,
5051
UCOUNT_INOTIFY_WATCHES,

include/uapi/linux/sched.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
/* Flags for the clone3() syscall. */
3737
#define CLONE_CLEAR_SIGHAND 0x100000000ULL /* Clear any signal handler and reset to SIG_DFL. */
3838

39+
/*
40+
* cloning flags intersect with CSIGNAL so can be used with unshare and clone3
41+
* syscalls only:
42+
*/
43+
#define CLONE_NEWTIME 0x00000080 /* New time namespace */
44+
3945
#ifndef __ASSEMBLY__
4046
/**
4147
* struct clone_args - arguments for the clone3 syscall

init/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,13 @@ config UTS_NS
10801080
In this namespace tasks see different info provided with the
10811081
uname() system call
10821082

1083+
config TIME_NS
1084+
bool "TIME namespace"
1085+
default y
1086+
help
1087+
In this namespace boottime and monotonic clocks can be set.
1088+
The time will keep going with the same pace.
1089+
10831090
config IPC_NS
10841091
bool "IPC namespace"
10851092
depends on (SYSVIPC || POSIX_MQUEUE)

kernel/fork.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,7 @@ static __latent_entropy struct task_struct *copy_process(
18321832
struct multiprocess_signals delayed;
18331833
struct file *pidfile = NULL;
18341834
u64 clone_flags = args->flags;
1835+
struct nsproxy *nsp = current->nsproxy;
18351836

18361837
/*
18371838
* Don't allow sharing the root directory with processes in a different
@@ -1874,8 +1875,16 @@ static __latent_entropy struct task_struct *copy_process(
18741875
*/
18751876
if (clone_flags & CLONE_THREAD) {
18761877
if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
1877-
(task_active_pid_ns(current) !=
1878-
current->nsproxy->pid_ns_for_children))
1878+
(task_active_pid_ns(current) != nsp->pid_ns_for_children))
1879+
return ERR_PTR(-EINVAL);
1880+
}
1881+
1882+
/*
1883+
* If the new process will be in a different time namespace
1884+
* do not allow it to share VM or a thread group with the forking task.
1885+
*/
1886+
if (clone_flags & (CLONE_THREAD | CLONE_VM)) {
1887+
if (nsp->time_ns != nsp->time_ns_for_children)
18791888
return ERR_PTR(-EINVAL);
18801889
}
18811890

@@ -2811,7 +2820,8 @@ static int check_unshare_flags(unsigned long unshare_flags)
28112820
if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
28122821
CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
28132822
CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
2814-
CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP))
2823+
CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP|
2824+
CLONE_NEWTIME))
28152825
return -EINVAL;
28162826
/*
28172827
* Not implemented, but pretend it works if there is nothing

kernel/nsproxy.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/pid_namespace.h>
1919
#include <net/net_namespace.h>
2020
#include <linux/ipc_namespace.h>
21+
#include <linux/time_namespace.h>
2122
#include <linux/proc_ns.h>
2223
#include <linux/file.h>
2324
#include <linux/syscalls.h>
@@ -40,6 +41,10 @@ struct nsproxy init_nsproxy = {
4041
#ifdef CONFIG_CGROUPS
4142
.cgroup_ns = &init_cgroup_ns,
4243
#endif
44+
#ifdef CONFIG_TIME_NS
45+
.time_ns = &init_time_ns,
46+
.time_ns_for_children = &init_time_ns,
47+
#endif
4348
};
4449

4550
static inline struct nsproxy *create_nsproxy(void)
@@ -106,8 +111,18 @@ static struct nsproxy *create_new_namespaces(unsigned long flags,
106111
goto out_net;
107112
}
108113

114+
new_nsp->time_ns_for_children = copy_time_ns(flags, user_ns,
115+
tsk->nsproxy->time_ns_for_children);
116+
if (IS_ERR(new_nsp->time_ns_for_children)) {
117+
err = PTR_ERR(new_nsp->time_ns_for_children);
118+
goto out_time;
119+
}
120+
new_nsp->time_ns = get_time_ns(tsk->nsproxy->time_ns);
121+
109122
return new_nsp;
110123

124+
out_time:
125+
put_net(new_nsp->net_ns);
111126
out_net:
112127
put_cgroup_ns(new_nsp->cgroup_ns);
113128
out_cgroup:
@@ -136,15 +151,16 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
136151
struct nsproxy *old_ns = tsk->nsproxy;
137152
struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
138153
struct nsproxy *new_ns;
154+
int ret;
139155

140156
if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
141157
CLONE_NEWPID | CLONE_NEWNET |
142-
CLONE_NEWCGROUP)))) {
143-
get_nsproxy(old_ns);
144-
return 0;
145-
}
146-
147-
if (!ns_capable(user_ns, CAP_SYS_ADMIN))
158+
CLONE_NEWCGROUP | CLONE_NEWTIME)))) {
159+
if (likely(old_ns->time_ns_for_children == old_ns->time_ns)) {
160+
get_nsproxy(old_ns);
161+
return 0;
162+
}
163+
} else if (!ns_capable(user_ns, CAP_SYS_ADMIN))
148164
return -EPERM;
149165

150166
/*
@@ -162,6 +178,12 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
162178
if (IS_ERR(new_ns))
163179
return PTR_ERR(new_ns);
164180

181+
ret = timens_on_fork(new_ns, tsk);
182+
if (ret) {
183+
free_nsproxy(new_ns);
184+
return ret;
185+
}
186+
165187
tsk->nsproxy = new_ns;
166188
return 0;
167189
}
@@ -176,6 +198,10 @@ void free_nsproxy(struct nsproxy *ns)
176198
put_ipc_ns(ns->ipc_ns);
177199
if (ns->pid_ns_for_children)
178200
put_pid_ns(ns->pid_ns_for_children);
201+
if (ns->time_ns)
202+
put_time_ns(ns->time_ns);
203+
if (ns->time_ns_for_children)
204+
put_time_ns(ns->time_ns_for_children);
179205
put_cgroup_ns(ns->cgroup_ns);
180206
put_net(ns->net_ns);
181207
kmem_cache_free(nsproxy_cachep, ns);
@@ -192,7 +218,8 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags,
192218
int err = 0;
193219

194220
if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
195-
CLONE_NEWNET | CLONE_NEWPID | CLONE_NEWCGROUP)))
221+
CLONE_NEWNET | CLONE_NEWPID | CLONE_NEWCGROUP |
222+
CLONE_NEWTIME)))
196223
return 0;
197224

198225
user_ns = new_cred ? new_cred->user_ns : current_user_ns();

kernel/time/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ obj-$(CONFIG_TICK_ONESHOT) += tick-oneshot.o tick-sched.o
1919
obj-$(CONFIG_HAVE_GENERIC_VDSO) += vsyscall.o
2020
obj-$(CONFIG_DEBUG_FS) += timekeeping_debug.o
2121
obj-$(CONFIG_TEST_UDELAY) += test_udelay.o
22+
obj-$(CONFIG_TIME_NS) += namespace.o

0 commit comments

Comments
 (0)