From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793837380; b=FkXDrA18C7VntnZUuY0hdqY55mIS9erptfapwY0GpGbRoJY2Ca+VnPpllXJpiqnGoNO6 XltDsOfrZLGn073xr5fW9Cnz4ywX1H4ui216+70VkQtXKHziMn2TWwMuPdCD1eSUiHa3k NEBSxrhK2y/86ZfjHnCnA19h3wLB+mBbyvGTHhaBKefRgiPTQhgSAj+9TEld/E5PdnHdG OZT/X7rFSPXTq7CtPdLkLAuaYRfp9zJE0r4uQiZpzQyA/9CpxTQ0EvguvGabiOXaBzCSI pEIYQGzNNs6EDwRjcHU6ooJw9E+WJrmPZWPT2TZggnxP/uhCMRu7JOpFwqoz/r42WHw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793837380; 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=4/DGjjZzZ0GRb1AC8/DxRH3JZkBlPhNEmvZ3LAb6pIs=; b=DbFnkuRb5kEl07mubwu86GmRjlK0ridCR2Qd11ECZMy6A9xedonipqlAOrr8pWQ2Rr3p eRGo1RLgiWuFsMJ2VlIAYnkEuUiOOd95YDGa9Lfq2y3osk0LxX2pD5D5o7dAMaGH7ymSj OwgWKtPA9t9Ue7urzkjtVPSXQQuiUXBVomRkjk2gThMK5O0Ra7z31nSqrk+pGWZ9sI6vB jO50uK99VpPvhaupX4ddO6qwEi60VJk7EJiM02z2CJg3WHWfcdXS6kgu4hc1ovYBcxCaE yiifAwW12RtoDk8kjW5Fk6pp6oHY5ZY3NcnjEZHCJhfpRe5iD8lnlc0P0U5d1FXeBDw== 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=Ws4aO3R0; arc=none smtp.remote-ip=209.85.215.179 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 452C8606EEAC for ; Thu, 5 Nov 2026 00:09:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 452C8606EEAC 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, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Ws4aO3R0 Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-cc4dbda1e5dso521061a12.2 for ; Wed, 04 Nov 2026 16:09:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793837380; x=1794442180; darn=lists.linux.dev; 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=4/DGjjZzZ0GRb1AC8/DxRH3JZkBlPhNEmvZ3LAb6pIs=; b=Ws4aO3R0BrC2+NhizIe0R3BE3O1zvJ/PTxZFSf/YRK+3XI1KN4R6nSyiyV5OjH3yr1 d24GjUYomjSfuppOSwqhOAuN56XtE36IiG42EKCMnj3TvAiMQU2WzVCPhKR86UYBz3Eo UnXXgCUEghT7wWNT6z6QjP3aRC1/mUupYrNMVic59y9FQFCgQPX8ScNmQ46ldLoPeASS TrjFFNfG2FnxwTuRwZnRDqIDUZZkFaHhVe7lw38oK41iltlYvI7w7drROdohePwOTeDX +ZpLm5/v/ovS34QeGzRBuOESKRKwAp+u8vYNq6C4RxdCw+p8ccN+6pf/TT44mvXr9q+E f+7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793837380; x=1794442180; 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=4/DGjjZzZ0GRb1AC8/DxRH3JZkBlPhNEmvZ3LAb6pIs=; b=tS14GjYIfZXWJPcuD+7HMFDYO88VWg3sKWBSPn2D8cIjHejPgxE6KsygbXQnuil78F qY8OdK8mX4hOfi3TOJGZi/v/e4vhb9xS1aFscYgvCRmNx5NmmlAYEyhEmTfprk+qx1jF hASstvTr0Q/N/SLxzl8nzP+Xq4HARKt3WoLQcZLRhW38MF47/bxhA91iB5umFN7Qdvkj evcm22/spbV3Ey/QjFJvsDc/54ZfChZaaCXQ7SevizClqx/ENcse9TfAhbtCMh904x/9 e/7h9h2VYp6XIZVIss7S3gkEcntGCfZpbZXFzNJQNpx4kgg735Or27j57ESPR+pQ66GF e+pA== X-Forwarded-Encrypted: i=1; AKwUvBxRKniDjxphdCaJKeL++eF9qACjE7v5kaXEJDzALDi9EMfB90CCI5sbsRW968s1wsnHgl0Y2G5SJVTFhEd6tQ==@lists.linux.dev X-Gm-Message-State: APuU4Z/6xj3yrROWs4JedCn2JnzgRydMeAyh8iBqzWgrjWwYZiS3o38G huwEAAsK0H0c7wxzZJjRZNFihKIoxMm3lTIJ3zLxoYDDOgAV2ohtYvGH X-Gm-Gg: AYBFou33LLkts1TVp/jy+CTsyAfMMV+cGe+uk/ORH4eG8lnrjnGT+XHm/ACBfE2Xf8k IKYagq9ggFxHrcF8h6oSOpFUYDIjXUzd4zg/YMVCZ2h/kyQvX0PvirXIuJRYm4/gEOUNpIa7U4W TgpMM5IrJZINgK8XcKFSe77xGdI7Qn3miGY6IynIGhlYX7pDT93hUaVbrYq0cy+lhf4w6BYhFoQ 7uVMpJowyb6mxarqE5je7xz0PckXiFNeXlkETbxnYKWbnqVWPMTVbcysCGkJ4SqespbouRednMU XS1+64DLQ/KTf1KvtrQdZJOs5W3al37WWDgwbttxcfAzDLemgYZkCUF6oPpbP/J3QkxIl3lM03K YPrvfenhUPnh60xdcAEx+er7zUuS4mPtt2ttwpKL2tmLW710DAGrbKHOxwznTUx3TRkCtE2AY+N QPldMKWiDI9IJrYe6D8tXu7hpMwHOwkIIbv6YKFsetVvIiVQtDWiPg+xwurNPuMSZegnZE36QKc 79cyTpN5u3H9O3uk9nNFJuCRjhivHjArl6DCJf4go9AVnACW8sKCKfRZp1gzdI5pDDHtWZCz7+d 0tzG20yfWp7UP5afRG9bIorgo3BY/BWpGd+alMrya96ck2ec92UqBCR4px8eebV6L3kDuJ8YFEA 3hmKj8qj5tbCrej9vGO4/AdAeVK9W54RMc1h0TbeRsevlJ3s= X-Received: by 2002:a17:90b:2246:b0:3a4:b8dd:1447 with SMTP id 98e67ed59e1d1-3ae8974bf64mr15546718a91.34.1793837379581; Wed, 04 Nov 2026 16:09:39 -0800 (PST) Received: from localhost.localdomain ([183.96.175.225]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b323c18fsm897208a91.4.2026.11.04.16.09.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 04 Nov 2026 16:09:39 -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 v15 05/12] ALSA: virtio: Recover PCM state after a failed start Date: Thu, 5 Nov 2026 09:09:08 +0900 Message-ID: <20261105000915.15177-6-mynameisjune111@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261105000915.15177-1-mynameisjune111@gmail.com> References: <20261105000915.15177-1-mynameisjune111@gmail.com> X-Mailing-List: virtualization@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 Capture trigger queues I/O messages before PCM_START. If the START message cannot be allocated or sent, transfers may remain queued, and ALSA does not invoke the action undo callback for the failed trigger. Mark the stream stopped and disable transfers for every failed START path. Arm runtime->stop_operating so sync_stop releases pending I/O and prevents prepare, hw_params, or START retries until release succeeds. The stopped flag is shared with ACK callbacks that can run without the ALSA stream lock. Protect every stopped access with vss->lock. Read the pending message count under the same lock in open and hw_params. In sync_stop, decide whether RELEASE is needed and mark the stream stopped in one locked transition; leave it stopped when release or draining fails. Always cancel elapsed-period work in sync_stop, even when no RELEASE is needed. Record the RELEASE obligation after successful PREPARE and clear it as soon as RELEASE succeeds. If the subsequent RX wait fails, retry that wait without submitting RELEASE again. Fixes: da76e9f3e43a ("ALSA: virtio: PCM substream operators") Assisted-by: LLM Signed-off-by: Joonho Lee --- sound/virtio/virtio_pcm.h | 6 ++ sound/virtio/virtio_pcm_ops.c | 107 +++++++++++++++++++++++++++++++---------- 2 files changed, 85 insertions(+), 28 deletions(-) diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -26,13 +26,13 @@ * @hw: Kernel ALSA substream hardware descriptor. * @elapsed_period: Kernel work to handle the elapsed period state. * @lock: Spinlock that protects fields shared by interrupt handlers and - * substream operators. + * substream operators, including @stopped. * @buffer_bytes: Current buffer size in bytes. * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes). * @xfer_enabled: Data transfer state (0 - off, 1 - on). * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun). * @stopped: True if the substream is stopped and must be released on the device - * side. + * side; protected by @lock. * @prepared: True after device PREPARE and the core pointer reset succeed; * protected by @lock. * @preparing: ACK gate through teardown and prepare's core reset; @@ -62,6 +62,8 @@ bool stopped; bool prepared; bool preparing; + /* Successful PREPARE still needs RELEASE; protected by lock. */ + bool release_required; bool suspended; struct virtio_pcm_msg **msgs; unsigned int nmsgs; diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c --- a/sound/virtio/virtio_pcm_ops.c +++ b/sound/virtio/virtio_pcm_ops.c @@ -116,6 +116,15 @@ } } +static void virtsnd_pcm_start_failed(struct virtio_pcm_substream *vss) +{ + scoped_guard(spinlock_irqsave, &vss->lock) { + vss->xfer_enabled = false; + vss->stopped = true; + } + vss->substream->runtime->stop_operating = true; +} + /* Keep completed RX data valid while RELEASE drains the device queue. */ static void virtsnd_pcm_block_ack(struct snd_pcm_substream *substream) { @@ -148,7 +157,7 @@ SNDRV_PCM_HW_PARAM_PERIODS); scoped_guard(spinlock_irqsave, &vss->lock) - vss->stopped = !!vss->msg_count; + vss->stopped |= !!vss->msg_count; vss->suspended = false; /* @@ -250,13 +259,26 @@ { struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); struct virtio_device *vdev = vss->snd->vdev; + unsigned int msg_count; + bool stopped; int rc; virtsnd_pcm_close_ack(substream, false); - if (virtsnd_pcm_msg_pending_num(vss)) { + scoped_guard(spinlock_irqsave, &vss->lock) { + msg_count = vss->msg_count; + stopped = vss->stopped; + } + if (msg_count) { dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n", vss->sid); + return -EBADFD; + } + if (stopped) { + dev_err(&vdev->dev, "SID %u: stream release failed\n", + vss->sid); + scoped_guard(pcm_stream_lock_irqsave, substream) + substream->runtime->stop_operating = true; return -EBADFD; } @@ -310,7 +332,18 @@ struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); struct virtio_device *vdev = vss->snd->vdev; struct virtio_snd_msg *msg; + bool stopped; int rc; + + scoped_guard(spinlock_irqsave, &vss->lock) + stopped = vss->stopped; + if (stopped) { + dev_err(&vdev->dev, "SID %u: stream release failed\n", + vss->sid); + scoped_guard(pcm_stream_lock_irqsave, substream) + substream->runtime->stop_operating = true; + return -EBADFD; + } if (!vss->suspended) { if (virtsnd_pcm_msg_pending_num(vss)) { @@ -353,6 +386,7 @@ scoped_guard(spinlock_irqsave, &vss->lock) { vss->xfer_xrun = false; + vss->release_required = true; } return 0; } @@ -395,33 +429,40 @@ guard(spinlock)(&vss->lock); if (vss->preparing || !vss->prepared) return -EBADFD; + if (vss->stopped) { + substream->runtime->stop_operating = true; + return -EBADFD; + } if (vss->direction == SNDRV_PCM_STREAM_CAPTURE) rc = virtsnd_pcm_msg_send(vss, 0, vss->buffer_bytes); - if (rc) + if (rc) { + vss->stopped = true; + substream->runtime->stop_operating = true; return rc; + } vss->xfer_enabled = true; } msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START, GFP_KERNEL); if (!msg) { - guard(spinlock_irqsave)(&vss->lock); - vss->xfer_enabled = false; - + virtsnd_pcm_start_failed(vss); return -ENOMEM; } - return virtsnd_ctl_msg_send_sync(snd, msg); + rc = virtsnd_ctl_msg_send_sync(snd, msg); + if (rc) + virtsnd_pcm_start_failed(vss); + + return rc; case SNDRV_PCM_TRIGGER_SUSPEND: - scoped_guard(spinlock_irqsave, &vss->lock) - vss->suspended = true; + vss->suspended = true; fallthrough; case SNDRV_PCM_TRIGGER_STOP: - scoped_guard(spinlock_irqsave, &vss->lock) - vss->stopped = true; - fallthrough; case SNDRV_PCM_TRIGGER_PAUSE_PUSH: scoped_guard(spinlock_irqsave, &vss->lock) { + if (command != SNDRV_PCM_TRIGGER_PAUSE_PUSH) + vss->stopped = true; vss->xfer_enabled = false; } @@ -452,26 +493,38 @@ struct virtio_snd *snd = vss->snd; struct virtio_snd_msg *msg; unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms); - bool stopped; + bool need_release; int rc; virtsnd_pcm_block_ack(substream); + scoped_guard(spinlock_irqsave, &vss->lock) { + vss->xfer_enabled = false; + need_release = vss->release_required; + if (need_release) + vss->stopped = true; + } cancel_work_sync(&vss->elapsed_period); - scoped_guard(spinlock_irqsave, &vss->lock) - stopped = vss->stopped; - if (!stopped) - return 0; - - msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_RELEASE, - GFP_KERNEL); - if (!msg) - return -ENOMEM; - - rc = virtsnd_ctl_msg_send_sync(snd, msg); - if (rc) - return rc; + if (need_release) { + msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_RELEASE, + GFP_KERNEL); + if (!msg) { + scoped_guard(pcm_stream_lock_irqsave, substream) + substream->runtime->stop_operating = true; + return -ENOMEM; + } + + rc = virtsnd_ctl_msg_send_sync(snd, msg); + if (rc) { + scoped_guard(pcm_stream_lock_irqsave, substream) + substream->runtime->stop_operating = true; + return rc; + } + + scoped_guard(spinlock_irqsave, &vss->lock) + vss->release_required = false; + } /* * The spec states that upon receipt of the RELEASE command "the device @@ -485,6 +538,8 @@ if (rc <= 0) { dev_warn(&snd->vdev->dev, "SID %u: failed to flush I/O queue\n", vss->sid); + scoped_guard(pcm_stream_lock_irqsave, substream) + substream->runtime->stop_operating = true; return !rc ? -ETIMEDOUT : rc; }