A driver which keeps private capture counters needs to reset those
counters together with the PCM application's position. The ioctl RESET
path releases the stream lock before core completes the position rebase,
and appl_ptr is updated in a separate post_action. Period processing can
observe the intermediate state.

Add an optional reset operation whose stream lock remains held through
the core position and appl_ptr updates. Export the common pointer reset
as snd_pcm_lib_reset(), so drivers can reuse pointer validation, time
accounting and XRUN handling without reacquiring the stream lock.
Drivers without the new operation keep their existing ioctl path.

A successful callback member completes its reset immediately. If a later
linked member fails, the first error is returned and completed members
are not rolled back. Legacy members retain the existing post_action
behavior, including in mixed groups. An error can leave partial changes
in the failing member as well. Callers must recover the group before
relying on its capture positions again.

The operation also runs after a successful driver prepare. A later
prepare failure can leave an earlier member reset without the group's
post_prepare state transition. Document this together with the atomic
and nonatomic locking requirements.

Linked synthetic capture tests cover both locking modes, four callback
combinations, later-member errors and recovery. The lock-window concern
was found by inspection; the existing delayed v7 test did not reproduce
a user-visible failure. The new-candidate tests validate the
synchronization contract and recovery paths.

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

---
diff --git a/include/sound/pcm.h b/include/sound/pcm.h
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -77,6 +77,20 @@
                             unsigned long offset);
        int (*mmap)(struct snd_pcm_substream *substream, struct vm_area_struct 
*vma);
        int (*ack)(struct snd_pcm_substream *substream);
+       /*
+        * Optional RESET operation, also used after a successful prepare.
+        * Called with this substream's PCM lock held; atomic streams must not
+        * sleep. Do not reacquire the PCM lock through snd_pcm_lib_ioctl().
+        * snd_pcm_lib_reset() provides the common pointer update under this
+        * lock. The core completes the position and appl_ptr rebase before
+        * unlocking a successful member.
+        *
+        * Return zero on success or a negative error. Linked RESET/prepare
+        * stops at the first error without rollback. Earlier callback members
+        * retain their completed reset; legacy members retain the existing
+        * post_action policy. An error does not guarantee unchanged state.
+        */
+       int (*reset)(struct snd_pcm_substream *substream);
 };
 
 /*
@@ -1179,6 +1193,7 @@
 {
        substream->runtime->std_sync_id = true;
 }
+int snd_pcm_lib_reset(struct snd_pcm_substream *substream);
 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
                      unsigned int cmd, void *arg);                      
 void snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream 
*substream);
diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -1760,20 +1760,39 @@
 }
 EXPORT_SYMBOL(snd_pcm_hw_params_bits);
 
-static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
-                                  void *arg)
+/**
+ * snd_pcm_lib_reset - Reset the pointer while holding the PCM stream lock
+ * @substream: PCM substream
+ *
+ * Context: PCM stream lock held. The caller must not hold driver locks
+ * acquired by the pointer or XRUN callbacks.
+ *
+ * Update a running stream through the ordinary pointer path. Other states
+ * start at zero. Pointer errors keep the existing RESET return semantics;
+ * a zero return does not imply recovery from XRUN.
+ * This helper does not update appl_ptr or acquire the stream lock.
+ *
+ * Return: zero.
+ */
+int snd_pcm_lib_reset(struct snd_pcm_substream *substream)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
 
-       guard(pcm_stream_lock_irqsave)(substream);
-       if (snd_pcm_running(substream) &&
-           snd_pcm_update_hw_ptr(substream) >= 0)
+       if (snd_pcm_running(substream) && snd_pcm_update_hw_ptr(substream) >= 0)
                runtime->status->hw_ptr %= runtime->buffer_size;
        else {
                runtime->status->hw_ptr = 0;
                runtime->hw_ptr_wrap = 0;
        }
        return 0;
+}
+EXPORT_SYMBOL(snd_pcm_lib_reset);
+
+static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
+                                  void *arg)
+{
+       guard(pcm_stream_lock_irqsave)(substream);
+       return snd_pcm_lib_reset(substream);
 }
 
 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -1989,7 +1989,31 @@
                            snd_pcm_state_t state)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
-       int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
+       int err;
+
+       if (substream->ops->reset) {
+               /*
+                * Complete this member before releasing its PCM lock. A later
+                * linked member may fail; RESET has no group rollback 
operation.
+                */
+               guard(pcm_stream_lock_irq)(substream);
+
+               err = substream->ops->reset(substream);
+               if (err < 0)
+                       return err;
+               runtime->hw_ptr_base = 0;
+               runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
+                       runtime->status->hw_ptr % runtime->period_size;
+               runtime->silence_start = runtime->status->hw_ptr;
+               runtime->silence_filled = 0;
+               runtime->control->appl_ptr = runtime->status->hw_ptr;
+               if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
+                   runtime->silence_size > 0)
+                       snd_pcm_playback_silence(substream, ULONG_MAX);
+               return 0;
+       }
+
+       err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
        if (err < 0)
                return err;
        guard(pcm_stream_lock_irq)(substream);
@@ -2005,6 +2029,9 @@
                               snd_pcm_state_t state)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
+
+       if (substream->ops->reset)
+               return;
        guard(pcm_stream_lock_irq)(substream);
        runtime->control->appl_ptr = runtime->status->hw_ptr;
        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&

Reply via email to