mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vijendar Mukunda <Vijendar.Mukunda@amd.com>
To: <vkoul@kernel.org>
Cc: <yung-chuan.liao@linux.intel.com>,
	<pierre-louis.bossart@linux.dev>, <Basavaraj.Hiregoudar@amd.com>,
	<Sunil-kumar.Dommati@amd.com>, <venkataprasad.potturu@amd.com>,
	<Syed.SabaKareem@amd.com>, <Mario.Limonciello@amd.com>,
	<Richard.Gong@amd.com>, <Poojitha.N@amd.com>, <lee@kernel.org>,
	<linux-sound@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	Vijendar Mukunda <Vijendar.Mukunda@amd.com>
Subject: [PATCH v9 24/24] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts()
Date: Tue, 24 Nov 2026 14:18:30 +0530	[thread overview]
Message-ID: <20261124085033.682755-25-Vijendar.Mukunda@amd.com> (raw)
In-Reply-To: <20261124085033.682755-1-Vijendar.Mukunda@amd.com>

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
(e.g. amd_suspend_runtime() or intel_suspend_runtime()) 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 is not
  RPM_ACTIVE): return early to defer the alert. By PM ordering the slave
  is already RPM_SUSPENDED and hardware is inaccessible. The MIPI
  SoundWire spec guarantees the peripheral re-asserts the alert after
  clock resume; when the link fully powers off the peripheral loses
  power and its alert state does not survive, so the early return is
  harmless in both cases. This also closes the race where the slave is
  in RPM_SUSPENDING at the check: if the slave's suspend completes
  before handle_nested_irq() runs and the manager enters RPM_SUSPENDING,
  pm_runtime_get_if_active(mgr_dev) would have returned 0 and the
  function would already have returned early.

- 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 | 52 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 47 insertions(+), 5 deletions(-)

diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
index aeaae5a57c89..0f8d63477927 100644
--- a/drivers/soundwire/bus.c
+++ b/drivers/soundwire/bus.c
@@ -1667,15 +1667,52 @@ 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; slave is already RPM_SUSPENDED
+		 * by PM ordering. Return early to avoid I/O on 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.
+		 *
+		 * When the SoundWire link enters clock-stop mode, the MIPI
+		 * SoundWire spec guarantees the peripheral re-asserts the
+		 * alert after clock resume, so the interrupt is deferred
+		 * rather than dropped. When the link fully powers off
+		 * (e.g. D3 or equivalent), the peripheral loses power and
+		 * its alert state does not survive; the early return is
+		 * harmless.
+		 */
+		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).
+	 * Fall through 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; fall through 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 +1898,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


      parent reply	other threads:[~2026-11-24  8:53 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  8:48 [PATCH v9 00/24] soundwire: amd: SoundWire manager driver bug fixes Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 01/24] soundwire: amd: consolidate pad register macros into amd_init.h Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 02/24] soundwire: amd: program pad SCHMEN and OE control for ACP 7.0/7.1/7.2 Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 03/24] soundwire: amd: add pad ctrl helpers for D0/D3 PM transitions Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 04/24] soundwire: amd: track full pad teardown across suspend/resume Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 05/24] soundwire: amd: fix missing wake interrupt and group wake-arming calls in runtime suspend Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 06/24] soundwire: amd: fix IRQ thread dropping STATUS_0TO7 on simultaneous wake Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 07/24] soundwire: amd: remove STATUS_8TO11 clear from amd_sdw_process_wake_event() Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 08/24] soundwire: amd: fix missing wake interrupt for ACP6.3 in POWER_OFF suspend Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 09/24] soundwire: amd: fix SDW command timeout return value handling Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 10/24] soundwire: amd: cache ping slave status to avoid spurious disconnect on timeout Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 11/24] soundwire: amd: assert msg_lock is held in amd_sdw_read_ping_status() Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 12/24] soundwire: amd: fix work drain ordering in remove path Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 13/24] soundwire: amd: defer dev_set_drvdata() until after INIT_WORK() Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 14/24] soundwire: amd: pm_runtime guard in remove path Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 15/24] soundwire: amd: guard IRQ work with interrupt_enabled and clear STATUS Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 16/24] soundwire: amd: fix *sdw_ctx leak when sdw_amd_startup() fails Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 17/24] soundwire: amd: propagate amd_init_sdw_manager() error on resume Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 18/24] soundwire: amd: drop amd_deinit_sdw_manager() in POWER_OFF suspend Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 19/24] soundwire: amd: replace >= ACP70 with explicit switch/case in PM paths Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 20/24] soundwire: amd: fix interrupt gate and work drain ordering in PM ops Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 21/24] soundwire: amd: check for NULL manager drvdata in sdw_amd_startup() Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 22/24] soundwire: amd: fix shared-register RMW race in amd_sdw_set_device_state() Vijendar Mukunda
2026-11-24  8:48 ` [PATCH v9 23/24] soundwire: amd: fix shared-register RMW race in amd_sdw_host_wake_enable() Vijendar Mukunda
2026-11-24  8:48 ` Vijendar Mukunda [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=20261124085033.682755-25-Vijendar.Mukunda@amd.com \
    --to=vijendar.mukunda@amd.com \
    --cc=Basavaraj.Hiregoudar@amd.com \
    --cc=Mario.Limonciello@amd.com \
    --cc=Poojitha.N@amd.com \
    --cc=Richard.Gong@amd.com \
    --cc=Sunil-kumar.Dommati@amd.com \
    --cc=Syed.SabaKareem@amd.com \
    --cc=lee@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

all inboxes | Powered by JetHome®