2022-11-28 10:00:48

by Ricardo Ribalda

[permalink] [raw]
Subject: [PATCH v3 0/2] ALSA: core: Fix deadlock when shutdown a frozen userspace

Since 83bfc7e793b5 ("ASoC: SOF: core: unregister clients and machine drivers in .shutdown")
we wait for userspace to close its fds.

But that will never occur with a frozen userspace (like during kexec()).

Lets detect the frozen userpace and act accordingly.

To: Jaroslav Kysela <[email protected]>
To: Takashi Iwai <[email protected]>
To: "Rafael J. Wysocki" <[email protected]>
To: Pavel Machek <[email protected]>
To: Len Brown <[email protected]>
To: Kai Vehmanen <[email protected]>
To: Ranjani Sridharan <[email protected]>
To: Pierre-Louis Bossart <[email protected]>
To: Mark Brown <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: "Joel Fernandes (Google)" <[email protected]>
Cc: [email protected]
Signed-off-by: Ricardo Ribalda <[email protected]>
---
Changes in v3:
- Wrap pm_freezing in a function
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Only use pm_freezing if CONFIG_FREEZER
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Ricardo Ribalda (2):
freezer: Add processes_frozen()
ALSA: core: Fix deadlock when shutdown a frozen userspace

include/linux/freezer.h | 2 ++
kernel/freezer.c | 11 +++++++++++
sound/core/init.c | 13 +++++++++++++
3 files changed, 26 insertions(+)
---
base-commit: 4312098baf37ee17a8350725e6e0d0e8590252d4
change-id: 20221127-snd-freeze-1ee143228326

Best regards,
--
Ricardo Ribalda <[email protected]>


2022-11-28 10:18:10

by Ricardo Ribalda

[permalink] [raw]
Subject: [PATCH v3 2/2] ALSA: core: Fix deadlock when shutdown a frozen userspace

If the user space is frozen, we cannot wait for it to complete.

This fixes:

[ 84.943749] Freezing user space processes ... (elapsed 0.111 seconds) done.
[ 246.784446] INFO: task kexec-lite:5123 blocked for more than 122 seconds.
[ 246.819035] Call Trace:
[ 246.821782] <TASK>
[ 246.824186] __schedule+0x5f9/0x1263
[ 246.828231] schedule+0x87/0xc5
[ 246.831779] snd_card_disconnect_sync+0xb5/0x127
...
[ 246.889249] snd_sof_device_shutdown+0xb4/0x150
[ 246.899317] pci_device_shutdown+0x37/0x61
[ 246.903990] device_shutdown+0x14c/0x1d6
[ 246.908391] kernel_kexec+0x45/0xb9

And:

[ 246.893222] INFO: task kexec-lite:4891 blocked for more than 122 seconds.
[ 246.927709] Call Trace:
[ 246.930461] <TASK>
[ 246.932819] __schedule+0x5f9/0x1263
[ 246.936855] ? fsnotify_grab_connector+0x5c/0x70
[ 246.942045] schedule+0x87/0xc5
[ 246.945567] schedule_timeout+0x49/0xf3
[ 246.949877] wait_for_completion+0x86/0xe8
[ 246.954463] snd_card_free+0x68/0x89
...
[ 247.001080] platform_device_unregister+0x12/0x35

Fixes: 83bfc7e793b5 ("ASoC: SOF: core: unregister clients and machine drivers in .shutdown")
Signed-off-by: Ricardo Ribalda <[email protected]>
---
sound/core/init.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/sound/core/init.c b/sound/core/init.c
index 5377f94eb211..bc038b49d4d3 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -9,6 +9,7 @@
#include <linux/module.h>
#include <linux/device.h>
#include <linux/file.h>
+#include <linux/freezer.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/ctype.h>
@@ -573,6 +574,11 @@ void snd_card_disconnect_sync(struct snd_card *card)
return;
}

+ if (processes_frozen()) {
+ dev_err(card->dev, "Userspace is frozen, skipping sync\n");
+ return;
+ }
+
spin_lock_irq(&card->files_lock);
wait_event_lock_irq(card->remove_sleep,
list_empty(&card->files_list),
@@ -658,6 +664,13 @@ int snd_card_free(struct snd_card *card)
ret = snd_card_free_when_closed(card);
if (ret)
return ret;
+
+ /*
+ * If userspace is frozen the wait from completion will never end.
+ */
+ if (processes_frozen())
+ return 0;
+
/* wait, until all devices are ready for the free operation */
wait_for_completion(&released);


--
2.38.1.584.g0f3c55d4c2-goog-b4-0.11.0-dev-696ae