Skip to contentSkip to conversation

i3c: master: Fix fwnode reference leak in i3c_master_register_fwnode()

PATCH In review Wentao Liang 3 participants 3 messages Nov 8 – Nov 11 I3C

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 i3c: master: Fix fwnode reference leak in i3c_master_register_fwnode()

i3c_master_register_fwnode() takes a reference on the bus firmware node with fwnode_handle_get() and stores it in master->dev. That reference is normally dropped by i3c_masterdev_release(), which runs when the last reference on the master device is put.

If i3c_master_rpm_get() fails, the function returns before device_initialize(), so put_device() is never called, the release callback never runs, and the reference is leaked.

Release the reference explicitly on that path. of_node_put() is not enough here: it only acts on Open Firmware nodes, so the reference would still leak on ACPI and software node systems.

The candidate was found with static analysis tools; DeepSeek and GPT-6.1 Sol were used to audit it and to help prepare this fix.

Fixes 3a379bbcea0a ("i3c: Add core I3C infrastructure")

---
Changes in v2:
- Sorry for the delay. v1 used of_node_put() on a reference taken with fwnode_handle_get(), which Frank pointed out.
- Use fwnode_handle_put(dev_fwnode(&master->dev)) instead.
- Rebased on current mainline (fa4ad798f6f6), where this code lives in i3c_master_register_fwnode().
- Dropped Cc: [email protected]. This only leaks when the runtime PM resume fails during registration, so I leave the backport decision to you.
- Build-tested with an x86_64 defconfig, including W=1: drivers/i3c/ compiles without warnings and checkpatch.pl is clean. I could not make i3c_master_rpm_get() fail at runtime, so the failing path itself was not exercised on hardware.
- Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/i3c/master.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i3c/master.c +3 -1
index 01e05bad8661..c532693f8463 100644
@@ -3897,8 +3897,10 @@ int i3c_master_register_fwnode(struct i3c_master_controller *master,
 	INIT_LIST_HEAD(&master->boardinfo.i3c);
 
 	ret = i3c_master_rpm_get(master);
-	if (ret)
+	if (ret) {
+		fwnode_handle_put(dev_fwnode(&master->dev));
 		return ret;
+	}
 
 	device_initialize(&master->dev);
 
Signature · 7 lines
2.34.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] i3c: master: wrong Fixes trailer in commit message for i3c_master_rpm_get() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] i3c: master: wrong Fixes trailer in commit message for i3c_master_rpm_get()
--

commit ba01aeefaeda899682ea26dfc02330d61d63300a
Author: Wentao Liang <[email protected]>

Quoted 15 lines

i3c: master: Fix fwnode reference leak in i3c_master_register_fwnode()

i3c_master_register_fwnode() takes a reference on the bus firmware node with fwnode_handle_get() and stores it in master->dev. That reference is normally dropped by i3c_masterdev_release(), which runs when the last reference on the master device is put.

If i3c_master_rpm_get() fails, the function returns before device_initialize(), so put_device() is never called, the release callback never runs, and the reference is leaked.

Release the reference explicitly on that path. of_node_put() is not enough here: it only acts on Open Firmware nodes, so the reference would still leak on ACPI and software node systems.

Quoted 5 lines

The candidate was found with static analysis tools; DeepSeek and GPT-6.1 Sol were used to audit it and to help prepare this fix.

Fixes 3a379bbcea0a ("i3c: Add core I3C infrastructure")

[Severity: Low]
Should the Fixes: tag reference 990c149c61ee ("i3c: master: Introduce optional Runtime PM support") instead?

The original error return before device_initialize() in 3a379bbcea0a ("i3c: Add core I3C infrastructure") was fixed by 9d4f219807d5 ("i3c: fix refcount inconsistency in i3c_master_register"), while 990c149c61ee ("i3c: master: Introduce optional Runtime PM support") added the i3c_master_rpm_get() call before device_initialize().

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#2 lore raw
In reply to Wentao Liang
Quoted 42 lines · Wentao Liang

i3c_master_register_fwnode() takes a reference on the bus firmware node with fwnode_handle_get() and stores it in master->dev. That reference is normally dropped by i3c_masterdev_release(), which runs when the last reference on the master device is put.

If i3c_master_rpm_get() fails, the function returns before device_initialize(), so put_device() is never called, the release callback never runs, and the reference is leaked.

Release the reference explicitly on that path. of_node_put() is not enough here: it only acts on Open Firmware nodes, so the reference would still leak on ACPI and software node systems.

The candidate was found with static analysis tools; DeepSeek and GPT-6.1 Sol were used to audit it and to help prepare this fix.

Fixes 3a379bbcea0a ("i3c: Add core I3C infrastructure")

---
Changes in v2:
- Sorry for the delay. v1 used of_node_put() on a reference taken with fwnode_handle_get(), which Frank pointed out.
- Use fwnode_handle_put(dev_fwnode(&master->dev)) instead.
- Rebased on current mainline (fa4ad798f6f6), where this code lives in i3c_master_register_fwnode().
- Dropped Cc: [email protected]. This only leaks when the runtime PM resume fails during registration, so I leave the backport decision to you.
- Build-tested with an x86_64 defconfig, including W=1: drivers/i3c/ compiles without warnings and checkpatch.pl is clean. I could not make i3c_master_rpm_get() fail at runtime, so the failing path itself was not exercised on hardware.
- Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/i3c/master.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i3c/master.c +0 -0
index 01e05bad8661..c532693f8463 100644

drivers/i3c/master.c int i3c_master_register_fwnode(struct i3c_master_controller *master, · line 3901

Quoted 6 lines
drivers/i3c/master.c +2 -1
@@ -3897,8 +3897,10 @@ int i3c_master_register_fwnode(struct i3c_master_controller *master,
 	INIT_LIST_HEAD(&master->boardinfo.i3c);
 
 	ret = i3c_master_rpm_get(master);
-	if (ret)
+	if (ret) {
+		fwnode_handle_put(dev_fwnode(&master->dev));

direct use parameter fwnode

Frank

Quoted 8 lines
drivers/i3c/master.c +1 -1
 		return ret;
+	}
 
 	device_initialize(&master->dev);
 
--

2.34.1