From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796039984; b=3zad/CXD9E24l5yQWjxsOFlY+LXgI+eSRsaL60I02UYoLimqCcrXskqbwumyOzZcYcLB lpXVeHCAJpFjPV2hMhU/Qjan+aeCrhgnGTV5m8rLmECe/phaJ2xtaLPi8SAEqjKPe7Kg+ EEtt3CHRrDiOs1KeerBo69/eJjRTK3CKD6mNpOUXbV6AeMCiJdmJYcuLExw6Y77DhVdas nFrRTC1vndxQyOtdjFK79qyfKAEoeQCXGPqgn/CcvJ0W5/zTdROmh2JnECuyJUFdhI+yz g37ckpDF/gHlcETb4YMHetu+b/TeGOrjL9H034YHbWfMT9QJy7V9MSFhiGah0Sz525w== 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=X6eyPWUlnRgjWSPGj8WtyF/F03OavhB/Zu7F/tN+ygNR9t1TklrkQuSkUEKnKMSPelq8 PMZIJ7PT3nvha9VeUxfef+2GzO1rkr7uO5ve0B0io/JMFLWFieeBl+lWICl6qFDrmTa5G 38Qq2fOOQsGp5s6K7QoSnFxZpGnPKWKqve1nE5vCZRFuW4prb08hNr9TBkvcWYOzz38kj 8GLEFo/DtnFUiW/Xef5AptpF0FiyFz+PxsS/SOsoIB1zIV9rxcb4BE9YRIbHgE/ZW2fKA eYzohOd23Q4Fzqt7lPtRT2aT4KQWx9bRhgIJdnefVltPX0uHJ4jrE1HPo9ljcI0V+1A== 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=wU0GkAxe; arc=none smtp.remote-ip=209.85.128.51 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 E33FD606A320 for ; Mon, 30 Nov 2026 11:59:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E33FD606A320 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=wU0GkAxe Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4bba5e17cbeso46505815e9.1 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=vger.kernel.org; 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=wU0GkAxehRZf/9c1jdUBxxdHCtCCgvp3K4axVTDu8ENATY36iNtI9XyegZwl60ljNE 8yHqU0LOKinJzcHXvYdnRd5kiRAmC5fb3/Sitetsq1zmmQZV90jp7rj1TMxBn2vY7r1d XhiLNwOBA3rv8/kSsZptHyriAb+DFSo1La6nOezRKt5TghBPu8aDA5ydPIXZvb6EH07k 4f4vSfYUZHR6QZexnjtCEFbjN0ArhHhcPTMAb5ricc0wxFSmvoD6ZanW+oZ1oY7Q+51z Iv6NtzMfld6DbD/C8rqA2VMVLB2hEo0rUvv4dtKMaUteWQ+8TlQZCxjlbxkJj6YHngoq YNqQ== 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=qPb76V4s+sDqzXIoM4nLdt167JzWIXcbU/TzI3P94/dPqp9MdCLN50+AsE/GDkcsoT 8ed7510B1QLqvrycCFJyMn+e2efaJqb6JfuvzJuc3tJniMQKgig4zc5Gek0d1MoYvPqG WOlqs8HgvYxH1LTNQO+OYrt5PzA+rp1F0FdhclFP+viDWtV7+eh0yMtlpqpCvhlKPwdy ZYDvkwtLD7HgcUHD7K50sAKhMnOadjsO2lMYuEPdIXolLt3v4lTvcy+aOupLF+VL8Y9K fzRMFx22MgTn7x6arHkLM/Fbnut42CoJ59gal9zRcbsV2mzXSO6PTXGLRs+e5vDAkhlu 0mzQ== X-Forwarded-Encrypted: i=1; AL+hErHK6mlocToZtL3XqoYqRx5T1AnbTDRdu9lp8Y1WTsu91E8k9UAwz7sgJ3EcsQPcZ12vkrwb8kceIcovrA==@vger.kernel.org X-Gm-Message-State: APuU4Z8+XGDbLb67oIPrfj5VHhYQ79a+M41beD5m/AChNppNRxW8MZHo TXtaEUjolREZlZBFFiHueLiU22Vv38hKrhRp1k6CzdBonWibdicj5lYPKKN14zN+IhTh6+z3 X-Gm-Gg: AZFdTZfiGN07CKdEaRktLLEu12uvOPuoQ5CL6z9tVwyIAz0IgIH9tSA3nM81oZDljii vh7oZfLdTenxluPTSNphmBgRb8iUIX7fsZpKinYgMoLg83A5iIFpI6L1gem9C9Luxt1k96Mppsg hwY7mAM6pHKEKGB5klbAJRQ1dgYAT13h0wP/W3YZPxpKPfQXT2dRZ4cTm+Hv6t+aSEGZzoc2Qs0 yuZOCgDQ3gAT5LsezcUW7YnlYcLpj+f97si/w0wclCedqgFjD4l1vpVWjAHxXlkdX88PGd0AjOJ +oixyvLBs2QpLSWC6ds/60f0kls2RyzhDK8zUCDk9PTtGF/HTh6ouTES2ariEihVnyZtHI0SfSR Avsf5FsbrPIB8QwZIOxiO941yuWLbfsCKh34hHynzoaU5IE26WxzFASWEBJCcR09/yiZpNWpxNL Sw0lX16Qopky7DLWpZWorkR1Br1KHKP/Q6Ghb9pB4gp3mM9UxnzWKfKEKTbdUnPf94Wgioc83ye d6CgDPb/wGXC25ynGHV+U5AhYNJ3GxnDfUb08Uv+xsF4QAWhfKX7kdGwuSpQiyyamSHpazvrx65 uSrHv/R7KpILjQ+EcaLuAcgyhOork3Hu 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: linux-sound@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 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