Skip to content

Commit 81f9507

Browse files
mcgrofgregkh
authored andcommitted
firmware: add sanity check on shutdown/suspend
The firmware API should not be used after we go to suspend and after we reboot/halt. The suspend/resume case is a bit complex, so this documents that so things are clearer. We want to know about users of the API in incorrect places so that their callers are corrected, so this also adds a warn for those cases. Signed-off-by: Luis R. Rodriguez <mcgrof@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a669f04 commit 81f9507

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

‎Documentation/driver-api/firmware/request_firmware.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ request_firmware_nowait
4444
.. kernel-doc:: drivers/base/firmware_class.c
4545
:functions: request_firmware_nowait
4646

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+
4758
request firmware API expected driver use
4859
========================================
4960

‎drivers/base/firmware_class.c‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,38 @@ static int fw_cache_piggyback_on_request(const char *name);
260260
* guarding for corner cases a global lock should be OK */
261261
static DEFINE_MUTEX(fw_lock);
262262

263+
static bool __enable_firmware = false;
264+
265+
static void enable_firmware(void)
266+
{
267+
mutex_lock(&fw_lock);
268+
__enable_firmware = true;
269+
mutex_unlock(&fw_lock);
270+
}
271+
272+
static void disable_firmware(void)
273+
{
274+
mutex_lock(&fw_lock);
275+
__enable_firmware = false;
276+
mutex_unlock(&fw_lock);
277+
}
278+
279+
/*
280+
* When disabled only the built-in firmware and the firmware cache will be
281+
* used to look for firmware.
282+
*/
283+
static bool firmware_enabled(void)
284+
{
285+
bool enabled = false;
286+
287+
mutex_lock(&fw_lock);
288+
if (__enable_firmware)
289+
enabled = true;
290+
mutex_unlock(&fw_lock);
291+
292+
return enabled;
293+
}
294+
263295
static struct firmware_cache fw_cache;
264296

265297
static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
@@ -1163,6 +1195,12 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
11631195
if (ret <= 0) /* error or already assigned */
11641196
goto out;
11651197

1198+
if (!firmware_enabled()) {
1199+
WARN(1, "firmware request while host is not available\n");
1200+
ret = -EHOSTDOWN;
1201+
goto out;
1202+
}
1203+
11661204
ret = 0;
11671205
timeout = firmware_loading_timeout();
11681206
if (opt_flags & FW_OPT_NOWAIT) {
@@ -1695,6 +1733,62 @@ static void device_uncache_fw_images_delay(unsigned long delay)
16951733
msecs_to_jiffies(delay));
16961734
}
16971735

1736+
/**
1737+
* fw_pm_notify - notifier for suspend/resume
1738+
* @notify_block: unused
1739+
* @mode: mode we are switching to
1740+
* @unused: unused
1741+
*
1742+
* Used to modify the firmware_class state as we move in between states.
1743+
* The firmware_class implements a firmware cache to enable device driver
1744+
* to fetch firmware upon resume before the root filesystem is ready. We
1745+
* disable API calls which do not use the built-in firmware or the firmware
1746+
* cache when we know these calls will not work.
1747+
*
1748+
* The inner logic behind all this is a bit complex so it is worth summarizing
1749+
* the kernel's own suspend/resume process with context and focus on how this
1750+
* can impact the firmware API.
1751+
*
1752+
* First a review on how we go to suspend::
1753+
*
1754+
* pm_suspend() --> enter_state() -->
1755+
* sys_sync()
1756+
* suspend_prepare() -->
1757+
* __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
1758+
* suspend_freeze_processes() -->
1759+
* freeze_processes() -->
1760+
* __usermodehelper_set_disable_depth(UMH_DISABLED);
1761+
* freeze all tasks ...
1762+
* freeze_kernel_threads()
1763+
* suspend_devices_and_enter() -->
1764+
* dpm_suspend_start() -->
1765+
* dpm_prepare()
1766+
* dpm_suspend()
1767+
* suspend_enter() -->
1768+
* platform_suspend_prepare()
1769+
* dpm_suspend_late()
1770+
* freeze_enter()
1771+
* syscore_suspend()
1772+
*
1773+
* When we resume we bail out of a loop from suspend_devices_and_enter() and
1774+
* unwind back out to the caller enter_state() where we were before as follows::
1775+
*
1776+
* enter_state() -->
1777+
* suspend_devices_and_enter() --> (bail from loop)
1778+
* dpm_resume_end() -->
1779+
* dpm_resume()
1780+
* dpm_complete()
1781+
* suspend_finish() -->
1782+
* suspend_thaw_processes() -->
1783+
* thaw_processes() -->
1784+
* __usermodehelper_set_disable_depth(UMH_FREEZING);
1785+
* thaw_workqueues();
1786+
* thaw all processes ...
1787+
* usermodehelper_enable();
1788+
* pm_notifier_call_chain(PM_POST_SUSPEND);
1789+
*
1790+
* fw_pm_notify() works through pm_notifier_call_chain().
1791+
*/
16981792
static int fw_pm_notify(struct notifier_block *notify_block,
16991793
unsigned long mode, void *unused)
17001794
{
@@ -1708,6 +1802,7 @@ static int fw_pm_notify(struct notifier_block *notify_block,
17081802
*/
17091803
kill_pending_fw_fallback_reqs(true);
17101804
device_cache_fw_images();
1805+
disable_firmware();
17111806
break;
17121807

17131808
case PM_POST_SUSPEND:
@@ -1720,6 +1815,7 @@ static int fw_pm_notify(struct notifier_block *notify_block,
17201815
mutex_lock(&fw_lock);
17211816
fw_cache.state = FW_LOADER_NO_CACHE;
17221817
mutex_unlock(&fw_lock);
1818+
enable_firmware();
17231819

17241820
device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
17251821
break;
@@ -1768,6 +1864,7 @@ static void __init fw_cache_init(void)
17681864
static int fw_shutdown_notify(struct notifier_block *unused1,
17691865
unsigned long unused2, void *unused3)
17701866
{
1867+
disable_firmware();
17711868
/*
17721869
* Kill all pending fallback requests to avoid both stalling shutdown,
17731870
* and avoid a deadlock with the usermode_lock.
@@ -1783,6 +1880,7 @@ static struct notifier_block fw_shutdown_nb = {
17831880

17841881
static int __init firmware_class_init(void)
17851882
{
1883+
enable_firmware();
17861884
fw_cache_init();
17871885
register_reboot_notifier(&fw_shutdown_nb);
17881886
#ifdef CONFIG_FW_LOADER_USER_HELPER
@@ -1794,6 +1892,7 @@ static int __init firmware_class_init(void)
17941892

17951893
static void __exit firmware_class_exit(void)
17961894
{
1895+
disable_firmware();
17971896
#ifdef CONFIG_PM_SLEEP
17981897
unregister_syscore_ops(&fw_syscore_ops);
17991898
unregister_pm_notifier(&fw_cache.pm_notify);

0 commit comments

Comments
 (0)