usb: typec: ucsi: Fix missing typec_unregister_port on error paths
Subsystems (2)
PATCH 1/1 usb: typec: ucsi: Fix missing typec_unregister_port on error paths
The ucsi_register_port() registers a Type-C port with typec_register_port(), but several error paths after a successful registration returned directly to cleanup without calling typec_unregister_port(). This lead to a potential resource leak.
Add a common error-unwind path that calls typec_unregister_port() for all failures to ensure proper cleanup.
--- drivers/usb/typec/ucsi/ucsi.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +6 -2
@@ -1656,14 +1656,14 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)if (ret) {dev_err (ucsi->dev,"con%d: failed to register alt modes\n" , con->num); - goto out; + goto out_unregister; }/* Get the status */ ret =ucsi_get_connector_status (con, false);if (ret) {dev_err (ucsi->dev,"con%d: failed to get status\n" , con->num); - goto out; + goto out_unregister; }if (ucsi->ops->connector_status)
@@ -1717,6 +1717,10 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)trace_ucsi_register_port (con->num, con); + goto out; + +out_unregister: +typec_unregister_port (con->port); out:fwnode_handle_put (cap->fwnode); out_unlock:
Signature · 1 line
2.25.1
Greg Kroah-Hartman
maintainerQuoted 10 lines · Haotian Zhang
The
ucsi_register_port()registers a Type-C port withtypec_register_port(), but several error paths after a successful registration returned directly to cleanup without callingtypec_unregister_port(). This lead to a potential resource leak.Add a common error-unwind path that calls
typec_unregister_port()for all failures to ensure proper cleanup.Fixes c1b0bc2dabfa ("usb: typec: Add support for UCSI interface")Signed-off-by Haotian Zhang <[email protected]>
How was this found?
How was this tested?
thanks,
greg k-h
Heikki Krogerus
maintainerMon, Nov 24, 2025 at 08:20:30PM +0800, Haotian Zhang kirjoitti:
Quoted 36 lines
The
ucsi_register_port()registers a Type-C port withtypec_register_port(), but several error paths after a successful registration returned directly to cleanup without callingtypec_unregister_port(). This lead to a potential resource leak.Add a common error-unwind path that calls
typec_unregister_port()for all failures to ensure proper cleanup.Fixes c1b0bc2dabfa ("usb: typec: Add support for UCSI interface")Signed-off-by Haotian Zhang <[email protected]>--- drivers/usb/typec/ucsi/ucsi.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/usb/typec/ucsi/ucsi.c +2
-2@@ -1656,14 +1656,14 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)if (ret) {dev_err (ucsi->dev,"con%d: failed to register alt modes\n" , con->num); - goto out; + goto out_unregister; }/* Get the status */ ret =ucsi_get_connector_status (con, false);if (ret) {dev_err (ucsi->dev,"con%d: failed to get status\n" , con->num); - goto out; + goto out_unregister; }if (ucsi->ops->connector_status)
drivers/usb/typec/ucsi/ucsi.c static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) · line 1723
Quoted 7 lines
drivers/usb/typec/ucsi/ucsi.c +4
-0@@ -1717,6 +1717,10 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)trace_ucsi_register_port (con->num, con); + goto out; + +out_unregister: +typec_unregister_port (con->port);
No. We don't destroy the port if status or alt modes fail. The alt modes can't be considered critical because the firmware quite often claims alt modes are supported even though they are not. That should be explained, if not in documentation, then at least in the commit messages.
Try something like this instead.
drivers/usb/typec/ucsi/ucsi.c +2 -0
@@ -1682,6 +1682,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)if (ret) {dev_err (ucsi->dev,"con%d: failed to register alt modes\n" , con->num); + ret =0 ; goto out; }
@@ -1689,6 +1690,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) ret =ucsi_get_connector_status (con, false);if (ret) {dev_err (ucsi->dev,"con%d: failed to get status\n" , con->num); + ret =0 ; goto out; }
And the commit you want to fix is b9aa02ca39a4 ("usb: typec: ucsi: Add polling mechanism for partner tasks like alt mode checking").
thanks,
Signature · 1 line
heikki
PATCH v2 1/1 usb: typec: ucsi: Treat alt mode and status failures as non-fatal
In reply to Haotian ZhangThe ucsi_register_altmodes() and ucsi_get_connector_status() can fail for reasons that should not prevent the port from being registered. These failures should be logged but should not cause the entire port registration to fail.
Set ret to 0 in these error paths to allow registration to continue.
---
Changes in v2:
- Instead of unregistering the port on alt mode/status failures, treat these errors as non-fatal by setting ret = 0
- Changed Fixes tag to b9aa02ca39a4.
--- drivers/usb/typec/ucsi/ucsi.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/usb/typec/ucsi/ucsi.c +2 -0
@@ -1656,6 +1656,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)if (ret) {dev_err (ucsi->dev,"con%d: failed to register alt modes\n" , con->num); + ret =0 ; goto out; }
@@ -1663,6 +1664,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) ret =ucsi_get_connector_status (con, false);if (ret) {dev_err (ucsi->dev,"con%d: failed to get status\n" , con->num); + ret =0 ; goto out; }
Signature · 1 line
2.25.1
Greg Kroah-Hartman
maintainerRe: [PATCH v2] usb: typec: ucsi: Treat alt mode and status failures as non-fatal
Quoted 43 lines · Haotian Zhang
The
ucsi_register_altmodes()anducsi_get_connector_status()can fail for reasons that should not prevent the port from being registered. These failures should be logged but should not cause the entire port registration to fail.Set ret to 0 in these error paths to allow registration to continue.
Fixes b9aa02ca39a4 ("usb: typec: ucsi: Add polling mechanism for partner tasks like alt mode checking")Suggested-by Heikki Krogerus <[email protected]>Signed-off-by Haotian Zhang <[email protected]>---
Changes in v2:
- Instead of unregistering the port on alt mode/status failures, treat these errors as non-fatal by setting ret = 0
- Changed Fixes tag tob9aa02ca39a4.--- drivers/usb/typec/ucsi/ucsi.c | 2 ++ 1 file changed, 2 insertions(+)drivers/usb/typec/ucsi/ucsi.c +2
-0@@ -1656,6 +1656,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)if (ret) {dev_err (ucsi->dev,"con%d: failed to register alt modes\n" , con->num); + ret =0 ; goto out; }@@ -1663,6 +1664,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) ret =ucsi_get_connector_status (con, false);if (ret) {dev_err (ucsi->dev,"con%d: failed to get status\n" , con->num); + ret =0 ; goto out; }Signature · 1 line
2.25.1
Hi,
This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.
You are receiving this message because of the following common error(s) as indicated below:
- You have marked a patch with a "Fixes:" tag for a commit that is in an older released kernel, yet you do not have a cc: stable line in the signed-off-by area at all, which means that the patch will not be applied to any older kernel releases. To properly fix this, please follow the documented rules in the Documentation/process/stable-kernel-rules.rst file for how to resolve this.
If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.
thanks,
greg k-h's patch email bot