ALSA: virtio: stage capture for VZ backends
Subsystems (3)
cover ALSA: virtio: stage capture for VZ backends
Base: d58a3ac9b1719fdf8ce9d286be606128aeebf1aa
("ALSA: virtio: Keep control payloads alive through DMA completion") Local tip of the five submitted v3 patches; not an upstream commit.
Previous series: <[email protected]>
This is a follow-up RFC, not a replacement or v4 of that five-patch series. The submitted v3 fixes ACK error propagation, failed START recovery, and message/buffer lifetime through completion and reset. It does not add the VZ capture profile or the ALSA core drain handling in these companion RFCs.
On the tested Apple Virtualization.framework capture path, short RX completions and the observed response layout exposed problems with the existing ring accounting. We also observed capture failing to resume queued RX after pause release. These observations motivate a compatibility path; they do not establish that every VZ version behaves this way or settle whether a backend response violates the virtio-sound specification.
This RFC adds request-owned capture buffers. It validates returned length, status, frame alignment and ring capacity before copying audio into ALSA, and accounts copied, consumed and notified data separately. On the normal MMU path this replaces direct DMA into the ALSA ring when staging is enabled, at the cost of extra memory and copying. v3 already has a separate NOMMU bounce-buffer path; staging is not the first owned-buffer path in the driver.
The pause profile uses STOP/RELEASE and re-preparation for subsequent START, while preserving audio already captured in the ALSA ring. It does not promise continuous capture during the paused interval. An ambiguous control outcome fails closed and may require device reprobe rather than an automatic retry.
All five module parameters default off: capture_staging, capture_packed_status, capture_allow_short, capture_allow_empty and capture_pause_restart. These are explicit profile controls, not automatic VZ detection or newly negotiated virtio features. The companion generic wait and ALSA core changes still apply when this profile is disabled.
Dependencies and scope
Apply after v3 and these three proposed prerequisites:
- ALSA: virtio: wait through signals for PCM teardown
- ALSA: pcm: allow capture reads while draining
- ALSA: pcm: refresh capture pointers before drain
Companion RFC cover Message-IDs reserved for this submission:
- <[email protected]>
- <[email protected]> These are pending companion threads, not already published messages.
This combined PCM patch shares ownership, counters and lifecycle state across staging and pause/restart. Its size is a review concern. A smaller split needs independently buildable and validated intermediate states. The patch changes guest behavior; it does not add a microphone device to stock Docker Desktop or implement host audio routing in its other VMMs.
Validation and limits
The complete local candidate (v3 plus all four RFC patches) was freshly built on 2026-10-14 as an ARM64 KASAN Image and installed module set with W=1, release 7.3.0-rc6-snd-lifetime-kasan. No old kernel objects were reused. There were no build errors or warnings in the changed sound paths; 517 -Woverride-init warnings remain in two unchanged ARM64 base files. This is a rebuild of the pinned candidate, not a rebase to a newer upstream or sound maintainer tree.
All ten ordinary-TCG scenarios passed with the same fresh kernel/module
set: three short-tail drain/drop cases, four delayed-control cases, one pending-RX case, and two direct STOP/RELEASE reset-cancellation cases. Ambiguous STOP/RELEASE/PREPARE/START timeouts leave the reader at XRUN with zero frames; prepare is rejected before reprobe, then capture recovers.
In the pending-RX case, STOP and RELEASE are acknowledged; waiting for the held RX times out after 1017 ms. It completes once after 5.08 guest seconds, then capture returns 1024 frames without another RELEASE or reset. Running this case from the relocated final archive also passes (1016 ms / 4.99s). The direct cases cancel held STOP/RELEASE responses by device unbind/rebind after SIGKILL and timeout, wait beyond the old response deadline, then pass playback/capture/replay. This does not cover every reset interleaving or actual event-virtqueue XRUN delivery.
The QEMU 11.1.2 fixture includes backend reset cleanup changes as well as fault injection. These are integrated candidate results, not standalone v3 or isolated-patch proof, and not stock-QEMU, VZ or hardware acceptance. Earlier runs failed follow-on capture with ring saturation and overruns; those logs remain retained. Final passes do not establish a permanent fix for load or clock/scheduling sensitivity.
Historical microphone evidence (not rerun with this new KASAN build):
Our LinuxKit VZ launcher passed AirPods Compose capture at 6s/600s/6s. The 600s run returned 28,800,000 frames in 600.088s with recorder status 0 and no non-finite samples. After recorder SIGKILL, two captures passed; after VM stop and fresh boot, 6s/70s/6s passed. SIP stayed enabled, the host audio service was not restarted, and no microphone PCM was saved. That earlier Compose virtio_snd.ko SHA-256 was 245db509cb64b596e608d9ed5d86fb30235033d08b4bfcc495026252087e4476. It is a different binary/configuration from the fresh KASAN runtime set. The historical guest was 7.3.0-rc6-linuxkit-vz-compose-r3-20261014, with two vCPUs and --mem 4096. The packaging host reported macOS 26.7 (25G229), but that build was not recorded in the original run log. The exact historical launcher binary is not pinned by its public PR link. See vz/README.txt in the bundle for known inputs and these reproduction limits.
The historical floating-point peak was 1.73. Liveness and finite samples do not establish acoustic calibration or absence of clipping. Built-in input, input switching, macOS sleep/wake, guest freeze/restore, physical non-coherent DMA/NOMMU systems and other Docker Desktop VMMs have not been validated on this candidate. This remains an experimental RFC.
Reproducer
A source-only bundle contains the pinned base/v3/RFC preparation recipe, kernel builder, QEMU patch stack, guest probes and all ten runtime commands. See qemu/README.txt and qemu/VALIDATION-RESULTS.txt in that bundle. The final runtime set passed under ordinary TCG; no KASAN/BUG/WARNING/oops/panic was matched. The 24 Python harness tests and relocated source comparison also passed. Existing v3 patches are included only as build prerequisites, not for resubmission. Reproducer source (pinned commit):
https://github.com/whatisyourname0/virtio-snd-reproducer/tree/7a9876738bdc70ce669f02ec31fe8968eb0ee4fc/rfc/vz-capture-20261014
The source bundle includes qemu/README.txt with the full build/run recipe, qemu/VALIDATION-RESULTS.txt and checksums. Downloaded public files were verified against the tested source package. Bundled RFC mail drafts are reproduction snapshots; these emails are the submission copies.
Questions for review
- Which observed response and pause behaviors should be fixed in the host backend instead of accepted by a guest compatibility path? What protocol traces or conformance cases would help decide this?
- Are module parameters appropriate for this profile, or should activation use an identifiable device quirk or a negotiated capability? What stable identification would be acceptable rather than a blanket VZ assumption?
- Is release/reprepare an acceptable implementation of capture pause, with buffered-tail preservation but no continuity during the pause interval?
- Is request-owned staging acceptable for this scope, and how should the patch be split so ownership and lifecycle invariants remain reviewable?
- Which PM, non-VZ backend and capture-format regressions should gate a non-RFC submission?
Joonho Lee (1):
ALSA: virtio: stage VZ capture and restart paused streams
sound/virtio/virtio_pcm.c | 93 ++++- sound/virtio/virtio_pcm.h | 68 ++++ sound/virtio/virtio_pcm_msg.c | 286 ++++++++++++- sound/virtio/virtio_pcm_ops.c | 734 +++++++++++++++++++++++++++++++++- 4 files changed, 1151 insertions(+), 30 deletions(-)
RFC PATCH 1/1 ALSA: virtio: stage VZ capture and restart paused streams
Add opt-in capture staging with response validation and accounting. Add a pause release/reprepare state machine for the VZ profile.
--- sound/virtio/virtio_pcm.c | 93 ++++- sound/virtio/virtio_pcm.h | 68 ++++ sound/virtio/virtio_pcm_msg.c | 286 ++++++++++++- sound/virtio/virtio_pcm_ops.c | 734 +++++++++++++++++++++++++++++++++- 4 files changed, 1151 insertions(+), 30 deletions(-)
sound/virtio/virtio_pcm.c +91 -2
@@ -310,8 +310,94 @@ static void virtsnd_pcm_period_elapsed(struct work_struct *work) {struct virtio_pcm_substream *vss = container_of(work, struct virtio_pcm_substream, elapsed_period); +struct snd_pcm_substream *substream = vss->substream; +struct snd_pcm_runtime *runtime; +size_t period_bytes; +u64 period_floor; +u64 snapshot; +bool staged_capture; +bool paused_xrun = false; +bool notify = false; +bool requeue = false; + + scoped_guard(spinlock_irqsave, &vss->lock) { + staged_capture = vss->direction ==SNDRV_PCM_STREAM_CAPTURE && + vss->capture_staging; + } + +if (!staged_capture) { +snd_pcm_period_elapsed (substream); +return ; + } + +/* Serialize the pointer snapshot with ALSA's pointer and ACK callbacks. */ +guard (pcm_stream_lock_irqsave)(substream); +if (!snd_pcm_running (substream)) { + scoped_guard(spinlock_irqsave, &vss->lock) { + runtime = substream->runtime; + paused_xrun = runtime && + runtime->state ==SNDRV_PCM_STATE_PAUSED && + vss->runtime_active && vss->xfer_xrun; + } +if (paused_xrun) { +/* Wake readers without sending STOP for a paused stream. */ +snd_pcm_stop (substream,SNDRV_PCM_STATE_XRUN ); + } +return ; + } + + scoped_guard(spinlock_irqsave, &vss->lock) { + runtime = substream->runtime; +if (!vss->runtime_active || + (!vss->xfer_enabled && !vss->xfer_xrun) || !runtime) +return ; + + period_bytes =snd_pcm_lib_period_bytes (substream); +if (vss->xfer_xrun) { + notify = true; + }else if (!period_bytes || period_bytes >= vss->buffer_bytes || + !virtsnd_pcm_capture_counters_valid (vss) || + vss->capture_notified_periods > + vss->capture_copied_total / period_bytes) { + vss->xfer_xrun = true; + notify = true; + }else { + period_floor = vss->capture_copied_total / period_bytes; +if (period_floor > vss->capture_notified_periods) { +/* Reserve crossed boundaries before the callback. */ + vss->capture_notified_periods = period_floor; + notify = true; + } + } + +if (notify) { + snapshot = vss->capture_copied_total; + vss->capture_callback_snapshot = snapshot; + vss->capture_callback_snapshot_active = true; + } + } + +if (!notify) +return ; + +/* Pin the callback position while completions keep updating actual totals. */ +snd_pcm_period_elapsed_under_stream_lock (substream); + + scoped_guard(spinlock_irqsave, &vss->lock) { + vss->capture_callback_snapshot_active = false; + vss->capture_callback_snapshot =0 ; +if (vss->runtime_active && vss->xfer_enabled && + !vss->xfer_xrun && substream->runtime) { + period_bytes =snd_pcm_lib_period_bytes (substream); +if (period_bytes && + vss->capture_copied_total / period_bytes > + vss->capture_notified_periods) + requeue = true; + } + } -snd_pcm_period_elapsed (vss->substream); +if (requeue) +schedule_work (&vss->elapsed_period); }/**
@@ -517,8 +603,11 @@ void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event)break ;case VIRTIO_SND_EVT_PCM_XRUN : scoped_guard(spinlock, &vss->lock) { -if (vss->xfer_enabled) +if (vss->xfer_enabled) { vss->xfer_xrun = true; +if (vss->capture_staging) +schedule_work (&vss->elapsed_period); + } }break ; }
sound/virtio/virtio_pcm.h +68 -0
114 lines not on this page · raw message
sound/virtio/virtio_pcm_msg.c +274 -12
365 lines not on this page · raw message
sound/virtio/virtio_pcm_ops.c +718 -16
875 lines not on this page · raw message
v2 cover ALSA: virtio: stage capture for VZ backends
In reply to Joonho LeeHi,
This updates the VZ capture RFC with an explicit public base and its prerequisite series. The implementation is unchanged from v1. The core prerequisite is now the capture-drain RFC v3, including the notify=false pointer update discussed with Takashi.
The patch adds request-owned capture staging, validates completion sizes, and accounts for captured data independently of fixed-size requests. It also adds an opt-in pause/restart path using RELEASE and reprepare.
The five module parameters default to off: capture_staging, capture_packed_status, capture_allow_short, capture_allow_empty, and capture_pause_restart. Enabling the profile is still an explicit choice. Automatic VZ detection, a negotiated feature and Docker Desktop host-audio routing are outside this patch.
Please apply the following on base 8d70e364e866, in this order:
1. The five virtio-snd lifetime/error-recovery v3 patches:
https://lore.kernel.org/all/[email protected]/
2. The generic teardown wait RFC v2:
https://lore.kernel.org/all/[email protected]/
3. Both core capture-drain RFC v3 patches:
https://lore.kernel.org/all/[email protected]/
4. This VZ patch.
The trailers below identify all eight prerequisites in application order. The three prerequisite-message-id hints identify the public Lore threads containing them. I fetched those threads, checked every stable patch ID, and applied the stack from the declared base. The resulting nine changed source files match the kernel and modules used for the tests below. Use core v3 in place of core v2; the two revisions must not be stacked.
Core v3 refreshes capture pointers inside the drain action and suppresses nested stop actions during that update. Its proposed error policy ends a failing capture member in SETUP while allowing a healthy linked member to retain its tail in DRAINING. A blocking capture-only drain returns 0 if the action otherwise succeeds. Whether it should instead report EPIPE after stopping all members remains a question in the core discussion:
https://lore.kernel.org/all/[email protected]/
This generic core behavior also applies with the VZ parameters disabled.
Validation with the complete stack above:
- Rebuilt the ARM64 Image and modules with W=1, KASAN, lockdep and atomic-sleep debugging. There were no build errors or sound warnings; the base emitted 517 -Woverride-init warnings outside sound/.
- Passed 10 QEMU scenarios: short capture tails, drop, delayed PREPARE/START/STOP/RELEASE, pending RX after RELEASE, and held STOP and RELEASE requests followed by SIGKILL, timeout, reset and I/O recovery.
- Passed 24 synthetic linked capture cases across atomic and nonatomic locking, covering pointer faults, tail reads and reopen/recovery. Two additional unlinked controls also passed.
- Candidate runs produced no KASAN or lockdep diagnostics.
The QEMU tests use modified fault-injection and backend-reset fixtures. The rebuilt stack has not been tested with a physical microphone or VZ. Earlier AirPods results used a different kernel/module set. Input switching, sleep/wake, freeze/restore, physical non-coherent DMA, NOMMU, other Docker VMMs and mixed capture/playback linked groups still need coverage. Nonblocking drain behavior has only been code-reviewed.
The existing public test package is at:
https://github.com/whatisyourname0/virtio-snd-reproducer/tree/64fa85f3260b45e7dbb85c4eee68a5b4a8b064e1/rfc/vz-capture-20261014/sashiko-followup
That snapshot contains the earlier core v2 source. The prerequisite mails above provide the exact updated stack. The new linked capture fixture and comparison logs are still local.
I would appreciate feedback on these points before splitting this up:
- Which observed behaviors should be fixed in Apple's backend?
- What activation mechanism would be acceptable for the compatibility profile? The module parameters are experimental.
- Is RELEASE/reprepare an acceptable way to implement pause for this backend, and what constraints should it have?
- How should capture staging and lifecycle handling be split for review?
Thanks,
Joonho
Changes in v2:
- Pin the public base, list the eight prerequisite patch IDs, and add Message-ID hints for the three public prerequisite series.
- Use core capture-drain v3 and report tests on that integrated stack.
- Preserve the VZ implementation and its existing human signoff.
base-commit: 8d70e364e866bdfca81eee24d0688fac9472cfb9
prerequisite-patch-id: 39fb327a004c966ab90f66a34a64bbdbb3ffb475
prerequisite-patch-id: 7c6bc344656fd839ae0d697ba18d66ac0b47827a
prerequisite-patch-id: 9dd290ac3147fa7b49a475d7df8a6172778f2860
prerequisite-patch-id: f4c24a5e441343a690f4ac2437f181c815bc23d0
prerequisite-patch-id: ab4a5b770247a65272ab7af18a75a147daf81287
prerequisite-patch-id: cfe3e7000e695a2fda27993f4d9acf0a637cf24e
prerequisite-patch-id: 0fc5fec2c6ac5d3043cf2210570472ed4ce32952
prerequisite-patch-id: fa5c13a4c698a82bdce335a34c22e66bf72960d8
prerequisite-message-id: [email protected]
prerequisite-message-id: [email protected]
prerequisite-message-id: [email protected]
RFC PATCH v2 1/1 ALSA: virtio: stage VZ capture and restart paused streams
Add opt-in capture staging with response validation and accounting. Add a pause release/reprepare state machine for the VZ profile.
--- sound/virtio/virtio_pcm.c | 93 ++++- sound/virtio/virtio_pcm.h | 68 ++++ sound/virtio/virtio_pcm_msg.c | 286 ++++++++++++- sound/virtio/virtio_pcm_ops.c | 734 +++++++++++++++++++++++++++++++++- 4 files changed, 1151 insertions(+), 30 deletions(-)
sound/virtio/virtio_pcm.c +91 -2
@@ -310,8 +310,94 @@ static void virtsnd_pcm_period_elapsed(struct work_struct *work) {struct virtio_pcm_substream *vss = container_of(work, struct virtio_pcm_substream, elapsed_period); +struct snd_pcm_substream *substream = vss->substream; +struct snd_pcm_runtime *runtime; +size_t period_bytes; +u64 period_floor; +u64 snapshot; +bool staged_capture; +bool paused_xrun = false; +bool notify = false; +bool requeue = false; + + scoped_guard(spinlock_irqsave, &vss->lock) { + staged_capture = vss->direction ==SNDRV_PCM_STREAM_CAPTURE && + vss->capture_staging; + } + +if (!staged_capture) { +snd_pcm_period_elapsed (substream); +return ; + } + +/* Serialize the pointer snapshot with ALSA's pointer and ACK callbacks. */ +guard (pcm_stream_lock_irqsave)(substream); +if (!snd_pcm_running (substream)) { + scoped_guard(spinlock_irqsave, &vss->lock) { + runtime = substream->runtime; + paused_xrun = runtime && + runtime->state ==SNDRV_PCM_STATE_PAUSED && + vss->runtime_active && vss->xfer_xrun; + } +if (paused_xrun) { +/* Wake readers without sending STOP for a paused stream. */ +snd_pcm_stop (substream,SNDRV_PCM_STATE_XRUN ); + } +return ; + } + + scoped_guard(spinlock_irqsave, &vss->lock) { + runtime = substream->runtime; +if (!vss->runtime_active || + (!vss->xfer_enabled && !vss->xfer_xrun) || !runtime) +return ; + + period_bytes =snd_pcm_lib_period_bytes (substream); +if (vss->xfer_xrun) { + notify = true; + }else if (!period_bytes || period_bytes >= vss->buffer_bytes || + !virtsnd_pcm_capture_counters_valid (vss) || + vss->capture_notified_periods > + vss->capture_copied_total / period_bytes) { + vss->xfer_xrun = true; + notify = true; + }else { + period_floor = vss->capture_copied_total / period_bytes; +if (period_floor > vss->capture_notified_periods) { +/* Reserve crossed boundaries before the callback. */ + vss->capture_notified_periods = period_floor; + notify = true; + } + } + +if (notify) { + snapshot = vss->capture_copied_total; + vss->capture_callback_snapshot = snapshot; + vss->capture_callback_snapshot_active = true; + } + } + +if (!notify) +return ; + +/* Pin the callback position while completions keep updating actual totals. */ +snd_pcm_period_elapsed_under_stream_lock (substream); + + scoped_guard(spinlock_irqsave, &vss->lock) { + vss->capture_callback_snapshot_active = false; + vss->capture_callback_snapshot =0 ; +if (vss->runtime_active && vss->xfer_enabled && + !vss->xfer_xrun && substream->runtime) { + period_bytes =snd_pcm_lib_period_bytes (substream); +if (period_bytes && + vss->capture_copied_total / period_bytes > + vss->capture_notified_periods) + requeue = true; + } + } -snd_pcm_period_elapsed (vss->substream); +if (requeue) +schedule_work (&vss->elapsed_period); }/**
@@ -517,8 +603,11 @@ void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event)break ;case VIRTIO_SND_EVT_PCM_XRUN : scoped_guard(spinlock, &vss->lock) { -if (vss->xfer_enabled) +if (vss->xfer_enabled) { vss->xfer_xrun = true; +if (vss->capture_staging) +schedule_work (&vss->elapsed_period); + } }break ; }
sound/virtio/virtio_pcm.h +68 -0
114 lines not on this page · raw message
sound/virtio/virtio_pcm_msg.c +274 -12
365 lines not on this page · raw message
sound/virtio/virtio_pcm_ops.c +718 -16
875 lines not on this page · raw message
[email protected]
AI reviewRe: [RFC PATCH v2 1/1] ALSA: virtio: stage VZ capture and restart paused streams
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
Add opt-in capture staging with response validation and accounting. Add a pause release/reprepare state machine for the VZ profile.
Assisted-by LLMSigned-off-by Joonho Lee <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1