Skip to contentSkip to conversation

net: mctp i3c: clean up notifier and buses if driver register fails

PATCH Applied Myeonghun Pak 3 participants 3 messages Jul 15 – Jul 21

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

PATCH v2 1/1 net: mctp i3c: clean up notifier and buses if driver register fails

mctp_i3c_mod_init() registers the I3C bus notifier and then walks the existing buses with i3c_for_each_bus_locked(mctp_i3c_bus_add_new, NULL) before registering the I3C device driver. If i3c_driver_register() fails, the function returns the error directly, leaving the notifier registered and every mctp_i3c_bus object created for the existing buses allocated. The notifier is left pointing into the module that failed to load and the bus list is leaked.

Mirror the module exit path on this failure: unregister the notifier and tear down the buses that were added before returning the error.

This issue was identified during our ongoing static-analysis research while reviewing kernel code.

Fixes c8755b29b58e ("mctp i3c: MCTP I3C driver")

---
Changes in v2:
- Remove the unmatched i3c_driver_unregister() from the notifier registration failure path, as suggested by Jeremy.
- Add Simon Horman to Cc.

 drivers/net/mctp/mctp-i3c.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/net/mctp/mctp-i3c.c +7 -4
index 6d2bbae747..88d9e36cd4 100644
@@ -731,18 +731,21 @@ static __init int mctp_i3c_mod_init(void)
 	int rc;
 
 	rc = i3c_register_notifier(&mctp_i3c_notifier);
-	if (rc < 0) {
-		i3c_driver_unregister(&mctp_i3c_driver);
+	if (rc < 0)
 		return rc;
-	}
 
 	i3c_for_each_bus_locked(mctp_i3c_bus_add_new, NULL);
 
 	rc = i3c_driver_register(&mctp_i3c_driver);
 	if (rc < 0)
-		return rc;
+		goto err_unregister_notifier;
 
 	return 0;
+
+err_unregister_notifier:
+	i3c_unregister_notifier(&mctp_i3c_notifier);
+	mctp_i3c_bus_remove_all();
+	return rc;
 }
 
 static __exit void mctp_i3c_mod_exit(void)

--
2.53.0

maintainer Acked-by on v2 1/1
#1 lore raw

Hi Myeonghun,

Quoted 10 lines

mctp_i3c_mod_init() registers the I3C bus notifier and then walks the existing buses with i3c_for_each_bus_locked(mctp_i3c_bus_add_new, NULL) before registering the I3C device driver.  If i3c_driver_register() fails, the function returns the error directly, leaving the notifier registered and every mctp_i3c_bus object created for the existing buses allocated.  The notifier is left pointing into the module that failed to load and the bus list is leaked.

Mirror the module exit path on this failure: unregister the notifier and tear down the buses that were added before returning the error.

Looks good, thank you.

Cheers,

Jeremy

#2 lore raw
In reply to Myeonghun Pak
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Wed, 15 Jul 2026 16:25:17 +0900 you wrote:

mctp_i3c_mod_init() registers the I3C bus notifier and then walks the existing buses with i3c_for_each_bus_locked(mctp_i3c_bus_add_new, NULL) before registering the I3C device driver. If i3c_driver_register() fails, the function returns the error directly, leaving the notifier registered and every mctp_i3c_bus object created for the existing buses allocated. The notifier is left pointing into the module that failed to load and the bus list is leaked.

[...]

Here is the summary with links:
- [net,v2] net: mctp i3c: clean up notifier and buses if driver register fails

    https://git.kernel.org/netdev/net/c/03d1057305ef

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html