Skip to content

Commit b7d3e3d

Browse files
daviddaneydavem330
authored andcommitted
net: thunderx: Don't leak phy device references on -EPROBE_DEFER condition.
It is possible, although unlikely, that probing will find the phy_device for the first LMAC of a thunder BGX device, but then need to fail with -EPROBE_DEFER on a subsequent LMAC. In this case, we need to call put_device() on each of the phy_devices that were obtained, but will be unused due to returning -EPROBE_DEFER. Also, since we can break out of the probing loop early, we need to explicitly call of_node_put() outside of the loop. Signed-off-by: David Daney <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 9277a4f commit b7d3e3d

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

drivers/net/ethernet/cavium/thunder/thunder_bgx.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,17 +974,18 @@ static int bgx_init_acpi_phy(struct bgx *bgx)
974974
static int bgx_init_of_phy(struct bgx *bgx)
975975
{
976976
struct fwnode_handle *fwn;
977+
struct device_node *node = NULL;
977978
u8 lmac = 0;
978-
const char *mac;
979979

980980
device_for_each_child_node(&bgx->pdev->dev, fwn) {
981981
struct phy_device *pd;
982982
struct device_node *phy_np;
983-
struct device_node *node = to_of_node(fwn);
983+
const char *mac;
984984

985985
/* Should always be an OF node. But if it is not, we
986986
* cannot handle it, so exit the loop.
987987
*/
988+
node = to_of_node(fwn);
988989
if (!node)
989990
break;
990991

@@ -1005,17 +1006,30 @@ static int bgx_init_of_phy(struct bgx *bgx)
10051006
/* Wait until the phy drivers are available */
10061007
pd = of_phy_find_device(phy_np);
10071008
if (!pd)
1008-
return -EPROBE_DEFER;
1009+
goto defer;
10091010
bgx->lmac[lmac].phydev = pd;
10101011
}
10111012

10121013
lmac++;
1013-
if (lmac == MAX_LMAC_PER_BGX) {
1014-
of_node_put(node);
1014+
if (lmac == MAX_LMAC_PER_BGX)
10151015
break;
1016-
}
10171016
}
1017+
of_node_put(node);
10181018
return 0;
1019+
1020+
defer:
1021+
/* We are bailing out, try not to leak device reference counts
1022+
* for phy devices we may have already found.
1023+
*/
1024+
while (lmac) {
1025+
if (bgx->lmac[lmac].phydev) {
1026+
put_device(&bgx->lmac[lmac].phydev->mdio.dev);
1027+
bgx->lmac[lmac].phydev = NULL;
1028+
}
1029+
lmac--;
1030+
}
1031+
of_node_put(node);
1032+
return -EPROBE_DEFER;
10191033
}
10201034

10211035
#else

0 commit comments

Comments
 (0)