Skip to content

Commit e8db3a5

Browse files
committed
timekeeping: Make timekeeping_inject_offset() reusable
Split out the inner workings for auxiliary clock support and feed the core time keeper into it. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: John Stultz <jstultz@google.com> Link: https://lore.kernel.org/all/20250625183758.059934561@linutronix.de
1 parent 60ecc26 commit e8db3a5

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

kernel/time/timekeeping.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,32 +1433,32 @@ EXPORT_SYMBOL(do_settimeofday64);
14331433

14341434
/**
14351435
* __timekeeping_inject_offset - Adds or subtracts from the current time.
1436+
* @tkd: Pointer to the timekeeper to modify
14361437
* @ts: Pointer to the timespec variable containing the offset
14371438
*
14381439
* Adds or subtracts an offset value from the current time.
14391440
*/
1440-
static int __timekeeping_inject_offset(const struct timespec64 *ts)
1441+
static int __timekeeping_inject_offset(struct tk_data *tkd, const struct timespec64 *ts)
14411442
{
1442-
struct timekeeper *tks = &tk_core.shadow_timekeeper;
1443+
struct timekeeper *tks = &tkd->shadow_timekeeper;
14431444
struct timespec64 tmp;
14441445

14451446
if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC)
14461447
return -EINVAL;
14471448

1448-
14491449
timekeeping_forward_now(tks);
14501450

14511451
/* Make sure the proposed value is valid */
14521452
tmp = timespec64_add(tk_xtime(tks), *ts);
14531453
if (timespec64_compare(&tks->wall_to_monotonic, ts) > 0 ||
14541454
!timespec64_valid_settod(&tmp)) {
1455-
timekeeping_restore_shadow(&tk_core);
1455+
timekeeping_restore_shadow(tkd);
14561456
return -EINVAL;
14571457
}
14581458

14591459
tk_xtime_add(tks, ts);
14601460
tk_set_wall_to_mono(tks, timespec64_sub(tks->wall_to_monotonic, *ts));
1461-
timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
1461+
timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL);
14621462
return 0;
14631463
}
14641464

@@ -1467,7 +1467,7 @@ static int timekeeping_inject_offset(const struct timespec64 *ts)
14671467
int ret;
14681468

14691469
scoped_guard (raw_spinlock_irqsave, &tk_core.lock)
1470-
ret = __timekeeping_inject_offset(ts);
1470+
ret = __timekeeping_inject_offset(&tk_core, ts);
14711471

14721472
/* Signal hrtimers about time change */
14731473
if (!ret)
@@ -2568,6 +2568,7 @@ EXPORT_SYMBOL_GPL(random_get_entropy_fallback);
25682568
*/
25692569
int do_adjtimex(struct __kernel_timex *txc)
25702570
{
2571+
struct tk_data *tkd = &tk_core;
25712572
struct timespec64 delta, ts;
25722573
struct audit_ntp_data ad;
25732574
bool offset_set = false;
@@ -2585,16 +2586,19 @@ int do_adjtimex(struct __kernel_timex *txc)
25852586
ktime_get_real_ts64(&ts);
25862587
add_device_randomness(&ts, sizeof(ts));
25872588

2588-
scoped_guard (raw_spinlock_irqsave, &tk_core.lock) {
2589-
struct timekeeper *tks = &tk_core.shadow_timekeeper;
2589+
scoped_guard (raw_spinlock_irqsave, &tkd->lock) {
2590+
struct timekeeper *tks = &tkd->shadow_timekeeper;
25902591
s32 orig_tai, tai;
25912592

2593+
if (!tks->clock_valid)
2594+
return -ENODEV;
2595+
25922596
if (txc->modes & ADJ_SETOFFSET) {
25932597
delta.tv_sec = txc->time.tv_sec;
25942598
delta.tv_nsec = txc->time.tv_usec;
25952599
if (!(txc->modes & ADJ_NANO))
25962600
delta.tv_nsec *= 1000;
2597-
ret = __timekeeping_inject_offset(&delta);
2601+
ret = __timekeeping_inject_offset(tkd, &delta);
25982602
if (ret)
25992603
return ret;
26002604

@@ -2607,15 +2611,15 @@ int do_adjtimex(struct __kernel_timex *txc)
26072611

26082612
if (tai != orig_tai) {
26092613
__timekeeping_set_tai_offset(tks, tai);
2610-
timekeeping_update_from_shadow(&tk_core, TK_CLOCK_WAS_SET);
2614+
timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET);
26112615
clock_set = true;
26122616
} else {
26132617
tk_update_leap_state_all(&tk_core);
26142618
}
26152619

26162620
/* Update the multiplier immediately if frequency was set directly */
26172621
if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
2618-
clock_set |= __timekeeping_advance(&tk_core, TK_ADV_FREQ);
2622+
clock_set |= __timekeeping_advance(tkd, TK_ADV_FREQ);
26192623
}
26202624

26212625
if (txc->modes & ADJ_SETOFFSET)

0 commit comments

Comments
 (0)