Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793664256; b=Kn2HTf4mD3WBxsWvVy1hSdV2FVDiAc9JDK1OwrKyPHQPEu+125fuhGVbGgj939uaEZsv fE/10EnkhOmxuHMfAzAlZet+PMweWJpRpA3Vr+EuewzszDqBsDhnNAr7/NemCO/K8nPLR sbrzcoh7H5bysk1WkzVx3BaDRWIlJ19+rSAqTn50rdV8dRA5HS9pN4hMAwQ5oOavLZ7Px yNAJRmtgCu4dSI6J9L6G6gzjrRwFFLNMKzEF8exc5LkDY2GUJCU8wdmNz3zd1RmsNarn4 JXoLetNeLQB1JroQ+ME53hbVwihn2fDR99N5RdQVwLZVPlIdUo1VHqqmbD291RD41nQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793664256; 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=pPsijI/WFroQ3WU8JWBkdFg+dO3cPuO9jwy6Y07G2CQ=; b=2BRfkR6GeaKQFmLFvaUa9f1WHc+QT+GUhVi8+nKE9z+Axc9PKZhpcRAyecEHear2NVqZ gFEPz+nI2jBEsGDr9Py8A0tO3b7iylv6xWhexWfiZeJGLH7llDZcSbMPU5+KgYgcHVd1W cqr6XNJfrgUltqdXChma5G8TMz+QXm/fdNZgm1kzT4aCMVTsPB0lJLnumg0zlv+dpC40y k0BeWunYsqNFvGNW9tjLbDYDYMm7MQV3PWOGsvyeMlqS9F6/RPT8FXwlZk23/eA1P1MnK mkD5psLFiTfKk4soXgDFJjd8LXUzXXr+3dmoYcMSiaNW+WFfLC7XMBcEQBxvCSXIx7g== 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="ZQUz/2/L"; arc=none smtp.remote-ip=209.85.214.180 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 7CAE4606D808 for ; Tue, 3 Nov 2026 00:04:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7CAE4606D808 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=ZQUz/2/L Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2e5378b281aso21348225ad.2 for ; Mon, 02 Nov 2026 16:04:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793664256; x=1794269056; 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=pPsijI/WFroQ3WU8JWBkdFg+dO3cPuO9jwy6Y07G2CQ=; b=ZQUz/2/LkaFJ5t87SwgV+Ss4Vymu21X/tqN7cyzYh4EMON3ZlzHW6U7r1HtaORJfWt p3Hj2gA5HWqaoHG2l+UHLysKknIRnt29eenmi0qiAEeZOHD+MtBRFfObS8cH5rZTFvue knzXOfGLml9AhIlT6TAttNUL99ZpoblnzP7a9TFMi5tPDbFnmSv7Bd+AQfRDlSpgVpG1 8PSFdByB468LzGcEapWepEyQljnXrCF521AjvFO5B07njpO20AjYK0zWGqOIhRyJCD43 5XoYetNYtUHuWQ3gfrRjVIK4chweMlrl/LdR62XeLDgQznyBE6u/uMgi9vp9Z82S8cXe 9Esw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793664256; x=1794269056; 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=pPsijI/WFroQ3WU8JWBkdFg+dO3cPuO9jwy6Y07G2CQ=; b=tM0+7PeOvXx+eAwpQFLQZsj6ecWE2S5ewF2f5QoIa0/x4qPe6hCQN7cTx/a4k40+RZ qOfcoIRtVO/7/cwdJK+WMnWFbxxdkKmAuBw46d8oi4ENneDHiv1k+MWu505IcBtEW2Io eKDGWZPlb1Xr1/2O+xetg5dd1EDYoHRj/P1Ab3dkKODBBIoV2HH0QvzwGkS6Bs2QJLp3 kyStdFMy/r7o0UWypyuxRz9+ZUF+teE7Sdi6cyICk9CAAn3h5ARtE74g8CLnJVOSIgM/ e0Zz7wZPd4Mwq2IWlXHlCV2uKDJ4HNLfJW9gpDAVeMUlFk0XardvLuxhakdQXodJbb/q BU6Q== X-Forwarded-Encrypted: i=1; AKwUvByISsJ5Z+eCJpa0JHXkLZ/QGa+3JL8DJOE7OgjhSJBxIq6d6wVqc+Qn+7uVSJHGGXOT3ANLi8SlFnk=@vger.kernel.org X-Gm-Message-State: APuU4Z+55BQOlHB/a0iEfP2S2HjfbpdtUreu0+ICeuALfKDrczKiLZwt cqPW0c0Q7rPE9Ci75NS2HS2gfQjJmy26zRS2JZP/XAGOuXNeWnSRy7CK X-Gm-Gg: AYBFou0a9ttaS/MRLFM+xFf48EmcmOmiONwL/zyvIBXSLxUvY1gxWPIyzaNI7c5Z6m+ uxTqrtIG05/QSSFE1z2Bwvudc/FcEtnlfHmb26YsfAnJ8ZgH8yOh6D7fGtigbbffHlCtMyt8ABm EjcSxB1yePi2pxSrv3cWJ5jn5PmTBW2UyLxmET74aXl0nX8ZE9i4OqdvH2bMlqqwmgfkhNR75hw ZeCCHhCHIgjvQGx+bZNOv2APhqQo3bcGRyyzWOUJzFEWCN2FGxTCgj0TLw9mUew8YGuC5LWF4+o 1deovl/w+DYt8RldoY4h7qr+6L6hOMgbEC3Ob4H0hMyLCGbaN6VOGOjfhB38s4zWVOaI5auN/+y EO9WPQHbjUQp9zDYFzor6LOPdCm0plUsaJqmRg+1TEb5++I67hVVPBmPOSPNVK4L9ra9cTU3O6/ grEcvsOdyWTtYD0WalnaVrxFhZCO9ivZlOONFFGZA0NY6ApJB3FiE3lzKh7APeYs5RuMpCuCq4m 8Y9K8whyiipbVuHkAOvXTGy4vuwb4nPyJwlT9LVANoWfJGHRDXwFuycAwiXowV8nQG4wgDXGuPC CCNwSDcSeRjb9vMkWFZQSxXuK7vlEaI32L13i1fWyOiwuiHoGDkgwWMKP9KNkpxWdqWszr7wtX7 UC9/398siH/GIyY5FPqYYZybCSf7GtXcrV6LwzT45Ux3XuAk= X-Received: by 2002:a17:90b:53c7:b0:3ac:7e3a:1033 with SMTP id 98e67ed59e1d1-3ae897c5e5dmr9665911a91.59.1793664255901; Mon, 02 Nov 2026 16:04:15 -0800 (PST) Received: from localhost.localdomain ([183.96.175.225]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b07c6b3493sm1339225a91.13.2026.11.02.16.04.12 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 02 Nov 2026 16:04:15 -0800 (PST) From: Joonho Lee To: linux-sound@vger.kernel.org Cc: tiwai@suse.de, 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 v12 02/12] ALSA: virtio: Recover PCM state after a failed start Date: Tue, 3 Nov 2026 09:03:53 +0900 Message-ID: <20261103000403.67106-3-mynameisjune111@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261103000403.67106-1-mynameisjune111@gmail.com> References: <20261103000403.67106-1-mynameisjune111@gmail.com> X-Mailing-List: linux-doc@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 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 --- 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. * @suspended: True if the substream is suspended and must be reconfigured on * the device side at resume. * @msgs: Allocated I/O messages. @@ -56,6 +56,8 @@ bool xfer_enabled; bool xfer_xrun; bool stopped; + /* 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 @@ -96,6 +96,15 @@ static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream); +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; +} + /** * virtsnd_pcm_open() - Open the PCM substream. * @substream: Kernel ALSA substream. @@ -115,7 +124,8 @@ snd_pcm_hw_constraint_integer(substream->runtime, SNDRV_PCM_HW_PARAM_PERIODS); - vss->stopped = !!virtsnd_pcm_msg_pending_num(vss); + scoped_guard(spinlock_irqsave, &vss->lock) + vss->stopped |= !!vss->msg_count; vss->suspended = false; /* @@ -217,11 +227,23 @@ { 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; - 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); + substream->runtime->stop_operating = true; return -EBADFD; } @@ -273,6 +295,17 @@ 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); + substream->runtime->stop_operating = true; + return -EBADFD; + } if (!vss->suspended) { if (virtsnd_pcm_msg_pending_num(vss)) { @@ -311,7 +344,12 @@ if (!msg) return -ENOMEM; - return virtsnd_ctl_msg_send_sync(vss->snd, msg); + rc = virtsnd_ctl_msg_send_sync(vss->snd, msg); + if (!rc) { + guard(spinlock_irqsave)(&vss->lock); + vss->release_required = true; + } + return rc; } /** @@ -338,31 +376,40 @@ scoped_guard(spinlock_irqsave, &queue->lock) { guard(spinlock)(&vss->lock); + 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: vss->suspended = true; fallthrough; case SNDRV_PCM_TRIGGER_STOP: - 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; } @@ -393,21 +440,34 @@ struct virtio_snd *snd = vss->snd; struct virtio_snd_msg *msg; unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms); + bool need_release; int rc; + 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); - if (!vss->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) { + substream->runtime->stop_operating = true; + return -ENOMEM; + } + + rc = virtsnd_ctl_msg_send_sync(snd, msg); + if (rc) { + 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 @@ -421,11 +481,13 @@ if (rc <= 0) { dev_warn(&snd->vdev->dev, "SID %u: failed to flush I/O queue\n", vss->sid); + substream->runtime->stop_operating = true; return !rc ? -ETIMEDOUT : rc; } - vss->stopped = false; + scoped_guard(spinlock_irqsave, &vss->lock) + vss->stopped = false; return 0; }