Re: [PATCH v7] IB/mlx4: Fix refcount leak in add_port() error path
From: Jason Gunthorpe
Date: Mon May 25 2026 - 10:31:47 EST
On Mon, May 18, 2026 at 10:19:10AM +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(), failure paths after kobject_init_and_add() must not free
> struct mlx4_port directly, because the embedded kobject is then managed
> by the kobject core. Freeing it directly leaves the kobject reference
> counting unbalanced and can lead to incorrect lifetime handling.
>
> Allocate the pkey and gid attribute arrays before kobject_init_and_add(),
> so failures before kobject initialization can be handled by directly
> freeing the allocated memory. Once kobject_init_and_add() has been
> called, unwind later failures by removing any successfully created sysfs
> groups, calling kobject_del(), and then releasing the embedded kobject
> with kobject_put().
>
> Fixes: c1e7e466120b ("IB/mlx4: Add iov directory in sysfs under the ib device")
> Signed-off-by: Guangshuo Li <lgs201920130244@xxxxxxxxx>
> drivers/infiniband/hw/mlx4/sysfs.c | 45 ++++++++++++++++++------------
> 1 file changed, 27 insertions(+), 18 deletions(-)
Applied to for-next, thanks
Jason