Skip to content

Commit 775f71e

Browse files
committed
timekeeping: Make do_adjtimex() reusable
Split out the actual functionality of adjtimex() and make do_adjtimex() a wrapper which feeds the core timekeeper into it and handles the result including audit at the call site. This allows to reuse the actual functionality for auxiliary clocks. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: John Stultz <jstultz@google.com> Link: https://lore.kernel.org/all/20250625183758.187322876@linutronix.de
1 parent 2c8aea5 commit 775f71e

1 file changed

Lines changed: 56 additions & 46 deletions

File tree

kernel/time/timekeeping.c

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,17 +2585,18 @@ unsigned long random_get_entropy_fallback(void)
25852585
}
25862586
EXPORT_SYMBOL_GPL(random_get_entropy_fallback);
25872587

2588-
/**
2589-
* do_adjtimex() - Accessor function to NTP __do_adjtimex function
2590-
* @txc: Pointer to kernel_timex structure containing NTP parameters
2591-
*/
2592-
int do_adjtimex(struct __kernel_timex *txc)
2588+
struct adjtimex_result {
2589+
struct audit_ntp_data ad;
2590+
struct timespec64 delta;
2591+
bool clock_set;
2592+
};
2593+
2594+
static int __do_adjtimex(struct tk_data *tkd, struct __kernel_timex *txc,
2595+
struct adjtimex_result *result)
25932596
{
2594-
struct tk_data *tkd = &tk_core;
2595-
struct timespec64 delta, ts;
2596-
struct audit_ntp_data ad;
2597-
bool offset_set = false;
2598-
bool clock_set = false;
2597+
struct timekeeper *tks = &tkd->shadow_timekeeper;
2598+
struct timespec64 ts;
2599+
s32 orig_tai, tai;
25992600
int ret;
26002601

26012602
/* Validate the data before disabling interrupts */
@@ -2604,56 +2605,65 @@ int do_adjtimex(struct __kernel_timex *txc)
26042605
return ret;
26052606
add_device_randomness(txc, sizeof(*txc));
26062607

2607-
audit_ntp_init(&ad);
2608-
26092608
ktime_get_real_ts64(&ts);
26102609
add_device_randomness(&ts, sizeof(ts));
26112610

2612-
scoped_guard (raw_spinlock_irqsave, &tkd->lock) {
2613-
struct timekeeper *tks = &tkd->shadow_timekeeper;
2614-
s32 orig_tai, tai;
2611+
guard(raw_spinlock_irqsave)(&tkd->lock);
26152612

2616-
if (!tks->clock_valid)
2617-
return -ENODEV;
2618-
2619-
if (txc->modes & ADJ_SETOFFSET) {
2620-
delta.tv_sec = txc->time.tv_sec;
2621-
delta.tv_nsec = txc->time.tv_usec;
2622-
if (!(txc->modes & ADJ_NANO))
2623-
delta.tv_nsec *= 1000;
2624-
ret = __timekeeping_inject_offset(tkd, &delta);
2625-
if (ret)
2626-
return ret;
2627-
2628-
offset_set = delta.tv_sec != 0;
2629-
clock_set = true;
2630-
}
2613+
if (!tks->clock_valid)
2614+
return -ENODEV;
26312615

2632-
orig_tai = tai = tks->tai_offset;
2633-
ret = ntp_adjtimex(tks->id, txc, &ts, &tai, &ad);
2616+
if (txc->modes & ADJ_SETOFFSET) {
2617+
result->delta.tv_sec = txc->time.tv_sec;
2618+
result->delta.tv_nsec = txc->time.tv_usec;
2619+
if (!(txc->modes & ADJ_NANO))
2620+
result->delta.tv_nsec *= 1000;
2621+
ret = __timekeeping_inject_offset(tkd, &result->delta);
2622+
if (ret)
2623+
return ret;
2624+
result->clock_set = true;
2625+
}
26342626

2635-
if (tai != orig_tai) {
2636-
__timekeeping_set_tai_offset(tks, tai);
2637-
timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET);
2638-
clock_set = true;
2639-
} else {
2640-
tk_update_leap_state_all(&tk_core);
2641-
}
2627+
orig_tai = tai = tks->tai_offset;
2628+
ret = ntp_adjtimex(tks->id, txc, &ts, &tai, &result->ad);
26422629

2643-
/* Update the multiplier immediately if frequency was set directly */
2644-
if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
2645-
clock_set |= __timekeeping_advance(tkd, TK_ADV_FREQ);
2630+
if (tai != orig_tai) {
2631+
__timekeeping_set_tai_offset(tks, tai);
2632+
timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET);
2633+
result->clock_set = true;
2634+
} else {
2635+
tk_update_leap_state_all(&tk_core);
26462636
}
26472637

2638+
/* Update the multiplier immediately if frequency was set directly */
2639+
if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
2640+
result->clock_set |= __timekeeping_advance(tkd, TK_ADV_FREQ);
2641+
2642+
return ret;
2643+
}
2644+
2645+
/**
2646+
* do_adjtimex() - Accessor function to NTP __do_adjtimex function
2647+
* @txc: Pointer to kernel_timex structure containing NTP parameters
2648+
*/
2649+
int do_adjtimex(struct __kernel_timex *txc)
2650+
{
2651+
struct adjtimex_result result = { };
2652+
int ret;
2653+
2654+
ret = __do_adjtimex(&tk_core, txc, &result);
2655+
if (ret < 0)
2656+
return ret;
2657+
26482658
if (txc->modes & ADJ_SETOFFSET)
2649-
audit_tk_injoffset(delta);
2659+
audit_tk_injoffset(result.delta);
26502660

2651-
audit_ntp_log(&ad);
2661+
audit_ntp_log(&result.ad);
26522662

2653-
if (clock_set)
2663+
if (result.clock_set)
26542664
clock_was_set(CLOCK_SET_WALL);
26552665

2656-
ntp_notify_cmos_timer(offset_set);
2666+
ntp_notify_cmos_timer(result.delta.tv_sec != 0);
26572667

26582668
return ret;
26592669
}

0 commit comments

Comments
 (0)