Skip to contentSkip to conversation

spi: fix NULL pointer dereference for board info devices

PATCH Applied Ahmad Fatoum 3 participants 8 messages Nov 19 – Nov 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/6 spi: fix NULL pointer dereference for board info devices

Devices registered with spi_register_board_info() have no device node, but spi_new_device() unconditionally stores the new device in it.

The at91sam9m10g45ek board, which has the Atmel SPI driver enabled in its defconfig, still registers its devices this way. Let's add an of_node_set_dev() helper that only sets the node's device if there is a node and use it here.

Fixes cd23b6facf33 ("common: add initial barebox deep-probe support")
---
 drivers/spi/spi.c | 2 +-
 include/of.h      | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
drivers/spi/spi.c +1 -1
index b64e20664985..53bcd22b8c56 100644
@@ -125,7 +125,7 @@ struct spi_device *spi_new_device(struct spi_controller *ctrl,
 	if (status)
 		goto fail;
 
-	chip->device_node->dev = &proxy->dev;
+	of_node_set_dev(chip->device_node, &proxy->dev);
 
 	return proxy;
 fail:
include/of.h +6 -0
index 07d435f91475..993315247bf0 100644
@@ -134,6 +134,12 @@ static inline struct device_node *of_node_get(struct device_node *node)
 static inline void of_node_put(struct device_node *node) { }
 DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))
 
+static inline void of_node_set_dev(struct device_node *np, struct device *dev)
+{
+	if (np)
+		np->dev = dev;
+}
+
 void of_print_property(const void *data, int len);
 void of_print_cmdline(struct device_node *root);
 
Signature · 1 line
2.47.3
#1 lore raw

PATCH 2/6 driver: pass the requested node to the rescan hook

of_device_create_on_demand() rescans the parent device, so a bus like I2C or SPI registers the device for the requested node. The hooks register every child that has no device yet though, not only the one asked for.

Let's pass the node along, so the hooks can restrict themselves to it. NULL still means all children. The I2C and SPI hooks ignore the node for now, so there is no functional change.

---
 drivers/i2c/i2c.c     | 2 +-
 drivers/of/platform.c | 4 ++--
 drivers/spi/spi.c     | 2 +-
 include/device.h      | 5 +++--
 include/driver.h      | 4 ++--
 5 files changed, 9 insertions(+), 8 deletions(-)
drivers/i2c/i2c.c +1 -1
index 407c4db8a76a..cfc340736309 100644
@@ -485,7 +485,7 @@ int of_i2c_register_devices_by_node(struct device_node *node)
 	return 0;
 }
 
-static void i2c_hw_rescan(struct device *dev)
+static void i2c_hw_rescan(struct device *dev, struct device_node *np)
 {
 	struct i2c_adapter *adap;
 
drivers/of/platform.c +2 -2
index 0346d87a2b61..12a1b759fc07 100644
@@ -138,7 +138,7 @@ struct device *of_platform_device_create(struct device_node *np,
 	 * devices.
 	 */
 	if (np->dev) {
-		device_rescan(np->dev);
+		device_rescan(np->dev, NULL);
 		return np->dev;
 	}
 
@@ -450,7 +450,7 @@ static struct device *of_device_create_on_demand(struct device_node *np)
 		return NULL;
 
 	if (!np->dev && parent->dev)
-		device_rescan(parent->dev);
+		device_rescan(parent->dev, np);
 
 	/* Create all parent devices needed for the requested device */
 	parent_dev = parent->dev ? : of_device_create_on_demand(parent);
drivers/spi/spi.c +1 -1
index 53bcd22b8c56..b1806a2ea445 100644
@@ -199,7 +199,7 @@ static void spi_of_register_slaves(struct spi_controller *ctrl)
 	}
 }
 
-static void spi_controller_rescan(struct device *dev)
+static void spi_controller_rescan(struct device *dev, struct device_node *np)
 {
 	struct spi_controller *ctrl;
 
include/device.h +3 -2
index 7abeed9c460f..34b45a9bb014 100644
@@ -59,7 +59,8 @@ struct of_device_id;
  * @dma_offset: DMA offset.
  * @detect: For devices which take longer to probe, this is called when the driver
  *          should actually detect client devices.
- * @rescan: Callback to rescan the device.
+ * @rescan: Callback to rescan the device. If @np is not NULL, only the
+ *          device for that child node needs to be created.
  * @deferred_probe_reason: If a driver probe is deferred, this stores the last error.
  */
 struct device {
@@ -119,7 +120,7 @@ struct device {
 #endif
 
 	int (*detect)(struct device *);
-	void (*rescan)(struct device *);
+	void (*rescan)(struct device *dev, struct device_node *np);
 
 	char *deferred_probe_reason;
 };
include/driver.h +2 -2
index a93da2e06f62..ec1f1ccfb85e 100644
@@ -98,10 +98,10 @@ static inline void put_device(struct device *dev) {}
 void free_device_res(struct device *dev);
 void free_device(struct device *dev);
 
-static inline void device_rescan(struct device *dev)
+static inline void device_rescan(struct device *dev, struct device_node *np)
 {
 	if (dev->rescan)
-		dev->rescan(dev);
+		dev->rescan(dev, np);
 }
 
 /* Iterate over a devices children
Signature · 1 line
2.47.3
#2 lore raw

PATCH 3/6 i2c: factor out of_i2c_register_device()

In reply to Ahmad Fatoum

Let's move the registration of a single device node into its own function. A follow-up commit will use it to register only the node requested by the rescan hook.

No functional change.

---
 drivers/i2c/i2c.c | 72 +++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 34 deletions(-)
drivers/i2c/i2c.c +38 -34
index cfc340736309..ef92854eefe0 100644
@@ -427,6 +427,42 @@ void i2c_unregister_device(struct i2c_client *client)
 	devinfo_del(&client->dev, i2c_info);
 }
 
+static void of_i2c_register_device(struct i2c_adapter *adap,
+				   struct device_node *n)
+{
+	struct i2c_board_info info = {};
+	struct i2c_client *result;
+	const __be32 *addr;
+	int len;
+
+	if (n->dev) {
+		dev_dbg(&adap->dev, "of_i2c: skipping already registered %s\n",
+			dev_name(n->dev));
+		return;
+	}
+
+	of_modalias_node(n, info.type, I2C_NAME_SIZE);
+
+	info.of_node = n;
+
+	addr = of_get_property(n, "reg", &len);
+	if (!addr || (len < sizeof(int))) {
+		dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", n);
+		return;
+	}
+
+	info.addr = be32_to_cpup(addr);
+	if (info.addr > (1 << 10) - 1) {
+		dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n",
+			info.addr, n);
+		return;
+	}
+
+	result = i2c_new_device(adap, &info);
+	if (!result)
+		dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", n);
+}
+
 static void of_i2c_register_devices(struct i2c_adapter *adap)
 {
 	struct device_node *n;
@@ -435,40 +471,8 @@ static void of_i2c_register_devices(struct i2c_adapter *adap)
 	if (!IS_ENABLED(CONFIG_OFDEVICE) || !adap->dev.of_node)
 		return;
 
-	for_each_available_child_of_node(adap->dev.of_node, n) {
-		struct i2c_board_info info = {};
-		struct i2c_client *result;
-		const __be32 *addr;
-		int len;
-
-		if (n->dev) {
-			dev_dbg(&adap->dev, "of_i2c: skipping already registered %s\n",
-				dev_name(n->dev));
-			continue;
-		}
-
-		of_modalias_node(n, info.type, I2C_NAME_SIZE);
-
-		info.of_node = n;
-
-		addr = of_get_property(n, "reg", &len);
-		if (!addr || (len < sizeof(int))) {
-			dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", n);
-			continue;
-		}
-
-		info.addr = be32_to_cpup(addr);
-		if (info.addr > (1 << 10) - 1) {
-			dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n",
-				info.addr, n);
-			continue;
-		}
-
-		result = i2c_new_device(adap, &info);
-		if (!result)
-			dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n",
-			        n);
-	}
+	for_each_available_child_of_node(adap->dev.of_node, n)
+		of_i2c_register_device(adap, n);
 }
 
 int of_i2c_register_devices_by_node(struct device_node *node)
Signature · 1 line
2.47.3
#3 lore raw

PATCH 4/6 i2c: allow creating siblings on demand while populating the bus

In reply to Ahmad Fatoum

With deep probe, a device probed while its adapter populates the bus may depend on a sibling that comes later in the device tree, e.g. a GPIO expander whose vcc-supply is a PMIC regulator. To create the PMIC, of_device_create_on_demand() rescans the controller, but the rescan hook is only installed after population. So the PMIC node gets a platform device without a driver instead, the bus then skips it as already registered and the PMIC never probes.

Install the hook before populating the bus. Alone, that makes the nested rescan register the expander again, recursing until the stack overflows, because its node only gets its device after probe. So set it before registering the client, like platform devices already do.

The nested rescan must also register only the requested node. Otherwise, all later siblings are probed while the expander is still probing, and one using the expander's GPIOs fails to get them.

---
 drivers/i2c/i2c.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/i2c/i2c.c +16 -9
index ef92854eefe0..d0f9be826025 100644
@@ -405,16 +405,17 @@ static struct i2c_client *i2c_new_device(struct i2c_adapter *adapter,
 
 	client->dev.parent = &adapter->dev;
 
+	/* claim the node before probe can rescan the bus */
+	of_node_set_dev(chip->of_node, &client->dev);
+
 	status = register_device(&client->dev);
 	if (status) {
+		of_node_set_dev(chip->of_node, NULL);
 		free(client);
 		return NULL;
 	}
 	devinfo_add(&client->dev, i2c_info);
 
-	if (chip->of_node)
-		chip->of_node->dev = &client->dev;
-
 	dev_dbg(&client->dev, "registered on bus %d, chip->addr 0x%02x\n",
 		adapter->nr, client->addr);
 
@@ -496,7 +497,12 @@ static void i2c_hw_rescan(struct device *dev, struct device_node *np)
 	for_each_i2c_adapter(adap) {
 		if (dev != adap->dev.parent)
 			continue;
-		of_i2c_register_devices(adap);
+
+		if (!np)
+			of_i2c_register_devices(adap);
+		else if (np->parent == adap->dev.of_node &&
+			 of_device_is_available(np))
+			of_i2c_register_device(adap, np);
 		break;
 	}
 }
@@ -742,17 +748,18 @@ int i2c_add_numbered_adapter(struct i2c_adapter *adapter)
 
 	slice_init(&adapter->slice, dev_name(&adapter->dev));
 
-	/* populate children from any i2c device tables */
-	scan_boardinfo(adapter);
-
-	of_i2c_register_devices(adapter);
-
+	/* children probed below may need their siblings created on demand */
 	hw_dev = adapter->dev.parent;
 	if (hw_dev && dev_of_node(hw_dev)) {
 		if (!hw_dev->rescan)
 			hw_dev->rescan = i2c_hw_rescan;
 	}
 
+	/* populate children from any i2c device tables */
+	scan_boardinfo(adapter);
+
+	of_i2c_register_devices(adapter);
+
 	return 0;
 }
 EXPORT_SYMBOL(i2c_add_numbered_adapter);
Signature · 1 line
2.47.3
Tested-by on v1 4/6
#4 lore raw

Re: [PATCH 4/6] i2c: allow creating siblings on demand while populating the bus

чт, 19 нояб. 2026 г. в 21:58, Ahmad Fatoum <[email protected]>:

Quoted 93 lines

With deep probe, a device probed while its adapter populates the bus may depend on a sibling that comes later in the device tree, e.g. a GPIO expander whose vcc-supply is a PMIC regulator. To create the PMIC, of_device_create_on_demand() rescans the controller, but the rescan hook is only installed after population. So the PMIC node gets a platform device without a driver instead, the bus then skips it as already registered and the PMIC never probes.

Install the hook before populating the bus. Alone, that makes the nested rescan register the expander again, recursing until the stack overflows, because its node only gets its device after probe. So set it before registering the client, like platform devices already do.

The nested rescan must also register only the requested node. Otherwise, all later siblings are probed while the expander is still probing, and one using the expander's GPIOs fails to get them.

---
 drivers/i2c/i2c.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/i2c/i2c.c +16 -10
index ef92854eefe0..d0f9be826025 100644
@@ -405,16 +405,17 @@ static struct i2c_client *i2c_new_device(struct i2c_adapter *adapter,
 
        client->dev.parent = &adapter->dev;
 
+       /* claim the node before probe can rescan the bus */
+       of_node_set_dev(chip->of_node, &client->dev);
+
        status = register_device(&client->dev);
        if (status) {
+               of_node_set_dev(chip->of_node, NULL);
                free(client);
                return NULL;
        }
        devinfo_add(&client->dev, i2c_info);
 
-       if (chip->of_node)
-               chip->of_node->dev = &client->dev;
-
        dev_dbg(&client->dev, "registered on bus %d, chip->addr 0x%02x\n",
                adapter->nr, client->addr);
 
@@ -496,7 +497,12 @@ static void i2c_hw_rescan(struct device *dev, struct device_node *np)
        for_each_i2c_adapter(adap) {
                if (dev != adap->dev.parent)
                        continue;
-               of_i2c_register_devices(adap);
+
+               if (!np)
+                       of_i2c_register_devices(adap);
+               else if (np->parent == adap->dev.of_node &&
+                        of_device_is_available(np))
+                       of_i2c_register_device(adap, np);
                break;
        }
 }
@@ -742,17 +748,18 @@ int i2c_add_numbered_adapter(struct i2c_adapter *adapter)
 
        slice_init(&adapter->slice, dev_name(&adapter->dev));
 
-       /* populate children from any i2c device tables */
-       scan_boardinfo(adapter);
-
-       of_i2c_register_devices(adapter);
-
+       /* children probed below may need their siblings created on demand */
        hw_dev = adapter->dev.parent;
        if (hw_dev && dev_of_node(hw_dev)) {
                if (!hw_dev->rescan)
                        hw_dev->rescan = i2c_hw_rescan;
        }
 
+       /* populate children from any i2c device tables */
+       scan_boardinfo(adapter);
+
+       of_i2c_register_devices(adapter);
+
        return 0;
 }
 EXPORT_SYMBOL(i2c_add_numbered_adapter);
--

2.47.3

#5 lore raw

PATCH 5/6 spi: factor out spi_of_register_slave()

In reply to Ahmad Fatoum

Let's move the registration of a single device node into its own function. A follow-up commit will use it to register only the node requested by the rescan hook.

No functional change.

---
 drivers/spi/spi.c | 77 +++++++++++++++++++++++++----------------------
 1 file changed, 41 insertions(+), 36 deletions(-)
drivers/spi/spi.c +41 -36
index b1806a2ea445..850ea2c7b62f 100644
@@ -150,10 +150,48 @@ static void of_spi_parse_dt_cs_delay(struct device_node *nc,
 	}
 }
 
+static void spi_of_register_slave(struct spi_controller *ctrl,
+				  struct device_node *n)
+{
+	struct spi_board_info chip = {};
+	struct property *reg;
+
+	if (n->dev) {
+		dev_dbg(ctrl->dev, "skipping already registered %s\n",
+			dev_name(n->dev));
+		return;
+	}
+
+	chip.name = xstrdup(n->name);
+	chip.bus_num = ctrl->bus_num;
+	/* Mode (clock phase/polarity/etc.) */
+	if (of_property_read_bool(n, "spi-cpha"))
+		chip.mode |= SPI_CPHA;
+	if (of_property_read_bool(n, "spi-cpol"))
+		chip.mode |= SPI_CPOL;
+	if (of_property_read_bool(n, "spi-cs-high"))
+		chip.mode |= SPI_CS_HIGH;
+	if (of_property_read_bool(n, "spi-3wire"))
+		chip.mode |= SPI_3WIRE;
+	of_property_read_u32(n, "spi-max-frequency",
+			&chip.max_speed_hz);
+
+	/* Device CS delays */
+	of_spi_parse_dt_cs_delay(n, &chip.cs_setup, "spi-cs-setup-delay-ns");
+	of_spi_parse_dt_cs_delay(n, &chip.cs_hold, "spi-cs-hold-delay-ns");
+	of_spi_parse_dt_cs_delay(n, &chip.cs_inactive, "spi-cs-inactive-delay-ns");
+
+	reg = of_find_property(n, "reg", NULL);
+	if (!reg)
+		return;
+	chip.chip_select = of_read_number(reg->value, 1);
+	chip.device_node = n;
+	spi_new_device(ctrl, &chip);
+}
+
 static void spi_of_register_slaves(struct spi_controller *ctrl)
 {
 	struct device_node *n;
-	struct property *reg;
 	struct device_node *node = ctrl->dev->of_node;
 
 	if (!IS_ENABLED(CONFIG_OFDEVICE))
@@ -162,41 +200,8 @@ static void spi_of_register_slaves(struct spi_controller *ctrl)
 	if (!node)
 		return;
 
-	for_each_available_child_of_node(node, n) {
-		struct spi_board_info chip = {};
-
-		if (n->dev) {
-			dev_dbg(ctrl->dev, "skipping already registered %s\n",
-				dev_name(n->dev));
-			continue;
-		}
-
-		chip.name = xstrdup(n->name);
-		chip.bus_num = ctrl->bus_num;
-		/* Mode (clock phase/polarity/etc.) */
-		if (of_property_read_bool(n, "spi-cpha"))
-			chip.mode |= SPI_CPHA;
-		if (of_property_read_bool(n, "spi-cpol"))
-			chip.mode |= SPI_CPOL;
-		if (of_property_read_bool(n, "spi-cs-high"))
-			chip.mode |= SPI_CS_HIGH;
-		if (of_property_read_bool(n, "spi-3wire"))
-			chip.mode |= SPI_3WIRE;
-		of_property_read_u32(n, "spi-max-frequency",
-				&chip.max_speed_hz);
-
-		/* Device CS delays */
-		of_spi_parse_dt_cs_delay(n, &chip.cs_setup, "spi-cs-setup-delay-ns");
-		of_spi_parse_dt_cs_delay(n, &chip.cs_hold, "spi-cs-hold-delay-ns");
-		of_spi_parse_dt_cs_delay(n, &chip.cs_inactive, "spi-cs-inactive-delay-ns");
-
-		reg = of_find_property(n, "reg", NULL);
-		if (!reg)
-			continue;
-		chip.chip_select = of_read_number(reg->value, 1);
-		chip.device_node = n;
-		spi_new_device(ctrl, &chip);
-	}
+	for_each_available_child_of_node(node, n)
+		spi_of_register_slave(ctrl, n);
 }
 
 static void spi_controller_rescan(struct device *dev, struct device_node *np)
Signature · 1 line
2.47.3
#6 lore raw

PATCH 6/6 spi: allow creating siblings on demand while populating the bus

In reply to Ahmad Fatoum

SPI orders things like I2C: the rescan hook is installed only after the controller registered its devices, and a device's node only gets its device after probe. A device that needs a later sibling while probing thus gets that sibling created as a platform device instead of an SPI device.

Fix it the same way as for I2C, including that the nested rescan only registers the requested node.

Fixes 2366fa3a808e ("spi: implement rescan")
---
 drivers/spi/spi.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/spi/spi.c +18 -10
index 850ea2c7b62f..15b654b750db 100644
@@ -121,12 +121,15 @@ struct spi_device *spi_new_device(struct spi_controller *ctrl,
 	if (status)
 		goto fail;
 
-	status = register_device(&proxy->dev);
-	if (status)
-		goto fail;
-
+	/* claim the node before probe can rescan the bus */
 	of_node_set_dev(chip->device_node, &proxy->dev);
 
+	status = register_device(&proxy->dev);
+	if (status) {
+		of_node_set_dev(chip->device_node, NULL);
+		goto fail;
+	}
+
 	return proxy;
 fail:
 	free(proxy);
@@ -211,7 +214,12 @@ static void spi_controller_rescan(struct device *dev, struct device_node *np)
 	list_for_each_entry(ctrl, &spi_controller_list, list) {
 		if (ctrl->dev != dev)
 			continue;
-		spi_of_register_slaves(ctrl);
+
+		if (!np)
+			spi_of_register_slaves(ctrl);
+		else if (np->parent == ctrl->dev->of_node &&
+			 of_device_is_available(np))
+			spi_of_register_slave(ctrl, np);
 	}
 }
 
@@ -579,16 +587,16 @@ int spi_register_controller(struct spi_controller *ctrl)
 
 	list_add_tail(&ctrl->list, &spi_controller_list);
 
+	/* children probed below may need their siblings created on demand */
+	if (!ctrl->dev->rescan)
+		ctrl->dev->rescan = spi_controller_rescan;
+
 	spi_of_register_slaves(ctrl);
 
 	/* populate children from any spi device tables */
 	scan_boardinfo(ctrl);
-	status = 0;
 
-	if (!ctrl->dev->rescan)
-		ctrl->dev->rescan = spi_controller_rescan;
-
-	return status;
+	return 0;
 }
 EXPORT_SYMBOL(spi_register_controller);
 
Signature · 1 line
2.47.3
Applied
#7 lore raw
In reply to Ahmad Fatoum
Quoted 9 lines · Ahmad Fatoum

Devices registered with spi_register_board_info() have no device node, but spi_new_device() unconditionally stores the new device in it.

The at91sam9m10g45ek board, which has the Atmel SPI driver enabled in its defconfig, still registers its devices this way. Let's add an of_node_set_dev() helper that only sets the node's device if there is a node and use it here.

[...]

Applied, thanks!

[1/6] spi: fix NULL pointer dereference for board info devices
https://git.pengutronix.de/cgit/barebox/commit/?id=cf00a1ea1bfc (link may not be stable)
[2/6] driver: pass the requested node to the rescan hook
https://git.pengutronix.de/cgit/barebox/commit/?id=22d993692cd0 (link may not be stable)
[3/6] i2c: factor out of_i2c_register_device()

      https://git.pengutronix.de/cgit/barebox/commit/?id=91246097b638 (link may not be stable)
[4/6] i2c: allow creating siblings on demand while populating the bus
      https://git.pengutronix.de/cgit/barebox/commit/?id=6433a89b6339 (link may not be stable)
[5/6] spi: factor out spi_of_register_slave()
      https://git.pengutronix.de/cgit/barebox/commit/?id=b09871d81629 (link may not be stable)

[6/6] spi: allow creating siblings on demand while populating the bus
https://git.pengutronix.de/cgit/barebox/commit/?id=940243e14d3b (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>