diff mbox series

ASoC: intel: Fix memleak in sst_media_open

Message ID 20200809085654.16483-1-dinghao.liu@zju.edu.cn (mailing list archive)
State New, archived
Headers show
Series ASoC: intel: Fix memleak in sst_media_open | expand

Commit Message

Dinghao Liu Aug. 9, 2020, 8:56 a.m. UTC
When power_up_sst() fails, stream needs to be freed
just like when try_module_get() fails. However, current
code is returning directly and ends up leaking memory.

Fixes: 0121327c1a68b ("ASoC: Intel: mfld-pcm: add control for powering up/down dsp")
Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
---
 sound/soc/intel/atom/sst-mfld-platform-pcm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Pierre-Louis Bossart Aug. 10, 2020, 2:34 p.m. UTC | #1
On 8/9/20 3:56 AM, Dinghao Liu wrote:
> When power_up_sst() fails, stream needs to be freed
> just like when try_module_get() fails. However, current
> code is returning directly and ends up leaking memory.
> 
> Fixes: 0121327c1a68b ("ASoC: Intel: mfld-pcm: add control for powering up/down dsp")
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> ---
>   sound/soc/intel/atom/sst-mfld-platform-pcm.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/sound/soc/intel/atom/sst-mfld-platform-pcm.c b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> index 49b9f18472bc..79fedf9e3da1 100644
> --- a/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> +++ b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> @@ -330,8 +330,10 @@ static int sst_media_open(struct snd_pcm_substream *substream,
>   	runtime->private_data = stream;
>   
>   	ret_val = power_up_sst(stream);
> -	if (ret_val < 0)
> +	if (ret_val < 0) {
> +		kfree(stream);
>   		return ret_val;
> +	}
>   
>   	/* Make sure, that the period size is always even */
>   	snd_pcm_hw_constraint_step(substream->runtime, 0,
> 

Alternate suggestion to fix this, it's not great when we mix gotos and 
returns.

diff --git a/sound/soc/intel/atom/sst-mfld-platform-pcm.c 
b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
index 49b9f18472bc..b1cac7abdc0a 100644
--- a/sound/soc/intel/atom/sst-mfld-platform-pcm.c
+++ b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
@@ -331,7 +331,7 @@ static int sst_media_open(struct snd_pcm_substream 
*substream,

         ret_val = power_up_sst(stream);
         if (ret_val < 0)
-               return ret_val;
+               goto out_power_up;

         /* Make sure, that the period size is always even */
         snd_pcm_hw_constraint_step(substream->runtime, 0,
@@ -340,8 +340,9 @@ static int sst_media_open(struct snd_pcm_substream 
*substream,
         return snd_pcm_hw_constraint_integer(runtime,
                          SNDRV_PCM_HW_PARAM_PERIODS);
  out_ops:
-       kfree(stream);
         mutex_unlock(&sst_lock);
+out_power_up:
+       kfree(stream);
         return ret_val;
  }
Dinghao Liu Aug. 13, 2020, 8:28 a.m. UTC | #2
> 
> On 8/9/20 3:56 AM, Dinghao Liu wrote:
> > When power_up_sst() fails, stream needs to be freed
> > just like when try_module_get() fails. However, current
> > code is returning directly and ends up leaking memory.
> > 
> > Fixes: 0121327c1a68b ("ASoC: Intel: mfld-pcm: add control for powering up/down dsp")
> > Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> > ---
> >   sound/soc/intel/atom/sst-mfld-platform-pcm.c | 4 +++-
> >   1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/sound/soc/intel/atom/sst-mfld-platform-pcm.c b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> > index 49b9f18472bc..79fedf9e3da1 100644
> > --- a/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> > +++ b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> > @@ -330,8 +330,10 @@ static int sst_media_open(struct snd_pcm_substream *substream,
> >   	runtime->private_data = stream;
> >   
> >   	ret_val = power_up_sst(stream);
> > -	if (ret_val < 0)
> > +	if (ret_val < 0) {
> > +		kfree(stream);
> >   		return ret_val;
> > +	}
> >   
> >   	/* Make sure, that the period size is always even */
> >   	snd_pcm_hw_constraint_step(substream->runtime, 0,
> > 
> 
> Alternate suggestion to fix this, it's not great when we mix gotos and 
> returns.
> 
> diff --git a/sound/soc/intel/atom/sst-mfld-platform-pcm.c 
> b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> index 49b9f18472bc..b1cac7abdc0a 100644
> --- a/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> +++ b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
> @@ -331,7 +331,7 @@ static int sst_media_open(struct snd_pcm_substream 
> *substream,
> 
>          ret_val = power_up_sst(stream);
>          if (ret_val < 0)
> -               return ret_val;
> +               goto out_power_up;
> 
>          /* Make sure, that the period size is always even */
>          snd_pcm_hw_constraint_step(substream->runtime, 0,
> @@ -340,8 +340,9 @@ static int sst_media_open(struct snd_pcm_substream 
> *substream,
>          return snd_pcm_hw_constraint_integer(runtime,
>                           SNDRV_PCM_HW_PARAM_PERIODS);
>   out_ops:
> -       kfree(stream);
>          mutex_unlock(&sst_lock);
> +out_power_up:
> +       kfree(stream);
>          return ret_val;
>   }

Thank you for your advice! This is better and I will send 
a new patch soon. 

Regards,
Dinghao
diff mbox series

Patch

diff --git a/sound/soc/intel/atom/sst-mfld-platform-pcm.c b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
index 49b9f18472bc..79fedf9e3da1 100644
--- a/sound/soc/intel/atom/sst-mfld-platform-pcm.c
+++ b/sound/soc/intel/atom/sst-mfld-platform-pcm.c
@@ -330,8 +330,10 @@  static int sst_media_open(struct snd_pcm_substream *substream,
 	runtime->private_data = stream;
 
 	ret_val = power_up_sst(stream);
-	if (ret_val < 0)
+	if (ret_val < 0) {
+		kfree(stream);
 		return ret_val;
+	}
 
 	/* Make sure, that the period size is always even */
 	snd_pcm_hw_constraint_step(substream->runtime, 0,