Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Revert "firmware: add sanity check on shutdown/suspend"

This reverts commit 81f95076281fdd3bc382e004ba1bce8e82fccbce.

It causes random failures of firmware loading at resume time (well,
random for me, it seems to be more reliable for others) because the
firmware disabling is not actually synchronous with any particular
resume event, and at least the btusb driver that uses a workqueue to
load the firmware at resume seems to occasionally hit the "firmware
loading is disabled" logic because the firmware loader hasn't gotten the
resume event yet.

Some kind of sanity check for not trying to load firmware when it's not
possible might be a good thing, but this commit was not it.

Greg seems to have silently suffered the same issue, and pointed to the
likely culprit, and Gabriel C verified the revert fixed it for him too.

Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Pointed-at-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Tested-by: Gabriel C <nix.or.die@gmail.com>
Cc: Luis R. Rodriguez <mcgrof@kernel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

-110
-11
Documentation/driver-api/firmware/request_firmware.rst
··· 44 44 .. kernel-doc:: drivers/base/firmware_class.c 45 45 :functions: request_firmware_nowait 46 46 47 - Considerations for suspend and resume 48 - ===================================== 49 - 50 - During suspend and resume only the built-in firmware and the firmware cache 51 - elements of the firmware API can be used. This is managed by fw_pm_notify(). 52 - 53 - fw_pm_notify 54 - ------------ 55 - .. kernel-doc:: drivers/base/firmware_class.c 56 - :functions: fw_pm_notify 57 - 58 47 request firmware API expected driver use 59 48 ======================================== 60 49
-99
drivers/base/firmware_class.c
··· 258 258 * guarding for corner cases a global lock should be OK */ 259 259 static DEFINE_MUTEX(fw_lock); 260 260 261 - static bool __enable_firmware = false; 262 - 263 - static void enable_firmware(void) 264 - { 265 - mutex_lock(&fw_lock); 266 - __enable_firmware = true; 267 - mutex_unlock(&fw_lock); 268 - } 269 - 270 - static void disable_firmware(void) 271 - { 272 - mutex_lock(&fw_lock); 273 - __enable_firmware = false; 274 - mutex_unlock(&fw_lock); 275 - } 276 - 277 - /* 278 - * When disabled only the built-in firmware and the firmware cache will be 279 - * used to look for firmware. 280 - */ 281 - static bool firmware_enabled(void) 282 - { 283 - bool enabled = false; 284 - 285 - mutex_lock(&fw_lock); 286 - if (__enable_firmware) 287 - enabled = true; 288 - mutex_unlock(&fw_lock); 289 - 290 - return enabled; 291 - } 292 - 293 261 static struct firmware_cache fw_cache; 294 262 295 263 static struct firmware_buf *__allocate_fw_buf(const char *fw_name, ··· 1214 1246 if (ret <= 0) /* error or already assigned */ 1215 1247 goto out; 1216 1248 1217 - if (!firmware_enabled()) { 1218 - WARN(1, "firmware request while host is not available\n"); 1219 - ret = -EHOSTDOWN; 1220 - goto out; 1221 - } 1222 - 1223 1249 ret = fw_get_filesystem_firmware(device, fw->priv); 1224 1250 if (ret) { 1225 1251 if (!(opt_flags & FW_OPT_NO_WARN)) ··· 1724 1762 msecs_to_jiffies(delay)); 1725 1763 } 1726 1764 1727 - /** 1728 - * fw_pm_notify - notifier for suspend/resume 1729 - * @notify_block: unused 1730 - * @mode: mode we are switching to 1731 - * @unused: unused 1732 - * 1733 - * Used to modify the firmware_class state as we move in between states. 1734 - * The firmware_class implements a firmware cache to enable device driver 1735 - * to fetch firmware upon resume before the root filesystem is ready. We 1736 - * disable API calls which do not use the built-in firmware or the firmware 1737 - * cache when we know these calls will not work. 1738 - * 1739 - * The inner logic behind all this is a bit complex so it is worth summarizing 1740 - * the kernel's own suspend/resume process with context and focus on how this 1741 - * can impact the firmware API. 1742 - * 1743 - * First a review on how we go to suspend:: 1744 - * 1745 - * pm_suspend() --> enter_state() --> 1746 - * sys_sync() 1747 - * suspend_prepare() --> 1748 - * __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...); 1749 - * suspend_freeze_processes() --> 1750 - * freeze_processes() --> 1751 - * __usermodehelper_set_disable_depth(UMH_DISABLED); 1752 - * freeze all tasks ... 1753 - * freeze_kernel_threads() 1754 - * suspend_devices_and_enter() --> 1755 - * dpm_suspend_start() --> 1756 - * dpm_prepare() 1757 - * dpm_suspend() 1758 - * suspend_enter() --> 1759 - * platform_suspend_prepare() 1760 - * dpm_suspend_late() 1761 - * freeze_enter() 1762 - * syscore_suspend() 1763 - * 1764 - * When we resume we bail out of a loop from suspend_devices_and_enter() and 1765 - * unwind back out to the caller enter_state() where we were before as follows:: 1766 - * 1767 - * enter_state() --> 1768 - * suspend_devices_and_enter() --> (bail from loop) 1769 - * dpm_resume_end() --> 1770 - * dpm_resume() 1771 - * dpm_complete() 1772 - * suspend_finish() --> 1773 - * suspend_thaw_processes() --> 1774 - * thaw_processes() --> 1775 - * __usermodehelper_set_disable_depth(UMH_FREEZING); 1776 - * thaw_workqueues(); 1777 - * thaw all processes ... 1778 - * usermodehelper_enable(); 1779 - * pm_notifier_call_chain(PM_POST_SUSPEND); 1780 - * 1781 - * fw_pm_notify() works through pm_notifier_call_chain(). 1782 - */ 1783 1765 static int fw_pm_notify(struct notifier_block *notify_block, 1784 1766 unsigned long mode, void *unused) 1785 1767 { ··· 1737 1831 */ 1738 1832 kill_pending_fw_fallback_reqs(true); 1739 1833 device_cache_fw_images(); 1740 - disable_firmware(); 1741 1834 break; 1742 1835 1743 1836 case PM_POST_SUSPEND: ··· 1749 1844 mutex_lock(&fw_lock); 1750 1845 fw_cache.state = FW_LOADER_NO_CACHE; 1751 1846 mutex_unlock(&fw_lock); 1752 - enable_firmware(); 1753 1847 1754 1848 device_uncache_fw_images_delay(10 * MSEC_PER_SEC); 1755 1849 break; ··· 1797 1893 static int fw_shutdown_notify(struct notifier_block *unused1, 1798 1894 unsigned long unused2, void *unused3) 1799 1895 { 1800 - disable_firmware(); 1801 1896 /* 1802 1897 * Kill all pending fallback requests to avoid both stalling shutdown, 1803 1898 * and avoid a deadlock with the usermode_lock. ··· 1812 1909 1813 1910 static int __init firmware_class_init(void) 1814 1911 { 1815 - enable_firmware(); 1816 1912 fw_cache_init(); 1817 1913 register_reboot_notifier(&fw_shutdown_nb); 1818 1914 #ifdef CONFIG_FW_LOADER_USER_HELPER ··· 1823 1921 1824 1922 static void __exit firmware_class_exit(void) 1825 1923 { 1826 - disable_firmware(); 1827 1924 #ifdef CONFIG_PM_SLEEP 1828 1925 unregister_syscore_ops(&fw_syscore_ops); 1829 1926 unregister_pm_notifier(&fw_cache.pm_notify);