ALSA: pcmtest: Fix leak at probe error

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: 315a3d57c6 ("ALSA: Implement the new Virtual PCM Test Driver")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20261007172533.14667-3-tiwai@suse.de
This commit is contained in:
Takashi Iwai 2026-10-07 19:25:20 +02:00
parent 9d3e19c192
commit d0f2ca62e8

View File

@ -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",
},