Skip to contentSkip to conversation

ALSA: hda: fix possible memory leak in azx_codec_configure()

PATCH In review Yang Yingliang 2 participants 3 messages Oct 24, 2022 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: hda: fix possible memory leak in azx_codec_configure()

If the codec device is registered, after calling snd_hdac_device_unregister(), codec has already been removed from chip bus, it should call put_device() to give up reference to free codec and device name.

Fixes c0f1886de7e1 ("ALSA: hda: intel: Allow repeatedly probing on codec configuration errors")
---
 sound/pci/hda/hda_controller.c | 3 +++
 1 file changed, 3 insertions(+)
sound/pci/hda/hda_controller.c +3 -0
index 0ff286b7b66b..f6a73ce300a4 100644
@@ -1257,8 +1257,11 @@ int azx_codec_configure(struct azx *chip)
 		/* unregister failed codecs if any codec has been probed */
 		list_for_each_codec_safe(codec, next, &chip->bus) {
 			if (!codec->configured) {
+				bool is_registered = device_is_registered(&codec->core.dev);
 				codec_err(codec, "Unable to configure, disabling\n");
 				snd_hdac_device_unregister(&codec->core);
+				if (is_registered)
+					put_device(&codec->core.dev);
 			}
 		}
 	}
Signature · 1 line
2.25.1
#1 lore raw
Quoted 7 lines · Yang Yingliang

If the codec device is registered, after calling snd_hdac_device_unregister(), codec has already been removed from chip bus, it should call put_device() to give up reference to free codec and device name.

Fixes c0f1886de7e1 ("ALSA: hda: intel: Allow repeatedly probing on codec configuration errors")

Hrm, are you sure that this will result in a memory leak? put_device() is called at snd_hda_codec_unregister() that is invoked via dev_free callback of the snd_hda_codec object -- which is called when the top-level driver is removed. I'm afraid that your patch will lead to a double-free.

thanks,

Takashi

Quoted 23 lines
---
 sound/pci/hda/hda_controller.c | 3 +++
 1 file changed, 3 insertions(+)
sound/pci/hda/hda_controller.c +3 -0
index 0ff286b7b66b..f6a73ce300a4 100644
@@ -1257,8 +1257,11 @@ int azx_codec_configure(struct azx *chip)
 		/* unregister failed codecs if any codec has been probed */
 		list_for_each_codec_safe(codec, next, &chip->bus) {
 			if (!codec->configured) {
+				bool is_registered = device_is_registered(&codec->core.dev);
 				codec_err(codec, "Unable to configure, disabling\n");
 				snd_hdac_device_unregister(&codec->core);
+				if (is_registered)
+					put_device(&codec->core.dev);
 			}
 		}
 	}
Signature · 1 line
2.25.1
#2 lore raw

Hi,

Quoted 6 lines · Takashi Iwai
Quoted 4 lines · Yang Yingliang

If the codec device is registered, after calling snd_hdac_device_unregister(), codec has already been removed from chip bus, it should call put_device() to give up reference to free codec and device name.

Quoted 7 lines
Quoted 2 lines
Fixes c0f1886de7e1 ("ALSA: hda: intel: Allow repeatedly probing on codec configuration errors")

Hrm, are you sure that this will result in a memory leak? put_device() is called at snd_hda_codec_unregister() that is invoked via dev_free callback of the snd_hda_codec object -- which is called when the top-level driver is removed. I'm afraid that your patch will lead to a double-free.

Thanks for point out.
I look deeper, the snd device is add to device list of card, but snd_hdac_device_unregister() is just remove the codec from chip bus, and the snd device still in device list of card, at last each of snd device is freed in snd_device_free_all() with calling snd_hda_codec_dev_free().

Thanks,
Yang

Quoted 30 lines

thanks,

Takashi

Quoted 23 lines
---
  sound/pci/hda/hda_controller.c | 3 +++
  1 file changed, 3 insertions(+)
sound/pci/hda/hda_controller.c +3 -0
index 0ff286b7b66b..f6a73ce300a4 100644
@@ -1257,8 +1257,11 @@ int azx_codec_configure(struct azx *chip)
  		/* unregister failed codecs if any codec has been probed */
  		list_for_each_codec_safe(codec, next, &chip->bus) {
  			if (!codec->configured) {
+				bool is_registered = device_is_registered(&codec->core.dev);
  				codec_err(codec, "Unable to configure, disabling\n");
  				snd_hdac_device_unregister(&codec->core);
+				if (is_registered)
+					put_device(&codec->core.dev);
  			}
  		}
  	}
Signature · 1 line
2.25.1

.