Re: [PATCH] ALSA: control: fix find_id()/find_numid() kerneldoc and lock the usb-mixer dedup loop
From: Takashi Iwai
Date: Wed Dec 02 2026 - 03:55:05 EST
On Wed, 02 Dec 2026 03:34:00 +0100,
Hanxiang Xu wrote:
>
> 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@xxxxxxxxxxxxxxx>
> Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
> ---
> 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)
The comment updates are fine, but...
> 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);
this one is rather superfluous. This probe doesn't happen
concurrently. And, if any, split to another patch. It's totally
different from the documentation update itself.
thanks,
Takashi