git: 812bdff114cf - main - linuxkpi: Release the "from" device in pci_get_class() and pci_get_base_class()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 18 Nov 2026 07:42:08 UTC
The branch main has been updated by manu:
URL: https://cgit.FreeBSD.org/src/commit/?id=812bdff114cf2473ab1f45e37d3a1a8742d80c64
commit 812bdff114cf2473ab1f45e37d3a1a8742d80c64
Author: Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-11-04 08:14:38 +0000
Commit: Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-18 07:42:02 +0000
linuxkpi: Release the "from" device in pci_get_class() and pci_get_base_class()
On Linux the pci_get_*() iterators drop the reference of the device passed
as "from" once the next one has been looked up, so a loop like
while ((pch = pci_get_class(class, pch)))
does not leak anything. LinuxKPI allocates a new pci_dev for every device
it returns but never released "from", so each device but the last one was
leaked together with its kobject, its name and its path name. i915 does
exactly this in intel_detect_pch() and leaked a device at every load.
Drop the reference, but only for the devices created by lkpinew_pci_dev();
the pci_dev of a device attached to a driver belongs to the bus.
Differential Revision: https://reviews.freebsd.org/D61222
---
sys/compat/linuxkpi/common/src/linux_pci.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c
index 8270f3bed54a..a3e55c136b4a 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -560,6 +560,19 @@ lkpinew_pci_dev(device_t dev)
return (pdev);
}
+/*
+ * Linux drops the reference on the "from" device of the pci_get_*() iterators
+ * once the next device has been looked up. Only do it for the devices which
+ * were created by lkpinew_pci_dev(); the other ones belong to the bus.
+ */
+static void
+lkpi_pci_put_from(struct pci_dev *from)
+{
+
+ if (from != NULL && from->dev.release == lkpinew_pci_dev_release)
+ pci_dev_put(from);
+}
+
struct pci_dev *
lkpi_pci_get_class(unsigned int class, struct pci_dev *from)
{
@@ -571,6 +584,7 @@ lkpi_pci_get_class(unsigned int class, struct pci_dev *from)
devfrom = from->dev.bsddev;
dev = pci_find_class_from(class >> 16, (class >> 8) & 0xFF, devfrom);
+ lkpi_pci_put_from(from);
if (dev == NULL)
return (NULL);
@@ -589,6 +603,7 @@ lkpi_pci_get_base_class(unsigned int baseclass, struct pci_dev *from)
devfrom = from->dev.bsddev;
dev = pci_find_base_class_from(baseclass, devfrom);
+ lkpi_pci_put_from(from);
if (dev == NULL)
return (NULL);