Skip to contentSkip to conversation

net: phy: leds: fix memory leak

PATCH Applied Qingfang Deng 4 participants 5 messages Apr 17, 2025 – Apr 23, 2025

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

PATCH 1/1 net: phy: leds: fix memory leak

From: Qingfang Deng <[email protected]>

A network restart test on a router led to an out-of-memory condition, which was traced to a memory leak in the PHY LED trigger code.

The root cause is misuse of the devm API. The registration function (phy_led_triggers_register) is called from phy_attach_direct, not phy_probe, and the unregister function (phy_led_triggers_unregister) is called from phy_detach, not phy_remove. This means the register and unregister functions can be called multiple times for the same PHY device, but devm-allocated memory is not freed until the driver is unbound.

This also prevents kmemleak from detecting the leak, as the devm API internally stores the allocated pointer.

Fix this by replacing devm_kzalloc/devm_kcalloc with standard kzalloc/kcalloc, and add the corresponding kfree calls in the unregister path.

Fixes 3928ee6485a3 ("net: phy: leds: Add support for "link" trigger")
Fixes 2e0bc452f472 ("net: phy: leds: add support for led triggers on phy link state change")
---
 drivers/net/phy/phy_led_triggers.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
drivers/net/phy/phy_led_triggers.c +13 -10
index bd3c9554f6ac..60893691d4c3 100644
@@ -93,9 +93,8 @@ int phy_led_triggers_register(struct phy_device *phy)
 	if (!phy->phy_num_led_triggers)
 		return 0;
 
-	phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev,
-					     sizeof(*phy->led_link_trigger),
-					     GFP_KERNEL);
+	phy->led_link_trigger = kzalloc(sizeof(*phy->led_link_trigger),
+					GFP_KERNEL);
 	if (!phy->led_link_trigger) {
 		err = -ENOMEM;
 		goto out_clear;
@@ -105,10 +104,9 @@ int phy_led_triggers_register(struct phy_device *phy)
 	if (err)
 		goto out_free_link;
 
-	phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev,
-					    phy->phy_num_led_triggers,
-					    sizeof(struct phy_led_trigger),
-					    GFP_KERNEL);
+	phy->phy_led_triggers = kcalloc(phy->phy_num_led_triggers,
+					sizeof(struct phy_led_trigger),
+					GFP_KERNEL);
 	if (!phy->phy_led_triggers) {
 		err = -ENOMEM;
 		goto out_unreg_link;
@@ -129,11 +127,11 @@ int phy_led_triggers_register(struct phy_device *phy)
 out_unreg:
 	while (i--)
 		phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
-	devm_kfree(&phy->mdio.dev, phy->phy_led_triggers);
+	kfree(phy->phy_led_triggers);
 out_unreg_link:
 	phy_led_trigger_unregister(phy->led_link_trigger);
 out_free_link:
-	devm_kfree(&phy->mdio.dev, phy->led_link_trigger);
+	kfree(phy->led_link_trigger);
 	phy->led_link_trigger = NULL;
 out_clear:
 	phy->phy_num_led_triggers = 0;
@@ -147,8 +145,13 @@ void phy_led_triggers_unregister(struct phy_device *phy)
 
 	for (i = 0; i < phy->phy_num_led_triggers; i++)
 		phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
+	kfree(phy->phy_led_triggers);
+	phy->phy_led_triggers = NULL;
 
-	if (phy->led_link_trigger)
+	if (phy->led_link_trigger) {
 		phy_led_trigger_unregister(phy->led_link_trigger);
+		kfree(phy->led_link_trigger);
+		phy->led_link_trigger = NULL;
+	}
 }
 EXPORT_SYMBOL_GPL(phy_led_triggers_unregister);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · On Thu, 17 Apr 2025 11:25:56 +0800 Qingfang Deng <[email protected]> wrote:

From: Qingfang Deng <[email protected]>

A network restart test on a router led to an out-of-memory condition, which was traced to a memory leak in the PHY LED trigger code.

Quoted 7 lines

The root cause is misuse of the devm API. The registration function (phy_led_triggers_register) is called from phy_attach_direct, not phy_probe, and the unregister function (phy_led_triggers_unregister) is called from phy_detach, not phy_remove. This means the register and unregister functions can be called multiple times for the same PHY device, but devm-allocated memory is not freed until the driver is unbound.

Are there historical reasons for the triggers not to be registered at probe time ? I agree with your analysis otherwise.

Maxime

#2 lore raw

Hi Maxime,

Quoted 17 lines · Maxime Chevallier
Quoted 5 lines · On Thu, 17 Apr 2025 11:25:56 +0800 Qingfang Deng <[email protected]> wrote:

From: Qingfang Deng <[email protected]>

A network restart test on a router led to an out-of-memory condition, which was traced to a memory leak in the PHY LED trigger code.

Quoted 7 lines

The root cause is misuse of the devm API. The registration function (phy_led_triggers_register) is called from phy_attach_direct, not phy_probe, and the unregister function (phy_led_triggers_unregister) is called from phy_detach, not phy_remove. This means the register and unregister functions can be called multiple times for the same PHY device, but devm-allocated memory is not freed until the driver is unbound.

Quoted 2 lines

Are there historical reasons for the triggers not to be registered at probe time ? I agree with your analysis otherwise.

I'm not sure exactly, but both register and unregister are called under a condition:

- if (!phydev->is_on_sfp_module)

Which may not be available at probe time.

Quoted 2 lines

Maxime

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Qingfang Deng
Quoted 16 lines · Qingfang Deng

From: Qingfang Deng <[email protected]>

A network restart test on a router led to an out-of-memory condition, which was traced to a memory leak in the PHY LED trigger code.

The root cause is misuse of the devm API. The registration function (phy_led_triggers_register) is called from phy_attach_direct, not phy_probe, and the unregister function (phy_led_triggers_unregister) is called from phy_detach, not phy_remove. This means the register and unregister functions can be called multiple times for the same PHY device, but devm-allocated memory is not freed until the driver is unbound.

This also prevents kmemleak from detecting the leak, as the devm API internally stores the allocated pointer.

Quoted 8 lines

Fix this by replacing devm_kzalloc/devm_kcalloc with standard kzalloc/kcalloc, and add the corresponding kfree calls in the unregister path.

Fixes 3928ee6485a3 ("net: phy: leds: Add support for "link" trigger")
Fixes 2e0bc452f472 ("net: phy: leds: add support for led triggers on phy link state change")

Thanks for the fix. I agree with Maxime, this looks correct.

The use of devm_free() should trigger any reviewer to take a closer look because it generally means something is wrong.

Andrew

#4 lore raw
In reply to Qingfang Deng
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 13 lines · On Thu, 17 Apr 2025 11:25:56 +0800 you wrote:

From: Qingfang Deng <[email protected]>

A network restart test on a router led to an out-of-memory condition, which was traced to a memory leak in the PHY LED trigger code.

The root cause is misuse of the devm API. The registration function (phy_led_triggers_register) is called from phy_attach_direct, not phy_probe, and the unregister function (phy_led_triggers_unregister) is called from phy_detach, not phy_remove. This means the register and unregister functions can be called multiple times for the same PHY device, but devm-allocated memory is not freed until the driver is unbound.

Quoted 1 line

[...]

Here is the summary with links:
- [net] net: phy: leds: fix memory leak

    https://git.kernel.org/netdev/net/c/b7f0ee992adf

You are awesome, thank you!

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