git: 71d26da7e43d - main - linuxkpi: Release devres on PCI driver detach

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Wed, 04 Nov 2026 06:36:25 UTC
The branch main has been updated by manu:

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

commit 71d26da7e43db58116911e432dcb261f745e520b
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-10-28 11:17:37 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-04 06:34:04 +0000

    linuxkpi: Release devres on PCI driver detach
    
    linux_pci_detach_device() called the driver remove method and then only
    dropped its reference on the device, expecting the devres list to be
    released from lkpi_pci_dev_release() once the last reference is gone.
    
    That never happens when a devres action itself holds a reference on the
    device.  The typical case is devm_drm_dev_alloc(): the drm_device takes a
    reference on its parent and the matching drm_dev_put() is a devres
    action.  The device is thus never released, drm_dev_release() is never
    called and the DRM device and everything it owns is leaked, which makes
    unloading drm-kmod drivers fail ("failed due to N other pending
    references on the LinuxKPI device") and leaves the module unloadable or
    crashing.
    
    Linux releases the devres resources when a driver is unbound, so do the
    same: call lkpi_devres_release_free_list() right after the remove method.
    The list is empty when lkpi_pci_dev_release() runs later.
    
    Reviewed by:    bz
    Sponsored by:   Beckhoff Automation GmbH & Co. KG
---
 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 bd51082fb4d1..9509cc81fa72 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -735,6 +735,14 @@ linux_pci_detach_device(struct pci_dev *pdev)
 	if (pdev->pdrv != NULL)
 		pdev->pdrv->remove(pdev);
 
+	/*
+	 * As on Linux when a driver is unbound, release the devres actions now.
+	 * 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.
+	 */
+	lkpi_devres_release_free_list(&pdev->dev);
+
 	pci_dev_put(pdev);
 
 	/*