From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796163524; b=j/nB+hDmV0MlGihH4bMMSZ0qpizLBggAbT8JFk8cgg6eIfnKMGC7VxOPqU9G4GMDRqpK 1cGyvjJI7d37OUE5xtlGLyFuVUcNNdLuTMTFAHOBfdZyhdFvqSxY4SRLRzcS/M2Aud7C5 nxB3VuQ/pzDE8FJJLg3NC5ZphzuFwpdqv1IJ8ARG0dT2Gi963Vja2D08EtrGATUdrIjrG O4Vz42M28lroam1++o+v8OcflwV+bdnsZJBV4j7ZlgF45YfsOFPpZGMmBeQs/XZNA0X/a YJba5Tq9V8iZIn5n4aGUVc5KwfX11faWXvpiVXy8TAN0Nu8+ZVpNSJDiY8auOh7a+2g== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796163524; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=J2JgHdntmE8A6sXzKwfOYyps1it+9hSUHmV3h0zSBjQ=; b=DrmqnH4eppfJvdmHqWHkx6WhyQmLqgHS3PxyIoVGwxh+ry/bICPcLqi9ycvMvbVnF4QS VG4u2gTT7YVOxgiRNo/M7/iZjGMxl5kfdEY7r+ym4drYa/G8BFEEx/pN3AlXAnVAeVLFT cI2fIDSZZGl3JajXXXOlLAAmdOOQ3MqnOjJycF8kSrOgoqcNovz9wQ3W98+wOACwud+ID ETQU0yLF9vN/bnSvNAr1c95kSWKng4W6h+2L297IHOTIw+zu1fBEvp7R626IPYUyaDvpS EtT5Fc5UHQZLwh/liPAXIUPIJFBfe0bLEBHfw8ap4D76Ve28gA1EgNpanKcOczSCLjg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=a6wShlsw; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4151E606A32E; Tue, 1 Dec 2026 22:18:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=a6wShlsw Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796163523; b=2UAF2wtS2taai3ceD0hBMxt1vxxaRYvojpb6q2JeiZyW3myqbgr4himjMeTa3/ZWhvKa pmlU4MHIZC/QOjDXRPkQy57zOdXxZWaOhxq+8wt3B12an7dHEATSOhRTCTQk1/RXUyDS9 X/1a7dpKXfpuOy6j4GozKRbveLGFdmCtg7CKovY/KAE64YLEZ8SK55EkZbER7IBT9PZhj 4hhiGFEYihyXlr4uwV+9LnNfQ/RCf6IHJxfqvAVI/8qSLg2VV6qSU3+f/9PCJOGiGPaWI Nu6Ehp+hm1A3e+M4oTF2VeAoZDttWinVv7RBwCuKqoxZl5SljcqcQc8MhByYpm3l+6g== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796163523; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=J2JgHdntmE8A6sXzKwfOYyps1it+9hSUHmV3h0zSBjQ=; b=VgSssAam7OkJj9iAD0BbofH1E+MVrvj/3jzbOyOh8nt+C7sgCYfc9RqnKU2evf23KUeq ahWHUOezwlKtXm8iFLcL6wgRYV1RtXQdbJXCIQ5urfxV3fHm03v6SKqi4dEOcqNchyMqo HmOFFkSgS3IdArRZ56ERppA//8fKdpArwCq1HC1rymNKnkUCyLUgsryq7msMUGTSGqHyr Iqk7y0BCPrCfwAl4TpgCageeBLxeoYsTg2M4kWaPug5iI0SdEe7NAZTykYnAfewURyd0Y ab3HSE/lsRssqpoReUOKPaNcnQKJq1dRGwXgXiXrcOWDj036dAX0mSAdAvpN+vGInbA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 4C0241F0089B; Tue, 1 Dec 2026 22:18:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796163523; bh=J2JgHdntmE8A6sXzKwfOYyps1it+9hSUHmV3h0zSBjQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=a6wShlsw+klKuCmBJfVC3aqLzvODzS1pnoKDSG9OoU845Pbu06TXu9zMOiXRcy8mY UQVSHdYjJatZ7jmkoP6aULW9DQZH5uaVgagub+yy3OYmaiLF5lrwzOUovTXweGEB8i KkrP9GAL0Kuw3s9uGlby2rmTlG5UySo454VWQhFFxzHHpQtVN8FSDibZf5Axo0dIZI oBr2N/NW9nSedLLmOfBG1VUZEO/6DH/dITxD+0u0Y6sQXPJhIZd5IRUg5vZuzyLgru 3xR/rzaMQcKS8wDanGWWeUaL+S26NZNqrH2/kne+VkJ/AoY6x6ZCO5tYeMoHFlfmWT 7G+MGolIrUAMw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id D16C54F106; Tue, 1 Dec 2026 22:18:42 +0000 (UTC) From: Igor Velkov via B4 Relay Date: Tue, 01 Dec 2026 22:18:40 +0000 Subject: [PATCH 1/2] rtc: interface: let rtc_timer_start() fail for an expired timer X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261201-b4-rtc-timer-round-down-v1-1-4ac60b362a54@iav.lv> References: <20261201-b4-rtc-timer-round-down-v1-0-4ac60b362a54@iav.lv> In-Reply-To: <20261201-b4-rtc-timer-round-down-v1-0-4ac60b362a54@iav.lv> To: Alexandre Belloni , John Stultz , Thomas Gleixner Cc: Stephen Boyd , Miroslav Lichvar , linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Igor Velkov X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4369; i=iav@iav.lv; h=from:subject:message-id; bh=V7hHfJXqPGH/gYTnZOT6ShC7Ksgaqgzw2HCaddJgFik=; b=owGbwMvMwCVmNH1q96v4SmbG02pJDNn87geDMtY2Pq2wlbJ6Pr0+tMer+5w3R0ndSg45jdDGB onn7eIdpSwMYlwMsmKKLBMOadzKWHL1r7+H+QOYOaxMIEMYuDgFYCJhjIwMD2SnPDtpfK/+RlRG +U+hjy2sc4XPltod7uzV9SuTtZV/xfBX7vl7Dv/ZWXaTJhzXarc4eFBYqvXGq2frlX5nmN+Uc4n gBwA= X-Developer-Key: i=iav@iav.lv; a=openpgp; fpr=90C228DA68A4D5FD4F4837E03297958BEA5F7903 X-Endpoint-Received: by B4 Relay for iav@iav.lv/default with auth_id=1060 X-Original-From: Igor Velkov Reply-To: iav@iav.lv Content-Transfer-Encoding: 7bit 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