From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796039984; b=P2LnvXoKE0anyuSjUjS+Af0B3wx6TdADJpqQERfqNqa8iGEPaqce5cQYg+r2R271fasX Cp5Gx64YSK3QDo9Rg0s5J07qJRb8ZxIMQcgh7UHJR2C+i8vvGV23vAXiPM8P1LEnVgAlP oQ9Gqv6rUzKIGRkoYpAKkjtdkWid5oylqQ5Bz5FTE3sFd2tFQoCp6qxwH2WvfUKy0M6xG v41kJAawNhQcgfC/LqNvbjI3/SDvjOgAG/PB9rXzic0x+UfzhdGwRKKNIeSOsOMJRjT6F iLtN1y4MglxJQ5yqwmMHEl6i0ZwhoNVJKPavE2ot29RO1ZdojO6JptN9c5aHpZBzsYQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796039984; 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:MIME-Version: Content-Transfer-Encoding; bh=6QD26UWpxYKvW83erExD6c2P25w2Rr8/2aq+Ga3sZJY=; b=YSGI8yoAOyIYg7KaFk6RjUQWw4X/OZfr0GgJosLUHcr17MLqIlLKhXkHgfp+NYwTRYwn tUpSmvDeymWoNOjOOy4HEWL+fdATH4RLWW6hi8dHtHTRIZ+MC5wE2HvgLVm/XiIeL3cFC vruNvYp4FeB6z0QwVtc2IZtUifEjahWc2n9wS+jUQ1yZ+DHmmLn339BzytZ2Nd1Zq4t1k 5YhqJzQ8G16DhaIpYUwiuDi1AJa/gBFQT4m5L6LQGJ1tmD+T7gVwRaQREfqzF2MJ20872 VIIJqeh7+QKh9015aleLrTB6kF2MGP7+DbZusQiJL4vmP/reAGLqn32eD6xc9dVfLSA== 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=TMkrRW92; arc=none smtp.remote-ip=209.85.221.54 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 C1B2C606A31C for ; Mon, 30 Nov 2026 11:59:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C1B2C606A31C 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.a=rsa-sha256 header.s=20260707 header.b=TMkrRW92 Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-4a42b34e930so9180902f8f.2 for ; Mon, 30 Nov 2026 03:59:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796039982; x=1796644782; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=6QD26UWpxYKvW83erExD6c2P25w2Rr8/2aq+Ga3sZJY=; b=TMkrRW92lF/QhOFc4bCEt5RD1om5lf9Og/8Lh4Ixf9LVnUH3yh4I8uJIUUviGPB7SB hQegYDCSc2+be7OH6AxU5erZFIR+Vek+Ue4ceEd1iSL16kLW3lMqwV1r2xKMQ9QaxvPx 2k1vNQBYXCrM/b+XYOHFy5W1NBhHzKQTzHRHM8d4F+iVx5mb3Y6JcZgq6ZK60PSrB+Kb PEPaD0KCQQ37AveXMJT1dH1iimyFaEhKKUnZr74DX1ekuOymQ6Sod9oLoHURvasR4FNX XyRcvNjy8XUKuDp9t1jDoiHr7dwMmVbuQXY0jY9qX8OosFgmMmfsTNCd/0NwVzJ7IWyQ klKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796039982; x=1796644782; h=content-transfer-encoding:mime-version: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=6QD26UWpxYKvW83erExD6c2P25w2Rr8/2aq+Ga3sZJY=; b=Vd2u9dJ6UJt3BSIyJ4Efu9kiOLkZLgN2l1Kuota/8iQ1xG2I91pMRMbp5neq7RByHA FLqH9liwSlUyMCvRdtwrgH3A/D3gc9ksHTELguGTC2OGDIT1j7RLUgGcV1ljhpEYgkGV 6YKaUS4jBYHEEln4Uj4bUBWcedJsbhj9SgdXrSH3eZambQAxTYa03iRKte6ibjL3ho15 fH++7DNYVCkSuk1S8pyrXeuAddgyXTGi0YgZ52tKi01ZCEw2KOevMInH3eWBX/eHOkGw 4FE8Zd2vWgAZgyBoGUtggFYuaWXjgaHXhfFSpu9YZf1wpZgiXCu79f+tRBeWm0boxPcT aLgA== X-Forwarded-Encrypted: i=1; AL+hErFMOkyaX5NwplgWQIakrJ5rBWvU2NpxbGZDan000qRwP0HDMjMnwFl8hwaKyoTnFgTPkd8ZFSUV+5s1kKjDuw==@lists.linux.dev X-Gm-Message-State: APuU4Z85r+kpkbd2vu9jVC/9j3uM6dQOC5lztU8xrBgXDGIjte6Ino2K UblNl4mF8OaDaCbAY6JjjNrzF2JyIxO/By8lkJ6Snt5r25123sI9i0Af X-Gm-Gg: AZFdTZd95LGGWpfcJXWq19nEB3O7Zy5FbJY9ofcMg/bREQvrt9tOXCkOOaR7M8riLcS 2eZXRhXZ2VgNZzzXdVIuwx+4SgE2Byxyirio4om2O4ABx4YaMbbDfChcydSWEgjIuXeYxFg8uez m8f9VhyWW475Flyw03sjII5nG+4lBzhtUbR6fAqg2YoxWxngwGzc7dzT8i5C8IlZ49n0FkKUWIl nONH5CGjcS9nv3yTneF34JID6jNrndQO7179RCbJclwOt5eC18gsGBXw36gwHnnN/g+iPASSxeC KcwzGE40PmKCUoYYBaG7Po8fy0+fCzS4In8BT+7p/gcF9On9NMR2cdrUupI66Nf3+RCAN31++KU SrsPy/qITkIcCneEL958P9PjGJsNg6kPcm1bU24KQhEQ+wqSv2WDk6RYyJkXZ/OENgiI9Y2YWY4 6Tq6CCSYI+TvKW/30CZL3q+zNnmfCdl8+przvZtgaolftZPxkQLCfnW5M14xg9OVM1NxHnmYcUt 2yfmbR3YkH4lX+5OH9bxXXg7/fXxlt1Qs6h40ugcE14x9o1j4IK2QymTfLwr9mqe2IYR30V/1qq Cc/DFE7YMPRK6DFzd6cBljAl3Bk0kaUf X-Received: by 2002:a05:600c:1548:b0:4a1:e00b:a10b with SMTP id 5b1f17b1804b1-4bb88800020mr702388755e9.7.1796039981897; Mon, 30 Nov 2026 03:59:41 -0800 (PST) Received: from NULL.Home ([2001:8a0:7280:4000:12d:c3b9:cecc:8966]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884b8429sm1452982215e9.14.2026.11.30.03.59.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 03:59:40 -0800 (PST) From: Eric Curtin To: Anton Yakovlev , "Michael S. Tsirkin" Cc: Takashi Iwai , Jaroslav Kysela , 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 Message-ID: <20261130115939.434308-1-ericcurtin17@gmail.com> X-Mailer: git-send-email 2.56.1 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 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 --- 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