Skip to content

Commit b7f0ee9

Browse files
LGA1150kuba-moo
authored andcommitted
net: phy: leds: fix memory leak
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: 3928ee6 ("net: phy: leds: Add support for "link" trigger") Fixes: 2e0bc45 ("net: phy: leds: add support for led triggers on phy link state change") Signed-off-by: Hao Guan <[email protected]> Signed-off-by: Qingfang Deng <[email protected]> Reviewed-by: Andrew Lunn <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent ce68155 commit b7f0ee9

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

drivers/net/phy/phy_led_triggers.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ int phy_led_triggers_register(struct phy_device *phy)
9393
if (!phy->phy_num_led_triggers)
9494
return 0;
9595

96-
phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev,
97-
sizeof(*phy->led_link_trigger),
98-
GFP_KERNEL);
96+
phy->led_link_trigger = kzalloc(sizeof(*phy->led_link_trigger),
97+
GFP_KERNEL);
9998
if (!phy->led_link_trigger) {
10099
err = -ENOMEM;
101100
goto out_clear;
@@ -105,10 +104,9 @@ int phy_led_triggers_register(struct phy_device *phy)
105104
if (err)
106105
goto out_free_link;
107106

108-
phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev,
109-
phy->phy_num_led_triggers,
110-
sizeof(struct phy_led_trigger),
111-
GFP_KERNEL);
107+
phy->phy_led_triggers = kcalloc(phy->phy_num_led_triggers,
108+
sizeof(struct phy_led_trigger),
109+
GFP_KERNEL);
112110
if (!phy->phy_led_triggers) {
113111
err = -ENOMEM;
114112
goto out_unreg_link;
@@ -129,11 +127,11 @@ int phy_led_triggers_register(struct phy_device *phy)
129127
out_unreg:
130128
while (i--)
131129
phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
132-
devm_kfree(&phy->mdio.dev, phy->phy_led_triggers);
130+
kfree(phy->phy_led_triggers);
133131
out_unreg_link:
134132
phy_led_trigger_unregister(phy->led_link_trigger);
135133
out_free_link:
136-
devm_kfree(&phy->mdio.dev, phy->led_link_trigger);
134+
kfree(phy->led_link_trigger);
137135
phy->led_link_trigger = NULL;
138136
out_clear:
139137
phy->phy_num_led_triggers = 0;
@@ -147,8 +145,13 @@ void phy_led_triggers_unregister(struct phy_device *phy)
147145

148146
for (i = 0; i < phy->phy_num_led_triggers; i++)
149147
phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
148+
kfree(phy->phy_led_triggers);
149+
phy->phy_led_triggers = NULL;
150150

151-
if (phy->led_link_trigger)
151+
if (phy->led_link_trigger) {
152152
phy_led_trigger_unregister(phy->led_link_trigger);
153+
kfree(phy->led_link_trigger);
154+
phy->led_link_trigger = NULL;
155+
}
153156
}
154157
EXPORT_SYMBOL_GPL(phy_led_triggers_unregister);

0 commit comments

Comments
 (0)