Re: infinite loop in read_hpet from ktime_get_boot_fast_ns

From: Thomas Gleixner
Date: Thu Jun 13 2019 - 12:31:22 EST


On Thu, 13 Jun 2019, Jason A. Donenfeld wrote:
> Hey Arnd,
>
> On Thu, Jun 13, 2019 at 5:40 PM Arnd Bergmann <arnd@xxxxxxxx> wrote:
> > A seqlock is a very cheap synchronization primitive, I would actually
> > guess that this is faster than most implementations of sched_clock()
> > that access a hardware register for reading the time.
>
> It appears to me that ktime_get_coarse_boottime() has a granularity of
> a whole second, which is a lot worse than jiffies. Looking at the
> source, you assign base but don't then add ns like the other
> functions. At first I thought this was an intentional quirk to avoid
> hitting the slow hardware paths. But noticing this poor granularity
> now and observing that there's actually a blank line (\n\n) where the
> nanosecond addition normally would be, I wonder if something was lost
> in cut-and-paste?
>
> I'm still poking around trying to see what's up. As a quick test,
> running this on every packet during a high speed test shows the left
> incrementing many times per second, whereas the right increments once
> per second:
>
> static int x = 0;
> if (!(x++ % 30000))
> pr_err("%llu %llu\n", local_clock(), ktime_get_coarse_boottime());

That does not make sense. The coarse time getters use
tk->tkr_mono.base. base is updated every tick (or if the machine is
completely idle right when the first CPU wakes up again).

timekeeping_get_ns() which is added in ktime_get_boottime() is the time in
ns elapsed since base was updated last, which is less than a tick.

Thanks,

tglx