i2c: acpi: Fix adapter reference leak in i2c_acpi_new_device_by_fwnode()
Subsystems (5)
PATCH 1/1 i2c: acpi: Fix adapter reference leak in i2c_acpi_new_device_by_fwnode()
i2c_acpi_new_device_by_fwnode() obtains an i2c_adapter through i2c_acpi_find_adapter_by_handle(). That helper uses bus_find_device() internally, so it returns the adapter with a reference held:
adapter = i2c_verify_adapter(dev);
if (!adapter)
put_device(dev);
return adapter;The reference is only put on the failure path inside that helper. The caller owns it otherwise. i2c_acpi_new_device_by_fwnode() never drops it, neither on the -EPROBE_DEFER path nor after i2c_new_client_device(), so every call leaks one reference on the adapter and the adapter can never be released.
i2c_new_client_device() does not consume that reference. It registers the new client with the adapter as its parent, and device_add() takes its own reference on the parent, which device_del() releases when the client goes away. The reference obtained by the lookup is a separate one and has to be dropped here.
i2c_acpi_notify() in the same file already does exactly this. It takes the adapter from the same helper, registers the client through i2c_acpi_register_device(), which calls i2c_new_client_device(), and then puts the reference.
i2c_new_client_device() gives the client its own reference on the adapter, so ours can be dropped unconditionally.
Found by code review.
--- drivers/i2c/i2c-core-acpi.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core-acpi.c +5 -1
@@ -547,6 +547,7 @@ struct i2c_client *i2c_acpi_new_device_by_fwnode(struct fwnode_handle *fwnode, {struct i2c_acpi_lookup lookup;struct i2c_adapter *adapter; +struct i2c_client *client;struct acpi_device *adev;LIST_HEAD (resource_list);int ret;
@@ -574,7 +575,10 @@ struct i2c_client *i2c_acpi_new_device_by_fwnode(struct fwnode_handle *fwnode,if (!adapter)return ERR_PTR (-EPROBE_DEFER ); -return i2c_new_client_device (adapter, info); + client =i2c_new_client_device (adapter, info); +put_device (&adapter->dev); + +return client; }EXPORT_SYMBOL_GPL (i2c_acpi_new_device_by_fwnode);
Signature · 1 line
2.43.0
Hi Ma,
Quoted 16 lines · Ma Ke
i2c_acpi_new_device_by_fwnode()obtains ani2c_adapterthroughi2c_acpi_find_adapter_by_handle(). That helper usesbus_find_device()internally, so it returns the adapter with a reference held:adapter = i2c_verify_adapter(dev); if (!adapter) put_device(dev); return adapter;The reference is only put on the failure path inside that helper. The caller owns it otherwise.
i2c_acpi_new_device_by_fwnode()never drops it, neither on the -EPROBE_DEFER path nor afteri2c_new_client_device(), so every call leaks one reference on the adapter and the adapter can never be released.
Quoted 5 lines
i2c_new_client_device()does not consume that reference. It registers the new client with the adapter as its parent, anddevice_add()takes its own reference on the parent, whichdevice_del()releases when the client goes away. The reference obtained by the lookup is a separate one and has to be dropped here.
actually device_del() can even end up looping indefinitely waiting for the reference to drop to 0.
Quoted 5 lines
i2c_acpi_notify()in the same file already does exactly this. It takes the adapter from the same helper, registers the client throughi2c_acpi_register_device(), which callsi2c_new_client_device(), and then puts the reference.
Quoted 8 lines
i2c_new_client_device()gives the client its own reference on the adapter, so ours can be dropped unconditionally.Found by code review.
Fixes 605f8fc22442 ("i2c: core: Add new i2c_acpi_new_device helper function")Signed-off-by Ma Ke <[email protected]>
Pushed to i2c/i2c-2.
Thanks,
Andi