serenity/Kernel/Tasks/PowerStateSwitchTask.h

42 lines
957 B
C
Raw Normal View History

/*
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Kernel: Overhaul system shutdown procedure For a long time, our shutdown procedure has basically been: - Acquire big process lock. - Switch framebuffer to Kernel debug console. - Sync and lock all file systems so that disk caches are flushed and files are in a good state. - Use firmware and architecture-specific functionality to perform hardware shutdown. This naive and simple shutdown procedure has multiple issues: - No processes are terminated properly, meaning they cannot perform more complex cleanup work. If they were in the middle of I/O, for instance, only the data that already reached the Kernel is written to disk, and data corruption due to unfinished writes can therefore still occur. - No file systems are unmounted, meaning that any important unmount work will never happen. This is important for e.g. Ext2, which has facilites for detecting improper unmounts (see superblock's s_state variable) and therefore requires a proper unmount to be performed. This was also the starting point for this PR, since I wanted to introduce basic Ext2 file system checking and unmounting. - No hardware is properly shut down beyond what the system firmware does on its own. - Shutdown is performed within the write() call that asked the Kernel to change its power state. If the shutdown procedure takes longer (i.e. when it's done properly), this blocks the process causing the shutdown and prevents any potentially-useful interactions between Kernel and userland during shutdown. In essence, current shutdown is a glorified system crash with minimal file system cleanliness guarantees. Therefore, this commit is the first step in improving our shutdown procedure. The new shutdown flow is now as follows: - From the write() call to the power state SysFS node, a new task is started, the Power State Switch Task. Its only purpose is to change the operating system's power state. This task takes over shutdown and reboot duties, although reboot is not modified in this commit. - The Power State Switch Task assumes that userland has performed all shutdown duties it can perform on its own. In particular, it assumes that all kinds of clean process shutdown have been done, and remaining processes can be hard-killed without consequence. This is an important separation of concerns: While this commit does not modify userland, in the future SystemServer will be responsible for performing proper shutdown of user processes, including timeouts for stubborn processes etc. - As mentioned above, the task hard-kills remaining user processes. - The task hard-kills all Kernel processes except itself and the Finalizer Task. Since Kernel processes can delay their own shutdown indefinitely if they want to, they have plenty opportunity to perform proper shutdown if necessary. This may become a problem with non-cooperative Kernel tasks, but as seen two commits earlier, for now all tasks will cooperate within a few seconds. - The task waits for the Finalizer Task to clean up all processes. - The task hard-kills and finalizes the Finalizer Task itself, meaning that it now is the only remaining process in the system. - The task syncs and locks all file systems, and then unmounts them. Due to an unknown refcount bug we currently cannot unmount the root file system; therefore the task is able to abort the clean unmount if necessary. - The task performs platform-dependent hardware shutdown as before. This commit has multiple remaining issues (or exposed existing ones) which will need to be addressed in the future but are out of scope for now: - Unmounting the root filesystem is impossible due to remaining references to the inodes /home and /home/anon. I investigated this very heavily and could not find whoever is holding the last two references. - Userland cannot perform proper cleanup, since the Kernel's power state variable is accessed directly by tools instead of a proper userland shutdown procedure directed by SystemServer. The recently introduced Firmware/PowerState procedures are removed again, since all of the architecture-independent code can live in the power state switch task. The architecture-specific code is kept, however.
2023-07-09 22:17:11 +00:00
#pragma once
#include <AK/Forward.h>
#include <Kernel/Forward.h>
namespace Kernel {
Kernel: Overhaul system shutdown procedure For a long time, our shutdown procedure has basically been: - Acquire big process lock. - Switch framebuffer to Kernel debug console. - Sync and lock all file systems so that disk caches are flushed and files are in a good state. - Use firmware and architecture-specific functionality to perform hardware shutdown. This naive and simple shutdown procedure has multiple issues: - No processes are terminated properly, meaning they cannot perform more complex cleanup work. If they were in the middle of I/O, for instance, only the data that already reached the Kernel is written to disk, and data corruption due to unfinished writes can therefore still occur. - No file systems are unmounted, meaning that any important unmount work will never happen. This is important for e.g. Ext2, which has facilites for detecting improper unmounts (see superblock's s_state variable) and therefore requires a proper unmount to be performed. This was also the starting point for this PR, since I wanted to introduce basic Ext2 file system checking and unmounting. - No hardware is properly shut down beyond what the system firmware does on its own. - Shutdown is performed within the write() call that asked the Kernel to change its power state. If the shutdown procedure takes longer (i.e. when it's done properly), this blocks the process causing the shutdown and prevents any potentially-useful interactions between Kernel and userland during shutdown. In essence, current shutdown is a glorified system crash with minimal file system cleanliness guarantees. Therefore, this commit is the first step in improving our shutdown procedure. The new shutdown flow is now as follows: - From the write() call to the power state SysFS node, a new task is started, the Power State Switch Task. Its only purpose is to change the operating system's power state. This task takes over shutdown and reboot duties, although reboot is not modified in this commit. - The Power State Switch Task assumes that userland has performed all shutdown duties it can perform on its own. In particular, it assumes that all kinds of clean process shutdown have been done, and remaining processes can be hard-killed without consequence. This is an important separation of concerns: While this commit does not modify userland, in the future SystemServer will be responsible for performing proper shutdown of user processes, including timeouts for stubborn processes etc. - As mentioned above, the task hard-kills remaining user processes. - The task hard-kills all Kernel processes except itself and the Finalizer Task. Since Kernel processes can delay their own shutdown indefinitely if they want to, they have plenty opportunity to perform proper shutdown if necessary. This may become a problem with non-cooperative Kernel tasks, but as seen two commits earlier, for now all tasks will cooperate within a few seconds. - The task waits for the Finalizer Task to clean up all processes. - The task hard-kills and finalizes the Finalizer Task itself, meaning that it now is the only remaining process in the system. - The task syncs and locks all file systems, and then unmounts them. Due to an unknown refcount bug we currently cannot unmount the root file system; therefore the task is able to abort the clean unmount if necessary. - The task performs platform-dependent hardware shutdown as before. This commit has multiple remaining issues (or exposed existing ones) which will need to be addressed in the future but are out of scope for now: - Unmounting the root filesystem is impossible due to remaining references to the inodes /home and /home/anon. I investigated this very heavily and could not find whoever is holding the last two references. - Userland cannot perform proper cleanup, since the Kernel's power state variable is accessed directly by tools instead of a proper userland shutdown procedure directed by SystemServer. The recently introduced Firmware/PowerState procedures are removed again, since all of the architecture-independent code can live in the power state switch task. The architecture-specific code is kept, however.
2023-07-09 22:17:11 +00:00
enum class PowerStateCommand : uintptr_t {
Shutdown,
Reboot,
};
// We will pass the power state command to the task in place of a void* as to avoid the complications of raw allocations.
static_assert(sizeof(PowerStateCommand) == sizeof(void*));
extern bool g_in_system_shutdown;
Kernel: Overhaul system shutdown procedure For a long time, our shutdown procedure has basically been: - Acquire big process lock. - Switch framebuffer to Kernel debug console. - Sync and lock all file systems so that disk caches are flushed and files are in a good state. - Use firmware and architecture-specific functionality to perform hardware shutdown. This naive and simple shutdown procedure has multiple issues: - No processes are terminated properly, meaning they cannot perform more complex cleanup work. If they were in the middle of I/O, for instance, only the data that already reached the Kernel is written to disk, and data corruption due to unfinished writes can therefore still occur. - No file systems are unmounted, meaning that any important unmount work will never happen. This is important for e.g. Ext2, which has facilites for detecting improper unmounts (see superblock's s_state variable) and therefore requires a proper unmount to be performed. This was also the starting point for this PR, since I wanted to introduce basic Ext2 file system checking and unmounting. - No hardware is properly shut down beyond what the system firmware does on its own. - Shutdown is performed within the write() call that asked the Kernel to change its power state. If the shutdown procedure takes longer (i.e. when it's done properly), this blocks the process causing the shutdown and prevents any potentially-useful interactions between Kernel and userland during shutdown. In essence, current shutdown is a glorified system crash with minimal file system cleanliness guarantees. Therefore, this commit is the first step in improving our shutdown procedure. The new shutdown flow is now as follows: - From the write() call to the power state SysFS node, a new task is started, the Power State Switch Task. Its only purpose is to change the operating system's power state. This task takes over shutdown and reboot duties, although reboot is not modified in this commit. - The Power State Switch Task assumes that userland has performed all shutdown duties it can perform on its own. In particular, it assumes that all kinds of clean process shutdown have been done, and remaining processes can be hard-killed without consequence. This is an important separation of concerns: While this commit does not modify userland, in the future SystemServer will be responsible for performing proper shutdown of user processes, including timeouts for stubborn processes etc. - As mentioned above, the task hard-kills remaining user processes. - The task hard-kills all Kernel processes except itself and the Finalizer Task. Since Kernel processes can delay their own shutdown indefinitely if they want to, they have plenty opportunity to perform proper shutdown if necessary. This may become a problem with non-cooperative Kernel tasks, but as seen two commits earlier, for now all tasks will cooperate within a few seconds. - The task waits for the Finalizer Task to clean up all processes. - The task hard-kills and finalizes the Finalizer Task itself, meaning that it now is the only remaining process in the system. - The task syncs and locks all file systems, and then unmounts them. Due to an unknown refcount bug we currently cannot unmount the root file system; therefore the task is able to abort the clean unmount if necessary. - The task performs platform-dependent hardware shutdown as before. This commit has multiple remaining issues (or exposed existing ones) which will need to be addressed in the future but are out of scope for now: - Unmounting the root filesystem is impossible due to remaining references to the inodes /home and /home/anon. I investigated this very heavily and could not find whoever is holding the last two references. - Userland cannot perform proper cleanup, since the Kernel's power state variable is accessed directly by tools instead of a proper userland shutdown procedure directed by SystemServer. The recently introduced Firmware/PowerState procedures are removed again, since all of the architecture-independent code can live in the power state switch task. The architecture-specific code is kept, however.
2023-07-09 22:17:11 +00:00
class PowerStateSwitchTask {
public:
static void shutdown() { spawn(PowerStateCommand::Shutdown); }
static void reboot() { spawn(PowerStateCommand::Reboot); }
private:
static void spawn(PowerStateCommand);
static void power_state_switch_task(void* raw_entry_data);
static ErrorOr<void> kill_all_user_processes();
enum class DoReboot {
No,
Yes,
};
static ErrorOr<void> perform_shutdown(DoReboot);
Kernel: Overhaul system shutdown procedure For a long time, our shutdown procedure has basically been: - Acquire big process lock. - Switch framebuffer to Kernel debug console. - Sync and lock all file systems so that disk caches are flushed and files are in a good state. - Use firmware and architecture-specific functionality to perform hardware shutdown. This naive and simple shutdown procedure has multiple issues: - No processes are terminated properly, meaning they cannot perform more complex cleanup work. If they were in the middle of I/O, for instance, only the data that already reached the Kernel is written to disk, and data corruption due to unfinished writes can therefore still occur. - No file systems are unmounted, meaning that any important unmount work will never happen. This is important for e.g. Ext2, which has facilites for detecting improper unmounts (see superblock's s_state variable) and therefore requires a proper unmount to be performed. This was also the starting point for this PR, since I wanted to introduce basic Ext2 file system checking and unmounting. - No hardware is properly shut down beyond what the system firmware does on its own. - Shutdown is performed within the write() call that asked the Kernel to change its power state. If the shutdown procedure takes longer (i.e. when it's done properly), this blocks the process causing the shutdown and prevents any potentially-useful interactions between Kernel and userland during shutdown. In essence, current shutdown is a glorified system crash with minimal file system cleanliness guarantees. Therefore, this commit is the first step in improving our shutdown procedure. The new shutdown flow is now as follows: - From the write() call to the power state SysFS node, a new task is started, the Power State Switch Task. Its only purpose is to change the operating system's power state. This task takes over shutdown and reboot duties, although reboot is not modified in this commit. - The Power State Switch Task assumes that userland has performed all shutdown duties it can perform on its own. In particular, it assumes that all kinds of clean process shutdown have been done, and remaining processes can be hard-killed without consequence. This is an important separation of concerns: While this commit does not modify userland, in the future SystemServer will be responsible for performing proper shutdown of user processes, including timeouts for stubborn processes etc. - As mentioned above, the task hard-kills remaining user processes. - The task hard-kills all Kernel processes except itself and the Finalizer Task. Since Kernel processes can delay their own shutdown indefinitely if they want to, they have plenty opportunity to perform proper shutdown if necessary. This may become a problem with non-cooperative Kernel tasks, but as seen two commits earlier, for now all tasks will cooperate within a few seconds. - The task waits for the Finalizer Task to clean up all processes. - The task hard-kills and finalizes the Finalizer Task itself, meaning that it now is the only remaining process in the system. - The task syncs and locks all file systems, and then unmounts them. Due to an unknown refcount bug we currently cannot unmount the root file system; therefore the task is able to abort the clean unmount if necessary. - The task performs platform-dependent hardware shutdown as before. This commit has multiple remaining issues (or exposed existing ones) which will need to be addressed in the future but are out of scope for now: - Unmounting the root filesystem is impossible due to remaining references to the inodes /home and /home/anon. I investigated this very heavily and could not find whoever is holding the last two references. - Userland cannot perform proper cleanup, since the Kernel's power state variable is accessed directly by tools instead of a proper userland shutdown procedure directed by SystemServer. The recently introduced Firmware/PowerState procedures are removed again, since all of the architecture-independent code can live in the power state switch task. The architecture-specific code is kept, however.
2023-07-09 22:17:11 +00:00
};
}