Staged capture maintains private copied and acknowledged byte totals.
Its ioctl RESET handler cannot keep the PCM lock held until core has
rebased appl_ptr. It also rebases the copied total, which loses the
relationship to completions arriving after the pointer observation.

Select the optional PCM reset operation when opening staged capture. At
this step, ordinary capture and playback keep the legacy RESET
completion policy, including when a later linked member fails. Call the
common snd_pcm_lib_reset() before acquiring the queue and substream
locks. For a running stream, retain copied_total and advance acked_total
only to pointer_seen_total. Completions after that observation remain
readable. For other states, align the private counters with the common
reset position. The operation table stays fixed for the opened runtime.

Complete the core rebase even if refilling RX fails, then report XRUN
through the period worker. A successful RESET return therefore does not
promise a usable running stream. A device-generation cancellation can
return -ECANCELED after common pointer processing; recovery still needs
the normal prepare path.

This patch depends on the core reset operation in patch 1 and the
pending staged capture and lifecycle work described in the cover letter.

Earlier narrow-candidate ARM64 QEMU tests forced RX progress between the
pointer observation and accounting, covering two capture policies and
10, 20 and 40 ms gaps. Two 1-vCPU runs crossed the ring boundary and
matched all 3,360,000 samples over the following 70 seconds, then
reopened successfully. Intentional-loss controls were detected. KASAN
and lockdep were quiet. The current narrow candidate has not been tested
with VZ and MOTU.

The ordinary capture RESET failure remains reproducible at this step.
Patch 3 handles its indirect accounting in a separate operation.
Playback continues to use the existing ioctl RESET completion path.

Assisted-by: LLM
Signed-off-by: Joonho Lee <[email protected]>

---
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
@@ -8,6 +8,8 @@
 #include <sound/pcm_params.h>
 
 #include "virtio_card.h"
+
+static const struct snd_pcm_ops virtsnd_pcm_capture_staged_ops;
 
 bool virtsnd_capture_staging;
 module_param_named(capture_staging, virtsnd_capture_staging, bool, 0444);
@@ -387,6 +389,11 @@
                }
        }
 
+       /* Keep the legacy RESET completion policy outside staged capture. */
+       substream->ops = vss->capture_staging ?
+               &virtsnd_pcm_capture_staged_ops :
+               &virtsnd_pcm_ops[substream->stream];
+
        snd_pcm_hw_constraint_integer(substream->runtime,
                                      SNDRV_PCM_HW_PARAM_PERIODS);
 
@@ -1591,6 +1598,58 @@
        return virtsnd_pcm_sync_stop_locked(substream);
 }
 
+/* The core holds the stream lock through the matching appl_ptr reset. */
+static int virtsnd_pcm_reset(struct snd_pcm_substream *substream)
+{
+       struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
+       struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss);
+       size_t position;
+       size_t period;
+       int rc;
+
+       /* Keep ALSA's pointer validation, time accounting and XRUN processing. 
*/
+       rc = snd_pcm_lib_reset(substream);
+       if (rc || !vss->capture_staging)
+               return rc;
+
+       guard(spinlock_irqsave)(&queue->lock);
+       guard(spinlock)(&vss->lock);
+       if (vss->resetting)
+               return -ECANCELED;
+       /* The common reset has already handled a pointer-reported XRUN. */
+       if (vss->xfer_xrun)
+               return 0;
+       if (snd_pcm_running(substream)) {
+               /* Keep completions arriving after the core's pointer 
observation. */
+               vss->capture_acked_total = vss->capture_pointer_seen_total;
+               vss->capture_ready = vss->capture_free_running ? 0 :
+                       vss->capture_copied_total - vss->capture_acked_total;
+       } else {
+               position = frames_to_bytes(runtime, runtime->status->hw_ptr);
+               vss->hw_ptr = position;
+               period = snd_pcm_lib_period_bytes(substream);
+               vss->capture_copied_total = position;
+               vss->capture_acked_total = position;
+               vss->capture_pointer_seen_total = position;
+               vss->capture_ready = 0;
+               vss->capture_notified_periods = period ? position / period : 0;
+       }
+       vss->capture_callback_snapshot = 0;
+       vss->capture_callback_snapshot_active = false;
+       vss->pcm_indirect.appl_ptr = runtime->status->hw_ptr;
+       if (snd_pcm_running(substream) && vss->xfer_enabled && vss->prepared) {
+               rc = virtsnd_pcm_capture_send(vss);
+               if (rc) {
+                       vss->xfer_xrun = true;
+                       virtsnd_pcm_schedule_period_elapsed(vss);
+               }
+       }
+       /* Complete the matching core pointer rebase before reporting XRUN. */
+       return 0;
+}
+
+/* PCM substream operators map. */
 const struct snd_pcm_ops virtsnd_pcm_ops[] = {
        {
                .open = virtsnd_pcm_open,
@@ -1617,3 +1676,18 @@
                .ack = virtsnd_pcm_cp_ack,
        },
 };
+
+/* Selected at open; the staging policy remains fixed for that runtime. */
+static const struct snd_pcm_ops virtsnd_pcm_capture_staged_ops = {
+       .open = virtsnd_pcm_open,
+       .close = virtsnd_pcm_close,
+       .ioctl = snd_pcm_lib_ioctl,
+       .reset = virtsnd_pcm_reset,
+       .hw_params = virtsnd_pcm_hw_params,
+       .hw_free = virtsnd_pcm_hw_free,
+       .prepare = virtsnd_pcm_prepare,
+       .trigger = virtsnd_pcm_trigger,
+       .sync_stop = virtsnd_pcm_sync_stop,
+       .pointer = virtsnd_pcm_cp_pointer,
+       .ack = virtsnd_pcm_cp_ack,
+};

Reply via email to