From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82B7532AACD; Mon, 27 Oct 2025 18:56:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761591400; cv=none; b=SpqL/cVLKhZm0fIrG0xm5YJyYviCtNr/6SHM//nIcSRqf0fuJmqipCBGyiA1QSFg0G0t5rfBFBN8vgRo0s4ADkqm5XribzNiM1ooRMnfJ7t76CKv9xhLP0XoelgFwlPdMBg/WYUjFkxbnaPJLPcXSl+fcVsHFKBgg7bpX3hEDuw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761591400; c=relaxed/simple; bh=oaTYx/R6OHQp/PbtCsv7UOaIHBSkSzUszVXRZMom3cs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WNnAfRyAQTp/Cj5KVUVEIPsQkI9MF+uT36tRBi+7XtMPvjRcwQvp1ZLOBNsaUZZ3hUicijrvrtcM/naebEuCHL5XJSKX5FcdWOb+XoawpzaHlrODnd4xruxJ3pw/zmNB87Zb06hhJq6ZoYNpHSveuJBTTxNG8TjS6hmzw4+bCXk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=gmpr2jWi; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="gmpr2jWi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA5BEC4CEF1; Mon, 27 Oct 2025 18:56:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1761591400; bh=oaTYx/R6OHQp/PbtCsv7UOaIHBSkSzUszVXRZMom3cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gmpr2jWilg/JLaFWTOIgQKNvcWA8DxASV1ZMk2J9MhSR2whURew9CBmtv4NSD5FT2 fotgPJCwBuBZw5fwFQFdGcsmAfkaPD7lJiuyzYi0kSms9CUuk6NX8/waHqqv1YX4DS GyrsY3BZaKixjeoxkazWMY40zyMXZSjU1mmuS34o= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Esben Haabendal , Alexandre Belloni Subject: [PATCH 5.10 155/332] rtc: interface: Fix long-standing race when setting alarm Date: Mon, 27 Oct 2025 19:33:28 +0100 Message-ID: <20251027183528.723805186@linuxfoundation.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20251027183524.611456697@linuxfoundation.org> References: <20251027183524.611456697@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: Esben Haabendal commit 795cda8338eab036013314dbc0b04aae728880ab upstream. As described in the old comment dating back to commit 6610e0893b8b ("RTC: Rework RTC code to use timerqueue for events") from 2010, we have been living with a race window when setting alarm with an expiry in the near future (i.e. next second). With 1 second resolution, it can happen that the second ticks after the check for the timer having expired, but before the alarm is actually set. When this happen, no alarm IRQ is generated, at least not with some RTC chips (isl12022 is an example of this). With UIE RTC timer being implemented on top of alarm irq, being re-armed every second, UIE will occasionally fail to work, as an alarm irq lost due to this race will stop the re-arming loop. For now, I have limited the additional expiry check to only be done for alarms set to next seconds. I expect it should be good enough, although I don't know if we can now for sure that systems with loads could end up causing the same problems for alarms set 2 seconds or even longer in the future. I haven't been able to reproduce the problem with this check in place. Cc: stable@vger.kernel.org Signed-off-by: Esben Haabendal Link: https://lore.kernel.org/r/20250516-rtc-uie-irq-fixes-v2-1-3de8e530a39e@geanix.com Signed-off-by: Alexandre Belloni Signed-off-by: Greg Kroah-Hartman --- drivers/rtc/interface.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -442,6 +442,29 @@ static int __rtc_set_alarm(struct rtc_de else err = rtc->ops->set_alarm(rtc->dev.parent, alarm); + /* + * Check for potential race described above. If the waiting for next + * second, and the second just ticked since the check above, either + * + * 1) It ticked after the alarm was set, and an alarm irq should be + * generated. + * + * 2) It ticked before the alarm was set, and alarm irq most likely will + * not be generated. + * + * While we cannot easily check for which of these two scenarios we + * are in, we can return -ETIME to signal that the timer has already + * expired, which is true in both cases. + */ + if ((scheduled - now) <= 1) { + err = __rtc_read_time(rtc, &tm); + if (err) + return err; + now = rtc_tm_to_time64(&tm); + if (scheduled <= now) + return -ETIME; + } + trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err); return err; }