From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=206.189.79.184 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796180910; b=e7RbhMY+SykT5WBGyeAMtZUoD9bE1Awk3efT3K7U6tOXWtjH32LKnldfg8/J/HlLcqh2 Clu/UFEQnWFmbQX0LOINsoT4LACDflF7TDDsDig2QNcA8ZrNHScXU/aaFRWuw7jKwZ4NG pwikn6I6XMq5qkXSNqwQ0CEvUtK2MZ3XBKEYLkXgLmnghTRGiHgeizITudsdydanDLfi2 wBYGzXxpDIN1TImghKqBLQmJ+s1YcnaM0FStPpn7TDtPizaH+CvbjzWvl+vivOecIqCNc HQJiFXUp9FWWbCkY82kOEG6QjKk3OwMRd9U1sr0k6b0bWuhwMJERGQrYd9ahf9hkCIA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796180910; h=DMARC-Filter:Received:Received:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Type:Content-Transfer-Encoding:X-CM-TRANSID: X-Coremail-Antispam:X-CM-SenderInfo; bh=OcRI24++ZCiNioWaS21kTVkmDY76C0HcKJyaJv5EOPk=; b=kaRLrzsaO9gPCKzEfRxw/h+xnAyYwT+tfE6AQFq8q3mnRu1Sf3ECd/hZjgoCJUrSraeD zfv5Pn4SGXniE6yg/6h0Nlm2pQMCRzrKPlVb8roxKdj4d5K1JeXUGdz00sQ0qKO8e9/CU 1jBQlD+fw0lY2n2YmOUY1/4y0xAzhIZo1iD1YU3MIG7Y6kY1Tc04q49YPqT3i6k0VG9H3 C+ZM5zqnIQadHldnZM3ruO6O5tDjB5yz59Szloms9/eLB+9X7GAxcA3aJil7rEeLuDBWK uFXT9nXD+NGz0/ZZpd0wlyXTvULYGtWcVf8UKXkJgkNNbfQO6b1sQECene//GgfopUg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=hust.edu.cn; spf=pass smtp.mailfrom=hust.edu.cn; arc=none smtp.remote-ip=206.189.79.184 Received: from zg8tmja2lje4os43os4xodqa.icoremail.net (zg8tmja2lje4os43os4xodqa.icoremail.net [206.189.79.184]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6F6FA606A31A; Wed, 2 Dec 2026 03:08:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6F6FA606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hust.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hust.edu.cn Received: from hust.edu.cn (unknown [172.16.0.52]) by app2 (Coremail) with UTF8SMTP id IQEQrMBWPhGqiw9r5qwSAA--.42264S2; Wed, 02 Dec 2026 11:08:26 +0800 (CST) Received: from [222.20.126.109] (unknown [222.20.126.109]) by gateway (Coremail) with SMTP id _____wA32zwhiw9rhI04AA--.32988S6; Wed, 02 Dec 2026 11:08:23 +0800 (CST) From: Hanxiang Xu To: Takashi Iwai , Jaroslav Kysela 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 Message-ID: <20261202103400.5-1-xuhx@hust.edu.cn> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID:IQEQrMBWPhGqiw9r5qwSAA--.42264S2 Authentication-Results: app2; spf=neutral smtp.mail=xuhx@hust.edu.cn; X-Coremail-Antispam: 1UD129KBjvJXoWxWryfJrWktF15XFy8ur1rZwb_yoW5WrWrpF W7WryvkayDJrs0y3W7XF48uF43uw4IkF9xC3y7JwnY9r43t3s5Xrn7GryruF17KwnagFWj qF12qrsYvas8J3DanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9Cb7Iv0xC_KF4lb4IE77IF4wAFc2x0x2IEx4CE42xK8VAvwI8I cIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK021l84ACjcxK6xIIjx v20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1l84ACjcxK 6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4 CY6c8Ij28IcVAaY2xG8wAqjxCEc2xF0cIa020Ex4CE44I27wAqx4xG64xvF2IEw4CE5I8C rVC2j2WlYx0EF7xvrVAajcxG14v26r4UJVWxJr1lYx0E74AGY7Cv6cx26r4fZr1UJr1lYx 0Ec7CjxVAajcxG14v26r4UJVWxJr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI48J MxkIecxEwVCm-wCF04k20xvY0x0EwIxGrwCF04k20xvE74AGY7Cv6cx26r4fZr1UJr1l4I 8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AK xVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcV AFwI0_Xr0_Ar1lIxAIcVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v2 0xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Cr0_Gr1UMIIF0xvEx4A2jsIEc7CjxVAFwI 0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07j-5rxUUUUU= X-CM-SenderInfo: bgsqjkayqyjmo6kx23oohg3hdfq/ 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: Signed-off-by: Hanxiang Xu --- 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