Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jameson Thies" <jthies@google.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v2] usb: typec: ucsi: Enable notifications before registering connectors
Date: Fri, 04 Dec 2026 01:53:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-248918@kernel.org> (raw)
In-Reply-To: <20261204012637.2469388-1-jthies@google.com>

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

      reply	other threads:[~2026-12-04  1:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=sashiko-outbox-248918@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=jthies@google.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox