From: "Mukunda,Vijendar" <vijendar.mukunda@amd.com>
To: vkoul@kernel.org
Cc: broonie@kernel.org, yung-chuan.liao@linux.intel.com,
pierre-louis.bossart@linux.dev, 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,
srini@kernel.org
Subject: Re: [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
Date: Tue, 1 Dec 2026 16:04:25 +0530 [thread overview]
Message-ID: <a542bd0d-dbed-4d02-a2da-053c8c4516a5@amd.com> (raw)
In-Reply-To: <20261201082651.3700713-1-Vijendar.Mukunda@amd.com>
+ 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;
> }
prev parent reply other threads:[~2026-12-01 10:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 8:26 Vijendar Mukunda
2026-12-01 10:34 ` Mukunda,Vijendar [this message]
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=a542bd0d-dbed-4d02-a2da-053c8c4516a5@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=srini@kernel.org \
--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
all inboxes | Powered by JetHome®