Skip to content

Commit bddbe13

Browse files
Ma Kekuba-moo
authored andcommitted
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. Cc: [email protected] Fixes: 1ac210d ("bus: fsl-mc: add the fsl_mc_get_endpoint function") Signed-off-by: Ma Ke <[email protected]> Tested-by: Ioana Ciornei <[email protected]> Reviewed-by: Simon Horman <[email protected]> Fixes: 8567494 ("bus: fsl-mc: rescan devices if endpoint not found") Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 6832a93 commit bddbe13

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

drivers/bus/fsl-mc/fsl-mc-bus.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
943943
struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
944944
struct dprc_endpoint endpoint1 = {{ 0 }};
945945
struct dprc_endpoint endpoint2 = {{ 0 }};
946+
struct fsl_mc_bus *mc_bus;
946947
int state, err;
947948

948949
mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
@@ -966,24 +967,22 @@ struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
966967
strcpy(endpoint_desc.type, endpoint2.type);
967968
endpoint_desc.id = endpoint2.id;
968969
endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
970+
if (endpoint)
971+
return endpoint;
969972

970973
/*
971974
* We know that the device has an endpoint because we verified by
972975
* interrogating the firmware. This is the case when the device was not
973976
* yet discovered by the fsl-mc bus, thus the lookup returned NULL.
974977
* Force a rescan of the devices in this container and retry the lookup.
975978
*/
976-
if (!endpoint) {
977-
struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
978-
979-
if (mutex_trylock(&mc_bus->scan_mutex)) {
980-
err = dprc_scan_objects(mc_bus_dev, true);
981-
mutex_unlock(&mc_bus->scan_mutex);
982-
}
983-
984-
if (err < 0)
985-
return ERR_PTR(err);
979+
mc_bus = to_fsl_mc_bus(mc_bus_dev);
980+
if (mutex_trylock(&mc_bus->scan_mutex)) {
981+
err = dprc_scan_objects(mc_bus_dev, true);
982+
mutex_unlock(&mc_bus->scan_mutex);
986983
}
984+
if (err < 0)
985+
return ERR_PTR(err);
987986

988987
endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
989988
/*

0 commit comments

Comments
 (0)