Skip to contentSkip to conversation

alarmtimer: Preserve rearmed alarms during callback expiry

PATCH In review Avi Weiss 2 participants 2 messages Nov 1 – Nov 9 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 alarmtimer: Preserve rearmed alarms during callback expiry

An alarm can be rearmed while its previous hrtimer callback is still running. alarmtimer_fired() then dequeues the newly armed alarm, so alarmtimer_suspend() no longer sees the wakeup even though the hrtimer remains queued.

Only dequeue the alarm if the hrtimer is no longer queued.

Fixes dae373be9fec ("alarmtimer: Use hrtimer per-alarm instead of per-base")
---
 kernel/time/alarmtimer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/time/alarmtimer.c +2 -1
index ea5be5870e515..bf77af5e32c22 100644
@@ -189,7 +189,8 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
 	struct alarm_base *base = &alarm_bases[alarm->type];
 
 	scoped_guard(spinlock_irqsave, &base->lock)
-		alarmtimer_dequeue(base, alarm);
+		if (!hrtimer_is_queued(timer))
+			alarmtimer_dequeue(base, alarm);
 
 	if (alarm->function)
 		alarm->function(alarm, base->get_ktime());

base-commit: cbbddb6b08cc9900a87cccaa9b22bbe06aa4f50a

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 4 lines · Avi Weiss

An alarm can be rearmed while its previous hrtimer callback is still running. alarmtimer_fired() then dequeues the newly armed alarm, so alarmtimer_suspend() no longer sees the wakeup even though the hrtimer remains queued.

This fails to explain wich alarm timer user is affected. POSIX-timers and timerfd timers are not as they cancel the timer before re-arming it. Which leaves the other two users of alarm_start_timer() which I did not look at.

But if that actually happens then alarm_start_timer() is subtly broken because of this sequence:

        alarm->node.expires = expires;
        alarmtime_enqueue(base, alarm);

Setting alarm->node.expires on a queued timer temporarily "corrupts" the rbtree. That's "harmless" because it's serialized via base->lock and timerqueue_del() does not care as of today, but that's all but robust and future proof.

So this needs to be fixed as well and all of this needs to be documented.

kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) · line 193

Quoted 4 lines
kernel/time/alarmtimer.c +2 -1
 	scoped_guard(spinlock_irqsave, &base->lock)
-		alarmtimer_dequeue(base, alarm);
+		if (!hrtimer_is_queued(timer))
+			alarmtimer_dequeue(base, alarm);

Reading this without a comment just makes me scratch my head and you will scratch yours too three month down the road.

Thanks,

tglx