drivers/usb/typec/ucsi/ucsi.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
In ucsi_init(), ucsi->connector is assigned only after the
UCSI_SET_NOTIFICATION_ENABLE command completes. But once that command
is sent, the PPM is free to raise a connector change notification at
any time, which is handled through:
ucsi_notify_common() -> ucsi_connector_change()
ucsi_connector_change() indexes into ucsi->connector to get the
connector and schedule its work item. If this notification arrives
before ucsi_init() reaches the "ucsi->connector = connector;" line,
ucsi->connector is still NULL and the driver crashes dereferencing it.
Fix this by setting ucsi->connector right after the connectors are
registered, before notifications are enabled, so it is always valid
by the time a connector change event can be delivered. Also clear
ucsi->connector in the err_unregister path so it isn't left pointing
at freed memory if a later step in ucsi_init() fails.
Signed-off-by: Uttkarsh Aggarwal <uttkarsh.aggarwal@oss.qualcomm.com>
---
drivers/usb/typec/ucsi/ucsi.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index bef3f9b71d71..8f7b73a9f4f4 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -2122,6 +2122,14 @@ static int ucsi_init(struct ucsi *ucsi)
goto err_unregister;
}
+ /*
+ * Set ucsi->connector before enabling notifications. A connector
+ * change event can be handled as soon as the notifications are
+ * enabled below, and ucsi_connector_change() indexes into
+ * ucsi->connector, so it must not be NULL at that point.
+ */
+ ucsi->connector = connector;
+
/* Enable all supported notifications */
ntfy = ucsi_get_supported_notifications(ucsi);
command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
@@ -2129,7 +2137,6 @@ static int ucsi_init(struct ucsi *ucsi)
if (ret < 0)
goto err_unregister;
- ucsi->connector = connector;
ucsi->ntfy = ntfy;
mutex_lock(&ucsi->ppm_lock);
@@ -2143,6 +2150,7 @@ static int ucsi_init(struct ucsi *ucsi)
return 0;
err_unregister:
+ ucsi->connector = NULL;
for (con = connector; con->port; con++)
ucsi_unregister_port(con);
for (i = 0; i < ucsi->cap.num_connectors; i++)
--
2.34.1
On 26-09-23 11:34:34, Uttkarsh Aggarwal wrote: > In ucsi_init(), ucsi->connector is assigned only after the > UCSI_SET_NOTIFICATION_ENABLE command completes. But once that command > is sent, the PPM is free to raise a connector change notification at > any time, which is handled through: > > ucsi_notify_common() -> ucsi_connector_change() > > ucsi_connector_change() indexes into ucsi->connector to get the > connector and schedule its work item. If this notification arrives > before ucsi_init() reaches the "ucsi->connector = connector;" line, > ucsi->connector is still NULL and the driver crashes dereferencing it. If you have logs for it, you can attach it. > > Fix this by setting ucsi->connector right after the connectors are > registered, before notifications are enabled, so it is always valid > by the time a connector change event can be delivered. Also clear > ucsi->connector in the err_unregister path so it isn't left pointing > at freed memory if a later step in ucsi_init() fails. > > Signed-off-by: Uttkarsh Aggarwal <uttkarsh.aggarwal@oss.qualcomm.com> It seems it is a bug. You could add Fixed-by tag and Cc to stable tree. Peter > --- > drivers/usb/typec/ucsi/ucsi.c | 10 +++++++++- > 1 file changed, 9 insertions(+), 1 deletion(-) > > diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c > index bef3f9b71d71..8f7b73a9f4f4 100644 > --- a/drivers/usb/typec/ucsi/ucsi.c > +++ b/drivers/usb/typec/ucsi/ucsi.c > @@ -2122,6 +2122,14 @@ static int ucsi_init(struct ucsi *ucsi) > goto err_unregister; > } > > + /* > + * Set ucsi->connector before enabling notifications. A connector > + * change event can be handled as soon as the notifications are > + * enabled below, and ucsi_connector_change() indexes into > + * ucsi->connector, so it must not be NULL at that point. > + */ > + ucsi->connector = connector; > + > /* Enable all supported notifications */ > ntfy = ucsi_get_supported_notifications(ucsi); > command = UCSI_SET_NOTIFICATION_ENABLE | ntfy; > @@ -2129,7 +2137,6 @@ static int ucsi_init(struct ucsi *ucsi) > if (ret < 0) > goto err_unregister; > > - ucsi->connector = connector; > ucsi->ntfy = ntfy; > > mutex_lock(&ucsi->ppm_lock); > @@ -2143,6 +2150,7 @@ static int ucsi_init(struct ucsi *ucsi) > return 0; > > err_unregister: > + ucsi->connector = NULL; > for (con = connector; con->port; con++) > ucsi_unregister_port(con); > for (i = 0; i < ucsi->cap.num_connectors; i++) > -- > 2.34.1 > > -- Thanks, Peter Chen
© 2016 - 2026 Red Hat, Inc.