linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rtc-cmos: use spin_lock_irqsave in cmos_interrupt
@ 2025-06-07 21:06 Mateusz Jończyk
  2025-06-23 22:29 ` Alexandre Belloni
  0 siblings, 1 reply; 2+ messages in thread
From: Mateusz Jończyk @ 2025-06-07 21:06 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: Thomas Gleixner, Borislav Petkov, linux-rtc, lkml,
	Frederic Weisbecker, Peter Zijlstra, Chris Bainbridge,
	Sebastian Andrzej Siewior, Xiaofei Tan, Mateusz Jończyk,
	stable

cmos_interrupt() can be called in a non-interrupt context, such as in
an ACPI event handler (which runs in an interrupt thread). Therefore,
usage of spin_lock(&rtc_lock) is insecure. Use spin_lock_irqsave() /
spin_unlock_irqrestore() instead.

Before a misguided
commit 6950d046eb6e ("rtc: cmos: Replace spin_lock_irqsave with spin_lock in hard IRQ")
the cmos_interrupt() function used spin_lock_irqsave(). That commit
changed it to spin_lock() and broke locking, which was partially fixed in
commit 13be2efc390a ("rtc: cmos: Disable irq around direct invocation of cmos_interrupt()")

That second commit did not take account of the ACPI fixed event handler
pathway, however. It introduced local_irq_disable() workarounds in
cmos_check_wkalrm(), which can cause problems on PREEMPT_RT kernels
and are now unnecessary.

Add an explicit comment so that this change will not be reverted by
mistake.

Cc: <stable@vger.kernel.org>
Fixes: 6950d046eb6e ("rtc: cmos: Replace spin_lock_irqsave with spin_lock in hard IRQ")
Signed-off-by: Mateusz Jończyk <mat.jonczyk@o2.pl>
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Tested-by: Chris Bainbridge <chris.bainbridge@gmail.com>
Reported-by: Chris Bainbridge <chris.bainbridge@gmail.com>
Closes: https://lore.kernel.org/all/aDtJ92foPUYmGheF@debian.local/

---

Changes after DRAFT version of the patch:
- rewrite commit message,
- test this locally (also on top of 5.10.238 for the stable backport),
- fix a grammar mistake in the comment.
---
 drivers/rtc/rtc-cmos.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
index 8172869bd3d7..0743c6acd6e2 100644
--- a/drivers/rtc/rtc-cmos.c
+++ b/drivers/rtc/rtc-cmos.c
@@ -692,8 +692,12 @@ static irqreturn_t cmos_interrupt(int irq, void *p)
 {
 	u8		irqstat;
 	u8		rtc_control;
+	unsigned long	flags;
 
-	spin_lock(&rtc_lock);
+	/* We cannot use spin_lock() here, as cmos_interrupt() is also called
+	 * in a non-irq context.
+	 */
+	spin_lock_irqsave(&rtc_lock, flags);
 
 	/* When the HPET interrupt handler calls us, the interrupt
 	 * status is passed as arg1 instead of the irq number.  But
@@ -727,7 +731,7 @@ static irqreturn_t cmos_interrupt(int irq, void *p)
 			hpet_mask_rtc_irq_bit(RTC_AIE);
 		CMOS_READ(RTC_INTR_FLAGS);
 	}
-	spin_unlock(&rtc_lock);
+	spin_unlock_irqrestore(&rtc_lock, flags);
 
 	if (is_intr(irqstat)) {
 		rtc_update_irq(p, 1, irqstat);
@@ -1295,9 +1299,7 @@ static void cmos_check_wkalrm(struct device *dev)
 	 * ACK the rtc irq here
 	 */
 	if (t_now >= cmos->alarm_expires && cmos_use_acpi_alarm()) {
-		local_irq_disable();
 		cmos_interrupt(0, (void *)cmos->rtc);
-		local_irq_enable();
 		return;
 	}
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rtc-cmos: use spin_lock_irqsave in cmos_interrupt
  2025-06-07 21:06 [PATCH] rtc-cmos: use spin_lock_irqsave in cmos_interrupt Mateusz Jończyk
@ 2025-06-23 22:29 ` Alexandre Belloni
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Belloni @ 2025-06-23 22:29 UTC (permalink / raw)
  To: Mateusz Jończyk
  Cc: Thomas Gleixner, Borislav Petkov, linux-rtc, lkml,
	Frederic Weisbecker, Peter Zijlstra, Chris Bainbridge,
	Sebastian Andrzej Siewior, Xiaofei Tan, stable

On Sat, 07 Jun 2025 23:06:08 +0200, Mateusz Jończyk wrote:
> cmos_interrupt() can be called in a non-interrupt context, such as in
> an ACPI event handler (which runs in an interrupt thread). Therefore,
> usage of spin_lock(&rtc_lock) is insecure. Use spin_lock_irqsave() /
> spin_unlock_irqrestore() instead.
> 
> Before a misguided
> commit 6950d046eb6e ("rtc: cmos: Replace spin_lock_irqsave with spin_lock in hard IRQ")
> the cmos_interrupt() function used spin_lock_irqsave(). That commit
> changed it to spin_lock() and broke locking, which was partially fixed in
> commit 13be2efc390a ("rtc: cmos: Disable irq around direct invocation of cmos_interrupt()")
> 
> [...]

Applied, thanks!

[1/1] rtc-cmos: use spin_lock_irqsave in cmos_interrupt
      https://git.kernel.org/abelloni/c/00a39d8652ff

Best regards,

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-06-23 22:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-07 21:06 [PATCH] rtc-cmos: use spin_lock_irqsave in cmos_interrupt Mateusz Jończyk
2025-06-23 22:29 ` Alexandre Belloni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).