Re: [PATCH] drm/amdgpu: fix IP instance memory leak on kobject add failure

From: Alex Deucher

Date: Mon Sep 21 2026 - 17:58:40 EST


Applied. Thanks!

On Mon, Sep 21, 2026 at 1:16 AM Lazar, Lijo <lijo.lazar@xxxxxxx> wrote:
>
>
>
> On 19-Sep-26 10:25 PM, Guangshuo Li wrote:
> > amdgpu_discovery_sysfs_ips() allocates ip_hw_instance with
> > kzalloc_flex() and initializes its embedded kobject before calling
> > kobject_add().
> >
> > If kobject_add() fails, the return value is ignored and execution
> > continues without dropping the initial kobject reference. The failed
> > kobject is not retained in the kset list, so the normal sysfs teardown
> > path cannot find it. As a result, ip_hw_instance_release() is never
> > called and the ip_hw_instance allocation is leaked.
> >
> > Call kobject_put() when kobject_add() fails so the initial reference is
> > dropped and ip_hw_instance_release() can free the allocation. Keep the
> > existing best-effort sysfs behavior by continuing with the remaining IP
> > entries after the failed registration.
> >
> > The issue was identified by a static analysis tool I developed and
> > confirmed by manual review.
> >
> > Fixes: a6c40b178092 ("drm/amdgpu: Show IP discovery in sysfs")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Guangshuo Li <lgs201920130244@xxxxxxxxx>
> > ---
> > drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 2 ++
> > 1 file changed, 2 insertions(+)
> >
> > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> > index a404d8aa13ee..4b1ad1c0a10b 100644
> > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c
> > @@ -1314,6 +1314,8 @@ static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev,
> > ip_hw_instance->kobj.kset = &ip_hw_id->hw_id_kset;
> > res = kobject_add(&ip_hw_instance->kobj, NULL,
> > "%d", ip_hw_instance->num_instance);
> > + if (res)
> > + kobject_put(&ip_hw_instance->kobj);
>
> Adding a message that this instance got skipped also will help.
>
> Reviewed-by: Lijo Lazar <lijo.lazar@xxxxxxx>
>
> Thanks,
> Lijo
>
> > next_ip:
> > if (reg_base_64)
> > ip_offset += struct_size(ip, base_address_64,
>