Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 3928ee6485a3 ("net: phy: leds: Add support for "link" trigger")
Fixes: 2e0bc452f472 ("net: phy: leds: add support for led triggers on phy link state change")
Signed-off-by: Hao Guan <hao.guan@siflower.com.cn>
Signed-off-by: Qingfang Deng <qingfang.deng@siflower.com.cn>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Link: https://patch.msgid.link/20250417032557.2929427-1-dqfext@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Qingfang Deng and committed by
Jakub Kicinski
b7f0ee99 ce681558

+13 -10
+13 -10
drivers/net/phy/phy_led_triggers.c
··· 93 93 if (!phy->phy_num_led_triggers) 94 94 return 0; 95 95 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); 99 98 if (!phy->led_link_trigger) { 100 99 err = -ENOMEM; 101 100 goto out_clear; ··· 104 105 if (err) 105 106 goto out_free_link; 106 107 107 - phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev, 108 - phy->phy_num_led_triggers, 109 - sizeof(struct phy_led_trigger), 110 - GFP_KERNEL); 108 + phy->phy_led_triggers = kcalloc(phy->phy_num_led_triggers, 109 + sizeof(struct phy_led_trigger), 110 + GFP_KERNEL); 111 111 if (!phy->phy_led_triggers) { 112 112 err = -ENOMEM; 113 113 goto out_unreg_link; ··· 127 129 out_unreg: 128 130 while (i--) 129 131 phy_led_trigger_unregister(&phy->phy_led_triggers[i]); 130 - devm_kfree(&phy->mdio.dev, phy->phy_led_triggers); 132 + kfree(phy->phy_led_triggers); 131 133 out_unreg_link: 132 134 phy_led_trigger_unregister(phy->led_link_trigger); 133 135 out_free_link: 134 - devm_kfree(&phy->mdio.dev, phy->led_link_trigger); 136 + kfree(phy->led_link_trigger); 135 137 phy->led_link_trigger = NULL; 136 138 out_clear: 137 139 phy->phy_num_led_triggers = 0; ··· 145 147 146 148 for (i = 0; i < phy->phy_num_led_triggers; i++) 147 149 phy_led_trigger_unregister(&phy->phy_led_triggers[i]); 150 + kfree(phy->phy_led_triggers); 151 + phy->phy_led_triggers = NULL; 148 152 149 - if (phy->led_link_trigger) 153 + if (phy->led_link_trigger) { 150 154 phy_led_trigger_unregister(phy->led_link_trigger); 155 + kfree(phy->led_link_trigger); 156 + phy->led_link_trigger = NULL; 157 + } 151 158 } 152 159 EXPORT_SYMBOL_GPL(phy_led_triggers_unregister);