mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hanxiang Xu <xuhx@hust.edu.cn>
To: Takashi Iwai <tiwai@suse.com>, Jaroslav Kysela <perex@perex.cz>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ALSA: control: fix find_id()/find_numid() kerneldoc and lock the usb-mixer dedup loop
Date: Tue, 02 Dec 2026 10:34:00 +0800	[thread overview]
Message-ID: <20261202103400.5-1-xuhx@hust.edu.cn> (raw)

With CONFIG_SND_CTL_FAST_LOOKUP the fast paths of
snd_ctl_find_id()/snd_ctl_find_numid() are unlocked xa_load() calls
that return an unreferenced struct snd_kcontrol pointer, but their
kerneldoc still states "Note that this function takes
card->controls_rwlock lock internally".  Since the removal path frees
the control without a grace period, a caller that relies on that
stale statement dereferences freed memory.

The jack-creation (get_available_index()) and vmaster-follower
(snd_ctl_add_followers()) callers already hold the lock as of
7.4-rc5.  Fix the two kerneldoc blocks and lock the remaining
unlocked caller, the dedup loop of snd_usb_mixer_add_list() (the
read lock is released before snd_ctl_add() takes the write lock).

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
 sound/core/control.c | 11 +++++++++--
 sound/usb/mixer.c | 6 ++++--
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/sound/core/control.c b/sound/core/control.c
index 42f16d2d0e79881159250a43a80063ab775b7907..1808ad405d580b50b12183e5ecfeaf7cc0849ba7 100644
@@ -813,7 +813,10 @@
  *
  * Return: The pointer of the instance if found, or %NULL if not.
  *
- * Note that this function takes card->controls_rwlock lock internally.
+ * Note that the returned pointer is unreferenced: with
+ * CONFIG_SND_CTL_FAST_LOOKUP this is an unlocked xa_load() and the
+ * control can be freed concurrently.  The caller must hold
+ * card->controls_rwsem or otherwise protect the returned object.
  */
 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card,
 					unsigned int numid)
@@ -838,7 +841,11 @@ snd_ctl_find_numid_slow(struct snd_card *card, unsigned int numid)
  *
  * Return: The pointer of the instance if found, or %NULL if not.
  *
- * Note that this function takes card->controls_rwlock lock internally.
+ * Note that the returned pointer is unreferenced: with
+ * CONFIG_SND_CTL_FAST_LOOKUP the hash/numid paths are unlocked
+ * xa_load() calls and the control can be freed concurrently.  The
+ * caller must hold card->controls_rwsem or otherwise protect the
+ * returned object.
  */
 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
 				     const struct snd_ctl_elem_id *id)
diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
index 95209ec006b3c650bffe0ad20acf66d0ee61382c..40f280a9c76ce4bf72a49e2daf0e4d1fd75c1ab1 100644
@@ -664,8 +664,10 @@
 	int err;
 
 	if (look_for_empty) {
-		while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
-			kctl->id.index++;
+		scoped_guard(rwsem_read, &mixer->chip->card->controls_rwsem) {
+			while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
+				kctl->id.index++;
+		}
 	}
 
 	err = snd_ctl_add(mixer->chip->card, kctl);
-- 
2.43.0


             reply	other threads:[~2026-12-02  3:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  2:34 Hanxiang Xu [this message]
2026-12-02  8:54 ` 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=20261202103400.5-1-xuhx@hust.edu.cn \
    --to=xuhx@hust.edu.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®