Skip to contentSkip to conversation

ASoC: hdac_hda: Keep runtime PM state in sync across codec remove/probe

PATCH In review Peter Ujfalusi 2 participants 4 messages Oct 27 – Oct 28

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

PATCH 1/1 ASoC: hdac_hda: Keep runtime PM state in sync across codec remove/probe

After the codec has been removed, its runtime PM status is RPM_SUSPENDED while hdac_hda_codec_probe() powers the codec up via snd_hda_codec_device_new(). The pm_runtime_put()/snd_hda_power_down() calls on probe and registration then see an already suspended device, the codec suspend callback is not called and the codec, its HDA link and the codec_powered bit stay set, preventing the controller from runtime suspending.

The remove has a similar problem with the codec link accounting:
hdac_hda_codec_remove() drops the link reference taken in probe without looking at the runtime PM state of the codec. If the codec was already runtime suspended, the link reference has been dropped by the codec suspend callback and the remove drops it a second time.

Resume the codec in hdac_hda_codec_remove() before disabling runtime PM so that the link reference and the codec_powered bit are always in the same state as after the probe, and mark the device active on probe once the codec has been powered up.

Finally mark the codec device suspended at the end of the remove. Its runtime PM is disabled at this point and if only the ASoC card is unbound (e.g. by unbinding the machine driver), a following system suspend would otherwise call the runtime suspend callback on the already cleaned up codec via pm_runtime_force_suspend().

This is a followup patch to address Sashiko reports.

Fixes c3ec8ac82105 ("ASoC: hdac_hda: fix memleak on module unload")

---
Hi,

this is an incremental update for
https://lore.kernel.org/linux-sound/[email protected]/

to address Sashiko concerns and validated again on hardware.

I think this is the first time the unbind/bind was used for the HDA codec with SOF stack, so the real world impact is low, but never the less, it has been broken eversince hdac_hda is used.

Regards,
Peter

 sound/soc/codecs/hdac_hda.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
sound/soc/codecs/hdac_hda.c +12 -0
index bd4b113401a8..48eb95973b7a 100644
@@ -514,6 +514,9 @@ static int hdac_hda_codec_probe(struct snd_soc_component *component)
 		snd_hdac_display_power(hdev->bus,
 				       HDA_CODEC_IDX_CONTROLLER, false);
 
+	/* The codec is in D0, the status is RPM_SUSPENDED after a previous unbind */
+	pm_runtime_set_active(&hdev->dev);
+
 	/* match for forbid call in snd_hda_codec_device_new() */
 	pm_runtime_allow(&hdev->dev);
 
@@ -554,6 +557,12 @@ static void hdac_hda_codec_remove(struct snd_soc_component *component)
 		return;
 	}
 
+	/*
+	 * Resume the codec to have its link reference (dropped below) and
+	 * codec_powered bit in the same state as after the probe.
+	 */
+	pm_runtime_get_sync(&hdev->dev);
+
 	/* prevent runtime PM callbacks while the link and the codec are torn down */
 	pm_runtime_disable(&hdev->dev);
 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
@@ -565,6 +574,9 @@ static void hdac_hda_codec_remove(struct snd_soc_component *component)
 
 	/* Balances the disable above, cleanup_for_unbind keeps runtime PM disabled */
 	pm_runtime_enable(&hdev->dev);
+	pm_runtime_put_noidle(&hdev->dev);
+	/* The codec is cleaned up, system suspend must skip it from now on */
+	pm_runtime_set_suspended(&hdev->dev);
 }
 
 static const struct snd_soc_dapm_route hdac_hda_dapm_routes[] = {
Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 9 lines

After the codec has been removed, its runtime PM status is RPM_SUSPENDED while hdac_hda_codec_probe() powers the codec up via snd_hda_codec_device_new(). The pm_runtime_put()/snd_hda_power_down() calls on probe and registration then see an already suspended device, the codec suspend callback is not called and the codec, its HDA link and the codec_powered bit stay set, preventing the controller from runtime suspending.

sound/soc/codecs/hdac_hda.c static void hdac_hda_codec_remove(struct snd_soc_component *component) · line 564

Quoted 8 lines
sound/soc/codecs/hdac_hda.c +5 -0
@@ -554,6 +557,12 @@ static void hdac_hda_codec_remove(struct snd_soc_component *component)
 		return;
 	}
 
+	/*
+	 * Resume the codec to have its link reference (dropped below) and
+	 * codec_powered bit in the same state as after the probe.
+	 */
+	pm_runtime_get_sync(&hdev->dev);

What drops the reference that this takes? The probe shouldn't have exited with a runtime reference, AFAICT it doesn't. There's separate runtime PM managment for the CODEC in the HDA code...

sound/soc/codecs/hdac_hda.c static void hdac_hda_codec_remove(struct snd_soc_component *component) · line 579

Quoted 7 lines
sound/soc/codecs/hdac_hda.c +3 -0
@@ -565,6 +574,9 @@ static void hdac_hda_codec_remove(struct snd_soc_component *component)
 
 	/* Balances the disable above, cleanup_for_unbind keeps runtime PM disabled */
 	pm_runtime_enable(&hdev->dev);
+	pm_runtime_put_noidle(&hdev->dev);
+	/* The codec is cleaned up, system suspend must skip it from now on */
+	pm_runtime_set_suspended(&hdev->dev);

This updates the runtime PM reference tracking but doesn't affect the hardware. Is that what we want?

Signature · 1 line
Mark Brown <[email protected]>
#2 lore raw
Quoted 12 lines · Mark Brown
Quoted 12 lines

After the codec has been removed, its runtime PM status is RPM_SUSPENDED while hdac_hda_codec_probe() powers the codec up via snd_hda_codec_device_new(). The pm_runtime_put()/snd_hda_power_down() calls on probe and registration then see an already suspended device, the codec suspend callback is not called and the codec, its HDA link and the codec_powered bit stay set, preventing the controller from runtime suspending.

(quoted diff) +0 -0
@@ -554,6 +557,12 @@ static void hdac_hda_codec_remove(struct snd_soc_component *component)
 		return;
 	}
 
Quoted 7 lines

sound/soc/codecs/hdac_hda.c static void hdac_hda_codec_remove(struct snd_soc_component *component) · line 564

Quoted 5 lines
sound/soc/codecs/hdac_hda.c +5 -0
+	/*
+	 * Resume the codec to have its link reference (dropped below) and
+	 * codec_powered bit in the same state as after the probe.
+	 */
+	pm_runtime_get_sync(&hdev->dev);

What drops the reference that this takes?

it is dropped later at hunk 565, I can move it closer, after the pm_runtime_disable() for better visibility for v2?

Quoted 1 line

The probe shouldn't have exited with a runtime reference, AFAICT it doesn't.

Correct, the probe exits without a reference.

Quoted 1 line

There's separate runtime PM managment for the CODEC in the HDA code...

The get_sync() is not for holding a reference, it is to make sure that the codec is resumed before the snd_hdac_ext_bus_link_put() below. The HDA link refcount follows the codec's runtime PM state on SOF, so the put is only balanced if the codec is active at that point.

Quoted 10 lines

sound/soc/codecs/hdac_hda.c static void hdac_hda_codec_remove(struct snd_soc_component *component) · line 579

Quoted 7 lines
sound/soc/codecs/hdac_hda.c +3 -0
@@ -565,6 +574,9 @@ static void hdac_hda_codec_remove(struct snd_soc_component *component)
 
 	/* Balances the disable above, cleanup_for_unbind keeps runtime PM disabled */
 	pm_runtime_enable(&hdev->dev);
+	pm_runtime_put_noidle(&hdev->dev);
+	/* The codec is cleaned up, system suspend must skip it from now on */
+	pm_runtime_set_suspended(&hdev->dev);

This updates the runtime PM reference tracking but doesn't affect the hardware. Is that what we want?

Yes. At this point the link reference is dropped and the codec driver state is freed, there is nothing left for a suspend callback to do.

This is what the driver core does via pm_runtime_reinit() when the codec driver is unbound. It is needed for the case when only the card is unbound (machine driver unbind): the codec would stay RPM_ACTIVE with runtime PM disabled and a system suspend would call the runtime suspend callback on the freed codec state via pm_runtime_force_suspend().

Signature · 1 line
Péter
maintainer
#3 lore raw
In reply to Peter Ujfalusi
Quoted 1 line · Peter Ujfalusi

ASoC: hdac_hda: Keep runtime PM state in sync across codec remove/probe

Applied to

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

Thanks!

[1/1] ASoC: hdac_hda: Keep runtime PM state in sync across codec remove/probe

      https://git.kernel.org/broonie/sound/c/99203e8d8968

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