All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sang-Hoon Choi <csh0052@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, Chance Yang <chance.yang@kneron.us>,
	Changyul Lee <lcy8047@gmail.com>
Subject: [PATCH] usb: common: usb-conn-gpio: manage connector ID with devres
Date: Fri, 02 Oct 2026 19:47:40 +0000	[thread overview]
Message-ID: <20261003.final019.b1f833b54899139a@gmail.com> (raw)

usb_conn_psy_register() allocates a connector ID, but usb_conn_probe()
does not release it if a later GPIO-to-IRQ conversion or IRQ request
fails. The failed probe therefore leaks the allocated ID.

Manage the ID with a devm action registered before the power supply. This
also makes the ID available only after the power supply has been
unregistered during teardown, avoiding premature reuse of its name.

Fixes: d4e5b10c5562 ("usb: common: usb-conn-gpio: use a unique name for usb connector device")
Reported-by: Changyul Lee <lcy8047@gmail.com>
Assisted-by: LLM
Signed-off-by: Sang-Hoon Choi <csh0052@gmail.com>
---
Compile-tested the affected object with x86_64 allmodconfig
and W=1 (GCC 13.3.0). Base: mainline 3b7cab693ba2bab63774bf5b988e8a61b2ef0f32.
No hardware testing or runtime failure reproduction was performed.

 drivers/usb/common/usb-conn-gpio.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/drivers/usb/common/usb-conn-gpio.c b/drivers/usb/common/usb-conn-gpio.c
index 421c3af38e06..128ab683f110 100644
--- a/drivers/usb/common/usb-conn-gpio.c
+++ b/drivers/usb/common/usb-conn-gpio.c
@@ -157,6 +157,13 @@ static int usb_charger_get_property(struct power_supply *psy,
 	return 0;
 }
 
+static void usb_conn_ida_free(void *data)
+{
+	struct usb_conn_info *info = data;
+
+	ida_free(&usb_conn_ida, info->conn_id);
+}
+
 static int usb_conn_psy_register(struct usb_conn_info *info)
 {
 	struct device *dev = info->dev;
@@ -164,17 +171,20 @@ static int usb_conn_psy_register(struct usb_conn_info *info)
 	struct power_supply_config cfg = {
 		.fwnode = dev_fwnode(dev),
 	};
+	int ret;
 
 	info->conn_id = ida_alloc(&usb_conn_ida, GFP_KERNEL);
 	if (info->conn_id < 0)
 		return info->conn_id;
 
+	ret = devm_add_action_or_reset(dev, usb_conn_ida_free, info);
+	if (ret)
+		return ret;
+
 	desc->name = devm_kasprintf(dev, GFP_KERNEL, "usb-charger-%d",
 				    info->conn_id);
-	if (!desc->name) {
-		ida_free(&usb_conn_ida, info->conn_id);
+	if (!desc->name)
 		return -ENOMEM;
-	}
 
 	desc->properties = usb_charger_properties;
 	desc->num_properties = ARRAY_SIZE(usb_charger_properties);
@@ -183,10 +193,8 @@ static int usb_conn_psy_register(struct usb_conn_info *info)
 	cfg.drv_data = info;
 
 	info->charger = devm_power_supply_register(dev, desc, &cfg);
-	if (IS_ERR(info->charger)) {
+	if (IS_ERR(info->charger))
 		dev_err(dev, "Unable to register charger %d\n", info->conn_id);
-		ida_free(&usb_conn_ida, info->conn_id);
-	}
 
 	return PTR_ERR_OR_ZERO(info->charger);
 }
@@ -294,9 +302,6 @@ static void usb_conn_remove(struct platform_device *pdev)
 
 	cancel_delayed_work_sync(&info->dw_det);
 
-	if (info->charger)
-		ida_free(&usb_conn_ida, info->conn_id);
-
 	if (info->last_role == USB_ROLE_HOST && info->vbus)
 		regulator_disable(info->vbus);
 

                 reply	other threads:[~2026-10-02 19:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261003.final019.b1f833b54899139a@gmail.com \
    --to=csh0052@gmail.com \
    --cc=chance.yang@kneron.us \
    --cc=gregkh@linuxfoundation.org \
    --cc=lcy8047@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.