Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: control: fix find_id()/find_numid() kerneldoc and lock the usb-mixer dedup loop
@ 2026-12-02  2:34 Hanxiang Xu
  2026-12-02  3:15 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:34 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  3:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  2:34 [PATCH] ALSA: control: fix find_id()/find_numid() kerneldoc and lock the usb-mixer dedup loop Hanxiang Xu
2026-12-02  3:15 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox