Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
@ 2026-12-01  8:26 Vijendar Mukunda
  2026-12-01  8:56 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Vijendar Mukunda @ 2026-12-01  8:26 UTC (permalink / raw)
  To: vkoul
  Cc: broonie, yung-chuan.liao, pierre-louis.bossart,
	Sunil-kumar.Dommati, venkataprasad.potturu, Syed.SabaKareem,
	Mario.Limonciello, Richard.Gong, linux-sound, linux-kernel,
	Vijendar Mukunda

sdw_handle_slave_alerts() calls pm_runtime_get_sync(&slave->dev) which,
when slave->dev is not in RPM_ACTIVE, calls rpm_resume() on the slave.
rpm_resume() propagates synchronously up the parent chain: slave->dev ->
bus->dev -> manager->dev. If manager->dev is in RPM_SUSPENDING at that
point, rpm_resume() blocks on dev->power.wait_queue waiting for the
suspend to complete.

This creates a deadlock: the manager's runtime_suspend callback is blocked
in cancel_work_sync() waiting for the alert work to finish, while the alert
work is blocked in pm_runtime_get_sync() waiting for the manager's
runtime_suspend to complete.

Fix this by first checking the manager device state using
pm_runtime_get_if_active() on slave->bus->dev->parent (the physical
manager device). This is an atomic check: if it returns 1 the usage
counter is incremented in the same operation and the manager cannot
transition to RPM_SUSPENDING while the reference is held.

- If the manager reference cannot be taken (ret == 0, manager not
  RPM_ACTIVE): request an async resume via pm_request_resume(mgr_dev)
  and return early. This avoids I/O on potentially inaccessible hardware
  and breaks the cancel_work_sync() deadlock. After the manager resumes,
  the peripheral re-asserts the alert via the first PING frame.

- If the manager reference is taken (ret > 0, manager is RPM_ACTIVE):
  proceed with pm_runtime_resume_and_get(&slave->dev) to resume the
  slave. Holding the manager reference prevents the manager from
  entering RPM_SUSPENDING while the slave resumes, closing the deadlock
  window for all slave PM states.

- If ret < 0 (-EINVAL, PM disabled on manager): fall through without a
  manager reference. PM being disabled means no suspend/resume callbacks
  run, so no deadlock risk. This is consistent with the original -EACCES
  exemption for the slave.

pm_runtime_resume_and_get() is used for the slave (instead of
pm_runtime_get_sync()) because it does not increment the usage counter
on error, simplifying the error-path accounting. Introduce have_pm_ref
and have_mgr_ref booleans to guard the pm_runtime_put calls at io_err:
so they are only reached when a reference was actually taken. The
manager reference is released at the new mgr_put: label after io_err:.

Fixes: aa79293517b3 ("soundwire: bus: fix io error when processing alert event")
Signed-off-by: Vijendar Mukunda <Vijendar.Mukunda@amd.com>
---
 drivers/soundwire/bus.c | 60 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 55 insertions(+), 5 deletions(-)

diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
index 15c66effbf4f..5dc3598eb85f 100644
--- a/drivers/soundwire/bus.c
+++ b/drivers/soundwire/bus.c
@@ -1667,15 +1667,60 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
 	u8 buf, buf2[2];
 	bool parity_check;
 	bool parity_quirk;
+	bool have_pm_ref = false;
+	bool have_mgr_ref = false;
+	struct device *mgr_dev = slave->bus->dev->parent;
 
 	sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
 
-	ret = pm_runtime_get_sync(&slave->dev);
+	ret = pm_runtime_get_if_active(mgr_dev);
+	if (ret == 0) {
+		/*
+		 * Manager is not RPM_ACTIVE (RPM_SUSPENDED, RPM_SUSPENDING, or
+		 * RPM_RESUMING). Return early to avoid I/O on potentially
+		 * inaccessible hardware and to prevent a deadlock: if the manager
+		 * is in RPM_SUSPENDING, its runtime_suspend callback may be blocked
+		 * in cancel_work_sync() waiting for this work, while this work
+		 * would block in pm_runtime_resume_and_get() waiting for the
+		 * manager to complete its suspend.
+		 *
+		 * Request an async resume so the deferred alert is re-serviced:
+		 * - RPM_SUSPENDING: pm_request_resume() sets deferred_resume so
+		 *   the manager resumes immediately after its suspend completes;
+		 *   on resume the peripheral re-asserts the alert via the first
+		 *   PING frame and the interrupt fires again.
+		 * - RPM_RESUMING: pm_request_resume() is a no-op (device already
+		 *   resuming); after the manager reaches RPM_ACTIVE, the peripheral
+		 *   re-asserts the alert via the first PING frame.
+		 * - RPM_SUSPENDED: pm_request_resume() triggers an async resume;
+		 *   after resume the peripheral re-asserts via PING.
+		 *
+		 * When the SoundWire link fully powers off (e.g. D3 or equivalent),
+		 * the peripheral loses power and its alert state does not survive;
+		 * pm_request_resume() is unnecessary but harmless.
+		 */
+		pm_request_resume(mgr_dev);
+		dev_dbg(&slave->dev, "Alert deferred: manager not active\n");
+		return 0;
+	}
+	if (ret > 0)
+		have_mgr_ref = true;
+	/*
+	 * ret < 0 (-EINVAL): PM is disabled (CONFIG_PM=n or disable_depth > 0).
+	 * Proceed without a manager reference.
+	 */
+
+	ret = pm_runtime_resume_and_get(&slave->dev);
 	if (ret < 0 && ret != -EACCES) {
 		dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
-		pm_runtime_put_noidle(&slave->dev);
-		return ret;
+		goto mgr_put;
 	}
+	if (ret >= 0)
+		have_pm_ref = true;
+	/*
+	 * ret == -EACCES: PM is disabled on slave; proceed without a
+	 * slave reference, consistent with the original -EACCES exemption.
+	 */
 
 	/* Read Intstat 1, Intstat 2 and Intstat 3 registers */
 	ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
@@ -1861,8 +1906,13 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
 		dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
 
 io_err:
-	pm_runtime_mark_last_busy(&slave->dev);
-	pm_runtime_put_autosuspend(&slave->dev);
+	if (have_pm_ref) {
+		pm_runtime_mark_last_busy(&slave->dev);
+		pm_runtime_put_autosuspend(&slave->dev);
+	}
+mgr_put:
+	if (have_mgr_ref)
+		pm_runtime_put(mgr_dev);
 
 	return ret;
 }
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
  2026-12-01  8:26 [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts() Vijendar Mukunda
@ 2026-12-01  8:56 ` sashiko-bot
  2026-12-01 10:34   ` Mukunda,Vijendar
  2026-12-01 10:34 ` Mukunda,Vijendar
  2026-12-01 17:14 ` Pierre-Louis Bossart
  2 siblings, 1 reply; 5+ messages in thread
From: sashiko-bot @ 2026-12-01  8:56 UTC (permalink / raw)
  To: Vijendar Mukunda; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] soundwire: bus: sdw_handle_slave_alerts() drops alerts during
  RPM_RESUMING
- [High] soundwire: bus: early return in sdw_handle_slave_alerts() hangs
  qcom IRQ
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] soundwire: race between sdw_irq_dispose_mapping() and
  sdw_handle_slave_alerts() [1]
--

commit 5b44fb227649378eb41f6e363683a96229baf7ac
Author: Vijendar Mukunda <Vijendar.Mukunda@amd.com>

soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()

This commit checks the runtime PM state of the manager device with
pm_runtime_get_if_active() before resuming the slave in
sdw_handle_slave_alerts(), deferring alert handling with
pm_request_resume() when the manager is not active to avoid a deadlock
with the manager's runtime_suspend callback.

> diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
> index 15c66effbf4f4..5dc3598eb85f6 100644
> --- a/drivers/soundwire/bus.c
> +++ b/drivers/soundwire/bus.c
> @@ -1667,15 +1667,60 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
>  	u8 buf, buf2[2];
>  	bool parity_check;
>  	bool parity_quirk;
> +	bool have_pm_ref = false;
> +	bool have_mgr_ref = false;
> +	struct device *mgr_dev = slave->bus->dev->parent;
>  
>  	sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
>  
> -	ret = pm_runtime_get_sync(&slave->dev);
> +	ret = pm_runtime_get_if_active(mgr_dev);
> +	if (ret == 0) {
> +		/*
> +		 * Manager is not RPM_ACTIVE (RPM_SUSPENDED, RPM_SUSPENDING, or
> +		 * RPM_RESUMING). Return early to avoid I/O on potentially
> +		 * inaccessible hardware and to prevent a deadlock: if the manager
> +		 * is in RPM_SUSPENDING, its runtime_suspend callback may be blocked
> +		 * in cancel_work_sync() waiting for this work, while this work
> +		 * would block in pm_runtime_resume_and_get() waiting for the
> +		 * manager to complete its suspend.
> +		 *
> +		 * Request an async resume so the deferred alert is re-serviced:
> +		 * - RPM_SUSPENDING: pm_request_resume() sets deferred_resume so
> +		 *   the manager resumes immediately after its suspend completes;
> +		 *   on resume the peripheral re-asserts the alert via the first
> +		 *   PING frame and the interrupt fires again.
> +		 * - RPM_RESUMING: pm_request_resume() is a no-op (device already
> +		 *   resuming); after the manager reaches RPM_ACTIVE, the peripheral
> +		 *   re-asserts the alert via the first PING frame.
> +		 * - RPM_SUSPENDED: pm_request_resume() triggers an async resume;
> +		 *   after resume the peripheral re-asserts via PING.
> +		 *
> +		 * When the SoundWire link fully powers off (e.g. D3 or equivalent),
> +		 * the peripheral loses power and its alert state does not survive;
> +		 * pm_request_resume() is unnecessary but harmless.
> +		 */
> +		pm_request_resume(mgr_dev);
> +		dev_dbg(&slave->dev, "Alert deferred: manager not active\n");
> +		return 0;
> +	}

[Severity: High]
Can returning early when mgr_dev is in RPM_RESUMING drop the peripheral
alert and leave future alerts wedged?

mgr_dev remains in RPM_RESUMING until the manager's runtime_resume callback
returns, but manager drivers restart the SoundWire bus clock and enable
interrupts inside runtime_resume before returning.  For example,
amd_resume_runtime() calls amd_sdw_clock_stop_exit(), which restarts the
clock and PING frames before calling sdw_bus_exit_clk_stop(), and
swrm_runtime_resume() even calls sdw_handle_slave_status() directly:

drivers/soundwire/qcom.c:swrm_runtime_resume() {
	...
		qcom_swrm_get_device_status(ctrl);
		sdw_handle_slave_status(&ctrl->bus, ctrl->status);
	...
}

When the first PING frame after clock restart arrives while mgr_dev is
still in RPM_RESUMING, the manager IRQ/work handler clears its state-change
interrupt status register and calls sdw_handle_slave_alerts().  With
pm_runtime_get_if_active(mgr_dev) returning 0 and pm_request_resume()
doing nothing in RPM_RESUMING, sdw_handle_slave_alerts() returns without
clearing SDW_SCP_INT1 on the peripheral.  Because the peripheral stays in
SDW_SLAVE_ALERT on subsequent PING frames with no new ATTACHED-to-ALERT
transition, no further state-change interrupt fires once mgr_dev reaches
RPM_ACTIVE.

[Severity: High]
Can returning 0 here without clearing the peripheral's alert registers
cause qcom_swrm_irq_handler() to spin forever and deadlock
swrm_runtime_suspend()?

Unlike Cadence and AMD managers, qcom_swrm_irq_handler() runs as a threaded
IRQ handler that loops until SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ clears:

drivers/soundwire/qcom.c:qcom_swrm_irq_handler() {
	...
			case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
				devnum = qcom_swrm_get_alert_slave_dev_num(ctrl);
				if (devnum < 0) {
					dev_err_ratelimited(ctrl->dev,
					    "no slave alert found.spurious interrupt\n");
				} else {
					sdw_handle_slave_status(&ctrl->bus, ctrl->status);
				}

				break;
	...
		ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CLEAR],
				intr_sts);
		ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_STATUS],
			       &intr_sts);
		intr_sts_masked = intr_sts & ctrl->intr_mask;
	} while (intr_sts_masked);

CPU0                                  CPU1
swrm_runtime_suspend()
  /* mgr_dev is RPM_SUSPENDING */
                                      qcom_swrm_irq_handler()
                                        do {
                                          sdw_handle_slave_status()
                                            sdw_handle_slave_alerts()
                                              pm_runtime_get_if_active() == 0
                                              return 0;
                                          /* SLAVE_PEND_IRQ still set */
                                        } while (intr_sts_masked);
                                          <-- spins forever
  disable_irq(ctrl->irq)
    <-- waits for IRQ thread

Because sdw_handle_slave_alerts() returns without clearing SDW_SCP_INT1 on
the peripheral, SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ remains asserted in
SWRM_REG_INTERRUPT_STATUS, so qcom_swrm_irq_handler() never exits its loop
and disable_irq() in swrm_runtime_suspend() hangs waiting for it.

[Severity: High]
This problem wasn't introduced by this patch, but nothing serializes
[1] sdw_irq_dispose_mapping() against sdw_handle_slave_alerts()
calling handle_nested_irq(slave->irq):

CPU0                                  CPU1
device_release_driver_internal()
  sdw_bus_remove()
    slave->probed = false
  device_unbind_cleanup()
    ...
      sdw_irq_dispose_mapping()
        irq_dispose_mapping(slave->irq)
          ...
            delete_irq_desc()
                                      sdw_handle_slave_alerts()
                                        handle_nested_irq(slave->irq)
                                          desc = irq_to_desc(irq)
                                          raw_spin_lock_irq(&desc->lock)
                                            <-- NULL pointer dereference

1ccb7bb13c00 ("soundwire: Move handle_nested_irq outside of sdw_dev_lock")
moved handle_nested_irq(slave->irq) ahead of slave->sdw_dev_lock and the
slave->probed check, while sdw_irq_dispose_mapping() disposes of the IRQ
descriptor without clearing slave->irq or synchronizing with alert work.

[1] https://sashiko.dev/#/bug/linux-90121507-1cdf-435b-b5f4-ce9da544a790

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201082651.3700713-1-Vijendar.Mukunda@amd.com?part=1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
  2026-12-01  8:56 ` sashiko-bot
@ 2026-12-01 10:34   ` Mukunda,Vijendar
  0 siblings, 0 replies; 5+ messages in thread
From: Mukunda,Vijendar @ 2026-12-01 10:34 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: linux-sound, Vinod Koul, Mario Limonciello, Dommati, Sunil-kumar,
	Bard Liao, Pierre-Louis Bossart, srini, Prasad, Prasad,
	Syed Saba Kareem



On 12/1/26 14:26, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [High] soundwire: bus: sdw_handle_slave_alerts() drops alerts during
>    RPM_RESUMING
> - [High] soundwire: bus: early return in sdw_handle_slave_alerts() hangs
>    qcom IRQ
> It also noticed 1 related pre-existing issue this patch does not
> introduce:
> - [High] soundwire: race between sdw_irq_dispose_mapping() and
>    sdw_handle_slave_alerts() [1]
> --
>
> commit 5b44fb227649378eb41f6e363683a96229baf7ac
> Author: Vijendar Mukunda <Vijendar.Mukunda@amd.com>
>
> soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
>
> This commit checks the runtime PM state of the manager device with
> pm_runtime_get_if_active() before resuming the slave in
> sdw_handle_slave_alerts(), deferring alert handling with
> pm_request_resume() when the manager is not active to avoid a deadlock
> with the manager's runtime_suspend callback.
>
>> diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
>> index 15c66effbf4f4..5dc3598eb85f6 100644
>> --- a/drivers/soundwire/bus.c
>> +++ b/drivers/soundwire/bus.c
>> @@ -1667,15 +1667,60 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
>>   	u8 buf, buf2[2];
>>   	bool parity_check;
>>   	bool parity_quirk;
>> +	bool have_pm_ref = false;
>> +	bool have_mgr_ref = false;
>> +	struct device *mgr_dev = slave->bus->dev->parent;
>>   
>>   	sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
>>   
>> -	ret = pm_runtime_get_sync(&slave->dev);
>> +	ret = pm_runtime_get_if_active(mgr_dev);
>> +	if (ret == 0) {
>> +		/*
>> +		 * Manager is not RPM_ACTIVE (RPM_SUSPENDED, RPM_SUSPENDING, or
>> +		 * RPM_RESUMING). Return early to avoid I/O on potentially
>> +		 * inaccessible hardware and to prevent a deadlock: if the manager
>> +		 * is in RPM_SUSPENDING, its runtime_suspend callback may be blocked
>> +		 * in cancel_work_sync() waiting for this work, while this work
>> +		 * would block in pm_runtime_resume_and_get() waiting for the
>> +		 * manager to complete its suspend.
>> +		 *
>> +		 * Request an async resume so the deferred alert is re-serviced:
>> +		 * - RPM_SUSPENDING: pm_request_resume() sets deferred_resume so
>> +		 *   the manager resumes immediately after its suspend completes;
>> +		 *   on resume the peripheral re-asserts the alert via the first
>> +		 *   PING frame and the interrupt fires again.
>> +		 * - RPM_RESUMING: pm_request_resume() is a no-op (device already
>> +		 *   resuming); after the manager reaches RPM_ACTIVE, the peripheral
>> +		 *   re-asserts the alert via the first PING frame.
>> +		 * - RPM_SUSPENDED: pm_request_resume() triggers an async resume;
>> +		 *   after resume the peripheral re-asserts via PING.
>> +		 *
>> +		 * When the SoundWire link fully powers off (e.g. D3 or equivalent),
>> +		 * the peripheral loses power and its alert state does not survive;
>> +		 * pm_request_resume() is unnecessary but harmless.
>> +		 */
>> +		pm_request_resume(mgr_dev);
>> +		dev_dbg(&slave->dev, "Alert deferred: manager not active\n");
>> +		return 0;
>> +	}
> [Severity: High]
> Can returning early when mgr_dev is in RPM_RESUMING drop the peripheral
> alert and leave future alerts wedged?
This appears to be a false positive. SLAVE_PEND_IRQ is level-asserted:
it fires whenever a slave responds with ALERT in a PING frame, not only
on ATTACHED->ALERT state-machine transitions.

During suspend the SoundWire clock stops, PING frames cease, and
SLAVE_PEND_IRQ deasserts.  When the clock restarts during resume the
peripheral—which still has SDW_SCP_INT1 set—responds with ALERT on
subsequent PING frames.  While mgr_dev is still in RPM_RESUMING, any
IRQ handler invocation reaching sdw_handle_slave_alerts() returns early
(pm_runtime_get_if_active() == 0); this is expected and harmless. Once
the resume callback returns and mgr_dev reaches RPM_ACTIVE, the
peripheral keeps asserting ALERT in PING responses, SLAVE_PEND_IRQ
re-fires, pm_runtime_get_if_active() returns > 0, and
sdw_handle_slave_alerts() services the alert normally.  No alert is
lost.

Based on our code walk of swrm_runtime_resume(), there are two paths
to consider:

For the clock_stop_not_supported path, swrm_runtime_resume() calls
sdw_handle_slave_status() (and thereby sdw_handle_slave_alerts())
while still in RPM_RESUMING.  Our understanding is that the early
return there is harmless: the full bus reset and re-enumeration in
this path clears the peripheral's SDW_SCP_INT1 state, so there is
no stale alert to re-service after resume.

For the clock stop mode path, swrm_runtime_resume() does not call
sdw_handle_slave_status() internally.  After the resume callback
returns and mgr_dev reaches RPM_ACTIVE, the peripheral re-asserts
ALERT in PING responses and SLAVE_PEND_IRQ fires again, this time
servicing the alert normally.

Cc-ing Vinod, Bard, Pierre-Louis, and Srinivas from Qualcomm to
confirm whether our understanding of swrm_runtime_resume() and the
SLAVE_PEND_IRQ level-asserted behaviour is correct for both paths.
>
> mgr_dev remains in RPM_RESUMING until the manager's runtime_resume callback
> returns, but manager drivers restart the SoundWire bus clock and enable
> interrupts inside runtime_resume before returning.  For example,
> amd_resume_runtime() calls amd_sdw_clock_stop_exit(), which restarts the
> clock and PING frames before calling sdw_bus_exit_clk_stop(), and
> swrm_runtime_resume() even calls sdw_handle_slave_status() directly:
>
> drivers/soundwire/qcom.c:swrm_runtime_resume() {
> 	...
> 		qcom_swrm_get_device_status(ctrl);
> 		sdw_handle_slave_status(&ctrl->bus, ctrl->status);
> 	...
> }
>
> When the first PING frame after clock restart arrives while mgr_dev is
> still in RPM_RESUMING, the manager IRQ/work handler clears its state-change
> interrupt status register and calls sdw_handle_slave_alerts().  With
> pm_runtime_get_if_active(mgr_dev) returning 0 and pm_request_resume()
> doing nothing in RPM_RESUMING, sdw_handle_slave_alerts() returns without
> clearing SDW_SCP_INT1 on the peripheral.  Because the peripheral stays in
> SDW_SLAVE_ALERT on subsequent PING frames with no new ATTACHED-to-ALERT
> transition, no further state-change interrupt fires once mgr_dev reaches
> RPM_ACTIVE.
>
> [Severity: High]
> Can returning 0 here without clearing the peripheral's alert registers
> cause qcom_swrm_irq_handler() to spin forever and deadlock
> swrm_runtime_suspend()?
++

As per our understanding after walking through the QCOM driver code,
this scenario also appears to be a false positive.
The deadlock scenario in your table references
disable_irq(ctrl->irq) inside swrm_runtime_suspend(), but as per our
understanding swrm_runtime_suspend() does not call disable_irq().
Our understanding of the actual suspend sequence is:

   1. Write INTERRUPT_CPU_EN to mask interrupts via register
   2. sdw_bus_prep_clk_stop() -- SoundWire protocol clock-stop prepare
   3. sdw_bus_clk_stop()      -- stops PING frames on the bus
   4. clk_disable_unprepare(ctrl->hclk)

The do { } while (intr_sts_masked) loop in qcom_swrm_irq_handler() does
continue while SLAVE_PEND_IRQ is re-asserted after each INTERRUPT_CLEAR,
but as per our understanding sdw_bus_clk_stop() terminates PING frames,
after which SLAVE_PEND_IRQ deasserts, intr_sts_masked reaches 0, and
the loop exits naturally with no permanent deadlock.

It is also worth noting that this patch improves the QCOM situation
relative to the original pm_runtime_get_sync() code.  With the original
code the IRQ handler could block in rpm_resume() waiting for the
manager's suspend to complete—a true deadlock if the suspend path
synchronizes with the IRQ thread.  With this patch the handler returns
immediately, so at worst the loop iterates at PING frequency until
sdw_bus_clk_stop() completes.

@Srinivas, could you please confirm whether our understanding of
swrm_runtime_suspend() is correct and that sdw_bus_clk_stop() is
indeed what clears SLAVE_PEND_IRQ, allowing the loop to exit?

> Unlike Cadence and AMD managers, qcom_swrm_irq_handler() runs as a threaded
> IRQ handler that loops until SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ clears:
>
> drivers/soundwire/qcom.c:qcom_swrm_irq_handler() {
> 	...
> 			case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
> 				devnum = qcom_swrm_get_alert_slave_dev_num(ctrl);
> 				if (devnum < 0) {
> 					dev_err_ratelimited(ctrl->dev,
> 					    "no slave alert found.spurious interrupt\n");
> 				} else {
> 					sdw_handle_slave_status(&ctrl->bus, ctrl->status);
> 				}
>
> 				break;
> 	...
> 		ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CLEAR],
> 				intr_sts);
> 		ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_STATUS],
> 			       &intr_sts);
> 		intr_sts_masked = intr_sts & ctrl->intr_mask;
> 	} while (intr_sts_masked);
>
> CPU0                                  CPU1
> swrm_runtime_suspend()
>    /* mgr_dev is RPM_SUSPENDING */
>                                        qcom_swrm_irq_handler()
>                                          do {
>                                            sdw_handle_slave_status()
>                                              sdw_handle_slave_alerts()
>                                                pm_runtime_get_if_active() == 0
>                                                return 0;
>                                            /* SLAVE_PEND_IRQ still set */
>                                          } while (intr_sts_masked);
>                                            <-- spins forever
>    disable_irq(ctrl->irq)
>      <-- waits for IRQ thread
>
> Because sdw_handle_slave_alerts() returns without clearing SDW_SCP_INT1 on
> the peripheral, SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ remains asserted in
> SWRM_REG_INTERRUPT_STATUS, so qcom_swrm_irq_handler() never exits its loop
> and disable_irq() in swrm_runtime_suspend() hangs waiting for it.
>
> [Severity: High]
> This problem wasn't introduced by this patch, but nothing serializes
> [1] sdw_irq_dispose_mapping() against sdw_handle_slave_alerts()
> calling handle_nested_irq(slave->irq):
Agreed, this is a pre-existing issue and out of scope for this patch.
A proper fix requires clearing slave->irq and synchronizing with any
in-flight handle_nested_irq() call (e.g. via synchronize_irq()) before
irq_dispose_mapping() runs, or re-introducing lock protection around
both handle_nested_irq() and irq_dispose_mapping() so they cannot race.
Happy to follow up with a separate patch.
>
> CPU0                                  CPU1
> device_release_driver_internal()
>    sdw_bus_remove()
>      slave->probed = false
>    device_unbind_cleanup()
>      ...
>        sdw_irq_dispose_mapping()
>          irq_dispose_mapping(slave->irq)
>            ...
>              delete_irq_desc()
>                                        sdw_handle_slave_alerts()
>                                          handle_nested_irq(slave->irq)
>                                            desc = irq_to_desc(irq)
>                                            raw_spin_lock_irq(&desc->lock)
>                                              <-- NULL pointer dereference
>
> 1ccb7bb13c00 ("soundwire: Move handle_nested_irq outside of sdw_dev_lock")
> moved handle_nested_irq(slave->irq) ahead of slave->sdw_dev_lock and the
> slave->probed check, while sdw_irq_dispose_mapping() disposes of the IRQ
> descriptor without clearing slave->irq or synchronizing with alert work.
>
> [1] https://sashiko.dev/#/bug/linux-90121507-1cdf-435b-b5f4-ce9da544a790
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
  2026-12-01  8:26 [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts() Vijendar Mukunda
  2026-12-01  8:56 ` sashiko-bot
@ 2026-12-01 10:34 ` Mukunda,Vijendar
  2026-12-01 17:14 ` Pierre-Louis Bossart
  2 siblings, 0 replies; 5+ messages in thread
From: Mukunda,Vijendar @ 2026-12-01 10:34 UTC (permalink / raw)
  To: vkoul
  Cc: broonie, yung-chuan.liao, pierre-louis.bossart,
	Sunil-kumar.Dommati, venkataprasad.potturu, Syed.SabaKareem,
	Mario.Limonciello, Richard.Gong, linux-sound, linux-kernel, srini

+ Srinivas

On 12/1/26 13:56, Vijendar Mukunda wrote:
> sdw_handle_slave_alerts() calls pm_runtime_get_sync(&slave->dev) which,
> when slave->dev is not in RPM_ACTIVE, calls rpm_resume() on the slave.
> rpm_resume() propagates synchronously up the parent chain: slave->dev ->
> bus->dev -> manager->dev. If manager->dev is in RPM_SUSPENDING at that
> point, rpm_resume() blocks on dev->power.wait_queue waiting for the
> suspend to complete.
>
> This creates a deadlock: the manager's runtime_suspend callback is blocked
> in cancel_work_sync() waiting for the alert work to finish, while the alert
> work is blocked in pm_runtime_get_sync() waiting for the manager's
> runtime_suspend to complete.
>
> Fix this by first checking the manager device state using
> pm_runtime_get_if_active() on slave->bus->dev->parent (the physical
> manager device). This is an atomic check: if it returns 1 the usage
> counter is incremented in the same operation and the manager cannot
> transition to RPM_SUSPENDING while the reference is held.
>
> - If the manager reference cannot be taken (ret == 0, manager not
>    RPM_ACTIVE): request an async resume via pm_request_resume(mgr_dev)
>    and return early. This avoids I/O on potentially inaccessible hardware
>    and breaks the cancel_work_sync() deadlock. After the manager resumes,
>    the peripheral re-asserts the alert via the first PING frame.
>
> - If the manager reference is taken (ret > 0, manager is RPM_ACTIVE):
>    proceed with pm_runtime_resume_and_get(&slave->dev) to resume the
>    slave. Holding the manager reference prevents the manager from
>    entering RPM_SUSPENDING while the slave resumes, closing the deadlock
>    window for all slave PM states.
>
> - If ret < 0 (-EINVAL, PM disabled on manager): fall through without a
>    manager reference. PM being disabled means no suspend/resume callbacks
>    run, so no deadlock risk. This is consistent with the original -EACCES
>    exemption for the slave.
>
> pm_runtime_resume_and_get() is used for the slave (instead of
> pm_runtime_get_sync()) because it does not increment the usage counter
> on error, simplifying the error-path accounting. Introduce have_pm_ref
> and have_mgr_ref booleans to guard the pm_runtime_put calls at io_err:
> so they are only reached when a reference was actually taken. The
> manager reference is released at the new mgr_put: label after io_err:.
>
> Fixes: aa79293517b3 ("soundwire: bus: fix io error when processing alert event")
> Signed-off-by: Vijendar Mukunda <Vijendar.Mukunda@amd.com>
> ---
>   drivers/soundwire/bus.c | 60 +++++++++++++++++++++++++++++++++++++----
>   1 file changed, 55 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
> index 15c66effbf4f..5dc3598eb85f 100644
> --- a/drivers/soundwire/bus.c
> +++ b/drivers/soundwire/bus.c
> @@ -1667,15 +1667,60 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
>   	u8 buf, buf2[2];
>   	bool parity_check;
>   	bool parity_quirk;
> +	bool have_pm_ref = false;
> +	bool have_mgr_ref = false;
> +	struct device *mgr_dev = slave->bus->dev->parent;
>   
>   	sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
>   
> -	ret = pm_runtime_get_sync(&slave->dev);
> +	ret = pm_runtime_get_if_active(mgr_dev);
> +	if (ret == 0) {
> +		/*
> +		 * Manager is not RPM_ACTIVE (RPM_SUSPENDED, RPM_SUSPENDING, or
> +		 * RPM_RESUMING). Return early to avoid I/O on potentially
> +		 * inaccessible hardware and to prevent a deadlock: if the manager
> +		 * is in RPM_SUSPENDING, its runtime_suspend callback may be blocked
> +		 * in cancel_work_sync() waiting for this work, while this work
> +		 * would block in pm_runtime_resume_and_get() waiting for the
> +		 * manager to complete its suspend.
> +		 *
> +		 * Request an async resume so the deferred alert is re-serviced:
> +		 * - RPM_SUSPENDING: pm_request_resume() sets deferred_resume so
> +		 *   the manager resumes immediately after its suspend completes;
> +		 *   on resume the peripheral re-asserts the alert via the first
> +		 *   PING frame and the interrupt fires again.
> +		 * - RPM_RESUMING: pm_request_resume() is a no-op (device already
> +		 *   resuming); after the manager reaches RPM_ACTIVE, the peripheral
> +		 *   re-asserts the alert via the first PING frame.
> +		 * - RPM_SUSPENDED: pm_request_resume() triggers an async resume;
> +		 *   after resume the peripheral re-asserts via PING.
> +		 *
> +		 * When the SoundWire link fully powers off (e.g. D3 or equivalent),
> +		 * the peripheral loses power and its alert state does not survive;
> +		 * pm_request_resume() is unnecessary but harmless.
> +		 */
> +		pm_request_resume(mgr_dev);
> +		dev_dbg(&slave->dev, "Alert deferred: manager not active\n");
> +		return 0;
> +	}
> +	if (ret > 0)
> +		have_mgr_ref = true;
> +	/*
> +	 * ret < 0 (-EINVAL): PM is disabled (CONFIG_PM=n or disable_depth > 0).
> +	 * Proceed without a manager reference.
> +	 */
> +
> +	ret = pm_runtime_resume_and_get(&slave->dev);
>   	if (ret < 0 && ret != -EACCES) {
>   		dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
> -		pm_runtime_put_noidle(&slave->dev);
> -		return ret;
> +		goto mgr_put;
>   	}
> +	if (ret >= 0)
> +		have_pm_ref = true;
> +	/*
> +	 * ret == -EACCES: PM is disabled on slave; proceed without a
> +	 * slave reference, consistent with the original -EACCES exemption.
> +	 */
>   
>   	/* Read Intstat 1, Intstat 2 and Intstat 3 registers */
>   	ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
> @@ -1861,8 +1906,13 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
>   		dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
>   
>   io_err:
> -	pm_runtime_mark_last_busy(&slave->dev);
> -	pm_runtime_put_autosuspend(&slave->dev);
> +	if (have_pm_ref) {
> +		pm_runtime_mark_last_busy(&slave->dev);
> +		pm_runtime_put_autosuspend(&slave->dev);
> +	}
> +mgr_put:
> +	if (have_mgr_ref)
> +		pm_runtime_put(mgr_dev);
>   
>   	return ret;
>   }


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
  2026-12-01  8:26 [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts() Vijendar Mukunda
  2026-12-01  8:56 ` sashiko-bot
  2026-12-01 10:34 ` Mukunda,Vijendar
@ 2026-12-01 17:14 ` Pierre-Louis Bossart
  2 siblings, 0 replies; 5+ messages in thread
From: Pierre-Louis Bossart @ 2026-12-01 17:14 UTC (permalink / raw)
  To: Vijendar Mukunda, vkoul
  Cc: broonie, yung-chuan.liao, Sunil-kumar.Dommati,
	venkataprasad.potturu, Syed.SabaKareem, Mario.Limonciello,
	Richard.Gong, linux-sound, linux-kernel



On 12/1/26 9:26 AM, Vijendar Mukunda wrote:
> sdw_handle_slave_alerts() calls pm_runtime_get_sync(&slave->dev) which,
> when slave->dev is not in RPM_ACTIVE, calls rpm_resume() on the slave.
> rpm_resume() propagates synchronously up the parent chain: slave->dev ->
> bus->dev -> manager->dev. If manager->dev is in RPM_SUSPENDING at that
> point, rpm_resume() blocks on dev->power.wait_queue waiting for the
> suspend to complete.
> 
> This creates a deadlock: the manager's runtime_suspend callback is blocked
> in cancel_work_sync() waiting for the alert work to finish, while the alert
> work is blocked in pm_runtime_get_sync() waiting for the manager's
> runtime_suspend to complete.

Can I ask why the manager doesn't disable the interrupts before 
initiating a runtime pm_suspend and presumably stopping the clock?

It's a known design feature btw with the clock stop, once you start the 
transition you want to go all the way. If there is indeed a jack or 
something, the device can keep the data line high to resume.

I am not sure if there's any merit is requesting a resume that could be 
done with the regular wake from clock stop?
> Fix this by first checking the manager device state using
> pm_runtime_get_if_active() on slave->bus->dev->parent (the physical
> manager device). This is an atomic check: if it returns 1 the usage
> counter is incremented in the same operation and the manager cannot
> transition to RPM_SUSPENDING while the reference is held.
> 
> - If the manager reference cannot be taken (ret == 0, manager not
>    RPM_ACTIVE): request an async resume via pm_request_resume(mgr_dev)
>    and return early. This avoids I/O on potentially inaccessible hardware
>    and breaks the cancel_work_sync() deadlock. After the manager resumes,
>    the peripheral re-asserts the alert via the first PING frame.
> 
> - If the manager reference is taken (ret > 0, manager is RPM_ACTIVE):
>    proceed with pm_runtime_resume_and_get(&slave->dev) to resume the
>    slave. Holding the manager reference prevents the manager from
>    entering RPM_SUSPENDING while the slave resumes, closing the deadlock
>    window for all slave PM states.
> 
> - If ret < 0 (-EINVAL, PM disabled on manager): fall through without a
>    manager reference. PM being disabled means no suspend/resume callbacks
>    run, so no deadlock risk. This is consistent with the original -EACCES
>    exemption for the slave.
> 
> pm_runtime_resume_and_get() is used for the slave (instead of
> pm_runtime_get_sync()) because it does not increment the usage counter
> on error, simplifying the error-path accounting. Introduce have_pm_ref
> and have_mgr_ref booleans to guard the pm_runtime_put calls at io_err:
> so they are only reached when a reference was actually taken. The
> manager reference is released at the new mgr_put: label after io_err:.

we've gone back and forth on the use of pm_runtime_resume_and_get(), it 
was great until we reverted most of my changes.

see e9537962519e ("soundwire: bus: Fix unbalanced pm_runtime_put() 
causing usage count underflow")
> 
> Fixes: aa79293517b3 ("soundwire: bus: fix io error when processing alert event")
> Signed-off-by: Vijendar Mukunda <Vijendar.Mukunda@amd.com>
> ---
>   drivers/soundwire/bus.c | 60 +++++++++++++++++++++++++++++++++++++----
>   1 file changed, 55 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
> index 15c66effbf4f..5dc3598eb85f 100644
> --- a/drivers/soundwire/bus.c
> +++ b/drivers/soundwire/bus.c
> @@ -1667,15 +1667,60 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
>   	u8 buf, buf2[2];
>   	bool parity_check;
>   	bool parity_quirk;
> +	bool have_pm_ref = false;
> +	bool have_mgr_ref = false;
> +	struct device *mgr_dev = slave->bus->dev->parent;
>   
>   	sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
>   
> -	ret = pm_runtime_get_sync(&slave->dev);
> +	ret = pm_runtime_get_if_active(mgr_dev);
> +	if (ret == 0) {
> +		/*
> +		 * Manager is not RPM_ACTIVE (RPM_SUSPENDED, RPM_SUSPENDING, or
> +		 * RPM_RESUMING). Return early to avoid I/O on potentially
> +		 * inaccessible hardware and to prevent a deadlock: if the manager
> +		 * is in RPM_SUSPENDING, its runtime_suspend callback may be blocked
> +		 * in cancel_work_sync() waiting for this work, while this work
> +		 * would block in pm_runtime_resume_and_get() waiting for the
> +		 * manager to complete its suspend.
> +		 *
> +		 * Request an async resume so the deferred alert is re-serviced:
> +		 * - RPM_SUSPENDING: pm_request_resume() sets deferred_resume so
> +		 *   the manager resumes immediately after its suspend completes;
> +		 *   on resume the peripheral re-asserts the alert via the first
> +		 *   PING frame and the interrupt fires again.
> +		 * - RPM_RESUMING: pm_request_resume() is a no-op (device already
> +		 *   resuming); after the manager reaches RPM_ACTIVE, the peripheral
> +		 *   re-asserts the alert via the first PING frame.
> +		 * - RPM_SUSPENDED: pm_request_resume() triggers an async resume;
> +		 *   after resume the peripheral re-asserts via PING.
> +		 *
> +		 * When the SoundWire link fully powers off (e.g. D3 or equivalent),
> +		 * the peripheral loses power and its alert state does not survive;

that statement isn't quite right, in theory for runtime_pm you *do* want 
the ability to exit from clock stop. It's only in the system suspend 
that it's ok to lose an alert and request the peripheral state when the 
user restarts the device.

> +		 * pm_request_resume() is unnecessary but harmless.
> +		 */
> +		pm_request_resume(mgr_dev);

what happens if you don't do this? My take is that the clock stop wake 
will be used.

> +		dev_dbg(&slave->dev, "Alert deferred: manager not active\n");
> +		return 0;
> +	}
> +	if (ret > 0)
> +		have_mgr_ref = true;
> +	/*
> +	 * ret < 0 (-EINVAL): PM is disabled (CONFIG_PM=n or disable_depth > 0).
> +	 * Proceed without a manager reference.
> +	 */
> +
> +	ret = pm_runtime_resume_and_get(&slave->dev);
>   	if (ret < 0 && ret != -EACCES) {
>   		dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
> -		pm_runtime_put_noidle(&slave->dev);
> -		return ret;
> +		goto mgr_put;
>   	}
> +	if (ret >= 0)
> +		have_pm_ref = true;
> +	/*
> +	 * ret == -EACCES: PM is disabled on slave; proceed without a
> +	 * slave reference, consistent with the original -EACCES exemption.
> +	 */
>   
>   	/* Read Intstat 1, Intstat 2 and Intstat 3 registers */
>   	ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
> @@ -1861,8 +1906,13 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
>   		dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
>   
>   io_err:
> -	pm_runtime_mark_last_busy(&slave->dev);
> -	pm_runtime_put_autosuspend(&slave->dev);
> +	if (have_pm_ref) {
> +		pm_runtime_mark_last_busy(&slave->dev);
> +		pm_runtime_put_autosuspend(&slave->dev);
> +	}
> +mgr_put:
> +	if (have_mgr_ref)
> +		pm_runtime_put(mgr_dev);
>   
>   	return ret;
>   }


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01 17:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  8:26 [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts() Vijendar Mukunda
2026-12-01  8:56 ` sashiko-bot
2026-12-01 10:34   ` Mukunda,Vijendar
2026-12-01 10:34 ` Mukunda,Vijendar
2026-12-01 17:14 ` Pierre-Louis Bossart

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox