Skip to content

Commit 650224f

Browse files
committed
ALSA: pcm: Use guard() for PCM stream locks
Define guard() usage for PCM stream locking and use it in appropriate places. The pair of snd_pcm_stream_lock() and snd_pcm_stream_unlock() can be presented with guard(pcm_stream_lock) now. Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://lore.kernel.org/r/20240227085306.9764-23-tiwai@suse.de
1 parent dd0da75 commit 650224f

5 files changed

Lines changed: 191 additions & 218 deletions

File tree

include/sound/pcm.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,18 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
659659
flags = _snd_pcm_stream_lock_irqsave_nested(substream); \
660660
} while (0)
661661

662+
/* definitions for guard(); use like guard(pcm_stream_lock) */
663+
DEFINE_LOCK_GUARD_1(pcm_stream_lock, struct snd_pcm_substream,
664+
snd_pcm_stream_lock(_T->lock),
665+
snd_pcm_stream_unlock(_T->lock))
666+
DEFINE_LOCK_GUARD_1(pcm_stream_lock_irq, struct snd_pcm_substream,
667+
snd_pcm_stream_lock_irq(_T->lock),
668+
snd_pcm_stream_unlock_irq(_T->lock))
669+
DEFINE_LOCK_GUARD_1(pcm_stream_lock_irqsave, struct snd_pcm_substream,
670+
snd_pcm_stream_lock_irqsave(_T->lock, _T->flags),
671+
snd_pcm_stream_unlock_irqrestore(_T->lock, _T->flags),
672+
unsigned long flags)
673+
662674
/**
663675
* snd_pcm_group_for_each_entry - iterate over the linked substreams
664676
* @s: the iterator

sound/core/oss/pcm_oss.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,9 +1623,8 @@ static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
16231623
break;
16241624
result = 0;
16251625
set_current_state(TASK_INTERRUPTIBLE);
1626-
snd_pcm_stream_lock_irq(substream);
1627-
state = runtime->state;
1628-
snd_pcm_stream_unlock_irq(substream);
1626+
scoped_guard(pcm_stream_lock_irq, substream)
1627+
state = runtime->state;
16291628
if (state != SNDRV_PCM_STATE_RUNNING) {
16301629
set_current_state(TASK_RUNNING);
16311630
break;
@@ -2840,23 +2839,23 @@ static __poll_t snd_pcm_oss_poll(struct file *file, poll_table * wait)
28402839
if (psubstream != NULL) {
28412840
struct snd_pcm_runtime *runtime = psubstream->runtime;
28422841
poll_wait(file, &runtime->sleep, wait);
2843-
snd_pcm_stream_lock_irq(psubstream);
2844-
if (runtime->state != SNDRV_PCM_STATE_DRAINING &&
2845-
(runtime->state != SNDRV_PCM_STATE_RUNNING ||
2846-
snd_pcm_oss_playback_ready(psubstream)))
2847-
mask |= EPOLLOUT | EPOLLWRNORM;
2848-
snd_pcm_stream_unlock_irq(psubstream);
2842+
scoped_guard(pcm_stream_lock_irq, psubstream) {
2843+
if (runtime->state != SNDRV_PCM_STATE_DRAINING &&
2844+
(runtime->state != SNDRV_PCM_STATE_RUNNING ||
2845+
snd_pcm_oss_playback_ready(psubstream)))
2846+
mask |= EPOLLOUT | EPOLLWRNORM;
2847+
}
28492848
}
28502849
if (csubstream != NULL) {
28512850
struct snd_pcm_runtime *runtime = csubstream->runtime;
28522851
snd_pcm_state_t ostate;
28532852
poll_wait(file, &runtime->sleep, wait);
2854-
snd_pcm_stream_lock_irq(csubstream);
2855-
ostate = runtime->state;
2856-
if (ostate != SNDRV_PCM_STATE_RUNNING ||
2857-
snd_pcm_oss_capture_ready(csubstream))
2858-
mask |= EPOLLIN | EPOLLRDNORM;
2859-
snd_pcm_stream_unlock_irq(csubstream);
2853+
scoped_guard(pcm_stream_lock_irq, csubstream) {
2854+
ostate = runtime->state;
2855+
if (ostate != SNDRV_PCM_STATE_RUNNING ||
2856+
snd_pcm_oss_capture_ready(csubstream))
2857+
mask |= EPOLLIN | EPOLLRDNORM;
2858+
}
28602859
if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
28612860
struct snd_pcm_oss_file ofile;
28622861
memset(&ofile, 0, sizeof(ofile));

sound/core/pcm_compat.c

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -432,22 +432,22 @@ static int snd_pcm_ioctl_sync_ptr_x32(struct snd_pcm_substream *substream,
432432
boundary = recalculate_boundary(runtime);
433433
if (!boundary)
434434
boundary = 0x7fffffff;
435-
snd_pcm_stream_lock_irq(substream);
436-
/* FIXME: we should consider the boundary for the sync from app */
437-
if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
438-
control->appl_ptr = scontrol.appl_ptr;
439-
else
440-
scontrol.appl_ptr = control->appl_ptr % boundary;
441-
if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
442-
control->avail_min = scontrol.avail_min;
443-
else
444-
scontrol.avail_min = control->avail_min;
445-
sstatus.state = status->state;
446-
sstatus.hw_ptr = status->hw_ptr % boundary;
447-
sstatus.tstamp = status->tstamp;
448-
sstatus.suspended_state = status->suspended_state;
449-
sstatus.audio_tstamp = status->audio_tstamp;
450-
snd_pcm_stream_unlock_irq(substream);
435+
scoped_guard(pcm_stream_lock_irq, substream) {
436+
/* FIXME: we should consider the boundary for the sync from app */
437+
if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
438+
control->appl_ptr = scontrol.appl_ptr;
439+
else
440+
scontrol.appl_ptr = control->appl_ptr % boundary;
441+
if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
442+
control->avail_min = scontrol.avail_min;
443+
else
444+
scontrol.avail_min = control->avail_min;
445+
sstatus.state = status->state;
446+
sstatus.hw_ptr = status->hw_ptr % boundary;
447+
sstatus.tstamp = status->tstamp;
448+
sstatus.suspended_state = status->suspended_state;
449+
sstatus.audio_tstamp = status->audio_tstamp;
450+
}
451451
if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
452452
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
453453
if (put_user(sstatus.state, &src->s.status.state) ||
@@ -510,26 +510,24 @@ static int snd_pcm_ioctl_sync_ptr_buggy(struct snd_pcm_substream *substream,
510510
if (err < 0)
511511
return err;
512512
}
513-
snd_pcm_stream_lock_irq(substream);
514-
if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
515-
err = pcm_lib_apply_appl_ptr(substream, sync_cp->appl_ptr);
516-
if (err < 0) {
517-
snd_pcm_stream_unlock_irq(substream);
518-
return err;
513+
scoped_guard(pcm_stream_lock_irq, substream) {
514+
if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
515+
err = pcm_lib_apply_appl_ptr(substream, sync_cp->appl_ptr);
516+
if (err < 0)
517+
return err;
518+
} else {
519+
sync_cp->appl_ptr = control->appl_ptr;
519520
}
520-
} else {
521-
sync_cp->appl_ptr = control->appl_ptr;
521+
if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
522+
control->avail_min = sync_cp->avail_min;
523+
else
524+
sync_cp->avail_min = control->avail_min;
525+
sync_ptr.s.status.state = status->state;
526+
sync_ptr.s.status.hw_ptr = status->hw_ptr;
527+
sync_ptr.s.status.tstamp = status->tstamp;
528+
sync_ptr.s.status.suspended_state = status->suspended_state;
529+
sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
522530
}
523-
if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
524-
control->avail_min = sync_cp->avail_min;
525-
else
526-
sync_cp->avail_min = control->avail_min;
527-
sync_ptr.s.status.state = status->state;
528-
sync_ptr.s.status.hw_ptr = status->hw_ptr;
529-
sync_ptr.s.status.tstamp = status->tstamp;
530-
sync_ptr.s.status.suspended_state = status->suspended_state;
531-
sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
532-
snd_pcm_stream_unlock_irq(substream);
533531
if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
534532
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
535533
if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))

sound/core/pcm_lib.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,16 +1744,15 @@ static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
17441744
void *arg)
17451745
{
17461746
struct snd_pcm_runtime *runtime = substream->runtime;
1747-
unsigned long flags;
1748-
snd_pcm_stream_lock_irqsave(substream, flags);
1747+
1748+
guard(pcm_stream_lock_irqsave)(substream);
17491749
if (snd_pcm_running(substream) &&
17501750
snd_pcm_update_hw_ptr(substream) >= 0)
17511751
runtime->status->hw_ptr %= runtime->buffer_size;
17521752
else {
17531753
runtime->status->hw_ptr = 0;
17541754
runtime->hw_ptr_wrap = 0;
17551755
}
1756-
snd_pcm_stream_unlock_irqrestore(substream, flags);
17571756
return 0;
17581757
}
17591758

@@ -1899,14 +1898,11 @@ EXPORT_SYMBOL(snd_pcm_period_elapsed_under_stream_lock);
18991898
*/
19001899
void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
19011900
{
1902-
unsigned long flags;
1903-
19041901
if (snd_BUG_ON(!substream))
19051902
return;
19061903

1907-
snd_pcm_stream_lock_irqsave(substream, flags);
1904+
guard(pcm_stream_lock_irqsave)(substream);
19081905
snd_pcm_period_elapsed_under_stream_lock(substream);
1909-
snd_pcm_stream_unlock_irqrestore(substream, flags);
19101906
}
19111907
EXPORT_SYMBOL(snd_pcm_period_elapsed);
19121908

0 commit comments

Comments
 (0)