git: ee2f66a74d18 - main - linuxkpi: Release devres when the probe of a PCI driver fails

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 07:42:09 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=ee2f66a74d1850eea8deeae3cb2c9adfe08de49f

commit ee2f66a74d1850eea8deeae3cb2c9adfe08de49f
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-11-05 09:28:42 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-18 07:42:02 +0000

    linuxkpi: Release devres when the probe of a PCI driver fails
    
    When the probe method of a driver fails, linux_pci_attach_device() only
    dropped its reference on the device.  Like on detach (see the previous
    change), the devres actions are then not run if one of them holds a
    reference on the device, which is what devm_drm_dev_alloc() does through the
    drm_device.  The device is never released, the drm_device is leaked with its
    minor, its managed memory and its debugfs entries: unloading the driver
    after a failed load (e.g. a missing firmware) printed
    "WARNING !xa_empty(&drm_minors_xa)" and leaked memory.
    
    Linux releases the devres resources of a device whose probe failed, so do the
    same.
    
    Differential Reivision: https://reviews.freebsd.org/D61223
---
 sys/compat/linuxkpi/common/src/linux_pci.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c
index a3e55c136b4a..365bd2c020b2 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -722,6 +722,14 @@ linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
 	return (0);
 
 out_err:
+	/*
+	 * As on Linux when a probe fails, release the devres actions.  Waiting
+	 * for the last reference to go away is wrong: devres actions (e.g. the
+	 * drm_dev_put() of devm_drm_dev_alloc()) may hold references on this
+	 * very device, which then never gets released, nor does the object
+	 * which holds the reference.
+	 */
+	lkpi_devres_release_free_list(&pdev->dev);
 	put_device(&pdev->dev);
 	return (-error);
 }