mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ASoC: rt5665: do not block workqueue if card is unbound
@ 2026-11-17  4:13 Hyunjung Ko
  0 siblings, 0 replies; only message in thread
From: Hyunjung Ko @ 2026-11-17  4:13 UTC (permalink / raw)
  To: Mark Brown, Oder Chiou
  Cc: Liam Girdwood, Jaroslav Kysela, Takashi Iwai, linux-sound,
	linux-kernel, stable, Hyunjung Ko

Commit 4561d07d897f ("ASoC: rt5665: cancel jack detect work on removal
to avoid use-after-free") added cancel_delayed_work_sync() for
jack_detect_work and jd_check_work on shutdown and remove. However
rt5665_jack_detect_handler() and rt5665_calibrate_handler() busy-wait
inside the work callback until the component is bound, the card is
instantiated and calibration has finished. If such a work item is queued
and the device is then unbound before the card is instantiated, those
loops never make progress and cancel_delayed_work_sync() blocks forever.

calibrate_work is scheduled from rt5665_probe() but is never canceled, so
on unbind its handler can also run against the devm-freed rt5665_priv.

Reschedule the work with mod_delayed_work() when the card is not ready
yet instead of looping, and cancel calibrate_work on teardown too, as the
sibling rt5682 and rt5668 drivers already do:

commit 4c33de0673ce ("ASoC: rt5682: do not block workqueue if card is unbound")
commit a6d78661dc90 ("ASoC: rt5668: do not block workqueue if card is unbound")

Found by static analysis and code review; not runtime-reproduced (needs
RT5665 hardware).

Fixes: 4561d07d897f ("ASoC: rt5665: cancel jack detect work on removal to avoid use-after-free")
Cc: stable@vger.kernel.org
Signed-off-by: Hyunjung Ko <hj351016@gmail.com>
---
 sound/soc/codecs/rt5665.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/sound/soc/codecs/rt5665.c b/sound/soc/codecs/rt5665.c
index 4f7b13571369..27744497033a 100644
--- a/sound/soc/codecs/rt5665.c
+++ b/sound/soc/codecs/rt5665.c
@@ -1194,19 +1194,13 @@ static void rt5665_jack_detect_handler(struct work_struct *work)
 		container_of(work, struct rt5665_priv, jack_detect_work.work);
 	int val, btn_type;
 
-	while (!rt5665->component) {
-		pr_debug("%s codec = null\n", __func__);
-		usleep_range(10000, 15000);
-	}
-
-	while (!snd_soc_card_is_instantiated(rt5665->component->card)) {
-		pr_debug("%s\n", __func__);
-		usleep_range(10000, 15000);
-	}
-
-	while (!rt5665->calibration_done) {
-		pr_debug("%s calibration not ready\n", __func__);
-		usleep_range(10000, 15000);
+	if (!rt5665->component ||
+	    !snd_soc_card_is_instantiated(rt5665->component->card) ||
+	    !rt5665->calibration_done) {
+		/* card not instantiated or calibration pending, try later */
+		mod_delayed_work(system_power_efficient_wq,
+				 &rt5665->jack_detect_work, msecs_to_jiffies(15));
+		return;
 	}
 
 	guard(mutex)(&rt5665->calibrate_mutex);
@@ -4652,9 +4646,12 @@ static void rt5665_calibrate_handler(struct work_struct *work)
 	struct rt5665_priv *rt5665 = container_of(work, struct rt5665_priv,
 		calibrate_work.work);
 
-	while (!snd_soc_card_is_instantiated(rt5665->component->card)) {
-		pr_debug("%s\n", __func__);
-		usleep_range(10000, 15000);
+	if (!rt5665->component ||
+	    !snd_soc_card_is_instantiated(rt5665->component->card)) {
+		/* card not instantiated yet, try later */
+		mod_delayed_work(system_power_efficient_wq,
+				 &rt5665->calibrate_work, msecs_to_jiffies(15));
+		return;
 	}
 
 	rt5665_calibrate(rt5665);
@@ -4840,6 +4837,7 @@ static void rt5665_i2c_shutdown(struct i2c_client *client)
 		disable_irq(client->irq);
 	cancel_delayed_work_sync(&rt5665->jack_detect_work);
 	cancel_delayed_work_sync(&rt5665->jd_check_work);
+	cancel_delayed_work_sync(&rt5665->calibrate_work);
 
 	regmap_write(rt5665->regmap, RT5665_RESET, 0);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-17  4:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17  4:13 [PATCH] ASoC: rt5665: do not block workqueue if card is unbound Hyunjung Ko

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®