Skip to content

Commit d4317c3

Browse files
RD Babierasmb49
authored andcommitted
usb: typec: tcpm: move tcpm_queue_vdm_unlocked to asynchronous work
BugLink: https://bugs.launchpad.net/bugs/2119603 commit 324d45e53f1a36c88bc649dc39e0c8300a41be0a upstream. A state check was previously added to tcpm_queue_vdm_unlocked to prevent a deadlock where the DisplayPort Alt Mode driver would be executing work and attempting to grab the tcpm_lock while the TCPM was holding the lock and attempting to unregister the altmode, blocking on the altmode driver's cancel_work_sync call. Because the state check isn't protected, there is a small window where the Alt Mode driver could determine that the TCPM is in a ready state and attempt to grab the lock while the TCPM grabs the lock and changes the TCPM state to one that causes the deadlock. The callstack is provided below: [110121.667392][ C7] Call trace: [110121.667396][ C7] __switch_to+0x174/0x338 [110121.667406][ C7] __schedule+0x608/0x9f0 [110121.667414][ C7] schedule+0x7c/0xe8 [110121.667423][ C7] kernfs_drain+0xb0/0x114 [110121.667431][ C7] __kernfs_remove+0x16c/0x20c [110121.667436][ C7] kernfs_remove_by_name_ns+0x74/0xe8 [110121.667442][ C7] sysfs_remove_group+0x84/0xe8 [110121.667450][ C7] sysfs_remove_groups+0x34/0x58 [110121.667458][ C7] device_remove_groups+0x10/0x20 [110121.667464][ C7] device_release_driver_internal+0x164/0x2e4 [110121.667475][ C7] device_release_driver+0x18/0x28 [110121.667484][ C7] bus_remove_device+0xec/0x118 [110121.667491][ C7] device_del+0x1e8/0x4ac [110121.667498][ C7] device_unregister+0x18/0x38 [110121.667504][ C7] typec_unregister_altmode+0x30/0x44 [110121.667515][ C7] tcpm_reset_port+0xac/0x370 [110121.667523][ C7] tcpm_snk_detach+0x84/0xb8 [110121.667529][ C7] run_state_machine+0x4c0/0x1b68 [110121.667536][ C7] tcpm_state_machine_work+0x94/0xe4 [110121.667544][ C7] kthread_worker_fn+0x10c/0x244 [110121.667552][ C7] kthread+0x104/0x1d4 [110121.667557][ C7] ret_from_fork+0x10/0x20 [110121.667689][ C7] Workqueue: events dp_altmode_work [110121.667697][ C7] Call trace: [110121.667701][ C7] __switch_to+0x174/0x338 [110121.667710][ C7] __schedule+0x608/0x9f0 [110121.667717][ C7] schedule+0x7c/0xe8 [110121.667725][ C7] schedule_preempt_disabled+0x24/0x40 [110121.667733][ C7] __mutex_lock+0x408/0xdac [110121.667741][ C7] __mutex_lock_slowpath+0x14/0x24 [110121.667748][ C7] mutex_lock+0x40/0xec [110121.667757][ C7] tcpm_altmode_enter+0x78/0xb4 [110121.667764][ C7] typec_altmode_enter+0xdc/0x10c [110121.667769][ C7] dp_altmode_work+0x68/0x164 [110121.667775][ C7] process_one_work+0x1e4/0x43c [110121.667783][ C7] worker_thread+0x25c/0x430 [110121.667789][ C7] kthread+0x104/0x1d4 [110121.667794][ C7] ret_from_fork+0x10/0x20 Change tcpm_queue_vdm_unlocked to queue for tcpm_queue_vdm_work, which can perform the state check while holding the TCPM lock while the Alt Mode lock is no longer held. This requires a new struct to hold the vdm data, altmode_vdm_event. Fixes: cdc9946 ("usb: typec: tcpm: enforce ready state when queueing alt mode vdm") Cc: stable <stable@kernel.org> Signed-off-by: RD Babiera <rdbabiera@google.com> Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com> Reviewed-by: Badhri Jagan Sridharan <badhri@google.com> Link: https://lore.kernel.org/r/20250506232853.1968304-2-rdbabiera@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> CVE-2025-38268 Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Mehmet Basaran <mehmet.basaran@canonical.com>
1 parent 7329753 commit d4317c3

File tree

1 file changed

+71
-20
lines changed

1 file changed

+71
-20
lines changed

drivers/usb/typec/tcpm/tcpm.c

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,15 @@ struct pd_rx_event {
596596
enum tcpm_transmit_type rx_sop_type;
597597
};
598598

599+
struct altmode_vdm_event {
600+
struct kthread_work work;
601+
struct tcpm_port *port;
602+
u32 header;
603+
u32 *data;
604+
int cnt;
605+
enum tcpm_transmit_type tx_sop_type;
606+
};
607+
599608
static const char * const pd_rev[] = {
600609
[PD_REV10] = "rev1",
601610
[PD_REV20] = "rev2",
@@ -1608,18 +1617,68 @@ static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
16081617
mod_vdm_delayed_work(port, 0);
16091618
}
16101619

1611-
static void tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
1612-
const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
1620+
static void tcpm_queue_vdm_work(struct kthread_work *work)
16131621
{
1614-
if (port->state != SRC_READY && port->state != SNK_READY &&
1615-
port->state != SRC_VDM_IDENTITY_REQUEST)
1616-
return;
1622+
struct altmode_vdm_event *event = container_of(work,
1623+
struct altmode_vdm_event,
1624+
work);
1625+
struct tcpm_port *port = event->port;
16171626

16181627
mutex_lock(&port->lock);
1619-
tcpm_queue_vdm(port, header, data, cnt, tx_sop_type);
1628+
if (port->state != SRC_READY && port->state != SNK_READY &&
1629+
port->state != SRC_VDM_IDENTITY_REQUEST) {
1630+
tcpm_log_force(port, "dropping altmode_vdm_event");
1631+
goto port_unlock;
1632+
}
1633+
1634+
tcpm_queue_vdm(port, event->header, event->data, event->cnt, event->tx_sop_type);
1635+
1636+
port_unlock:
1637+
kfree(event->data);
1638+
kfree(event);
16201639
mutex_unlock(&port->lock);
16211640
}
16221641

1642+
static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
1643+
const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
1644+
{
1645+
struct altmode_vdm_event *event;
1646+
u32 *data_cpy;
1647+
int ret = -ENOMEM;
1648+
1649+
event = kzalloc(sizeof(*event), GFP_KERNEL);
1650+
if (!event)
1651+
goto err_event;
1652+
1653+
data_cpy = kcalloc(cnt, sizeof(u32), GFP_KERNEL);
1654+
if (!data_cpy)
1655+
goto err_data;
1656+
1657+
kthread_init_work(&event->work, tcpm_queue_vdm_work);
1658+
event->port = port;
1659+
event->header = header;
1660+
memcpy(data_cpy, data, sizeof(u32) * cnt);
1661+
event->data = data_cpy;
1662+
event->cnt = cnt;
1663+
event->tx_sop_type = tx_sop_type;
1664+
1665+
ret = kthread_queue_work(port->wq, &event->work);
1666+
if (!ret) {
1667+
ret = -EBUSY;
1668+
goto err_queue;
1669+
}
1670+
1671+
return 0;
1672+
1673+
err_queue:
1674+
kfree(data_cpy);
1675+
err_data:
1676+
kfree(event);
1677+
err_event:
1678+
tcpm_log_force(port, "failed to queue altmode vdm, err:%d", ret);
1679+
return ret;
1680+
}
1681+
16231682
static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt)
16241683
{
16251684
u32 vdo = p[VDO_INDEX_IDH];
@@ -2830,8 +2889,7 @@ static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo)
28302889
header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
28312890
header |= VDO_OPOS(altmode->mode);
28322891

2833-
tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP);
2834-
return 0;
2892+
return tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP);
28352893
}
28362894

28372895
static int tcpm_altmode_exit(struct typec_altmode *altmode)
@@ -2847,18 +2905,15 @@ static int tcpm_altmode_exit(struct typec_altmode *altmode)
28472905
header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
28482906
header |= VDO_OPOS(altmode->mode);
28492907

2850-
tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP);
2851-
return 0;
2908+
return tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP);
28522909
}
28532910

28542911
static int tcpm_altmode_vdm(struct typec_altmode *altmode,
28552912
u32 header, const u32 *data, int count)
28562913
{
28572914
struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
28582915

2859-
tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP);
2860-
2861-
return 0;
2916+
return tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP);
28622917
}
28632918

28642919
static const struct typec_altmode_ops tcpm_altmode_ops = {
@@ -2882,8 +2937,7 @@ static int tcpm_cable_altmode_enter(struct typec_altmode *altmode, enum typec_pl
28822937
header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
28832938
header |= VDO_OPOS(altmode->mode);
28842939

2885-
tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP_PRIME);
2886-
return 0;
2940+
return tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP_PRIME);
28872941
}
28882942

28892943
static int tcpm_cable_altmode_exit(struct typec_altmode *altmode, enum typec_plug_index sop)
@@ -2899,18 +2953,15 @@ static int tcpm_cable_altmode_exit(struct typec_altmode *altmode, enum typec_plu
28992953
header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
29002954
header |= VDO_OPOS(altmode->mode);
29012955

2902-
tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP_PRIME);
2903-
return 0;
2956+
return tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP_PRIME);
29042957
}
29052958

29062959
static int tcpm_cable_altmode_vdm(struct typec_altmode *altmode, enum typec_plug_index sop,
29072960
u32 header, const u32 *data, int count)
29082961
{
29092962
struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
29102963

2911-
tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP_PRIME);
2912-
2913-
return 0;
2964+
return tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP_PRIME);
29142965
}
29152966

29162967
static const struct typec_cable_ops tcpm_cable_ops = {

0 commit comments

Comments
 (0)