From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.75 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796146278; b=KhSoh3Jg+eYqYAF/X4eYFQuBJos0RasFmIxcJB9pGPFjfJQs54MqEnxAEU+cWoy3CMKu 2poOdiIK2uyEmQn3qNaGjQzv5xUeHZSKolNxeEUPxm9QfDLGx3CxIrl+xidjuEH1DAqbv Nsk7CtS3A7PmFwfVKkJthIM3zSJjpJcUKRffkaEP09hj0QRa2QH1R9YlNle2OC/kkLg7h 5cYTypF61D2/vVShNQiyYQXJ+ZCAERA17yp6qmhNCnG8NWbZDaPCK6/BiUCi0HIOVln+P OnJ4IQUm5bSFn1yra1f+oFfxtNAuqUcOKPv3TQvJ6VPFOz9XspIoSjG/osgVnpd73QQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146278; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=n4lMaIwBSXn/83lZGEmdazlTMl3hjARmW7WjsBMYjbA=; b=FHl+AMpyKPHC5Lz12Oz+l0mHNAQ0g5ojbaIZWZAw0A1Bt5eNd+TbGUi33qsoXm76+Rmc U67lTnHY5usWON6DH2lHVTlCAX7rsj0a+WfvucTH2O+KdUiN/HP4w/iV8gtnkgouDDd65 ZxaPfED3ZeVcAHgzLeUCtoWJUb5pkoUt/k+NWZ8orZ3SPCB1n+2cn/5tOdeFFGZUK9QfG DIQZ/+DFiGMAuTanQBAJSaqcFTjiUMsgBHRoZaHu20tDzT6vZLPlnCsSvcyCRtMzbc2yZ uOckoKQxp2MWjD/hq9yiV/NtfSZxafClg1AxTZT4tRkIe7+otUfOCTu+/zIVO39oO0g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=rB5zGUYW; arc=none smtp.remote-ip=95.215.58.75 Received: from mta1.migadu.com (out-75.mta1.migadu.com [95.215.58.75]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4560D606A327 for ; Tue, 1 Dec 2026 17:31:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4560D606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=rB5zGUYW X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=14hFDyEb5f9Am3+8ImtQ9mG+0Qgful1IxqfMTEbQ/lI=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796146275; v=1; x=1796751075; b=rB5zGUYWYiJuBwYaT3LDhdR25Bd+E+uYQgyc5Y5DGBWxGghVZCiYOuP6HWemsBSp4XBhGtSE uP8ZnQIsK3JWm4zk/pEj3oDXaACS+EsH/I5A/acybdcJliHUGt85FjkV1/BNoGeDGJYzX4GWspC ka8fbpd7+Ca+Z4v2+tFaGdWw= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 41e4b3ece1756d8b; Tue, 01 Dec 2026 17:31:15 +0000 X-Mizu-Trace-ID: 41e4b3ece1756d8b X-Migadu-Flow: FLOW_OUT Message-ID: <89ff4a51-4346-4013-b645-4bcc8643509c@linux.dev> Date: Tue, 1 Dec 2026 18:14:05 +0100 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] soundwire: bus: use pm_runtime_get_if_active() in sdw_handle_slave_alerts() To: Vijendar Mukunda , 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 References: <20261201082651.3700713-1-Vijendar.Mukunda@amd.com> From: Pierre-Louis Bossart Content-Language: en-US In-Reply-To: <20261201082651.3700713-1-Vijendar.Mukunda@amd.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 > --- > 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; > }