Re: [PATCH] usb: gadget: f_hid: fix device reference leak in hidg_alloc()

From: Johan Hovold

Date: Mon Apr 13 2026 - 03:40:21 EST


On Mon, Apr 13, 2026 at 12:15:55AM +0800, Guangshuo Li wrote:
> hidg_alloc() initializes hidg->dev with device_initialize() before
> calling dev_set_name(). If dev_set_name() fails, the function currently
> jumps to err_unlock and returns without calling put_device().
>
> This leaves the device reference unbalanced and prevents hidg_release()
> from being called. Calling put_device() here is also safe, since
> hidg_release() only frees resources owned by hidg.

Good catch.

> Route the dev_set_name() failure path through err_put_device so the
> device reference is dropped properly.
>
> Fixes: 944fe915d00d ("usb: gadget: f_hid: tidy error handling in hidg_alloc")

This isn't the commit that introduced the issue, though. This should be:

Fixes: 89ff3dfac604 ("usb: gadget: f_hid: fix f_hidg lifetime vs cdev")

With that fixed you can add my:

Reviewed-by: Johan Hovold <johan@xxxxxxxxxx>

Johan