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=1796389013; b=kWQYWMckwBHO8aFpqJxgVnrmASuOLHjkT/xACnlylH/stCJGdb/10G7ionCA74kCbn7U uoveVxYpSzB+aQh0PJ34AEDFvOzOjEs3m2d1fI3bYRlEvNdIk9Jf5GZcsw9DPmmku46g6 IHus0TFi3vaTVxNBrZus8uTTgxU6BP26kjboV8YzmVMpsUXPzZazOrtALp2KYXaR6GvHr Rsui/N5VQOboMHqkvP2pfsJWBs1QheMiKTCRdcnYFXfHV0RfbmrMjAKiSAVB8rlAxok3f znUM1EPm5Q5k03YmGWcj0SL8OxDFqX1q42xaRYDqurPCbd+Am/YkpTUAKayh7NpTqrg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389013; 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=aHZfBNWRRqWeoJh6w6hIwzhMzwkrWVaEJstmk4EKq0I=; b=qMI2kD9EI6CMchnM+Lz/0LMgiKfZHDm9T9XY1m5gltRcJupUyuvfXb0c5EJ1aCFXUlWb T3oLl1SOyaJU15H2jpZ0eP/o2hDYDR1VwfWdM0GbSYCHcCSd4jcTEJR6hvmiMBJe19NX4 7H9BWY47o6L+4izZ0bMpCA5HoT1Z1De2tlvsZ/sAIBZ/nVTD8/2CfP/K9vVRa3caXIrda 1dwlajecsVx/+gD1ryRi1beCZ1S5RXfZRQKEh9mB8YRYXra+olUJmeEhrJFm1YSAfTUda EGImIlFUIFceJP8bD6lf9+vFR6CM90rTJ7HAVw3Dj8z+gKw5sYLixhGdZpBvCKtKrnA== 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=jtWvssaE; 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 4946A606A32C; Fri, 4 Dec 2026 12:56:53 +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=jtWvssaE Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59D451F00A07; Fri, 4 Dec 2026 12:56:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796389012; bh=aHZfBNWRRqWeoJh6w6hIwzhMzwkrWVaEJstmk4EKq0I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jtWvssaE4eqa0YzZsdFRY4VLIcFlvUJFTEe6j7FvXNHWNgFEhYrT6GmD9e7Ql8irJ wLiFTsnpTdbggJygsiX7uLrqrZiLcvxZX54ZkX4zm4L5U8ToKkFMm27UuzTEn2iz/d 2GI3zXA+tcZUI/BqO35Jlsvri3qMjx8l3ESlnzUo= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, sashiko-bot@kernel.org, Takashi Iwai , Sasha Levin Subject: [PATCH 6.1 0833/1059] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic() Date: Fri, 4 Dec 2026 19:06:04 +0900 Message-ID: <20261204100705.601433386@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: patches@lists.linux.dev 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: Takashi Iwai [ Upstream commit bedefcbb8a0827e2758d6fc4a0601756eba7c248 ] snd_pcm_action_nonatomic() is called for PCM PREPARE and other operations, and the function takes snd_pcm_buffer_access_lock() of the given substream (that takes runtime->buffer_mutex internally) at first, followed by the call of snd_pcm_action_group() that takes buffer_mutex of other substreams in the same link group again. Recently Sashiko suggested it may lead to an ABBA deadlock when two different substreams in the same PCM link call it concurrently: CPU0 CPU1 ---- ---- lock(&A->runtime->buffer_mutex) lock(&B->runtime->buffer_mutex) lock(&B->runtime->buffer_mutex) lock(&A->runtime->buffer_mutex) This patch tries to address the potential deadlock. Instead of taking the buffer_mutex of the target substream at first and processing the rest in the loop, take the whole buffer_mutex for each in the loop. This assures the order of the nested mutexes -- the linked list itself is kept intact by the read_rwsem() around snd_pcm_action_nonatomic(). Also, this changes the action in the snd_pcm_action_group() loop from the direct call of mutex_lock(buffer_mutex) to snd_pcm_buffer_access_lock(). This prevents the unexpected state transition or the buffer access during the action, too. Since the target substream is no longer locked before the loop, the lockdep subclass for buffer_mutex in the loop now starts from 0, so that a link group of N substreams uses the subclasses 0..N-1 as before. The subclass for the stream lock case still starts from 1, as the caller holds the target stream lock at subclass 0. Note that this change might have a slight side-effect, though: when some concurrent operations happen during the PREPARE or RESET, now it may return -EBUSY. But this can be seen rather as an expected behavior that was overlooked before the fix. Fixes: 3c3201f8c7bb ("ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls") Reported-by: sashiko-bot@kernel.org Closes: https://lore.kernel.org/sashiko-outbox-221145@kernel.org Signed-off-by: Takashi Iwai Link: https://patch.msgid.link/20261119122356.2134523-2-tiwai@suse.de Signed-off-by: Sasha Levin --- sound/core/pcm_native.c | 63 +++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index 31a6f2940454a..cc4b564f89a76 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -682,14 +682,20 @@ static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, /* acquire buffer_mutex; if it's in r/w operation, return -EBUSY, otherwise * block the further r/w operations */ -static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime) +static int __snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime, + int depth) { if (!atomic_dec_unless_positive(&runtime->buffer_accessing)) return -EBUSY; - mutex_lock(&runtime->buffer_mutex); + mutex_lock_nested(&runtime->buffer_mutex, depth); return 0; /* keep buffer_mutex, unlocked by below */ } +static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime) +{ + return __snd_pcm_buffer_access_lock(runtime, 0); +} + /* release buffer_mutex and clear r/w access flag */ static void snd_pcm_buffer_access_unlock(struct snd_pcm_runtime *runtime) { @@ -1225,26 +1231,38 @@ struct action_ops { * this functions is core for handling of linked stream * Note: the stream state might be changed also on failure * Note2: call with calling stream lock + link lock + * + * Locking: + * When @stream_lock is false, it takes buffer-access lock for all substreams. + * OTOH, when @stream_lock is true, takes substream lock except for the + * target substream (that should have taken its lock before calling this). */ static int snd_pcm_action_group(const struct action_ops *ops, struct snd_pcm_substream *substream, snd_pcm_state_t state, bool stream_lock) { - struct snd_pcm_substream *s = NULL; - struct snd_pcm_substream *s1; - int res = 0, depth = 1; + struct snd_pcm_substream *s, *s1, *last; + int res = 0, depth; + /* the caller already holds the target stream lock at depth 0 */ + depth = stream_lock ? 1 : 0; + + last = NULL; snd_pcm_group_for_each_entry(s, substream) { - if (s != substream) { - if (!stream_lock) - mutex_lock_nested(&s->runtime->buffer_mutex, depth); - else if (s->pcm->nonatomic) + if (!stream_lock) { + res = __snd_pcm_buffer_access_lock(s->runtime, depth); + if (res < 0) + goto _unlock; + depth++; + } else if (s != substream) { + if (s->pcm->nonatomic) mutex_lock_nested(&s->self_group.mutex, depth); else spin_lock_nested(&s->self_group.lock, depth); depth++; } + last = s; res = ops->pre_action(s, state); if (res < 0) goto _unlock; @@ -1259,7 +1277,6 @@ static int snd_pcm_action_group(const struct action_ops *ops, ops->undo_action(s1, state); } } - s = NULL; /* unlock all */ goto _unlock; } } @@ -1267,17 +1284,19 @@ static int snd_pcm_action_group(const struct action_ops *ops, ops->post_action(s, state); } _unlock: + if (!last) + return res; /* unlock streams */ - snd_pcm_group_for_each_entry(s1, substream) { - if (s1 != substream) { - if (!stream_lock) - mutex_unlock(&s1->runtime->buffer_mutex); - else if (s1->pcm->nonatomic) - mutex_unlock(&s1->self_group.mutex); + snd_pcm_group_for_each_entry(s, substream) { + if (!stream_lock) { + snd_pcm_buffer_access_unlock(s->runtime); + } else if (s != substream) { + if (s->pcm->nonatomic) + mutex_unlock(&s->self_group.mutex); else - spin_unlock(&s1->self_group.lock); + spin_unlock(&s->self_group.lock); } - if (s1 == s) /* end */ + if (s == last) /* end */ break; } return res; @@ -1408,13 +1427,13 @@ static int snd_pcm_action_nonatomic(const struct action_ops *ops, /* Guarantee the group members won't change during non-atomic action */ guard(rwsem_read)(&snd_pcm_link_rwsem); + if (snd_pcm_stream_linked(substream)) + return snd_pcm_action_group(ops, substream, state, false); + res = snd_pcm_buffer_access_lock(substream->runtime); if (res < 0) return res; - if (snd_pcm_stream_linked(substream)) - res = snd_pcm_action_group(ops, substream, state, false); - else - res = snd_pcm_action_single(ops, substream, state); + res = snd_pcm_action_single(ops, substream, state); snd_pcm_buffer_access_unlock(substream->runtime); return res; } -- 2.53.0