From: Igor Velkov When __rtc_set_alarm() finds that the alarm time has already passed, rtc_timer_enqueue() keeps the system awake with pm_stay_awake(), expires the timer from the work queue and returns 0. pm_stay_awake() aborts a suspend in progress only when the wakeup event check is armed, through /sys/power/wakeup_count or by autosleep. Otherwise the suspend goes on with no alarm set. alarmtimer_suspend() rejects the suspend when rtc_timer_start() fails ("if in the past reject suspend briefly to handle"), but rtc_timer_start() never fails for an expired timer. alarmtimer passes an expiry at least 2 seconds ahead, so this shows once rtc_timer_start() rounds the expiry down to the minute. Return -ETIME from rtc_timer_start() and leave the timer stopped when its expiry has already passed, whether before queueing it or when programming the alarm. Removing the timer reprograms the alarm for the next timer on the queue, as rtc_timer_cancel() does. The alarm and update interrupt timers are still expired from the work queue when late. Fixes: 59a93c27c489 ("alarmtimer: Implement minimum alarm interval for allowing suspend") Cc: stable@vger.kernel.org Signed-off-by: Igor Velkov --- drivers/rtc/interface.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index 96626f8068f9..bc281037c67c 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -856,9 +856,11 @@ int rtc_irq_set_freq(struct rtc_device *rtc, int freq) } /** - * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue + * __rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue * @rtc: rtc device * @timer: timer being added. + * @fail_if_past: return -ETIME for a timer that has already expired + * instead of expiring it from the work queue * * Enqueues a timer onto the rtc devices timerqueue and sets * the next alarm event appropriately. @@ -867,7 +869,8 @@ int rtc_irq_set_freq(struct rtc_device *rtc, int freq) * * Must hold ops_lock for proper serialization of timerqueue */ -static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) +static int __rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer, + bool fail_if_past) { struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); struct rtc_time tm; @@ -878,8 +881,11 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) if (err) return err; - timer->enabled = 1; now = rtc_tm_to_ktime(tm); + if (fail_if_past && timer->node.expires <= now) + return -ETIME; + + timer->enabled = 1; /* Skip over expired timers */ while (next) { @@ -897,6 +903,10 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) alarm.enabled = 1; err = __rtc_set_alarm(rtc, &alarm); if (err == -ETIME) { + if (fail_if_past) { + rtc_timer_remove(rtc, timer); + return err; + } pm_stay_awake(rtc->dev.parent); schedule_work(&rtc->irqwork); } else if (err) { @@ -909,6 +919,11 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) return 0; } +static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) +{ + return __rtc_timer_enqueue(rtc, timer, false); +} + static void rtc_alarm_disable(struct rtc_device *rtc) { if (!rtc->ops || !test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable) @@ -1060,7 +1075,8 @@ void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r), * @ expires: time at which to expire the timer * @ period: period that the timer will recur * - * Kernel interface to set an rtc_timer + * Kernel interface to set an rtc_timer. Returns -ETIME and leaves the + * timer stopped if @expires has already passed. */ int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer, ktime_t expires, ktime_t period) @@ -1074,7 +1090,7 @@ int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer, timer->node.expires = expires; timer->period = period; - ret = rtc_timer_enqueue(rtc, timer); + ret = __rtc_timer_enqueue(rtc, timer, true); mutex_unlock(&rtc->ops_lock); return ret; -- 2.43.0