ASoC: topology: Fix not to keep a reference to tplg fw
PATCH 1/1 ASoC: topology: Fix not to keep a reference to tplg fw
During element creation, the name of some of the elements point to memory referenced in tplg fw. If the tplg fw is released after tplg is parsed by framework, kernel panic happens during creation of elements while card initialization.
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
So create a copy of the memory and assign to names instead.
--- sound/soc/soc-topology.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
sound/soc/soc-topology.c +8 -8
@@ -709,7 +709,7 @@ static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count, be->hdr.name, be->hdr.access);memset (&kc,0 ,sizeof (kc)); - kc.name = be->hdr.name; + kc.name =kstrdup (be->hdr.name,GFP_KERNEL ); kc.private_value = (long )sbe; kc.iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc.access = be->hdr.access;
@@ -789,7 +789,7 @@ static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count, mc->hdr.name, mc->hdr.access);memset (&kc,0 ,sizeof (kc)); - kc.name = mc->hdr.name; + kc.name =kstrdup (mc->hdr.name,GFP_KERNEL ); kc.private_value = (long )sm; kc.iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc.access = mc->hdr.access;
@@ -935,7 +935,7 @@ static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count, ec->hdr.name, ec->items);memset (&kc,0 ,sizeof (kc)); - kc.name = ec->hdr.name; + kc.name =kstrdup (ec->hdr.name,GFP_KERNEL ); kc.private_value = (long )se; kc.iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc.access = ec->hdr.access;
@@ -1105,8 +1105,8 @@ static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, SNDRV_CTL_ELEM_ID_NAME_MAXLEN)return -EINVAL ; - route.source = elem->source; - route.sink = elem->sink; + route.source =kstrdup (elem->source,GFP_KERNEL ); + route.sink =kstrdup (elem->sink,GFP_KERNEL ); route.connected =NULL ;/* set to NULL atm for tplg users */ if (strnlen (elem->control,SNDRV_CTL_ELEM_ID_NAME_MAXLEN ) ==0 ) route.control =NULL ;
@@ -1149,7 +1149,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(dev_dbg (tplg->dev," adding DAPM widget mixer control %s at %d\n" , mc->hdr.name, i); - kc[i].name = mc->hdr.name; + kc[i].name =kstrdup (mc->hdr.name,GFP_KERNEL ); kc[i].private_value = (long )sm; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = mc->hdr.access;
@@ -1228,7 +1228,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(dev_dbg (tplg->dev," adding DAPM widget enum control %s\n" , ec->hdr.name); - kc->name = ec->hdr.name; + kc->name =kstrdup (ec->hdr.name,GFP_KERNEL ); kc->private_value = (long )se; kc->iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc->access = ec->hdr.access;
@@ -1330,7 +1330,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create("ASoC: adding bytes kcontrol %s with access 0x%x\n" , be->hdr.name, be->hdr.access); - kc[i].name = be->hdr.name; + kc[i].name =kstrdup (be->hdr.name,GFP_KERNEL ); kc[i].private_value = (long )sbe; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = be->hdr.access;
Signature · 1 line
1.9.1
Quoted 5 lines · Subhransu S. Prusty
During element creation, the name of some of the elements point to memory referenced in tplg fw. If the tplg fw is released after tplg is parsed by framework, kernel panic happens during creation of elements while card initialization.
In which code path? When the kctl is already instantiated from snd_kcontrol_new template, we don't have to duplicate the string. The only case where the strdup() is required is to delay the instantiation, i.e. storing the kcontrol_new object itself instead of referring temporarily.
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
Takashi
Quoted 83 lines
Signed-off-by Subhransu S. Prusty <[email protected]>Signed-off-by Vinod Koul <[email protected]>--- sound/soc/soc-topology.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)sound/soc/soc-topology.c +8
-8@@ -709,7 +709,7 @@ static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count, be->hdr.name, be->hdr.access);memset (&kc,0 ,sizeof (kc)); - kc.name = be->hdr.name; + kc.name =kstrdup (be->hdr.name,GFP_KERNEL ); kc.private_value = (long )sbe; kc.iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc.access = be->hdr.access;@@ -789,7 +789,7 @@ static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count, mc->hdr.name, mc->hdr.access);memset (&kc,0 ,sizeof (kc)); - kc.name = mc->hdr.name; + kc.name =kstrdup (mc->hdr.name,GFP_KERNEL ); kc.private_value = (long )sm; kc.iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc.access = mc->hdr.access;@@ -935,7 +935,7 @@ static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count, ec->hdr.name, ec->items);memset (&kc,0 ,sizeof (kc)); - kc.name = ec->hdr.name; + kc.name =kstrdup (ec->hdr.name,GFP_KERNEL ); kc.private_value = (long )se; kc.iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc.access = ec->hdr.access;@@ -1105,8 +1105,8 @@ static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, SNDRV_CTL_ELEM_ID_NAME_MAXLEN)return -EINVAL ; - route.source = elem->source; - route.sink = elem->sink; + route.source =kstrdup (elem->source,GFP_KERNEL ); + route.sink =kstrdup (elem->sink,GFP_KERNEL ); route.connected =NULL ;/* set to NULL atm for tplg users */ if (strnlen (elem->control,SNDRV_CTL_ELEM_ID_NAME_MAXLEN ) ==0 ) route.control =NULL ;@@ -1149,7 +1149,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(dev_dbg (tplg->dev," adding DAPM widget mixer control %s at %d\n" , mc->hdr.name, i); - kc[i].name = mc->hdr.name; + kc[i].name =kstrdup (mc->hdr.name,GFP_KERNEL ); kc[i].private_value = (long )sm; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = mc->hdr.access;@@ -1228,7 +1228,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(dev_dbg (tplg->dev," adding DAPM widget enum control %s\n" , ec->hdr.name); - kc->name = ec->hdr.name; + kc->name =kstrdup (ec->hdr.name,GFP_KERNEL ); kc->private_value = (long )se; kc->iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc->access = ec->hdr.access;@@ -1330,7 +1330,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create("ASoC: adding bytes kcontrol %s with access 0x%x\n" , be->hdr.name, be->hdr.access); - kc[i].name = be->hdr.name; + kc[i].name =kstrdup (be->hdr.name,GFP_KERNEL ); kc[i].private_value = (long )sbe; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = be->hdr.access;Signature · 6 lines
1.9.1 _______________________________________________ Alsa-devel mailing list [email protected] http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Quoted 8 lines · Takashi Iwai
Quoted 5 lines · Subhransu S. Prusty
During element creation, the name of some of the elements point to memory referenced in tplg fw. If the tplg fw is released after tplg is parsed by framework, kernel panic happens during creation of elements while card initialization.
Quoted 5 lines
In which code path? When the kctl is already instantiated from
snd_kcontrol_newtemplate, we don't have to duplicate the string. The only case where thestrdup()is required is to delay the instantiation, i.e. storing thekcontrol_newobject itself instead of referring temporarily.
So in SKL, we do request firmware of topology binary and topology core uses that for strings here, so the patch 87b5ed8ec freed the topology binary which causes panic while accessing kcontrols.
Your second point is applicable here as card instantiation is delayed often for us as all components may not be present and delayed probe finally creates the card.
Quoted 3 lines
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Yes we will add that
Quoted 4 lines
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
That is a good point and I think we should do devm_kstrdup() here so that this is freed when we cleanup the device, or do you have any better suggestion ?
Signature · 1 line
~Vinod
Quoted 16 lines · Vinod Koul
Quoted 8 lines · Takashi Iwai
Quoted 5 lines · Subhransu S. Prusty
During element creation, the name of some of the elements point to memory referenced in tplg fw. If the tplg fw is released after tplg is parsed by framework, kernel panic happens during creation of elements while card initialization.
Quoted 5 lines
In which code path? When the kctl is already instantiated from
snd_kcontrol_newtemplate, we don't have to duplicate the string. The only case where thestrdup()is required is to delay the instantiation, i.e. storing thekcontrol_newobject itself instead of referring temporarily.
Quoted 3 lines
So in SKL, we do request firmware of topology binary and topology core uses that for strings here, so the patch
87b5ed8ecfreed the topology binary which causes panic while accessing kcontrols.
This is strange. If it's about the kctl name string, the panic shouldn't happen at accessing the kctl but at instantiating the kctl from snd_kcontrol_new that contains the invalid string pointer. The kctl object contains the string in itself, and there copies the string from the template.
Also I wonder why it kernel panics, not the normal Oops.
Quoted 10 lines
Your second point is applicable here as card instantiation is delayed often for us as all components may not be present and delayed probe finally creates the card.
Quoted 3 lines
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Yes we will add that
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
That is a good point and I think we should do
devm_kstrdup()here so that this is freed when we cleanup the device, or do you have any better suggestion ?
devm_kstrdup() is bad in this case. You can reload the topology unlimitedly, and the memory won't be freed until the device unbind, thus it keeps hogging.
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole snd_kcontrol_new template and the allocated string is no use, so they can be freed.
Takashi
Mark Brown
maintainerQuoted 11 lines · Takashi Iwai
Quoted 3 lines · Vinod Koul
So in SKL, we do request firmware of topology binary and topology core uses that for strings here, so the patch
87b5ed8ecfreed the topology binary which causes panic while accessing kcontrols.This is strange. If it's about the kctl name string, the panic shouldn't happen at accessing the kctl but at instantiating the kctl from
snd_kcontrol_newthat contains the invalid string pointer. The kctl object contains the string in itself, and there copies the string from the template.
I guess it's possible that if the control creation happens soon enough after the memory is freed the data will still be valid. This could be tested for by hacking things to deliberately trash the memory before we get to control creation.
Quoted 4 lines
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.
That does suggest a fairly simple fix of just holding on to the firmware for longer assuming that the analysis is correct.
Quoted 19 lines · Mark Brown
Quoted 11 lines · Takashi Iwai
Quoted 3 lines · Vinod Koul
So in SKL, we do request firmware of topology binary and topology core uses that for strings here, so the patch
87b5ed8ecfreed the topology binary which causes panic while accessing kcontrols.This is strange. If it's about the kctl name string, the panic shouldn't happen at accessing the kctl but at instantiating the kctl from
snd_kcontrol_newthat contains the invalid string pointer. The kctl object contains the string in itself, and there copies the string from the template.I guess it's possible that if the control creation happens soon enough after the memory is freed the data will still be valid. This could be tested for by hacking things to deliberately trash the memory before we get to control creation.
Quoted 7 lines
Quoted 4 lines
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.That does suggest a fairly simple fix of just holding on to the firmware for longer assuming that the analysis is correct.
Right, that would be the simplest fix. Just assure that the whole f/w image is kept until all objects are instantiated.
Takashi
Quoted 26 lines · Takashi Iwai
Quoted 24 lines · Mark Brown
Quoted 11 lines · Takashi Iwai
Quoted 3 lines · Vinod Koul
So in SKL, we do request firmware of topology binary and topology core uses that for strings here, so the patch
87b5ed8ecfreed the topology binary which causes panic while accessing kcontrols.This is strange. If it's about the kctl name string, the panic shouldn't happen at accessing the kctl but at instantiating the kctl from
snd_kcontrol_newthat contains the invalid string pointer. The kctl object contains the string in itself, and there copies the string from the template.I guess it's possible that if the control creation happens soon enough after the memory is freed the data will still be valid. This could be tested for by hacking things to deliberately trash the memory before we get to control creation.
Quoted 4 lines
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.
Quoted 5 lines
Quoted 2 lines
That does suggest a fairly simple fix of just holding on to the firmware for longer assuming that the analysis is correct.
Right, that would be the simplest fix. Just assure that the whole f/w image is kept until all objects are instantiated.
Yes, going by the discussion here, we can then free the topology firmware later, but then question is how do we know when is the card completely instantiated and we can free the topology binary... I do not know how..
Only thing I can think of is to free this is driver .remove()
So then we can simply revert 87b5ed8ecb : ('ASoC: Intel: Skylake: fix memory leak) and then add a new one..
Thanks
Signature · 1 line
~Vinod
Quoted 22 lines · Takashi Iwai
Quoted 16 lines · Vinod Koul
Quoted 8 lines · Takashi Iwai
Quoted 5 lines · Subhransu S. Prusty
During element creation, the name of some of the elements point to memory referenced in tplg fw. If the tplg fw is released after tplg is parsed by framework, kernel panic happens during creation of elements while card initialization.
Quoted 5 lines
In which code path? When the kctl is already instantiated from
snd_kcontrol_newtemplate, we don't have to duplicate the string. The only case where thestrdup()is required is to delay the instantiation, i.e. storing thekcontrol_newobject itself instead of referring temporarily.Quoted 3 lines
So in SKL, we do request firmware of topology binary and topology core uses that for strings here, so the patch
87b5ed8ecfreed the topology binary which causes panic while accessing kcontrols.
Quoted 7 lines
This is strange. If it's about the kctl name string, the panic shouldn't happen at accessing the kctl but at instantiating the kctl from
snd_kcontrol_newthat contains the invalid string pointer. The kctl object contains the string in itself, and there copies the string from the template.Also I wonder why it kernel panics, not the normal Oops.
Sorry it a oops, paging request failure and not a panic
Quoted 19 lines
Quoted 10 lines
Your second point is applicable here as card instantiation is delayed often for us as all components may not be present and delayed probe finally creates the card.
Quoted 3 lines
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Yes we will add that
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
That is a good point and I think we should do
devm_kstrdup()here so that this is freed when we cleanup the device, or do you have any better suggestion ?
Quoted 8 lines
devm_kstrdup()is bad in this case. You can reload the topology unlimitedly, and the memory won't be freed until the device unbind, thus it keeps hogging.You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.
but then question of where should these be freed. For current drivers they declare controls statically, so memory is always there.. How do free up in the cases where we allocate dynamically?
Signature · 1 line
~Vinod
Quoted 57 lines · Vinod Koul
Quoted 22 lines · Takashi Iwai
Quoted 16 lines · Vinod Koul
Quoted 8 lines · Takashi Iwai
Quoted 5 lines · Subhransu S. Prusty
During element creation, the name of some of the elements point to memory referenced in tplg fw. If the tplg fw is released after tplg is parsed by framework, kernel panic happens during creation of elements while card initialization.
Quoted 5 lines
In which code path? When the kctl is already instantiated from
snd_kcontrol_newtemplate, we don't have to duplicate the string. The only case where thestrdup()is required is to delay the instantiation, i.e. storing thekcontrol_newobject itself instead of referring temporarily.Quoted 3 lines
So in SKL, we do request firmware of topology binary and topology core uses that for strings here, so the patch
87b5ed8ecfreed the topology binary which causes panic while accessing kcontrols.Quoted 7 lines
This is strange. If it's about the kctl name string, the panic shouldn't happen at accessing the kctl but at instantiating the kctl from
snd_kcontrol_newthat contains the invalid string pointer. The kctl object contains the string in itself, and there copies the string from the template.Also I wonder why it kernel panics, not the normal Oops.
Sorry it a oops, paging request failure and not a panic
Quoted 23 lines
Quoted 10 lines
Your second point is applicable here as card instantiation is delayed often for us as all components may not be present and delayed probe finally creates the card.
Quoted 3 lines
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Yes we will add that
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
That is a good point and I think we should do
devm_kstrdup()here so that this is freed when we cleanup the device, or do you have any better suggestion ?
devm_kstrdup()is bad in this case. You can reload the topology unlimitedly, and the memory won't be freed until the device unbind, thus it keeps hogging.
Quoted 8 lines
Quoted 4 lines
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.but then question of where should these be freed. For current drivers they declare controls statically, so memory is always there.. How do free up in the cases where we allocate dynamically?
Well, for judging this, we have to follow the code more closely. And it's why I asked which path does it happen exactly.
There are two different paths where the snd_kcontrol_new is used: the standard controls and dapm. The former is immediately instantiated via snd_soc_cnew(), so it's fine as is, no need to change. But the latter is different.
The latter, dapm case, always allocates the snd_kcontrol_new array in kcontrol_news field. So, we need to change in each function allocating this to do kstrdump() for each kcontrol_new element, and each place calling kfree() of kcontrol_news should free the string of each item in return.
Takashi
Quoted 42 lines · Takashi Iwai
Quoted 25 lines
Sorry it a oops, paging request failure and not a panic
Quoted 23 lines
Quoted 10 lines
Your second point is applicable here as card instantiation is delayed often for us as all components may not be present and delayed probe finally creates the card.
Quoted 3 lines
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Yes we will add that
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
That is a good point and I think we should do
devm_kstrdup()here so that this is freed when we cleanup the device, or do you have any better suggestion ?
devm_kstrdup()is bad in this case. You can reload the topology unlimitedly, and the memory won't be freed until the device unbind, thus it keeps hogging.Quoted 8 lines
Quoted 4 lines
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.but then question of where should these be freed. For current drivers they declare controls statically, so memory is always there.. How do free up in the cases where we allocate dynamically?
Well, for judging this, we have to follow the code more closely. And it's why I asked which path does it happen exactly.
There are two different paths where the
snd_kcontrol_newis used: the standard controls and dapm. The former is immediately instantiated viasnd_soc_cnew(), so it's fine as is, no need to change. But the latter is different.
Quoted 5 lines
The latter, dapm case, always allocates the
snd_kcontrol_newarray inkcontrol_newsfield. So, we need to change in each function allocating this to dokstrdump()for eachkcontrol_newelement, and each place callingkfree()ofkcontrol_newsshould free the string of each item in return.
It is the latter dapm case with added complexity of topology core creating these kcontrols. I will reproduce this and send the oops tomorrow
Signature · 1 line
~Vinod
Quoted 45 lines · Vinod Koul
Quoted 42 lines · Takashi Iwai
Quoted 25 lines
Sorry it a oops, paging request failure and not a panic
Quoted 23 lines
Quoted 10 lines
Your second point is applicable here as card instantiation is delayed often for us as all components may not be present and delayed probe finally creates the card.
Quoted 3 lines
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Yes we will add that
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
That is a good point and I think we should do
devm_kstrdup()here so that this is freed when we cleanup the device, or do you have any better suggestion ?
devm_kstrdup()is bad in this case. You can reload the topology unlimitedly, and the memory won't be freed until the device unbind, thus it keeps hogging.Quoted 8 lines
Quoted 4 lines
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.but then question of where should these be freed. For current drivers they declare controls statically, so memory is always there.. How do free up in the cases where we allocate dynamically?
Well, for judging this, we have to follow the code more closely. And it's why I asked which path does it happen exactly.
There are two different paths where the
snd_kcontrol_newis used: the standard controls and dapm. The former is immediately instantiated viasnd_soc_cnew(), so it's fine as is, no need to change. But the latter is different.
Quoted 8 lines
Quoted 5 lines
The latter, dapm case, always allocates the
snd_kcontrol_newarray inkcontrol_newsfield. So, we need to change in each function allocating this to dokstrdump()for eachkcontrol_newelement, and each place callingkfree()ofkcontrol_newsshould free the string of each item in return.It is the latter dapm case with added complexity of topology core creating these kcontrols. I will reproduce this and send the oops tomorrow
Not too complex in this case because there are only a few users. A totally untested patch is below.
Takashi
---
sound/soc/soc-topology.c +24 -8
@@ -427,6 +427,16 @@ static void remove_enum(struct snd_soc_component *comp,kfree (se); } +static void free_kcontrol_news (const struct snd_kcontrol_new *_wc,int nums) +{ +struct snd_kcontrol_new *wc = (struct snd_kcontrol_new *)_wc; +int i; + +for (i =0 ; i < nums && wc[i].name; i++) +kfree (wc[i].name); +kfree (wc); +} +/* remove a byte kcontrol */ static void remove_bytes (struct snd_soc_component *comp,struct snd_soc_dobj *dobj,int pass)
@@ -477,7 +487,7 @@ static void remove_widget(struct snd_soc_component *comp,kfree (se->dobj.control.dtexts[i]);kfree (se); -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news,1 ); }else {/* non enumerated widget mixer */ for (i =0 ; i < w->num_kcontrols; i++) {
@@ -490,7 +500,7 @@ static void remove_widget(struct snd_soc_component *comp,snd_ctl_remove (card, w->kcontrols[i]);kfree (sm); } -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news, w->num_kcontrols); }/* widget w is freed by soc-dapm.c */ }
@@ -1149,7 +1159,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(dev_dbg (tplg->dev," adding DAPM widget mixer control %s at %d\n" , mc->hdr.name, i); - kc[i].name = mc->hdr.name; + kc[i].name =kstrdup (mc->hdr.name,GFP_KERNEL ); +if (!kc[i].name) + goto err_str; kc[i].private_value = (long )sm; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = mc->hdr.access;
@@ -1196,7 +1208,7 @@ err_str: err:for (--i; i >=0 ; i--)kfree ((void *)kc[i].private_value); -kfree (kc); +free_kcontrol_news (kc, num_kcontrols);return NULL ; }
@@ -1228,7 +1240,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(dev_dbg (tplg->dev," adding DAPM widget enum control %s\n" , ec->hdr.name); - kc->name = ec->hdr.name; + kc->name =kstrdup (ec->hdr.name,GFP_KERNEL ); +if (!kc->name) + goto err; kc->private_value = (long )se; kc->iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc->access = ec->hdr.access;
@@ -1294,7 +1308,7 @@ err_se:kfree (se); err: -kfree (kc); +free_kcontrol_news (kc,1 );return NULL ; }
@@ -1330,7 +1344,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create("ASoC: adding bytes kcontrol %s with access 0x%x\n" , be->hdr.name, be->hdr.access); - kc[i].name = be->hdr.name; + kc[i].name =kstrdup (be->hdr.name,GFP_KERNEL ); +if (!kc[i].name) + goto err; kc[i].private_value = (long )sbe; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = be->hdr.access;
@@ -1363,7 +1379,7 @@ err:for (--i; i >=0 ; i--)kfree ((void *)kc[i].private_value); -kfree (kc); +free_kcontrol_news (kc, count);return NULL ; }
Quoted 94 lines · Takashi Iwai
Quoted 45 lines · Vinod Koul
Quoted 42 lines · Takashi Iwai
Quoted 25 lines
Sorry it a oops, paging request failure and not a panic
Quoted 23 lines
Quoted 10 lines
Your second point is applicable here as card instantiation is delayed often for us as all components may not be present and delayed probe finally creates the card.
Quoted 3 lines
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Yes we will add that
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
That is a good point and I think we should do
devm_kstrdup()here so that this is freed when we cleanup the device, or do you have any better suggestion ?
devm_kstrdup()is bad in this case. You can reload the topology unlimitedly, and the memory won't be freed until the device unbind, thus it keeps hogging.Quoted 8 lines
Quoted 4 lines
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.but then question of where should these be freed. For current drivers they declare controls statically, so memory is always there.. How do free up in the cases where we allocate dynamically?
Well, for judging this, we have to follow the code more closely. And it's why I asked which path does it happen exactly.
There are two different paths where the
snd_kcontrol_newis used: the standard controls and dapm. The former is immediately instantiated viasnd_soc_cnew(), so it's fine as is, no need to change. But the latter is different.Quoted 8 lines
Quoted 5 lines
The latter, dapm case, always allocates the
snd_kcontrol_newarray inkcontrol_newsfield. So, we need to change in each function allocating this to dokstrdump()for eachkcontrol_newelement, and each place callingkfree()ofkcontrol_newsshould free the string of each item in return.It is the latter dapm case with added complexity of topology core creating these kcontrols. I will reproduce this and send the oops tomorrow
Not too complex in this case because there are only a few users. A totally untested patch is below.
Takashi
---
sound/soc/soc-topology.c +11
-1@@ -427,6 +427,16 @@ static void remove_enum(struct snd_soc_component *comp,kfree (se); } +static void free_kcontrol_news (const struct snd_kcontrol_new *_wc,int nums) +{ +struct snd_kcontrol_new *wc = (struct snd_kcontrol_new *)_wc; +int i; + +for (i =0 ; i < nums && wc[i].name; i++) +kfree (wc[i].name); +kfree (wc); +} +/* remove a byte kcontrol */ static void remove_bytes (struct snd_soc_component *comp,struct snd_soc_dobj *dobj,int pass)@@ -477,7 +487,7 @@ static void remove_widget(struct snd_soc_component *comp,kfree (se->dobj.control.dtexts[i]);kfree (se); -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news,1 ); }else {/* non enumerated widget mixer */ for (i =0 ; i < w->num_kcontrols; i++) {
sound/soc/soc-topology.c static void remove_widget(struct snd_soc_component *comp, · line 503
Quoted 5 lines
sound/soc/soc-topology.c +1
-1@@ -490,7 +500,7 @@ static void remove_widget(struct snd_soc_component *comp,snd_ctl_remove (card, w->kcontrols[i]);kfree (sm); } -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news, w->num_kcontrols);
Hi Takashi,
I have not tested this patch yet. But it should fix the oops. Just looking the code I find remove_widget is either called from snd_soc_tplg_widget_remove or from snd_soc_tplg_component_remove. The xxx_component_remove is called during unregister of the component and there is no caller to snd_soc_tplg_widget_remove.
I guess the intention here is to free the kcontrol_news immediately after the card is registered. Please correct me if I am wrong.
Otherwise shouldn't the devm version of kstrdup work good as it just frees the memory when the device is removed?
Regards,
Subhransu
Quoted 63 lines
sound/soc/soc-topology.c +12
-6}/* widget w is freed by soc-dapm.c */ }@@ -1149,7 +1159,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(dev_dbg (tplg->dev," adding DAPM widget mixer control %s at %d\n" , mc->hdr.name, i); - kc[i].name = mc->hdr.name; + kc[i].name =kstrdup (mc->hdr.name,GFP_KERNEL ); +if (!kc[i].name) + goto err_str; kc[i].private_value = (long )sm; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = mc->hdr.access;@@ -1196,7 +1208,7 @@ err_str: err:for (--i; i >=0 ; i--)kfree ((void *)kc[i].private_value); -kfree (kc); +free_kcontrol_news (kc, num_kcontrols);return NULL ; }@@ -1228,7 +1240,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(dev_dbg (tplg->dev," adding DAPM widget enum control %s\n" , ec->hdr.name); - kc->name = ec->hdr.name; + kc->name =kstrdup (ec->hdr.name,GFP_KERNEL ); +if (!kc->name) + goto err; kc->private_value = (long )se; kc->iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc->access = ec->hdr.access;@@ -1294,7 +1308,7 @@ err_se:kfree (se); err: -kfree (kc); +free_kcontrol_news (kc,1 );return NULL ; }@@ -1330,7 +1344,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create("ASoC: adding bytes kcontrol %s with access 0x%x\n" , be->hdr.name, be->hdr.access); - kc[i].name = be->hdr.name; + kc[i].name =kstrdup (be->hdr.name,GFP_KERNEL ); +if (!kc[i].name) + goto err; kc[i].private_value = (long )sbe; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = be->hdr.access;@@ -1363,7 +1379,7 @@ err:for (--i; i >=0 ; i--)kfree ((void *)kc[i].private_value); -kfree (kc); +free_kcontrol_news (kc, count);return NULL ; }
Quoted 103 lines · Subhransu S. Prusty
Quoted 94 lines · Takashi Iwai
Quoted 45 lines · Vinod Koul
Quoted 42 lines · Takashi Iwai
Quoted 25 lines
Sorry it a oops, paging request failure and not a panic
Quoted 23 lines
Quoted 10 lines
Your second point is applicable here as card instantiation is delayed often for us as all components may not be present and delayed probe finally creates the card.
Quoted 3 lines
Quoted 1 line
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
You should put the commit subject, too.
Yes we will add that
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
So create a copy of the memory and assign to names instead.
And who releases these duplicated memory? It looks like another memory leak to me.
That is a good point and I think we should do
devm_kstrdup()here so that this is freed when we cleanup the device, or do you have any better suggestion ?
devm_kstrdup()is bad in this case. You can reload the topology unlimitedly, and the memory won't be freed until the device unbind, thus it keeps hogging.Quoted 8 lines
Quoted 4 lines
You really need to identify which path hits the issue exactly how. In general, the string passed to template is only for creating the kctl. Once when kctl is created, the whole
snd_kcontrol_newtemplate and the allocated string is no use, so they can be freed.but then question of where should these be freed. For current drivers they declare controls statically, so memory is always there.. How do free up in the cases where we allocate dynamically?
Well, for judging this, we have to follow the code more closely. And it's why I asked which path does it happen exactly.
There are two different paths where the
snd_kcontrol_newis used: the standard controls and dapm. The former is immediately instantiated viasnd_soc_cnew(), so it's fine as is, no need to change. But the latter is different.Quoted 8 lines
Quoted 5 lines
The latter, dapm case, always allocates the
snd_kcontrol_newarray inkcontrol_newsfield. So, we need to change in each function allocating this to dokstrdump()for eachkcontrol_newelement, and each place callingkfree()ofkcontrol_newsshould free the string of each item in return.It is the latter dapm case with added complexity of topology core creating these kcontrols. I will reproduce this and send the oops tomorrow
Not too complex in this case because there are only a few users. A totally untested patch is below.
Takashi
---
sound/soc/soc-topology.c +11
-1@@ -427,6 +427,16 @@ static void remove_enum(struct snd_soc_component *comp,kfree (se); } +static void free_kcontrol_news (const struct snd_kcontrol_new *_wc,int nums) +{ +struct snd_kcontrol_new *wc = (struct snd_kcontrol_new *)_wc; +int i; + +for (i =0 ; i < nums && wc[i].name; i++) +kfree (wc[i].name); +kfree (wc); +} +/* remove a byte kcontrol */ static void remove_bytes (struct snd_soc_component *comp,struct snd_soc_dobj *dobj,int pass)@@ -477,7 +487,7 @@ static void remove_widget(struct snd_soc_component *comp,kfree (se->dobj.control.dtexts[i]);kfree (se); -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news,1 ); }else {/* non enumerated widget mixer */ for (i =0 ; i < w->num_kcontrols; i++) {sound/soc/soc-topology.c static void remove_widget(struct snd_soc_component *comp, · line 503
Quoted 5 lines
sound/soc/soc-topology.c +1
-1@@ -490,7 +500,7 @@ static void remove_widget(struct snd_soc_component *comp,snd_ctl_remove (card, w->kcontrols[i]);kfree (sm); } -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news, w->num_kcontrols);Hi Takashi,
Quoted 8 lines
I have not tested this patch yet. But it should fix the oops. Just looking the code I find
remove_widgetis either called fromsnd_soc_tplg_widget_removeor fromsnd_soc_tplg_component_remove. Thexxx_component_removeis called during unregister of the component and there is no caller tosnd_soc_tplg_widget_remove.I guess the intention here is to free the
kcontrol_newsimmediately after the card is registered. Please correct me if I am wrong.
It is already freed in the original code. The only addition is to free the newly allocated strings in kcontrol_news. So kfree() is replaced with free_kcontrol_news().
Quoted 2 lines
Otherwise shouldn't the devm version of kstrdup work good as it just frees the memory when the device is removed?
No, as already mentioned, devm won't release the data until unbind and the topology data might be reloaded repeatedly, thus user can hog the kernel memory unlimitedly.
Takashi
Quoted 69 lines
Regards,
SubhransuQuoted 63 lines
sound/soc/soc-topology.c +12
-6}/* widget w is freed by soc-dapm.c */ }@@ -1149,7 +1159,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(dev_dbg (tplg->dev," adding DAPM widget mixer control %s at %d\n" , mc->hdr.name, i); - kc[i].name = mc->hdr.name; + kc[i].name =kstrdup (mc->hdr.name,GFP_KERNEL ); +if (!kc[i].name) + goto err_str; kc[i].private_value = (long )sm; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = mc->hdr.access;@@ -1196,7 +1208,7 @@ err_str: err:for (--i; i >=0 ; i--)kfree ((void *)kc[i].private_value); -kfree (kc); +free_kcontrol_news (kc, num_kcontrols);return NULL ; }@@ -1228,7 +1240,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(dev_dbg (tplg->dev," adding DAPM widget enum control %s\n" , ec->hdr.name); - kc->name = ec->hdr.name; + kc->name =kstrdup (ec->hdr.name,GFP_KERNEL ); +if (!kc->name) + goto err; kc->private_value = (long )se; kc->iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc->access = ec->hdr.access;@@ -1294,7 +1308,7 @@ err_se:kfree (se); err: -kfree (kc); +free_kcontrol_news (kc,1 );return NULL ; }@@ -1330,7 +1344,9 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create("ASoC: adding bytes kcontrol %s with access 0x%x\n" , be->hdr.name, be->hdr.access); - kc[i].name = be->hdr.name; + kc[i].name =kstrdup (be->hdr.name,GFP_KERNEL ); +if (!kc[i].name) + goto err; kc[i].private_value = (long )sbe; kc[i].iface =SNDRV_CTL_ELEM_IFACE_MIXER ; kc[i].access = be->hdr.access;@@ -1363,7 +1379,7 @@ err:for (--i; i >=0 ; i--)kfree ((void *)kc[i].private_value); -kfree (kc); +free_kcontrol_news (kc, count);return NULL ; }
Quoted 85 lines · Takashi Iwai
Quoted 70 lines · Subhransu S. Prusty
Quoted 61 lines · Takashi Iwai
Quoted 12 lines · Vinod Koul
Quoted 9 lines · Takashi Iwai
Well, for judging this, we have to follow the code more closely. And it's why I asked which path does it happen exactly.
There are two different paths where the
snd_kcontrol_newis used: the standard controls and dapm. The former is immediately instantiated viasnd_soc_cnew(), so it's fine as is, no need to change. But the latter is different.Quoted 8 lines
Quoted 5 lines
The latter, dapm case, always allocates the
snd_kcontrol_newarray inkcontrol_newsfield. So, we need to change in each function allocating this to dokstrdump()for eachkcontrol_newelement, and each place callingkfree()ofkcontrol_newsshould free the string of each item in return.It is the latter dapm case with added complexity of topology core creating these kcontrols. I will reproduce this and send the oops tomorrow
Not too complex in this case because there are only a few users. A totally untested patch is below.
Takashi
---
sound/soc/soc-topology.c +11
-1@@ -427,6 +427,16 @@ static void remove_enum(struct snd_soc_component *comp,kfree (se); } +static void free_kcontrol_news (const struct snd_kcontrol_new *_wc,int nums) +{ +struct snd_kcontrol_new *wc = (struct snd_kcontrol_new *)_wc; +int i; + +for (i =0 ; i < nums && wc[i].name; i++) +kfree (wc[i].name); +kfree (wc); +} +/* remove a byte kcontrol */ static void remove_bytes (struct snd_soc_component *comp,struct snd_soc_dobj *dobj,int pass)@@ -477,7 +487,7 @@ static void remove_widget(struct snd_soc_component *comp,kfree (se->dobj.control.dtexts[i]);kfree (se); -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news,1 ); }else {/* non enumerated widget mixer */ for (i =0 ; i < w->num_kcontrols; i++) {sound/soc/soc-topology.c static void remove_widget(struct snd_soc_component *comp, · line 503
Quoted 5 lines
sound/soc/soc-topology.c +1
-1@@ -490,7 +500,7 @@ static void remove_widget(struct snd_soc_component *comp,snd_ctl_remove (card, w->kcontrols[i]);kfree (sm); } -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news, w->num_kcontrols);Hi Takashi,
Quoted 8 lines
I have not tested this patch yet. But it should fix the oops. Just looking the code I find
remove_widgetis either called fromsnd_soc_tplg_widget_removeor fromsnd_soc_tplg_component_remove. Thexxx_component_removeis called during unregister of the component and there is no caller tosnd_soc_tplg_widget_remove.I guess the intention here is to free the
kcontrol_newsimmediately after the card is registered. Please correct me if I am wrong.It is already freed in the original code. The only addition is to free the newly allocated strings in
kcontrol_news. Sokfree()is replaced withfree_kcontrol_news().
Quoted 6 lines
Quoted 2 lines
Otherwise shouldn't the devm version of kstrdup work good as it just frees the memory when the device is removed?
No, as already mentioned, devm won't release the data until unbind and the topology data might be reloaded repeatedly, thus user can hog the kernel memory unlimitedly.
Then which of the APIs snd_soc_tplg_widget_remove or snd_soc_tplg_component_remove should free the memory in this scenairo? I guess it should be snd_soc_tplg_widget_remove, but I don't see a caller of this API.
Regards,
Subhransu
Quoted 7 lines
Takashi
Quoted 3 lines
Signature · 1 line
--
Quoted 90 lines · Subhransu S. Prusty
Quoted 88 lines · Takashi Iwai
Quoted 70 lines · Subhransu S. Prusty
Quoted 61 lines · Takashi Iwai
Quoted 12 lines · Vinod Koul
Quoted 9 lines · Takashi Iwai
Well, for judging this, we have to follow the code more closely. And it's why I asked which path does it happen exactly.
There are two different paths where the
snd_kcontrol_newis used: the standard controls and dapm. The former is immediately instantiated viasnd_soc_cnew(), so it's fine as is, no need to change. But the latter is different.Quoted 8 lines
Quoted 5 lines
The latter, dapm case, always allocates the
snd_kcontrol_newarray inkcontrol_newsfield. So, we need to change in each function allocating this to dokstrdump()for eachkcontrol_newelement, and each place callingkfree()ofkcontrol_newsshould free the string of each item in return.It is the latter dapm case with added complexity of topology core creating these kcontrols. I will reproduce this and send the oops tomorrow
Not too complex in this case because there are only a few users. A totally untested patch is below.
Takashi
---
sound/soc/soc-topology.c +11
-1@@ -427,6 +427,16 @@ static void remove_enum(struct snd_soc_component *comp,kfree (se); } +static void free_kcontrol_news (const struct snd_kcontrol_new *_wc,int nums) +{ +struct snd_kcontrol_new *wc = (struct snd_kcontrol_new *)_wc; +int i; + +for (i =0 ; i < nums && wc[i].name; i++) +kfree (wc[i].name); +kfree (wc); +} +/* remove a byte kcontrol */ static void remove_bytes (struct snd_soc_component *comp,struct snd_soc_dobj *dobj,int pass)@@ -477,7 +487,7 @@ static void remove_widget(struct snd_soc_component *comp,kfree (se->dobj.control.dtexts[i]);kfree (se); -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news,1 ); }else {/* non enumerated widget mixer */ for (i =0 ; i < w->num_kcontrols; i++) {sound/soc/soc-topology.c static void remove_widget(struct snd_soc_component *comp, · line 503
Quoted 5 lines
sound/soc/soc-topology.c +1
-1@@ -490,7 +500,7 @@ static void remove_widget(struct snd_soc_component *comp,snd_ctl_remove (card, w->kcontrols[i]);kfree (sm); } -kfree (w->kcontrol_news); +free_kcontrol_news (w->kcontrol_news, w->num_kcontrols);Hi Takashi,
Quoted 8 lines
I have not tested this patch yet. But it should fix the oops. Just looking the code I find
remove_widgetis either called fromsnd_soc_tplg_widget_removeor fromsnd_soc_tplg_component_remove. Thexxx_component_removeis called during unregister of the component and there is no caller tosnd_soc_tplg_widget_remove.I guess the intention here is to free the
kcontrol_newsimmediately after the card is registered. Please correct me if I am wrong.It is already freed in the original code. The only addition is to free the newly allocated strings in
kcontrol_news. Sokfree()is replaced withfree_kcontrol_news().Quoted 2 lines
Otherwise shouldn't the devm version of kstrdup work good as it just frees the memory when the device is removed?
Quoted 7 lines
Quoted 3 lines
No, as already mentioned, devm won't release the data until unbind and the topology data might be reloaded repeatedly, thus user can hog the kernel memory unlimitedly.
Then which of the APIs
snd_soc_tplg_widget_removeorsnd_soc_tplg_component_removeshould free the memory in this scenairo? I guess it should besnd_soc_tplg_widget_remove, but I don't see a caller of this API.
Then it's a driver's failure. It needs to call it appropriately before reloading. Or it was supposed to be invoked from snd_soc_tplg_component_remove()? I don't know.
Takashi
Mark Brown
maintainerQuoted 1 line · Subhransu S. Prusty
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
Please provide human readable descriptions of things - the id# means that's not even pasteable. :(
Quoted 6 lines · Mark Brown
Quoted 1 line · Subhransu S. Prusty
Issue is caught with id#87b5ed8ecb9fe05a696e1c0b53c7a49ea66432c1
Please provide human readable descriptions of things - the id# means that's not even pasteable. :(
Sorry about that, I did ask Subhransu to send out rev 2 with proper style
Thanks
Signature · 1 line
~Vinod