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

David Jeffery <[email protected]>
Newsgroups org.kernel.vger.linux-pci,org.kernel.vger.linux-kernel
Message-ID <CA+-xHTE20nzFqk+vQB=+jCz7=FiC63zMJAGtjM_nqxOuS_W_ZQ@mail.gmail.com>
On Sun, Jul 26, 2026 at 5:32 AM Rihyeon Kim <[email protected]> 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 <[email protected]>
> Link: https://lore.kernel.org/linux-pci/[email protected]/
> Assisted-by: Claude:claude-opus-5 sparse
> Signed-off-by: Rihyeon Kim <[email protected]>
> ---
> 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 <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.