Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexey Charkov" <alchark@flipper.net>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH 3/3] usb: typec: tcpm: Keep cable plug messages away from the port partner
Date: Tue, 17 Nov 2026 11:42:58 +0000	[thread overview]
Message-ID: <sashiko-outbox-218719@kernel.org> (raw)
In-Reply-To: <20261117-tcpm-soft-reset-sop-v1-3-c7e1543828c0@flipper.net>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] usb: typec: tcpm: SOP' VDMs in tcpm_pd_cable_msg_request()
  affect partner
- [Medium] usb: typec: tcpm: SOP' PD_CTRL_REJECT in
  tcpm_pd_ctrl_request() resets partner
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] usb: typec: tcpm: infinite discovery retry loop in
  tcpm_pd_ctrl_request() [1]
--

commit a17153f1f38f59679f1ba169d5aec3d715a00c73
Author: Alexey Charkov <alchark@flipper.net>

usb: typec: tcpm: Keep cable plug messages away from the port partner

This commit filters incoming SOP' messages in tcpm_pd_rx_handler() through
tcpm_pd_cable_msg_request() so that only VDMs and expected control responses
from a cable plug are processed, while dropping unsupported SOP* types before
updating MessageID tracking.

> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 947fca918414e..2a909ca96811d 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
[ ... ]
> @@ -4164,6 +4166,52 @@ static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
>  	}
>  }
>  
> +/* Whether a cable plug control message responds to what the port sent it */
> +static bool tcpm_cable_ctrl_msg_expected(struct tcpm_port *port,
> +					 enum pd_ctrl_msg_type type)
> +{
> +	switch (type) {
> +	case PD_CTRL_ACCEPT:
> +		/* the Soft_Reset after a VCONN Swap */
> +		return port->state == SOFT_RESET_SEND &&
> +		       port->soft_reset_sent_prime;
> +	case PD_CTRL_REJECT:
> +	case PD_CTRL_NOT_SUPP:
> +		/* a VDM, in the ready state, which takes it as the answer */
> +		return port->vdm_sop_type == TCPC_TX_SOP_PRIME &&
> +		       port->vdm_state > VDM_STATE_READY &&
> +		       port->state == ready_state(port);
> +	default:
> +		return false;
> +	}
> +}

[Severity: Medium]
When a cable plug replies to a structured VDM with PD_CTRL_REJECT in the
ready state, does forwarding it to tcpm_pd_ctrl_request() still soft-reset
the port partner?

At the top of tcpm_pd_ctrl_request(), any control message other than
PD_CTRL_NOT_SUPP and PD_CTRL_GOOD_CRC aborts an active VDM AMS before the
switch statement:

drivers/usb/typec/tcpm/tcpm.c:tcpm_pd_ctrl_request() {
    ...
	if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) {
		port->vdm_state = VDM_STATE_ERR_BUSY;
		tcpm_ams_finish(port);
		mod_vdm_delayed_work(port, 0);
	}
    ...
}

Because port->vdm_state is overwritten with VDM_STATE_ERR_BUSY (-3), the
subsequent port->vdm_state > VDM_STATE_READY check in the SRC_READY and
SNK_READY cases evaluates to false, falling through to the default branch
and triggering a soft reset against the port partner on SOP.

> +
> +/*
> + * A cable plug only responds to the VCONN source: to its VDMs, and to its
> + * Soft_Reset. Hand those to the usual handlers, which tell SOP' apart where
> + * it matters, and drop anything else on SOP': it comes from a cable plug
> + * that breaks the rules, and no sequence has the port answer a cable plug.
> + */
> +static void tcpm_pd_cable_msg_request(struct tcpm_port *port,
> +				      const struct pd_message *msg)
> +{
> +	unsigned int type = pd_header_type_le(msg->header);
> +	bool ext = le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR;
> +	bool ctrl = !ext && !pd_header_cnt_le(msg->header);
> +
> +	if (!ext && !ctrl && type == PD_DATA_VENDOR_DEF) {
> +		tcpm_pd_data_request(port, msg, TCPC_TX_SOP_PRIME);
> +		return;
> +	}

[Severity: Medium]
Can an unexpected or malformed SOP' VDM passed to tcpm_pd_data_request()
still cause the port to send messages to or reset the port partner on SOP?

Unlike tcpm_cable_ctrl_msg_expected(), this path does not check whether the
port is waiting for an SOP' VDM response.  In tcpm_handle_vdm_request(), an
unstructured SOP' VDM (or an SVDM when no VDM AMS, partner altmode, or sink
VDO is active) hits the else branch and calls
tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS), which transmits
Not_Supported on SOP or initiates a soft or hard reset of the port partner:

drivers/usb/typec/tcpm/tcpm.c:tcpm_handle_vdm_request() {
    ...
	if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) {
		port->vdm_sm_running = true;
		rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action,
				    rx_sop_type, &response_tx_sop_type);
	} else {
		if (port->negotiated_rev >= PD_REV30)
			tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
	}
    ...
}

In addition, if the port is waiting for an SOP VDM response from the partner
(port->vdm_state == VDM_STATE_BUSY && port->vdm_sop_type == TCPC_TX_SOP), an
unsolicited SOP' VDM mutates port->vdm_state and port->vdo_retry for the
partner VDM state machine.  Finally, an SOP' SVDM that hits a CMDT_RSP_NAK
path in tcpm_pd_svdm() leaves response_tx_sop_type at its default of
TCPC_TX_SOP and queues the VDM NAK to the port partner.

> +	if (ctrl && tcpm_cable_ctrl_msg_expected(port, type)) {
> +		tcpm_pd_ctrl_request(port, msg, TCPC_TX_SOP_PRIME);
> +		return;
> +	}

[Severity: High]
This problem wasn't introduced by this patch, but when
tcpm_pd_ctrl_request() handles PD_CTRL_NOT_SUPP or PD_CTRL_REJECT for an
in-flight VDM in SRC_READY or SNK_READY [1], does it leave
port->vdm_discovery_state unadvanced and cause an infinite discovery retry
loop?

In tcpm_pd_ctrl_request(), the SRC_READY and SNK_READY handler sets
port->vdm_state = VDM_STATE_DONE and finishes the VDM AMS without updating
port->vdm_discovery_state:

drivers/usb/typec/tcpm/tcpm.c:tcpm_pd_ctrl_request() {
    ...
		case SRC_READY:
		case SNK_READY:
			if (port->vdm_state > VDM_STATE_READY) {
				port->vdm_state = VDM_STATE_DONE;
				if (tcpm_vdm_ams(port))
					tcpm_ams_finish(port);
				mod_vdm_delayed_work(port, 0);
				break;
			}
    ...
}

When a discovery command such as DISCOVER_SVIDS or DISCOVER_MODES is
answered with PD_CTRL_NOT_SUPP, port->vdm_discovery_state remains in its
intermediate phase instead of advancing, so subsequent state machine runs in
SRC_READY or SNK_READY that schedule mod_vdm_discovery_delayed_work(port, 0)
repeatedly re-send the same rejected VDM command.

[1] https://sashiko.dev/#/bug/linux-2c1789d2-5c2d-407e-af88-42956bee42c7

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-tcpm-soft-reset-sop-v1-0-c7e1543828c0@flipper.net?part=3

      reply	other threads:[~2026-11-17 11:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 11:15 [PATCH 0/3] usb: typec: tcpm: Fixes for talking to the cable plug as a VCONN source Alexey Charkov
2026-11-17 11:15 ` [PATCH 1/3] usb: typec: tcpm: Keep the Soft_Reset target apart from the VDM one Alexey Charkov
2026-11-17 11:30   ` sashiko-bot
2026-11-17 11:15 ` [PATCH 2/3] usb: typec: tcpm: Leave SRC_VDM_IDENTITY_REQUEST whatever the cable answers Alexey Charkov
2026-11-17 11:39   ` sashiko-bot
2026-11-17 11:15 ` [PATCH 3/3] usb: typec: tcpm: Keep cable plug messages away from the port partner Alexey Charkov
2026-11-17 11:42   ` 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-218719@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alchark@flipper.net \
    --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