Skip to contentSkip to conversation

ata: libahci: Stop the SW activity blink timers on port suspend

PATCH Applied Niklas Cassel 2 participants 4 messages Nov 10 – Nov 13

Subsystems (2)
lore
Share & tools
lore

Niklas Cassel

author maintainer
#0 lore raw

PATCH 1/1 ata: libahci: Stop the SW activity blink timers on port suspend

The SW activity blink timers are not stopped when a port is suspended. Every command goes through ahci_qc_issue() -> ahci_sw_activity(), so with SW activity enabled, a blink timer can still be pending when ahci_port_suspend() runs, e.g. armed by the commands issued just before it, and ahci_sw_activity_blink() re-arms it as long as there was activity.

If the timer fires while the port is suspended, ahci_sw_activity_blink() -> ahci_transmit_led_message() -> ahci_rpm_get_port() calls pm_runtime_get_sync() from timer softirq context, and the EM registers of a suspended controller get written. If the port is resumed while the timer is still pending, ahci_port_resume() -> ahci_start_port() -> ahci_init_sw_activity() calls timer_setup() on a timer that is still queued in the timer wheel, corrupting it.

Stop the timers in ahci_port_suspend(). Use timer_delete_sync() rather than timer_shutdown_sync(): the timers are only stopped until the port is resumed, while timer_shutdown_sync() is meant for timers that are torn down for good.

Fixes 18f7ba4c2f4b ("libata/ahci: enclosure management support")

---
Developed with the assistance of an LLM.

Tested in QEMU (q35, KVM) with KASAN, UBSAN, DEBUG_OBJECTS_TIMERS and lockdep, using TEST-ONLY hooks: one fakes enclosure management (QEMU's AHCI has none), the other arms the blink timer as ahci_qc_issue() does and then calls ->port_suspend() and ->port_resume() back to back, while the timer is still pending.

  unpatched                              patched
  ODEBUG: init active ... timer_list     no warnings,
  hint: ahci_sw_activity_blink,          I/O after resume works

from ahci_port_resume()

The timer firing while the port is suspended is not covered, as the fake EM support never reaches ahci_rpm_get_port().

 drivers/ata/libahci.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/ata/libahci.c +14 -1
index ef760d81d1d1..2e71ba7ea374 100644
@@ -2491,8 +2491,21 @@ static void ahci_handle_s2idle(struct ata_port *ap)
 
 static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
 {
+	struct ahci_port_priv *pp = ap->private_data;
 	const char *emsg = NULL;
-	int rc;
+	int rc, i;
+
+	/*
+	 * Stop the SW activity blink timers, so that they cannot fire while
+	 * the port is suspended. ahci_init_sw_activity() sets them up again
+	 * on resume.
+	 */
+	for (i = 0; i < EM_MAX_SLOTS; i++) {
+		struct ahci_em_priv *emp = &pp->em_priv[i];
+
+		if (emp->timer.function)
+			timer_delete_sync(&emp->timer);
+	}
 
 	rc = ahci_deinit_port(ap, &emsg);
 	if (rc == 0)
Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 56 lines · Niklas Cassel

The SW activity blink timers are not stopped when a port is suspended. Every command goes through ahci_qc_issue() -> ahci_sw_activity(), so with SW activity enabled, a blink timer can still be pending when ahci_port_suspend() runs, e.g. armed by the commands issued just before it, and ahci_sw_activity_blink() re-arms it as long as there was activity.

If the timer fires while the port is suspended, ahci_sw_activity_blink() -> ahci_transmit_led_message() -> ahci_rpm_get_port() calls pm_runtime_get_sync() from timer softirq context, and the EM registers of a suspended controller get written. If the port is resumed while the timer is still pending, ahci_port_resume() -> ahci_start_port() -> ahci_init_sw_activity() calls timer_setup() on a timer that is still queued in the timer wheel, corrupting it.

Stop the timers in ahci_port_suspend(). Use timer_delete_sync() rather than timer_shutdown_sync(): the timers are only stopped until the port is resumed, while timer_shutdown_sync() is meant for timers that are torn down for good.

Fixes 18f7ba4c2f4b ("libata/ahci: enclosure management support")

---
Developed with the assistance of an LLM.

Tested in QEMU (q35, KVM) with KASAN, UBSAN, DEBUG_OBJECTS_TIMERS and lockdep, using TEST-ONLY hooks: one fakes enclosure management (QEMU's AHCI has none), the other arms the blink timer as ahci_qc_issue() does and then calls ->port_suspend() and ->port_resume() back to back, while the timer is still pending.

  unpatched                              patched
  ODEBUG: init active ... timer_list     no warnings,
  hint: ahci_sw_activity_blink,          I/O after resume works

from ahci_port_resume()

The timer firing while the port is suspended is not covered, as the fake EM support never reaches ahci_rpm_get_port().

 drivers/ata/libahci.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/ata/libahci.c +3 -1
index ef760d81d1d1..2e71ba7ea374 100644
@@ -2491,8 +2491,21 @@ static void ahci_handle_s2idle(struct ata_port *ap)
 
 static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
 {
+	struct ahci_port_priv *pp = ap->private_data;
 	const char *emsg = NULL;
-	int rc;
+	int rc, i;
+

drivers/ata/libahci.c static void ahci_handle_s2idle(struct ata_port *ap) · line 2507

Quoted 10 lines
drivers/ata/libahci.c +10 -0
+	/*
+	 * Stop the SW activity blink timers, so that they cannot fire while
+	 * the port is suspended. ahci_init_sw_activity() sets them up again
+	 * on resume.
+	 */
+	for (i = 0; i < EM_MAX_SLOTS; i++) {
+		struct ahci_em_priv *emp = &pp->em_priv[i];
+
+		if (emp->timer.function)
+			timer_delete_sync(&emp->timer);

Looks OK. Did you check that the timer handler does not restart/requeue the timer? timer_delete_sync() states that it should not.

Quoted 4 lines
drivers/ata/libahci.c +1 -0
+	}
 
 	rc = ahci_deinit_port(ap, &emsg);
 	if (rc == 0)
Signature · 2 lines
Damien Le Moal
Western Digital Research
author maintainer
#2 lore raw
Quoted 14 lines · Damien Le Moal

drivers/ata/libahci.c static void ahci_handle_s2idle(struct ata_port *ap) · line 2507

Quoted 10 lines · Niklas Cassel
drivers/ata/libahci.c +10 -0
+	/*
+	 * Stop the SW activity blink timers, so that they cannot fire while
+	 * the port is suspended. ahci_init_sw_activity() sets them up again
+	 * on resume.
+	 */
+	for (i = 0; i < EM_MAX_SLOTS; i++) {
+		struct ahci_em_priv *emp = &pp->em_priv[i];
+
+		if (emp->timer.function)
+			timer_delete_sync(&emp->timer);

Looks OK. Did you check that the timer handler does not restart/requeue the timer? timer_delete_sync() states that it should not.

The handler does re-arm itself, but timer_delete_sync() handles that. It waits for a running handler, then dequeues the re-armed timer.

The rule is about re-arming from other code, and the only other path, ahci_qc_issue(), is not called once EH has frozen the port and called ->port_suspend().

So we should be good.

Kind regards,
Niklas

author maintainer Applied
#3 lore raw
In reply to Niklas Cassel
Quoted 8 lines · Niklas Cassel

The SW activity blink timers are not stopped when a port is suspended. Every command goes through ahci_qc_issue() -> ahci_sw_activity(), so with SW activity enabled, a blink timer can still be pending when ahci_port_suspend() runs, e.g. armed by the commands issued just before it, and ahci_sw_activity_blink() re-arms it as long as there was activity.

[...]

Applied to libata/linux.git (for-7.5), thanks!

[1/1] ata: libahci: Stop the SW activity blink timers on port suspend

      https://git.kernel.org/libata/linux/c/3cf33dcb

Kind regards,
Niklas