Skip to contentSkip to conversation

ASoC: mediatek: fix AFE IRQ registration and removal ordering

PATCH In review Myeonghun Pak 2 participants 7 messages Nov 12 – Nov 20

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

v2 cover ASoC: mediatek: fix AFE IRQ registration and removal ordering

This series fixes IRQ lifetime ordering in the MT8186, MT8188, MT8189, MT8195 and MT8196 AFE drivers.

Request each IRQ after regmap initialization so it is freed before the regmap. Where remove() shuts down the AFE, free the IRQ first. Also balance MT8186's IRQ wake setting.

Found by a static analysis tool under development. Not tested on hardware.

Changes in v2:
- Rebased onto broonie/sound for-next (cde5523c75f1).
- 1-3: removed redundant IRQ request error logs and clarified changelogs.
- 4-5: added MT8195 and MT8188.

Link to v1: https://lore.kernel.org/r/[email protected]

Myeonghun Pak (5):
ASoC: mediatek: mt8186: Keep the AFE IRQ within its resource lifetime
ASoC: mediatek: mt8189: Keep the AFE IRQ within its resource lifetime
ASoC: mediatek: mt8196: Keep the AFE IRQ within its resource lifetime
ASoC: mediatek: mt8195: Keep the AFE IRQ within its resource lifetime
ASoC: mediatek: mt8188: Keep the AFE IRQ within its resource lifetime

 sound/soc/mediatek/mt8186/mt8186-afe-common.h |  1 +
 sound/soc/mediatek/mt8186/mt8186-afe-pcm.c    | 42 +++++++++++++------
 sound/soc/mediatek/mt8188/mt8188-afe-pcm.c    | 10 ++---
 sound/soc/mediatek/mt8189/mt8189-afe-common.h |  1 +
 sound/soc/mediatek/mt8189/mt8189-afe-pcm.c    | 16 ++++---
 sound/soc/mediatek/mt8195/mt8195-afe-common.h |  1 +
 sound/soc/mediatek/mt8195/mt8195-afe-pcm.c    | 16 ++++---
 sound/soc/mediatek/mt8196/mt8196-afe-common.h |  1 +
 sound/soc/mediatek/mt8196/mt8196-afe-pcm.c    | 16 ++++---
 9 files changed, 70 insertions(+), 34 deletions(-)

base-commit: cde5523c75f1320a55ac1d55a717ccbb566557f0

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/5 ASoC: mediatek: mt8186: Keep the AFE IRQ within its resource lifetime

The AFE IRQ handler uses afe->regmap, but its managed IRQ is requested before the audiosys reset and before the regmap is initialized. Apart from allowing an interrupt left pending by the firmware or a previous kernel to dereference a NULL regmap, the reverse devres order releases the regmap before the IRQ on probe failure and removal. The IRQ wake setting enabled at probe is also never undone.

Request the IRQ after the reset, the regmap and the handler's other inputs have been initialized, and keep the existing runtime PM error cleanup for a failed request. Balance the IRQ wake setting on the remaining probe error paths and in a new remove callback, which also releases the IRQ before devres tears down the component and the regmap. Save the IRQ number in the private state for that.

Found by a static analysis tool under development.

Fixes 097e874ad3fc ("ASoC: mediatek: mt8186: add platform driver")

---

 sound/soc/mediatek/mt8186/mt8186-afe-common.h |  1 +
 sound/soc/mediatek/mt8186/mt8186-afe-pcm.c    | 42 +++++++++++++------
 2 files changed, 31 insertions(+), 12 deletions(-)
sound/soc/mediatek/mt8186/mt8186-afe-common.h +1 -0
index d59258520995..d03e59ca7d65 100644
@@ -139,6 +139,7 @@ struct mtk_base_irq_data;
 struct clk;
 
 struct mt8186_afe_private {
+	int irq_id;
 	struct clk **clk;
 	struct clk_lookup **lookup;
 	struct regmap *topckgen;
sound/soc/mediatek/mt8186/mt8186-afe-pcm.c +30 -12

83 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/5 ASoC: mediatek: mt8189: Keep the AFE IRQ within its resource lifetime

In reply to Myeonghun Pak

The managed AFE IRQ is requested before the regmap is allocated. The handler can therefore run before its regmap exists, and devres releases the regmap before freeing the IRQ. The remove callback also suspends the AFE and disables its clocks before devres releases the IRQ.

Request the IRQ after initializing the regmap and before enabling MCU IRQ routing. A failed request then takes the existing runtime PM unwind without enabling that routing. Release the IRQ at the start of removal before the explicit clock shutdown, using the IRQ number saved at probe. The regmap's managed lifetime also encloses the IRQ on probe errors.

Found by a static analysis tool under development.

Fixes 7eb153585598 ("ASoC: mediatek: mt8189: add platform driver")

---

 sound/soc/mediatek/mt8189/mt8189-afe-common.h |  1 +
 sound/soc/mediatek/mt8189/mt8189-afe-pcm.c    | 16 ++++++++++------
 2 files changed, 11 insertions(+), 6 deletions(-)
sound/soc/mediatek/mt8189/mt8189-afe-common.h +1 -0
index 910d90d3d146..de715c92add5 100644
@@ -202,6 +202,7 @@ enum {
 struct clk;
 
 struct mt8189_afe_private {
+	int irq_id;
 	struct clk **clk;
 	struct regmap *pmic_regmap;
 
sound/soc/mediatek/mt8189/mt8189-afe-pcm.c +10 -6
index eefa96b701a5..e9fddf2063f5 100644
@@ -2516,12 +2516,7 @@ static int mt8189_afe_pcm_dev_probe(struct platform_device *pdev)
 	irq_id = platform_get_irq(pdev, 0);
 	if (irq_id < 0)
 		return irq_id;
-
-	ret = devm_request_irq(dev, irq_id, mt8189_afe_irq_handler,
-			       IRQF_TRIGGER_NONE,
-			       "Afe_ISR_Handle", afe);
-	if (ret)
-		return ret;
+	afe_priv->irq_id = irq_id;
 
 	/* init sub_dais */
 	INIT_LIST_HEAD(&afe->sub_dais);
@@ -2576,6 +2571,12 @@ static int mt8189_afe_pcm_dev_probe(struct platform_device *pdev)
 		goto err_pm_put;
 	}
 
+	ret = devm_request_irq(dev, irq_id, mt8189_afe_irq_handler,
+			       IRQF_TRIGGER_NONE,
+			       "Afe_ISR_Handle", afe);
+	if (ret)
+		goto err_pm_put;
+
 	regmap_read(afe->regmap, AFE_IRQ_MCU_EN, &tmp_reg);
 	regmap_write(afe->regmap, AFE_IRQ_MCU_EN, 0xffffffff);
 	regmap_read(afe->regmap, AFE_IRQ_MCU_EN, &tmp_reg);
@@ -2603,8 +2604,11 @@ static int mt8189_afe_pcm_dev_probe(struct platform_device *pdev)
 static void mt8189_afe_pcm_dev_remove(struct platform_device *pdev)
 {
 	struct mtk_base_afe *afe = platform_get_drvdata(pdev);
+	struct mt8189_afe_private *afe_priv = afe->platform_priv;
 	struct device *dev = &pdev->dev;
 
+	devm_free_irq(dev, afe_priv->irq_id, afe);
+
 	if (!pm_runtime_status_suspended(dev))
 		mt8189_afe_runtime_suspend(dev);
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/5 ASoC: mediatek: mt8196: Keep the AFE IRQ within its resource lifetime

In reply to Myeonghun Pak

The managed AFE IRQ is requested before the regmap is allocated. The handler can therefore run before its regmap exists, and devres releases the regmap before freeing the IRQ. The remove callback also suspends the AFE and disables its clocks before devres releases the IRQ.

Request the IRQ after initializing the regmap and before enabling MCU IRQ routing. A failed request then takes the existing runtime PM unwind without enabling that routing. Release the IRQ at the start of removal before the explicit clock shutdown, using the IRQ number saved at probe. The regmap's managed lifetime also encloses the IRQ on probe errors.

Found by a static analysis tool under development.

Fixes 57513aabfe5b ("ASoC: mediatek: mt8196: add platform driver")

---

 sound/soc/mediatek/mt8196/mt8196-afe-common.h |  1 +
 sound/soc/mediatek/mt8196/mt8196-afe-pcm.c    | 16 ++++++++++------
 2 files changed, 11 insertions(+), 6 deletions(-)
sound/soc/mediatek/mt8196/mt8196-afe-common.h +1 -0
index 4951a6f32abb..ce5d586721db 100644
@@ -186,6 +186,7 @@ struct clk;
 struct mtk_base_afe;
 
 struct mt8196_afe_private {
+	int irq_id;
 	struct clk **clk;
 	/* dai */
 	void *dai_priv[MT8196_DAI_NUM];
sound/soc/mediatek/mt8196/mt8196-afe-pcm.c +10 -6
index f69ea22cfabb..dc462dd4235c 100644
@@ -2403,12 +2403,7 @@ static int mt8196_afe_pcm_dev_probe(struct platform_device *pdev)
 	irq_id = platform_get_irq(pdev, 0);
 	if (irq_id < 0)
 		return irq_id;
-
-	ret = devm_request_irq(dev, irq_id, mt8196_afe_irq_handler,
-			       IRQF_TRIGGER_NONE,
-			       "Afe_ISR_Handle", afe);
-	if (ret)
-		return ret;
+	afe_priv->irq_id = irq_id;
 
 	/* init sub_dais */
 	INIT_LIST_HEAD(&afe->sub_dais);
@@ -2463,6 +2458,12 @@ static int mt8196_afe_pcm_dev_probe(struct platform_device *pdev)
 		goto err_pm_put;
 	}
 
+	ret = devm_request_irq(dev, irq_id, mt8196_afe_irq_handler,
+			       IRQF_TRIGGER_NONE,
+			       "Afe_ISR_Handle", afe);
+	if (ret)
+		goto err_pm_put;
+
 	regmap_read(afe->regmap, AFE_IRQ_MCU_EN, &tmp_reg);
 	regmap_write(afe->regmap, AFE_IRQ_MCU_EN, 0xffffffff);
 	regmap_read(afe->regmap, AFE_IRQ_MCU_EN, &tmp_reg);
@@ -2490,8 +2491,11 @@ static int mt8196_afe_pcm_dev_probe(struct platform_device *pdev)
 static void mt8196_afe_pcm_dev_remove(struct platform_device *pdev)
 {
 	struct mtk_base_afe *afe = platform_get_drvdata(pdev);
+	struct mt8196_afe_private *afe_priv = afe->platform_priv;
 	struct device *dev = &pdev->dev;
 
+	devm_free_irq(dev, afe_priv->irq_id, afe);
+
 	if (!pm_runtime_status_suspended(dev))
 		mt8196_afe_runtime_suspend(dev);
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/5 ASoC: mediatek: mt8195: Keep the AFE IRQ within its resource lifetime

In reply to Myeonghun Pak

The AFE IRQ handler uses afe->regmap, but its managed IRQ is requested before the regmap is allocated. The audiosys reset earlier in probe makes an interrupt before that point unlikely, but devres still releases the regmap before the IRQ on probe failure and removal. The remove callback also suspends the AFE, which disables its clocks, while the IRQ is still requested.

Request the IRQ after initializing the regmap and before registering the component, so that a failed request takes the existing runtime PM unwind and the regmap's managed lifetime encloses the IRQ. Release the IRQ at the start of removal, before the AFE is suspended, using the IRQ number saved at probe.

Found by a static analysis tool under development.

Fixes 6746cc858259 ("ASoC: mediatek: mt8195: add platform driver")

---

 sound/soc/mediatek/mt8195/mt8195-afe-common.h |  1 +
 sound/soc/mediatek/mt8195/mt8195-afe-pcm.c    | 16 +++++++++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)
sound/soc/mediatek/mt8195/mt8195-afe-common.h +1 -0
index f93f439e2bd9..905f29673d3e 100644
@@ -123,6 +123,7 @@ struct mtkaif_param {
 struct clk;
 
 struct mt8195_afe_private {
+	int irq_id;
 	struct clk **clk;
 	struct clk_lookup **lookup;
 	struct regmap *topckgen;
sound/soc/mediatek/mt8195/mt8195-afe-pcm.c +11 -5
index 13e5bc02fac5..0cf39f66992e 100644
@@ -3133,11 +3133,7 @@ static int mt8195_afe_pcm_dev_probe(struct platform_device *pdev)
 	irq_id = platform_get_irq(pdev, 0);
 	if (irq_id < 0)
 		return irq_id;
-
-	ret = devm_request_irq(dev, irq_id, mt8195_afe_irq_handler,
-			       IRQF_TRIGGER_NONE, "asys-isr", (void *)afe);
-	if (ret)
-		return ret;
+	afe_priv->irq_id = irq_id;
 
 	/* init sub_dais */
 	INIT_LIST_HEAD(&afe->sub_dais);
@@ -3192,6 +3188,11 @@ static int mt8195_afe_pcm_dev_probe(struct platform_device *pdev)
 		goto err_pm_put;
 	}
 
+	ret = devm_request_irq(dev, irq_id, mt8195_afe_irq_handler,
+			       IRQF_TRIGGER_NONE, "asys-isr", afe);
+	if (ret)
+		goto err_pm_put;
+
 	/* register component */
 	ret = devm_snd_soc_register_component(dev, &mtk_afe_pcm_platform,
 					      afe->dai_drivers, afe->num_dai_drivers);
@@ -3223,6 +3224,11 @@ static int mt8195_afe_pcm_dev_probe(struct platform_device *pdev)
 
 static void mt8195_afe_pcm_dev_remove(struct platform_device *pdev)
 {
+	struct mtk_base_afe *afe = platform_get_drvdata(pdev);
+	struct mt8195_afe_private *afe_priv = afe->platform_priv;
+
+	devm_free_irq(&pdev->dev, afe_priv->irq_id, afe);
+
 	if (!pm_runtime_status_suspended(&pdev->dev))
 		mt8195_afe_runtime_suspend(&pdev->dev);
 }
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 5/5 ASoC: mediatek: mt8188: Keep the AFE IRQ within its resource lifetime

In reply to Myeonghun Pak

The AFE IRQ handler uses afe->regmap, but its managed IRQ is requested before the regmap is allocated. The audiosys reset earlier in probe makes an interrupt before that point unlikely, but devres still releases the regmap before the IRQ on probe failure and removal.

Request the IRQ after initializing the regmap and before registering the component, so that a failed request takes the existing runtime PM unwind and devres releases the component, the IRQ and the regmap in that order. The driver has no remove callback, so the IRQ needs no explicit release.

Found by a static analysis tool under development.

Fixes bf106bf09376 ("ASoC: mediatek: mt8188: add platform driver")

---

 sound/soc/mediatek/mt8188/mt8188-afe-pcm.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
sound/soc/mediatek/mt8188/mt8188-afe-pcm.c +5 -5
index d9c82cd8869e..2a27e5de9184 100644
@@ -3306,11 +3306,6 @@ static int mt8188_afe_pcm_dev_probe(struct platform_device *pdev)
 	if (irq_id < 0)
 		return irq_id;
 
-	ret = devm_request_irq(dev, irq_id, mt8188_afe_irq_handler,
-			       IRQF_TRIGGER_NONE, "asys-isr", (void *)afe);
-	if (ret)
-		return ret;
-
 	/* init sub_dais */
 	INIT_LIST_HEAD(&afe->sub_dais);
 
@@ -3362,6 +3357,11 @@ static int mt8188_afe_pcm_dev_probe(struct platform_device *pdev)
 		goto err_pm_put;
 	}
 
+	ret = devm_request_irq(dev, irq_id, mt8188_afe_irq_handler,
+			       IRQF_TRIGGER_NONE, "asys-isr", afe);
+	if (ret)
+		goto err_pm_put;
+
 	/* register component */
 	ret = devm_snd_soc_register_component(dev, &mtk_afe_pcm_platform,
 					      afe->dai_drivers, afe->num_dai_drivers);
Signature · 1 line
2.43.0
maintainer
#6 lore raw
In reply to Myeonghun Pak
Quoted 10 lines · Myeonghun Pak

ASoC: mediatek: fix AFE IRQ registration and removal ordering

This series fixes IRQ lifetime ordering in the MT8186, MT8188, MT8189, MT8195 and MT8196 AFE drivers.

Request each IRQ after regmap initialization so it is freed before the regmap. Where remove() shuts down the AFE, free the IRQ first. Also balance MT8186's IRQ wake setting.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.5

Thanks!

[1/5] ASoC: mediatek: mt8186: Keep the AFE IRQ within its resource lifetime

      https://git.kernel.org/broonie/sound/c/420be55fe0e2
[2/5] ASoC: mediatek: mt8189: Keep the AFE IRQ within its resource lifetime
      https://git.kernel.org/broonie/sound/c/4f4067c2d110
[3/5] ASoC: mediatek: mt8196: Keep the AFE IRQ within its resource lifetime
      https://git.kernel.org/broonie/sound/c/eb9a5be4c2c1
[4/5] ASoC: mediatek: mt8195: Keep the AFE IRQ within its resource lifetime
      https://git.kernel.org/broonie/sound/c/eac2ac34a18d
[5/5] ASoC: mediatek: mt8188: Keep the AFE IRQ within its resource lifetime
      https://git.kernel.org/broonie/sound/c/94d0e251e7d7

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark