Re: [PATCHv3 03/27] timens: Introduce CLOCK_MONOTONIC offsets

From: Thomas Gleixner
Date: Thu Apr 25 2019 - 15:53:07 EST


On Thu, 25 Apr 2019, Dmitry Safonov wrote:
> From: Andrei Vagin <avagin@xxxxxxxxxx>
>
> Add monotonic time virtualisation for time namespace.
> Introduce timespec for monotionic clock into timens offsets and wire
> clock_gettime() syscall.

That's a bit meager. It should at least explain the concept how this is
supposed to work.

> #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC)
> #define CLOCKS_MONO CLOCK_MONOTONIC

Unrelated to this, but those two defines can go away.

> +#define CLOCK_TIMENS 1024

Random number pulled out of thin air without any explanation. Aside of that
why is this exposed to user space?

> @@ -77,6 +78,7 @@ int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
> break;
> case CLOCK_MONOTONIC:
> ktime_get_ts64(tp);
> + timens_add_monotonic(tp);
> break;
> case CLOCK_BOOTTIME:
> ktime_get_boottime_ts64(tp);
> diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
> index 29176635991f..a047d6b7c768 100644
> --- a/kernel/time/posix-timers.c
> +++ b/kernel/time/posix-timers.c
> @@ -30,6 +30,7 @@
> #include <linux/hashtable.h>
> #include <linux/compat.h>
> #include <linux/nospec.h>
> +#include <linux/time_namespace.h>
>
> #include "timekeeping.h"
> #include "posix-timers.h"
> @@ -190,6 +191,8 @@ static int posix_clock_realtime_adj(const clockid_t which_clock,
> static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
> {
> ktime_get_ts64(tp);
> + if (which_clock & CLOCK_TIMENS)
> + timens_add_monotonic(tp);

> @@ -1039,7 +1046,7 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
> if (!kc)
> return -EINVAL;
>
> - error = kc->clock_get(which_clock, &kernel_tp);
> + error = kc->clock_get(which_clock | CLOCK_TIMENS, &kernel_tp);

Bah. After some banging the head against the wall I figured out what you
are doing here. It's because kc->clock_get() is used in common_timer_get()
as well and there you don't want the adjustment there. Sorry, but this is
uncomprehensible and unmaintainable and adds yet another conditional into
the code flow.

The callsite in common_timer_get() has already a comment:
/*
* The timespec64 based conversion is suboptimal, but it's not
* worth to implement yet another callback.
*/
kc->clock_get(timr->it_clock, &ts64);
now = timespec64_to_ktime(ts64);

So now we have a proper justification to add that extra callback:
separation of functionality. It's trivial and not a massive code size
overhead.

Thanks,

tglx