Skip to content

Commit

Permalink
timekeeping: Abort clocksource change in case of failure
Browse files Browse the repository at this point in the history
There is no point to go through a full timekeeping update when acquiring a
module reference or enabling the new clocksource fails.

Signed-off-by: Thomas Gleixner <[email protected]>
Signed-off-by: Anna-Maria Behnsen <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Acked-by: John Stultz <[email protected]>
Link: https://lore.kernel.org/all/20241009-devel-anna-maria-b4-timers-ptp-timekeeping-v2-4-554456a44a15@linutronix.de
  • Loading branch information
KAGA-KOKO committed Oct 25, 2024
1 parent 9fe7d9a commit 1f7226b
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions kernel/time/timekeeping.c
Original file line number Diff line number Diff line change
Expand Up @@ -1608,33 +1608,29 @@ static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
static int change_clocksource(void *data)
{
struct timekeeper *tk = &tk_core.timekeeper;
struct clocksource *new, *old = NULL;
struct clocksource *new = data, *old = NULL;
unsigned long flags;
bool change = false;

new = (struct clocksource *) data;

/*
* If the cs is in module, get a module reference. Succeeds
* for built-in code (owner == NULL) as well.
* If the clocksource is in a module, get a module reference.
* Succeeds for built-in code (owner == NULL) as well. Abort if the
* reference can't be acquired.
*/
if (try_module_get(new->owner)) {
if (!new->enable || new->enable(new) == 0)
change = true;
else
module_put(new->owner);
if (!try_module_get(new->owner))
return 0;

/* Abort if the device can't be enabled */
if (new->enable && new->enable(new) != 0) {
module_put(new->owner);
return 0;
}

raw_spin_lock_irqsave(&timekeeper_lock, flags);
write_seqcount_begin(&tk_core.seq);

timekeeping_forward_now(tk);

if (change) {
old = tk->tkr_mono.clock;
tk_setup_internals(tk, new);
}

old = tk->tkr_mono.clock;
tk_setup_internals(tk, new);
timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);

write_seqcount_end(&tk_core.seq);
Expand All @@ -1643,7 +1639,6 @@ static int change_clocksource(void *data)
if (old) {
if (old->disable)
old->disable(old);

module_put(old->owner);
}

Expand Down

0 comments on commit 1f7226b

Please sign in to comment.