ALSA: virtio-snd: fix dsnoop capture with staged buffers
Subsystems (3)
v4 cover ALSA: virtio-snd: fix dsnoop capture with staged buffers
Hi,
Changes in v4:
* Arm synchronous stop before paused capture enters XRUN through ACK or period work, so recovery releases pending RX requests before prepare.
* Add read/forward ACK regressions for first-prepare recovery and direct close/reopen, with native pause and pause-restart on one and two vCPUs.
* Keep patch 1 and the prerequisite metadata unchanged.
Sashiko identified a missing cleanup path in v3 patch 2:
https://sashiko.dev/#/patchset/20261025055702.69259-1-mynameisjune111%40gmail.com?part=2
Previous version:
https://lore.kernel.org/all/[email protected]/
This series fixes dsnoop capture on top of the staged-capture virtio-snd changes. dsnoop configures its slave with stop_threshold == boundary and can keep capture running without advancing the slave application pointer as an ordinary reader would. The staged-capture accounting currently exhausts its bounded unread capacity in this configuration.
The first patch marks stop_threshold accesses used by the driver's policy sampling. READ_ONCE/WRITE_ONCE provide single-access annotations; the virtio-snd state transitions remain protected by the substream lock.
The second patch caches the prepared boundary and samples the stop policy under that lock. A completion uses one sampled policy for validation, copying and queue refill. Free-running capture keeps requests queued and retains the existing frame, payload and pointer-window checks. Returning to bounded capture restores unread accounting from the monotonic counters. If the outstanding data already fills the ring, the policy helper returns -EPIPE without changing the cached mode or counters. The detecting RX, pointer or ACK path then reports XRUN through its existing error path. Native pause-release refill failures schedule the period worker to move a paused stream to XRUN and wake capture waiters.
This does not guarantee a synchronous error from the sw_params ioctl.
The ACK accounting and subsequent queue refill each sample the policy under the appropriate locks. Prepare and teardown reset the cached state. The existing ACK-race and priority changes are prerequisites to this series and are excluded from these two diffs.
New testing for v4:
* v3 reproduced -EPIPE from paused read/forward ACK, followed by -EBADFD on the first prepare in both native pause and pause-restart modes.
* The corrected module passed 16 combinations of ACK operation, recovery route, pause mode and vCPU count under ARM64 QEMU with KASAN/lockdep. Prepare recovery uses the first prepare without an intervening drop. Recovery and reopening each capture 48,000 frames.
* The native-pause regression and 13-case mode suite also passed. No KASAN or lockdep diagnostics were recorded in the six final VM runs.
* Direct close/reopen succeeded even on v3 in this fixture; these results establish the prepare regression, without establishing a persistent buffer leak across close. Forced RX/pause interleaving, new physical microphone tests and KCSAN reruns were not performed for v4.
Earlier testing for v3:
* ARM64 QEMU with KASAN and lockdep, native pause: v2 returned -EPIPE but stayed PAUSED, and a waiting poll timed out. The corrected module reached XRUN, woke poll with POLLERR and returned -EPIPE from read. Prepare recovery and reopening each captured 48,000 frames. The corrected case passed with both one and two vCPUs.
* The 13-case mode suite passed with the corrected module, including direct and mmap capture, policy transitions, delayed-reader recovery, reopening, mixed-rate dsnoop readers and client departure. KASAN and lockdep reported no diagnostics in these runs.
The following testing was performed on v2 before this focused change:
* ARM64 QEMU synthetic dsnoop capture: 12 files and 2,160,000 frames matched the reference sequence from each client's observed starting position. All 48 corruption, loss, duplication and reordering controls were detected.
* Default KCSAN: 13/13 cases and 619 policy transitions passed without race or lockdep reports. Forced RX/ACK policy changes and four isolated policy-helper branches passed under KASAN and KCSAN.
* macOS VZ with MOTU UltraLite-mk5 at 48 kHz: comparisons used one or two vCPUs and idle or CPU-saturated guests. The baseline failed all 32 shared-client captures. The candidate passed 48 captures and eight recovery diagnostics, including mixed-rate sharing, client departure, reopening and recovery after an intentionally delayed reader. Loaded shared captures ran for 70 seconds. All eight accepted VM runs shut down on the first request.
The physical tests checked frame counts and nonzero input. Exact sample continuity was checked with the QEMU synthetic source. VZ exposed mono; these tests do not establish physical input-channel routing or multichannel support. Aggressive KCSAN still produced dsnoop client-threshold XRUNs with a 160 ms buffer. Further isolation supports a deadline sensitivity, but does not exclude every kernel timing contribution. Separate generic KCSAN reports remain recorded in the test evidence.
This series depends on the staged-capture prerequisite stack below. The tested local base is 8d0d4c1a6c4bcb81ae9f52d1c0d979fafb2e22f2, consisting of nine prerequisite patches above sound.git commit 5618f6837ef3314e2c581d4c05ec13b7ed975cb4.
Prerequisites, in application order:
1-5. Lifetime and error recovery v3:
https://lore.kernel.org/all/[email protected]/
6. Generic teardown/control wait v2:
https://lore.kernel.org/all/[email protected]/
7. VZ staged capture v2:
https://lore.kernel.org/all/[email protected]/
8-9. ACK accounting and high-priority period work:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
This fixes the staged-capture accounting introduced by that prerequisite stack. The reproductions do not establish the same failure in an unmodified upstream kernel.
The policy uses the existing stop_threshold == boundary convention. Sampling it under the driver lock avoids introducing a PCM stream lock into RX completion. A completion overlapping a policy update may finish with the previous policy; its accounting and refill use that same sample. Rejecting a transition whose bounded accounting cannot be restored preserves explicit overflow detection and lets the caller recover the stream.
Patches:
ALSA: pcm: mark stop-threshold accesses for capture policy sampling
ALSA: virtio-snd: fix dsnoop capture with staged buffers
Thanks,
Joonho
---
base-commit: 5618f6837ef3314e2c581d4c05ec13b7ed975cb4
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: 771e8815f2b874d98376f309dc60bc90244f378c
prerequisite-patch-id: fdea8eaa29a47360e19820e27301dd8b6b26e43e
prerequisite-patch-id: 67ffb5c72970e1aeace72b6537dab1b977a3fc6c
prerequisite-message-id: [email protected]
prerequisite-message-id: [email protected]
prerequisite-message-id: [email protected]
prerequisite-message-id: [email protected]
PATCH v4 1/2 ALSA: pcm: mark stop-threshold accesses for capture policy sampling
The staged virtio-snd capture path needs to sample stop_threshold from completion context to distinguish free-running capture from bounded capture. Software parameters can update this field while capture runs.
Mark the runtime stop-threshold assignments with WRITE_ONCE and the proc status read with READ_ONCE. This pairs the assignments with the driver's READ_ONCE policy sampling in the following patch.
These annotations do not provide a transaction for the entire sw_params update. The driver serializes its cached policy and accounting with its own substream lock.
---
sound/core/oss/pcm_oss.c +1 -1
@@ -2958,7 +2958,7 @@ runtime->oss.mmap_bytes);#endif /* In mmap mode we never stop */ - runtime->stop_threshold = runtime->boundary; +WRITE_ONCE (runtime->stop_threshold, runtime->boundary);return 0 ; }
sound/core/pcm.c +2 -1
@@ -422,7 +422,8 @@snd_iprintf (buffer,"period_step: %u\n" , runtime->period_step);snd_iprintf (buffer,"avail_min: %lu\n" , runtime->control->avail_min);snd_iprintf (buffer,"start_threshold: %lu\n" , runtime->start_threshold); -snd_iprintf (buffer,"stop_threshold: %lu\n" , runtime->stop_threshold); +snd_iprintf (buffer,"stop_threshold: %lu\n" , +READ_ONCE (runtime->stop_threshold));snd_iprintf (buffer,"silence_threshold: %lu\n" , runtime->silence_threshold);snd_iprintf (buffer,"silence_size: %lu\n" , runtime->silence_size);snd_iprintf (buffer,"boundary: %lu\n" , runtime->boundary);
sound/core/pcm_native.c +3 -2
@@ -871,7 +871,7 @@ runtime->period_step =1 ; runtime->control->avail_min = runtime->period_size; runtime->start_threshold =1 ; - runtime->stop_threshold = runtime->buffer_size; +WRITE_ONCE (runtime->stop_threshold, runtime->buffer_size); runtime->silence_threshold =0 ; runtime->silence_size =0 ; runtime->boundary = runtime->buffer_size;
@@ -1062,7 +1062,8 @@ runtime->period_step = params->period_step; runtime->control->avail_min = params->avail_min; runtime->start_threshold = params->start_threshold; - runtime->stop_threshold = params->stop_threshold; +/* Drivers may sample the stop policy from completion context. */ +WRITE_ONCE (runtime->stop_threshold, params->stop_threshold); runtime->silence_threshold = params->silence_threshold; runtime->silence_size = params->silence_size; params->boundary = runtime->boundary;
[email protected]
AI reviewRe: [PATCH v4 1/2] ALSA: pcm: mark stop-threshold accesses for capture policy sampling
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines
The staged virtio-snd capture path needs to sample
stop_thresholdfrom completion context to distinguish free-running capture from bounded capture. Software parameters can update this field while capture runs.Mark the runtime stop-threshold assignments with
WRITE_ONCEand the proc status read withREAD_ONCE. This pairs the assignments with the driver'sREAD_ONCEpolicy sampling in the following patch.
Quoted 6 lines
These annotations do not provide a transaction for the entire
sw_paramsupdate. The driver serializes its cached policy and accounting with its own substream lock.Assisted-by LLMSigned-off-by Joonho Lee <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v4 2/2 ALSA: virtio-snd: fix dsnoop capture with staged buffers
In reply to Joonho Leedsnoop 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. A paused ACK can reach XRUN before that worker runs. Arm synchronous stop before returning -EPIPE from staged ACK, and before the worker stops a paused stream. ALSA skips trigger(STOP) for paused streams; these paths must preserve the RELEASE and pending-RX cleanup needed by prepare. Reset cached policy state during prepare and teardown.
Before the pause revisions, MOTU/VZ tests with one and two vCPUs 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.
Paused read and forward ACK errors reproduced a failing first prepare with v3. This revision passes 16 recovery cases across native pause and pause-restart with one and two vCPUs. Each prepare recovery and reopen captures 48,000 frames. The existing 13-case mode suite and native-pause regression also pass with KASAN and lockdep. No new physical-device or KCSAN results are claimed for this revision.
---
sound/virtio/virtio_pcm.c +2 -0
@@ -338,6 +338,8 @@ paused_xrun = runtime && runtime->state ==SNDRV_PCM_STATE_PAUSED && vss->runtime_active && vss->xfer_xrun; +if (paused_xrun) +virtsnd_pcm_capture_arm_paused_stop (vss); }if (paused_xrun) {/* Wake readers without sending STOP for a paused stream. */
sound/virtio/virtio_pcm.h +44 -14
104 lines not on this page · raw message
sound/virtio/virtio_pcm_msg.c +96 -28
191 lines not on this page · raw message
sound/virtio/virtio_pcm_ops.c +47 -7
119 lines not on this page · raw message
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 15 lines
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_readygrows 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
-EPIPEand aring_capacityrejection. 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.
Quoted 6 lines
[ ... ]
regression also pass with KASAN and lockdep. No new physical-device or KCSAN results are claimed for this revision.Assisted-by LLMSigned-off-by Joonho Lee <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Quoted 123 lines · Joonho Lee
Hi,
Changes in v4:
* Arm synchronous stop before paused capture enters XRUN through ACK or period work, so recovery releases pending RX requests before prepare.
* Add read/forward ACK regressions for first-prepare recovery and direct close/reopen, with native pause and pause-restart on one and two vCPUs.
* Keep patch 1 and the prerequisite metadata unchanged.Sashiko identified a missing cleanup path in v3 patch 2:
https://sashiko.dev/#/patchset/20261025055702.69259-1-mynameisjune111%40gmail.com?part=2Previous version:
https://lore.kernel.org/all/[email protected]/This series fixes dsnoop capture on top of the staged-capture virtio-snd changes. dsnoop configures its slave with
stop_threshold== boundary and can keep capture running without advancing the slave application pointer as an ordinary reader would. The staged-capture accounting currently exhausts its bounded unread capacity in this configuration.The first patch marks
stop_thresholdaccesses used by the driver's policy sampling. READ_ONCE/WRITE_ONCE provide single-access annotations; the virtio-snd state transitions remain protected by the substream lock.The second patch caches the prepared boundary and samples the stop policy under that lock. A completion uses one sampled policy for validation, copying and queue refill. Free-running capture keeps requests queued and retains the existing frame, payload and pointer-window checks. Returning to bounded capture restores unread accounting from the monotonic counters. If the outstanding data already fills the ring, the policy helper returns
-EPIPEwithout changing the cached mode or counters. The detecting RX, pointer or ACK path then reports XRUN through its existing error path. Native pause-release refill failures schedule the period worker to move a paused stream to XRUN and wake capture waiters.
This does not guarantee a synchronous error from thesw_paramsioctl.The ACK accounting and subsequent queue refill each sample the policy under the appropriate locks. Prepare and teardown reset the cached state. The existing ACK-race and priority changes are prerequisites to this series and are excluded from these two diffs.
New testing for v4:
* v3 reproduced
-EPIPEfrom paused read/forward ACK, followed by-EBADFDon the first prepare in both native pause and pause-restart modes.
* The corrected module passed 16 combinations of ACK operation, recovery route, pause mode and vCPU count under ARM64 QEMU with KASAN/lockdep. Prepare recovery uses the first prepare without an intervening drop. Recovery and reopening each capture 48,000 frames.
* The native-pause regression and 13-case mode suite also passed. No KASAN or lockdep diagnostics were recorded in the six final VM runs.
* Direct close/reopen succeeded even on v3 in this fixture; these results establish the prepare regression, without establishing a persistent buffer leak across close. Forced RX/pause interleaving, new physical microphone tests and KCSAN reruns were not performed for v4.Earlier testing for v3:
* ARM64 QEMU with KASAN and lockdep, native pause: v2 returned
-EPIPEbut stayed PAUSED, and a waiting poll timed out. The corrected module reached XRUN, woke poll with POLLERR and returned-EPIPEfrom read. Prepare recovery and reopening each captured 48,000 frames. The corrected case passed with both one and two vCPUs.
* The 13-case mode suite passed with the corrected module, including direct and mmap capture, policy transitions, delayed-reader recovery, reopening, mixed-rate dsnoop readers and client departure. KASAN and lockdep reported no diagnostics in these runs.The following testing was performed on v2 before this focused change:
* ARM64 QEMU synthetic dsnoop capture: 12 files and 2,160,000 frames matched the reference sequence from each client's observed starting position. All 48 corruption, loss, duplication and reordering controls were detected.
* Default KCSAN: 13/13 cases and 619 policy transitions passed without race or lockdep reports. Forced RX/ACK policy changes and four isolated policy-helper branches passed under KASAN and KCSAN.
* macOS VZ with MOTU UltraLite-mk5 at 48 kHz: comparisons used one or two vCPUs and idle or CPU-saturated guests. The baseline failed all 32 shared-client captures. The candidate passed 48 captures and eight recovery diagnostics, including mixed-rate sharing, client departure, reopening and recovery after an intentionally delayed reader. Loaded shared captures ran for 70 seconds. All eight accepted VM runs shut down on the first request.The physical tests checked frame counts and nonzero input. Exact sample continuity was checked with the QEMU synthetic source. VZ exposed mono; these tests do not establish physical input-channel routing or multichannel support. Aggressive KCSAN still produced dsnoop client-threshold XRUNs with a 160 ms buffer. Further isolation supports a deadline sensitivity, but does not exclude every kernel timing contribution. Separate generic KCSAN reports remain recorded in the test evidence.
This series depends on the staged-capture prerequisite stack below. The tested local base is
8d0d4c1a6c4bcb81ae9f52d1c0d979fafb2e22f2, consisting of nine prerequisite patches above sound.git commit5618f6837ef3314e2c581d4c05ec13b7ed975cb4.Prerequisites, in application order:
1-5. Lifetime and error recovery v3:
https://lore.kernel.org/all/[email protected]/
6. Generic teardown/control wait v2:
https://lore.kernel.org/all/[email protected]/
7. VZ staged capture v2:
https://lore.kernel.org/all/[email protected]/
8-9. ACK accounting and high-priority period work:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/This fixes the staged-capture accounting introduced by that prerequisite stack. The reproductions do not establish the same failure in an unmodified upstream kernel.
The policy uses the existing
stop_threshold== boundary convention. Sampling it under the driver lock avoids introducing a PCM stream lock into RX completion. A completion overlapping a policy update may finish with the previous policy; its accounting and refill use that same sample. Rejecting a transition whose bounded accounting cannot be restored preserves explicit overflow detection and lets the caller recover the stream.
Quoted 3 lines
Patches:
ALSA: pcm: mark stop-threshold accesses for capture policy sampling
ALSA: virtio-snd: fix dsnoop capture with staged buffers
The patch2 doesn't seem cleanly applicable to the latest sound git tree. Could you verify and rebase/submit? I think it'll be a material for 7.5, as it's no regression fix, though.
thanks,
Takashi