svdm_consume_identity() clears port->mode_data when a Discover Identity ACK is received. mode_data.altmodes is also the count used to walk port->partner_altmode[] when unregistering the partner alternate modes, so if the partner altmodes from an earlier discovery are still registered, clearing it leaks them and the following Discover Modes round registers a second set for the same partner. Unregister the partner altmodes before clearing mode_data, and reuse the new helper in tcpm_unregister_altmodes(). Cc: stable@vger.kernel.org Fixes: f0690a25a140 ("staging: typec: USB Type-C Port Manager (tcpm)") Assisted-by: LLM Reviewed-by: Heikki Krogerus Signed-off-by: Neil Armstrong --- drivers/usb/typec/tcpm/tcpm.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c index ca38e05f831a..20e18bca649e 100644 --- a/drivers/usb/typec/tcpm/tcpm.c +++ b/drivers/usb/typec/tcpm/tcpm.c @@ -2068,6 +2068,17 @@ static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header, return ret; } +static void tcpm_unregister_partner_altmodes(struct tcpm_port *port) +{ + struct pd_mode_data *modep = &port->mode_data; + int i; + + for (i = 0; i < modep->altmodes && i < ALTMODE_DISCOVERY_MAX; i++) { + typec_unregister_altmode(port->partner_altmode[i]); + port->partner_altmode[i] = NULL; + } +} + static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) { u32 vdo = p[VDO_INDEX_IDH]; @@ -2076,6 +2087,7 @@ static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) if (cnt <= VDO_INDEX_PRODUCT) return; + tcpm_unregister_partner_altmodes(port); memset(&port->mode_data, 0, sizeof(port->mode_data)); port->partner_ident.id_header = vdo; @@ -5137,10 +5149,7 @@ static void tcpm_unregister_altmodes(struct tcpm_port *port) struct pd_mode_data *modep_prime = &port->mode_data_prime; int i; - for (i = 0; i < modep->altmodes && i < ALTMODE_DISCOVERY_MAX; i++) { - typec_unregister_altmode(port->partner_altmode[i]); - port->partner_altmode[i] = NULL; - } + tcpm_unregister_partner_altmodes(port); for (i = 0; i < modep_prime->altmodes && i < ALTMODE_DISCOVERY_MAX; i++) { typec_unregister_altmode(port->plug_prime_altmode[i]); port->plug_prime_altmode[i] = NULL; -- 2.34.1