Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: smart: add reboot service #9654

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

polarvid
Copy link
Contributor

@polarvid polarvid commented Nov 15, 2024

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

Provide software reboot service.

There are several annoying problems with doing the hardware reboot on the current DFS safely:

  1. The rt_console always saves a reference to devfs which keeps us away from unmounting it reasonably;
  2. We can not umount rootfs since it is designed not to do so;

So, the current solution focuses on the RDONLY mounting which cleans up the dirty page, keeps the devices away from writing during hardware resetting.

你的解决方案是什么 (what is your solution)

  • Supported kill-all signal in POSIX signal frameworks.
  • Supported remount(2) and dfs_umount()
  • Supported readonly mounting of filesystem

请提供验证的bsp和config (provide the config and bsp)

  • BSP:

QEMU virt aarch64:

image

RK35 series:

image

  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/workflows/bsp_buildings.yml 详细请参考链接BSP自查

@github-actions github-actions bot added RT-Smart RT-Thread Smart related PR or issues component: lwp Component labels Nov 15, 2024
@polarvid polarvid changed the title Shell/smart reboot feat: smart: add reboot service Nov 15, 2024
@BernardXiong
Copy link
Member

后面可以注意到一点,

  • 之前lwP的实现是相对乱的,一些点上可能想到了一些就实现了对应的功能;
  • 后续可以考虑多往这个方向走
    • 对于系统来说,好的架构首先是清晰的功能模块的划分;
    • 好的模块,它的边界是相对清晰的;相互之间的关系是相对清晰的;
    • 这些清晰都会表现在逻辑自洽上,能很好的自圆其说;
    • 所以从这个角度上来说,好的设计是
      • 清晰的模块划分;<lwP需要拆解成哪些小模块>
      • 清晰的模块定义;<这些lwP小模块的边界,接口定义是什么样的>
      • 并且是逻辑自洽的;<每个lwP小模块的功能是完整,完善的,并是必须的,不冗余的>
      • 然后在这个基础上反复迭代;

@polarvid polarvid force-pushed the shell/smart-reboot branch 2 times, most recently from 632bd59 to a3bf8eb Compare November 18, 2024 09:43
This change introduces the `lwp_pid_for_each` function, which provides a
convenient and thread-safe method for iterating over PIDs with a user-
defined callback. This addition is necessary to support cases where
operations must be performed on each PID in the balanced tree, enhancing
flexibility and modularity for PID management.

Changes:
- Added `lwp_pid_for_each` function in `lwp_pid.c` to allow iteration
  over PIDs using a callback function and optional data parameter.
- Defined a new internal `pid_foreach_param` structure to encapsulate
  the callback and data for the iteration.
- Added `_before_cb` helper function for executing the callback on each
  PID node during AVL traversal.
- Ensured thread safety by acquiring and releasing the PID lock around
  the AVL traversal within `lwp_pid_for_each`.
- Updated `lwp_pid.h` with the `lwp_pid_for_each` function prototype and
  included `rtthread.h` for necessary types.

Signed-off-by: Shell <[email protected]>
This change introduces the `lwp_signal_kill_all` function, which allows a
signal to be sent to all processes in the system. The function iterates
over all PIDs and sends the specified signal to each process, except those
that are protected from signals. This enhancement provides a convenient way
to broadcast signals across all processes in the system.

Changes:
- Added `lwp_signal_kill_all` function in `lwp_signal.c` to broadcast a
  signal to all processes by iterating over all PIDs using the existing
  `lwp_pid_for_each` function.
- Introduced a new `kill_all_param` structure to encapsulate the signal
  information needed for killing processes.
- Added internal `_kill_each` helper function for sending the signal to each
  PID.
- Updated `lwp_signal.h` with the new function prototype for `lwp_signal_kill_all`.
- Modified `sys_kill` in `lwp_syscall.c` to call `lwp_signal_kill_all` when
  a process is not specified.

Signed-off-by: Shell <[email protected]>
This change introduces the `lwp_runtime.c` component, which provides the
necessary runtime environment for the init process, including boot scripts,
shutdown, and poweroff functionalities. The initialization logic has been
moved from `lwp.c` to this new file, enhancing modularity and flexibility
in handling LWP runtime tasks.

Changes:
- Moved the `lwp_startup` function from `lwp.c` to `lwp_runtime.c` to handle
  system initialization and runtime environment setup.
- Added `lwp_teardown` placeholder for system shutdown and cleanup tasks in
  the future (though not yet implemented).
- Introduced the `LWP_USING_RUNTIME` configuration option in `Kconfig` to
  conditionally enable the runtime environment.
- Updated the `SConscript` to conditionally include `lwp_runtime.c` based on
  the `LWP_USING_RUNTIME` configuration.
- Removed the old `lwp_startup` code from `lwp.c`, simplifying the file.

Signed-off-by: Shell <[email protected]>
The patch introduces support for system reboot functionality and process teardown,
allowing for a clean shutdown and unmounting of the root filesystem. This is
necessary for ensuring a proper system shutdown process, especially when dealing
with resource cleanup and ensuring that all processes have exited before system
shutdown.

Changes:
- Added `lwp_teardown()` function to handle process cleanup and system teardown.
- Introduced `lwp_pid_wait_for_empty()` to wait for process ID table emptiness
  before proceeding with shutdown.
- Updated `dfs_mnt_unref()` to trigger callbacks when unmounting a filesystem.
- Added new reboot types (`RB_AUTOBOOT`, `RB_POWER_OFF`) and implemented their
  corresponding actions, including cleanup of processes and unmounting root
  filesystem.
- Extended `sys_reboot()` to handle reboot and power off types with appropriate
  callbacks for process and filesystem teardown.
- Introduced callback mechanism for root filesystem unmount notifications.

Signed-off-by: Shell <[email protected]>
This patch introduces a remount feature for the DFS, allowing for the
modification of mount parameters without unmounting the filesystem,
the remount functionality helps modify certain mount flags (like `MS_RDONLY`) without
requiring an unmount, providing more control over mounted filesystems in the system.

The updates is essential for user space init proc to cleanup the runtime
resource, ensuring clean handling of cached data and enhancing system
robustness during power down processing.

Changes:
- Defined new constants for remount flags in `dfs_fs.h`.
- Added the `dfs_remount()` function in `dfs_fs.c` to handle remount operations.
- Introduced a check for unsupported flags and handle error conditions such as invalid paths
  or non-directory targets.
- Updated the `dfs_mnt` structure in `dfs_mnt.h` to include a read-only flag (`MNT_RDONLY`).
- The `dfs_remount()` function allows changing the read-only status of a mounted filesystem.
- Added `MNT_LAZY_UMNT` and `MNT_RDONLY` flags to `dfs_mnt` structure.
- Introduced `dfs_mnt_setflags` function for dynamic flag management.
- Updated `dfs_remount` to utilize `dfs_mnt_setflags` for flag setting.
- Enhanced unmount operations with `dfs_mnt_umount_iter` and lazy unmounting.
- Added `dfs_pcache_clean` to handle cache cleanup for read-only mounts.
- Improved error reporting in `dfs_umount` for better user feedback.
- Refactored `sys_mount` to streamline parameter handling and support remounts.
- Introduced `_cp_from_usr_string` helper for user-space string operations.
- Updated internal APIs to ensure consistency in reference count management.

Signed-off-by: Shell <[email protected]>
According to the POSIX.1, we should give a EBUSY on umount(2) of busy
filesystem.

Signed-off-by: Shell <[email protected]>
To improve the readability of the menuconfig.

Signed-off-by: Shell <[email protected]>
@polarvid polarvid marked this pull request as ready for review November 18, 2024 12:59
@polarvid
Copy link
Contributor Author

后面可以注意到一点,

  • 之前lwP的实现是相对乱的,一些点上可能想到了一些就实现了对应的功能;

  • 后续可以考虑多往这个方向走

    • 对于系统来说,好的架构首先是清晰的功能模块的划分;

    • 好的模块,它的边界是相对清晰的;相互之间的关系是相对清晰的;

    • 这些清晰都会表现在逻辑自洽上,能很好的自圆其说;

    • 所以从这个角度上来说,好的设计是

      • 清晰的模块划分;<lwP需要拆解成哪些小模块>
      • 清晰的模块定义;<这些lwP小模块的边界,接口定义是什么样的>
      • 并且是逻辑自洽的;<每个lwP小模块的功能是完整,完善的,并是必须的,不冗余的>
      • 然后在这个基础上反复迭代;

好啊,后续推进文档中心完善可以再多来 review 一下设计。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: lwp Component RT-Smart RT-Thread Smart related PR or issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants