Skip to content

Commit f9d2028

Browse files
committed
ALSA: rawmidi - Fix possible race in open
The module refcount should be handled in the register_mutex to avoid possible races with module unloading. Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent fec6c6f commit f9d2028

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

sound/core/rawmidi.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,16 @@ int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
237237
rfile->input = rfile->output = NULL;
238238
mutex_lock(&register_mutex);
239239
rmidi = snd_rawmidi_search(card, device);
240-
mutex_unlock(&register_mutex);
241240
if (rmidi == NULL) {
242-
err = -ENODEV;
243-
goto __error1;
241+
mutex_unlock(&register_mutex);
242+
return -ENODEV;
244243
}
245244
if (!try_module_get(rmidi->card->module)) {
246-
err = -EFAULT;
247-
goto __error1;
245+
mutex_unlock(&register_mutex);
246+
return -ENXIO;
248247
}
248+
mutex_unlock(&register_mutex);
249+
249250
if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
250251
mutex_lock(&rmidi->open_mutex);
251252
if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
@@ -370,10 +371,9 @@ int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
370371
snd_rawmidi_runtime_free(sinput);
371372
if (output != NULL)
372373
snd_rawmidi_runtime_free(soutput);
373-
module_put(rmidi->card->module);
374374
if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
375375
mutex_unlock(&rmidi->open_mutex);
376-
__error1:
376+
module_put(rmidi->card->module);
377377
return err;
378378
}
379379

0 commit comments

Comments
 (0)