bus: fsl-mc: Fix potential double device reference in fsl_mc_get_endpoint()
Subsystems (6)
PATCH v2 1/3 bus: fsl-mc: Fix potential double device reference in fsl_mc_get_endpoint()
The fsl_mc_get_endpoint() function may call fsl_mc_device_lookup() twice, which would increment the device's reference count twice if both lookups find a device. This could lead to a reference count leak.
Found by code review.
--- drivers/bus/fsl-mc/fsl-mc-bus.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/bus/fsl-mc/fsl-mc-bus.c +9 -10
@@ -943,6 +943,7 @@ struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,struct fsl_mc_obj_desc endpoint_desc = {{0 }};struct dprc_endpoint endpoint1 = {{0 }};struct dprc_endpoint endpoint2 = {{0 }}; +struct fsl_mc_bus *mc_bus;int state, err; mc_bus_dev =to_fsl_mc_device (mc_dev->dev.parent);
@@ -966,6 +967,8 @@ struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,strcpy (endpoint_desc.type, endpoint2.type); endpoint_desc.id = endpoint2.id; endpoint =fsl_mc_device_lookup (&endpoint_desc, mc_bus_dev); +if (endpoint) +return endpoint;/* * We know that the device has an endpoint because we verified by
@@ -973,17 +976,13 @@ struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,* yet discovered by the fsl-mc bus, thus the lookup returned NULL. * Force a rescan of the devices in this container and retry the lookup. */ -if (!endpoint) { -struct fsl_mc_bus *mc_bus =to_fsl_mc_bus (mc_bus_dev); - -if (mutex_trylock (&mc_bus->scan_mutex)) { - err =dprc_scan_objects (mc_bus_dev, true); -mutex_unlock (&mc_bus->scan_mutex); - } - -if (err <0 ) -return ERR_PTR (err); + mc_bus =to_fsl_mc_bus (mc_bus_dev); +if (mutex_trylock (&mc_bus->scan_mutex)) { + err =dprc_scan_objects (mc_bus_dev, true); +mutex_unlock (&mc_bus->scan_mutex); } +if (err <0 ) +return ERR_PTR (err); endpoint =fsl_mc_device_lookup (&endpoint_desc, mc_bus_dev);/*
Signature · 1 line
2.25.1
PATCH v2 2/3 dpaa2-eth: Fix device reference count leak in MAC endpoint handling
The fsl_mc_get_endpoint() function uses device_find_child() for localization, which implicitly calls get_device() to increment the device's reference count before returning the pointer. However, the caller dpaa2_eth_connect_mac() fails to properly release this reference in multiple scenarios. We should call put_device() to decrement reference count properly.
As comment of device_find_child() says, 'NOTE: you will need to drop the reference with put_device() after use'.
Found by code review.
--- drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +12 -3
@@ -4666,12 +4666,19 @@ static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)return PTR_ERR (dpmac_dev); } -if (IS_ERR (dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type) +if (IS_ERR (dpmac_dev))return 0 ; +if (dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type) { + err =0 ; + goto out_put_device; + } + mac =kzalloc (sizeof (struct dpaa2_mac ),GFP_KERNEL ); -if (!mac) -return -ENOMEM ; +if (!mac) { + err = -ENOMEM ; + goto out_put_device; + } mac->mc_dev = dpmac_dev; mac->mc_io = priv->mc_io;
@@ -4705,6 +4712,8 @@ static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)dpaa2_mac_close (mac); err_free_mac:kfree (mac); +out_put_device: +put_device (&dpmac_dev->dev);return err; }
Signature · 1 line
2.25.1
Re: [PATCH net v2 2/3] dpaa2-eth: Fix device reference count leak in MAC endpoint handling
Quoted 7 lines · Ma Ke
The
fsl_mc_get_endpoint()function usesdevice_find_child()for localization, which implicitly callsget_device()to increment the device's reference count before returning the pointer. However, the callerdpaa2_eth_connect_mac()fails to properly release this reference in multiple scenarios. We should callput_device()to decrement reference count properly.
Quoted 8 lines
As comment of
device_find_child()says, 'NOTE: you will need to drop the reference withput_device()after use'.Found by code review.
Fixes 719479230893 ("dpaa2-eth: add MAC/PHY support through phylink")Signed-off-by Ma Ke <[email protected]>
Re: [PATCH net v2 2/3] dpaa2-eth: Fix device reference count leak in MAC endpoint handling
In reply to Ma KeQuoted 7 lines · Ma Ke
The
fsl_mc_get_endpoint()function usesdevice_find_child()for localization, which implicitly callsget_device()to increment the device's reference count before returning the pointer. However, the callerdpaa2_eth_connect_mac()fails to properly release this reference in multiple scenarios. We should callput_device()to decrement reference count properly.
Quoted 8 lines
As comment of
device_find_child()says, 'NOTE: you will need to drop the reference withput_device()after use'.Found by code review.
Fixes 719479230893 ("dpaa2-eth: add MAC/PHY support through phylink")Signed-off-by Ma Ke <[email protected]>
PATCH v2 3/3 dpaa2-switch: Fix device reference count leak in MAC endpoint handling
In reply to Ma KeThe fsl_mc_get_endpoint() function uses device_find_child() for localization, which implicitly calls get_device() to increment the device's reference count before returning the pointer. However, the caller dpaa2_switch_port_connect_mac() fails to properly release this reference in multiple scenarios. We should call put_device() to decrement reference count properly.
As comment of device_find_child() says, 'NOTE: you will need to drop the reference with put_device() after use'.
Found by code review.
--- .../net/ethernet/freescale/dpaa2/dpaa2-switch.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/dpaa2/dpaa2-switch.c +12 -3
@@ -1448,12 +1448,19 @@ static int dpaa2_switch_port_connect_mac(struct ethsw_port_priv *port_priv)if (PTR_ERR (dpmac_dev) == -EPROBE_DEFER )return PTR_ERR (dpmac_dev); -if (IS_ERR (dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type) +if (IS_ERR (dpmac_dev))return 0 ; +if (dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type) { + err =0 ; + goto out_put_device; + } + mac =kzalloc (sizeof (*mac),GFP_KERNEL ); -if (!mac) -return -ENOMEM ; +if (!mac) { + err = -ENOMEM ; + goto out_put_device; + } mac->mc_dev = dpmac_dev; mac->mc_io = port_priv->ethsw_data->mc_io;
@@ -1483,6 +1490,8 @@ static int dpaa2_switch_port_connect_mac(struct ethsw_port_priv *port_priv)dpaa2_mac_close (mac); err_free_mac:kfree (mac); +out_put_device: +put_device (&dpmac_dev->dev);return err; }
Signature · 1 line
2.25.1
Re: [PATCH net v2 3/3] dpaa2-switch: Fix device reference count leak in MAC endpoint handling
Quoted 7 lines · Ma Ke
The
fsl_mc_get_endpoint()function usesdevice_find_child()for localization, which implicitly callsget_device()to increment the device's reference count before returning the pointer. However, the callerdpaa2_switch_port_connect_mac()fails to properly release this reference in multiple scenarios. We should callput_device()to decrement reference count properly.
Quoted 8 lines
As comment of
device_find_child()says, 'NOTE: you will need to drop the reference withput_device()after use'.Found by code review.
Fixes 84cba72956fd ("dpaa2-switch: integrate the MAC endpoint support")Signed-off-by Ma Ke <[email protected]>
Re: [PATCH net v2 3/3] dpaa2-switch: Fix device reference count leak in MAC endpoint handling
In reply to Ma KeQuoted 7 lines · Ma Ke
The
fsl_mc_get_endpoint()function usesdevice_find_child()for localization, which implicitly callsget_device()to increment the device's reference count before returning the pointer. However, the callerdpaa2_switch_port_connect_mac()fails to properly release this reference in multiple scenarios. We should callput_device()to decrement reference count properly.
Quoted 8 lines
As comment of
device_find_child()says, 'NOTE: you will need to drop the reference withput_device()after use'.Found by code review.
Fixes 84cba72956fd ("dpaa2-switch: integrate the MAC endpoint support")Signed-off-by Ma Ke <[email protected]>
Quoted 9 lines · Ma Ke
The
fsl_mc_get_endpoint()function may callfsl_mc_device_lookup()twice, which would increment the device's reference count twice if both lookups find a device. This could lead to a reference count leak.Found by code review.
Fixes 1ac210d128ef ("bus: fsl-mc: add the fsl_mc_get_endpoint function")Signed-off-by Ma Ke <[email protected]>
Quoted 8 lines · Ma Ke
The
fsl_mc_get_endpoint()function may callfsl_mc_device_lookup()twice, which would increment the device's reference count twice if both lookups find a device. This could lead to a reference count leak.Found by code review.
Fixes 1ac210d128ef ("bus: fsl-mc: add the fsl_mc_get_endpoint function")
I think this should be:
I've CCed Laurentiu, the author of that commit.
Quoted 1 line
Signed-off-by Ma Ke <[email protected]>
Quoted 52 lines
--- drivers/bus/fsl-mc/fsl-mc-bus.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)drivers/bus/fsl-mc/fsl-mc-bus.c +9
-10@@ -943,6 +943,7 @@ struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,struct fsl_mc_obj_desc endpoint_desc = {{0 }};struct dprc_endpoint endpoint1 = {{0 }};struct dprc_endpoint endpoint2 = {{0 }}; +struct fsl_mc_bus *mc_bus;int state, err; mc_bus_dev =to_fsl_mc_device (mc_dev->dev.parent);@@ -966,6 +967,8 @@ struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,strcpy (endpoint_desc.type, endpoint2.type); endpoint_desc.id = endpoint2.id; endpoint =fsl_mc_device_lookup (&endpoint_desc, mc_bus_dev); +if (endpoint) +return endpoint;/* * We know that the device has an endpoint because we verified by @@ -973,17 +976,13 @@ struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,* yet discovered by the fsl-mc bus, thus the lookup returned NULL. * Force a rescan of the devices in this container and retry the lookup. */ -if (!endpoint) { -struct fsl_mc_bus *mc_bus =to_fsl_mc_bus (mc_bus_dev); - -if (mutex_trylock (&mc_bus->scan_mutex)) { - err =dprc_scan_objects (mc_bus_dev, true); -mutex_unlock (&mc_bus->scan_mutex); - } - -if (err <0 ) -return ERR_PTR (err); + mc_bus =to_fsl_mc_bus (mc_bus_dev); +if (mutex_trylock (&mc_bus->scan_mutex)) { + err =dprc_scan_objects (mc_bus_dev, true); +mutex_unlock (&mc_bus->scan_mutex); } +if (err <0 ) +return ERR_PTR (err); endpoint =fsl_mc_device_lookup (&endpoint_desc, mc_bus_dev);/* Signature · 1 line
2.25.1
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Thu, 17 Jul 2025 10:23:07 +0800 you wrote:
The
fsl_mc_get_endpoint()function may callfsl_mc_device_lookup()twice, which would increment the device's reference count twice if both lookups find a device. This could lead to a reference count leak.Found by code review.
Fixes 1ac210d128ef ("bus: fsl-mc: add the fsl_mc_get_endpoint function")Signed-off-by Ma Ke <[email protected]>[...]
Here is the summary with links:
- [net,v2,1/3] bus: fsl-mc: Fix potential double device reference in fsl_mc_get_endpoint()
https://git.kernel.org/netdev/net/c/bddbe13d36a0 - [net,v2,2/3] dpaa2-eth: Fix device reference count leak in MAC endpoint handling https://git.kernel.org/netdev/net/c/ee9f3a81ab08 - [net,v2,3/3] dpaa2-switch: Fix device reference count leak in MAC endpoint handling https://git.kernel.org/netdev/net/c/96e056ffba91
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html