[RFC 04/20] timens: Introduce CLOCK_BOOTTIME offset

From: Dmitry Safonov
Date: Wed Sep 19 2018 - 16:52:26 EST


From: Andrei Vagin <avagin@xxxxxxxxxx>

Adds boottime virtualisation for time namespace.
Provide clock_set() API to set boottime clock inside ns.

Signed-off-by: Andrei Vagin <avagin@xxxxxxxxxx>
Signed-off-by: Dmitry Safonov <dima@xxxxxxxxxx>
---
include/linux/timens_offsets.h | 1 +
kernel/time/posix-timers.c | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+)

diff --git a/include/linux/timens_offsets.h b/include/linux/timens_offsets.h
index 248b0c0bb92a..777530c46852 100644
--- a/include/linux/timens_offsets.h
+++ b/include/linux/timens_offsets.h
@@ -4,6 +4,7 @@

struct timens_offsets {
struct timespec64 monotonic_time_offset;
+ struct timespec64 monotonic_boottime_offset;
};

#endif
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index 3c1f98760dec..d38835a21c5d 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -276,7 +276,32 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *

static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
{
+ struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets;
+
ktime_get_boottime_ts64(tp);
+
+ if (!ns_offsets)
+ return 0;
+
+ *tp = timespec64_add(*tp, ns_offsets->monotonic_boottime_offset);
+
+ return 0;
+}
+
+static int posix_set_boottime(clockid_t which_clock, const struct timespec64 *tp)
+{
+ struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets;
+ struct timespec64 ktp;
+
+ if (!ns_capable(current->nsproxy->time_ns->user_ns, CAP_SYS_TIME))
+ return -EPERM;
+
+ ktime_get_boottime_ts64(&ktp);
+
+ if (ns_offsets)
+ ns_offsets->monotonic_boottime_offset = timespec64_sub(*tp, ktp);
+ else
+ return -EINVAL;
return 0;
}

@@ -1362,6 +1387,7 @@ static const struct k_clock clock_tai = {
static const struct k_clock clock_boottime = {
.clock_getres = posix_get_hrtimer_res,
.clock_get = posix_get_boottime,
+ .clock_set = posix_set_boottime,
.nsleep = common_nsleep,
.timer_create = common_timer_create,
.timer_set = common_timer_set,
--
2.13.6