Re: [PATCH v4] IB/mlx4: Fix refcount leak in add_port() error path
From: Guangshuo Li
Date: Mon May 11 2026 - 08:29:51 EST
Hi Leon,
On Mon, 11 May 2026 at 19:22, Leon Romanovsky <leon@xxxxxxxxxx> wrote:
>
> On Wed, Apr 29, 2026 at 12:30:14AM +0800, Guangshuo Li wrote:
> > After kobject_init_and_add(), the lifetime of the embedded struct
> > kobject is expected to be managed through the kobject core reference
> > counting.
> >
> > In add_port(), several failure paths after kobject_init_and_add() free
> > struct mlx4_port directly instead of releasing the embedded kobject with
> > kobject_put(). This leaves the kobject reference count unbalanced and can
> > lead to incorrect lifetime handling.
>
> AFAIK, you should call to kobject_put() if kobject_init_and_add() fails,
> but in other case, you should call to kobject_del().
>
> Thanks
Thanks, I agree. I will address this in v5 by splitting the error paths:
kobject_init_and_add() failure will go through kobject_put(), while later
failures after a successful add will call kobject_del() before dropping
the references.
Thanks,
Guangshuo