Skip to content

Commit

Permalink
i2c: fix leaked device refcount on of_find_i2c_* error path
Browse files Browse the repository at this point in the history
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 <vladimir_zapolskiy@mentor.com>
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
  • Loading branch information
Vladimir Zapolskiy authored and Wolfram Sang committed Aug 1, 2015
1 parent e952849 commit e331146
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions drivers/i2c/i2c-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1340,27 +1340,35 @@ static int of_dev_node_match(struct device *dev, void *data)
struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
{
struct device *dev;
struct i2c_client *client;

dev = bus_find_device(&i2c_bus_type, NULL, node,
of_dev_node_match);
dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
if (!dev)
return NULL;

return i2c_verify_client(dev);
client = i2c_verify_client(dev);
if (!client)
put_device(dev);

return client;
}
EXPORT_SYMBOL(of_find_i2c_device_by_node);

/* must call put_device() when done with returned i2c_adapter device */
struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
{
struct device *dev;
struct i2c_adapter *adapter;

dev = bus_find_device(&i2c_bus_type, NULL, node,
of_dev_node_match);
dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
if (!dev)
return NULL;

return i2c_verify_adapter(dev);
adapter = i2c_verify_adapter(dev);
if (!adapter)
put_device(dev);

return adapter;
}
EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
#else
Expand Down

0 comments on commit e331146

Please sign in to comment.