Linux USB
 help / color / mirror / Atom feed
* [PATCH v2] usb: typec: ucsi: Enable notifications before registering connectors
@ 2026-12-04  1:26 Jameson Thies
  2026-12-04  1:53 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Jameson Thies @ 2026-12-04  1:26 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman
  Cc: linux-usb, linux-kernel, Benson Leung, Andrei Kuchynski,
	Jameson Thies

During ucsi_init(), the UCSI driver registers each connector and queries
its initial status before sending SET_NOTIFICATION_ENABLE to enable
connector change notifications. If a port's connection state changes
after its connector is registered but before notifications are enabled,
the PPM does not latch the connector change in CCI and the event is
lost, leaving the port in a stale state.

Move SET_NOTIFICATION_ENABLE before registering connectors, and publish
ucsi->connector with smp_store_release() only after all connectors are
registered. ucsi_connector_change() ignores events while ucsi->connector
is NULL without acknowledging them, so the change stays latched in CCI
for the existing read_cci() check at the end of ucsi_init(). Also set
ucsi->ntfy as soon as notifications are enabled, so the PPM reset in
ucsi_role_cmd() restores the full mask if it runs during registration.

Fixes: c1b0bc2dabfa ("usb: typec: Add support for UCSI interface")
Signed-off-by: Jameson Thies <jthies@google.com>
---
Changes in v2:
 - Add Fixes: tag.
 - Gate early connector change events on ucsi->connector using
   smp_store_release()/smp_load_acquire() instead of ucsi->ntfy.
 - Set ucsi->ntfy right after enabling notifications.

 drivers/usb/typec/ucsi/ucsi.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index 9e453d216c16..3c51d1da256e 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -1561,7 +1561,9 @@ void ucsi_connector_change(struct ucsi *ucsi, u8 num)
 {
 	struct ucsi_connector *con;
 
-	if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
+	/* Pairs with smp_store_release() in ucsi_init() */
+	con = smp_load_acquire(&ucsi->connector);
+	if (!con) {
 		dev_dbg(ucsi->dev, "Early connector change event\n");
 		return;
 	}
@@ -1573,7 +1575,7 @@ void ucsi_connector_change(struct ucsi *ucsi, u8 num)
 		return;
 	}
 
-	con = &ucsi->connector[num - 1];
+	con = &con[num - 1];
 
 	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
 		schedule_work(&con->work);
@@ -2126,22 +2128,6 @@ static int ucsi_init(struct ucsi *ucsi)
 	for (i = 0; i < ucsi->cap.num_connectors; i++)
 		lockdep_register_key(&connector[i].lock_key);
 
-	/* Register all connectors */
-	for (i = 0; i < ucsi->cap.num_connectors; i++) {
-		connector[i].num = i + 1;
-		ret = ucsi_register_port(ucsi, &connector[i]);
-		if (ret)
-			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;
@@ -2151,6 +2137,17 @@ static int ucsi_init(struct ucsi *ucsi)
 
 	ucsi->ntfy = ntfy;
 
+	/* Register all connectors */
+	for (i = 0; i < ucsi->cap.num_connectors; i++) {
+		connector[i].num = i + 1;
+		ret = ucsi_register_port(ucsi, &connector[i]);
+		if (ret)
+			goto err_unregister;
+	}
+
+	/* Pairs with smp_load_acquire() in ucsi_connector_change() */
+	smp_store_release(&ucsi->connector, connector);
+
 	mutex_lock(&ucsi->ppm_lock);
 	ret = ucsi->ops->read_cci(ucsi, &cci);
 	mutex_unlock(&ucsi->ppm_lock);

base-commit: 9357829ac604c28510b71b154cb6630af81226ff
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] usb: typec: ucsi: Enable notifications before registering connectors
  2026-12-04  1:26 [PATCH v2] usb: typec: ucsi: Enable notifications before registering connectors Jameson Thies
@ 2026-12-04  1:53 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04  1:53 UTC (permalink / raw)
  To: Jameson Thies; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] usb: typec: ucsi: UAF in ucsi_connector_change() on
  re-registration
- [Medium] usb: typec: ucsi: 2s EC IRQ stalls in gaokun_ucsi_notify()
  during init
- [Low] usb: typec: ucsi: missing smp_mb() between connector publish and
  read_cci()
--

commit 0cdc70b936d7ce3ae2f45a36f06d4808bd20f8c0
Author: Jameson Thies <jthies@google.com>

usb: typec: ucsi: Enable notifications before registering connectors

This commit enables UCSI notifications before registering connectors in
ucsi_init(), deferring the publication of ucsi->connector with
smp_store_release() until registration finishes and gating early events
in ucsi_connector_change() on ucsi->connector being NULL.

> diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> index 9e453d216c169..3c51d1da256ef 100644
> --- a/drivers/usb/typec/ucsi/ucsi.c
> +++ b/drivers/usb/typec/ucsi/ucsi.c
> @@ -1561,7 +1561,9 @@ void ucsi_connector_change(struct ucsi *ucsi, u8 num)
>  {
>  	struct ucsi_connector *con;
>  
> -	if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
> +	/* Pairs with smp_store_release() in ucsi_init() */
> +	con = smp_load_acquire(&ucsi->connector);
> +	if (!con) {
>  		dev_dbg(ucsi->dev, "Early connector change event\n");
>  		return;
>  	}
> @@ -1573,7 +1575,7 @@ void ucsi_connector_change(struct ucsi *ucsi, u8 num)
>  		return;
>  	}
>  
> -	con = &ucsi->connector[num - 1];
> +	con = &con[num - 1];
>  
>  	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
>  		schedule_work(&con->work);

[Severity: High]
Can this trigger a use-after-free on ucsi->connector when a UCSI device is
re-registered?

ucsi_unregister() frees ucsi->connector with kfree(ucsi->connector) without
setting it to NULL.  Drivers such as pmic_glink_ucsi_register() reuse the
same struct ucsi across ucsi_unregister() and ucsi_register() during PDR
recovery.

Because ucsi_init() now enables connector change notifications before
registering connectors and updating ucsi->connector, a connector change
arriving during ucsi_register_port() loads the dangling pointer and calls
schedule_work(&con->work) on freed memory.

Clearing ucsi->connector to NULL in ucsi_unregister() before freeing it
would close this.

> @@ -2126,22 +2128,6 @@ static int ucsi_init(struct ucsi *ucsi)

[ ... ]

>  	/* Enable all supported notifications */
>  	ntfy = ucsi_get_supported_notifications(ucsi);
>  	command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
> @@ -2151,6 +2137,17 @@ static int ucsi_init(struct ucsi *ucsi)
>  
>  	ucsi->ntfy = ntfy;
>  
> +	/* Register all connectors */
> +	for (i = 0; i < ucsi->cap.num_connectors; i++) {
> +		connector[i].num = i + 1;
> +		ret = ucsi_register_port(ucsi, &connector[i]);
> +		if (ret)
> +			goto err_unregister;
> +	}

[Severity: Medium]
Does keeping UCSI_CCI_CONNECTOR(cci) latched during ucsi_register_port()
cause repeated 2-second EC IRQ stalls on Huawei Gaokun hardware?

Because ucsi_connector_change() drops early events without acknowledging
them while ucsi->connector is NULL, UCSI_CCI_CONNECTOR(cci) stays non-zero
across every command and ACK in ucsi_register_port().

In drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c, gaokun_ucsi_notify() runs
inside the IRQF_ONESHOT EC IRQ thread and calls
gaokun_ucsi_handle_no_usb_event() whenever UCSI_CCI_CONNECTOR(cci) is set.

That waits in wait_for_completion_timeout(&port->usb_ack, 2 * HZ), masking
EC_EVENT_USB and UCSI completions.  On timeout, gaokun_ucsi_usb_notify_ind()
sees !uec->ucsi->connector and returns early without completing
port->usb_ack, so every remaining command and ACK stalls the EC IRQ for 2s.

> +
> +	/* Pairs with smp_load_acquire() in ucsi_connector_change() */
> +	smp_store_release(&ucsi->connector, connector);
> +
>  	mutex_lock(&ucsi->ppm_lock);
>  	ret = ucsi->ops->read_cci(ucsi, &cci);
>  	mutex_unlock(&ucsi->ppm_lock);

[Severity: Low]
Could a connector change event still be lost between publishing
ucsi->connector and reading CCI on weakly-ordered ACPI systems?

An uncontended mutex_lock() fastpath only provides acquire semantics, so
the release store to ucsi->connector does not prevent a later load from
reordering before it.  In ucsi_acpi, ucsi_acpi_read_cci() does a plain
memcpy() from cacheable RAM and ucsi_acpi_notify() does not take ppm_lock:

CPU0 (ucsi_init)                    CPU1 (ucsi_acpi_notify)
smp_store_release(&ucsi->connector,
                  connector)
mutex_lock(&ucsi->ppm_lock)
  /* acquire only; no smp_mb() */   /* EC updates UCSI_CCI */
ucsi_acpi_read_cci()                ucsi_acpi_read_cci() -> sees change
  /* reordered before store,        ucsi_connector_change()
     reads 0 */                       smp_load_acquire(&ucsi->connector)
                                        /* sees NULL */  <-- event lost

On RISC-V 64 (where arch_cmpxchg_acquire() emits only fence r, rw), this
reordering leaves the port in a stale state with the unacknowledged change
latched in CCI.  Adding smp_mb() between smp_store_release() and read_cci()
would prevent this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204012637.2469388-1-jthies@google.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04  1:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  1:26 [PATCH v2] usb: typec: ucsi: Enable notifications before registering connectors Jameson Thies
2026-12-04  1:53 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox