Skip to content

Commit 3c3201f

Browse files
committed
ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls
Like the previous fixes to hw_params and hw_free ioctl races, we need to paper over the concurrent prepare ioctl calls against hw_params and hw_free, too. This patch implements the locking with the existing runtime->buffer_mutex for prepare ioctls. Unlike the previous case for snd_pcm_hw_hw_params() and snd_pcm_hw_free(), snd_pcm_prepare() is performed to the linked streams, hence the lock can't be applied simply on the top. For tracking the lock in each linked substream, we modify snd_pcm_action_group() slightly and apply the buffer_mutex for the case stream_lock=false (formerly there was no lock applied) there. Cc: <stable@vger.kernel.org> Reviewed-by: Jaroslav Kysela <perex@perex.cz> Link: https://lore.kernel.org/r/20220322170720.3529-4-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent dca947d commit 3c3201f

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

sound/core/pcm_native.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,15 +1190,17 @@ struct action_ops {
11901190
static int snd_pcm_action_group(const struct action_ops *ops,
11911191
struct snd_pcm_substream *substream,
11921192
snd_pcm_state_t state,
1193-
bool do_lock)
1193+
bool stream_lock)
11941194
{
11951195
struct snd_pcm_substream *s = NULL;
11961196
struct snd_pcm_substream *s1;
11971197
int res = 0, depth = 1;
11981198

11991199
snd_pcm_group_for_each_entry(s, substream) {
1200-
if (do_lock && s != substream) {
1201-
if (s->pcm->nonatomic)
1200+
if (s != substream) {
1201+
if (!stream_lock)
1202+
mutex_lock_nested(&s->runtime->buffer_mutex, depth);
1203+
else if (s->pcm->nonatomic)
12021204
mutex_lock_nested(&s->self_group.mutex, depth);
12031205
else
12041206
spin_lock_nested(&s->self_group.lock, depth);
@@ -1226,18 +1228,18 @@ static int snd_pcm_action_group(const struct action_ops *ops,
12261228
ops->post_action(s, state);
12271229
}
12281230
_unlock:
1229-
if (do_lock) {
1230-
/* unlock streams */
1231-
snd_pcm_group_for_each_entry(s1, substream) {
1232-
if (s1 != substream) {
1233-
if (s1->pcm->nonatomic)
1234-
mutex_unlock(&s1->self_group.mutex);
1235-
else
1236-
spin_unlock(&s1->self_group.lock);
1237-
}
1238-
if (s1 == s) /* end */
1239-
break;
1231+
/* unlock streams */
1232+
snd_pcm_group_for_each_entry(s1, substream) {
1233+
if (s1 != substream) {
1234+
if (!stream_lock)
1235+
mutex_unlock(&s1->runtime->buffer_mutex);
1236+
else if (s1->pcm->nonatomic)
1237+
mutex_unlock(&s1->self_group.mutex);
1238+
else
1239+
spin_unlock(&s1->self_group.lock);
12401240
}
1241+
if (s1 == s) /* end */
1242+
break;
12411243
}
12421244
return res;
12431245
}
@@ -1367,10 +1369,12 @@ static int snd_pcm_action_nonatomic(const struct action_ops *ops,
13671369

13681370
/* Guarantee the group members won't change during non-atomic action */
13691371
down_read(&snd_pcm_link_rwsem);
1372+
mutex_lock(&substream->runtime->buffer_mutex);
13701373
if (snd_pcm_stream_linked(substream))
13711374
res = snd_pcm_action_group(ops, substream, state, false);
13721375
else
13731376
res = snd_pcm_action_single(ops, substream, state);
1377+
mutex_unlock(&substream->runtime->buffer_mutex);
13741378
up_read(&snd_pcm_link_rwsem);
13751379
return res;
13761380
}

0 commit comments

Comments
 (0)