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=1796383445; b=5Sa3RPkpFEuJlc8AktfCJ0kN+l9ykoY7BEm6u11Pg9IEyzWprCuLBsISTsSfCPe6kmga 6HO3Phdc3vqtv9wxt05qAZpqJ15IfEG+to46P+LxqwDTp+Nu9OuJzdfBqUhP4Iis2oHbx 3j1vy01i/kiHddTAaMq8K6ca36dRowME0G18mt0XdqTktXy0Rqzogn1EE+ErdMMN5enur 6r+namlOhuzgM7+PlEdj6OAv+xeC83frkVVzMGIJfk+ZR+1D0yqwobwO4tB27yqvUVp2m RHSCotUPIJs58oIlQmkINGxaGwv4+bpeOpQZ/yzTMmWxFrbcmuyGXExrWFWCGTUW8ig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383445; 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=kA1mOqsnNivAspoWTPVn9OSjoMmpJQVzJO46n3o9Ayo=; b=aCZWVtx8LdboLRyqodCLkFI5M00+8/+pwYtpt0lD5KZtpXhcGPHANBCTG/dAJ5gwZdNH JM6rS5GAXzJyAZKS294VszXv98eo/3aRMSjbcr3dLqDqz0LJ8IAJPJPZ6Uk2WjSXyttJS 8+0TMTC/4XdRaANsAUj+rZzY0N0x+CnHhmZ0/NXHRxR2gyS4Ghby7hqrAuhLMZ5EuDe7G DZsa9gxNNokTD4vNn+dO+kU/nw7OuksZdHP5oUbMTMYibyrBrtGZqurnn2QxR6SNplNv2 bvXI4nvUXZnhBG6YWEWm2bWV4dw+DO0a+AUaSqgAg5/IPyKNsfTybqy7i1sejyYJQNA== 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="h/ClhwUr"; 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 D92AF606A334; Fri, 4 Dec 2026 11:24:05 +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=h/ClhwUr Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9C481F00936; Fri, 4 Dec 2026 11:24:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383445; bh=kA1mOqsnNivAspoWTPVn9OSjoMmpJQVzJO46n3o9Ayo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=h/ClhwUr4cTUtc+yYbz+3YWSQn73idLBAWOWQ7DZWqEyQGUUGYx2t6vZqwlnZMJiR 2AIJ+JGTsS0L52UkoPQiyf80orTcgkcnJKaM5WoQIh+GbLe7Blb7kvpsd9BlqGKviz +9N2eZu1neelSMyXOt3BPBaWv1TFj8zCNUrYUwIs= 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.12 225/555] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic() Date: Fri, 4 Dec 2026 19:04:28 +0900 Message-ID: <20261204100617.628903052@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@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.12-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 8f023ef792213..acdf6a2a3a676 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -714,14 +714,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) { @@ -1251,26 +1257,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; @@ -1285,7 +1303,6 @@ static int snd_pcm_action_group(const struct action_ops *ops, ops->undo_action(s1, state); } } - s = NULL; /* unlock all */ goto _unlock; } } @@ -1293,17 +1310,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; @@ -1430,13 +1449,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