From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794466191; b=oGL907hyoqzSNW+tmxO+vTilJeRlvwaOUqQjAu2lZ6JZtXTm17aDa4GDp0DSeOn8LlTp hIuCFXa8oGUoVSNpXNAe4Bac8IwRhALmH7nSTaCRzFUQFhSVWaHArI07ChKlk+VttPEYS tXF7grRRRkWBjuNzbdcq6zbeS7tiPzw1MTNIA/7ccep3eO9i5VUUNNgPHgsc68wYFeCXr UVHO6caMk4ZOmUkltReSYDS08vmqc2PoqhKs2AxhGGvNQuyYcm96MW2dyYMrab1hnIjHh shwrg91pW24CSy3/3p6Dfi3vc+M+SxY08Cvz1/4Woyq32KwACndq9SyMC2SljINxw6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794466191; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=6WXLhdA9GsEkrddx60iks/fOWkdskBmQv8j22OOgMQQ=; b=4IKq3yPgucD0L3dO1FhIT0OZ80NZHjvxBU02R23+apPHJTlq6yADe5Ch7bxmO2UquQda AUMd44UEWdnz9VVe1KiV2lIjL2ad2f9FCDAue/L00q98dt9bLlWFpZxVBVt5+9t3SVrpP lO80c41ecaLFxf+j9qxab0yoKV2Mg0SIUsjUmJct+SXKfm4OGNlBzTJO3uPwFxjpt/q0/ SzRWcQWCqhYt5cxeuaVs0M2W3B0WzGt41oY4pwJeuBW3G0bv7S6NobS4UprYjFxBZ4wd5 IFK4yHlHMuOhCZNrQGMRzqKij4PL4nCV/TWFjxsuy8BFg7cpCPVzwLhZmfI75jvvlkg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=yselnEfz; arc=none smtp.remote-ip=209.85.210.172 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F91E606A89F for ; Thu, 12 Nov 2026 06:49:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1F91E606A89F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=yselnEfz Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-8b5c3288e6cso1730373b3a.3 for ; Wed, 11 Nov 2026 22:49:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794466190; x=1795070990; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6WXLhdA9GsEkrddx60iks/fOWkdskBmQv8j22OOgMQQ=; b=yselnEfzDHvu//Jr94m3Fm/t4MidffROLWmHffVzhSdo/85RXH0TIBV/XxPIVdVFfO KXoO2jOsjlCIv2V3bkz6xGqkDgVpCpVU+aymELVpfuqM2t5ySyZd5AZy+pXLBWgvCiX1 fjA67UKqGS5QS5huF6wiIgiMONM+pHF62GTyfcMYQt03hbGVNlO3Sr5tZphj3Z/6gXaI 2uicRv+AXq4bIhV+V6ZkrlNVIWEv519tdHKyoYV7FyxmHxJmsEiYUOWljwN0wpDGUGzg OIse2qDt99J4VyUaDDv21mxvvHV9hifhqE/agEc2O4Gqh4Xn3dY0eAqNB40vUrku14/j FwYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794466190; x=1795070990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=6WXLhdA9GsEkrddx60iks/fOWkdskBmQv8j22OOgMQQ=; b=aD/dWF9joBgcL9+tda81lc5sQfSQKpplE+ORudBnWYPrJ67HxfHgqDqk2vz+TLFbnK /8BUzdtCdAUuIRAalZFMZkBDPNwdoujUSY2f8FBJp9NLshXv9EO/ENUVN0HcxON6P8P5 RQmqvq8QfdRx2PqRDIMpTH9Vx0SDuoJsaZPEF17DV+/NVZPKEKqlsH8yhdIo9+oTU1+p BqlU5TZQ9KdMnTwl14ZKZCgRQ6OqvuNMjjuk8LlXGtDzBcDvwf/HwIszz/o3+u7dRY1/ Uq3kUmJjC/mGFI0biZg3HdCW51akDKrF+nv5L7DWSZsQhdjlvYOyn4NQr64YZqOrSszR xecA== X-Forwarded-Encrypted: i=1; AL+hErH4w0BSzv7yabGm1EOZMoP2UrLOn2e1OkzQRadpGvPQq7OHC6SAiI5vKfCqOgcVOqEHLCJzAqVSDzchRe0=@vger.kernel.org X-Gm-Message-State: APuU4Z8ZU/14JjzAVvUB2wL5i13WRzLn/2nktP9lqIMpEa/EYBBeHyfU lSj/X6QcwrDB7OoXTC98S+V2Q00S7nt5kehOdjUoHR3Z32PYtcPLIuWI X-Gm-Gg: AYBFou05CYXGiyjT6thfm32eExRsnXPmSDW9URmdrU1hzOGuFQtrKT+7EWOf1+hrvuc 3ypxml2l/NiBMkflfJn8ggP7f8tpnNhuoWHL6ErSoxPK4FTz0DKZUy42HhL9NodANnGfG8/ZzqX Ef7JZVMH1uHMYZGFYmlH+sewi1bIv4EahPz4OdXYz7pXY5pCN3KdMRMItXn408ZUBBbb5lVv6Bm so9revLJC4TUDLKQl1kjwCXU8wGh7vaHpm2j+3onUDCf2zevcDRc7c1YPI+raIIZIOIzDvTJNXK AKjPUFC3ZAz5ZXLKXIANECgh9bGM9ExBH4kvhIOfKywTYgFQ+qH/0hy8Bm/zdkxljjEm36Cy4Iu MWx9EWhyB6mkhUMakS8EQ6FiWQu7Nf5IGfLasJ3VdYa8Jz9JomnrSB3hMnDHAcQOaCApjtKc9lw sr7JJg4PinT+dLZra/ShX6RvvbIAwrVXHA18zvD7mOchifkMJ/c+4V5u27GehSd+1aVcL190VUM jxg7geTVZd2PXeWuvw3uFdM3xohXNBIiQgozla+7XB3jPqZmMm/SkV/8cPfp8isTLgrRAU50ytM AEFQDn446A1Gfox1OEptE58eUMff1C2JgD1gztb3vORqPNPZPVunJeb075jkFd7w/mTq07EMoup V3URurGLeIqj7wfAkJkD0rMZaGWxKD40wegEtLyutelqEg0g= X-Received: by 2002:a05:6a21:498d:b0:3de:79fe:13f with SMTP id adf61e73a8af0-3f2fe0a34f9mr3925552637.38.1794466190304; Wed, 11 Nov 2026 22:49:50 -0800 (PST) Received: from localhost.localdomain ([183.96.175.225]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8baab1228e2sm3097511b3a.41.2026.11.11.22.49.47 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 11 Nov 2026 22:49:49 -0800 (PST) From: Joonho Lee To: linux-sound@vger.kernel.org Cc: tiwai@suse.com, perex@perex.cz, anton.yakovlev@opensynergy.com, mst@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, corbet@lwn.net, skhan@linuxfoundation.org, rdunlap@infradead.org, linux-doc@vger.kernel.org, Joonho Lee Subject: [PATCH v31 04/17] ALSA: pcm: notify drivers after linked prepare completes Date: Thu, 12 Nov 2026 15:49:16 +0900 Message-ID: <20261112064929.11211-5-mynameisjune111@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261112064929.11211-1-mynameisjune111@gmail.com> References: <20261112064929.11211-1-mynameisjune111@gmail.com> X-Mailing-List: linux-kernel@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 A linked prepare can finish one member before another member's driver prepare or reset fails. Drivers that admit transfers after their own prepare callback can then accept an ACK while the group action is still incomplete. Add an optional prepare_complete callback. After all sleeping prepare and reset actions have succeeded, take the group and member PCM locks, publish every PREPARED state, and notify opted-in drivers before releasing those locks. Keep the buffer locks and access gates held throughout the operation. A concurrent DROP observes the group before or after this commit point. On a driver error, notify only the attempted prefix, including the failing member. On any revoked prepare, opted-in members in the affected prefix or group return to SETUP. Preserve DISCONNECTED members and keep SUSPENDED members suspended, but set both saved suspended-state fields to SETUP so a later prepare can recover. Leave unreached members untouched after a driver error. Preflight failures have no completion callback. A successful driver action canceled by a terminal state returns -ESTRPIPE or -EBADFD, respectively. A driver error keeps its original return value. DISCONNECTED takes precedence when a linked group contains both terminal states. Do not generate a stop-timer event while closing prepare admission. Ordinary legacy drivers keep their existing successful preparation and pointer rebasing. The new callback runs with the group and member locks held and must neither sleep nor acquire another PCM stream lock. Assisted-by: LLM Signed-off-by: Joonho Lee --- include/sound/pcm.h | 18 +++++ sound/core/pcm_native.c | 162 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 160 insertions(+), 20 deletions(-) diff --git a/include/sound/pcm.h b/include/sound/pcm.h --- a/include/sound/pcm.h +++ b/include/sound/pcm.h @@ -77,6 +77,24 @@ unsigned long offset); int (*mmap)(struct snd_pcm_substream *substream, struct vm_area_struct *vma); int (*ack)(struct snd_pcm_substream *substream); + /* + * Optional prepare completion notification. Called while the core holds + * all linked PCM stream locks, after prepare/reset actions succeed or stop + * at the first error. On success, the core publishes PREPARED for every + * member before calling this with true. If any member is DISCONNECTED or + * SUSPENDED, opted-in members in the affected group receive false. + * Nonterminal opted-in members become SETUP. DISCONNECTED members remain + * DISCONNECTED. SUSPENDED members remain SUSPENDED with both saved + * suspended-state fields set to SETUP. On a driver failure, called with false + * for opted-in members whose action ran, including the failing member. + * Members not reached and pre-action failures receive no callback. Standalone + * RESET does not call this callback. It must not sleep or acquire another PCM + * stream lock. + * A terminal-state cancellation returns -EBADFD for DISCONNECTED or + * -ESTRPIPE for SUSPENDED. + */ + void (*prepare_complete)(struct snd_pcm_substream *substream, + bool success); /* * Optional RESET operation, also used after a successful prepare. * Called with this substream's PCM lock held; atomic streams must not diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -1310,6 +1310,10 @@ snd_pcm_state_t state); void (*post_action)(struct snd_pcm_substream *substream, snd_pcm_state_t state); + int (*finish_action)(struct snd_pcm_substream *substream, + snd_pcm_state_t state, bool success, + struct snd_pcm_substream *failed_substream, + bool stream_lock); }; /* @@ -1350,12 +1354,19 @@ ops->undo_action(s1, state); } } + if (ops->finish_action) { + ops->finish_action(substream, state, false, s, + stream_lock); + } s = NULL; /* unlock all */ goto _unlock; } } - snd_pcm_group_for_each_entry(s, substream) { - ops->post_action(s, state); + if (ops->finish_action) { + res = ops->finish_action(substream, state, true, NULL, stream_lock); + } else { + snd_pcm_group_for_each_entry(s, substream) + ops->post_action(s, state); } _unlock: /* unlock streams */ @@ -1379,7 +1390,8 @@ */ static int snd_pcm_action_single(const struct action_ops *ops, struct snd_pcm_substream *substream, - snd_pcm_state_t state) + snd_pcm_state_t state, + bool stream_lock) { int res; @@ -1387,10 +1399,19 @@ if (res < 0) return res; res = ops->do_action(substream, state); - if (res == 0) - ops->post_action(substream, state); - else if (ops->undo_action) - ops->undo_action(substream, state); + if (res == 0) { + if (ops->finish_action) + res = ops->finish_action(substream, state, true, NULL, + stream_lock); + else + ops->post_action(substream, state); + } else { + if (ops->undo_action) + ops->undo_action(substream, state); + if (ops->finish_action) + ops->finish_action(substream, state, false, substream, + stream_lock); + } return res; } @@ -1469,7 +1490,7 @@ if (group) res = snd_pcm_action_group(ops, substream, state, true); else - res = snd_pcm_action_single(ops, substream, state); + res = snd_pcm_action_single(ops, substream, state, true); snd_pcm_group_unref(group, substream); return res; } @@ -1501,7 +1522,7 @@ 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, false); snd_pcm_buffer_access_unlock(substream->runtime); return res; } @@ -1660,7 +1681,7 @@ int snd_pcm_drain_done(struct snd_pcm_substream *substream) { return snd_pcm_action_single(&snd_pcm_action_stop, substream, - SNDRV_PCM_STATE_SETUP); + SNDRV_PCM_STATE_SETUP, true); } /** @@ -2063,9 +2084,12 @@ static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, snd_pcm_state_t state) { - snd_pcm_state_t cur_state = snd_pcm_get_state(substream); + struct snd_pcm_runtime *runtime = substream->runtime; + snd_pcm_state_t cur_state; int f_flags = state; + guard(pcm_stream_lock_irq)(substream); + cur_state = runtime->state; if (cur_state == SNDRV_PCM_STATE_OPEN || cur_state == SNDRV_PCM_STATE_DISCONNECTED) return -EBADFD; @@ -2089,20 +2113,118 @@ return snd_pcm_reset_substream(substream, true); } -static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, - snd_pcm_state_t state) -{ - struct snd_pcm_runtime *runtime = substream->runtime; - /* The reset callback path already completed this rebase under the lock. */ - if (!substream->ops->reset) - runtime->control->appl_ptr = runtime->status->hw_ptr; - snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED); +static int snd_pcm_finish_prepare(struct snd_pcm_substream *substream, + snd_pcm_state_t state, bool success, + struct snd_pcm_substream *failed_substream, + bool stream_lock) +{ + struct snd_pcm_substream *s; + struct snd_pcm_group *group = NULL; + bool cancel = false; + int cancel_err = 0; + int depth = 1; + + if (!stream_lock) { + snd_pcm_stream_lock_irq(substream); + group = snd_pcm_stream_group_ref(substream); + if (group) { + snd_pcm_group_for_each_entry(s, substream) { + if (s == substream) + continue; + if (s->pcm->nonatomic) + mutex_lock_nested(&s->self_group.mutex, depth); + else + spin_lock_nested(&s->self_group.lock, depth); + depth++; + } + } + } + + /* Preserve terminal states instead of publishing a partial group commit. */ + snd_pcm_group_for_each_entry(s, substream) { + struct snd_pcm_runtime *runtime = s->runtime; + + if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED) { + cancel = true; + cancel_err = -EBADFD; + } else if (runtime->state == SNDRV_PCM_STATE_SUSPENDED) { + cancel = true; + if (!cancel_err) + cancel_err = -ESTRPIPE; + } + if (!success && s == failed_substream) + break; + } + + if (success && !cancel) { + snd_pcm_group_for_each_entry(s, substream) { + struct snd_pcm_runtime *runtime = s->runtime; + + /* The reset callback path already completed this rebase. */ + if (!s->ops->reset) + runtime->control->appl_ptr = runtime->status->hw_ptr; + __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_PREPARED); + } + } else if (!success || cancel) { + snd_pcm_group_for_each_entry(s, substream) { + struct snd_pcm_runtime *runtime = s->runtime; + + if (s->ops->prepare_complete) { + if (runtime->state == SNDRV_PCM_STATE_SUSPENDED) { + runtime->suspended_state = + SNDRV_PCM_STATE_SETUP; + runtime->status->suspended_state = + SNDRV_PCM_STATE_SETUP; + } else if (runtime->state != + SNDRV_PCM_STATE_DISCONNECTED && + runtime->state != SNDRV_PCM_STATE_SETUP) { + __snd_pcm_set_state(runtime, + SNDRV_PCM_STATE_SETUP); + } + } + if (!success && s == failed_substream) + break; + } + } + + if (!success || cancel) { + snd_pcm_group_for_each_entry(s, substream) { + wake_up(&s->runtime->sleep); + wake_up(&s->runtime->tsleep); + if (!success && s == failed_substream) + break; + } + } + + /* Publish all states before any driver observes completion. */ + snd_pcm_group_for_each_entry(s, substream) { + if (s->ops->prepare_complete) + s->ops->prepare_complete(s, success && !cancel); + if (!success && s == failed_substream) + break; + } + + if (!stream_lock) { + if (group) { + snd_pcm_group_for_each_entry(s, substream) { + if (s == substream) + continue; + if (s->pcm->nonatomic) + mutex_unlock(&s->self_group.mutex); + else + spin_unlock(&s->self_group.lock); + } + snd_pcm_group_unref(group, substream); + } + snd_pcm_stream_unlock_irq(substream); + } + return success && cancel ? cancel_err : 0; } static const struct action_ops snd_pcm_action_prepare = { .pre_action = snd_pcm_pre_prepare, .do_action = snd_pcm_do_prepare, - .post_action = snd_pcm_post_prepare + .finish_action = snd_pcm_finish_prepare }; /**