ALSA: core: Fix deadlock when shutdown a frozen userspace

Message ID 20221127-snd-freeze-v1-0-57461a366ec2@chromium.org
State New
Headers
Series ALSA: core: Fix deadlock when shutdown a frozen userspace |

Commit Message

Ricardo Ribalda Nov. 27, 2022, 10:30 p.m. UTC
  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

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
To: Jaroslav Kysela <perex@perex.cz>
To: Takashi Iwai <tiwai@suse.com>
Cc: alsa-devel@alsa-project.org
Cc: linux-kernel@vger.kernel.org
Cc: "Joel Fernandes (Google)" <joel@joelfernandes.org>
---
 sound/core/init.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)


---
base-commit: 4312098baf37ee17a8350725e6e0d0e8590252d4
change-id: 20221127-snd-freeze-1ee143228326

Best regards,
  

Comments

kernel test robot Nov. 28, 2022, 12:59 a.m. UTC | #1
Hi Ricardo,

I love your patch! Yet something to improve:

[auto build test ERROR on 4312098baf37ee17a8350725e6e0d0e8590252d4]

url:    https://github.com/intel-lab-lkp/linux/commits/Ricardo-Ribalda/ALSA-core-Fix-deadlock-when-shutdown-a-frozen-userspace/20221128-063051
base:   4312098baf37ee17a8350725e6e0d0e8590252d4
patch link:    https://lore.kernel.org/r/20221127-snd-freeze-v1-0-57461a366ec2%40chromium.org
patch subject: [PATCH] ALSA: core: Fix deadlock when shutdown a frozen userspace
config: openrisc-randconfig-r003-20221128
compiler: or1k-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/1db83cb7b8d5d82de1492f028c45f47a83cd2b67
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Ricardo-Ribalda/ALSA-core-Fix-deadlock-when-shutdown-a-frozen-userspace/20221128-063051
        git checkout 1db83cb7b8d5d82de1492f028c45f47a83cd2b67
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=openrisc SHELL=/bin/bash sound/core/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   sound/core/init.c: In function 'snd_card_disconnect_sync':
>> sound/core/init.c:577:13: error: 'pm_freezing' undeclared (first use in this function); did you mean 'freezing'?
     577 |         if (pm_freezing) {
         |             ^~~~~~~~~~~
         |             freezing
   sound/core/init.c:577:13: note: each undeclared identifier is reported only once for each function it appears in
   sound/core/init.c: In function 'snd_card_free':
   sound/core/init.c:671:13: error: 'pm_freezing' undeclared (first use in this function); did you mean 'freezing'?
     671 |         if (pm_freezing)
         |             ^~~~~~~~~~~
         |             freezing


vim +577 sound/core/init.c

   555	
   556	/**
   557	 * snd_card_disconnect_sync - disconnect card and wait until files get closed
   558	 * @card: card object to disconnect
   559	 *
   560	 * This calls snd_card_disconnect() for disconnecting all belonging components
   561	 * and waits until all pending files get closed.
   562	 * It assures that all accesses from user-space finished so that the driver
   563	 * can release its resources gracefully.
   564	 */
   565	void snd_card_disconnect_sync(struct snd_card *card)
   566	{
   567		int err;
   568	
   569		err = snd_card_disconnect(card);
   570		if (err < 0) {
   571			dev_err(card->dev,
   572				"snd_card_disconnect error (%d), skipping sync\n",
   573				err);
   574			return;
   575		}
   576	
 > 577		if (pm_freezing) {
   578			dev_err(card->dev, "Userspace is frozen, skipping sync\n");
   579			return;
   580		}
   581	
   582		spin_lock_irq(&card->files_lock);
   583		wait_event_lock_irq(card->remove_sleep,
   584				    list_empty(&card->files_list),
   585				    card->files_lock);
   586		spin_unlock_irq(&card->files_lock);
   587	}
   588	EXPORT_SYMBOL_GPL(snd_card_disconnect_sync);
   589
  

Patch

diff --git a/sound/core/init.c b/sound/core/init.c
index 5377f94eb211..f614a2cdea3d 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 (pm_freezing) {
+		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 (pm_freezing)
+		return 0;
+
 	/* wait, until all devices are ready for the free operation */
 	wait_for_completion(&released);