Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: virtio: report an xrun when a whole buffer passed unseen
@ 2026-11-30 11:59 Eric Curtin
  2026-11-30 12:20 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Curtin @ 2026-11-30 11:59 UTC (permalink / raw)
  To: Anton Yakovlev, Michael S. Tsirkin
  Cc: Takashi Iwai, Jaroslav Kysela, virtualization, linux-sound

If the device completes a whole buffer between two pointer calls, the
progress wraps to zero and the stream stalls with a full buffer while
still RUNNING.

Count the bytes completed since the last pointer call and report
SNDRV_PCM_POS_XRUN when they cover the whole buffer.

Fixes: fe981e67568c ("ALSA: virtio: use ack callback")
Signed-off-by: Eric Curtin <ericcurtin17@gmail.com>
---
Written with LLM assistance; reviewed by me.

 sound/virtio/virtio_pcm.h     |  2 ++
 sound/virtio/virtio_pcm_msg.c |  8 ++++--
 sound/virtio/virtio_pcm_ops.c | 47 +++++++++++++++++++++++++++--------
 3 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h
index 8837dfc12828..c81d3f09fd89 100644
--- a/sound/virtio/virtio_pcm.h
+++ b/sound/virtio/virtio_pcm.h
@@ -29,6 +29,7 @@ struct virtio_pcm_msg;
  *        substream operators.
  * @buffer_bytes: Current buffer size in bytes.
  * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes).
+ * @hw_unseen: Bytes completed since the last pointer callback.
  * @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
@@ -53,6 +54,7 @@ struct virtio_pcm_substream {
 	spinlock_t lock;
 	size_t buffer_bytes;
 	size_t hw_ptr;
+	size_t hw_unseen;
 	bool xfer_enabled;
 	bool xfer_xrun;
 	bool stopped;
diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c
index 72103f606716..2c5729cb5e29 100644
--- a/sound/virtio/virtio_pcm_msg.c
+++ b/sound/virtio/virtio_pcm_msg.c
@@ -315,6 +315,7 @@ static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
 				     size_t written_bytes)
 {
 	struct virtio_pcm_substream *vss = msg->substream;
+	size_t bytes;
 
 	/*
 	 * hw_ptr always indicates the buffer position of the first I/O message
@@ -328,9 +329,12 @@ static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
 	 */
 	if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
 	    written_bytes <= sizeof(msg->status))
-		vss->hw_ptr += msg->length;
+		bytes = msg->length;
 	else
-		vss->hw_ptr += written_bytes - sizeof(msg->status);
+		bytes = written_bytes - sizeof(msg->status);
+
+	vss->hw_ptr += bytes;
+	vss->hw_unseen += bytes;
 
 	if (vss->hw_ptr >= vss->buffer_bytes)
 		vss->hw_ptr -= vss->buffer_bytes;
diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c
index 1091100a4c98..c7fdd837d38f 100644
--- a/sound/virtio/virtio_pcm_ops.c
+++ b/sound/virtio/virtio_pcm_ops.c
@@ -300,6 +300,7 @@ static int virtsnd_pcm_prepare(struct snd_pcm_substream *substream)
 	vss->suspended = false;
 	vss->msg_count = 0;
 	virtsnd_pcm_msg_reset(vss);
+	vss->hw_unseen = 0;
 
 	memset(&vss->pcm_indirect, 0, sizeof(vss->pcm_indirect));
 	vss->pcm_indirect.sw_buffer_size =
@@ -430,6 +431,38 @@ static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream)
 	return 0;
 }
 
+/*
+ * The pointer callback can only report a position inside the buffer, and both
+ * snd_pcm_indirect_{playback,capture}_pointer() and the PCM core derive the
+ * progress from it modulo the buffer size. A whole buffer completed since the
+ * previous call (the elapsed-period work delayed by a buffer time or more while
+ * the device kept completing messages) therefore looks like no progress: for
+ * playback the bytes stay in pcm_indirect.hw_ready and the core's hw_ptr stays a
+ * buffer behind, so nothing more is ever transferred and the stream stalls with
+ * a full buffer while still RUNNING. The device has then played (or captured)
+ * everything the application had provided for, which is an xrun: report it.
+ */
+static snd_pcm_uframes_t
+virtsnd_pcm_pointer(struct snd_pcm_substream *substream,
+		    snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *,
+						 struct snd_pcm_indirect *,
+						 unsigned int))
+{
+	struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
+	size_t hw_ptr, unseen;
+
+	scoped_guard(spinlock_irqsave, &vss->lock) {
+		hw_ptr = vss->hw_ptr;
+		unseen = vss->hw_unseen;
+		vss->hw_unseen = 0;
+	}
+
+	if (unseen >= vss->buffer_bytes)
+		return SNDRV_PCM_POS_XRUN;
+
+	return pointer(substream, &vss->pcm_indirect, hw_ptr);
+}
+
 /**
  * virtsnd_pcm_pb_pointer() - Get the current hardware position for the PCM
  *                         substream for playback.
@@ -441,11 +474,8 @@ static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream)
 static snd_pcm_uframes_t
 virtsnd_pcm_pb_pointer(struct snd_pcm_substream *substream)
 {
-	struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
-
-	return snd_pcm_indirect_playback_pointer(substream,
-		&vss->pcm_indirect,
-		vss->hw_ptr);
+	return virtsnd_pcm_pointer(substream,
+				   snd_pcm_indirect_playback_pointer);
 }
 
 /**
@@ -459,11 +489,8 @@ virtsnd_pcm_pb_pointer(struct snd_pcm_substream *substream)
 static snd_pcm_uframes_t
 virtsnd_pcm_cp_pointer(struct snd_pcm_substream *substream)
 {
-	struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
-
-	return snd_pcm_indirect_capture_pointer(substream,
-		&vss->pcm_indirect,
-		vss->hw_ptr);
+	return virtsnd_pcm_pointer(substream,
+				   snd_pcm_indirect_capture_pointer);
 }
 
 static void virtsnd_pcm_trans_copy(struct snd_pcm_substream *substream,
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: virtio: report an xrun when a whole buffer passed unseen
  2026-11-30 11:59 [PATCH] ALSA: virtio: report an xrun when a whole buffer passed unseen Eric Curtin
@ 2026-11-30 12:20 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 12:20 UTC (permalink / raw)
  To: Eric Curtin
  Cc: virtualization, linux-sound, Michael S. Tsirkin, Eugenio Perez

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] ALSA: virtio: ignored xfer_xrun state in
  virtsnd_pcm_pb_pointer() [1]
--

commit 8e4d30ac2cceb1e139c32d93ba83afe971b299a1
Author: Eric Curtin <ericcurtin17@gmail.com>

ALSA: virtio: report an xrun when a whole buffer passed unseen

This commit tracks bytes completed since the last pointer callback in
hw_unseen and reports SNDRV_PCM_POS_XRUN when an entire buffer passes
unseen.

> diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c
> index 1091100a4c982..c7fdd837d38fd 100644
> --- a/sound/virtio/virtio_pcm_ops.c
> +++ b/sound/virtio/virtio_pcm_ops.c
[ ... ]
> @@ -430,6 +431,38 @@ static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream)
>  	return 0;
>  }
>  
> +/*
> + * The pointer callback can only report a position inside the buffer, and both
> + * snd_pcm_indirect_{playback,capture}_pointer() and the PCM core derive the
> + * progress from it modulo the buffer size. A whole buffer completed since the
> + * previous call (the elapsed-period work delayed by a buffer time or more while
> + * the device kept completing messages) therefore looks like no progress: for
> + * playback the bytes stay in pcm_indirect.hw_ready and the core's hw_ptr stays a
> + * buffer behind, so nothing more is ever transferred and the stream stalls with
> + * a full buffer while still RUNNING. The device has then played (or captured)
> + * everything the application had provided for, which is an xrun: report it.
> + */
> +static snd_pcm_uframes_t
> +virtsnd_pcm_pointer(struct snd_pcm_substream *substream,
> +		    snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *,
> +						 struct snd_pcm_indirect *,
> +						 unsigned int))
> +{
> +	struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
> +	size_t hw_ptr, unseen;
> +
> +	scoped_guard(spinlock_irqsave, &vss->lock) {
> +		hw_ptr = vss->hw_ptr;
> +		unseen = vss->hw_unseen;
> +		vss->hw_unseen = 0;
> +	}
> +
> +	if (unseen >= vss->buffer_bytes)
> +		return SNDRV_PCM_POS_XRUN;

[Severity: High]
This problem wasn't introduced by this patch, but should
virtsnd_pcm_pointer() also check vss->xfer_xrun [1]?

When VIRTIO_SND_PCM_F_EVT_XRUNS is negotiated and the device sends a
VIRTIO_SND_EVT_PCM_XRUN event, virtsnd_pcm_event() sets vss->xfer_xrun =
true under vss->lock.  Commit fe981e67568c ("ALSA: virtio: use ack
callback") dropped the vss->xfer_xrun check from the pointer callback, so
device-reported xruns are silently ignored instead of returning
SNDRV_PCM_POS_XRUN.

[1] https://sashiko.dev/#/bug/linux-4c772c61-641e-49f3-a633-9a55f84dca60

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130115939.434308-1-ericcurtin17@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 12:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 11:59 [PATCH] ALSA: virtio: report an xrun when a whole buffer passed unseen Eric Curtin
2026-11-30 12:20 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox