Linux virtualization list
 help / color / mirror / Atom feed
From: Eric Curtin <ericcurtin17@gmail.com>
To: Anton Yakovlev <anton.yakovlev@opensynergy.com>,
	"Michael S. Tsirkin" <mst@redhat.com>
Cc: Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>,
	virtualization@lists.linux.dev, linux-sound@vger.kernel.org
Subject: [PATCH] ALSA: virtio: report an xrun when a whole buffer passed unseen
Date: Mon, 30 Nov 2026 11:59:39 +0000	[thread overview]
Message-ID: <20261130115939.434308-1-ericcurtin17@gmail.com> (raw)

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


             reply	other threads:[~2026-11-30 11:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 11:59 Eric Curtin [this message]
2026-11-30 12:20 ` [PATCH] ALSA: virtio: report an xrun when a whole buffer passed unseen sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130115939.434308-1-ericcurtin17@gmail.com \
    --to=ericcurtin17@gmail.com \
    --cc=anton.yakovlev@opensynergy.com \
    --cc=linux-sound@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.de \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox