Skip to contentSkip to conversation

ASoC: SOF: topology: Fix mem leak in sof_dai_load()

PATCH Superseded Kamil Duljas 2 participants 3 messages Nov 16, 2023 – Nov 17, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: SOF: topology: Fix mem leak in sof_dai_load()

The function has multiple return points at which it is not released previously allocated memory.

---
 sound/soc/sof/topology.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
sound/soc/sof/topology.c +9 -5
index a3a3af252259..ef8f8991f025 100644
@@ -1736,8 +1736,10 @@ static int sof_dai_load(struct snd_soc_component *scomp, int index,
 	/* perform pcm set op */
 	if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) {
 		ret = ipc_pcm_ops->pcm_setup(sdev, spcm);
-		if (ret < 0)
+		if (ret < 0) {
+			kfree(spcm);
 			return ret;
+		}
 	}
 
 	dai_drv->dobj.private = spcm;
@@ -1747,6 +1749,7 @@ static int sof_dai_load(struct snd_soc_component *scomp, int index,
 			       ARRAY_SIZE(stream_tokens), private->array,
 			       le32_to_cpu(private->size));
 	if (ret) {
+		kfree(dai_drv->dobj.private);
 		dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
 			le32_to_cpu(private->size));
 		return ret;
@@ -1764,9 +1767,9 @@ static int sof_dai_load(struct snd_soc_component *scomp, int index,
 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
 				  PAGE_SIZE, &spcm->stream[stream].page_table);
 	if (ret < 0) {
+		kfree(dai_drv->dobj.private);
 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
 			caps->name, ret);
-
 		return ret;
 	}
 
@@ -1782,9 +1785,10 @@ static int sof_dai_load(struct snd_soc_component *scomp, int index,
 	stream = SNDRV_PCM_STREAM_CAPTURE;
 
 	/* do we need to allocate capture PCM DMA pages */
-	if (!spcm->pcm.capture)
+	if (!spcm->pcm.capture) {
+		kfree(dai_drv->dobj.private);
 		return ret;
-
+	}
 	caps = &spcm->pcm.caps[stream];
 
 	/* allocate capture page table buffer */
@@ -1810,7 +1814,7 @@ static int sof_dai_load(struct snd_soc_component *scomp, int index,
 free_playback_tables:
 	if (spcm->pcm.playback)
 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
-
+	kfree(dai_drv->dobj.private);
 	return ret;
 }
 
Signature · 1 line
2.35.2.windows.1
#1 lore raw
Quoted 13 lines · Kamil Duljas

The function has multiple return points at which it is not released previously allocated memory.

---
 sound/soc/sof/topology.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
sound/soc/sof/topology.c +0 -0
index a3a3af252259..ef8f8991f025 100644

sound/soc/sof/topology.c static int sof_dai_load(struct · line 1743

Quoted 9 lines
sound/soc/sof/topology.c +3 -1
@@ -1736,8 +1736,10 @@ static int sof_dai_load(struct
 snd_soc_component *scomp, int index,
 	/* perform pcm set op */
 	if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) {
 		ret = ipc_pcm_ops->pcm_setup(sdev, spcm);
-		if (ret < 0)
+		if (ret < 0) {
+			kfree(spcm);
 			return ret;
+		}

Thanks for the patch, Kamil. I think just the change above should suffice to fix the memory leak in case of error. For all the other error paths below, snd_soc_tplg_component_remove() should be able to handle freeing the spcm and the page tables.

Thanks,
Ranjani

#2 lore raw

Hello Ranjani,
You right. My mistake. I will prepare patch v2

Kamil

czw., 16 lis 2023 o 19:57 Sridharan, Ranjani <[email protected]> napisał(a):

Quoted 32 lines · Kamil Duljas
Quoted 13 lines

The function has multiple return points at which it is not released previously allocated memory.

---
 sound/soc/sof/topology.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
sound/soc/sof/topology.c +0 -0
index a3a3af252259..ef8f8991f025 100644

sound/soc/sof/topology.c static int sof_dai_load(struct · line 1743

Quoted 9 lines
sound/soc/sof/topology.c +3 -1
@@ -1736,8 +1736,10 @@ static int sof_dai_load(struct
 snd_soc_component *scomp, int index,
      /* perform pcm set op */
      if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) {
              ret = ipc_pcm_ops->pcm_setup(sdev, spcm);
-             if (ret < 0)
+             if (ret < 0) {
+                     kfree(spcm);
                      return ret;
+             }

Thanks for the patch, Kamil. I think just the change above should suffice to fix the memory leak in case of error. For all the other error paths below, snd_soc_tplg_component_remove() should be able to handle freeing the spcm and the page tables.

Thanks,
Ranjani

Signature · 2 lines
Pozdrawiam,
Kamil Duljas