drm/amdgpu: Fix error handling in amdgpu_xcp_cfg_sysfs_init()
Subsystems (2)
PATCH 1/1 drm/amdgpu: Fix error handling in amdgpu_xcp_cfg_sysfs_init()
Once kobject_init_and_add() fails for an XCP resource kobject, we should call kobject_put() to decrement the reference count for cleanup. Otherwise, it could cause a memory leak.
The error handling loop also uses xcp_res[i] instead of xcp_res[j], so it fails to put the previously added resource kobjects and may put the failed kobject more than once.
Fix this by putting the failed resource kobject before jumping to the error path, and by using the correct loop index when putting the previously added resource kobjects.
Found by code review.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +4 -2
@@ -948,15 +948,17 @@ static void amdgpu_xcp_cfg_sysfs_init(struct amdgpu_device *adev) &xcp_cfg_res_sysfs_ktype, &xcp_cfg->kobj,"%s" , xcp_res_names[rid]); -if (r) +if (r) { +kobject_put (&xcp_res->kobj); goto err; + } } adev->xcp_mgr->xcp_cfg = xcp_cfg;return ; err:for (j =0 ; j < i; j++) { - xcp_res = &xcp_cfg->xcp_res[i]; + xcp_res = &xcp_cfg->xcp_res[j];kobject_put (&xcp_res->kobj); }
Signature · 1 line
2.43.0
Quoted 33 lines · Guangshuo Li
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Once
kobject_init_and_add()fails for an XCP resource kobject, we should callkobject_put()to decrement the reference count for cleanup. Otherwise, it could cause a memory leak.The error handling loop also uses xcp_res[i] instead of xcp_res[j], so it fails to put the previously added resource kobjects and may put the failed kobject more than once.
Fix this by putting the failed resource kobject before jumping to the error path, and by using the correct loop index when putting the previously added resource kobjects.
Found by code review.
Fixes 4ae86dc87850 ("drm/amdgpu: Add sysfs nodes to get xcp details")Signed-off-by Guangshuo Li <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +2
-1@@ -948,15 +948,17 @@ static void amdgpu_xcp_cfg_sysfs_init(struct amdgpu_device *adev) &xcp_cfg_res_sysfs_ktype, &xcp_cfg->kobj,"%s" , xcp_res_names[rid]); -if (r) +if (r) { +kobject_put (&xcp_res->kobj);
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c static void amdgpu_xcp_cfg_sysfs_init(struct amdgpu_device *adev) · line 961
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +2
-1goto err; + } } adev->xcp_mgr->xcp_cfg = xcp_cfg;return ; err:for (j =0 ; j < i; j++) { - xcp_res = &xcp_cfg->xcp_res[i]; + xcp_res = &xcp_cfg->xcp_res[j];
Good catch. What about just keeping it j <= i? Seeing only one path to get to this error handling.
Thanks,
Lijo
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +0
-1kobject_put (&xcp_res->kobj); } --2.43.0
Christian König
maintainerQuoted 40 lines · Lijo
Quoted 37 lines · Guangshuo Li
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Once
kobject_init_and_add()fails for an XCP resource kobject, we should callkobject_put()to decrement the reference count for cleanup. Otherwise, it could cause a memory leak.The error handling loop also uses xcp_res[i] instead of xcp_res[j], so it fails to put the previously added resource kobjects and may put the failed kobject more than once.
Fix this by putting the failed resource kobject before jumping to the error path, and by using the correct loop index when putting the previously added resource kobjects.
Found by code review.
Fixes 4ae86dc87850 ("drm/amdgpu: Add sysfs nodes to get xcp details")Signed-off-by Guangshuo Li <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c| 6 ++++--1 file changed, 4 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +3
-1@@ -948,15 +948,17 @@ static void amdgpu_xcp_cfg_sysfs_init(struct amdgpu_device *adev) &xcp_cfg_res_sysfs_ktype, &xcp_cfg->kobj, "%s", xcp_res_names[rid]); - if (r) + if (r) { + kobject_put(&xcp_res->kobj); goto err; + } }
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +0
-0adev->xcp_mgr->xcp_cfg = xcp_cfg; return;err:
for (j = 0; j < i; j++) {
-xcp_res= &xcp_cfg->xcp_res[i];
+xcp_res= &xcp_cfg->xcp_res[j];Good catch. What about just keeping it j <= i? Seeing only one path to get to this error handling.
The usual idiom for cleanup in loops you will find in books is:
for (i = 0; i < N; ++i);
...
if (r) goto error;
....
error:
while (i--) cleanup(array[i]);
The while (i--) looks counter intuitive on first glance but is actually correct.
Regards,
Christian.
Quoted 11 lines
Thanks,
LijoQuoted 6 lines
kobject_put(&xcp_res->kobj);
}Signature · 1 line
2.43.0