Skip to content

Commit

Permalink
ALSA: fix locking in snd_pcm_open*() and snd_rawmidi_open*()
Browse files Browse the repository at this point in the history
The PCM and rawmidi open callbacks have a lock against card->controls_list
but it takes a wrong one, card->controls_rwsem, instead of a right one
card->ctl_files_rwlock.  This patch fixes them.

This change also fixes automatically the potential deadlocks due to
mm->mmap_sem in munmap and copy_from/to_user, reported by Sitsofe
Wheeler:

  A: snd_ctl_elem_user_tlv(): card->controls_rwsem => mm->mmap_sem
  B: snd_pcm_open(): card->open_mutex => card->controls_rwsem
  C: munmap: mm->mmap_sem => snd_pcm_release(): card->open_mutex

The patch breaks the chain.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
  • Loading branch information
Takashi Iwai committed Sep 25, 2008
1 parent b0b8daf commit 399ccdc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sound/core/pcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,15 +781,15 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
return -ENODEV;

card = pcm->card;
down_read(&card->controls_rwsem);
read_lock(&card->ctl_files_rwlock);
list_for_each_entry(kctl, &card->ctl_files, list) {
if (kctl->pid == current->pid) {
prefer_subdevice = kctl->prefer_pcm_subdevice;
if (prefer_subdevice != -1)
break;
}
}
up_read(&card->controls_rwsem);
read_unlock(&card->ctl_files_rwlock);

switch (stream) {
case SNDRV_PCM_STREAM_PLAYBACK:
Expand Down
4 changes: 2 additions & 2 deletions sound/core/rawmidi.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,15 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
mutex_lock(&rmidi->open_mutex);
while (1) {
subdevice = -1;
down_read(&card->controls_rwsem);
read_lock(&card->ctl_files_rwlock);
list_for_each_entry(kctl, &card->ctl_files, list) {
if (kctl->pid == current->pid) {
subdevice = kctl->prefer_rawmidi_subdevice;
if (subdevice != -1)
break;
}
}
up_read(&card->controls_rwsem);
read_unlock(&card->ctl_files_rwlock);
err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
subdevice, fflags, rawmidi_file);
if (err >= 0)
Expand Down

0 comments on commit 399ccdc

Please sign in to comment.