All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zhen Ni <nizhen@uniontech.com>
To: oder_chiou@realtek.com, broonie@kernel.org, tiwai@suse.com
Cc: alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org,
	Zhen Ni <nizhen@uniontech.com>
Subject: [PATCH] ASoC: rt5665: Don't block workqueue if card is unbound
Date: Fri,  4 Mar 2022 14:27:58 +0800	[thread overview]
Message-ID: <20220304062758.9701-1-nizhen@uniontech.com> (raw)

The current rt5665_jack_detect_handler() assumes the component
and card will always show up and implements an infinite usleep
loop waiting for them to show up.

This does not hold true if a codec interrupt (or other
event) occurs when the card is unbound. The codec driver's
remove  or shutdown functions cannot cancel the workqueue due
to the wait loop. As a result, code can either end up blocking
the workqueue, or hit a kernel oops when the card is freed.

Fix the issue by rescheduling the jack detect handler in
case the card is not ready. In case card never shows up,
the shutdown/remove/suspend calls can now cancel the detect
task.

Signed-off-by: Zhen Ni <nizhen@uniontech.com>
---
 sound/soc/codecs/rt5665.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/sound/soc/codecs/rt5665.c b/sound/soc/codecs/rt5665.c
index 33e889802ff8..cdfd7085df2a 100644
--- a/sound/soc/codecs/rt5665.c
+++ b/sound/soc/codecs/rt5665.c
@@ -1293,19 +1293,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 (!rt5665->component->card->instantiated) {
-		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 || !rt5665->component->card->instantiated ||
+			!rt5665->calibration_done) {
+		pr_debug("%s card not yet ready\n", __func__);
+		/* try later */
+		mod_delayed_work(system_power_efficient_wq,
+				&rt5665->jack_detect_work, msecs_to_jiffies(15));
+		return;
 	}
 
 	mutex_lock(&rt5665->calibrate_mutex);
-- 
2.20.1




WARNING: multiple messages have this Message-ID (diff)
From: Zhen Ni <nizhen@uniontech.com>
To: oder_chiou@realtek.com, broonie@kernel.org, tiwai@suse.com
Cc: Zhen Ni <nizhen@uniontech.com>,
	alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ASoC: rt5665: Don't block workqueue if card is unbound
Date: Fri,  4 Mar 2022 14:27:58 +0800	[thread overview]
Message-ID: <20220304062758.9701-1-nizhen@uniontech.com> (raw)

The current rt5665_jack_detect_handler() assumes the component
and card will always show up and implements an infinite usleep
loop waiting for them to show up.

This does not hold true if a codec interrupt (or other
event) occurs when the card is unbound. The codec driver's
remove  or shutdown functions cannot cancel the workqueue due
to the wait loop. As a result, code can either end up blocking
the workqueue, or hit a kernel oops when the card is freed.

Fix the issue by rescheduling the jack detect handler in
case the card is not ready. In case card never shows up,
the shutdown/remove/suspend calls can now cancel the detect
task.

Signed-off-by: Zhen Ni <nizhen@uniontech.com>
---
 sound/soc/codecs/rt5665.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/sound/soc/codecs/rt5665.c b/sound/soc/codecs/rt5665.c
index 33e889802ff8..cdfd7085df2a 100644
--- a/sound/soc/codecs/rt5665.c
+++ b/sound/soc/codecs/rt5665.c
@@ -1293,19 +1293,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 (!rt5665->component->card->instantiated) {
-		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 || !rt5665->component->card->instantiated ||
+			!rt5665->calibration_done) {
+		pr_debug("%s card not yet ready\n", __func__);
+		/* try later */
+		mod_delayed_work(system_power_efficient_wq,
+				&rt5665->jack_detect_work, msecs_to_jiffies(15));
+		return;
 	}
 
 	mutex_lock(&rt5665->calibrate_mutex);
-- 
2.20.1




             reply	other threads:[~2022-03-04  6:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04  6:27 Zhen Ni [this message]
2022-03-04  6:27 ` [PATCH] ASoC: rt5665: Don't block workqueue if card is unbound Zhen Ni

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=20220304062758.9701-1-nizhen@uniontech.com \
    --to=nizhen@uniontech.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oder_chiou@realtek.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.