Skip to contentSkip to conversation

ASoC: rt5665: do not block workqueue if card is unbound

PATCH In review Hyunjung Ko 2 participants 2 messages Nov 17

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: rt5665: do not block workqueue if card is unbound

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")
---
 sound/soc/codecs/rt5665.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
sound/soc/codecs/rt5665.c +14 -16
index 4f7b13571369..27744497033a 100644
@@ -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);
 }
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 1 line · Hyunjung Ko

ASoC: rt5665: do not block workqueue if card is unbound

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.5

Thanks!

[1/1] ASoC: rt5665: do not block workqueue if card is unbound

      https://git.kernel.org/broonie/sound/c/337a0fd20549

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark