From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796385158; b=RfIK+d+tvy4ee8oUFygZrpoK808yud0QCNFV7iEZr2VhRT/No0gqRjoPMXYAtHOfJ3Fi uNv7SJvhB0ZpIwm8kUNDw5lOffGmEQeV60Y4N38sdL73Qus0TQRDsZWledlV9cuG8BtLp BP2vuqvZBjLQOMcDn0T+Pd9Ig+b2xWIpfxwxXE8z6FpFUy0yWG1cEQ5pQ0F3JFd8whirI xjJxe2MaRd37qtJd5Z2sGWlnQHdA09aigI4pO21GiGoOVdYf6LO7eLqCdNPAtXs7QlbnV MGMIxjjkoCVV+NpckjT6zogtwr0G9sERdJ/Ej09Tmwat0cgd5CgoyeaWhmsHeT1YPJg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796385158; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=SaZ0gP0byTJo1VhqBEEkDER5hXPvWB75rcrDP3JQkaY=; b=Vn6Ba829vKpC4ACOLkrG++p9ICWuSr6YRUrOGFj9WfnlrqW5clREN6Vm0sTR4aPjQc24 MCo2j4EIasN9FJXv9Xs+HMIpyKRzRVRdPaN65rE8PM7wEhrNmXDcugyCb6wX4cxMJHCGF qQbHV+Kl2OUp5Ix0V35ygXJ8DQBIBANmtOTGrWC+kDPVKEQ33LvMlpAqjtxuVjTV2P+xT tPzhMrNUv+xMHQsRPjlXsJvZMiUXKc1n+ayS6bcrsMpjP83ZDfVs+jUpK0xjomD9FDdgb jII1pmxt0u9p9syrpaw99maZeB6BuuV0LzLpiYfIz9yc3g628VvgqjRzigce83BHbhw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Fq0zJVXK; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BC2F606A322; Fri, 4 Dec 2026 11:52:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Fq0zJVXK Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8AA261F000C2; Fri, 4 Dec 2026 11:52:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796385158; bh=SaZ0gP0byTJo1VhqBEEkDER5hXPvWB75rcrDP3JQkaY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Fq0zJVXKYrETGw/TjG1j+8xWK3l+gc5ICY6VQiNUukrfstpRj3st8U9cnGiecV5m5 ZDrefbZcJabp590gRrVMMd2ho0YR7EFCX7MsxRLgAI46QJmbfxLnmMx+Q9ekvMRAWh g+JQPbcFLfBbCU9b+KWAT5OT8uejFAFCtnqLALCg= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Cen Zhang , Takashi Iwai , Sasha Levin Subject: [PATCH 6.6 0441/1167] ALSA: aloop: Keep shared timer callbacks alive through close Date: Fri, 4 Dec 2026 18:57:48 +0900 Message-ID: <20261204100713.501668058@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Cen Zhang [ Upstream commit a7d4d780f34b9e43eefab6a7e6c9f4101f2c144e ] The shared sound-timer callback must finish using the selected PCM runtimes before a close can detach them. loopback_snd_timer_period_elapsed() saves the active substreams under cable->lock, then drops the lock before calling snd_pcm_period_elapsed() or snd_pcm_stop_xrun(). The sound-timer backend has no per-stream close drain, and free_cable() keeps the timer open when the paired endpoint remains attached. With both directions running on a shared sound timer, playback can be dropped and closed while a notification is pending. Capture keeps the timer alive, allowing this order: Timer callback Playback owner -------------- -------------- select playback under cable->lock unlock; delay notification DROP; HW_FREE -> OPEN close: snd_pcm_drop(); unlock free_cable(); peer stays alive snd_pcm_period_elapsed() lock stream; cache runtime snd_pcm_detach_substream() free runtime and its fasync read runtime->fasync The stream lock taken by snd_pcm_drop() cannot protect a notification that starts after it returns, and detach does not take it. The callback can therefore read runtime->fasync after the runtime has been freed, and an escaped notification can also reach a replacement runtime if the same substream slot is reopened before the notification resumes. Take the existing cable->stop_count reference under cable->lock when selecting the targets and release it after the PCM callbacks finish, including the XRUN path. free_cable() already removes the closing endpoint under that lock and waits for stop_count before returning, so runtime detach and slot reuse cannot overtake the notification. Fixes: 26c53379f98d ("ALSA: aloop: Support selection of snd_timer instead of jiffies") Signed-off-by: Cen Zhang Assisted-by: LLM Link: https://patch.msgid.link/pm-pmb-275c4399f517e09d7188-v2-2f64c1328529ef7a8fec@gmail.com Signed-off-by: Takashi Iwai [ sashal: Replaced the snd_refcount get/put/sync calls with atomic_inc, atomic_dec_and_test plus wake_up, and the existing wait_event on stop_count, and adjusted the context for the non-scoped_guard locking. ] Signed-off-by: Sasha Levin --- sound/drivers/aloop.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c index 0602b81275262..e66aef765c04c 100644 --- a/sound/drivers/aloop.c +++ b/sound/drivers/aloop.c @@ -98,7 +98,7 @@ struct loopback_ops { struct loopback_cable { spinlock_t lock; struct loopback_pcm *streams[2]; - /* in-flight peer stops running outside cable->lock */ + /* in-flight PCM callbacks running outside cable->lock */ atomic_t stop_count; wait_queue_head_t stop_wait; struct snd_pcm_hardware hw; @@ -766,6 +766,8 @@ static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable, valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? dpcm_play->substream->runtime : dpcm_capt->substream->runtime; + /* Keep runtimes alive through the PCM callbacks. */ + atomic_inc(&cable->stop_count); /* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */ if (event == SNDRV_TIMER_EVENT_TICK) { @@ -780,7 +782,7 @@ static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable, snd_pcm_stop_xrun(substream_play); if (substream_capt) snd_pcm_stop_xrun(substream_capt); - return; + goto exit; } } @@ -804,6 +806,10 @@ static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable, snd_pcm_period_elapsed(substream_play); if (substream_capt) snd_pcm_period_elapsed(substream_capt); + +exit: + if (atomic_dec_and_test(&cable->stop_count)) + wake_up(&cable->stop_wait); } static void loopback_snd_timer_function(struct snd_timer_instance *timeri, @@ -1048,7 +1054,7 @@ static void free_cable(struct snd_pcm_substream *substream) other_alive = cable->streams[!substream->stream]; } - /* Pair with the stop_count increment in loopback_check_format(). */ + /* Wait for PCM callbacks running outside cable->lock. */ wait_event(cable->stop_wait, !atomic_read(&cable->stop_count)); if (other_alive) return; -- 2.53.0