Skip to contentSkip to conversation

drm/amdgpu/discovery: fix possible memory leak

PATCH In review Yang Yingliang 2 participants 4 messages Oct 18, 2022 – Oct 19, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu/discovery: fix possible memory leak

If kset_register() fails, the refcount of device is not 0, the name allocated in dev_set_name() is leaked. Fix this by calling kset_put(), so that it will be freed in callback function kobject_cleanup().

Fixes a6c40b178092 ("drm/amdgpu: Show IP discovery in sysfs")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +3 -2
index 3993e6134914..638edcf70227 100644
@@ -863,7 +863,7 @@ static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev,
 				res = kset_register(&ip_hw_id->hw_id_kset);
 				if (res) {
 					DRM_ERROR("Couldn't register ip_hw_id kset");
-					kfree(ip_hw_id);
+					kset_put(&ip_hw_id->hw_id_kset);
 					return res;
 				}
 				if (hw_id_names[ii]) {
@@ -954,7 +954,7 @@ static int amdgpu_discovery_sysfs_recurse(struct amdgpu_device *adev)
 		res = kset_register(&ip_die_entry->ip_kset);
 		if (res) {
 			DRM_ERROR("Couldn't register ip_die_entry kset");
-			kfree(ip_die_entry);
+			kset_put(&ip_die_entry->ip_kset);
 			return res;
 		}
 
@@ -989,6 +989,7 @@ static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev)
 	res = kset_register(&adev->ip_top->die_kset);
 	if (res) {
 		DRM_ERROR("Couldn't register die_kset");
+		kset_put(&adev->ip_top->die_kset);
 		goto Err;
 	}
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 4 lines · Yang Yingliang

If kset_register() fails, the refcount of device is not 0, the name allocated in dev_set_name() is leaked. Fix this by calling kset_put(), so that it will be freed in callback function kobject_cleanup().

Good catch, but looking at the code it seems that what is being leaked is the memory pointed to by kset.kobj.name which is allocated in kobject_set_name(&kset.kobj, ...), which is called right before the call to kset_register().

Modify the description with this explanation and repost the patch and I'll R-B it then. Something like:

Quoted 3 lines

If kset_register() fails, the refcount of the device is not 0, and the name allocated in kobject_set_name(&kset.kobj, ...) is leaked. Fix this by calling kset_put(), so that kset.kobj.name is freed in the callback function kobject_cleanup().

Now, if there's a leak of dev_set_name(), then that would be something else/another patch.

The rest below looks good.

Since this patch touches only files under drm/amd/amdgpu/ and for a highly AMD-specific feature, you didn't need to spam the dri-dev ML--amdgfx ML should be plenty enough.

Regards,
Luben

Quoted 36 lines
Fixes a6c40b178092 ("drm/amdgpu: Show IP discovery in sysfs")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +3 -2
index 3993e6134914..638edcf70227 100644
@@ -863,7 +863,7 @@ static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev,
 				res = kset_register(&ip_hw_id->hw_id_kset);
 				if (res) {
 					DRM_ERROR("Couldn't register ip_hw_id kset");
-					kfree(ip_hw_id);
+					kset_put(&ip_hw_id->hw_id_kset);
 					return res;
 				}
 				if (hw_id_names[ii]) {
@@ -954,7 +954,7 @@ static int amdgpu_discovery_sysfs_recurse(struct amdgpu_device *adev)
 		res = kset_register(&ip_die_entry->ip_kset);
 		if (res) {
 			DRM_ERROR("Couldn't register ip_die_entry kset");
-			kfree(ip_die_entry);
+			kset_put(&ip_die_entry->ip_kset);
 			return res;
 		}
 
@@ -989,6 +989,7 @@ static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev)
 	res = kset_register(&adev->ip_top->die_kset);
 	if (res) {
 		DRM_ERROR("Couldn't register die_kset");
+		kset_put(&adev->ip_top->die_kset);
 		goto Err;
 	}
 
#2 lore raw
Quoted 18 lines · Luben Tuikov
Quoted 4 lines · Yang Yingliang

If kset_register() fails, the refcount of device is not 0, the name allocated in dev_set_name() is leaked. Fix this by calling kset_put(), so that it will be freed in callback function kobject_cleanup().

Good catch, but looking at the code it seems that what is being leaked is the memory pointed to by kset.kobj.name which is allocated in kobject_set_name(&kset.kobj, ...), which is called right before the call to kset_register().

Modify the description with this explanation and repost the patch and I'll R-B it then. Something like:

Quoted 3 lines

If kset_register() fails, the refcount of the device is not 0, and the name allocated in kobject_set_name(&kset.kobj, ...) is leaked. Fix this by calling kset_put(), so that kset.kobj.name is freed in the callback function kobject_cleanup().

Quoted 7 lines

Now, if there's a leak of dev_set_name(), then that would be something else/another patch.

The rest below looks good.

Since this patch touches only files under drm/amd/amdgpu/ and for a highly AMD-specific feature, you didn't need to spam the dri-dev ML--amdgfx ML should be plenty enough.

Also, don't forget to CC stable (by a tag in the commit description).

Regards,
Luben

Quoted 40 lines

Regards,
Luben

Quoted 36 lines
Fixes a6c40b178092 ("drm/amdgpu: Show IP discovery in sysfs")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +3 -2
index 3993e6134914..638edcf70227 100644
@@ -863,7 +863,7 @@ static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev,
 				res = kset_register(&ip_hw_id->hw_id_kset);
 				if (res) {
 					DRM_ERROR("Couldn't register ip_hw_id kset");
-					kfree(ip_hw_id);
+					kset_put(&ip_hw_id->hw_id_kset);
 					return res;
 				}
 				if (hw_id_names[ii]) {
@@ -954,7 +954,7 @@ static int amdgpu_discovery_sysfs_recurse(struct amdgpu_device *adev)
 		res = kset_register(&ip_die_entry->ip_kset);
 		if (res) {
 			DRM_ERROR("Couldn't register ip_die_entry kset");
-			kfree(ip_die_entry);
+			kset_put(&ip_die_entry->ip_kset);
 			return res;
 		}
 
@@ -989,6 +989,7 @@ static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev)
 	res = kset_register(&adev->ip_top->die_kset);
 	if (res) {
 		DRM_ERROR("Couldn't register die_kset");
+		kset_put(&adev->ip_top->die_kset);
 		goto Err;
 	}
 
#3 lore raw
In reply to Luben Tuikov

Hi,

Quoted 9 lines · Luben Tuikov
Quoted 4 lines · Yang Yingliang

If kset_register() fails, the refcount of device is not 0, the name allocated in dev_set_name() is leaked. Fix this by calling kset_put(), so that it will be freed in callback function kobject_cleanup().

Good catch, but looking at the code it seems that what is being leaked is the memory pointed to by kset.kobj.name which is allocated in kobject_set_name(&kset.kobj, ...), which is called right before the call to kset_register().

Ah, I forgot to use format-patch to generate new patch after updating commit

message in my local tree and sent a old one. Thanks for point out.

Quoted 7 lines

Modify the description with this explanation and repost the patch and I'll R-B it then. Something like:

Quoted 3 lines

If kset_register() fails, the refcount of the device is not 0, and the name allocated in kobject_set_name(&kset.kobj, ...) is leaked. Fix this by calling kset_put(), so that kset.kobj.name is freed in the callback function kobject_cleanup().

Quoted 7 lines

Now, if there's a leak of dev_set_name(), then that would be something else/another patch.

The rest below looks good.

Since this patch touches only files under drm/amd/amdgpu/ and for a highly AMD-specific feature, you didn't need to spam the dri-dev ML--amdgfx ML should be plenty enough.

OK.

Thanks,
Yang

Quoted 41 lines

Regards,
Luben

Quoted 29 lines
Fixes a6c40b178092 ("drm/amdgpu: Show IP discovery in sysfs")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +2 -2
index 3993e6134914..638edcf70227 100644
@@ -863,7 +863,7 @@ static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev,
  				res = kset_register(&ip_hw_id->hw_id_kset);
  				if (res) {
  					DRM_ERROR("Couldn't register ip_hw_id kset");
-					kfree(ip_hw_id);
+					kset_put(&ip_hw_id->hw_id_kset);
  					return res;
  				}
  				if (hw_id_names[ii]) {
@@ -954,7 +954,7 @@ static int amdgpu_discovery_sysfs_recurse(struct amdgpu_device *adev)
  		res = kset_register(&ip_die_entry->ip_kset);
  		if (res) {
  			DRM_ERROR("Couldn't register ip_die_entry kset");
-			kfree(ip_die_entry);
+			kset_put(&ip_die_entry->ip_kset);
  			return res;
  		}
 

drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev) · line 995

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +1 -0
@@ -989,6 +989,7 @@ static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev)
  	res = kset_register(&adev->ip_top->die_kset);
  	if (res) {
  		DRM_ERROR("Couldn't register die_kset");
+		kset_put(&adev->ip_top->die_kset);
  		goto Err;
  	}
 

.