Skip to content

Commit 7bdd712

Browse files
RD Babieragregkh
authored andcommitted
usb: typec: tcpm: move tcpm_queue_vdm_unlocked to asynchronous work
commit 324d45e 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 <[email protected]> Signed-off-by: RD Babiera <[email protected]> Reviewed-by: Heikki Krogerus <[email protected]> Reviewed-by: Badhri Jagan Sridharan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 9f907ee commit 7bdd712

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
@@ -568,6 +568,15 @@ struct pd_rx_event {
568568
enum tcpm_transmit_type rx_sop_type;
569569
};
570570

571+
struct altmode_vdm_event {
572+
struct kthread_work work;
573+
struct tcpm_port *port;
574+
u32 header;
575+
u32 *data;
576+
int cnt;
577+
enum tcpm_transmit_type tx_sop_type;
578+
};
579+
571580
static const char * const pd_rev[] = {
572581
[PD_REV10] = "rev1",
573582
[PD_REV20] = "rev2",
@@ -1562,18 +1571,68 @@ static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
15621571
mod_vdm_delayed_work(port, 0);
15631572
}
15641573

1565-
static void tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
1566-
const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
1574+
static void tcpm_queue_vdm_work(struct kthread_work *work)
15671575
{
1568-
if (port->state != SRC_READY && port->state != SNK_READY &&
1569-
port->state != SRC_VDM_IDENTITY_REQUEST)
1570-
return;
1576+
struct altmode_vdm_event *event = container_of(work,
1577+
struct altmode_vdm_event,
1578+
work);
1579+
struct tcpm_port *port = event->port;
15711580

15721581
mutex_lock(&port->lock);
1573-
tcpm_queue_vdm(port, header, data, cnt, tx_sop_type);
1582+
if (port->state != SRC_READY && port->state != SNK_READY &&
1583+
port->state != SRC_VDM_IDENTITY_REQUEST) {
1584+
tcpm_log_force(port, "dropping altmode_vdm_event");
1585+
goto port_unlock;
1586+
}
1587+
1588+
tcpm_queue_vdm(port, event->header, event->data, event->cnt, event->tx_sop_type);
1589+
1590+
port_unlock:
1591+
kfree(event->data);
1592+
kfree(event);
15741593
mutex_unlock(&port->lock);
15751594
}
15761595

1596+
static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
1597+
const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
1598+
{
1599+
struct altmode_vdm_event *event;
1600+
u32 *data_cpy;
1601+
int ret = -ENOMEM;
1602+
1603+
event = kzalloc(sizeof(*event), GFP_KERNEL);
1604+
if (!event)
1605+
goto err_event;
1606+
1607+
data_cpy = kcalloc(cnt, sizeof(u32), GFP_KERNEL);
1608+
if (!data_cpy)
1609+
goto err_data;
1610+
1611+
kthread_init_work(&event->work, tcpm_queue_vdm_work);
1612+
event->port = port;
1613+
event->header = header;
1614+
memcpy(data_cpy, data, sizeof(u32) * cnt);
1615+
event->data = data_cpy;
1616+
event->cnt = cnt;
1617+
event->tx_sop_type = tx_sop_type;
1618+
1619+
ret = kthread_queue_work(port->wq, &event->work);
1620+
if (!ret) {
1621+
ret = -EBUSY;
1622+
goto err_queue;
1623+
}
1624+
1625+
return 0;
1626+
1627+
err_queue:
1628+
kfree(data_cpy);
1629+
err_data:
1630+
kfree(event);
1631+
err_event:
1632+
tcpm_log_force(port, "failed to queue altmode vdm, err:%d", ret);
1633+
return ret;
1634+
}
1635+
15771636
static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt)
15781637
{
15791638
u32 vdo = p[VDO_INDEX_IDH];
@@ -2784,8 +2843,7 @@ static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo)
27842843
header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
27852844
header |= VDO_OPOS(altmode->mode);
27862845

2787-
tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP);
2788-
return 0;
2846+
return tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP);
27892847
}
27902848

27912849
static int tcpm_altmode_exit(struct typec_altmode *altmode)
@@ -2801,18 +2859,15 @@ static int tcpm_altmode_exit(struct typec_altmode *altmode)
28012859
header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
28022860
header |= VDO_OPOS(altmode->mode);
28032861

2804-
tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP);
2805-
return 0;
2862+
return tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP);
28062863
}
28072864

28082865
static int tcpm_altmode_vdm(struct typec_altmode *altmode,
28092866
u32 header, const u32 *data, int count)
28102867
{
28112868
struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
28122869

2813-
tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP);
2814-
2815-
return 0;
2870+
return tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP);
28162871
}
28172872

28182873
static const struct typec_altmode_ops tcpm_altmode_ops = {
@@ -2836,8 +2891,7 @@ static int tcpm_cable_altmode_enter(struct typec_altmode *altmode, enum typec_pl
28362891
header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
28372892
header |= VDO_OPOS(altmode->mode);
28382893

2839-
tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP_PRIME);
2840-
return 0;
2894+
return tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP_PRIME);
28412895
}
28422896

28432897
static int tcpm_cable_altmode_exit(struct typec_altmode *altmode, enum typec_plug_index sop)
@@ -2853,18 +2907,15 @@ static int tcpm_cable_altmode_exit(struct typec_altmode *altmode, enum typec_plu
28532907
header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
28542908
header |= VDO_OPOS(altmode->mode);
28552909

2856-
tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP_PRIME);
2857-
return 0;
2910+
return tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP_PRIME);
28582911
}
28592912

28602913
static int tcpm_cable_altmode_vdm(struct typec_altmode *altmode, enum typec_plug_index sop,
28612914
u32 header, const u32 *data, int count)
28622915
{
28632916
struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
28642917

2865-
tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP_PRIME);
2866-
2867-
return 0;
2918+
return tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP_PRIME);
28682919
}
28692920

28702921
static const struct typec_cable_ops tcpm_cable_ops = {

0 commit comments

Comments
 (0)