ASoC: soc-core: Create device_link to ensure correct suspend order
PATCH 1/1 ASoC: soc-core: Create device_link to ensure correct suspend order
In snd_soc_bind_card() create a device_link from card to all components to ensure correct order of system_suspend. The card is the consumer and the components are the supplier, so that the card will system_suspend before any of the components.
The PM core will normally system_suspend drivers in the opposite order that they registered. This ensures children are suspended before their parents, for example users of a bus driver should suspend before the bus driver suspends.
For ASoC, snd_soc_suspend() shuts down any active audio, which requires that the components are still able to communicate with their hardware. Previously there was nothing to ensure this ordering, because there is (usually) no relationship between a machine driver and component drivers. If the machine driver registered before the codec drivers, the codec drivers would be suspended before the machine driver snd_soc_suspend() runs, so that ASoC is attempting to stop audio on a driver that has already suspended.
---
Mark,
I think it would be ok to put this into -next and let it get some wider testing before it goes into 7.1 or older.
sound/soc/soc-core.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
sound/soc/soc-core.c +15 -0
@@ -2297,6 +2297,21 @@ static int snd_soc_bind_card(struct snd_soc_card *card)if (!snd_soc_component_active (component))pinctrl_pm_select_sleep_state (component->dev); +/* +* Add device_link from card to component so that system_suspend +* will be done in the correct order. The card must suspend first +* to stop audio activity before the components suspend. +*/ +for_each_card_components (card, component) { +if (!device_link_add (card->dev, component->dev, +DL_FLAG_AUTOREMOVE_CONSUMER )) { +dev_warn (card->dev,"Failed to create device link to %s\n" , +dev_name (component->dev)); + ret = -EINVAL ; + goto probe_end; + } + } + probe_end:if (ret <0 )soc_cleanup_card_resources (card);
Signature · 1 line
2.47.3
Quoted 19 lines · Richard Fitzgerald
In
snd_soc_bind_card()create adevice_linkfrom card to all components to ensure correct order ofsystem_suspend. The card is the consumer and the components are the supplier, so that the card willsystem_suspendbefore any of the components.The PM core will normally
system_suspenddrivers in the opposite order that they registered. This ensures children are suspended before their parents, for example users of a bus driver should suspend before the bus driver suspends.For ASoC,
snd_soc_suspend()shuts down any active audio, which requires that the components are still able to communicate with their hardware. Previously there was nothing to ensure this ordering, because there is (usually) no relationship between a machine driver and component drivers. If the machine driver registered before the codec drivers, the codec drivers would be suspended before the machine driversnd_soc_suspend()runs, so that ASoC is attempting to stop audio on a driver that has already suspended.
Quoted 2 lines
Signed-off-by Richard Fitzgerald <[email protected]>---
Thanks,
Charles
Mark Brown
maintainersound/soc/soc-core.c static int snd_soc_bind_card(struct snd_soc_card *card) · line 2311
Quoted 7 lines · Richard Fitzgerald
sound/soc/soc-core.c +7
-0+for_each_card_components (card, component) { +if (!device_link_add (card->dev, component->dev, +DL_FLAG_AUTOREMOVE_CONSUMER )) { +dev_warn (card->dev,"Failed to create device link to %s\n" , +dev_name (component->dev)); + ret = -EINVAL ; + goto probe_end;
Do we still have devices registering separate components with the same device for DMA and DAI operations? They'd end up duplicating the links here.
Quoted 13 lines · Mark Brown
sound/soc/soc-core.c static int snd_soc_bind_card(struct snd_soc_card *card) · line 2311
Quoted 7 lines · Richard Fitzgerald
sound/soc/soc-core.c +7
-0+for_each_card_components (card, component) { +if (!device_link_add (card->dev, component->dev, +DL_FLAG_AUTOREMOVE_CONSUMER )) { +dev_warn (card->dev,"Failed to create device link to %s\n" , +dev_name (component->dev)); + ret = -EINVAL ; + goto probe_end;Do we still have devices registering separate components with the same device for DMA and DAI operations? They'd end up duplicating the links here.
Ok, weird, but I agree it's possible for one driver to register multiple components. I'll make a V2 that skips the device link if the component dev is the same as the card dev.
Mark Brown
maintainerQuoted 9 lines · Richard Fitzgerald
Quoted 3 lines · Mark Brown
Do we still have devices registering separate components with the same device for DMA and DAI operations? They'd end up duplicating the links here.
Ok, weird, but I agree it's possible for one driver to register multiple components. I'll make a V2 that skips the device link if the component dev is the same as the card dev.
That wouldn't avoid the issue I think? The card is a separate device, but the DMA and DAI are two devices registered separately. This used to be because we had completely separate driver types for DMA and DAI, prior to the component refactoring. A simple move to component would keep two separate components, this could be refactored into a single component but that'd need to actually happen and I'm not sure we did that for everything. I was wondering if you'd checked already, sounds like no?
Quoted 11 lines · Mark Brown
Quoted 9 lines · Richard Fitzgerald
Quoted 3 lines · Mark Brown
Do we still have devices registering separate components with the same device for DMA and DAI operations? They'd end up duplicating the links here.
Ok, weird, but I agree it's possible for one driver to register multiple components. I'll make a V2 that skips the device link if the component dev is the same as the card dev.
Quoted 8 lines
That wouldn't avoid the issue I think? The card is a separate device, but the DMA and DAI are two devices registered separately. This used to be because we had completely separate driver types for DMA and DAI, prior to the component refactoring. A simple move to component would keep two separate components, this could be refactored into a single component but that'd need to actually happen and I'm not sure we did that for everything. I was wondering if you'd checked already, sounds like no?
Oh, you mean two components with the same dev, so that the device_link_add() gets called for the same (card,component) devices multiple times?
I tested multiple calls for the same device pair, and it's ok. This also covers the case where ASoC core now creates this device_link and the codec driver also creates the same device_link between the card and itself (hdac_hdmi does this).
From the kernel doc for device_link_add():
"if a device link between the given @consumer and @supplier pair
exists already when this function is called for them, the existing link will be returned"
Mark Brown
maintainerQuoted 9 lines · Richard Fitzgerald
Oh, you mean two components with the same dev, so that the
device_link_add()gets called for the same (card,component) devices multiple times?I tested multiple calls for the same device pair, and it's ok. This also covers the case where ASoC core now creates this
device_linkand the codec driver also creates the samedevice_linkbetween the card and itself (hdac_hdmidoes this).
Quoted 5 lines
From the kernel doc for
device_link_add():"if a device link between the given @consumer and @supplier pair
exists already when this function is called for them, the existing link will be returned"
Ah, good - I'd remembered there was an issue with duplicates there. I guess it's worth adding the defensiveness for card and component being the same in any case, just to be on the safe side. There were some SoCs (sunxi I think?) with integrated DACs that I can see doing something like that, can't remember if they actually do or not.
Quoted 19 lines · Richard Fitzgerald
In
snd_soc_bind_card()create adevice_linkfrom card to all components to ensure correct order ofsystem_suspend. The card is the consumer and the components are the supplier, so that the card willsystem_suspendbefore any of the components.The PM core will normally
system_suspenddrivers in the opposite order that they registered. This ensures children are suspended before their parents, for example users of a bus driver should suspend before the bus driver suspends.For ASoC,
snd_soc_suspend()shuts down any active audio, which requires that the components are still able to communicate with their hardware. Previously there was nothing to ensure this ordering, because there is (usually) no relationship between a machine driver and component drivers. If the machine driver registered before the codec drivers, the codec drivers would be suspended before the machine driversnd_soc_suspend()runs, so that ASoC is attempting to stop audio on a driver that has already suspended.
Quoted 5 lines
Signed-off-by Richard Fitzgerald <[email protected]>---
Mark,
I think it would be ok to put this into -next and let it get some wider testing before it goes into 7.1 or older.
I was looking at the Sashiko review for this patch. If Sashiko can be trusted, device_links seems to be thoroughly booby-trapped.
Short summary seems to be: only safe way to use them is with the STATELESS flag and do manual cleanup.
Mark Brown
maintainerQuoted 11 lines · Richard Fitzgerald
Quoted 2 lines · Richard Fitzgerald
I think it would be ok to put this into -next and let it get some wider testing before it goes into 7.1 or older.
I was looking at the Sashiko review for this patch. If Sashiko can be trusted,
device_linksseems to be thoroughly booby-trapped.Short summary seems to be: only safe way to use them is with the STATELESS flag and do manual cleanup.
Yeah, I saw that but didn't put the time in to confirming how reliable the specific analysis is. The booby trapped bit seems about right though.