Skip to contentSkip to conversation

spi: bcm2835: fix device_node reference leak in bcm2835_spi_setup()

PATCH Superseded Lucas Costa 3 participants 6 messages Sep 23 – Sep 28

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

PATCH 1/1 spi: bcm2835: fix device_node reference leak in bcm2835_spi_setup()

of_find_compatible_node() returns a device_node with its reference count incremented, but the function bcm2835_spi_setup() uses the ret value in the loop to check a condition and never releases it.

Store the returned node and drop it with of_node_put() before breaking out of the loop.

Fixes e19c1272c80a ("spi: bcm2835: Restore native CS probing when pinctrl-bcm2835 is absent")
---
 drivers/spi/spi-bcm2835.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/spi/spi-bcm2835.c +5 -1
index 8f8715809c7c..8909757c94e3 100644
@@ -1226,6 +1226,7 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
 	struct bcm2835_spidev *target = spi_get_ctldata(spi);
 	struct gpiod_lookup_table *lookup __free(kfree) = NULL;
+	struct device_node *np;
 	static const char * const pinctrl_compats[] = {
 		"brcm,bcm2835-gpio",
 		"brcm,bcm2711-gpio",
@@ -1297,8 +1298,11 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 	}
 
 	for (i = 0; i < ARRAY_SIZE(pinctrl_compats); i++) {
-		if (of_find_compatible_node(NULL, NULL, pinctrl_compats[i]))
+		np = of_find_compatible_node(NULL, NULL, pinctrl_compats[i]);
+		if (np) {
+			of_node_put(np);
 			break;
+		}
 	}
 
 	if (i == ARRAY_SIZE(pinctrl_compats))
Signature · 1 line
2.43.0
#1 lore raw
Quoted 40 lines · Lucas Costa

of_find_compatible_node() returns a device_node with its reference count incremented, but the function bcm2835_spi_setup() uses the ret value in the loop to check a condition and never releases it.

Store the returned node and drop it with of_node_put() before breaking out of the loop.

Fixes e19c1272c80a ("spi: bcm2835: Restore native CS probing when pinctrl-bcm2835 is absent")
---
 drivers/spi/spi-bcm2835.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/spi/spi-bcm2835.c +5 -2
index 8f8715809c7c..8909757c94e3 100644
@@ -1226,6 +1226,7 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
 	struct bcm2835_spidev *target = spi_get_ctldata(spi);
 	struct gpiod_lookup_table *lookup __free(kfree) = NULL;
+	struct device_node *np;
 	static const char * const pinctrl_compats[] = {
 		"brcm,bcm2835-gpio",
 		"brcm,bcm2711-gpio",
@@ -1297,8 +1298,11 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 	}
 
 	for (i = 0; i < ARRAY_SIZE(pinctrl_compats); i++) {
-		if (of_find_compatible_node(NULL, NULL, pinctrl_compats[i]))
+		np = of_find_compatible_node(NULL, NULL, pinctrl_compats[i]);
+		if (np) {
+			of_node_put(np);
 			break;
+		}
 	}
 
 	if (i == ARRAY_SIZE(pinctrl_compats))
--
Quoted 3 lines

2.43.0

Just use cleanup.h like so:

	struct device_node *np __free(device_node) = of_find_compatible_node(...);

It's a oneliner this way.

Bart

#2 lore raw

PATCH v2 1/1 spi: bcm2835: fix device_node reference leak in bcm2835_spi_setup()

In reply to Lucas Costa

of_find_compatible_node() returns a device_node with its reference count incremented, but bcm2835_spi_setup() only uses the return value to test for a matching node in the loop and never releases it.

Declare the node with a scope-based __free(device_node) cleanup so the reference is dropped automatically when leaving the loop body, matching the existing __free(kfree) usage in the same function.

Fixes e19c1272c80a ("spi: bcm2835: Restore native CS probing when pinctrl-bcm2835 is absent")

---
Changes in v2:
- Use a scope-based __free(device_node) cleanup instead of an explicit of_node_put(), as suggested by the reviewer.
- v1: https://lore.kernel.org/all/[email protected]/

 drivers/spi/spi-bcm2835.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/spi-bcm2835.c +3 -1
index 8f8715809c7c..23606967f7d1 100644
@@ -1297,7 +1297,9 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 	}
 
 	for (i = 0; i < ARRAY_SIZE(pinctrl_compats); i++) {
-		if (of_find_compatible_node(NULL, NULL, pinctrl_compats[i]))
+		struct device_node *np __free(device_node) =
+			of_find_compatible_node(NULL, NULL, pinctrl_compats[i]);
+		if (np)
 			break;
 	}
 

base-commit: fe2ec83746e501645709761605c2464a44fd2929

Signature · 1 line
2.43.0
maintainer
#3 lore raw
Quoted 3 lines · Lucas Costa

of_find_compatible_node() returns a device_node with its reference count incremented, but bcm2835_spi_setup() only uses the return value to test for a matching node in the loop and never releases it.

Please don't send new patches in reply to old patches or serieses, this makes it harder for both people and tools to understand what is going on - it can bury things in mailboxes and make it difficult to keep track of what current patches are, both for the new patches and the old ones.

Reviewed-by on v2 1/1
#4 lore raw
In reply to Lucas Costa
Quoted 4 lines · Lucas Costa

of_find_compatible_node() returns a device_node with its reference count incremented, but bcm2835_spi_setup() only uses the return value to test for a matching node in the loop and never releases it.

Quoted 8 lines

Declare the node with a scope-based __free(device_node) cleanup so the reference is dropped automatically when leaving the loop body, matching the existing __free(kfree) usage in the same function.

Fixes e19c1272c80a ("spi: bcm2835: Restore native CS probing when pinctrl-bcm2835 is absent")

---

As Mark said: don't send new versions in response to older versions.

In any case:

maintainer
#5 lore raw
In reply to Lucas Costa
Quoted 1 line · Lucas Costa

spi: bcm2835: fix device_node reference leak in bcm2835_spi_setup()

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-7.4

Thanks!

[1/1] spi: bcm2835: fix device_node reference leak in bcm2835_spi_setup()

      https://git.kernel.org/broonie/spi/c/8db44cca03df

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark