Re: [PATCH] timekeeping: move multigrain ctime floor handling into timekeeper
From: Jeff Layton
Date: Thu Sep 12 2024 - 10:37:33 EST
On Thu, 2024-09-12 at 09:26 -0400, Jeff Layton wrote:
> On Thu, 2024-09-12 at 13:17 +0000, Arnd Bergmann wrote:
> > On Thu, Sep 12, 2024, at 11:34, Jeff Layton wrote:
> > > On Thu, 2024-09-12 at 10:01 +0000, Arnd Bergmann wrote:
> > > > On Wed, Sep 11, 2024, at 20:43, Jeff Layton wrote:
> > > >
> > > > That way you avoid the atomic64_try_cmpxchg()
> > > > inode_set_ctime_current(), making that case faster,
> > > > and avoid all overhead in coarse_ctime() unless you
> > > > use both types during the same tick.
> > > >
> > >
> > > With the current code we only get a fine grained timestamp iff:
> > >
> > > 1/ the timestamps have been queried (a'la I_CTIME_QUERIED)
> > > 2/ the current coarse-grained or floor time would not show a change in
> > > the ctime
> > >
> > > If we do what you're suggesting above, as soon as one task sets the
> > > flag, anyone calling current_time() will end up getting a brand new
> > > fine-grained timestamp, even when the current floor time would have
> > > been fine.
> >
> > Right, I forgot about this part of your work, the
> > I_CTIME_QUERIED logic definitely has to stay.
> >
> > > That means a lot more calls into ktime_get_real_ts64(), at least until
> > > the timer ticks, and would probably mean a lot of extra journal
> > > transactions, since those timestamps would all be distinct from one
> > > another and would need to go to disk more often.
> >
> > I guess some of that overhead would go away if we just treated
> > tk_xtime() as the floor value without an additional cache,
> > and did the comparison against inode->i_ctime inside of
> > a new ktime_get_real_ts64_newer_than(), but there is still the
> > case of a single inode getting updated a lot, and it would
> > break the ordering of updates between inodes.
> >
>
> Yes, and the breaking of ordering is why we had to revert the last set,
> so that's definitely no good.
>
> I think your suggestion about using a tuple of the sequence and the
> delta should work. The trick is that we need to do the fetch and the
> cmpxchg of the floor tuple inside the read_seqcount loop. Zeroing it
> out can be done with write_once(). If we get a spurious update to the
> floor while zeroing then it's no big deal since everything would just
> loop and do it again.
>
> I'll plan to hack something together later today and see how it does.
>
Ok, already hit a couple of problems:
First, moving the floor word into struct timekeeper is probably not a
good idea. This is going to be updated more often than the rest of the
timekeeper, and so its cacheline will be invalidated more. I think we
need to keep the floor word on its own cacheline. It can be a static
u64 though inside timekeeper.c.
Second, the existing code basically does:
get the floor time (tfc = max of coarse and ctime_floor)
if ctime was queried and applying tfc would show no change:
get fine time
cmpxchg into place and accept the result
The key there is that if the floor time changes at any point between
when we first fetch it and the attempted cmpxchg, no update happens
(which is good).
If we move all of the floor handling into the timekeeper, then I think
we still need to have some state that we pass back when trying to get
the floor time initially, so that we keep the race window large (which
seems weird, but is good in this case).
So, I think that we actually need an API like this:
/* returns opaque cookie value */
u64 ktime_get_coarse_real_ts64_mg(struct timespec64 *ts);
/* accepts opaque cookie value from above function */
void ktime_get_real_ts64_mg(struct timespec64 *ts, u64 cookie);
The first function fills in @ts with the max of coarse time and floor,
and returns an opaque cookie (a copy of the floor word). The second
fetches a fine-grained timestamp and uses the floor cookie as the "old"
value when doing the cmpxchg, and then fills in @ts with the result.
Does that sound reasonable? If so, then the next question is around
what the floor word should hold:
IMO, just keeping it as a monotonic time value seems simplest. I'm
struggling to understand where the "delta" portion would come from in
your earlier proposal, and the fact that that value could overflow
seems less than ideal.
Cheers,
--
Jeff Layton <jlayton@xxxxxxxxxx>