From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.123 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796226597; b=HD7y+NyNCjfH4a7WHseV3Z4VxEu4dVZQAhQQyYpHWxwBldnI8pyvlcknVoBC0pETbeeO waCl6mCOjTTwJFf3S82UzfU8CbpuGzy7OxpAEhuJPaPv5pBs8Py+U04d+vl7isAjaQvKw aWQYngNiVh6a+5Cin+qD0hmnMoDKjvayNqa6dhIb1W96t0vFuoUSWRixtM/reNr4/yJS2 jMsqi4o7D+27BFejqtyGNxznBk40OH8Jo0JfDoINznCeKvWgiaipvZPauWx53SvcgJhuI CeEwWjaDwXpzpNqlbPEQEl/I3xnRouFwy4jQwwCy5slQt6+SJY9tg6pnlyITlssZu8Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796226597; 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:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=QIzWLHcgQ9jpvHWdafL7kK7NreHpDp9qjtfeTOZxkLw=; b=lxCwBAtervzsbIH2cyH2q3vnXiSYKNekom/1ViE7sbCuB77PiNxNdVNlaK73l1oNcQeM Xy4Mi6hRPuzp1FRqiDN4i1CI+8hCMAahcel+MTvQTfKWKYN0RaAmd9WRTGiaw1RMOG5f7 xOWAgP9RSP5a7Ic+33hlbys/n77m5rOqPPINiKgc9yXL4r70ud7IdeMAHgqeZIJIOJ/x9 gL3hKQDWEhWFkYj465SI2PaKOTai6d05KJT9q32ZRjeggiBEa3TDSvJ5RUo8BvzcJw5w6 JbJavbLDoQ+T9Wi9AEwKyQXJVtLPQ1AS5bT+IpfRE5OuCILhW3zOwGPMD2nvc9t3KuQ== 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=RxQ4SVjo; arc=none smtp.remote-ip=91.218.175.123 Received: from mta0.migadu.com (out-123.mta0.migadu.com [91.218.175.123]) (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 88DF6606A31A for ; Wed, 2 Dec 2026 15:49:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 88DF6606A31A 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=RxQ4SVjo X-Envelope-To: linux-sound@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=dVDshDWd5duaSxDpZWsViIp1Vr4IG0P+dJnjgF6qMvY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796226594; v=1; x=1796831394; b=RxQ4SVjo4y3fGrvgXjTpeGIoA186BSYA+2DrhaX/9ZvVKq7Nm6CO2v3xWAOuogwsEN+uBNCy vw4hRIMpsX1fpzYjvQUqolEexmu6ym/kun1MTfQY80k8wCRpytHMX0gZtvD4ks7fqrzCEvmv1o6 sXy5PtVwFjjIfJ5JnPBqgoyc= X-Envelope-To: linux-sound@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id c5929580b82d64f5; Wed, 02 Dec 2026 15:49:54 +0000 X-Mizu-Trace-ID: c5929580b82d64f5 X-Migadu-Flow: FLOW_OUT Message-ID: <89d18c0d-b19b-435c-aebd-e11c2793cb24@linux.dev> Date: Wed, 2 Dec 2026 16:28:56 +0100 X-Mailing-List: linux-sound@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: "Mukunda,Vijendar" , 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> <89ff4a51-4346-4013-b645-4bcc8643509c@linux.dev> Content-Language: en-US From: Pierre-Louis Bossart In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/2/26 7:43 AM, Mukunda,Vijendar wrote: > > > 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. my understanding is: - if the manager is already suspended, then the interrupt handling was disabled a long time ago. - if the manager resumes, there's no deadlock. So it'd really only be in the case of the manager starting to suspend with a peripheral already suspended but able to generate interrupts. Those interrupts would most likely be related to jack insert/removal or a catastrophic issue like reporting an amplifier catching on fire... I believe the solution suggested would cover this corner case, but it'd need to be documented as a paranoia check based on code analysis, with an additional dev_warn() or dev_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") > > 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(). ok, I must admit I didn't trace all the branches, if you've looked into this I'll give you the benefit of doubt. >>> 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. I would reword this paragraph to say that the suggested fix only improved functionality for the RPM_SUSPENDING case, and is harmless in all other cases. >>> +         * 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. I am not sure we need to care about those 'non-wake-capable CLK_STOP_MODE peripherals' since they would not work reliably anyway for regular pm_runtime. Adding support for this narrow window where this race condition happens doesn't fundamentally make those devices work well.. > 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. agreed.