Re: [PATCH v2 2/2] PCI: Free the device name on the pci_setup_device() error path

From: David Jeffery

Date: Fri Aug 07 2026 - 15:45:48 EST


On Sun, Jul 26, 2026 at 5:32 AM Rihyeon Kim <rihyeon8648@xxxxxxxxx> wrote:
>
> pci_setup_device() sets the device name with dev_set_name(), which
> allocates it. When the header type turns out to be unknown the function
> returns -EIO, and pci_scan_device() then releases the device with kfree()
> rather than put_device(), because the device has not been initialized with
> device_initialize() yet. The name is never freed, so it is leaked.
>
> Free it there and clear the pointer, so the freed name is not left behind
> in the kobject.
>
> Yang Yingliang reported the same leak in 2022.
>
> Fixes: 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
> Suggested-by: Yang Yingliang <yangyingliang@xxxxxxxxxx>
> Link: https://lore.kernel.org/linux-pci/20220817084444.2876059-1-yangyingliang@xxxxxxxxxx/
> Assisted-by: Claude:claude-opus-5 sparse
> Signed-off-by: Rihyeon Kim <rihyeon8648@xxxxxxxxx>
> ---
> v2: new patch, picked up from Yang Yingliang's 2022 posting as suggested in
> review, with the NULL assignment added.
>
> drivers/pci/probe.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
> index 3e6f0eb7d456..74d84093943b 100644
> --- a/drivers/pci/probe.c
> +++ b/drivers/pci/probe.c
> @@ -2192,6 +2192,8 @@ int pci_setup_device(struct pci_dev *dev)
> default: /* unknown header */
> pci_err(dev, "unknown header type %02x, ignoring device\n",
> dev->hdr_type);
> + kfree_const(dev->dev.kobj.name);
> + dev->dev.kobj.name = NULL;
> pci_release_of_node(dev);
> return -EIO;
>
> --
> 2.43.0
>

A solid, simple fix.

Reviewed-by: David Jeffery <djeffery@xxxxxxxxxx>