Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH 1/2] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers
@ 2026-11-18 13:55 Takashi Iwai
  2026-11-18 13:55 ` [PATCH 2/2] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
  2026-11-18 14:05 ` [PATCH 1/2] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers sashiko-bot
  0 siblings, 2 replies; 4+ messages in thread
From: Takashi Iwai @ 2026-11-18 13:55 UTC (permalink / raw)
  To: linux-sound

There are a few places to grant the PCM buffer access with the gate
using runtime->buffer_accessing.  They are paired like below in each
place:

	if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
		return -EBUSY;
	....
	atomic_dec(&runtime->buffer_accessing);

which is not really intuitive and error-prone, as being open-coded.

This patch introduces helper macros to replace the code like the above
with auto-cleanup.  With the new helper, the above will become

	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
		return -EBUSY;
	....

and the counter-part (atomic_dec()) is dropped as it's automagically
done by the auto-cleanup.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/core/pcm_lib.c    |  7 +++---
 sound/core/pcm_local.h  | 26 +++++++++++++++++++++++
 sound/core/pcm_native.c | 47 ++++++++++++++++-------------------------
 3 files changed, 47 insertions(+), 33 deletions(-)

diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
index bb16738bb591..3b66677c037a 100644
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -2377,10 +2377,9 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
 			err = -EINVAL;
 			goto _end_unlock;
 		}
-		if (!atomic_inc_unless_negative(&runtime->buffer_accessing)) {
-			err = -EBUSY;
+		err = snd_pcm_buffer_access_get(runtime);
+		if (err < 0)
 			goto _end_unlock;
-		}
 		snd_pcm_stream_unlock_irq(substream);
 		if (!is_playback)
 			snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
@@ -2389,7 +2388,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
 		if (is_playback)
 			snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
 		snd_pcm_stream_lock_irq(substream);
-		atomic_dec(&runtime->buffer_accessing);
+		snd_pcm_buffer_access_put(runtime);
 		if (err < 0)
 			goto _end_unlock;
 		err = pcm_accessible_state(substream);
diff --git a/sound/core/pcm_local.h b/sound/core/pcm_local.h
index 4b134d435c24..7762c89e0f9f 100644
--- a/sound/core/pcm_local.h
+++ b/sound/core/pcm_local.h
@@ -86,4 +86,30 @@ static inline void snd_pcm_dma_buffer_sync(struct snd_pcm_substream *substream,
 #define is_oss_stream(substream)	false
 #endif
 
+/*
+ * helpers for restricting hw_params / hw_refine changes
+ */
+static inline int snd_pcm_buffer_access_get(struct snd_pcm_runtime *runtime)
+{
+	if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+		return -EBUSY;
+	return 0;
+}
+
+static inline void snd_pcm_buffer_access_put(struct snd_pcm_runtime *runtime)
+{
+	atomic_dec(&runtime->buffer_accessing);
+}
+
+/* auto-cleanup macros */
+DEFINE_GUARD(snd_pcm_buffer_access, struct snd_pcm_runtime *,
+	     snd_pcm_buffer_access_get(_T), snd_pcm_buffer_access_put(_T));
+DEFINE_GUARD_COND(snd_pcm_buffer_access, _try,
+		  snd_pcm_buffer_access_get(_T), _RET == 0);
+
+#define SND_PCM_BUFFER_ACQUIRE(runtime, var) \
+	ACQUIRE(snd_pcm_buffer_access_try, var)(runtime)
+#define SND_PCM_BUFFER_ACQUIRE_ERR(var) \
+	ACQUIRE_ERR(snd_pcm_buffer_access_try, &(var))
+
 #endif	/* __SOUND_CORE_PCM_LOCAL_H */
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 5d80b56449e2..f58d9be2ad44 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -3128,7 +3128,8 @@ static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
 		return 0;
 
 	/* Keep the DMA buffer alive through the sync below. */
-	if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
 		return -EBUSY;
 	scoped_guard(pcm_stream_lock_irq, substream) {
 		ret = do_pcm_hwsync(substream);
@@ -3138,7 +3139,6 @@ static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
 	}
 	if (ret >= 0)
 		snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
-	atomic_dec(&runtime->buffer_accessing);
 	return ret;
 }
 
@@ -3152,7 +3152,8 @@ static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
 		return 0;
 
 	/* Keep the DMA buffer alive through the sync below. */
-	if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
 		return -EBUSY;
 	scoped_guard(pcm_stream_lock_irq, substream) {
 		ret = do_pcm_hwsync(substream);
@@ -3162,7 +3163,6 @@ static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
 	}
 	if (ret >= 0)
 		snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
-	atomic_dec(&runtime->buffer_accessing);
 	return ret;
 }
 
@@ -3173,7 +3173,8 @@ static int snd_pcm_delay(struct snd_pcm_substream *substream,
 	int err;
 
 	/* Keep the DMA buffer alive through the sync below. */
-	if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
 		return -EBUSY;
 	scoped_guard(pcm_stream_lock_irq, substream) {
 		err = do_pcm_hwsync(substream);
@@ -3181,7 +3182,6 @@ static int snd_pcm_delay(struct snd_pcm_substream *substream,
 			*delay = snd_pcm_calc_delay(substream);
 	}
 	snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
-	atomic_dec(&runtime->buffer_accessing);
 
 	return err;
 }
@@ -4085,32 +4085,23 @@ int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
 	}
 	runtime = substream->runtime;
 	/* don't race with buffer reallocation in hw_params/hw_free */
-	if (!atomic_inc_unless_negative(&runtime->buffer_accessing))
+	SND_PCM_BUFFER_ACQUIRE(runtime, lock);
+	if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
 		return -EBUSY;
-	if (runtime->state == SNDRV_PCM_STATE_OPEN) {
-		err = -EBADFD;
-		goto out;
-	}
-	if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
-		err = -ENXIO;
-		goto out;
-	}
+	if (runtime->state == SNDRV_PCM_STATE_OPEN)
+		return -EBADFD;
+	if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
+		return -ENXIO;
 	if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
-	    runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
-		err = -EINVAL;
-		goto out;
-	}
+	    runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
+		return -EINVAL;
 	size = area->vm_end - area->vm_start;
 	offset = area->vm_pgoff << PAGE_SHIFT;
 	dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
-	if ((size_t)size > dma_bytes) {
-		err = -EINVAL;
-		goto out;
-	}
-	if (offset > dma_bytes - size) {
-		err = -EINVAL;
-		goto out;
-	}
+	if ((size_t)size > dma_bytes)
+		return -EINVAL;
+	if (offset > dma_bytes - size)
+		return -EINVAL;
 
 	area->vm_ops = &snd_pcm_vm_ops_data;
 	area->vm_private_data = substream;
@@ -4120,8 +4111,6 @@ int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
 		err = snd_pcm_lib_default_mmap(substream, area);
 	if (!err)
 		atomic_inc(&substream->mmap_count);
-out:
-	atomic_dec(&runtime->buffer_accessing);
 	return err;
 }
 EXPORT_SYMBOL(snd_pcm_mmap_data);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18 14:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 13:55 [PATCH 1/2] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers Takashi Iwai
2026-11-18 13:55 ` [PATCH 2/2] ALSA: pcm: Avoid hw_params/hw_refine changes during PCM buffer access Takashi Iwai
2026-11-18 14:29   ` sashiko-bot
2026-11-18 14:05 ` [PATCH 1/2] ALSA: pcm: Simplify PCM buffer access gate with auto-cleanup helpers sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox