Re: [PATCH v2] usb: typec: ucsi: Set ucsi->connector before enabling notifications

From: Uttkarsh Aggarwal

Date: Mon Oct 05 2026 - 06:08:14 EST


On Wed, Sep 30, 2026 at 2:20 PM Peter Chen <peter.chen@xxxxxxxxxxxxxxxx> wrote:
>
> On 26-09-29 14:59:39, 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.
> >
> > 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.
> >
> > Fixes: 0482c34ec6f8 ("usb: ucsi: Fix ucsi->connector race")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Uttkarsh Aggarwal <uttkarsh.aggarwal@xxxxxxxxxxxxxxxx>
>
> Reviewed-by: Peter Chen <peter.chen@xxxxxxxxxxxxxxxx>
>
> It is better cc the one who gave you the comment :)
>
> Peter
> > ---
> > Changes in v2:
> > - Add Fixes tag pointing to 0482c34ec6f8 ("usb: ucsi: Fix ucsi->connector race").
> > - Add Cc: stable@xxxxxxxxxxxxxxx.
> > - Link to v1: https://lore.kernel.org/all/20260923060434.2599545-1-uttkarsh.aggarwal@xxxxxxxxxxxxxxxx/
> >
> > 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

Sure, Peter. I will take care of it. Thanks for the review.

Thanks,
Uttkarsh