[PATCH v2 1/3] iio: adc: versal-sysmon: fix race in interrupt teardown

From: Amit Kumar Mahapatra

Date: Mon Nov 30 2026 - 05:20:11 EST


sysmon_disable_interrupts() masks all interrupts, then clears masked_temp
under irq_lock and cancels the unmask worker. A worker that runs between
the mask write and the locked section still sees the old masked_temp and
can re-enable an alarm through IER. If that alarm fires, the handler
queues the worker again after cancel_delayed_work_sync() has returned.
On unbind this action runs before devm releases the IRQ, so the queued
work can run after the devm-allocated iio_dev that embeds it has been
freed.

Mask all interrupts and clear masked_temp in the same irq_lock section,
and only then cancel the worker. A worker that runs afterwards has
nothing to unmask, and a handler finds every source masked and returns
IRQ_NONE without queueing work. Make sysmon_unmask_temp() return early
when nothing is masked, so that the worker does not touch IER once
teardown has cleared masked_temp.

Fixes: c7f7eb10638f ("iio: adc: versal-sysmon: add threshold event support")
Signed-off-by: Amit Kumar Mahapatra <amit.kumar-mahapatra@xxxxxxx>
---
Changes in v2:
- Drop disable_irq() and the stored IRQ number. Mask all interrupts and
clear masked_temp under irq_lock before cancelling the worker (Jonathan)
- Return early from sysmon_unmask_temp() when nothing is masked (Jonathan)
- Drop Prasanna's Reviewed-by, as the fix has changed

drivers/iio/adc/versal-sysmon-core.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/adc/versal-sysmon-core.c b/drivers/iio/adc/versal-sysmon-core.c
index 1b55d343982e..a7ff65075d7d 100644
--- a/drivers/iio/adc/versal-sysmon-core.c
+++ b/drivers/iio/adc/versal-sysmon-core.c
@@ -712,6 +712,9 @@ static void sysmon_unmask_temp(struct sysmon *sysmon, unsigned int isr)
unsigned int status;
u32 ier;

+ if (!sysmon->masked_temp)
+ return;
+
status = isr & SYSMON_TEMP_INTR_MASK;

ier = ~status & sysmon->masked_temp;
@@ -781,10 +784,10 @@ static void sysmon_disable_interrupts(void *data)
{
struct sysmon *sysmon = data;

- regmap_write(sysmon->regmap, SYSMON_IDR, SYSMON_INTR_ALL_MASK);
-
- scoped_guard(spinlock_irq, &sysmon->irq_lock)
+ scoped_guard(spinlock_irq, &sysmon->irq_lock) {
+ regmap_write(sysmon->regmap, SYSMON_IDR, SYSMON_INTR_ALL_MASK);
sysmon->masked_temp = 0;
+ }

cancel_delayed_work_sync(&sysmon->sysmon_unmask_work);
}
--
2.44.4