Skip to content

Commit af993f5

Browse files
avaginKAGA-KOKO
authored andcommitted
time: Add timens_offsets to be used for tasks in time namespace
Introduce offsets for time namespace. They will contain an adjustment needed to convert clocks to/from host's. A new namespace is created with the same offsets as the time namespace of the current process. 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://lore.kernel.org/r/[email protected]
1 parent 769071a commit af993f5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

include/linux/time_namespace.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
struct user_namespace;
1313
extern struct user_namespace init_user_ns;
1414

15+
struct timens_offsets {
16+
struct timespec64 monotonic;
17+
struct timespec64 boottime;
18+
};
19+
1520
struct time_namespace {
1621
struct kref kref;
1722
struct user_namespace *user_ns;
1823
struct ucounts *ucounts;
1924
struct ns_common ns;
25+
struct timens_offsets offsets;
2026
} __randomize_layout;
2127

2228
extern struct time_namespace init_time_ns;
@@ -39,6 +45,20 @@ static inline void put_time_ns(struct time_namespace *ns)
3945
kref_put(&ns->kref, free_time_ns);
4046
}
4147

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+
4262
#else
4363
static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
4464
{
@@ -66,6 +86,8 @@ static inline int timens_on_fork(struct nsproxy *nsproxy,
6686
return 0;
6787
}
6888

89+
static inline void timens_add_monotonic(struct timespec64 *ts) { }
90+
static inline void timens_add_boottime(struct timespec64 *ts) { }
6991
#endif
7092

7193
#endif /* _LINUX_TIMENS_H */

kernel/time/namespace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/slab.h>
1515
#include <linux/cred.h>
1616
#include <linux/err.h>
17+
#include <linux/mm.h>
1718

1819
static struct ucounts *inc_time_namespaces(struct user_namespace *ns)
1920
{
@@ -60,6 +61,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
6061
ns->ucounts = ucounts;
6162
ns->ns.ops = &timens_operations;
6263
ns->user_ns = get_user_ns(user_ns);
64+
ns->offsets = old_ns->offsets;
6365
return ns;
6466

6567
fail_free:

0 commit comments

Comments
 (0)