[PATCH v2 2/7] ALSA: pcmtest: Fix leak at probe error

From: Takashi Iwai

Date: Wed Oct 07 2026 - 06:00:09 EST


When an error happens at probe of pcmtest driver before
snd_card_register() succeeds, we have to release the card explicitly,
but it's not done, leading to a potential memory leak.

Also, pcmtest driver manages struct pcmtst object unnecessarily
complicatedly by a dynamic allocation and release.

This patch fixes and cleans up the resource management:
- Use card's private_data allocation for struct sndpcmtst at the card
creation, which is released automatically together with the card
- Since the allocation of pcmtest is gone, snd_pcmtst_create() is
rather pointless, so just directly calling snd_pcmtst_new_pcm()
- Use the new auto-clean mechanism to handle the error case at probe.
- Many release handlers became superfluous and dropped

In the end we got a good amount of code reduction.

Fixes: 315a3d57c64c ("ALSA: Implement the new Virtual PCM Test Driver")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Signed-off-by: Takashi Iwai <tiwai@xxxxxxx>
---
sound/drivers/pcmtest.c | 66 +++++++----------------------------------
1 file changed, 10 insertions(+), 56 deletions(-)

diff --git a/sound/drivers/pcmtest.c b/sound/drivers/pcmtest.c
index fea9580593e6..b4e333b4c438 100644
--- a/sound/drivers/pcmtest.c
+++ b/sound/drivers/pcmtest.c
@@ -446,20 +446,6 @@ static snd_pcm_uframes_t snd_pcmtst_pcm_pointer(struct snd_pcm_substream *substr
return bytes_to_frames(substream->runtime, v_iter->buf_pos);
}

-static int snd_pcmtst_free(struct pcmtst *pcmtst)
-{
- if (!pcmtst)
- return 0;
- kfree(pcmtst);
- return 0;
-}
-
-// These callbacks are required, but empty - all freeing occurs in pdev_remove
-static int snd_pcmtst_dev_free(struct snd_device *device)
-{
- return 0;
-}
-
static void pcmtst_pdev_release(struct device *dev)
{
}
@@ -563,40 +549,9 @@ static int snd_pcmtst_new_pcm(struct pcmtst *pcmtst)
return err;
}

-static int snd_pcmtst_create(struct snd_card *card, struct platform_device *pdev,
- struct pcmtst **r_pcmtst)
-{
- struct pcmtst *pcmtst;
- int err;
- static const struct snd_device_ops ops = {
- .dev_free = snd_pcmtst_dev_free,
- };
-
- pcmtst = kzalloc_obj(*pcmtst);
- if (!pcmtst)
- return -ENOMEM;
- pcmtst->card = card;
- pcmtst->pdev = pdev;
-
- err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, pcmtst, &ops);
- if (err < 0)
- goto _err_free_chip;
-
- err = snd_pcmtst_new_pcm(pcmtst);
- if (err < 0)
- goto _err_free_chip;
-
- *r_pcmtst = pcmtst;
- return 0;
-
-_err_free_chip:
- snd_pcmtst_free(pcmtst);
- return err;
-}
-
static int pcmtst_probe(struct platform_device *pdev)
{
- struct snd_card *card;
+ struct snd_card *card __free(snd_card_free) = NULL;
struct pcmtst *pcmtst;
int err;

@@ -604,10 +559,16 @@ static int pcmtst_probe(struct platform_device *pdev)
if (err)
return err;

- err = snd_devm_card_new(&pdev->dev, index, id, THIS_MODULE, 0, &card);
+ err = snd_devm_card_new(&pdev->dev, index, id, THIS_MODULE,
+ sizeof(*pcmtst), &card);
if (err < 0)
return err;
- err = snd_pcmtst_create(card, pdev, &pcmtst);
+
+ pcmtst = card->private_data;
+ pcmtst->card = card;
+ pcmtst->pdev = pdev;
+
+ err = snd_pcmtst_new_pcm(pcmtst);
if (err < 0)
return err;

@@ -620,17 +581,11 @@ static int pcmtst_probe(struct platform_device *pdev)
return err;

platform_set_drvdata(pdev, pcmtst);
+ card = NULL; /* probe succeeded, don't release as error */

return 0;
}

-static void pdev_remove(struct platform_device *pdev)
-{
- struct pcmtst *pcmtst = platform_get_drvdata(pdev);
-
- snd_pcmtst_free(pcmtst);
-}
-
static struct platform_device pcmtst_pdev = {
.name = "pcmtest",
.dev.release = pcmtst_pdev_release,
@@ -638,7 +593,6 @@ static struct platform_device pcmtst_pdev = {

static struct platform_driver pcmtst_pdrv = {
.probe = pcmtst_probe,
- .remove = pdev_remove,
.driver = {
.name = "pcmtest",
},
--
2.55.0