Skip to content

Commit 48cdc13

Browse files
KAGA-KOKOjohnstultz-work
authored andcommitted
timekeeping: Implement a shadow timekeeper
Use the shadow timekeeper to do the update_wall_time() adjustments and then copy it over to the real timekeeper. Keep the shadow timekeeper in sync when updating stuff outside of update_wall_time(). This allows us to limit the timekeeper_seq hold time to the update of the real timekeeper and the vsyscall data in the next patch. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: John Stultz <john.stultz@linaro.org>
1 parent 7ec98e1 commit 48cdc13

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

‎kernel/time/timekeeping.c‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
static struct timekeeper timekeeper;
3030
static DEFINE_RAW_SPINLOCK(timekeeper_lock);
3131
static seqcount_t timekeeper_seq;
32+
static struct timekeeper shadow_timekeeper;
3233

3334
/* flag for if timekeeping is suspended */
3435
int __read_mostly timekeeping_suspended;
@@ -240,14 +241,17 @@ int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
240241
EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
241242

242243
/* must hold timekeeper_lock */
243-
static void timekeeping_update(struct timekeeper *tk, bool clearntp)
244+
static void timekeeping_update(struct timekeeper *tk, bool clearntp, bool mirror)
244245
{
245246
if (clearntp) {
246247
tk->ntp_error = 0;
247248
ntp_clear();
248249
}
249250
update_vsyscall(tk);
250251
update_pvclock_gtod(tk);
252+
253+
if (mirror)
254+
memcpy(&shadow_timekeeper, &timekeeper, sizeof(timekeeper));
251255
}
252256

253257
/**
@@ -504,7 +508,7 @@ int do_settimeofday(const struct timespec *tv)
504508

505509
tk_set_xtime(tk, tv);
506510

507-
timekeeping_update(tk, true);
511+
timekeeping_update(tk, true, true);
508512

509513
write_seqcount_end(&timekeeper_seq);
510514
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
@@ -548,7 +552,7 @@ int timekeeping_inject_offset(struct timespec *ts)
548552
tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts));
549553

550554
error: /* even if we error out, we forwarded the time, so call update */
551-
timekeeping_update(tk, true);
555+
timekeeping_update(tk, true, true);
552556

553557
write_seqcount_end(&timekeeper_seq);
554558
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
@@ -628,7 +632,7 @@ static int change_clocksource(void *data)
628632
if (old->disable)
629633
old->disable(old);
630634
}
631-
timekeeping_update(tk, true);
635+
timekeeping_update(tk, true, true);
632636

633637
write_seqcount_end(&timekeeper_seq);
634638
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
@@ -809,6 +813,8 @@ void __init timekeeping_init(void)
809813
tmp.tv_nsec = 0;
810814
tk_set_sleep_time(tk, tmp);
811815

816+
memcpy(&shadow_timekeeper, &timekeeper, sizeof(timekeeper));
817+
812818
write_seqcount_end(&timekeeper_seq);
813819
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
814820
}
@@ -865,7 +871,7 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
865871

866872
__timekeeping_inject_sleeptime(tk, delta);
867873

868-
timekeeping_update(tk, true);
874+
timekeeping_update(tk, true, true);
869875

870876
write_seqcount_end(&timekeeper_seq);
871877
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
@@ -947,7 +953,7 @@ static void timekeeping_resume(void)
947953
clock->cycle_last = cycle_now;
948954
tk->ntp_error = 0;
949955
timekeeping_suspended = 0;
950-
timekeeping_update(tk, false);
956+
timekeeping_update(tk, false, true);
951957
write_seqcount_end(&timekeeper_seq);
952958
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
953959

@@ -1328,7 +1334,8 @@ static inline void old_vsyscall_fixup(struct timekeeper *tk)
13281334
static void update_wall_time(void)
13291335
{
13301336
struct clocksource *clock;
1331-
struct timekeeper *tk = &timekeeper;
1337+
struct timekeeper *real_tk = &timekeeper;
1338+
struct timekeeper *tk = &shadow_timekeeper;
13321339
cycle_t offset;
13331340
int shift = 0, maxshift;
13341341
unsigned long flags;
@@ -1340,16 +1347,16 @@ static void update_wall_time(void)
13401347
if (unlikely(timekeeping_suspended))
13411348
goto out;
13421349

1343-
clock = tk->clock;
1350+
clock = real_tk->clock;
13441351

13451352
#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
1346-
offset = tk->cycle_interval;
1353+
offset = real_tk->cycle_interval;
13471354
#else
13481355
offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
13491356
#endif
13501357

13511358
/* Check if there's really nothing to do */
1352-
if (offset < tk->cycle_interval)
1359+
if (offset < real_tk->cycle_interval)
13531360
goto out;
13541361

13551362
/*
@@ -1388,12 +1395,22 @@ static void update_wall_time(void)
13881395

13891396
/* Update clock->cycle_last with the new value */
13901397
clock->cycle_last = tk->cycle_last;
1391-
timekeeping_update(tk, false);
1398+
/*
1399+
* Update the real timekeeper.
1400+
*
1401+
* We could avoid this memcpy by switching pointers, but that
1402+
* requires changes to all other timekeeper usage sites as
1403+
* well, i.e. move the timekeeper pointer getter into the
1404+
* spinlocked/seqcount protected sections. And we trade this
1405+
* memcpy under the timekeeper_seq against one before we start
1406+
* updating.
1407+
*/
1408+
memcpy(real_tk, tk, sizeof(*tk));
1409+
timekeeping_update(real_tk, false, false);
13921410

13931411
out:
13941412
write_seqcount_end(&timekeeper_seq);
13951413
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1396-
13971414
}
13981415

13991416
/**

0 commit comments

Comments
 (0)