Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v3 0/2] component: fix match add failure handling
@ 2026-12-06  2:48 Zhiqi Zhang
  2026-12-06  2:48 ` [PATCH v3 1/2] component: release match data on add failure Zhiqi Zhang
  2026-12-06  2:48 ` [PATCH v3 2/2] ASoC: codecs: wcd938x: deselect mux on probe failures Zhiqi Zhang
  0 siblings, 2 replies; 4+ messages in thread
From: Zhiqi Zhang @ 2026-12-06  2:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman, rafael, Mark Brown, Srinivas Kandagatla
  Cc: Danilo Krummrich, Johan Hovold, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, dri-devel, driver-core, linux-sound, linux-arm-msm,
	Zhiqi Zhang

Hi,

v2 fixed component_match_add_release() allocation failures in individual
ASoC codec helpers. Review pointed out that the underlying problem is in
the component helper: component_match_add_release() has a void interface,
so callers that pass owned data through wrappers such as
drm_of_component_match_add() cannot reliably drop it on failure. The same
error state can later be passed to component_master_add_with_match(),
which dereferences match->num.

This v3 moves the match-data cleanup and ERR_PTR handling into the
component core. The wcd938x us/euro mux cleanup that was previously
bundled with the match allocation fix is kept as a separate patch with
its own Fixes tag.

Changes since v2:
- Replace the per-codec match allocation checks with one component core
  fix.
- Split the wcd938x mux cleanup into a separate patch.
- Route wcd938x_populate_dt_data() failures through mux deselect as well.

v2: https://lore.kernel.org/r/20261204125837.81419-1-zhiqizhangg@gmail.com

Zhiqi Zhang (2):
  component: release match data on add failure
  ASoC: codecs: wcd938x: deselect mux on probe failures

 drivers/base/component.c   | 17 ++++++++++++++++-
 sound/soc/codecs/wcd938x.c | 24 +++++++++++++++++-------
 2 files changed, 33 insertions(+), 8 deletions(-)

-- 
2.54.0 (Apple Git-157)


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

* [PATCH v3 1/2] component: release match data on add failure
  2026-12-06  2:48 [PATCH v3 0/2] component: fix match add failure handling Zhiqi Zhang
@ 2026-12-06  2:48 ` Zhiqi Zhang
  2026-12-06  3:01   ` sashiko-bot
  2026-12-06  2:48 ` [PATCH v3 2/2] ASoC: codecs: wcd938x: deselect mux on probe failures Zhiqi Zhang
  1 sibling, 1 reply; 4+ messages in thread
From: Zhiqi Zhang @ 2026-12-06  2:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman, rafael, Mark Brown, Srinivas Kandagatla
  Cc: Danilo Krummrich, Johan Hovold, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, dri-devel, driver-core, linux-sound, linux-arm-msm,
	Zhiqi Zhang, stable

component_match_add_release() transfers ownership of compare_data to the
component core so the release callback can drop it when the match list is
destroyed.

If __component_match_add() fails to allocate the match list or grow its
match array, no entry is recorded and the release callback is never run.
Later add calls after matchptr already holds an ERR_PTR have the same
problem, because callers batch matches through void helpers and have no
return value to act on.

The stored ERR_PTR can also be passed to component_master_add_with_match(),
which dereferences match->num before returning an error.

Release the compare data on add failure and make
component_master_add_with_match() return the stored error when it receives
an ERR_PTR match list.

Fixes: ce657b1cddf1 ("component: add support for releasing match data")
Cc: stable@vger.kernel.org
Signed-off-by: Zhiqi Zhang <zhiqizhangg@gmail.com>
---
 drivers/base/component.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/drivers/base/component.c b/drivers/base/component.c
index 655d68deb5..6b84ce9581 100644
--- a/drivers/base/component.c
+++ b/drivers/base/component.c
@@ -356,6 +356,14 @@ static void devm_component_match_release(struct device *parent, void *res)
 	kfree(match->compare);
 }
 
+static void component_match_release(struct device *parent,
+				    void (*release)(struct device *, void *),
+				    void *compare_data)
+{
+	if (release)
+		release(parent, compare_data);
+}
+
 static int component_match_realloc(struct component_match *match, size_t num)
 {
 	struct component_match_array *new;
@@ -387,13 +395,16 @@ static void __component_match_add(struct device *parent,
 {
 	struct component_match *match = *matchptr;
 
-	if (IS_ERR(match))
+	if (IS_ERR(match)) {
+		component_match_release(parent, release, compare_data);
 		return;
+	}
 
 	if (!match) {
 		match = devres_alloc(devm_component_match_release,
 				     sizeof(*match), GFP_KERNEL);
 		if (!match) {
+			component_match_release(parent, release, compare_data);
 			*matchptr = ERR_PTR(-ENOMEM);
 			return;
 		}
@@ -409,6 +420,7 @@ static void __component_match_add(struct device *parent,
 
 		ret = component_match_realloc(match, new_size);
 		if (ret) {
+			component_match_release(parent, release, compare_data);
 			*matchptr = ERR_PTR(ret);
 			return;
 		}
@@ -516,6 +528,9 @@ int component_master_add_with_match(struct device *parent,
 	struct aggregate_device *adev;
 	int ret;
 
+	if (IS_ERR(match))
+		return PTR_ERR(match);
+
 	/* Reallocate the match array for its true size */
 	ret = component_match_realloc(match, match->num);
 	if (ret)
-- 
2.54.0 (Apple Git-157)


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

* [PATCH v3 2/2] ASoC: codecs: wcd938x: deselect mux on probe failures
  2026-12-06  2:48 [PATCH v3 0/2] component: fix match add failure handling Zhiqi Zhang
  2026-12-06  2:48 ` [PATCH v3 1/2] component: release match data on add failure Zhiqi Zhang
@ 2026-12-06  2:48 ` Zhiqi Zhang
  1 sibling, 0 replies; 4+ messages in thread
From: Zhiqi Zhang @ 2026-12-06  2:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman, rafael, Mark Brown, Srinivas Kandagatla
  Cc: Danilo Krummrich, Johan Hovold, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, dri-devel, driver-core, linux-sound, linux-arm-msm,
	Zhiqi Zhang, stable

wcd938x_populate_dt_data() selects the optional us/euro mux and records
mux_setup_done. If a later probe step fails, wcd938x_probe() returns
without deselecting the mux.

Route probe failures through a common mux cleanup helper once the private
data is available. Use the same helper for jack reconfiguration and driver
remove so mux_setup_done stays consistent in all deselect paths.

Fixes: eec611d26f84 ("ASoC: codecs: wcd938x: add mux control support for hp audio mux")
Cc: stable@vger.kernel.org
Signed-off-by: Zhiqi Zhang <zhiqizhangg@gmail.com>
---
 sound/soc/codecs/wcd938x.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/sound/soc/codecs/wcd938x.c b/sound/soc/codecs/wcd938x.c
index 1c4833584b..0ceec20d26 100644
--- a/sound/soc/codecs/wcd938x.c
+++ b/sound/soc/codecs/wcd938x.c
@@ -3167,6 +3167,14 @@ static const struct snd_soc_component_driver soc_codec_dev_wcd938x = {
 	.endianness = 1,
 };
 
+static void wcd938x_mux_deselect(struct wcd938x_priv *wcd938x)
+{
+	if (wcd938x->us_euro_mux && wcd938x->mux_setup_done) {
+		mux_control_deselect(wcd938x->us_euro_mux);
+		wcd938x->mux_setup_done = false;
+	}
+}
+
 static bool wcd938x_swap_gnd_mic(struct snd_soc_component *component)
 {
 	struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
@@ -3174,8 +3182,7 @@ static bool wcd938x_swap_gnd_mic(struct snd_soc_component *component)
 	int ret;
 
 	if (wcd938x->us_euro_mux) {
-		if (wcd938x->mux_setup_done)
-			mux_control_deselect(wcd938x->us_euro_mux);
+		wcd938x_mux_deselect(wcd938x);
 
 		ret = mux_control_try_select(wcd938x->us_euro_mux, !wcd938x->mux_state);
 		if (ret) {
@@ -3487,17 +3494,17 @@ static int wcd938x_probe(struct platform_device *pdev)
 
 	ret = wcd938x_populate_dt_data(wcd938x, dev);
 	if (ret)
-		return ret;
+		goto err_mux_deselect;
 
 	ret = wcd938x_add_slave_components(wcd938x, dev, &match);
 	if (ret)
-		return ret;
+		goto err_mux_deselect;
 
 	wcd938x_reset(wcd938x);
 
 	ret = component_master_add_with_match(dev, &wcd938x_comp_ops, match);
 	if (ret)
-		return ret;
+		goto err_mux_deselect;
 
 	pm_runtime_set_autosuspend_delay(dev, 1000);
 	pm_runtime_use_autosuspend(dev);
@@ -3507,6 +3514,10 @@ static int wcd938x_probe(struct platform_device *pdev)
 	pm_runtime_idle(dev);
 
 	return 0;
+
+err_mux_deselect:
+	wcd938x_mux_deselect(wcd938x);
+	return ret;
 }
 
 static void wcd938x_remove(struct platform_device *pdev)
@@ -3520,8 +3531,7 @@ static void wcd938x_remove(struct platform_device *pdev)
 	pm_runtime_set_suspended(dev);
 	pm_runtime_dont_use_autosuspend(dev);
 
-	if (wcd938x->us_euro_mux && wcd938x->mux_setup_done)
-		mux_control_deselect(wcd938x->us_euro_mux);
+	wcd938x_mux_deselect(wcd938x);
 }
 
 #if defined(CONFIG_OF)
-- 
2.54.0 (Apple Git-157)


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

* Re: [PATCH v3 1/2] component: release match data on add failure
  2026-12-06  2:48 ` [PATCH v3 1/2] component: release match data on add failure Zhiqi Zhang
@ 2026-12-06  3:01   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-06  3:01 UTC (permalink / raw)
  To: Zhiqi Zhang; +Cc: dri-devel, linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] component: double of_node_put() in ASoC codecs on match add
  failure
--

commit 0683b7fafbae8a82c09ea4152d6cf8bee316a38f
Author: Zhiqi Zhang <zhiqizhangg@gmail.com>

component: release match data on add failure

This commit invokes the match release callback in __component_match_add()
when allocation fails or *matchptr already holds an ERR_PTR, and returns
that error from component_master_add_with_match().

> diff --git a/drivers/base/component.c b/drivers/base/component.c
> index 655d68deb590c..6b84ce958140d 100644
> --- a/drivers/base/component.c
> +++ b/drivers/base/component.c

[ ... ]

> @@ -387,13 +395,16 @@ static void __component_match_add(struct device *parent,
>  {
>  	struct component_match *match = *matchptr;
>  
> -	if (IS_ERR(match))
> +	if (IS_ERR(match)) {
> +		component_match_release(parent, release, compare_data);
>  		return;
> +	}
>  
>  	if (!match) {
>  		match = devres_alloc(devm_component_match_release,
>  				     sizeof(*match), GFP_KERNEL);
>  		if (!match) {
> +			component_match_release(parent, release, compare_data);
>  			*matchptr = ERR_PTR(-ENOMEM);
>  			return;
>  		}
> @@ -409,6 +420,7 @@ static void __component_match_add(struct device *parent,
>  
>  		ret = component_match_realloc(match, new_size);
>  		if (ret) {
> +			component_match_release(parent, release, compare_data);
>  			*matchptr = ERR_PTR(ret);
>  			return;
>  		}

[Severity: Medium]
Can this cause a double of_node_put() in existing callers that drop their
device_node reference when *matchptr becomes an ERR_PTR?

Previously, __component_match_add() left compare_data to the caller on
allocation failure.  Three ASoC codec drivers rely on that and still drop
their rxnode/txnode reference on IS_ERR(*matchptr): pm4125.c, wcd937x.c,
and wcd939x.c.

sound/soc/codecs/pm4125.c:pm4125_add_slave_components() {
    ...
	component_match_add_release(dev, matchptr, component_release_of, component_compare_of,
				    pm4125->rxnode);
	if (IS_ERR(*matchptr)) {
		ret = PTR_ERR(*matchptr);
		of_node_put(pm4125->rxnode);
		pm4125->rxnode = NULL;
		return ret;
	}
    ...
}

When devres_alloc() or component_match_realloc() fails,
__component_match_add() now drops the single of_parse_phandle() reference
via component_release_of(), and the driver then calls of_node_put() again,
causing a refcount underflow and use-after-free under CONFIG_OF_DYNAMIC.

Updating those three callers (pm4125_add_slave_components(),
wcd937x_add_slave_components(), and wcd939x_add_slave_components()) along
with the component_match_add_release() kernel-doc would avoid this
regression.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206024833.37825-1-zhiqizhangg@gmail.com?part=1

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  2:48 [PATCH v3 0/2] component: fix match add failure handling Zhiqi Zhang
2026-12-06  2:48 ` [PATCH v3 1/2] component: release match data on add failure Zhiqi Zhang
2026-12-06  3:01   ` sashiko-bot
2026-12-06  2:48 ` [PATCH v3 2/2] ASoC: codecs: wcd938x: deselect mux on probe failures Zhiqi Zhang

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