Skip to contentSkip to conversation

ASoC: soc-core: reset rtd->num_components after allocation

PATCH In review Emanuele Ghidoli 7 participants 13 messages Nov 9 – Nov 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: soc-core: reset rtd->num_components after allocation

From: Emanuele Ghidoli <[email protected]>

Commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family") switched the snd_soc_pcm_runtime allocation in soc_new_pcm_runtime() to devm_kzalloc_flex(). components[] is annotated with __counted_by(num_components), so when the compiler provides __builtin_counted_by_ref() the allocation now also sets rtd->num_components to the number of allocated slots.

snd_soc_rtd_add_component() fills components[] one entry at a time and expects num_components to start at zero. Starting from the allocated size, for_each_rtd_components() stops at the first, still NULL, entry, so the runtime has no components: they are not probed, no controls or user-visible PCM devices are created and the card registers empty, without any error message. In addition, each added component is stored past the end of components[]. On the tested board these stores still land in the slack of the kmalloc bucket that devres rounds the allocation up to, so KASAN does not report them.

Reset num_components after the allocation.

Seen on a Colibri iMX7D (simple-audio-card, fsl-sai, sgtl5000): the card is listed in /proc/asound/cards, but /proc/asound/pcm is empty and

amixer: Unable to find simple control 'Capture Mux',0
aplay: main:850: audio open error: No such file or directory

Tested on that board with v7.4-rc2 built with GCC 15.2 (first seen with GCC 16.2): the failure is there without this change; with it, playback, capture and the codec controls work again.

The root cause analysis, this change and the testing were done with the help of an AI coding assistant (Claude Code, Claude Opus 5.5).

Fixes 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")

---
4dd10fbbeb9c only reached mainline in v7.4-rc2 (not in broonie/sound yet), so this is based on v7.4-rc2.

The other devm_k[mz]alloc_flex() sites added by 4dd10fbbeb9c were checked
too: none of them fills a __counted_by() counter one entry at a time, so this one looks like the only user hit by the preset counter.

 sound/soc/soc-core.c | 6 ++++++
 1 file changed, 6 insertions(+)
sound/soc/soc-core.c +6 -0
index 68b54e509b65..06a1d5718997 100644
@@ -387,6 +387,12 @@ static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
 		device_unregister(dev);
 		return NULL;
 	}
+	/*
+	 * devm_kzalloc_flex() presets the __counted_by() counter to the
+	 * allocated size when the compiler has __builtin_counted_by_ref(),
+	 * but snd_soc_rtd_add_component() expects it to start from zero.
+	 */
+	rtd->num_components = 0;
 
 	rtd->dev = dev;
 	INIT_LIST_HEAD(&rtd->list);

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.53.0
#1 lore raw

On Mon, 09 Nov 2026 11:25:20 +0100,
Emanuele Ghidoli wrote:

Quoted 38 lines · Emanuele Ghidoli

From: Emanuele Ghidoli <[email protected]>

Commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family") switched the snd_soc_pcm_runtime allocation in soc_new_pcm_runtime() to devm_kzalloc_flex(). components[] is annotated with __counted_by(num_components), so when the compiler provides __builtin_counted_by_ref() the allocation now also sets rtd->num_components to the number of allocated slots.

snd_soc_rtd_add_component() fills components[] one entry at a time and expects num_components to start at zero. Starting from the allocated size, for_each_rtd_components() stops at the first, still NULL, entry, so the runtime has no components: they are not probed, no controls or user-visible PCM devices are created and the card registers empty, without any error message. In addition, each added component is stored past the end of components[]. On the tested board these stores still land in the slack of the kmalloc bucket that devres rounds the allocation up to, so KASAN does not report them.

Reset num_components after the allocation.

Seen on a Colibri iMX7D (simple-audio-card, fsl-sai, sgtl5000): the card is listed in /proc/asound/cards, but /proc/asound/pcm is empty and

amixer: Unable to find simple control 'Capture Mux',0
aplay: main:850: audio open error: No such file or directory

Tested on that board with v7.4-rc2 built with GCC 15.2 (first seen with GCC 16.2): the failure is there without this change; with it, playback, capture and the codec controls work again.

The root cause analysis, this change and the testing were done with the help of an AI coding assistant (Claude Code, Claude Opus 5.5).

Fixes 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")
Quoted 7 lines

---
4dd10fbbeb9c only reached mainline in v7.4-rc2 (not in broonie/sound yet), so this is based on v7.4-rc2.

The other devm_k[mz]alloc_flex() sites added by 4dd10fbbeb9c were checked
too: none of them fills a __counted_by() counter one entry at a time, so this one looks like the only user hit by the preset counter.

I guess a bugzilla entry that was reported recently is also related with this problem.
https://bugzilla.kernel.org/show_bug.cgi?id=222249

My first gut feeling was that it might be safer to revert the use of devm_kmalloc_flex() in that code, as it has a significant side-effect unlike other straight conversions. But, if this fix covers well, we can go with this fix, of course.

thanks,

Takashi

Quoted 97 lines
 sound/soc/soc-core.c | 6 ++++++
 1 file changed, 6 insertions(+)
sound/soc/soc-core.c +6 -0
index 68b54e509b65..06a1d5718997 100644
@@ -387,6 +387,12 @@ static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
 		device_unregister(dev);
 		return NULL;
 	}
+	/*
+	 * devm_kzalloc_flex() presets the __counted_by() counter to the
+	 * allocated size when the compiler has __builtin_counted_by_ref(),
+	 * but snd_soc_rtd_add_component() expects it to start from zero.
+	 */
+	rtd->num_components = 0;
 
 	rtd->dev = dev;
 	INIT_LIST_HEAD(&rtd->list);

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 50 lines
2.53.0


From: Emanuele Ghidoli <[email protected]>


Commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the
devm_kmalloc_obj() family") switched the snd_soc_pcm_runtime allocation
in soc_new_pcm_runtime() to devm_kzalloc_flex(). components[] is
annotated with __counted_by(num_components), so when the compiler
provides __builtin_counted_by_ref() the allocation now also sets
rtd->num_components to the number of allocated slots.

snd_soc_rtd_add_component() fills components[] one entry at a time and
expects num_components to start at zero. Starting from the allocated
size, for_each_rtd_components() stops at the first, still NULL, entry,
so the runtime has no components: they are not probed, no controls or
user-visible PCM devices are created and the card registers empty,
without any error message. In addition, each added component is stored
past the end of components[]. On the tested board these stores still
land in the slack of the kmalloc bucket that devres rounds the
allocation up to, so KASAN does not report them.

Reset num_components after the allocation.

Seen on a Colibri iMX7D (simple-audio-card, fsl-sai, sgtl5000): the card
is listed in /proc/asound/cards, but /proc/asound/pcm is empty and

  amixer: Unable to find simple control 'Capture Mux',0
  aplay: main:850: audio open error: No such file or directory

Tested on that board with v7.4-rc2 built with GCC 15.2 (first seen with
GCC 16.2): the failure is there without this change; with it, playback,
capture and the codec controls work again.

The root cause analysis, this change and the testing were done with the
help of an AI coding assistant (Claude Code, Claude Opus 5.5).

Fixes: 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")
Signed-off-by: Emanuele Ghidoli <[email protected]>
---
4dd10fbbeb9c only reached mainline in v7.4-rc2 (not in broonie/sound yet),
so this is based on v7.4-rc2.

The other devm_k[mz]alloc_flex() sites added by 4dd10fbbeb9c were checked
too: none of them fills a __counted_by() counter one entry at a time, so
this one looks like the only user hit by the preset counter.

 sound/soc/soc-core.c | 6 ++++++
 1 file changed, 6 insertions(+)
sound/soc/soc-core.c +6 -0
index 68b54e509b65..06a1d5718997 100644
@@ -387,6 +387,12 @@ static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
 		device_unregister(dev);
 		return NULL;
 	}
+	/*
+	 * devm_kzalloc_flex() presets the __counted_by() counter to the
+	 * allocated size when the compiler has __builtin_counted_by_ref(),
+	 * but snd_soc_rtd_add_component() expects it to start from zero.
+	 */
+	rtd->num_components = 0;
 
 	rtd->dev = dev;
 	INIT_LIST_HEAD(&rtd->list);

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.53.0
Reviewed-by
#2 lore raw
Quoted 51 lines · Takashi Iwai

On Mon, 09 Nov 2026 11:25:20 +0100,
Emanuele Ghidoli wrote:

Quoted 38 lines · Emanuele Ghidoli

From: Emanuele Ghidoli <[email protected]>

Commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family") switched the snd_soc_pcm_runtime allocation in soc_new_pcm_runtime() to devm_kzalloc_flex(). components[] is annotated with __counted_by(num_components), so when the compiler provides __builtin_counted_by_ref() the allocation now also sets rtd->num_components to the number of allocated slots.

snd_soc_rtd_add_component() fills components[] one entry at a time and expects num_components to start at zero. Starting from the allocated size, for_each_rtd_components() stops at the first, still NULL, entry, so the runtime has no components: they are not probed, no controls or user-visible PCM devices are created and the card registers empty, without any error message. In addition, each added component is stored past the end of components[]. On the tested board these stores still land in the slack of the kmalloc bucket that devres rounds the allocation up to, so KASAN does not report them.

Reset num_components after the allocation.

Seen on a Colibri iMX7D (simple-audio-card, fsl-sai, sgtl5000): the card is listed in /proc/asound/cards, but /proc/asound/pcm is empty and

amixer: Unable to find simple control 'Capture Mux',0
aplay: main:850: audio open error: No such file or directory

Tested on that board with v7.4-rc2 built with GCC 15.2 (first seen with GCC 16.2): the failure is there without this change; with it, playback, capture and the codec controls work again.

The root cause analysis, this change and the testing were done with the help of an AI coding assistant (Claude Code, Claude Opus 5.5).

Fixes 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")
Quoted 7 lines

---
4dd10fbbeb9c only reached mainline in v7.4-rc2 (not in broonie/sound yet), so this is based on v7.4-rc2.

The other devm_k[mz]alloc_flex() sites added by 4dd10fbbeb9c were checked
too: none of them fills a __counted_by() counter one entry at a time, so this one looks like the only user hit by the preset counter.

Quoted 8 lines

I guess a bugzilla entry that was reported recently is also related with this problem.
https://bugzilla.kernel.org/show_bug.cgi?id=222249

My first gut feeling was that it might be safer to revert the use of devm_kmalloc_flex() in that code, as it has a significant side-effect unlike other straight conversions. But, if this fix covers well, we can go with this fix, of course.

And it seems working for the bugzilla issue above, too.

Mark, could you pick this up quickly? It looks like a severe regression.

thanks,

Takashi

#3 lore raw
In reply to Takashi Iwai
Quoted 49 lines · Takashi Iwai

On Mon, 09 Nov 2026 11:25:20 +0100,
Emanuele Ghidoli wrote:

Quoted 45 lines · Emanuele Ghidoli

From: Emanuele Ghidoli <[email protected]>

Commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family") switched the snd_soc_pcm_runtime allocation in soc_new_pcm_runtime() to devm_kzalloc_flex(). components[] is annotated with __counted_by(num_components), so when the compiler provides __builtin_counted_by_ref() the allocation now also sets rtd->num_components to the number of allocated slots.

snd_soc_rtd_add_component() fills components[] one entry at a time and expects num_components to start at zero. Starting from the allocated size, for_each_rtd_components() stops at the first, still NULL, entry, so the runtime has no components: they are not probed, no controls or user-visible PCM devices are created and the card registers empty, without any error message. In addition, each added component is stored past the end of components[]. On the tested board these stores still land in the slack of the kmalloc bucket that devres rounds the allocation up to, so KASAN does not report them.

Reset num_components after the allocation.

Seen on a Colibri iMX7D (simple-audio-card, fsl-sai, sgtl5000): the card is listed in /proc/asound/cards, but /proc/asound/pcm is empty and

amixer: Unable to find simple control 'Capture Mux',0
aplay: main:850: audio open error: No such file or directory

Tested on that board with v7.4-rc2 built with GCC 15.2 (first seen with GCC 16.2): the failure is there without this change; with it, playback, capture and the codec controls work again.

The root cause analysis, this change and the testing were done with the help of an AI coding assistant (Claude Code, Claude Opus 5.5).

Fixes 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")

---
4dd10fbbeb9c only reached mainline in v7.4-rc2 (not in broonie/sound yet), so this is based on v7.4-rc2.

The other devm_k[mz]alloc_flex() sites added by 4dd10fbbeb9c were checked
too: none of them fills a __counted_by() counter one entry at a time, so this one looks like the only user hit by the preset counter.

Aggh, sorry for the problem, and thanks for checking the other cases.

Quoted 9 lines

I guess a bugzilla entry that was reported recently is also related with this problem.
https://bugzilla.kernel.org/show_bug.cgi?id=222249

My first gut feeling was that it might be safer to revert the use of devm_kmalloc_flex() in that code, as it has a significant side-effect unlike other straight conversions. But, if this fix covers well, we can go with this fix, of course.

The problem is the annotation, IMO, as the counter member doesn't track allocation size, it tracks usage.

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 60 lines · Kees Cook
Quoted 49 lines · Takashi Iwai

On Mon, 09 Nov 2026 11:25:20 +0100,
Emanuele Ghidoli wrote:

Quoted 45 lines · Emanuele Ghidoli

From: Emanuele Ghidoli <[email protected]>

Commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family") switched the snd_soc_pcm_runtime allocation in soc_new_pcm_runtime() to devm_kzalloc_flex(). components[] is annotated with __counted_by(num_components), so when the compiler provides __builtin_counted_by_ref() the allocation now also sets rtd->num_components to the number of allocated slots.

snd_soc_rtd_add_component() fills components[] one entry at a time and expects num_components to start at zero. Starting from the allocated size, for_each_rtd_components() stops at the first, still NULL, entry, so the runtime has no components: they are not probed, no controls or user-visible PCM devices are created and the card registers empty, without any error message. In addition, each added component is stored past the end of components[]. On the tested board these stores still land in the slack of the kmalloc bucket that devres rounds the allocation up to, so KASAN does not report them.

Reset num_components after the allocation.

Seen on a Colibri iMX7D (simple-audio-card, fsl-sai, sgtl5000): the card is listed in /proc/asound/cards, but /proc/asound/pcm is empty and

amixer: Unable to find simple control 'Capture Mux',0
aplay: main:850: audio open error: No such file or directory

Tested on that board with v7.4-rc2 built with GCC 15.2 (first seen with GCC 16.2): the failure is there without this change; with it, playback, capture and the codec controls work again.

The root cause analysis, this change and the testing were done with the help of an AI coding assistant (Claude Code, Claude Opus 5.5).

Fixes 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")

---
4dd10fbbeb9c only reached mainline in v7.4-rc2 (not in broonie/sound yet), so this is based on v7.4-rc2.

The other devm_k[mz]alloc_flex() sites added by 4dd10fbbeb9c were checked
too: none of them fills a __counted_by() counter one entry at a time, so this one looks like the only user hit by the preset counter.

Aggh, sorry for the problem, and thanks for checking the other cases.

Quoted 5 lines

I guess a bugzilla entry that was reported recently is also related with this problem.
https://bugzilla.kernel.org/show_bug.cgi?id=222249

Quoted 6 lines
Quoted 4 lines

My first gut feeling was that it might be safer to revert the use of devm_kmalloc_flex() in that code, as it has a significant side-effect unlike other straight conversions. But, if this fix covers well, we can go with this fix, of course.

The problem is the annotation, IMO, as the counter member doesn't track allocation size, it tracks usage.

For example, what keeps snd_soc_rtd_add_component() from overflowing that array? If you want to protect the array, add another member, say max_component, and use that for the counter, which gets set at allocation time and protects the array from indexing beyond the actual allocation size.

Signature · 1 line
Kees Cook
#5 lore raw
Quoted 7 lines · Kees Cook

On November 10, 2026 9:50:08 PM PST, Kees Cook <[email protected]> wrote:

For example, what keeps snd_soc_rtd_add_component() from overflowing that array? If you want to protect the array, add another member, say max_component, and use that for the counter, which gets set at allocation time and protects the array from indexing beyond the actual allocation size.

Hello Kees,

Mark has already applied the fix to for-7.4 (d00ba321499c). I agree that your proposal is far better, but the applied patch was meant as the minimal change to fix the regression.

On your question about what keeps snd_soc_rtd_add_component() from overflowing the array: nothing, and it already seems possible today.

From an LLM-assisted review of the code: the platform loop in snd_soc_add_pcm_runtime() adds every component that matches the platform dai_link_component, while components[] has one entry per platform. On mt8173, for example, the AFE registers three components on the same device and the front-end links use it as platform, so four components go into three entries. The extra store has gone unnoticed in the slab slack, but with max_components as the counter UBSAN_BOUNDS would report it.

I can't work on a follow-up. If someone picks it up, components[] probably needs to be sized by the matching components before the counter is switched to max_components.

Emanuele

#6 lore raw
Quoted 13 lines · Emanuele Ghidoli
Quoted 9 lines · Kees Cook

On November 10, 2026 9:50:08 PM PST, Kees Cook <[email protected]> wrote:

For example, what keeps snd_soc_rtd_add_component() from overflowing that array? If you want to protect the array, add another member, say max_component, and use that for the counter, which gets set at allocation time and protects the array from indexing beyond the actual allocation size.

Quoted 5 lines

Hello Kees,

Mark has already applied the fix to for-7.4 (d00ba321499c). I agree that your proposal is far better, but the applied patch was meant as the minimal change to fix the regression.

Yes, totally correct. :) I was just looking at it from a root-cause perspective. Each time we hit issues with counted_by I try to understand what the problem was, since the goal is to make it "easy to use". :P

Quoted 11 lines

On your question about what keeps snd_soc_rtd_add_component() from overflowing the array: nothing, and it already seems possible today.

From an LLM-assisted review of the code: the platform loop in snd_soc_add_pcm_runtime() adds every component that matches the platform dai_link_component, while components[] has one entry per platform. On mt8173, for example, the AFE registers three components on the same device and the front-end links use it as platform, so four components go into three entries. The extra store has gone unnoticed in the slab slack, but with max_components as the counter UBSAN_BOUNDS would report it.

Quoted 3 lines

I can't work on a follow-up. If someone picks it up, components[] probably needs to be sized by the matching components before the counter is switched to max_components.

Yeah, I think we need some better documentation about best practices for counted_by, as there is clearly 2 very distinct cases it's getting applied to, and the "usage count" case is the most fragile.

Signature · 1 line
Kees Cook
maintainer
#7 lore raw
In reply to Kees Cook
Quoted 5 lines · Kees Cook

For example, what keeps snd_soc_rtd_add_component() from overflowing that array? If you want to protect the array, add another member, say max_component, and use that for the counter, which gets set at allocation time and protects the array from indexing beyond the actual allocation size.

We could do that, and it'd certainly be good to do so, but anything that's in a position to do anything here will already have sufficient access to the system that it can just directly cause issues.

Please fix your mail client to word wrap within paragraphs at something substantially less than 80 columns. Doing this makes your messages much easier to read and reply to.

#8 lore raw
Quoted 3 lines · Mark Brown

Please fix your mail client to word wrap within paragraphs at something substantially less than 80 columns. Doing this makes your messages much easier to read and reply to.

Yeah, this is a standing problem with K9-Mail, which is what I have on my phone. I only use it when I'm AFK but I need to reply to something urgently, such as when I break what looked like basically all soc sound :(

All the other android clients don't really seem to measure up, except for this particular gap. :( I'll try to remember to manually wrap lines in the future.

Signature · 1 line
Kees Cook
Tested-by on v1 1/1
#9 lore raw
In reply to Emanuele Ghidoli
Quoted 1 line · Emanuele Ghidoli

Reset num_components after the allocation.

The same regression hits Intel SOF/HDA: on a ThinkPad E16 Gen3 (Arrow Lake, Core Ultra 7 255H, Conexant SN6140) no internal card is registered on v7.4-rc2, with

skl_hda_dsp_generic skl_hda_dsp_generic: ASoC: Failed to add route ... Analog Playback and Capture: fail to add hda routes, ret -19

This also looks like the regression reported for Meteor Lake here:
https://lore.kernel.org/all/mAZY9HZuXAXUf7zv8tqXquMtquf7iW2CIZB7WhU-SnwWp-Rpr0e8yixTsb3lAeQsO64PwwWlOao8aMjQ4hTKOQ6grCiNmaWZmFRXMdPICD8=@proton.me/

With this patch on top of v7.4-rc2 the card is registered again: the topology loads, and speaker, mics and HDMI outputs are back, with no warnings in the log.

Best regards,
Jan

Tested-by on v1 1/1
#10 lore raw
In reply to Emanuele Ghidoli

Hi,

thanks for the patch.

Am Montag, 9. November 2026, 11:25:20 Mitteleuropäische Normalzeit schrieb Emanuele Ghidoli:

Quoted 32 lines

From: Emanuele Ghidoli <[email protected]>

Commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family") switched the snd_soc_pcm_runtime allocation in soc_new_pcm_runtime() to devm_kzalloc_flex(). components[] is annotated with __counted_by(num_components), so when the compiler provides __builtin_counted_by_ref() the allocation now also sets rtd->num_components to the number of allocated slots.

snd_soc_rtd_add_component() fills components[] one entry at a time and expects num_components to start at zero. Starting from the allocated size, for_each_rtd_components() stops at the first, still NULL, entry, so the runtime has no components: they are not probed, no controls or user-visible PCM devices are created and the card registers empty, without any error message. In addition, each added component is stored past the end of components[]. On the tested board these stores still land in the slack of the kmalloc bucket that devres rounds the allocation up to, so KASAN does not report them.

Reset num_components after the allocation.

Seen on a Colibri iMX7D (simple-audio-card, fsl-sai, sgtl5000): the card is listed in /proc/asound/cards, but /proc/asound/pcm is empty and

amixer: Unable to find simple control 'Capture Mux',0
aplay: main:850: audio open error: No such file or directory

Tested on that board with v7.4-rc2 built with GCC 15.2 (first seen with GCC 16.2): the failure is there without this change; with it, playback, capture and the codec controls work again.

Quoted 5 lines

The root cause analysis, this change and the testing were done with the help of an AI coding assistant (Claude Code, Claude Opus 5.5).

Fixes 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")

This fixes TQMa8MPxL/MBa8MPxL as well which is also affected by commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family").
FWIW:

Quoted 31 lines

---
4dd10fbbeb9c only reached mainline in v7.4-rc2 (not in broonie/sound yet), so this is based on v7.4-rc2.

The other devm_k[mz]alloc_flex() sites added by 4dd10fbbeb9c were checked
too: none of them fills a __counted_by() counter one entry at a time, so this one looks like the only user hit by the preset counter.

 sound/soc/soc-core.c | 6 ++++++
 1 file changed, 6 insertions(+)
sound/soc/soc-core.c +6 -0
index 68b54e509b65..06a1d5718997 100644
@@ -387,6 +387,12 @@ static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
 		device_unregister(dev);
 		return NULL;
 	}
+	/*
+	 * devm_kzalloc_flex() presets the __counted_by() counter to the
+	 * allocated size when the compiler has __builtin_counted_by_ref(),
+	 * but snd_soc_rtd_add_component() expects it to start from zero.
+	 */
+	rtd->num_components = 0;
 
 	rtd->dev = dev;
 	INIT_LIST_HEAD(&rtd->list);

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 4 lines
TQ-Systems GmbH | Mühlstraße 2, Gut Delling | 82229 Seefeld, Germany
Amtsgericht München, HRB 105018
Geschäftsführer: Detlef Schneider, Rüdiger Stahl, Stefan Schneider
http://www.tq-group.com/
Tested-by on v1 1/1
#11 lore raw
In reply to Emanuele Ghidoli
Quoted 32 lines

From: Emanuele Ghidoli <[email protected]>
Sent: Monday, November 9, 2026 12:25 PM

Commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family") switched the snd_soc_pcm_runtime allocation in soc_new_pcm_runtime() to devm_kzalloc_flex(). components[] is annotated with __counted_by(num_components), so when the compiler provides __builtin_counted_by_ref() the allocation now also sets rtd->num_components to the number of allocated slots.

snd_soc_rtd_add_component() fills components[] one entry at a time and expects num_components to start at zero. Starting from the allocated size, for_each_rtd_components() stops at the first, still NULL, entry, so the runtime has no components: they are not probed, no controls or user-visible PCM devices are created and the card registers empty, without any error message. In addition, each added component is stored past the end of components[]. On the tested board these stores still land in the slack of the kmalloc bucket that devres rounds the allocation up to, so KASAN does not report them.

Reset num_components after the allocation.

Seen on a Colibri iMX7D (simple-audio-card, fsl-sai, sgtl5000): the card is listed in /proc/asound/cards, but /proc/asound/pcm is empty and

amixer: Unable to find simple control 'Capture Mux',0
aplay: main:850: audio open error: No such file or directory

Tested on that board with v7.4-rc2 built with GCC 15.2 (first seen with GCC 16.2): the failure is there without this change; with it, playback, capture and the codec controls work again.

Quoted 5 lines

The root cause analysis, this change and the testing were done with the help of an AI coding assistant (Claude Code, Claude Opus 5.5).

Fixes 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family")

We also hit the same bug on several of our SoCs, although with different symptoms.

Unable to handle kernel NULL pointer dereference at virtual address 0000000000000060
Hardware name: HopeRun HiHope RZ/G2H with sub board (DT)
Workqueue: events_unbound deferred_probe_work_func
pc : mutex_lock+0x20/0x4c
lr : snd_soc_dapm_add_routes+0x28/0xa0
Call trace:
 mutex_lock+0x20/0x4c (P)
 hdmi_dai_probe+0xa4/0x1d0
 snd_soc_pcm_dai_probe+0x64/0xdc
 snd_soc_card_bind+0x568/0xb60
 snd_soc_card_bind_call+0x64/0xd0
 snd_soc_register_card+0xcc/0x100
 devm_snd_soc_register_card+0x14/0x20
 audio_graph_parse_of+0x228/0x2dc
 graph_probe+0x7c/0x94

With this patch applied on top of next-20261110, RZ/G2H, RZ/G2M, RZ/G2N, RZ/G2L and RZ/V2L no longer hit a NULL pointer dereference in hdmi_dai_probe() and the sound cards now register properly.

maintainer
#12 lore raw
In reply to Emanuele Ghidoli
Quoted 1 line · Emanuele Ghidoli

ASoC: soc-core: reset rtd->num_components after allocation

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.4

Thanks!

[1/1] ASoC: soc-core: reset rtd->num_components after allocation

      https://git.kernel.org/broonie/sound/c/d00ba321499c

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark