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=1796387951; b=G3ZA3l98xgF3X5XrTeoS8sMEWrRTLYow/FXlGDkYjScC+okl1B4caXrWSp8J26ocBuSL byoS7XWgFIHseI7/oGnt/g60R00Xh/m+oxr78xToWYsh9jFIpeJgUuEx8yxD9eN/KCwAl dfPrdX2ggsLJAe2EO95oIBe/jpBiazJVKRn/eWSYHONw3IA73ArdGhH6UYK7awB4YWXBg y1AatjWQq16ifHVXTC1TJgTEsxS9591O+ps7qFG299et8HTRLv1DFq3cs0Hfz58qaSV6u nm5c2W716/gq/rIduMEI0wxpqxYRIrKns7Vmw71lU67neNG3pmQKGMJy1YjBZYr6w7w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387951; 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=+cPoD4Yl35gUGQmuor8ZlmvZ4FbkVU9+PFRXRbz7ibQ=; b=z0o+gh3pRUvs7Y9W6g3KusHUokeULRVl82yA7zPn5k6tG0cXUrsawmnXiBnDhVa8CqAs wxeTHS8l4bw4nXzq1QuVhKOay4eGvvBN+LKlepcXZ54nOvh1DoJeZDCpfSHN1F8iJxjuW DVF32T/jcPPdnf5TQuseLZIKAaUTUYbu+JEumv40/1jUCepk7QUz9TJ5qJKCDxLcB9Lcp f5EcMBTg9300SzC7wqsa0wmpCNgZk8PjCwqB/NYabl0wMFDcOT1FbmPbaD1r/7wPqiend Vvynm+0IB7S5Lp5vhxBNe+6uuwX8OhE7ZlGmBspioQ5zuDHILT3Jyg/u8giKQGhoGTQ== 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=PC3u4XV5; 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 A8EF7606A32C; Fri, 4 Dec 2026 12:39:11 +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=PC3u4XV5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 238A61F000C2; Fri, 4 Dec 2026 12:39:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387951; bh=+cPoD4Yl35gUGQmuor8ZlmvZ4FbkVU9+PFRXRbz7ibQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=PC3u4XV5sYMS6q8xRvmNo23/pFYcb+ItNPaQemu+rSMk62+jShGA5MDr4KtQHTp8b yJfuZfq1ldBCPBrkIV8FpQttJ0hYBlY5JTezZkrNvuvAI8DP60pgrrkT1OSVZVuUYs RInLF9Ie/9TCD9cUspsLBQl82NdJOnzORgqb0yIQ= 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.1 0425/1059] ALSA: aloop: Keep shared timer callbacks alive through close Date: Fri, 4 Dec 2026 18:59:16 +0900 Message-ID: <20261204100655.339669808@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@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.1-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 snd_refcount and scoped_guard with the older stop_count atomic_inc, atomic_dec_and_test plus wake_up, and an exit label, keeping explicit spinlock calls and the existing wait_event in free_cable(). ] 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