Skip to contentSkip to conversation

ASOC: SOF: Intel: hda-codec: fix possible memory leak in hda_codec_device_init()

PATCH Superseded Yang Yingliang 2 participants 3 messages Oct 20, 2022 – Nov 2, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASOC: SOF: Intel: hda-codec: fix possible memory leak in hda_codec_device_init()

If snd_hdac_device_register() fails, 'codec' is leaked, free it in snd_hdac_device_exit(). And device_initialize() is called in snd_hdac_device_init(), it should call put_device() to give up reference or the name allocated in dev_set_name() is leaked. The snd_hdac_device_exit() will be called in dev->release().

Fixes 829c67319806 ("ASoC: SOF: Intel: Introduce HDA codec init and exit routines")
---
 sound/soc/sof/intel/hda-codec.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
sound/soc/sof/intel/hda-codec.c +6 -2
index 1e9afc48394c..f8cbcc25154b 100644
@@ -111,7 +111,11 @@ EXPORT_SYMBOL_NS(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC);
 
 static void hda_codec_device_exit(struct device *dev)
 {
-	snd_hdac_device_exit(dev_to_hdac_dev(dev));
+	struct hdac_device *hdac_dev = dev_to_hdac_dev(dev);
+	struct hda_codec *codec = container_of(hdac_dev, struct hda_codec, core);
+
+	snd_hdac_device_exit(hdac_dev);
+	kfree(codec);
 }
 
 static struct hda_codec *hda_codec_device_init(struct hdac_bus *bus, int addr, int type)
@@ -131,7 +135,7 @@ static struct hda_codec *hda_codec_device_init(struct hdac_bus *bus, int addr, i
 	ret = snd_hdac_device_register(&codec->core);
 	if (ret) {
 		dev_err(bus->dev, "failed to register hdac device\n");
-		snd_hdac_device_exit(&codec->core);
+		put_device(&codec->core.dev);
 		return ERR_PTR(ret);
 	}
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 5 lines · Yang Yingliang

If snd_hdac_device_register() fails, 'codec' is leaked, free it in snd_hdac_device_exit(). And device_initialize() is called in snd_hdac_device_init(), it should call put_device() to give up reference or the name allocated in dev_set_name() is leaked. The snd_hdac_device_exit() will be called in dev->release().

This doesn't apply against current code, please check and resend.

#2 lore raw

Hi,

Quoted 3 lines · Mark Brown
Quoted 1 line · Yang Yingliang

If snd_hdac_device_register() fails, 'codec' is leaked, free it in snd_hdac_device_exit(). And device_initialize() is called in snd_hdac_device_init(), it should call put_device() to give up reference or the name allocated in dev_set_name() is leaked. The snd_hdac_device_exit() will be called in dev->release().

This doesn't apply against current code, please check and resend.

This is v1, the v2 has already been merged.
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git/commit/?h=for-6.1&id=e9441675edc1bb8dbfadacf68aafacca60d65a25

Thanks,
Yang