Skip to content

Commit 89dd8ee

Browse files
avaginKAGA-KOKO
authored andcommitted
time: Add do_timens_ktime_to_host() helper
The helper subtracts namespace's clock offset from the given time and ensures that the result is within [0, KTIME_MAX]. Co-developed-by: Dmitry Safonov <dima@arista.com> Signed-off-by: Andrei Vagin <avagin@gmail.com> Signed-off-by: Dmitry Safonov <dima@arista.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Link: https://lore.kernel.org/r/20191112012724.250792-13-dima@arista.com
1 parent 5a590f3 commit 89dd8ee

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎include/linux/time_namespace.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ static inline void timens_add_boottime(struct timespec64 *ts)
5959
*ts = timespec64_add(*ts, ns_offsets->boottime);
6060
}
6161

62+
ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
63+
struct timens_offsets *offsets);
64+
65+
static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
66+
{
67+
struct time_namespace *ns = current->nsproxy->time_ns;
68+
69+
if (likely(ns == &init_time_ns))
70+
return tim;
71+
72+
return do_timens_ktime_to_host(clockid, tim, &ns->offsets);
73+
}
74+
6275
#else
6376
static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
6477
{
@@ -88,6 +101,10 @@ static inline int timens_on_fork(struct nsproxy *nsproxy,
88101

89102
static inline void timens_add_monotonic(struct timespec64 *ts) { }
90103
static inline void timens_add_boottime(struct timespec64 *ts) { }
104+
static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
105+
{
106+
return tim;
107+
}
91108
#endif
92109

93110
#endif /* _LINUX_TIMENS_H */

‎kernel/time/namespace.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,42 @@
1616
#include <linux/err.h>
1717
#include <linux/mm.h>
1818

19+
ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
20+
struct timens_offsets *ns_offsets)
21+
{
22+
ktime_t offset;
23+
24+
switch (clockid) {
25+
case CLOCK_MONOTONIC:
26+
offset = timespec64_to_ktime(ns_offsets->monotonic);
27+
break;
28+
case CLOCK_BOOTTIME:
29+
case CLOCK_BOOTTIME_ALARM:
30+
offset = timespec64_to_ktime(ns_offsets->boottime);
31+
break;
32+
default:
33+
return tim;
34+
}
35+
36+
/*
37+
* Check that @tim value is in [offset, KTIME_MAX + offset]
38+
* and subtract offset.
39+
*/
40+
if (tim < offset) {
41+
/*
42+
* User can specify @tim *absolute* value - if it's lesser than
43+
* the time namespace's offset - it's already expired.
44+
*/
45+
tim = 0;
46+
} else {
47+
tim = ktime_sub(tim, offset);
48+
if (unlikely(tim > KTIME_MAX))
49+
tim = KTIME_MAX;
50+
}
51+
52+
return tim;
53+
}
54+
1955
static struct ucounts *inc_time_namespaces(struct user_namespace *ns)
2056
{
2157
return inc_ucount(ns, current_euid(), UCOUNT_TIME_NAMESPACES);

0 commit comments

Comments
 (0)