git: c15aab01e9ea - main - linuxkpi: Fix lkpi_i2c_del_adapter() leaving the devices behind

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

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

commit c15aab01e9ea281f5fb071866619d9e664667672
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-10-28 13:05:32 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-04 06:35:06 +0000

    linuxkpi: Fix lkpi_i2c_del_adapter() leaving the devices behind
    
    lkpi_i2c_del_adapter() looked for the lkpi_iic or lkpi_iicbb device to
    delete with device_find_child() on increasing unit numbers, and stopped at
    the first unit that does not exist.  Units are allocated globally per
    devclass, so as soon as one adapter was removed (leaving a hole) or when the
    units of a parent do not start at 0, the adapter was not found and its
    device was never deleted.  The i2c devices of a driver such as i915 then
    accumulate, with a new set of unit numbers, each time it is loaded.
    
    Walk the children of the parent instead and match on the adapter.
    
    Sponsored by:   Beckhoff Automation GmbH & Co. KG
---
 sys/compat/linuxkpi/common/src/linux_i2c.c | 56 ++++++++++++++++--------------
 1 file changed, 29 insertions(+), 27 deletions(-)

diff --git a/sys/compat/linuxkpi/common/src/linux_i2c.c b/sys/compat/linuxkpi/common/src/linux_i2c.c
index f18570202f74..c48a74c95298 100644
--- a/sys/compat/linuxkpi/common/src/linux_i2c.c
+++ b/sys/compat/linuxkpi/common/src/linux_i2c.c
@@ -342,40 +342,42 @@ lkpi_i2c_add_adapter(struct i2c_adapter *adapter)
 int
 lkpi_i2c_del_adapter(struct i2c_adapter *adapter)
 {
-	device_t child;
-	int unit, rv;
+	device_t parent, *children, child;
+	const char *name;
+	int count, i, rv;
 
 	if (adapter == NULL)
 		return (-EINVAL);
+	parent = adapter->dev.parent->bsddev;
 	if (bootverbose)
-		device_printf(adapter->dev.parent->bsddev,
-		    "Removing i2c adapter %s\n", adapter->name);
+		device_printf(parent, "Removing i2c adapter %s\n",
+		    adapter->name);
 	sx_xlock(&lkpi_sx_i2c);
-	unit = 0;
-	while ((child = device_find_child(adapter->dev.parent->bsddev, "lkpi_iic", unit++)) != NULL) {
-
-		if (adapter == LKPI_IIC_GET_ADAPTER(child)) {
-			bus_topo_lock();
-			device_delete_child(adapter->dev.parent->bsddev, child);
-			bus_topo_unlock();
-			rv = 0;
-			goto out;
-		}
-	}
-
-	unit = 0;
-	while ((child = device_find_child(adapter->dev.parent->bsddev, "lkpi_iicbb", unit++)) != NULL) {
-
-		if (adapter == LKPI_IIC_GET_ADAPTER(child)) {
-			bus_topo_lock();
-			device_delete_child(adapter->dev.parent->bsddev, child);
-			bus_topo_unlock();
-			rv = 0;
-			goto out;
+	/*
+	 * Look at every child: the units of the lkpi_iic and lkpi_iicbb
+	 * devices are not contiguous once other adapters have been removed,
+	 * so searching by increasing unit until the first hole would miss
+	 * the device and leave it behind.
+	 */
+	rv = -EINVAL;
+	bus_topo_lock();
+	if (device_get_children(parent, &children, &count) == 0) {
+		for (i = 0; i < count; i++) {
+			child = children[i];
+			name = device_get_name(child);
+			if (name == NULL ||
+			    (strcmp(name, "lkpi_iic") != 0 &&
+			    strcmp(name, "lkpi_iicbb") != 0))
+				continue;
+			if (adapter == LKPI_IIC_GET_ADAPTER(child)) {
+				device_delete_child(parent, child);
+				rv = 0;
+				break;
+			}
 		}
+		free(children, M_TEMP);
 	}
-	rv = -EINVAL;
-out:
+	bus_topo_unlock();
 	sx_xunlock(&lkpi_sx_i2c);
 	return (rv);
 }