diff mbox series

i2c: acpi: Fix adapter reference leak in i2c_acpi_new_device_by_fwnode()

Message ID 20260916155507.2074970-1-vulab@iscas.ac.cn
State New
Headers show
Series i2c: acpi: Fix adapter reference leak in i2c_acpi_new_device_by_fwnode() | expand

Commit Message

Wentao Liang Sept. 16, 2026, 3:55 p.m. UTC
i2c_acpi_find_adapter_by_handle() returns the adapter with a reference
obtained via get_device() in bus_find_device(), which the caller is
responsible for dropping. i2c_new_client_device() stores the adapter
pointer without taking a reference and does not release the caller's
reference on any path, so the reference is leaked.

Drop the adapter device reference after creating the client, like
i2c_acpi_notify() does.

Fixes: 53f8f7c5cf14 ("i2c: break out ACPI support into separate file")
Cc: stable@vger.kernel.org
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
 drivers/i2c/i2c-core-acpi.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Mika Westerberg Sept. 17, 2026, 6:57 a.m. UTC | #1
Hi,

On Wed, Sep 16, 2026 at 03:55:07PM +0000, Wentao Liang wrote:
> i2c_acpi_find_adapter_by_handle() returns the adapter with a reference
> obtained via get_device() in bus_find_device(), which the caller is
> responsible for dropping. i2c_new_client_device() stores the adapter
> pointer without taking a reference and does not release the caller's
> reference on any path, so the reference is leaked.
> 
> Drop the adapter device reference after creating the client, like
> i2c_acpi_notify() does.
> 
> Fixes: 53f8f7c5cf14 ("i2c: break out ACPI support into separate file")

I don't think this is right. The issue existed already before the move.

> Cc: stable@vger.kernel.org
> Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>

Acked-by: Mika Westerberg <westeri@kernel.org>
Wentao Liang Nov. 6, 2026, 9:31 a.m. UTC | #2
Hi Mika,

I am very sorry for replying so late, seven weeks after your review.
You took the time to look at the patch and to ack it the same day, and I
left the thread hanging without a word. There is no good excuse for
that, and I apologize for the delay and for the extra work it caused.

> I don't think this is right. The issue existed already before the move.

You are right. 53f8f7c5cf14 only moved the existing ACPI support code
into a separate file, and I should have traced the history back instead
of stopping at the move. The leak was introduced by 605f8fc22442
("i2c: core: Add new i2c_acpi_new_device helper function"), which added
i2c_acpi_new_device() with the same pattern: the adapter is returned by
i2c_acpi_find_adapter_by_handle() with a reference taken by
bus_find_device(), and the function then returns
i2c_new_device(adapter, info) without dropping that reference. So the
correct tags are:

	Fixes: 605f8fc22442 ("i2c: core: Add new i2c_acpi_new_device helper function")
	Cc: <stable@vger.kernel.org> # v4.12+

While reading up on the thread I also saw that the same fix has since
been submitted by Ma Ke and is now in mainline as a65917d0a22f with the
corrected tag, so there is nothing left for me to resend and I will not
add a duplicate v2 to the list.

This has been an important lesson for me. From now on I will go through
all remaining review comments one by one myself and reply in time, and I
will not send patches in batches that exceed what I can properly follow
up on.

Thank you for your review, for the ack and for your patience.

Best regards,
Wentao Liang
diff mbox series

Patch

diff --git a/drivers/i2c/i2c-core-acpi.c b/drivers/i2c/i2c-core-acpi.c
index 28c0e4884a7f..5efd1c406bc2 100644
--- a/drivers/i2c/i2c-core-acpi.c
+++ b/drivers/i2c/i2c-core-acpi.c
@@ -544,6 +544,7 @@  struct i2c_client *i2c_acpi_new_device_by_fwnode(struct fwnode_handle *fwnode,
 						 struct i2c_board_info *info)
 {
 	struct i2c_acpi_lookup lookup;
+	struct i2c_client *client;
 	struct i2c_adapter *adapter;
 	struct acpi_device *adev;
 	LIST_HEAD(resource_list);
@@ -572,7 +573,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);