Skip to content

Commit dd0da75

Browse files
committed
ALSA: pcm: Use guard() for locking
We can simplify the code gracefully with new guard() macro and co for automatic cleanup of locks. Only the code refactoring, and no functional changes. Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://lore.kernel.org/r/20240227085306.9764-22-tiwai@suse.de
1 parent 1affe7b commit dd0da75

3 files changed

Lines changed: 59 additions & 109 deletions

File tree

sound/core/pcm.c

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ static int snd_pcm_control_ioctl(struct snd_card *card,
9191

9292
if (get_user(device, (int __user *)arg))
9393
return -EFAULT;
94-
mutex_lock(&register_mutex);
95-
device = snd_pcm_next(card, device);
96-
mutex_unlock(&register_mutex);
94+
scoped_guard(mutex, &register_mutex)
95+
device = snd_pcm_next(card, device);
9796
if (put_user(device, (int __user *)arg))
9897
return -EFAULT;
9998
return 0;
@@ -106,7 +105,6 @@ static int snd_pcm_control_ioctl(struct snd_card *card,
106105
struct snd_pcm *pcm;
107106
struct snd_pcm_str *pstr;
108107
struct snd_pcm_substream *substream;
109-
int err;
110108

111109
info = (struct snd_pcm_info __user *)arg;
112110
if (get_user(device, &info->device))
@@ -118,35 +116,23 @@ static int snd_pcm_control_ioctl(struct snd_card *card,
118116
stream = array_index_nospec(stream, 2);
119117
if (get_user(subdevice, &info->subdevice))
120118
return -EFAULT;
121-
mutex_lock(&register_mutex);
119+
guard(mutex)(&register_mutex);
122120
pcm = snd_pcm_get(card, device);
123-
if (pcm == NULL) {
124-
err = -ENXIO;
125-
goto _error;
126-
}
121+
if (pcm == NULL)
122+
return -ENXIO;
127123
pstr = &pcm->streams[stream];
128-
if (pstr->substream_count == 0) {
129-
err = -ENOENT;
130-
goto _error;
131-
}
132-
if (subdevice >= pstr->substream_count) {
133-
err = -ENXIO;
134-
goto _error;
135-
}
124+
if (pstr->substream_count == 0)
125+
return -ENOENT;
126+
if (subdevice >= pstr->substream_count)
127+
return -ENXIO;
136128
for (substream = pstr->substream; substream;
137129
substream = substream->next)
138130
if (substream->number == (int)subdevice)
139131
break;
140-
if (substream == NULL) {
141-
err = -ENXIO;
142-
goto _error;
143-
}
144-
mutex_lock(&pcm->open_mutex);
145-
err = snd_pcm_info_user(substream, info);
146-
mutex_unlock(&pcm->open_mutex);
147-
_error:
148-
mutex_unlock(&register_mutex);
149-
return err;
132+
if (substream == NULL)
133+
return -ENXIO;
134+
guard(mutex)(&pcm->open_mutex);
135+
return snd_pcm_info_user(substream, info);
150136
}
151137
case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
152138
{
@@ -389,15 +375,15 @@ static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
389375
struct snd_pcm_substream *substream = entry->private_data;
390376
struct snd_pcm_runtime *runtime;
391377

392-
mutex_lock(&substream->pcm->open_mutex);
378+
guard(mutex)(&substream->pcm->open_mutex);
393379
runtime = substream->runtime;
394380
if (!runtime) {
395381
snd_iprintf(buffer, "closed\n");
396-
goto unlock;
382+
return;
397383
}
398384
if (runtime->state == SNDRV_PCM_STATE_OPEN) {
399385
snd_iprintf(buffer, "no setup\n");
400-
goto unlock;
386+
return;
401387
}
402388
snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
403389
snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
@@ -416,8 +402,6 @@ static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
416402
snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
417403
}
418404
#endif
419-
unlock:
420-
mutex_unlock(&substream->pcm->open_mutex);
421405
}
422406

423407
static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
@@ -426,15 +410,15 @@ static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
426410
struct snd_pcm_substream *substream = entry->private_data;
427411
struct snd_pcm_runtime *runtime;
428412

429-
mutex_lock(&substream->pcm->open_mutex);
413+
guard(mutex)(&substream->pcm->open_mutex);
430414
runtime = substream->runtime;
431415
if (!runtime) {
432416
snd_iprintf(buffer, "closed\n");
433-
goto unlock;
417+
return;
434418
}
435419
if (runtime->state == SNDRV_PCM_STATE_OPEN) {
436420
snd_iprintf(buffer, "no setup\n");
437-
goto unlock;
421+
return;
438422
}
439423
snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
440424
snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
@@ -444,8 +428,6 @@ static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
444428
snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
445429
snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
446430
snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
447-
unlock:
448-
mutex_unlock(&substream->pcm->open_mutex);
449431
}
450432

451433
static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
@@ -456,17 +438,17 @@ static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
456438
struct snd_pcm_status64 status;
457439
int err;
458440

459-
mutex_lock(&substream->pcm->open_mutex);
441+
guard(mutex)(&substream->pcm->open_mutex);
460442
runtime = substream->runtime;
461443
if (!runtime) {
462444
snd_iprintf(buffer, "closed\n");
463-
goto unlock;
445+
return;
464446
}
465447
memset(&status, 0, sizeof(status));
466448
err = snd_pcm_status64(substream, &status);
467449
if (err < 0) {
468450
snd_iprintf(buffer, "error %d\n", err);
469-
goto unlock;
451+
return;
470452
}
471453
snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
472454
snd_iprintf(buffer, "owner_pid : %d\n", pid_vnr(substream->pid));
@@ -480,8 +462,6 @@ static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
480462
snd_iprintf(buffer, "-----\n");
481463
snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
482464
snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
483-
unlock:
484-
mutex_unlock(&substream->pcm->open_mutex);
485465
}
486466

487467
#ifdef CONFIG_SND_PCM_XRUN_DEBUG
@@ -1009,9 +989,8 @@ void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
1009989
kfree(runtime->hw_constraints.rules);
1010990
/* Avoid concurrent access to runtime via PCM timer interface */
1011991
if (substream->timer) {
1012-
spin_lock_irq(&substream->timer->lock);
1013-
substream->runtime = NULL;
1014-
spin_unlock_irq(&substream->timer->lock);
992+
scoped_guard(spinlock_irq, &substream->timer->lock)
993+
substream->runtime = NULL;
1015994
} else {
1016995
substream->runtime = NULL;
1017996
}
@@ -1068,10 +1047,10 @@ static int snd_pcm_dev_register(struct snd_device *device)
10681047
return -ENXIO;
10691048
pcm = device->device_data;
10701049

1071-
mutex_lock(&register_mutex);
1050+
guard(mutex)(&register_mutex);
10721051
err = snd_pcm_add(pcm);
10731052
if (err)
1074-
goto unlock;
1053+
return err;
10751054
for (cidx = 0; cidx < 2; cidx++) {
10761055
int devtype = -1;
10771056
if (pcm->streams[cidx].substream == NULL)
@@ -1090,17 +1069,14 @@ static int snd_pcm_dev_register(struct snd_device *device)
10901069
pcm->streams[cidx].dev);
10911070
if (err < 0) {
10921071
list_del_init(&pcm->list);
1093-
goto unlock;
1072+
return err;
10941073
}
10951074

10961075
for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
10971076
snd_pcm_timer_init(substream);
10981077
}
10991078

11001079
pcm_call_notify(pcm, n_register);
1101-
1102-
unlock:
1103-
mutex_unlock(&register_mutex);
11041080
return err;
11051081
}
11061082

@@ -1110,8 +1086,8 @@ static int snd_pcm_dev_disconnect(struct snd_device *device)
11101086
struct snd_pcm_substream *substream;
11111087
int cidx;
11121088

1113-
mutex_lock(&register_mutex);
1114-
mutex_lock(&pcm->open_mutex);
1089+
guard(mutex)(&register_mutex);
1090+
guard(mutex)(&pcm->open_mutex);
11151091
wake_up(&pcm->open_wait);
11161092
list_del_init(&pcm->list);
11171093

@@ -1138,8 +1114,6 @@ static int snd_pcm_dev_disconnect(struct snd_device *device)
11381114
snd_unregister_device(pcm->streams[cidx].dev);
11391115
free_chmap(&pcm->streams[cidx]);
11401116
}
1141-
mutex_unlock(&pcm->open_mutex);
1142-
mutex_unlock(&register_mutex);
11431117
return 0;
11441118
}
11451119

@@ -1164,7 +1138,7 @@ int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
11641138
!notify->n_unregister ||
11651139
!notify->n_disconnect))
11661140
return -EINVAL;
1167-
mutex_lock(&register_mutex);
1141+
guard(mutex)(&register_mutex);
11681142
if (nfree) {
11691143
list_del(&notify->list);
11701144
list_for_each_entry(pcm, &snd_pcm_devices, list)
@@ -1174,7 +1148,6 @@ int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
11741148
list_for_each_entry(pcm, &snd_pcm_devices, list)
11751149
notify->n_register(pcm);
11761150
}
1177-
mutex_unlock(&register_mutex);
11781151
return 0;
11791152
}
11801153
EXPORT_SYMBOL(snd_pcm_notify);
@@ -1190,7 +1163,7 @@ static void snd_pcm_proc_read(struct snd_info_entry *entry,
11901163
{
11911164
struct snd_pcm *pcm;
11921165

1193-
mutex_lock(&register_mutex);
1166+
guard(mutex)(&register_mutex);
11941167
list_for_each_entry(pcm, &snd_pcm_devices, list) {
11951168
snd_iprintf(buffer, "%02i-%02i: %s : %s",
11961169
pcm->card->number, pcm->device, pcm->id, pcm->name);
@@ -1202,7 +1175,6 @@ static void snd_pcm_proc_read(struct snd_info_entry *entry,
12021175
pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
12031176
snd_iprintf(buffer, "\n");
12041177
}
1205-
mutex_unlock(&register_mutex);
12061178
}
12071179

12081180
static struct snd_info_entry *snd_pcm_proc_entry;

sound/core/pcm_memory.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,15 @@ static void __update_allocated_size(struct snd_card *card, ssize_t bytes)
3838

3939
static void update_allocated_size(struct snd_card *card, ssize_t bytes)
4040
{
41-
mutex_lock(&card->memory_mutex);
41+
guard(mutex)(&card->memory_mutex);
4242
__update_allocated_size(card, bytes);
43-
mutex_unlock(&card->memory_mutex);
4443
}
4544

4645
static void decrease_allocated_size(struct snd_card *card, size_t bytes)
4746
{
48-
mutex_lock(&card->memory_mutex);
47+
guard(mutex)(&card->memory_mutex);
4948
WARN_ON(card->total_pcm_alloc_bytes < bytes);
5049
__update_allocated_size(card, -(ssize_t)bytes);
51-
mutex_unlock(&card->memory_mutex);
5250
}
5351

5452
static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
@@ -58,14 +56,12 @@ static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
5856
int err;
5957

6058
/* check and reserve the requested size */
61-
mutex_lock(&card->memory_mutex);
62-
if (max_alloc_per_card &&
63-
card->total_pcm_alloc_bytes + size > max_alloc_per_card) {
64-
mutex_unlock(&card->memory_mutex);
65-
return -ENOMEM;
59+
scoped_guard(mutex, &card->memory_mutex) {
60+
if (max_alloc_per_card &&
61+
card->total_pcm_alloc_bytes + size > max_alloc_per_card)
62+
return -ENOMEM;
63+
__update_allocated_size(card, size);
6664
}
67-
__update_allocated_size(card, size);
68-
mutex_unlock(&card->memory_mutex);
6965

7066
if (str == SNDRV_PCM_STREAM_PLAYBACK)
7167
dir = DMA_TO_DEVICE;
@@ -191,20 +187,20 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
191187
size_t size;
192188
struct snd_dma_buffer new_dmab;
193189

194-
mutex_lock(&substream->pcm->open_mutex);
190+
guard(mutex)(&substream->pcm->open_mutex);
195191
if (substream->runtime) {
196192
buffer->error = -EBUSY;
197-
goto unlock;
193+
return;
198194
}
199195
if (!snd_info_get_line(buffer, line, sizeof(line))) {
200196
snd_info_get_str(str, line, sizeof(str));
201197
size = simple_strtoul(str, NULL, 10) * 1024;
202198
if ((size != 0 && size < 8192) || size > substream->dma_max) {
203199
buffer->error = -EINVAL;
204-
goto unlock;
200+
return;
205201
}
206202
if (substream->dma_buffer.bytes == size)
207-
goto unlock;
203+
return;
208204
memset(&new_dmab, 0, sizeof(new_dmab));
209205
new_dmab.dev = substream->dma_buffer.dev;
210206
if (size > 0) {
@@ -218,7 +214,7 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
218214
substream->pcm->card->number, substream->pcm->device,
219215
substream->stream ? 'c' : 'p', substream->number,
220216
substream->pcm->name, size);
221-
goto unlock;
217+
return;
222218
}
223219
substream->buffer_bytes_max = size;
224220
} else {
@@ -230,8 +226,6 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
230226
} else {
231227
buffer->error = -EINVAL;
232228
}
233-
unlock:
234-
mutex_unlock(&substream->pcm->open_mutex);
235229
}
236230

237231
static inline void preallocate_info_init(struct snd_pcm_substream *substream)

0 commit comments

Comments
 (0)