Skip to content

Commit a37241d

Browse files
jwrdegoedegregkh
authored andcommitted
usb: typec: tcpm: Fix AB BA lock inversion between tcpm code and the alt-mode drivers
When we receive a PD data packet which ends up being for the alt-mode driver we have the following lock order: 1. tcpm_pd_rx_handler take the tcpm-port lock 2. We call into the alt-mode driver which takes the alt-mode's lock And when the alt-mode driver initiates communication we have the following lock order: 3. alt-mode driver takes the alt-mode's lock 4. alt-mode driver calls tcpm_altmode_enter which takes the tcpm-port lock This is a classic AB BA lock inversion issue. With the refactoring of tcpm_handle_vdm_request() done before this patch, we don't rely on, or need to make changes to the tcpm-port data by the time we make call 2. from above. All data to be passed to the alt-mode driver sits on our stack at this point, and thus does not need locking. So after the refactoring we can simply fix this by releasing the tcpm-port lock before calling into the alt-mode driver. This fixes the following lockdep warning: [ 191.454238] ====================================================== [ 191.454240] WARNING: possible circular locking dependency detected [ 191.454244] 5.8.0-rc5+ #1 Not tainted [ 191.454246] ------------------------------------------------------ [ 191.454248] kworker/u8:5/794 is trying to acquire lock: [ 191.454251] ffff9bac8e30d4a8 (&dp->lock){+.+.}-{3:3}, at: dp_altmode_vdm+0x30/0xf0 [typec_displayport] [ 191.454263] but task is already holding lock: [ 191.454264] ffff9bac9dc240a0 (&port->lock#2){+.+.}-{3:3}, at: tcpm_pd_rx_handler+0x43/0x12c0 [tcpm] [ 191.454273] which lock already depends on the new lock. [ 191.454275] the existing dependency chain (in reverse order) is: [ 191.454277] -> #1 (&port->lock#2){+.+.}-{3:3}: [ 191.454286] __mutex_lock+0x7b/0x820 [ 191.454290] tcpm_altmode_enter+0x23/0x90 [tcpm] [ 191.454293] dp_altmode_work+0xca/0xe0 [typec_displayport] [ 191.454299] process_one_work+0x23f/0x570 [ 191.454302] worker_thread+0x55/0x3c0 [ 191.454305] kthread+0x138/0x160 [ 191.454309] ret_from_fork+0x22/0x30 [ 191.454311] -> #0 (&dp->lock){+.+.}-{3:3}: [ 191.454317] __lock_acquire+0x1241/0x2090 [ 191.454320] lock_acquire+0xa4/0x3d0 [ 191.454323] __mutex_lock+0x7b/0x820 [ 191.454326] dp_altmode_vdm+0x30/0xf0 [typec_displayport] [ 191.454330] tcpm_pd_rx_handler+0x11ae/0x12c0 [tcpm] [ 191.454333] process_one_work+0x23f/0x570 [ 191.454336] worker_thread+0x55/0x3c0 [ 191.454338] kthread+0x138/0x160 [ 191.454341] ret_from_fork+0x22/0x30 [ 191.454343] other info that might help us debug this: [ 191.454345] Possible unsafe locking scenario: [ 191.454347] CPU0 CPU1 [ 191.454348] ---- ---- [ 191.454350] lock(&port->lock#2); [ 191.454353] lock(&dp->lock); [ 191.454355] lock(&port->lock#2); [ 191.454357] lock(&dp->lock); [ 191.454360] *** DEADLOCK *** Reviewed-by: Heikki Krogerus <[email protected]> Reviewed-by: Guenter Roeck <[email protected]> Signed-off-by: Hans de Goede <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 95b4d51 commit a37241d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

drivers/usb/typec/tcpm/tcpm.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,27 @@ static void tcpm_handle_vdm_request(struct tcpm_port *port,
12511251
if (PD_VDO_SVDM(p[0]))
12521252
rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action);
12531253

1254+
/*
1255+
* We are done with any state stored in the port struct now, except
1256+
* for any port struct changes done by the tcpm_queue_vdm() call
1257+
* below, which is a separate operation.
1258+
*
1259+
* So we can safely release the lock here; and we MUST release the
1260+
* lock here to avoid an AB BA lock inversion:
1261+
*
1262+
* If we keep the lock here then the lock ordering in this path is:
1263+
* 1. tcpm_pd_rx_handler take the tcpm port lock
1264+
* 2. One of the typec_altmode_* calls below takes the alt-mode's lock
1265+
*
1266+
* And we also have this ordering:
1267+
* 1. alt-mode driver takes the alt-mode's lock
1268+
* 2. alt-mode driver calls tcpm_altmode_enter which takes the
1269+
* tcpm port lock
1270+
*
1271+
* Dropping our lock here avoids this.
1272+
*/
1273+
mutex_unlock(&port->lock);
1274+
12541275
if (adev) {
12551276
switch (adev_action) {
12561277
case ADEV_NONE:
@@ -1275,6 +1296,15 @@ static void tcpm_handle_vdm_request(struct tcpm_port *port,
12751296
}
12761297
}
12771298

1299+
/*
1300+
* We must re-take the lock here to balance the unlock in
1301+
* tcpm_pd_rx_handler, note that no changes, other then the
1302+
* tcpm_queue_vdm call, are made while the lock is held again.
1303+
* All that is done after the call is unwinding the call stack until
1304+
* we return to tcpm_pd_rx_handler and do the unlock there.
1305+
*/
1306+
mutex_lock(&port->lock);
1307+
12781308
if (rlen > 0)
12791309
tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
12801310
}

0 commit comments

Comments
 (0)