Accuracy bounds of ktime_get_boot_fast_ns

From: Jason A. Donenfeld
Date: Sat Jun 23 2018 - 13:43:42 EST


Hi,

In my driver, I am constantly concerned with determining for how long
a certain object has been alive, in real time, because the lifetime
needs to be more or less synchronized with others on a network. For
this, I'm generally using a formulation like:

void foobar_create(struct foobar *f)
{
f->birthdate = get_jiffies_64();
}
bool foobar_has_expired(struct foobar *f)
{
return time_is_before_eq_jiffies64(f->birthdate + DEATH_AGE_SEC * HZ);
}

That works well, except after system suspend, since now that
comparison doesn't actually represent anything real in relation to
others on the network. So the fix is:

void foobar_create(struct foobar *f)
{
f->birthdate = ktime_get_boottime();
}
bool foobar_has_expired(struct foobar *f)
{
return !ktime_after(f->birthdate + DEATH_AGE_SEC * NSEC_PER_SEC,
ktime_get_boottime());
}

So far, so good. But what if `foobar_has_expired` is called in a
performance critical hotpath? Since precision isn't _that_ important,
maybe I can get away with:

void foobar_create(struct foobar *f)
{
f->birthdate = ktime_get_boot_fast_ns();
}
bool foobar_has_expired(struct foobar *f)
{
return f->birthdate + DEATH_AGE_SEC * NSEC_PER_SEC <=
ktime_get_boot_fast_ns();
}

I'm wondering if I can actually get away with this last iteration.
I've read the comments around the various _fast_ns functions, and they
all indicate that it might not be totally monotonic with respect to
all cpus. But I wonder if that actually matters for my use case. For
example, is it still correct within a 10th or so of a second? Or will
it occasionally be wrong by massive multi-second leaps, which would
make it unsuitable for my usage? In other words, I'm wondering if
there's still a level of accuracy for a certain low degree of
precision?

Thanks,
Jason