Linux Sound subsystem development
 help / color / mirror / Atom feed
From: "Mukunda,Vijendar" <vijendar.mukunda@amd.com>
To: Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>, vkoul@kernel.org
Cc: broonie@kernel.org, yung-chuan.liao@linux.intel.com,
	Sunil-kumar.Dommati@amd.com, venkataprasad.potturu@amd.com,
	Syed.SabaKareem@amd.com, Mario.Limonciello@amd.com,
	Richard.Gong@amd.com, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
Date: Wed, 2 Dec 2026 12:13:57 +0530	[thread overview]
Message-ID: <d6cff6e3-d8d4-4612-a08e-365bc36ce3aa@amd.com> (raw)
In-Reply-To: <89ff4a51-4346-4013-b645-4bcc8643509c@linux.dev>



On 12/1/26 22:44, Pierre-Louis Bossart wrote:
>
>
> 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?

The fix in bus.c is generic and applies to all SoundWire managers.
Even when a manager disables interrupts early in its suspend sequence,
there is still a timing window where:

1. The slave asserts ALERT; the IRQ fires and the alert work is queued.
2. The manager transitions to RPM_SUSPENDING and runtime_suspend calls
    cancel_work_sync() to drain the work.
3. The in-flight alert work reaches pm_runtime_get_sync(&slave->dev),
    which propagates rpm_resume() up to the manager — blocking on
    dev->power.wait_queue because the manager is in RPM_SUSPENDING.

cancel_work_sync() and pm_runtime_get_sync() deadlock on each other.
In theory this can occur on any SoundWire manager that calls
cancel_work_sync() inside runtime_suspend.

As per our understanding, the fix returns early from
sdw_handle_slave_alerts() in that window to break the deadlock and
calls pm_request_resume() so the alert is re-serviced after the
manager reaches RPM_ACTIVE.

This was originally identified during the AMD SoundWire driver series
review and the bus.c fix was proposed as the resolution:

https://sashiko.dev/#/message/9466c3c5-50f7-4566-9ff4-3d7d8aa4491f%40amd.com

The issue has not been observed in practice on AMD platforms.
Intel's cdns_update_slave_status_work() follows the same pattern,
so the exposure is not AMD-specific.

@Vinod, @Pierre-Louis: given that this has not been observed in
practice and the clock-stop wake mechanism handles most scenarios,
would you prefer to keep this fix or revert to the original
pm_runtime_get_sync() path?  We are happy to defer to your guidance.

>
> 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")

Acknowledged.  The revert `e9537962519e` notes that the previous
use of pm_runtime_resume_and_get() caused a usage count underflow
because pm_runtime_put() was called unconditionally even when
pm_runtime_resume_and_get() returned -EACCES (which does not
increment the counter).

The current implementation addresses this explicitly: have_pm_ref is
set to true only when pm_runtime_resume_and_get() returns >= 0, and
pm_runtime_put_autosuspend() at io_err: is guarded by have_pm_ref.
The put is never called on an error path where no reference was
taken.  This is precisely the conditional-put pattern that the
revert commit described as the requirement for safe use of
pm_runtime_resume_and_get().
>>
>> 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.

Agreed, the comment is imprecise.  For runtime PM the peripheral
retains its state via the clock-stop wake mechanism and the alert is
re-asserted after the manager resumes; pm_request_resume() is
therefore actively useful, not merely harmless.  Losing alert state
without recovery is acceptable only in system suspend (S3/S4) where
the peripheral may be fully powered off.  We will update the comment
to make this distinction explicit.
>
>> +         * 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.
>
For wake-capable CLK_STOP_MODE peripherals, agreed — the wake
mechanism triggers a resume and the alert is re-serviced.

The gap is non-wake-capable CLK_STOP_MODE peripherals.  Without
pm_request_resume(), the manager completes its suspend, the clock
stops, and nothing triggers a resume — the manager stays in
RPM_SUSPENDED and the alert goes unserviced indefinitely.
pm_request_resume() sets deferred_resume=true so the manager
resumes immediately after runtime_suspend returns, restarts the
clock, and the peripheral re-asserts the alert in the first PING.

For POWER_OFF_MODE and CLK_STOP_BUS_RESET the bus reset on resume
clears the peripheral's SCP state, so the alert is lost regardless;
pm_request_resume() is harmless but not needed in that path.

>> +        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;
>>   }
>


  reply	other threads:[~2026-12-02  6:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-12-02  6:43   ` Mukunda,Vijendar [this message]
2026-12-02 15:28     ` Pierre-Louis Bossart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d6cff6e3-d8d4-4612-a08e-365bc36ce3aa@amd.com \
    --to=vijendar.mukunda@amd.com \
    --cc=Mario.Limonciello@amd.com \
    --cc=Richard.Gong@amd.com \
    --cc=Sunil-kumar.Dommati@amd.com \
    --cc=Syed.SabaKareem@amd.com \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=pierre-louis.bossart@linux.dev \
    --cc=venkataprasad.potturu@amd.com \
    --cc=vkoul@kernel.org \
    --cc=yung-chuan.liao@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox