Skip to contentSkip to conversation

i2c: sun6i-p2wi: Fix device node reference leak in p2wi_probe()

PATCH In review Felix Gu 3 participants 3 messages Jan 31 – May 1

Subsystems (3)
lore
Share & tools
lore

Felix Gu

author
#0 lore raw

PATCH 1/1 i2c: sun6i-p2wi: Fix device node reference leak in p2wi_probe()

Use the __free(device_node) scoped variable to automatically release the device node reference, preventing a device node reference leak.

Fixes 3e833490fae5 ("i2c: sunxi: add P2WI (Push/Pull 2 Wire Interface) controller support")
---
 drivers/i2c/busses/i2c-sun6i-p2wi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-sun6i-p2wi.c +2 -2
index fb5280b8cf7f..652b37b57159 100644
@@ -184,7 +184,6 @@ static int p2wi_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct device_node *np = dev->of_node;
-	struct device_node *childnp;
 	unsigned long parent_clk_freq;
 	u32 clk_freq = I2C_MAX_STANDARD_MODE_FREQ;
 	struct p2wi *p2wi;
@@ -223,7 +222,8 @@ static int p2wi_probe(struct platform_device *pdev)
 	 * In this case the target_addr is set to -1 and won't be checked when
 	 * launching a P2WI transfer.
 	 */
-	childnp = of_get_next_available_child(np, NULL);
+	struct device_node *childnp __free(device_node) =
+		of_get_next_available_child(np, NULL);
 	if (childnp) {
 		ret = of_property_read_u32(childnp, "reg", &target_addr);
 		if (ret) {

---
base-commit: 33a647c659ffa5bdb94abc345c8c86768ff96215
change-id: 20260201-p2wi-a199cdfe69e0

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
maintainer
#1 lore raw

Hi Felix,

Quoted 22 lines · Felix Gu

Use the __free(device_node) scoped variable to automatically release the device node reference, preventing a device node reference leak.

Fixes 3e833490fae5 ("i2c: sunxi: add P2WI (Push/Pull 2 Wire Interface) controller support")
---
 drivers/i2c/busses/i2c-sun6i-p2wi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-sun6i-p2wi.c +0 -1
index fb5280b8cf7f..652b37b57159 100644
@@ -184,7 +184,6 @@ static int p2wi_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct device_node *np = dev->of_node;
-	struct device_node *childnp;
 	unsigned long parent_clk_freq;
 	u32 clk_freq = I2C_MAX_STANDARD_MODE_FREQ;
 	struct p2wi *p2wi;

drivers/i2c/busses/i2c-sun6i-p2wi.c static int p2wi_probe(struct platform_device *pdev) · line 226

Quoted 6 lines
drivers/i2c/busses/i2c-sun6i-p2wi.c +2 -1
@@ -223,7 +222,8 @@ static int p2wi_probe(struct platform_device *pdev)
 	 * In this case the target_addr is set to -1 and won't be checked when
 	 * launching a P2WI transfer.
 	 */
-	childnp = of_get_next_available_child(np, NULL);
+	struct device_node *childnp __free(device_node) =
+		of_get_next_available_child(np, NULL);

A few spontaneous questions here:

- Why are you moving the declaration of childnp in the middle of the function?

- Have you run checkpatch.pl before sending the patch?

- Is of_get_next_available_child() allocating the device_node?

- Have you tested the patch?

Thanks,
Andi

Quoted 12 lines
drivers/i2c/busses/i2c-sun6i-p2wi.c +0 -1
 	if (childnp) {
 		ret = of_property_read_u32(childnp, "reg", &target_addr);
 		if (ret) {
 
---

base-commit: 33a647c659ffa5bdb94abc345c8c86768ff96215
change-id: 20260201-p2wi-a199cdfe69e0

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
#2 lore raw

PATCH v2 1/1 i2c: sun6i-p2wi: fix of_node reference leak in probe

In reply to Felix Gu

of_get_next_available_child() returns a device_node pointer with an incremented reference count. The reference taken in p2wi_probe() for the optional child node was dropped on neither the early return when the "reg" property is missing/invalid nor on the success path, so a reference is leaked once on every successful probe and twice on every failed one.

Use the scoped __free(device_node) cleanup helper at the point of acquisition so the reference is dropped automatically on every exit path.

Fixes 3e833490fae5 ("i2c: sunxi: add P2WI (Push/Pull 2 Wire Interface) controller support")

---
Changes since v1 (Felix Gu, https://lore.kernel.org/linux-i2c/[email protected]/):
- Reword the commit message to make explicit that the leak is of an
of_node *reference*, not of an allocation.
- checkpatch --strict --codespell: clean.
- Build-tested: x86_64 allmodconfig (CONFIG_I2C_SUN6I_P2WI=m via
COMPILE_TEST) and arm sunxi_defconfig. No new warnings.
- Runtime test: not performed; no Allwinner A31 hardware available.

 drivers/i2c/busses/i2c-sun6i-p2wi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-sun6i-p2wi.c +2 -2
index fb5280b8cf7f..652b37b57159 100644
@@ -184,7 +184,6 @@ static int p2wi_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct device_node *np = dev->of_node;
-	struct device_node *childnp;
 	unsigned long parent_clk_freq;
 	u32 clk_freq = I2C_MAX_STANDARD_MODE_FREQ;
 	struct p2wi *p2wi;
@@ -223,7 +222,8 @@ static int p2wi_probe(struct platform_device *pdev)
 	 * In this case the target_addr is set to -1 and won't be checked when
 	 * launching a P2WI transfer.
 	 */
-	childnp = of_get_next_available_child(np, NULL);
+	struct device_node *childnp __free(device_node) =
+		of_get_next_available_child(np, NULL);
 	if (childnp) {
 		ret = of_property_read_u32(childnp, "reg", &target_addr);
 		if (ret) {
Signature · 1 line
2.25.1