mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yaohan Li <shunsheng1218@gmail.com>
To: Oder Chiou <oder_chiou@realtek.com>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>
Cc: Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ASoC: rt5645: stop jack detection on component remove
Date: Thu,  3 Dec 2026 16:53:09 +0800	[thread overview]
Message-ID: <20261203085309.2392653-1-shunsheng1218@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-03  8:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203085309.2392653-1-shunsheng1218@gmail.com \
    --to=shunsheng1218@gmail.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=oder_chiou@realtek.com \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®