Skip to content

Commit e331146

Browse files
Vladimir ZapolskiyWolfram Sang
authored andcommitted
i2c: fix leaked device refcount on of_find_i2c_* error path
If of_find_i2c_device_by_node() or of_find_i2c_adapter_by_node() find a device by node, but its type does not match, a reference to that device is still held. This change fixes the problem. Signed-off-by: Vladimir Zapolskiy <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent e952849 commit e331146

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

drivers/i2c/i2c-core.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,27 +1340,35 @@ static int of_dev_node_match(struct device *dev, void *data)
13401340
struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
13411341
{
13421342
struct device *dev;
1343+
struct i2c_client *client;
13431344

1344-
dev = bus_find_device(&i2c_bus_type, NULL, node,
1345-
of_dev_node_match);
1345+
dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
13461346
if (!dev)
13471347
return NULL;
13481348

1349-
return i2c_verify_client(dev);
1349+
client = i2c_verify_client(dev);
1350+
if (!client)
1351+
put_device(dev);
1352+
1353+
return client;
13501354
}
13511355
EXPORT_SYMBOL(of_find_i2c_device_by_node);
13521356

13531357
/* must call put_device() when done with returned i2c_adapter device */
13541358
struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
13551359
{
13561360
struct device *dev;
1361+
struct i2c_adapter *adapter;
13571362

1358-
dev = bus_find_device(&i2c_bus_type, NULL, node,
1359-
of_dev_node_match);
1363+
dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
13601364
if (!dev)
13611365
return NULL;
13621366

1363-
return i2c_verify_adapter(dev);
1367+
adapter = i2c_verify_adapter(dev);
1368+
if (!adapter)
1369+
put_device(dev);
1370+
1371+
return adapter;
13641372
}
13651373
EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
13661374
#else

0 commit comments

Comments
 (0)