Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtc: cmos: use spin_lock_irqsave in cmos_interrupt

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/
Link: https://lore.kernel.org/r/20250607210608.14835-1-mat.jonczyk@o2.pl
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

authored by

Mateusz Jończyk and committed by
Alexandre Belloni
00a39d86 b1248da0

+6 -4
+6 -4
drivers/rtc/rtc-cmos.c
··· 692 692 { 693 693 u8 irqstat; 694 694 u8 rtc_control; 695 + unsigned long flags; 695 696 696 - spin_lock(&rtc_lock); 697 + /* We cannot use spin_lock() here, as cmos_interrupt() is also called 698 + * in a non-irq context. 699 + */ 700 + spin_lock_irqsave(&rtc_lock, flags); 697 701 698 702 /* When the HPET interrupt handler calls us, the interrupt 699 703 * status is passed as arg1 instead of the irq number. But ··· 731 727 hpet_mask_rtc_irq_bit(RTC_AIE); 732 728 CMOS_READ(RTC_INTR_FLAGS); 733 729 } 734 - spin_unlock(&rtc_lock); 730 + spin_unlock_irqrestore(&rtc_lock, flags); 735 731 736 732 if (is_intr(irqstat)) { 737 733 rtc_update_irq(p, 1, irqstat); ··· 1299 1295 * ACK the rtc irq here 1300 1296 */ 1301 1297 if (t_now >= cmos->alarm_expires && cmos_use_acpi_alarm()) { 1302 - local_irq_disable(); 1303 1298 cmos_interrupt(0, (void *)cmos->rtc); 1304 - local_irq_enable(); 1305 1299 return; 1306 1300 } 1307 1301