From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1792907835; b=tNcpNJTQvcnB8fGJjg5V0HlYCvZFIwzx/lwOvHkpbXyG7zU7nwBlYOaIlmtkzA8XO4sM J88e0ePeSRN1LvBuZhqUXL7V7oQvGwV0bNYuIxrhx3ie7kfUMlJP4R0rZb0kkNaIq1uBk XzNc7Cc+TpgB73+ptysntFLhHgNezh62NxLipvSTURiF/yZwCBGZ8g4vZjKGa05lDlVLX N4ZXQ6OQhuWjwqqR19Vpv6xje6JC+cYQjyomoO2IOICpF1iP5gfuqHgGoYtk36yjHSL5U iK+K3m31ll5sOQEwtCCiUiapQgzAPRzJFTZN3xUPqbQBI8QByJiscHSZQTqp8CwS01g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792907835; 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:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=AFeBztJdhmtRqO91ALcIai2AfR3YJBiPt6/MZndpKgI=; b=CtldVUlkXWq4wOMaTvmdTepe/lene/1JgHQ1a7JpuqCyXqwU84zqG0lULx64QPV2otXP 2YCHJqgLoFrLJRiJey9YZD8gFGNmTGL9h736/Q39MXHkNdGougup3rCzezgaG/ybjZZ6B C1z0oq+FmE4W5QxNdwyjmDPoxXK+opubi9ZtCfhPzi0dnw3RXZPlfjovETCV9jeat6Fra mfl086dvueJc0/pcLEvG+BHDZXPzRlN5YtchrMDmcsykunGsmU0uq8LD5Z5+FBqe0Pnbs I7l2WLXy2ghEcjxoJGGeuERh/VNkPmrTwOvxz/IdZ0tZYDrlhbD+DTOQKnyWJgQQmQw== 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=20251104 header.b=hDB7U47J; arc=none smtp.remote-ip=74.125.82.177 Received: from mail-dy1-f177.google.com (mail-dy1-f177.google.com [74.125.82.177]) (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 2AC81606A326 for ; Sun, 25 Oct 2026 05:57:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2AC81606A326 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, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=hDB7U47J Received: by mail-dy1-f177.google.com with SMTP id 5a478bee46e88-36681e85e87so1724576eec.1 for ; Sat, 24 Oct 2026 22:57:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1792907834; x=1793512634; 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=AFeBztJdhmtRqO91ALcIai2AfR3YJBiPt6/MZndpKgI=; b=hDB7U47JSo6b80AzwgbyO4mVahTKijI1ho2kb9GtQGzIvxPHWqnwWofDDvariHBXEg Nt3+LDsjh1uIj6yUgY2S0pEiffMBbPb55EvTAE8un6ueQkJarRX25GgoC+e6Z4S0q3ZH I4PqKq0aEI8LNNqo4bFuQQbNbZtOgB+uXe1ZRq7ta7dcazm9zEKehNY2oUMaA2uNpNv6 hjrWn6P8qfuA/W5k4/SUl9ci6+mKYGNnmJwfK4O35+KJDumbIiLYGkE4IrOWrMRN04mw fL6imc876IRardyk+qjN9pmbweCgn3EhOSUOvsYS+mz2ljB23TqjiL8cEi9bF1yMCEo1 zKlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792907834; x=1793512634; 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=AFeBztJdhmtRqO91ALcIai2AfR3YJBiPt6/MZndpKgI=; b=pE9V85vY6yM5Gc5HbE5ZOeSge5iTs7gDDZyDy+S32D6D9eekILV3ktbrUXrTbz0W/g ilxPoeggjpntcjDOlDU3p7zharBF9q1iRMgc/+PIZ7lJEZijdqeLiH1k10/f6B/yVB03 tECreYjZEJ/7GI/kHYv1nQMU1/LmMGQWyZmpOkuTSqxqm/TZcFwLLNGpe7XM8nXdanrG lck2BHMjlJEuACXf9RsLNULqwqTtCa5APlkFq6LOw9f6FH1ljQSHLw+HqaWpawFDFdDf WKzDKQmDs0aQvEGdq1HBkixOozpbA0YJs20tRSv2tm2QcLHcqYT1Jx3a8J46Zo7k34I6 lG5w== X-Forwarded-Encrypted: i=1; AKwUvBx9W1BU9dBkIa84XnKHFEBwyUUXDsZkePoMZHHGmGmNQoWP7arMPZCv9DpJVFa+TX8vRElHC2RaLVrbVyU=@vger.kernel.org X-Gm-Message-State: AFq9FYIj2G0VltCp+HKJFUvMqjV4jy8PFMgC3UXsHazQkktCcEd8E0h1 RINeKdexCM57qE33SXQb2CA6On+ZunSL6k5hk1skL8+NCsw9QnghLwwj X-Gm-Gg: AYBFou2RNzqJ4G3jWiIsJhvSiAlBXkBD0pBem6tNlNf6uy17X0kpa8gUCeIvalne3Vp mC93Y63sVlSZWB45vLbh+Si2HYg+ltbi0TEE2eBDBSfAwhz8X+7I3lgN5VAJczFjGB0YVu2JZ3w XRWsK+TCs4weK6UlPc82hlfQCJKmXytUYACjbrolEsiwN36Fgwyw4ETG1WING9PrUpREW+3o7sJ NMJRRKDnu7iEEX2LV4setRwufWR4L7VVGUDKrRtcGtVlvWbbxVdOCIzXTVkKFlDzZfD1lgkhJM4 0GerXZjT8vxh1sGB8zYIdqCWIb3KxZJRNnGVycx5BAXB+mkC/l0mGyCTuzUL1MePKgyi2RbNFwB AWr0wMyoOv8z2eYHLwqeA4akO6p5yr35jSmQA8Dx7iVGjppX1eTeHysJFYWBgLZ5htj0/3DZPRL 3TrHOuO9oR49CBUrVbaJ0n6q+HOGmYJTm5RWfrMTJLHo60pSCZrtbD86wtixJ7o5hOZP5OUVGps lyI8RI0+EMrC+TU3w73UW3WprVz4Uial2rCp12+LcnBlYy/dYUsTerK3AxjIHigqQqpNLXAcM29 hMzpczI7vJC+bwCjwIprvo+3rJXBpDyxfNazYDqgcURS5aO7zcCGE/cnWACWHsdndRlsL4EF X-Received: by 2002:a05:7301:4394:b0:353:8122:27b4 with SMTP id 5a478bee46e88-369e845c87emr5357893eec.18.1792907834154; Sat, 24 Oct 2026 22:57:14 -0700 (PDT) Received: from localhost.localdomain ([180.182.24.11]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-369e8d9307bsm16046244eec.14.2026.10.24.22.57.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 24 Oct 2026 22:57:13 -0700 (PDT) From: Joonho Lee To: linux-sound@vger.kernel.org Cc: perex@perex.cz, tiwai@suse.com, anton.yakovlev@opensynergy.com, mst@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Joonho Lee Subject: [PATCH v3 2/2] ALSA: virtio-snd: fix dsnoop capture with staged buffers Date: Sun, 25 Oct 2026 14:57:02 +0900 Message-ID: <20261025055702.69259-3-mynameisjune111@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261025055702.69259-1-mynameisjune111@gmail.com> References: <20261025055702.69259-1-mynameisjune111@gmail.com> X-Mailing-List: linux-kernel@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 dsnoop runs its hardware slave with stop_threshold == boundary and can consume the mmap capture ring without advancing the hardware application pointer. Staged capture currently treats every stream as a bounded consumer: capture_ready grows until the completion capacity check fails. With a 48 kHz mono slave, 960-frame periods and a 7680-frame buffer, the baseline dsnoop reproduction stops after 6720 frames with -EPIPE and a ring_capacity rejection. Direct capture continues to work. Cache the prepared boundary and recognize the free-running policy under the substream lock. Keep RX requests supplied in that mode, while retaining payload, frame alignment, counter and pointer-window checks. Sample the policy once per completion so validation, copying and refill agree. Refresh it separately in the ACK accounting and refill critical sections. On a return to bounded capture, reconstruct unread occupancy from the copied and acknowledged totals. If outstanding data fills the ring, the policy helper returns -EPIPE and preserves the cached mode and counters. The detecting RX, pointer or ACK path reports XRUN through its existing error path; this does not make sw_params fail synchronously. A native pause release can also detect this overflow during queue refill. Its trigger error leaves ALSA in PAUSED, so mark the staged capture as XRUN and schedule the period worker before returning the error. The worker takes the PCM stream lock after the pause action and wakes readers from XRUN. Reset cached policy state during prepare and teardown. In MOTU/VZ comparisons with one and two vCPUs, the baseline failed all 32 shared-client captures. The candidate passed 48 capture cases and eight recovery diagnostics, including 70-second CPU-saturated shared captures. Synthetic QEMU dsnoop data matched 2160000 reference frames across 12 files. Forced policy transitions passed KASAN/KCSAN checks. Aggressive KCSAN still triggered dsnoop client-threshold XRUNs with a 160 ms buffer. The physical tests checked frame counts and nonzero mono input; exact sample continuity was checked with the synthetic source. The native-pause regression reproduces -EPIPE followed by a persistent PAUSED state and a poll timeout with v2. With this revision, the same case reaches XRUN, wakes poll with POLLERR and returns -EPIPE from read. Prepare recovery and reopening also pass under ARM64 QEMU with KASAN and lockdep. Assisted-by: LLM Signed-off-by: Joonho Lee --- diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -47,7 +47,9 @@ * substream operators, including @stopped. * @buffer_bytes: Current buffer size in bytes. * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes). - * @capture_ready: Captured bytes copied but not yet acknowledged by ALSA. + * @capture_ready: Managed-mode bytes copied but not yet acknowledged by ALSA; + * always zero in free-running mode. + * @capture_boundary: ALSA boundary captured for this prepared epoch. * @capture_copied_total: Monotonic bytes copied into the capture ring. * @capture_acked_total: Monotonic bytes acknowledged by the capture client. * @capture_pointer_seen_total: Last capture position sampled by ALSA. @@ -58,6 +60,7 @@ * @capture_staging: Capture uses request-owned payload buffers. * @capture_started: Non-empty capture data was received since prepare. * @capture_empty: Consecutive empty startup completions. + * @capture_free_running: Cached staged-capture policy, protected by @lock. * @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 @@ -88,6 +91,7 @@ size_t buffer_bytes; size_t hw_ptr; size_t capture_ready; + snd_pcm_uframes_t capture_boundary; u64 capture_copied_total; u64 capture_acked_total; u64 capture_pointer_seen_total; @@ -97,6 +101,7 @@ bool runtime_active; bool capture_staging; bool capture_started; + bool capture_free_running; unsigned int capture_empty; bool xfer_enabled; bool xfer_xrun; @@ -128,23 +133,29 @@ /* * ALSA may observe a subperiod tail before its period callback. Keep actual - * unread occupancy separate from pointer observations and notification state. + * unread occupancy separate from pointer observations and notification state; + * free-running streams account occupancy from monotonic totals instead. */ static inline bool virtsnd_pcm_capture_counters_valid(const struct virtio_pcm_substream *vss) { - return vss->buffer_bytes && - vss->capture_copied_total >= vss->capture_acked_total && - vss->capture_copied_total - vss->capture_acked_total == - (u64)vss->capture_ready && - vss->capture_ready < vss->buffer_bytes && - vss->capture_pointer_seen_total <= vss->capture_copied_total && - vss->hw_ptr == vss->capture_copied_total % vss->buffer_bytes && - (!vss->capture_callback_snapshot_active || - (vss->capture_callback_snapshot >= - vss->capture_pointer_seen_total && - vss->capture_callback_snapshot <= - vss->capture_copied_total)); + u64 outstanding; + + if (!vss->buffer_bytes || + vss->capture_copied_total < vss->capture_acked_total || + vss->capture_pointer_seen_total > vss->capture_copied_total || + vss->hw_ptr != vss->capture_copied_total % vss->buffer_bytes || + (vss->capture_callback_snapshot_active && + (vss->capture_callback_snapshot < vss->capture_pointer_seen_total || + vss->capture_callback_snapshot > vss->capture_copied_total))) + return false; + + if (vss->capture_free_running) + return !vss->capture_ready; + + outstanding = vss->capture_copied_total - vss->capture_acked_total; + return outstanding == (u64)vss->capture_ready && + vss->capture_ready < vss->buffer_bytes; } /** @@ -207,6 +218,9 @@ int virtsnd_pcm_capture_send(struct virtio_pcm_substream *vss); +int virtsnd_pcm_capture_refresh_mode_locked(struct virtio_pcm_substream *vss, + bool *free_running); + void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss); void virtsnd_pcm_msg_reset(struct virtio_pcm_substream *vss); diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c --- a/sound/virtio/virtio_pcm_msg.c +++ b/sound/virtio/virtio_pcm_msg.c @@ -3,6 +3,7 @@ * virtio-snd: Virtio sound device * Copyright (C) 2021 OpenSynergy GmbH */ +#include #include #include #include @@ -384,6 +385,8 @@ vss->capture_notified_periods = 0; vss->capture_callback_snapshot = 0; vss->capture_callback_snapshot_active = false; + vss->capture_boundary = 0; + vss->capture_free_running = false; vss->capture_empty = 0; vss->capture_started = false; } @@ -508,24 +511,54 @@ return 0; } -/** - * virtsnd_pcm_capture_send() - Queue staged capture requests. - * @vss: VirtIO PCM substream. - * - * Queue full-period requests while the outstanding requests fit the remaining - * ALSA ring capacity. The final request may exceed that capacity by less than - * one period; completion validates the returned payload before copying it. - * - * Context: Expects the RX queue and substream spinlocks to be held by caller. - * Return: 0 on success, -errno on failure. - */ -int virtsnd_pcm_capture_send(struct virtio_pcm_substream *vss) +/* Caller holds vss->lock. */ +int virtsnd_pcm_capture_refresh_mode_locked(struct virtio_pcm_substream *vss, + bool *free_running) +{ + struct snd_pcm_runtime *runtime; + snd_pcm_uframes_t stop_threshold; + u64 outstanding; + bool new_free_running; + + if (!vss->capture_staging || !vss->runtime_active || !vss->substream) + return -EBADFD; + + runtime = vss->substream->runtime; + if (!runtime || !vss->buffer_bytes || !vss->capture_boundary) + return -EBADFD; + + /* Validate the currently committed policy before observing a transition. */ + if (!virtsnd_pcm_capture_counters_valid(vss)) + return -EIO; + + stop_threshold = READ_ONCE(runtime->stop_threshold); + new_free_running = stop_threshold == vss->capture_boundary; + if (new_free_running != vss->capture_free_running) { + outstanding = vss->capture_copied_total - + vss->capture_acked_total; + if (new_free_running) { + vss->capture_ready = 0; + } else { + if (outstanding >= vss->buffer_bytes) + return -EPIPE; + vss->capture_ready = outstanding; + } + vss->capture_free_running = new_free_running; + } + + *free_running = new_free_running; + return 0; +} + +/* Caller holds the RX queue and vss locks and has sampled the policy. */ +static int virtsnd_pcm_capture_enqueue(struct virtio_pcm_substream *vss, + bool free_running) { struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss); struct virtqueue *vqueue = queue->vqueue; unsigned long period_bytes; size_t free_bytes; - size_t request_limit; + size_t request_limit = vss->nmsgs; unsigned int i; bool added = false; @@ -535,16 +568,17 @@ period_bytes = snd_pcm_lib_period_bytes(vss->substream); if (!period_bytes || !vss->buffer_bytes || period_bytes >= vss->buffer_bytes || + free_running != vss->capture_free_running || !virtsnd_pcm_capture_counters_valid(vss) || vss->msg_count > vss->nmsgs) return -EIO; - free_bytes = vss->buffer_bytes - vss->capture_ready; - request_limit = free_bytes / period_bytes + - !!(free_bytes % period_bytes); - request_limit = min_t(size_t, request_limit, vss->nmsgs); - if (vss->msg_count > request_limit) - return -EIO; + if (!free_running) { + free_bytes = vss->buffer_bytes - vss->capture_ready; + request_limit = free_bytes / period_bytes + + !!(free_bytes % period_bytes); + request_limit = min_t(size_t, request_limit, vss->nmsgs); + } for (i = 0; i < vss->nmsgs && vss->msg_count < request_limit; ++i) { struct virtio_pcm_msg *msg = vss->msgs[i]; @@ -585,6 +619,25 @@ return 0; } +/** + * virtsnd_pcm_capture_send() - Refresh policy and queue staged requests. + * @vss: VirtIO PCM substream. + * + * Context: Expects the RX queue and substream spinlocks to be held by caller. + * Return: 0 on success, -errno on failure. + */ +int virtsnd_pcm_capture_send(struct virtio_pcm_substream *vss) +{ + bool free_running; + int rc; + + rc = virtsnd_pcm_capture_refresh_mode_locked(vss, &free_running); + if (rc) + return rc; + + return virtsnd_pcm_capture_enqueue(vss, free_running); +} + static int virtsnd_pcm_capture_status(const struct virtio_pcm_msg *msg, size_t written_bytes, size_t frame_bytes, @@ -638,6 +691,7 @@ size_t audio_bytes = 0; size_t next_ready = 0; u64 next_copied_total = 0; + bool free_running = false; bool got_audio = false; guard(spinlock)(&vss->lock); @@ -651,6 +705,9 @@ /* A full ring is modulo-ambiguous, so fail closed before copying. */ if (!runtime || !runtime->dma_area) invalid_reason = "runtime"; + else if (virtsnd_pcm_capture_refresh_mode_locked(vss, + &free_running)) + invalid_reason = "policy"; else if (!virtsnd_pcm_capture_counters_valid(vss)) invalid_reason = "counters"; else if (vss->hw_ptr != @@ -660,18 +717,28 @@ frame_bytes, &audio_bytes, &latency_bytes)) invalid_reason = "status"; - else if (audio_bytes >= - vss->buffer_bytes - vss->capture_ready) - invalid_reason = "ring_capacity"; - else if (check_add_overflow(vss->capture_ready, audio_bytes, - &next_ready)) - invalid_reason = "ready_overflow"; - else if (next_ready >= vss->buffer_bytes) - invalid_reason = "ring_full"; else if (check_add_overflow(vss->capture_copied_total, (u64)audio_bytes, &next_copied_total)) invalid_reason = "total_overflow"; + else if (free_running && audio_bytes >= vss->buffer_bytes) + invalid_reason = "payload_exceeds_ring"; + else if (!free_running && + audio_bytes >= vss->buffer_bytes - vss->capture_ready) + invalid_reason = "ring_capacity"; + else if (!free_running && + check_add_overflow(vss->capture_ready, audio_bytes, + &next_ready)) + invalid_reason = "ready_overflow"; + else if (!free_running && next_ready >= vss->buffer_bytes) + invalid_reason = "ring_full"; + else if (free_running && + (next_copied_total < vss->capture_pointer_seen_total || + next_copied_total - vss->capture_pointer_seen_total >= + vss->buffer_bytes)) + invalid_reason = "pointer_window"; + else if (free_running) + next_ready = 0; if (invalid_reason) { virtsnd_pcm_capture_xrun_locked(vss); @@ -713,7 +780,8 @@ msg->length = 0; if (vss->runtime_active && vss->xfer_enabled) { - if (!vss->xfer_xrun && virtsnd_pcm_capture_send(vss)) { + if (!vss->xfer_xrun && + virtsnd_pcm_capture_enqueue(vss, free_running)) { dev_warn_ratelimited(&vss->snd->vdev->dev, "SID %u: failed to refill capture queue\n", vss->sid); diff --git a/sound/virtio/virtio_pcm_ops.c b/sound/virtio/virtio_pcm_ops.c --- a/sound/virtio/virtio_pcm_ops.c +++ b/sound/virtio/virtio_pcm_ops.c @@ -543,6 +543,8 @@ vss->capture_notified_periods = 0; vss->capture_callback_snapshot = 0; vss->capture_callback_snapshot_active = false; + vss->capture_boundary = 0; + vss->capture_free_running = false; } } @@ -565,6 +567,7 @@ struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); struct virtio_device *vdev = vss->snd->vdev; bool pause_restart = virtsnd_pcm_pause_restart_enabled(vss); + bool free_running; int rc; bool stopped; @@ -622,6 +625,15 @@ vss->suspended = false; vss->msg_count = 0; virtsnd_pcm_msg_reset(vss); + if (vss->capture_staging) { + scoped_guard(spinlock_irqsave, &vss->lock) { + vss->capture_boundary = substream->runtime->boundary; + rc = virtsnd_pcm_capture_refresh_mode_locked(vss, + &free_running); + } + if (rc) + return rc; + } memset(&vss->pcm_indirect, 0, sizeof(vss->pcm_indirect)); vss->pcm_indirect.sw_buffer_size = @@ -893,6 +905,13 @@ if (rc) { vss->stopped = true; substream->runtime->stop_operating = true; + if (vss->capture_staging && + vss->direction == SNDRV_PCM_STREAM_CAPTURE && + command == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) { + /* Pause undo leaves ALSA paused; wake its readers. */ + vss->xfer_xrun = true; + virtsnd_pcm_schedule_period_elapsed(vss); + } return rc; } vss->xfer_enabled = true; @@ -1132,12 +1151,18 @@ struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); size_t hw_ptr = 0; u64 total; + bool free_running; bool xrun; if (vss->capture_staging) { scoped_guard(spinlock_irqsave, &vss->lock) { - xrun = vss->xfer_xrun || - !virtsnd_pcm_capture_counters_valid(vss); + xrun = vss->xfer_xrun; + if (!xrun && + virtsnd_pcm_capture_refresh_mode_locked(vss, + &free_running)) + xrun = true; + if (!xrun && !virtsnd_pcm_capture_counters_valid(vss)) + xrun = true; if (!xrun) { total = vss->capture_callback_snapshot_active ? vss->capture_callback_snapshot : @@ -1244,14 +1269,18 @@ snd_pcm_uframes_t appl_ptr; snd_pcm_sframes_t diff; size_t bytes; + u64 outstanding; u64 next_acked_total; + bool free_running; if (!vss->runtime_active || !runtime || !runtime->control || !runtime->dma_area) return -EBADFD; if (vss->xfer_xrun) return -EPIPE; - if (!virtsnd_pcm_capture_counters_valid(vss)) { + rc = virtsnd_pcm_capture_refresh_mode_locked(vss, + &free_running); + if (rc || !virtsnd_pcm_capture_counters_valid(vss)) { vss->xfer_xrun = true; virtsnd_pcm_schedule_period_elapsed(vss); return -EPIPE; @@ -1270,7 +1299,11 @@ /* ACK includes any subperiod tail already copied to the ring. */ bytes = frames_to_bytes(runtime, diff); - if (bytes > vss->capture_ready || + outstanding = vss->capture_copied_total - + vss->capture_acked_total; + if ((!free_running && bytes > vss->capture_ready) || + (free_running && + (bytes > vss->buffer_bytes || bytes > outstanding)) || check_add_overflow(vss->capture_acked_total, (u64)bytes, &next_acked_total) || next_acked_total > vss->capture_copied_total) { @@ -1280,7 +1313,8 @@ } vss->capture_acked_total = next_acked_total; - vss->capture_ready -= bytes; + if (!free_running) + vss->capture_ready -= bytes; vss->pcm_indirect.appl_ptr = appl_ptr; }