[tip: timers/core] timekeeping: Mark auxiliary clock active under tk_core.lock

From: tip-bot2 for Andrea Parri

Date: Mon Nov 30 2026 - 16:13:06 EST


The following commit has been merged into the timers/core branch of tip:

Commit-ID: b4c794ff2b232e081d8ad3f0575eba2c49442949
Gitweb: https://git.kernel.org/tip/b4c794ff2b232e081d8ad3f0575eba2c49442949
Author: Andrea Parri <parri.andrea@xxxxxxxxx>
AuthorDate: Mon, 30 Nov 2026 11:07:32 +01:00
Committer: Thomas Gleixner <tglx@xxxxxxxxxx>
CommitterDate: Mon, 30 Nov 2026 22:06:25 +01:00

timekeeping: Mark auxiliary clock active under tk_core.lock

Enabling an auxiliary clock can race with a clocksource change, leaving
its timekeeper on the old clocksource until the next change. On x86-64,
a timekeeper left on hpet after tsc is selected runs fast by the
TSC/HPET frequency ratio, 31 with a 3.1 GHz TSC and QEMU's 100 MHz HPET.

aux_clock_enable() binds the auxiliary timekeeper to the current
clocksource under tk_core.lock, but aux_clock_enable_store() sets its
bit in aux_timekeepers only after the lock is dropped. On a preemptible
kernel the stopper thread can run in between, and
tk_aux_update_clocksource() rebinds only timekeepers whose bit is set:

CPU 0 CPU 1
aux_clock_enable()
lock tk_core.lock
bind aux timekeeper to old
unlock tk_core.lock
<preempted by stopper> stop_machine(change_clocksource, new)
switch core timekeeper to new
tk_aux_update_clocksource()
bit clear, timekeeper skipped
old->disable(old)
module_put(old->owner)
set_bit(id, &aux_timekeepers)

The auxiliary timekeeper then keeps the old clocksource pointer along
with its mult, shift, mask and cycle_last. Since commit 2e27beeb66e4
("timekeeping: Allow inlining clocksource::read()"), tk_clock_read() on
x86-64 reads the TSC directly once tsc is selected, so a timekeeper left
on hpet converts TSC cycles with the HPET mult and shift.

Without the inline read, tk_clock_read() calls the old clocksource's
read() callback instead. Either way, timekeeping_cycles_to_ns() and
__timekeeping_advance() read ->max_cycles and ->max_raw_delta from the
old clocksource, which is a use-after-free if it was unregistered and
freed. That case was not tested.

Set the bit in aux_clock_enable() before tk_core.lock is released.
stop_machine() cannot run change_clocksource() while this CPU holds
tk_core.lock with interrupts disabled, so the change either runs before
the timekeeper is set up, which then uses the new clocksource, or after
the bit is set and rebinds it.

The disable path needs no change. aux_clock_disable() clears
clock_valid before the bit is cleared, and tk_aux_update_clocksource()
skips timekeepers with clock_valid false.

Tested in a 4-vCPU x86-64 KVM guest with PREEMPT_LAZY. One thread
alternates the clocksource between tsc and hpet. Another toggles aux
clock 0, checks current_clocksource after each enable and samples the
CLOCK_AUX rate after every transition to tsc. Unpatched, 3 out of 3
runs hit the race within 3 transitions to tsc. Patched, none of
919075 transitions to tsc did.

The bug was found with the help of an LLM, which was also used to write
the fix, the changelog and the tests.

Fixes: 7b95663a3d96 ("timekeeping: Provide interface to control auxiliary clocks")
Signed-off-by: Andrea Parri <parri.andrea@xxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Link: https://patch.msgid.link/20261130100733.13227-1-parri.andrea@xxxxxxxxx
---
kernel/time/timekeeping.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 3668bd2..4f11708 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -3323,6 +3323,12 @@ static void aux_clock_enable(clockid_t id)
/* Mark it valid and set it live */
aux_tks->clock_valid = true;
timekeeping_update_from_shadow(aux_tkd, TK_UPDATE_ALL);
+
+ /*
+ * Mark it active before tk_core.lock is dropped, so that any later
+ * clocksource change rebinds it in tk_aux_update_clocksource().
+ */
+ set_bit(id - CLOCK_AUX, &aux_timekeepers);
}

static void aux_clock_disable(clockid_t id)
@@ -3355,7 +3361,6 @@ static ssize_t aux_clock_enable_store(struct kobject *kobj, struct kobj_attribut

if (enable) {
aux_clock_enable(CLOCK_AUX + id);
- set_bit(id, &aux_timekeepers);
} else {
aux_clock_disable(CLOCK_AUX + id);
clear_bit(id, &aux_timekeepers);