[PATCH v2] ASoC: rt5645: stop jack detection on component remove

From: Yaohan Li

Date: Thu Dec 03 2026 - 23:21:48 EST


rt5645_i2c_probe() requests the codec IRQ and initializes
btn_check_timer and jack_detect_work, but rt5645->component and the
card-owned snd_soc_jack pointers are only valid while the ASoC
component is bound. If the sound card is unbound while the I2C device
stays bound, rt5645_remove() leaves rt5645->component, the jack
pointers, btn_check_timer, and jack_detect_work active. A pending or
IRQ-triggered rt5645_jack_detect_work() can then dereference freed card
or jack state.

A few paths also touch this state without consistent locking:
- rt5645_probe() publishes rt5645->component before eq_param is
allocated.
- rt5645_set_jack_detect() updates the jack pointers without holding
jd_mutex or checking rt5645->component.
- rt5645_jack_detect_work() checks rt5645->component and reads the jack
pointers outside jd_mutex, while holding jd_mutex across
snd_soc_jack_report() in the jd_mode == 0 path.
- rt5645_sys_resume() calls rt5645_jack_detect_work() directly instead
of queuing it, so cancel_delayed_work_sync() cannot wait for it.

Protect rt5645->component and the jack pointers with jd_mutex across
probe, remove, rt5645_set_jack_detect(), and rt5645_jack_detect_work().
In rt5645_jack_detect_work(), snapshot the jack pointers under jd_mutex
and call snd_soc_jack_report() after dropping the lock in all jd_mode
paths so a jack notifier re-entering rt5645_set_jack_detect() cannot
deadlock. Pair WRITE_ONCE()/READ_ONCE() on rt5645->en_button_func
between the jd_mutex writers and rt5645_set_bias_level(), which runs
under card->dapm_mutex and cannot take jd_mutex without inverting the
lock order. In rt5645_sys_resume(), queue jack_detect_work under
jd_mutex and flush it outside the lock to keep the synchronous
post-resume CBJ detection from commit aa98697c7dbd ("ASoC: rt5645:
improve the depop sequences of CBJ detection") while letting
rt5645_remove() drain it along with btn_check_timer.

Fixes: f3fa1bbd836a ("ASoC: rt5645: Add headset detect function")
Signed-off-by: Yaohan Li <shunsheng1218@xxxxxxxxx>
---
Changes in v2:
- Use WRITE_ONCE()/READ_ONCE() on rt5645->en_button_func between the
jd_mutex writers (rt5645_set_jack_detect(), rt5645_remove()) and
rt5645_set_bias_level() under card->dapm_mutex to avoid a data race
without inverting the jd_mutex -> dapm_mutex lock order.
- Keep calling rt5645_irq(0, rt5645) unconditionally in
rt5645_set_jack_detect() so clearing all jack pointers preserves the
existing behavior.
- v1: https://lore.kernel.org/r/20261203085309.2392653-1-shunsheng1218@xxxxxxxxx/

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) using the in-tree snd-soc-rt5645.ko
with a virtual I2C bus, virtual IRQ, and virtual ASoC card:
- card unbind with I2C still bound (jd_mode 0 and 1), mid-work I2C
stall during RT5650 button polling, and rt5645_sys_resume() racing
with rt5645_remove() all hit KASAN slab-UAF on the unmodified driver
and pass cleanly with this patch;
- also checked post-resume synchronous CBJ completion, concurrent
rt5645_set_jack_detect() (both set and clear-all-NULL) vs
rt5645_remove(), jack notifier re-entry into rt5645_set_jack_detect(),
card rebind, both card/I2C teardown orders, and concurrent
rt5645_set_bias_level() vs rt5645_set_jack_detect() (0 KASAN or
lockdep warnings).
Calling snd_soc_jack_report() on the snapshotted pointers after
dropping jd_mutex relies on the ASoC card keeping snd_soc_jack alive
until component remove finishes, which matches the in-tree rt5645
machine drivers and soc_cleanup_card_resources(). Build-tested on
x86_64 and arm64 with W=1 (0 warnings).

AI assistance: Claude and Gemini helped trace the component, resume,
and bias-level races, extend the QEMU test module, and draft the v2
changelog. I reviewed the locking, test logs, and final diff myself.
sound/soc/codecs/rt5645.c | 103 ++++++++++++++++++++++++++------------
1 file changed, 70 insertions(+), 33 deletions(-)

diff --git a/sound/soc/codecs/rt5645.c b/sound/soc/codecs/rt5645.c
index e14b66d3118da..f0ee6fc0d78cb 100644
--- a/sound/soc/codecs/rt5645.c
+++ b/sound/soc/codecs/rt5645.c
@@ -3103,7 +3103,7 @@ static int rt5645_set_bias_level(struct snd_soc_component *component,
if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF) {
snd_soc_component_write(component, RT5645_DEPOP_M2, 0x1140);
msleep(40);
- if (rt5645->en_button_func)
+ if (READ_ONCE(rt5645->en_button_func))
queue_delayed_work(system_power_efficient_wq,
&rt5645->jack_detect_work,
msecs_to_jiffies(0));
@@ -3112,7 +3112,7 @@ static int rt5645_set_bias_level(struct snd_soc_component *component,

case SND_SOC_BIAS_OFF:
snd_soc_component_write(component, RT5645_DEPOP_M2, 0x1100);
- if (!rt5645->en_button_func)
+ if (!READ_ONCE(rt5645->en_button_func))
snd_soc_component_update_bits(component, RT5645_GEN_CTRL1,
RT5645_DIG_GATE_CTRL, 0);
snd_soc_component_update_bits(component, RT5645_PWR_ANLG1,
@@ -3278,19 +3278,27 @@ 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) {
+ WRITE_ONCE(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 {
+ WRITE_ONCE(rt5645->en_button_func, false);
+ }
+
+ rt5645_irq(0, rt5645);
}
- rt5645_irq(0, rt5645);

return 0;
}
@@ -3320,12 +3328,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 +3345,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 +3412,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 +3461,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 +3502,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 +3516,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;
+ WRITE_ONCE(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 +4371,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 +4382,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