Skip to contentSkip to conversation

iio: adc: versal-sysmon: Fix interrupt teardown and hysteresis, update maintainers

PATCH In review Amit Kumar Mahapatra 2 participants 6 messages Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover iio: adc: versal-sysmon: Fix interrupt teardown and hysteresis, update maintainers

This patch series:
- Fixes a race between the interrupt teardown and the interrupt handler
- Validates the temperature hysteresis written by userspace and read from hardware at probe
- Updates the Versal SysMon maintainers: removes Salih Erim, who is no longer with AMD, and adds myself

Changes in v2:
- 1/3: Mask all interrupts and clear masked_temp under irq_lock before cancelling the worker, instead of using disable_irq() (Jonathan)
- 2/3: Explain where the probe-time thresholds come from (Andy)
- 3/3: Collect Acked-by from Michal and Sai
- Drop "disable interrupts on shutdown": no concrete failure without it, as the next kernel's probe masks all interrupts before requesting the IRQ (Jonathan)
- Drop "add over-temperature alarm events" while the userspace interface is still under discussion on the v1 thread
- Drop "arm64: versal-net: Add SysMon and thermal zone", sent separately to go through Michal's tree

v1: https://lore.kernel.org/all/[email protected]/

Amit Kumar Mahapatra (3):
iio: adc: versal-sysmon: fix race in interrupt teardown
iio: adc: versal-sysmon: validate temperature hysteresis
MAINTAINERS: update Versal SysMon maintainers

 MAINTAINERS                          |  2 +-
 drivers/iio/adc/versal-sysmon-core.c | 19 +++++++++++--------
 drivers/iio/adc/versal-sysmon.h      |  4 ++++
 3 files changed, 16 insertions(+), 9 deletions(-)

base-commit: 92c9d64c0cf749c8369403a32630562f0e855a6c

Signature · 1 line
2.44.4
#1 lore raw

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

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")

---
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(-)
drivers/iio/adc/versal-sysmon-core.c +6 -3
index 1b55d343982e..a7ff65075d7d 100644
@@ -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);
 }
Signature · 1 line
2.44.4
Reviewed-by on v2 1/3
#2 lore raw

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

Quoted 2 lines · Amit Kumar Mahapatra

Mask all interrupts and clear masked_temp in the same irq_lock section, and only then cancel the worker.

#3 lore raw

PATCH v2 2/3 iio: adc: versal-sysmon: validate temperature hysteresis

In reply to Amit Kumar Mahapatra

The hysteresis written through sysfs is only checked for being negative. A large value makes upper_mc - hysteresis in sysmon_update_temp_lower() overflow before it is clamped. Reject values above the full Q8.7 span of 511992 millicelsius, and name the Q8.7 temperature limits so that the span and the existing range checks share one definition.

The cached hysteresis is seeded at probe from the threshold registers without checking that the lower threshold is not above the upper one. These registers are not described in DT: at probe they hold either their reset values or whatever the platform firmware programmed from the design configuration. Failing the probe would take away every voltage and temperature reading over a comparator setting the driver does not own, so treat an inverted pair as zero hysteresis, as negative values are rejected on the write path. The next threshold write then restores lower = upper - hysteresis.

Fixes c7f7eb10638f ("iio: adc: versal-sysmon: add threshold event support")

---
Changes in v2:
- Explain in the commit message where the probe-time thresholds come from, and why they are not rejected at probe (Andy)

 drivers/iio/adc/versal-sysmon-core.c | 10 +++++-----
 drivers/iio/adc/versal-sysmon.h      |  4 ++++
 2 files changed, 9 insertions(+), 5 deletions(-)
drivers/iio/adc/versal-sysmon-core.c +5 -5
index a7ff65075d7d..2bbc6808132c 100644
@@ -352,7 +352,8 @@ static int sysmon_update_temp_lower(struct sysmon *sysmon)
 		return ret;
 
 	sysmon_q8p7_to_millicelsius(upper_reg, &upper_mc);
-	lower_mc = clamp(upper_mc - sysmon->temp_hysteresis, -256000, 255992);
+	lower_mc = clamp(upper_mc - sysmon->temp_hysteresis,
+			 SYSMON_TEMP_MIN_MC, SYSMON_TEMP_MAX_MC);
 	sysmon_millicelsius_to_q8p7(&raw_val, lower_mc);
 
 	return regmap_write(sysmon->regmap, SYSMON_TEMP_TH_LOW, raw_val);
@@ -429,8 +430,7 @@ static int sysmon_write_event_value(struct iio_dev *indio_dev,
 	case IIO_TEMP:
 		switch (info) {
 		case IIO_EV_INFO_VALUE:
-			/* Q8.7 signed range: -256000 to +255992 mC */
-			if (val < -256000 || val > 255992)
+			if (val < SYSMON_TEMP_MIN_MC || val > SYSMON_TEMP_MAX_MC)
 				return -EINVAL;
 
 			sysmon_millicelsius_to_q8p7(&raw_val, val);
@@ -443,7 +443,7 @@ static int sysmon_write_event_value(struct iio_dev *indio_dev,
 			return sysmon_update_temp_lower(sysmon);
 
 		case IIO_EV_INFO_HYSTERESIS:
-			if (val < 0)
+			if (val < 0 || val > SYSMON_TEMP_MAX_MC - SYSMON_TEMP_MIN_MC)
 				return -EINVAL;
 
 			sysmon->temp_hysteresis = val;
@@ -838,7 +838,7 @@ static int sysmon_init_hysteresis(struct sysmon *sysmon, int *hysteresis)
 
 	sysmon_q8p7_to_millicelsius(upper_reg, &upper_mc);
 	sysmon_q8p7_to_millicelsius(lower_reg, &lower_mc);
-	*hysteresis = upper_mc - lower_mc;
+	*hysteresis = max(upper_mc - lower_mc, 0);
 
 	return 0;
 }
drivers/iio/adc/versal-sysmon.h +4 -0
index bb9a75bf71c0..930b7e7d21ce 100644
@@ -75,6 +75,10 @@ struct regmap;
 #define SYSMON_FRACTIONAL_SHIFT		7U
 #define SYSMON_SUPPLY_MANTISSA_BITS	16
 
+/* Q8.7 signed temperature range in millicelsius */
+#define SYSMON_TEMP_MIN_MC		-256000
+#define SYSMON_TEMP_MAX_MC		255992
+
 /* Bits per alarm register */
 #define SYSMON_ALARM_BITS_PER_REG	32
 
Signature · 1 line
2.44.4
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] iio: adc: versal-sysmon: validate temperature hysteresis

Quoted 3 lines · Amit Kumar Mahapatra

The hysteresis written through sysfs is only checked for being negative. A large value makes upper_mc - hysteresis in sysmon_update_temp_lower() overflow before it is clamped.

Not for this series: write_event_value() ignores val2, so with the default IIO_VAL_INT_PLUS_MICRO format a hysteresis of -0.5 is accepted as 0. Might be worth a separate follow-up rejecting val2 != 0, as Jonathan suggested for sx9324 [1].

[1] https://lore.kernel.org/linux-iio/20261130003945.0afa767e@jic23-hlaptop/

#5 lore raw

PATCH v2 3/3 MAINTAINERS: update Versal SysMon maintainers

In reply to Amit Kumar Mahapatra

Replace Salih Erim with myself as a maintainer of the Versal SysMon driver.

---
Changes in v2:
- Collect Acked-by from Michal and Sai

 MAINTAINERS | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
MAINTAINERS +1 -1
index 02015eaec312..a8f8d936fa12 100644
@@ -30800,8 +30800,8 @@ F:	drivers/edac/versalnet_edac.c
 F:	include/linux/cdx/edac_cdx_pcol.h
 
 XILINX VERSAL SYSMON DRIVER
-M:	Salih Erim <[email protected]>
 M:	Sai Krishna Potthuri <[email protected]>
+M:	Amit Kumar Mahapatra <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/adc/xlnx,versal-sysmon.yaml
Signature · 1 line
2.44.4