Skip to contentSkip to conversation

ALSA: hda - Fix hang caused by race during suspend.

PATCH Applied Dylan Reid 2 participants 2 messages Sep 28, 2012 – Oct 8, 2012 Sound

lore
Share & tools
lore

Dylan Reid

author
#0 lore raw

PATCH 1/1 ALSA: hda - Fix hang caused by race during suspend.

There was a race condition when the system suspends while hda_power_work is running in the work queue. If system suspend (snd_hda_suspend) happens after the work queue releases power_lock but before it calls hda_call_codec_suspend, codec_suspend runs with power_on=0, causing the codec to power up for register reads, and hanging when it calls cancel_delayed_work_sync from the running work queue.

The call chain from the work queue will look like this:
hda_power_work <<- power_on = 1, unlock, then power_on cleard by suspend
hda_call_codec_suspend
hda_set_power_state
snd_hda_codec_read
codec_exec_verb
snd_hda_power_up
snd_hda_power_save
__snd_hda_power_up
cancel_delayed_work_sync <<-- cancelling executing wq

Fix this by waiting for the work queue to finish before starting suspend if suspend is not happening on the work queue.

---
 sound/pci/hda/hda_codec.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
sound/pci/hda/hda_codec.c +6 -4
index 960800b..c0ab72c 100644
@@ -3612,7 +3612,7 @@ static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  * call suspend and power-down; used both from PM and power-save
  * this function returns the power state in the end
  */
-static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
+static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
 {
 	unsigned int state;
 
@@ -3620,7 +3620,9 @@ static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
 		codec->patch_ops.suspend(codec);
 	hda_cleanup_all_streams(codec);
 	state = hda_set_power_state(codec, AC_PWRST_D3);
-	cancel_delayed_work(&codec->power_work);
+	/* Cancel delayed work if we aren't currently running from it. */
+	if (!in_wq)
+		cancel_delayed_work_sync(&codec->power_work);
 	spin_lock(&codec->power_lock);
 	snd_hda_update_power_acct(codec);
 	trace_hda_power_down(codec);
@@ -4478,7 +4480,7 @@ static void hda_power_work(struct work_struct *work)
 	}
 	spin_unlock(&codec->power_lock);
 
-	state = hda_call_codec_suspend(codec);
+	state = hda_call_codec_suspend(codec, true);
 	codec->pm_down_notified = 0;
 	if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
 		codec->pm_down_notified = 1;
@@ -5127,7 +5129,7 @@ int snd_hda_suspend(struct hda_bus *bus)
 
 	list_for_each_entry(codec, &bus->codec_list, list) {
 		if (hda_codec_is_power_on(codec))
-			hda_call_codec_suspend(codec);
+			hda_call_codec_suspend(codec, false);
 	}
 	return 0;
 }
Signature · 1 line
1.7.12.146.g16d26b1
Applied
#1 lore raw

At Fri, 28 Sep 2012 15:57:01 -0700,

Quoted 19 lines · Dylan Reid

There was a race condition when the system suspends while hda_power_work is running in the work queue. If system suspend (snd_hda_suspend) happens after the work queue releases power_lock but before it calls hda_call_codec_suspend, codec_suspend runs with power_on=0, causing the codec to power up for register reads, and hanging when it calls cancel_delayed_work_sync from the running work queue.

The call chain from the work queue will look like this:
hda_power_work <<- power_on = 1, unlock, then power_on cleard by suspend
hda_call_codec_suspend
hda_set_power_state
snd_hda_codec_read
codec_exec_verb
snd_hda_power_up
snd_hda_power_save
__snd_hda_power_up
cancel_delayed_work_sync <<-- cancelling executing wq

Quoted 4 lines

Fix this by waiting for the work queue to finish before starting suspend if suspend is not happening on the work queue.

I queued it for 3.7 kernel with Cc to stable. Thanks!

Takashi

Quoted 49 lines
---
 sound/pci/hda/hda_codec.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
sound/pci/hda/hda_codec.c +6 -4
index 960800b..c0ab72c 100644
@@ -3612,7 +3612,7 @@ static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  * call suspend and power-down; used both from PM and power-save
  * this function returns the power state in the end
  */
-static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
+static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
 {
 	unsigned int state;
 
@@ -3620,7 +3620,9 @@ static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
 		codec->patch_ops.suspend(codec);
 	hda_cleanup_all_streams(codec);
 	state = hda_set_power_state(codec, AC_PWRST_D3);
-	cancel_delayed_work(&codec->power_work);
+	/* Cancel delayed work if we aren't currently running from it. */
+	if (!in_wq)
+		cancel_delayed_work_sync(&codec->power_work);
 	spin_lock(&codec->power_lock);
 	snd_hda_update_power_acct(codec);
 	trace_hda_power_down(codec);
@@ -4478,7 +4480,7 @@ static void hda_power_work(struct work_struct *work)
 	}
 	spin_unlock(&codec->power_lock);
 
-	state = hda_call_codec_suspend(codec);
+	state = hda_call_codec_suspend(codec, true);
 	codec->pm_down_notified = 0;
 	if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
 		codec->pm_down_notified = 1;
@@ -5127,7 +5129,7 @@ int snd_hda_suspend(struct hda_bus *bus)
 
 	list_for_each_entry(codec, &bus->codec_list, list) {
 		if (hda_codec_is_power_on(codec))
-			hda_call_codec_suspend(codec);
+			hda_call_codec_suspend(codec, false);
 	}
 	return 0;
 }
Signature · 1 line
1.7.12.146.g16d26b1