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=1796380200; b=UBYvQF/5pYNvrv1VXl5vBlfhfQFqTLcSitHlFj3gx9d/4VetP43AAN/3qRFT5RI6VZgo ojOukwC1PKYT+E1u390c7P5UVe09sUefcuX0FRhBnEXcl2GZEV5F9/b/otGnooO4yzrf1 IODHUG48F58/gHhji0Oi5lhjt6k3dc4TnLYH+11ZgfHldJBelpi+uXna+V3ePwZz5aU/l PzII3KaiHA67UCtASp12WfSrpmWZlDQ0obYNqnn4Y/3Yx0L6kWMxJs5G+SZag6qchg9e1 IWIZwBgosUiL999y9AtBF7xJeb5xeNu70pT1bVwotNCGpwezYyDqGv98jX+/NDwl2Aw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380200; 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=GdXwdbut6dDnZCt49umjODLBOfRiCF7P1NO8OyeTfPA=; b=PPVsyJDe69yFdkoaWqEhJp7z7R5Kx66/g/pIEqoPOSVCKnDbBJuuGd2WhxOYIfR2CcUq v6+jSIRZTOa2aeS5Ch+3zzf9efWwViPvqfB2F0u7tyGKtC/vaX5EcI/dqzGgL2aNM+LHr S/oMWTnQQ2VjeKZvNAttrT7NjYhYrK73m7CDbT54hTNZjsctBBsxFF0l04CTR8ExzlJjK 5OAlL1nWCAvNHnYlAvL2IeKo5iFQRjtAFYttif7GaXVQTl3c7/S0dqiFOmiUy9A1vLxit Y8qjCvPATIk3zIVSCtthuaVNpJkyvNEkCcAeoyNOqbcsYQiqIXprp6VOiKiCvPQd8Ew== 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=VRuyW9ip; 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 63167606A322; Fri, 4 Dec 2026 10:30:00 +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=VRuyW9ip Received: by smtp.kernel.org (Postfix) with ESMTPSA id 945B01F000C2; Fri, 4 Dec 2026 10:29:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380200; bh=GdXwdbut6dDnZCt49umjODLBOfRiCF7P1NO8OyeTfPA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VRuyW9ipSKijp6szCxeWG9PzsQEsSKBch4jFd1yUTz66x6bM/d+c2A1mIWdY8musD ec2bzXMJsxDFUWJ9CXYbS9Wu06Nh240YZ1J+Da9sxUZTiJBn95Ai/zzKzGCPmcpgCS X2uIRnlCsTpVbx8zYwyIZZ+RAhezxYjD9EuxhLYs= 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 7.3 169/742] ALSA: pcm: Fix potential ABBA deadlock at snd_pcm_action_nonatomic() Date: Fri, 4 Dec 2026 19:00:33 +0900 Message-ID: <20261204100639.590024612@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@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 7.3-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 f64ff3add229a..2bf38ba6950a3 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -741,14 +741,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) { @@ -1278,26 +1284,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; @@ -1312,7 +1330,6 @@ static int snd_pcm_action_group(const struct action_ops *ops, ops->undo_action(s1, state); } } - s = NULL; /* unlock all */ goto _unlock; } } @@ -1320,17 +1337,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; @@ -1457,13 +1476,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