All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: alsa-devel@alsa-project.org
Subject: [PATCH 07/11] ALSA: usxy2: Fix potential doubly allocations
Date: Mon, 17 May 2021 15:15:41 +0200	[thread overview]
Message-ID: <20210517131545.27252-8-tiwai@suse.de> (raw)
In-Reply-To: <20210517131545.27252-1-tiwai@suse.de>

The PCM shmem pages are allocated in snd_usx2y_usbpcm_prepare().
Theoretically the prepare callback may be called simultaneously for
both playback and capture, hence this allocation can be racy.

Make sure that the allocation is performed exclusively by extending
the pcm_mutex lock to cover the allocation code, too.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/usb/usx2y/usx2yhwdeppcm.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/sound/usb/usx2y/usx2yhwdeppcm.c b/sound/usb/usx2y/usx2yhwdeppcm.c
index b988a4870de4..da643c2dbb12 100644
--- a/sound/usb/usx2y/usx2yhwdeppcm.c
+++ b/sound/usb/usx2y/usx2yhwdeppcm.c
@@ -503,15 +503,18 @@ static int snd_usx2y_usbpcm_prepare(struct snd_pcm_substream *substream)
 
 	snd_printdd("snd_usx2y_pcm_prepare(%p)\n", substream);
 
+	mutex_lock(&usx2y->pcm_mutex);
+
 	if (!usx2y->hwdep_pcm_shm) {
 		usx2y->hwdep_pcm_shm = alloc_pages_exact(USX2Y_HWDEP_PCM_PAGES,
 							 GFP_KERNEL);
-		if (!usx2y->hwdep_pcm_shm)
-			return -ENOMEM;
+		if (!usx2y->hwdep_pcm_shm) {
+			err = -ENOMEM;
+			goto up_prepare_mutex;
+		}
 		memset(usx2y->hwdep_pcm_shm, 0, USX2Y_HWDEP_PCM_PAGES);
 	}
 
-	mutex_lock(&usx2y->pcm_mutex);
 	usx2y_subs_prepare(subs);
 	// Start hardware streams
 	// SyncStream first....
-- 
2.26.2


  parent reply	other threads:[~2021-05-17 13:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-17 13:15 [PATCH 00/11] ALSA: usx2y: Fixes and cleanups Takashi Iwai
2021-05-17 13:15 ` [PATCH 01/11] ALSA: usx2y: Avoid camelCase Takashi Iwai
2021-05-17 13:15 ` [PATCH 02/11] ALSA: usx2y: Fix spaces Takashi Iwai
2021-05-17 13:15 ` [PATCH 03/11] ALSA: usx2y: Coding style fixes Takashi Iwai
2021-05-17 13:15 ` [PATCH 04/11] ALSA: usx2y: Fix potential leaks of uninitialized memory Takashi Iwai
2021-05-17 13:15 ` [PATCH 05/11] ALSA: usx2y: Avoid self-killing Takashi Iwai
2021-05-17 13:15 ` [PATCH 06/11] ALSA: usx2y: Fix potential memory leaks Takashi Iwai
2021-05-17 13:15 ` Takashi Iwai [this message]
2021-05-17 13:15 ` [PATCH 08/11] ALSA: usx2y: Fix shmem initialization Takashi Iwai
2021-05-17 13:15 ` [PATCH 09/11] ALSA: usx2y: Don't call free_pages_exact() with NULL address Takashi Iwai
2021-05-17 13:15 ` [PATCH 10/11] ALSA: usx2y: Cleanup probe and disconnect callbacks Takashi Iwai
2021-05-17 13:15 ` [PATCH 11/11] ALSA: usx2y: Nuke pcm_list Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210517131545.27252-8-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.