Skip to content

Commit e697df3

Browse files
NTMangregkh
authored andcommitted
timekeeping: Register default clocksource before taking tk_core.lock
commit 8fa3082 upstream. Commit f24df84 ("time/jiffies: Register jiffies clocksource before usage") moved the jiffies clocksource registration into clocksource_default_clock(), so that it is registered lazily on the first call. __clocksource_register() acquires clocksource_mutex, but the first caller is timekeeping_init(), which invokes clocksource_default_clock() while holding tk_core.lock, a raw spinlock. Acquiring a sleeping mutex while holding a raw spinlock is invalid. The default clocksource only has to be registered before tk_setup_internals() consumes its mult/shift/maxadj. Neither clocksource_default_clock(), the ->enable() callback, nor the registration itself need tk_core.lock, so fetch and enable the clock before acquiring the lock. This preserves the "register before usage" ordering while keeping clocksource_mutex out of the raw spinlock section. clocksource_default_clock() has a second caller, clocksource_done_booting(), which invokes it with clocksource_mutex already held. That path avoids a recursive lock because timekeeping_init() has already run and set cs_jiffies_registered, so the registration is skipped there. This change does not alter that; it only fixes the invalid wait context in timekeeping_init(). Fixes: f24df84 ("time/jiffies: Register jiffies clocksource before usage") Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Reported-by: Breno Leitao <leitao@debian.org> Reported-by: Oleg Nesterov <oleg@redhat.com> Reviewed-by: Breno Leitao <leitao@debian.org> Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20260616070914.65818-1-mikhail.v.gavrilov@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9e04055 commit e697df3

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

kernel/time/timekeeping.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,13 +1825,14 @@ void __init timekeeping_init(void)
18251825
*/
18261826
wall_to_mono = timespec64_sub(boot_offset, wall_time);
18271827

1828+
clock = clocksource_default_clock();
1829+
if (clock->enable)
1830+
clock->enable(clock);
1831+
18281832
guard(raw_spinlock_irqsave)(&tk_core.lock);
18291833

18301834
ntp_init();
18311835

1832-
clock = clocksource_default_clock();
1833-
if (clock->enable)
1834-
clock->enable(clock);
18351836
tk_setup_internals(tks, clock);
18361837

18371838
tk_set_xtime(tks, &wall_time);

0 commit comments

Comments
 (0)