Skip to contentSkip to conversation

usb: typec: tcpm/tcpci_maxim: lockdep and PM wakeup fixes

PATCH New André Draszik 2 participants 4 messages Nov 23

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover usb: typec: tcpm/tcpci_maxim: lockdep and PM wakeup fixes

Hi,

These two patches fix a lockdep invalid wait context splat and a probe/unbind wakeup-event race in the Maxim 77759 TCPCI driver:

- Call pm_wakeup_event() from the threaded IRQ handler instead of the hard IRQ handler (the driver uses IRQF_ONESHOT, so the primary handler runs in hard IRQ context even on PREEMPT_RT).
- Initialise device wakeup before requesting the IRQ, so a pending alert or an interrupt during unbind cannot miss recording a wakeup event.

Tested on Pixel 6 (Oriole) with CONFIG_PROVE_RAW_LOCK_NESTING=y enabled.

Cheers
Andre'

---
André Draszik (2):
usb: typec: tcpm/tcpci_maxim: fix invalid wait context
usb: typec: tcpm/tcpci_maxim: init wakeup before irq

 drivers/usb/typec/tcpm/tcpci_maxim_core.c | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)

---
base-commit: 749a4de370409464cf8501ec5b2451df45c84eb4
change-id: 20261123-max77759-wait-10974f4c312d

Best regards,

Signature · 1 line
André Draszik <[email protected]>
#1 lore raw

PATCH 1/2 usb: typec: tcpm/tcpci_maxim: fix invalid wait context

Lockdep reports the following with CONFIG_PROVE_RAW_LOCK_NESTING=y:

    [ BUG: Invalid wait context ]
    swapper/0/0 is trying to lock:
    (&dev->power.lock){-...}-{3:3}, at: pm_wakeup_dev_event+0x38/0x80
    other info that might help us debug this:
    context-{2:2}
    locks held by swapper/0/0: 0, on CPU#0
    Call trace:
     _raw_spin_lock_irqsave+0x60/0x98
     pm_wakeup_dev_event+0x38/0x80
     max_tcpci_isr+0x2c/0x58 [tcpci_maxim]
     __handle_irq_event_percpu+0xc4/0x4d8
     handle_irq_event+0x4c/0x100
     handle_level_irq+0xe8/0x188

max_tcpci_isr() calls pm_wakeup_event(), which acquires two spinlocks (spinlock_t): dev->power.lock and the lock of the device's wakeup source. On PREEMPT_RT, spinlock_t turns into a sleeping lock. Since the interrupt is requested with IRQF_ONESHOT, max_tcpci_isr() is never force-threaded and runs in hard irq context even on PREEMPT_RT. Taking a sleeping lock in hard irq context is invalid, triggering the lockdep report above.

Move pm_wakeup_event() to the interrupt thread max_tcpci_irq() instead. Drop max_tcpci_isr() altogether, since its chip->port check is already duplicated in max_tcpci_irq(), and request the interrupt without a primary handler.

Note: Moving pm_wakeup_event() to the interrupt thread doesn't allow the system to suspend before the event is reported:
suspend_device_irqs() uses synchronize_irq() to wait for the interrupt thread to finish, and pm_wakeup_pending() is checked after suspend_device_irqs() returns. Once suspend_device_irqs() has armed the wake interrupt, the interrupt core reports the wakeup itself and doesn't call the driver's handlers.

Fixes 6f413b559f86 ("usb: typec: tcpci_maxim: Chip level TCPC driver")
---
 drivers/usb/typec/tcpm/tcpci_maxim_core.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/usb/typec/tcpm/tcpci_maxim_core.c +3 -13
index a19f52515429..6e202171f57a 100644
@@ -411,6 +411,8 @@ static irqreturn_t max_tcpci_irq(int irq, void *dev_id)
 	irqreturn_t irq_return = IRQ_HANDLED;
 	int ret;
 
+	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);
+
 	if (!chip->port)
 		return IRQ_HANDLED;
 
@@ -430,18 +432,6 @@ static irqreturn_t max_tcpci_irq(int irq, void *dev_id)
 	return irq_return;
 }
 
-static irqreturn_t max_tcpci_isr(int irq, void *dev_id)
-{
-	struct max_tcpci_chip *chip = dev_id;
-
-	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);
-
-	if (!chip->port)
-		return IRQ_HANDLED;
-
-	return IRQ_WAKE_THREAD;
-}
-
 static int max_tcpci_start_toggling(struct tcpci *tcpci, struct tcpci_data *tdata,
 				    enum typec_cc_status cc)
 {
@@ -538,7 +528,7 @@ static int max_tcpci_probe(struct i2c_client *client)
 
 	chip->port = tcpci_get_tcpm_port(chip->tcpci);
 
-	ret = devm_request_threaded_irq(&client->dev, client->irq, max_tcpci_isr, max_tcpci_irq,
+	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, max_tcpci_irq,
 					(IRQF_TRIGGER_LOW | IRQF_ONESHOT), dev_name(chip->dev),
 					chip);
 	if (ret < 0)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#2 lore raw

PATCH 2/2 usb: typec: tcpm/tcpci_maxim: init wakeup before irq

In reply to André Draszik

max_tcpci_probe() requests the TCPC interrupt before it initialises the wakeup source of the device. Since the interrupt is level triggered, an already asserted alert can trigger the interrupt handler before devm_device_init_wakeup() is called. Its pm_wakeup_event() does nothing while the device has no wakeup source, so the wakeup event of such an early interrupt is not recorded, meaning the system is then not prevented from suspending for PD_ACTIVITY_TIMEOUT_MS while handling that alert.

When the driver unbinds, devres works in reverse order and in this case removes the wakeup source before it frees the interrupt, which leaves a similar window.

Call devm_device_init_wakeup() before devm_request_threaded_irq(), so that the wakeup source exists for as long as the interrupt handler can run.

Fixes 6f413b559f86 ("usb: typec: tcpci_maxim: Chip level TCPC driver")
---
 drivers/usb/typec/tcpm/tcpci_maxim_core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/tcpci_maxim_core.c +4 -4
index 6e202171f57a..2bb306b6b1f9 100644
@@ -528,16 +528,16 @@ static int max_tcpci_probe(struct i2c_client *client)
 
 	chip->port = tcpci_get_tcpm_port(chip->tcpci);
 
+	ret = devm_device_init_wakeup(chip->dev);
+	if (ret)
+		return dev_err_probe(chip->dev, ret, "Failed to init wakeup\n");
+
 	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, max_tcpci_irq,
 					(IRQF_TRIGGER_LOW | IRQF_ONESHOT), dev_name(chip->dev),
 					chip);
 	if (ret < 0)
 		return ret;
 
-	ret = devm_device_init_wakeup(chip->dev);
-	if (ret)
-		return dev_err_probe(chip->dev, ret, "Failed to init wakeup\n");
-
 	return 0;
 }
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog