ASoC: core: Fix deferral of machine drivers
Subsystems (4)
Jon Hunter
maintainerPATCH 1/1 ASoC: core: Fix deferral of machine drivers
Commit daecf46ee0e5 ("ASoC: soc-core: use snd_soc_dai_link_component for platform") added a new member to the snd_soc_dai_link structure for storing a pointer for a platform link component. The pointer for this platform link component was allocated, if not already populated by the machine driver, using devm_kzalloc() such that the memory would be automatically freed on error or removal of the soundcard. However, this introduced a new problem, because if the probing of the soundcard is deferred, then although the memory allocated for the platform link component is freed, if the snd_soc_dai_link structure is declared statically by the machine driver, then the pointer in the DAI link structure will never be clearer. This means that when the soundcard is probed again, memory for the platform link component will not be allocated again because the address of the pointer was not cleared and this causes sound core to access memory that is no longer valid.
In most cases this causes the following error condition to be triggered and causes probing the soundcard to fail.
tegra-snd-sgtl5000 sound: ASoC: Both platform name/of_node are set for
sgtl5000
Unfortunately, because this platform link component is allocated before the DAI links are added to the soundcard, there is no easy way to clear this pointer on teardown if an error occurs.
The pointer for this platform link component was added for future proofing and communalising the structures for storing various data. Although a machine driver maybe used by more than one platform and so this platform data may vary from platform to platform, there is only ever a single instance for a given platform. Therefore, rather than dynamically allocate the platform link component structure, make it a static member of the snd_soc_dai_link to fix the problem.
It should be noted that if the platform_name of platform_of_node members of the snd_soc_dai_link structure are populated, these will always be used regardless of if the new platform.name or platform.of_node members are populated.
--- include/sound/simple_card_utils.h | 2 +- include/sound/soc.h | 2 +- sound/soc/generic/audio-graph-card.c | 4 +++- sound/soc/generic/simple-card-utils.c | 4 ++-- sound/soc/generic/simple-card.c | 6 ++++-- sound/soc/soc-core.c | 18 +++++++----------- 6 files changed, 18 insertions(+), 18 deletions(-)
include/sound/simple_card_utils.h +1 -1
@@ -75,7 +75,7 @@ void asoc_simple_card_clk_disable(struct asoc_simple_dai *dai); &dai_link->codec_dai_name, \ list_name, cells_name, NULL)#define asoc_simple_card_parse_platform (node, dai_link, list_name, cells_name) \ - asoc_simple_card_parse_dai(node, dai_link->platform, \ + asoc_simple_card_parse_dai(node, &dai_link->platform, \ &dai_link->platform_of_node, \ NULL, list_name, cells_name, NULL) int asoc_simple_card_parse_dai(struct device_node *node,
include/sound/soc.h +1 -1
@@ -925,7 +925,7 @@ struct snd_soc_dai_link {*/ const char *platform_name;struct device_node *platform_of_node; -struct snd_soc_dai_link_component *platform; +struct snd_soc_dai_link_component platform;int id;/* optional ID for machine driver link identification */
sound/soc/generic/audio-graph-card.c +3 -1
@@ -687,7 +687,9 @@ static int graph_probe(struct platform_device *pdev)for (i =0 ; i < li.link; i++) { dai_link[i].codecs = &dai_props[i].codecs; dai_link[i].num_codecs =1 ; - dai_link[i].platform = &dai_props[i].platform; + dai_link[i].platform.name = dai_props[i].platform.name; + dai_link[i].platform.of_node = dai_props[i].platform.of_node; + dai_link[i].platform.dai_name = dai_props[i].platform.dai_name; } priv->pa_gpio =devm_gpiod_get_optional (dev,"pa" ,GPIOD_OUT_LOW );
sound/soc/generic/simple-card-utils.c +2 -2
@@ -397,8 +397,8 @@ EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);int asoc_simple_card_canonicalize_dailink (struct snd_soc_dai_link *dai_link) {/* Assumes platform == cpu */ -if (!dai_link->platform->of_node) - dai_link->platform->of_node = dai_link->cpu_of_node; +if (!dai_link->platform.of_node) + dai_link->platform.of_node = dai_link->cpu_of_node;return 0 ;
sound/soc/generic/simple-card.c +4 -2
@@ -732,7 +732,9 @@ static int simple_probe(struct platform_device *pdev)for (i =0 ; i < li.link; i++) { dai_link[i].codecs = &dai_props[i].codecs; dai_link[i].num_codecs =1 ; - dai_link[i].platform = &dai_props[i].platform; + dai_link[i].platform.name = dai_props[i].platform.name; + dai_link[i].platform.of_node = dai_props[i].platform.of_node; + dai_link[i].platform.dai_name = dai_props[i].platform.dai_name; } priv->dai_props = dai_props;
@@ -782,7 +784,7 @@ static int simple_probe(struct platform_device *pdev) codecs->name = cinfo->codec; codecs->dai_name = cinfo->codec_dai.name; - platform = dai_link->platform; + platform = &dai_link->platform; platform->name = cinfo->platform; card->name = (cinfo->card) ? cinfo->card : cinfo->name;
sound/soc/soc-core.c +7 -11
@@ -915,7 +915,7 @@ static int soc_bind_dai_link(struct snd_soc_card *card,/* find one from the set of registered platforms */ for_each_component (component) { -if (!snd_soc_is_matching_component (dai_link->platform, +if (!snd_soc_is_matching_component (&dai_link->platform, component))continue ;
@@ -1026,7 +1026,7 @@ static void soc_remove_dai_links(struct snd_soc_card *card)static int snd_soc_init_platform (struct snd_soc_card *card,struct snd_soc_dai_link *dai_link) { -struct snd_soc_dai_link_component *platform = dai_link->platform; +struct snd_soc_dai_link_component *platform = &dai_link->platform;/* * FIXME
@@ -1034,14 +1034,10 @@ static int snd_soc_init_platform(struct snd_soc_card *card,* this function should be removed in the future */ /* convert Legacy platform link */ -if (!platform) { - platform =devm_kzalloc (card->dev, -sizeof (struct snd_soc_dai_link_component ), -GFP_KERNEL ); -if (!platform) -return -ENOMEM ; +if (dai_link->platform_name || dai_link->platform_of_node) { +dev_dbg (card->dev, +"ASoC: Defaulting to legacy platform data!\n" ); - dai_link->platform = platform; platform->name = dai_link->platform_name; platform->of_node = dai_link->platform_of_node; platform->dai_name =NULL ;
@@ -1123,7 +1119,7 @@ static int soc_init_dai_link(struct snd_soc_card *card,* Platform may be specified by either name or OF node, but * can be left unspecified, and a dummy platform will be used. */ -if (link->platform->name && link->platform->of_node) { +if (link->platform.name && link->platform.of_node) {dev_err (card->dev,"ASoC: Both platform name/of_node are set for %s\n" , link->name);
@@ -1921,7 +1917,7 @@ static void soc_check_tplg_fes(struct snd_soc_card *card)dev_err (card->dev,"init platform error" );continue ; } - dai_link->platform->name = component->name; + dai_link->platform.name = component->name;/* convert non BE into BE */ dai_link->no_pcm =1 ;
Signature · 1 line
2.7.4
Kuninori Morimoto
maintainerHi Jon
Quoted 38 lines
Commit
daecf46ee0e5("ASoC: soc-core: usesnd_soc_dai_link_componentfor platform") added a new member to thesnd_soc_dai_linkstructure for storing a pointer for a platform link component. The pointer for this platform link component was allocated, if not already populated by the machine driver, usingdevm_kzalloc()such that the memory would be automatically freed on error or removal of the soundcard. However, this introduced a new problem, because if the probing of the soundcard is deferred, then although the memory allocated for the platform link component is freed, if thesnd_soc_dai_linkstructure is declared statically by the machine driver, then the pointer in the DAI link structure will never be clearer. This means that when the soundcard is probed again, memory for the platform link component will not be allocated again because the address of the pointer was not cleared and this causes sound core to access memory that is no longer valid.In most cases this causes the following error condition to be triggered and causes probing the soundcard to fail.
tegra-snd-sgtl5000 sound: ASoC: Both platform name/of_node are set for
sgtl5000Unfortunately, because this platform link component is allocated before the DAI links are added to the soundcard, there is no easy way to clear this pointer on teardown if an error occurs.
The pointer for this platform link component was added for future proofing and communalising the structures for storing various data. Although a machine driver maybe used by more than one platform and so this platform data may vary from platform to platform, there is only ever a single instance for a given platform. Therefore, rather than dynamically allocate the platform link component structure, make it a static member of the
snd_soc_dai_linkto fix the problem.It should be noted that if the
platform_nameofplatform_of_nodemembers of thesnd_soc_dai_linkstructure are populated, these will always be used regardless of if the new platform.name or platform.of_node members are populated.
Quoted 5 lines
Fixes daecf46ee0e5 ("ASoC: soc-core: use snd_soc_dai_link_component for platform")Reported-by Marcel Ziswiler <[email protected]>Signed-off-by Jon Hunter <[email protected]>---
Thank you for your patch.
The reason why it is allocating memory is it is for glue Legacy vs Modern style. This means, this allocation style will be removed if ALSA SoC become modern style. The missing part so far is CPU.
Only CPU is not yet supporting snd_soc_dai_link_component style.
If all CPU/Codec/Platform supports snd_soc_dai_link_component style, all driver can switch to it, and then, all will be static style. Currently, simple card series is only(?) using this style.
The reason why platform is using pointer style is that someone (not me ;P) will support multi platform style in the future.
Best regards
---
Kuninori Morimoto
Jon Hunter
maintainerQuoted 56 lines · Kuninori Morimoto
Hi Jon
Quoted 38 lines
Commit
daecf46ee0e5("ASoC: soc-core: usesnd_soc_dai_link_componentfor platform") added a new member to thesnd_soc_dai_linkstructure for storing a pointer for a platform link component. The pointer for this platform link component was allocated, if not already populated by the machine driver, usingdevm_kzalloc()such that the memory would be automatically freed on error or removal of the soundcard. However, this introduced a new problem, because if the probing of the soundcard is deferred, then although the memory allocated for the platform link component is freed, if thesnd_soc_dai_linkstructure is declared statically by the machine driver, then the pointer in the DAI link structure will never be clearer. This means that when the soundcard is probed again, memory for the platform link component will not be allocated again because the address of the pointer was not cleared and this causes sound core to access memory that is no longer valid.In most cases this causes the following error condition to be triggered and causes probing the soundcard to fail.
tegra-snd-sgtl5000 sound: ASoC: Both platform name/of_node are set for
sgtl5000Unfortunately, because this platform link component is allocated before the DAI links are added to the soundcard, there is no easy way to clear this pointer on teardown if an error occurs.
The pointer for this platform link component was added for future proofing and communalising the structures for storing various data. Although a machine driver maybe used by more than one platform and so this platform data may vary from platform to platform, there is only ever a single instance for a given platform. Therefore, rather than dynamically allocate the platform link component structure, make it a static member of the
snd_soc_dai_linkto fix the problem.It should be noted that if the
platform_nameofplatform_of_nodemembers of thesnd_soc_dai_linkstructure are populated, these will always be used regardless of if the new platform.name or platform.of_node members are populated.Quoted 5 lines
Fixes daecf46ee0e5 ("ASoC: soc-core: use snd_soc_dai_link_component for platform")Reported-by Marcel Ziswiler <[email protected]>Signed-off-by Jon Hunter <[email protected]>---
Thank you for your patch.
The reason why it is allocating memory is it is for glue Legacy vs Modern style. This means, this allocation style will be removed if ALSA SoC become modern style. The missing part so far is CPU.
Only CPU is not yet supportingsnd_soc_dai_link_componentstyle.
If all CPU/Codec/Platform supportssnd_soc_dai_link_componentstyle, all driver can switch to it, and then, all will be static style. Currently, simple card series is only(?) using this style.
Quoted 2 lines
The reason why platform is using pointer style is that someone (not me ;P) will support multi platform style in the future.
Can you elaborate a bit more, I still not not understand what you mean here by 'support multi-platform style' and why this is needed in the future?
Furthermore, even if the CPU is not supporting the snd_soc_dai_link_component, I don't understand why this prevents us from making this change now other than it is not consistent.
Jon
Signature · 1 line
nvpublic
Mark Brown
maintainerinclude/sound/soc.h struct snd_soc_dai_link { · line 931
Quoted 11 lines · Jon Hunter
include/sound/soc.h +1
-1@@ -925,7 +925,7 @@ struct snd_soc_dai_link {*/ const char *platform_name;struct device_node *platform_of_node; -struct snd_soc_dai_link_component *platform; +struct snd_soc_dai_link_component platform;int id;/* optional ID for machine driver link identification */
This breaks the build for the SCU cards (and we needs a little rebase against another fix I just merged, though I did do that when applying). I do think that this is going to be the safest thing to do for v5.0, it can always be reverted later on when it's not needed but it seems clear that a better fix is going to be way too invasive for the -rcs. Can you respin and retest please?
Jon Hunter
maintainerQuoted 11 lines · Mark Brown
Quoted 9 lines · Jon Hunter
include/sound/soc.h +1
-1@@ -925,7 +925,7 @@ struct snd_soc_dai_link {*/ const char *platform_name;struct device_node *platform_of_node; -struct snd_soc_dai_link_component *platform; +struct snd_soc_dai_link_component platform;
Quoted 5 lines
include/sound/soc.h struct snd_soc_dai_link { · line 931
Quoted 2 lines
include/sound/soc.h +0
-0int id;/* optional ID for machine driver link identification */ This breaks the build for the SCU cards (and we needs a little rebase against another fix I just merged, though I did do that when applying).
Sorry I still don't see the build break, can you point me to it?
Quoted 4 lines
I do think that this is going to be the safest thing to do for v5.0, it can always be reverted later on when it's not needed but it seems clear that a better fix is going to be way too invasive for the -rcs. Can you respin and retest please?
Yes will do. I do wonder if we should be concerned about snd_soc_init_multicodec() as well? Looks like it could have a different problem if a machine driver already allocated the memory for the codec link component.
Cheers
Jon
Signature · 1 line
nvpublic
Mark Brown
maintainerQuoted 10 lines · Jon Hunter
Quoted 7 lines · Mark Brown
include/sound/soc.h struct snd_soc_dai_link { · line 928
Quoted 2 lines · Jon Hunter
include/sound/soc.h +1
-1-struct snd_soc_dai_link_component *platform; +struct snd_soc_dai_link_component platform;This breaks the build for the SCU cards (and we needs a little rebase against another fix I just merged, though I did do that when applying).
Sorry I still don't see the build break, can you point me to it?
I'd need to find your patch again and fix the rebase issue. It was assigning a pointer to a platform IIRC.
Quoted 9 lines
Quoted 4 lines
I do think that this is going to be the safest thing to do for v5.0, it can always be reverted later on when it's not needed but it seems clear that a better fix is going to be way too invasive for the -rcs. Can you respin and retest please?
Yes will do. I do wonder if we should be concerned about
snd_soc_init_multicodec()as well? Looks like it could have a different problem if a machine driver already allocated the memory for the codec link component.
Since you appear to be volunteering to check... :)
Jon Hunter
maintainerQuoted 10 lines · Mark Brown
Quoted 8 lines · Jon Hunter
Quoted 7 lines · Mark Brown
include/sound/soc.h struct snd_soc_dai_link { · line 928
Quoted 2 lines · Jon Hunter
include/sound/soc.h +1
-1-struct snd_soc_dai_link_component *platform; +struct snd_soc_dai_link_component platform;This breaks the build for the SCU cards (and we needs a little rebase against another fix I just merged, though I did do that when applying).
Quoted 4 lines
Quoted 1 line
Sorry I still don't see the build break, can you point me to it?
I'd need to find your patch again and fix the rebase issue. It was assigning a pointer to a platform IIRC.
Sorry I still don't see it. Happen to recall the config you were using to build? I tried enabling the various SH soundcards (which I believe the SCU card is under).
Jon
Signature · 1 line
nvpublic
Mark Brown
maintainerQuoted 8 lines · Jon Hunter
Quoted 2 lines · Mark Brown
I'd need to find your patch again and fix the rebase issue. It was assigning a pointer to a platform IIRC.
Sorry I still don't see it. Happen to recall the config you were using to build? I tried enabling the various SH soundcards (which I believe the SCU card is under).
Configure all SND, MFD, REGULATOR and SPI symbols y on top of a base config which is:
CONFIG_COMPILE_TEST=yCONFIG_OF=yCONFIG_I2C=yCONFIG_SPI=yCONFIG_REGULATOR=yCONFIG_SOUND=yCONFIG_SND=yCONFIG_SND_SOC=yCONFIG_SND_SOC_ALL_CODECS=yCONFIG_SPMI=yCONFIG_DEBUG_FS=y
(I should add ACPI to that too now I think about it) merged with defconfig. I was reproducing on x86.