Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ASoC: rt5645: stop jack detection on component remove
@ 2026-12-03  8:53 Yaohan Li
  2026-12-03  9:23 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Yaohan Li @ 2026-12-03  8:53 UTC (permalink / raw)
  To: Oder Chiou, Liam Girdwood, Mark Brown
  Cc: Jaroslav Kysela, Takashi Iwai, linux-sound, linux-kernel

rt5645_i2c_probe() requests the codec interrupt and initializes
btn_check_timer and jack_detect_work, whereas rt5645->component and the
card-owned snd_soc_jack pointers only live while the ASoC component is
bound.  If the sound card is unbound while the I2C codec stays bound,
rt5645_remove() leaves rt5645->component, the jack pointers, the button
timer, and jack_detect_work active, so a pending or IRQ-triggered
rt5645_jack_detect_work() can dereference the freed card or jack state.

Several jack-detection paths also bypass or split jd_mutex:
rt5645_probe() sets rt5645->component before eq_param allocation
succeeds, rt5645_set_jack_detect() updates the jack pointers without
taking jd_mutex or checking rt5645->component, rt5645_jack_detect_work()
reads rt5645->component and the jack pointers outside jd_mutex (and holds
jd_mutex across snd_soc_jack_report() when jd_mode == 0), and
rt5645_sys_resume() calls rt5645_jack_detect_work() directly off-queue
where cancel_delayed_work_sync() cannot wait for it.

Guard rt5645->component and the jack pointers under jd_mutex in probe,
rt5645_set_jack_detect(), and rt5645_jack_detect_work(), while
snapshotting the jack pointers and calling snd_soc_jack_report() outside
jd_mutex across all jd_mode paths so jack notifiers cannot deadlock if
they re-enter the setter.  In rt5645_sys_resume(), queue jack_detect_work
under jd_mutex and flush_delayed_work() outside jd_mutex to preserve
synchronous post-resume CBJ detection from commit aa98697c7dbd ("ASoC:
rt5645: improve the depop sequences of CBJ detection") while allowing
rt5645_remove() to drain it alongside btn_check_timer.

Fixes: f3fa1bbd836a ("ASoC: rt5645: Add headset detect function")
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Based on v7.4-rc5 (fe6ff8d5d675); applies cleanly on broonie/sound.git
for-linus and for-next, and does not conflict with my separate I2C
probe-error patch ("ASoC: rt5645: stop timer and delayed works on probe
error").

I don't have physical rt5645/rt5650 hardware, so I tested this in QEMU
(x86_64, KASAN + PROVE_LOCKING) with the in-tree snd-soc-rt5645.ko on a
virtual I2C bus, virtual IRQ, and virtual ASoC card covering:
- card unbind while I2C stays bound in both jd-mode = 0 and jd-mode = 1,
  mid-work I2C stall under RT5650 button polling, and concurrent
  rt5645_sys_resume() vs rt5645_remove() (all hit KASAN slab-UAF on the
  unmodified driver and pass cleanly with this patch);
- synchronous post-resume CBJ completion, setter vs remove races, jack
  notifier synchronous re-entry into rt5645_set_jack_detect() (0 lockdep
  warnings), Card 2 rebind on the same I2C codec, and both teardown orders.
The lock-free snd_soc_jack_report() after snapshotting under jd_mutex relies
on the standard ASoC contract that card-owned snd_soc_jack objects remain
valid until component remove runs.  Also build-tested on x86_64 and arm64
with W=1 (0 warnings).

AI assistance: Claude and Gemini helped analyze the component/resume races,
build the QEMU test module, and draft the patch and changelog.  I reviewed
the locking, test logs, and final diff myself.

 sound/soc/codecs/rt5645.c | 100 ++++++++++++++++++++++++++------------
 1 file changed, 69 insertions(+), 31 deletions(-)

diff --git a/sound/soc/codecs/rt5645.c b/sound/soc/codecs/rt5645.c
index e14b66d3118da..92a54e66f14c3 100644
--- a/sound/soc/codecs/rt5645.c
+++ b/sound/soc/codecs/rt5645.c
@@ -3278,19 +3278,28 @@ int rt5645_set_jack_detect(struct snd_soc_component *component,
 {
 	struct rt5645_priv *rt5645 = snd_soc_component_get_drvdata(component);
 
-	rt5645->hp_jack = hp_jack;
-	rt5645->mic_jack = mic_jack;
-	rt5645->btn_jack = btn_jack;
-	if (rt5645->btn_jack && rt5645->codec_type == CODEC_TYPE_RT5650) {
-		rt5645->en_button_func = true;
-		regmap_update_bits(rt5645->regmap, RT5645_GPIO_CTRL1,
-				RT5645_GP1_PIN_IRQ, RT5645_GP1_PIN_IRQ);
-		regmap_update_bits(rt5645->regmap, RT5645_GEN_CTRL1,
-				RT5645_DIG_GATE_CTRL, RT5645_DIG_GATE_CTRL);
-		regmap_update_bits(rt5645->regmap, RT5645_DEPOP_M1,
-				RT5645_HP_CB_MASK, RT5645_HP_CB_PU);
+	scoped_guard(mutex, &rt5645->jd_mutex) {
+		if (!rt5645->component)
+			return -ENODEV;
+
+		rt5645->hp_jack = hp_jack;
+		rt5645->mic_jack = mic_jack;
+		rt5645->btn_jack = btn_jack;
+		if (rt5645->btn_jack && rt5645->codec_type == CODEC_TYPE_RT5650) {
+			rt5645->en_button_func = true;
+			regmap_update_bits(rt5645->regmap, RT5645_GPIO_CTRL1,
+					   RT5645_GP1_PIN_IRQ, RT5645_GP1_PIN_IRQ);
+			regmap_update_bits(rt5645->regmap, RT5645_GEN_CTRL1,
+					   RT5645_DIG_GATE_CTRL, RT5645_DIG_GATE_CTRL);
+			regmap_update_bits(rt5645->regmap, RT5645_DEPOP_M1,
+					   RT5645_HP_CB_MASK, RT5645_HP_CB_PU);
+		} else {
+			rt5645->en_button_func = false;
+		}
+
+		if (hp_jack || mic_jack || btn_jack)
+			rt5645_irq(0, rt5645);
 	}
-	rt5645_irq(0, rt5645);
 
 	return 0;
 }
@@ -3320,12 +3329,13 @@ static void rt5645_jack_detect_work(struct work_struct *work)
 {
 	struct rt5645_priv *rt5645 =
 		container_of(work, struct rt5645_priv, jack_detect_work.work);
+	struct snd_soc_jack *hp_jack, *mic_jack, *btn_jack = NULL;
 	int val, btn_type, gpio_state = 0, report = 0;
 
-	if (!rt5645->component)
-		return;
-
 	scoped_guard(mutex, &rt5645->jd_mutex) {
+		if (!rt5645->component)
+			return;
+
 		switch (rt5645->pdata.jd_mode) {
 		case 0: /* Not using rt5645 JD */
 			if (rt5645->gpiod_hp_det) {
@@ -3336,11 +3346,7 @@ static void rt5645_jack_detect_work(struct work_struct *work)
 					gpio_state);
 				report = rt5645_jack_detect(rt5645->component, gpio_state);
 			}
-			snd_soc_jack_report(rt5645->hp_jack,
-					    report, SND_JACK_HEADPHONE);
-			snd_soc_jack_report(rt5645->mic_jack,
-					    report, SND_JACK_MICROPHONE);
-			return;
+			goto report_jack;
 		case 4:
 			val = snd_soc_component_read(rt5645->component, RT5645_A_JD_CTRL1) & 0x0020;
 			break;
@@ -3407,14 +3413,20 @@ static void rt5645_jack_detect_work(struct work_struct *work)
 						      RT5645_INT_IRQ_ST, 0x1, 0x0);
 			rt5645_jack_detect(rt5645->component, 0);
 		}
+
+		if (rt5645->en_button_func)
+			btn_jack = rt5645->btn_jack;
+report_jack:
+		hp_jack = rt5645->hp_jack;
+		mic_jack = rt5645->mic_jack;
 	}
 
-	snd_soc_jack_report(rt5645->hp_jack, report, SND_JACK_HEADPHONE);
-	snd_soc_jack_report(rt5645->mic_jack, report, SND_JACK_MICROPHONE);
-	if (rt5645->en_button_func)
-		snd_soc_jack_report(rt5645->btn_jack,
-			report, SND_JACK_BTN_0 | SND_JACK_BTN_1 |
-				SND_JACK_BTN_2 | SND_JACK_BTN_3);
+	snd_soc_jack_report(hp_jack, report, SND_JACK_HEADPHONE);
+	snd_soc_jack_report(mic_jack, report, SND_JACK_MICROPHONE);
+	if (btn_jack)
+		snd_soc_jack_report(btn_jack,
+				    report, SND_JACK_BTN_0 | SND_JACK_BTN_1 |
+				    SND_JACK_BTN_2 | SND_JACK_BTN_3);
 }
 
 static void rt5645_rcclock_work(struct work_struct *work)
@@ -3450,8 +3462,6 @@ static int rt5645_probe(struct snd_soc_component *component)
 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
 	struct rt5645_priv *rt5645 = snd_soc_component_get_drvdata(component);
 
-	rt5645->component = component;
-
 	switch (rt5645->codec_type) {
 	case CODEC_TYPE_RT5645:
 		snd_soc_dapm_new_controls(dapm,
@@ -3493,6 +3503,9 @@ static int rt5645_probe(struct snd_soc_component *component)
 	if (!rt5645->eq_param)
 		return -ENOMEM;
 
+	scoped_guard(mutex, &rt5645->jd_mutex)
+		rt5645->component = component;
+
 	/* no machine driver to call rt5645_set_jack_detect(), so detect here */
 	if (!rt5645->pdata.jd_mode && rt5645->gpiod_hp_det)
 		rt5645_irq(0, rt5645);
@@ -3504,6 +3517,23 @@ static void rt5645_remove(struct snd_soc_component *component)
 {
 	struct rt5645_priv *rt5645 = snd_soc_component_get_drvdata(component);
 
+	scoped_guard(mutex, &rt5645->jd_mutex) {
+		rt5645->component = NULL;
+		rt5645->hp_jack = NULL;
+		rt5645->mic_jack = NULL;
+		rt5645->btn_jack = NULL;
+		rt5645->en_button_func = false;
+		rt5645->jack_type = 0;
+	}
+
+	/*
+	 * Clearing rt5645->component under jd_mutex prevents jack_detect_work
+	 * from rearming btn_check_timer. Use timer_delete_sync() rather than
+	 * timer_shutdown_sync() so a subsequent component rebind can reuse it.
+	 */
+	timer_delete_sync(&rt5645->btn_check_timer);
+	cancel_delayed_work_sync(&rt5645->jack_detect_work);
+
 	rt5645_reset(component);
 
 	kfree(rt5645->eq_param);
@@ -4342,6 +4372,7 @@ static int rt5645_sys_suspend(struct device *dev)
 static int rt5645_sys_resume(struct device *dev)
 {
 	struct rt5645_priv *rt5645 = dev_get_drvdata(dev);
+	bool detect = false;
 	int ret;
 
 	regcache_cache_only(rt5645->regmap, false);
@@ -4352,10 +4383,17 @@ static int rt5645_sys_resume(struct device *dev)
 		return ret;
 	}
 
-	if (rt5645->hp_jack) {
-		rt5645->jack_type = 0;
-		rt5645_jack_detect_work(&rt5645->jack_detect_work.work);
+	scoped_guard(mutex, &rt5645->jd_mutex) {
+		if (rt5645->component && rt5645->hp_jack) {
+			rt5645->jack_type = 0;
+			mod_delayed_work(system_power_efficient_wq,
+					 &rt5645->jack_detect_work, 0);
+			detect = true;
+		}
 	}
+	if (detect)
+		flush_delayed_work(&rt5645->jack_detect_work);
+
 	return 0;
 }
 
-- 
2.34.1


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

* Re: [PATCH] ASoC: rt5645: stop jack detection on component remove
  2026-12-03  8:53 [PATCH] ASoC: rt5645: stop jack detection on component remove Yaohan Li
@ 2026-12-03  9:23 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03  9:23 UTC (permalink / raw)
  To: Yaohan Li; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] ASoC: rt5645: data race on en_button_func in
  rt5645_set_bias_level()
--

commit 99a615fa9435b7376ff9a3e1e4c3496127c1c760
Author: Yaohan Li <shunsheng1218@gmail.com>

ASoC: rt5645: stop jack detection on component remove

This patch serializes access to rt5645->component and the jack pointers
under jd_mutex, clears them and stops btn_check_timer and jack_detect_work
in rt5645_remove(), and moves snd_soc_jack_report() outside jd_mutex.  It
also queues and flushes jack_detect_work in rt5645_sys_resume() instead of
calling the work handler directly.

> diff --git a/sound/soc/codecs/rt5645.c b/sound/soc/codecs/rt5645.c
> index e14b66d3118da..92a54e66f14c3 100644
> --- a/sound/soc/codecs/rt5645.c
> +++ b/sound/soc/codecs/rt5645.c
> @@ -3278,19 +3278,28 @@ int rt5645_set_jack_detect(struct snd_soc_component *component,
>  {
>  	struct rt5645_priv *rt5645 = snd_soc_component_get_drvdata(component);
>  
> -	rt5645->hp_jack = hp_jack;
> -	rt5645->mic_jack = mic_jack;
> -	rt5645->btn_jack = btn_jack;
> -	if (rt5645->btn_jack && rt5645->codec_type == CODEC_TYPE_RT5650) {
> -		rt5645->en_button_func = true;
> -		regmap_update_bits(rt5645->regmap, RT5645_GPIO_CTRL1,
> -				RT5645_GP1_PIN_IRQ, RT5645_GP1_PIN_IRQ);
> -		regmap_update_bits(rt5645->regmap, RT5645_GEN_CTRL1,
> -				RT5645_DIG_GATE_CTRL, RT5645_DIG_GATE_CTRL);
> -		regmap_update_bits(rt5645->regmap, RT5645_DEPOP_M1,
> -				RT5645_HP_CB_MASK, RT5645_HP_CB_PU);
> +	scoped_guard(mutex, &rt5645->jd_mutex) {
> +		if (!rt5645->component)
> +			return -ENODEV;
> +
> +		rt5645->hp_jack = hp_jack;
> +		rt5645->mic_jack = mic_jack;
> +		rt5645->btn_jack = btn_jack;
> +		if (rt5645->btn_jack && rt5645->codec_type == CODEC_TYPE_RT5650) {
> +			rt5645->en_button_func = true;
> +			regmap_update_bits(rt5645->regmap, RT5645_GPIO_CTRL1,
> +					   RT5645_GP1_PIN_IRQ, RT5645_GP1_PIN_IRQ);
> +			regmap_update_bits(rt5645->regmap, RT5645_GEN_CTRL1,
> +					   RT5645_DIG_GATE_CTRL, RT5645_DIG_GATE_CTRL);
> +			regmap_update_bits(rt5645->regmap, RT5645_DEPOP_M1,
> +					   RT5645_HP_CB_MASK, RT5645_HP_CB_PU);
> +		} else {
> +			rt5645->en_button_func = false;
> +		}

[Severity: Low]
Can this write to rt5645->en_button_func race with rt5645_set_bias_level()?

During card teardown, soc_cleanup_card_resources() calls snd_soc_link_exit()
before snd_soc_flush_all_delayed_work().  On RT5650 boards whose machine
driver clears the jack in dai_link->exit() (such as mt8189-nau8825) and
leaves playback pmdown_time enabled, snd_soc_close_delayed_work() can run
concurrently under card->dapm_mutex:

CPU0                                   CPU1
soc_cleanup_card_resources()
  snd_soc_dapm_shutdown()
  snd_soc_link_exit()                  snd_soc_close_delayed_work()
    mt8189_headset_codec_exit()          snd_soc_dapm_stream_event()
      rt5645_set_jack_detect()             dapm_power_widgets()
        lock(&rt5645->jd_mutex)              rt5645_set_bias_level()
        rt5645->en_button_func = false         if (rt5645->en_button_func)
                                                 <-- data race

If rt5645_jack_detect_work() dirties forced supply pins between
snd_soc_dapm_shutdown() and snd_soc_link_exit(), dapm_power_widgets()
transitions the codec back to SND_SOC_BIAS_STANDBY and reads
rt5645->en_button_func without holding jd_mutex.

This is a KCSAN data race, though reading a stale true value only queues
jack_detect_work redundantly before rt5645_remove() cancels it.  Since
rt5645_set_bias_level() runs under card->dapm_mutex and cannot take jd_mutex
without inverting lock order with rt5645_jack_detect_work(), using
READ_ONCE() and WRITE_ONCE() on rt5645->en_button_func would avoid the race.

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

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

end of thread, other threads:[~2026-12-03  9:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  8:53 [PATCH] ASoC: rt5645: stop jack detection on component remove Yaohan Li
2026-12-03  9:23 ` sashiko-bot

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