From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F77E39CCEB for ; Tue, 13 Oct 2026 07:44:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791877450; cv=none; b=gX2zccjXf0Y2iQ9N01neGR0eEsfHJWpT0Aw9dfJZQ023/0zRiWtWBOW92gkhZa58x2uIKDjt3PQnoeE3M05UqOwMn/4XJifDHqpxvs42StHMrmCKQKa17ywePTBD9giyk4DUEf8lThnu+8+OEbU4Mi1YtWmcJCXxXDJ1h58+vCQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791877450; c=relaxed/simple; bh=YOzCikLNyNVJcr8w5OXxnR+ioOw9aSMiORLlnNiP9kI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KUlBfmKUBM3wXsPY+w/LqohoZ5YVIedKtlY4fLwDuk0bZNEr0q4zmNr2xx54Zfn06cVajnsu4Fkuc9TKF2Xr9fjr8eTsa4qnxRue++YCnSyuGW2z72NPkjm0/AY2xHCa3lRawPuP5GJzF3nhSkZLy4jDvjWvo2ee3Uo5WeixgDo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FfTJUBy/; arc=none smtp.client-ip=209.85.216.51 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.b="FfTJUBy/" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-3a4cdc9025bso1259472a91.0 for ; Tue, 13 Oct 2026 00:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791877447; x=1792482247; 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=wgs706lD6cJN8VJwbMiWaW3H+6B2tB75P5MVgHEx0P4=; b=FfTJUBy/p27MLiZnGKX50P9tSbyJwq6E/pBwlB8MhO6EtR3K/f96Ac6URQJiSvIv5h GTRlN1YJDwAz8sJ17jj7UxquN81aqb+fU3YqMIvty9kHO4M0O+boeuIyNTKy5/hJ3EGJ fBoobPn/pKdDIjxHlNwRHMU1qdlSr6oOA3OuDbpaKWmP/OFkcHqRG6bMKi7LLDudkaXC BGsdHapay38Le6zZMwbJm5Ou9FvhG3RmRmR/n9tRmzogKxwABtx7V54xGO/aG5wvEc4V 5620l6UtU4SH5bCbSw8S4Dz17bCdUEoKaLozOr7aGBdOB5nW9Lql26w5nsaNE3wRdx6n 4iyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791877447; x=1792482247; 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=wgs706lD6cJN8VJwbMiWaW3H+6B2tB75P5MVgHEx0P4=; b=hZP62+harGHVemkomFQMBv9+R07lyhoddWGdbP+UUGnyam7mn2OxYDQM6/NKqAK3Bt z2BbZHc70CYA5kgm6vMX7PFJT6B4FRfkMO0KqgL0SoMmxwEJ6TFi1J2jfYwoqGd64kWW LWUOyrdTJGbhKNrqkYpKkad47yCeriWYekkSIgxzVHXgEuJg2E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 98e67ed59e1d1-3ac05cac819mr3137611a91.60.1791877446638; Tue, 13 Oct 2026 00:44:06 -0700 (PDT) Received: from localhost.localdomain ([183.96.175.225]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3ab36ed053csm20552825a91.4.2026.10.13.00.44.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 13 Oct 2026 00:44:06 -0700 (PDT) From: Joonho Lee To: Takashi Iwai Cc: "Michael S . Tsirkin" , Jaroslav Kysela , virtualization@lists.linux.dev, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, Matias Ezequiel Vara Larsen Subject: [PATCH v2 3/5] ALSA: virtio: Retain queued messages through device reset Date: Tue, 13 Oct 2026 16:43:48 +0900 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Keep the control queue available through snd_card_free() so close-time STOP and RELEASE commands can still be submitted. Block new control requests and mark pending messages canceled before reset, then release their queue references only after reset and queue deletion. For PCM queues, detach each remaining virtqueue buffer after the device has stopped DMA and retire its message without reporting a normal completion. Detach all queues before deleting them so outstanding DMA mappings are unmapped and no token remains for a late device completion. Fixes: 9d45e514da88 ("ALSA: virtio: handling control messages") Assisted-by: LLM Signed-off-by: Joonho Lee --- Changes in v2: - Detach unused descriptors after reset on every queue, cancel PCM tokens, and use explicit balanced locking in control send. sound/virtio/virtio_card.c | 44 ++++++++++++++++++++++--- sound/virtio/virtio_card.h | 2 ++ sound/virtio/virtio_ctl_msg.c | 62 +++++++++++++++++++++++++++++++---- sound/virtio/virtio_ctl_msg.h | 4 +++ sound/virtio/virtio_pcm.h | 2 ++ sound/virtio/virtio_pcm_msg.c | 29 ++++++++++++++++ 6 files changed, 131 insertions(+), 12 deletions(-) diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c index 6f35276416fe..df7091b74a52 100644 --- a/sound/virtio/virtio_card.c +++ b/sound/virtio/virtio_card.c @@ -337,6 +337,38 @@ static int virtsnd_probe(struct virtio_device *vdev) return rc; } +/** + * virtsnd_reset_vqs() - Stop device access and unmap outstanding buffers. + * @snd: VirtIO sound device. + * + * Context: Any context that permits to sleep. + */ +static void virtsnd_reset_vqs(struct virtio_snd *snd) +{ + struct virtio_device *vdev = snd->vdev; + unsigned int i; + + virtio_reset_device(vdev); + + /* Deleting a virtqueue does not unmap its outstanding descriptors. */ + for (i = 0; i < VIRTIO_SND_VQ_MAX; ++i) { + struct virtio_snd_queue *queue = &snd->queues[i]; + unsigned long flags; + void *msg; + + spin_lock_irqsave(&queue->lock, flags); + if (queue->vqueue) + while ((msg = virtqueue_detach_unused_buf(queue->vqueue))) + if (i == VIRTIO_SND_VQ_TX || i == VIRTIO_SND_VQ_RX) + virtsnd_pcm_msg_cancel(msg); + spin_unlock_irqrestore(&queue->lock, flags); + } + + vdev->config->del_vqs(vdev); + for (i = 0; i < VIRTIO_SND_VQ_MAX; ++i) + snd->queues[i].vqueue = NULL; +} + /** * virtsnd_remove() - Remove VirtIO and ALSA devices. * @vdev: VirtIO parent device. @@ -349,13 +381,14 @@ static void virtsnd_remove(struct virtio_device *vdev) unsigned int i; virtsnd_disable_event_vq(snd); - virtsnd_ctl_msg_cancel_all(snd); if (snd->card) snd_card_free(snd->card); - virtio_reset_device(vdev); - vdev->config->del_vqs(vdev); + virtsnd_ctl_msg_cancel_all(snd); + + virtsnd_reset_vqs(snd); + virtsnd_ctl_msg_cleanup(snd); for (i = 0; snd->substreams && i < snd->nsubstreams; ++i) { struct virtio_pcm_substream *vss = &snd->substreams[i]; @@ -383,8 +416,8 @@ static int virtsnd_freeze(struct virtio_device *vdev) virtsnd_disable_event_vq(snd); virtsnd_ctl_msg_cancel_all(snd); - virtio_reset_device(vdev); - vdev->config->del_vqs(vdev); + virtsnd_reset_vqs(snd); + virtsnd_ctl_msg_cleanup(snd); for (i = 0; i < snd->nsubstreams; ++i) cancel_work_sync(&snd->substreams[i].elapsed_period); @@ -412,6 +445,7 @@ static int virtsnd_restore(struct virtio_device *vdev) return rc; virtio_device_ready(vdev); + virtsnd_ctl_msg_enable(snd); virtsnd_enable_event_vq(snd); diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h index 3ceee4e416fc..bd065bd00d25 100644 --- a/sound/virtio/virtio_card.h +++ b/sound/virtio/virtio_card.h @@ -47,6 +47,7 @@ struct virtio_kctl { * @queues: Virtqueue wrappers. * @card: ALSA sound card. * @ctl_msgs: Pending control request list. + * @ctl_msgs_blocked: Control requests are blocked during teardown or suspend. * @event_msgs: Device events. * @pcm_list: VirtIO PCM device list. * @jacks: VirtIO jacks. @@ -64,6 +65,7 @@ struct virtio_snd { struct virtio_snd_queue queues[VIRTIO_SND_VQ_MAX]; struct snd_card *card; struct list_head ctl_msgs; + bool ctl_msgs_blocked; struct virtio_snd_event *event_msgs; struct list_head pcm_list; struct virtio_jack *jacks; diff --git a/sound/virtio/virtio_ctl_msg.c b/sound/virtio/virtio_ctl_msg.c index 6433c870f271..7114bea4033a 100644 --- a/sound/virtio/virtio_ctl_msg.c +++ b/sound/virtio/virtio_ctl_msg.c @@ -15,6 +15,7 @@ * @list: Pending message list entry. * @notify: Request completed notification. * @ref_count: Reference count used to manage a message lifetime. + * @cancelled: Request canceled during device teardown. */ struct virtio_snd_msg { struct scatterlist sg_request; @@ -22,6 +23,7 @@ struct virtio_snd_msg { struct list_head list; struct completion notify; refcount_t ref_count; + bool cancelled; }; /** @@ -100,7 +102,7 @@ struct virtio_snd_msg *virtsnd_ctl_msg_alloc(size_t request_size, INIT_LIST_HEAD(&msg->list); init_completion(&msg->notify); - /* This reference is dropped in virtsnd_ctl_msg_complete(). */ + /* The caller owns this reference; send adds a queue reference. */ refcount_set(&msg->ref_count, 1); return msg; @@ -134,6 +136,7 @@ int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg, int rc; virtsnd_ctl_msg_ref(msg); + /* This reference stays with the queue until completion or teardown. */ /* Set the default status in case the message was canceled. */ response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR); @@ -147,12 +150,17 @@ int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg, psgs[nouts + nins++] = in_sgs; scoped_guard(spinlock_irqsave, &queue->lock) { - rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg, - GFP_ATOMIC); + if (snd->ctl_msgs_blocked) + rc = -ENODEV; + else + rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, + msg, GFP_ATOMIC); if (!rc) { notify = virtqueue_kick_prepare(queue->vqueue); list_add_tail(&msg->list, &snd->ctl_msgs); + if (notify) + virtqueue_notify(queue->vqueue); } } @@ -169,9 +177,6 @@ int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg, goto on_exit; } - if (notify) - virtqueue_notify(queue->vqueue); - if (nowait) goto on_exit; @@ -187,6 +192,11 @@ int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg, goto on_exit; } + if (READ_ONCE(msg->cancelled)) { + rc = -ENODEV; + goto on_exit; + } + switch (le32_to_cpu(response->code)) { case VIRTIO_SND_S_OK: rc = 0; @@ -227,9 +237,33 @@ void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg) * virtsnd_ctl_msg_cancel_all() - Cancel all pending control messages. * @snd: VirtIO sound device. * - * Context: Any context. + * Context: Any context. Blocks new senders and marks pending requests canceled. + * Existing senders are woken when their request completes or after + * the device has been reset. */ void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd) +{ + struct virtio_snd_queue *queue = virtsnd_control_queue(snd); + struct virtio_snd_msg *msg; + + guard(spinlock_irqsave)(&queue->lock); + snd->ctl_msgs_blocked = true; + list_for_each_entry(msg, &snd->ctl_msgs, list) { + WRITE_ONCE(msg->cancelled, true); + } +} + +/** + * virtsnd_ctl_msg_cleanup() - Release messages left after queue teardown. + * @snd: VirtIO sound device. + * + * This must be called after device reset, buffer detachment, and del_vqs(), + * when no queue callback or DMA access can still reach a message. The queue + * object and its lock must remain alive until this function returns. + * + * Context: Any context. + */ +void virtsnd_ctl_msg_cleanup(struct virtio_snd *snd) { struct virtio_snd_queue *queue = virtsnd_control_queue(snd); @@ -243,6 +277,20 @@ void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd) } } +/** + * virtsnd_ctl_msg_enable() - Allow control requests after queue restoration. + * @snd: VirtIO sound device. + * + * Context: Any context. + */ +void virtsnd_ctl_msg_enable(struct virtio_snd *snd) +{ + struct virtio_snd_queue *queue = virtsnd_control_queue(snd); + + guard(spinlock_irqsave)(&queue->lock); + snd->ctl_msgs_blocked = false; +} + /** * virtsnd_ctl_query_info() - Query the item configuration from the device. * @snd: VirtIO sound device. diff --git a/sound/virtio/virtio_ctl_msg.h b/sound/virtio/virtio_ctl_msg.h index 7f4db044f28e..d917435409d0 100644 --- a/sound/virtio/virtio_ctl_msg.h +++ b/sound/virtio/virtio_ctl_msg.h @@ -68,6 +68,10 @@ static inline int virtsnd_ctl_msg_send_async(struct virtio_snd *snd, void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd); +void virtsnd_ctl_msg_cleanup(struct virtio_snd *snd); + +void virtsnd_ctl_msg_enable(struct virtio_snd *snd); + void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg); int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id, diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h index 8837dfc12828..5787a300d0ee 100644 --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -120,6 +120,8 @@ void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss); void virtsnd_pcm_msg_reset(struct virtio_pcm_substream *vss); +void virtsnd_pcm_msg_cancel(struct virtio_pcm_msg *msg); + int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss, unsigned long offset, unsigned long bytes); diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c index 5371f4985fd0..762480363b86 100644 --- a/sound/virtio/virtio_pcm_msg.c +++ b/sound/virtio/virtio_pcm_msg.c @@ -307,6 +307,35 @@ unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss) return vss->msg_count; } +/** + * virtsnd_pcm_msg_cancel() - Cancel a message detached during device reset. + * @msg: Message returned by virtqueue_detach_unused_buf(). + * + * Reset cancellation retires the queue token without treating it as a device + * completion. In particular, it does not advance the PCM hardware pointer or + * schedule period work. + * + * Context: Process context with the PCM queue lock held. + */ +void virtsnd_pcm_msg_cancel(struct virtio_pcm_msg *msg) +{ + struct virtio_pcm_substream *vss = msg->substream; + bool wake; + + { + guard(spinlock)(&vss->lock); + msg->length = 0; + vss->xfer_enabled = false; + if (WARN_ON_ONCE(!vss->msg_count)) + return; + vss->msg_count--; + wake = !vss->msg_count; + } + + if (wake) + wake_up_all(&vss->msg_empty); +} + /** * virtsnd_pcm_msg_complete() - Complete an I/O message. * @msg: I/O message. -- 2.54.0 (Apple Git-157)