Skip to content

[pm] fix race condition and lock protection in rt_pm_device_register/unregister - #12075

Merged
Rbb666 merged 2 commits into
RT-Thread:masterfrom
niuxiaoch:patch-1
Nov 23, 2026
Merged

Rbb666 merged 2 commits into
RT-Thread:masterfrom
niuxiaoch:patch-1

Conversation

@niuxiaoch

@niuxiaoch niuxiaoch commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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

[

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

Fixes #10075
In components/drivers/pm/pm.c:

  1. rt_pm_device_register() previously appended device_pm->list to _pm.device_list before initializing device_pm->device and device_pm->ops, and without interrupt disable protection.
  2. rt_pm_device_unregister() traversed and modified _pm.device_list without interrupt disable protection and called RT_KERNEL_FREE() directly inside the loop.

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

  1. Initialize device_pm fields and list node before appending to _pm.device_list in rt_pm_device_register().
  2. Protect _pm.device_list operations with rt_hw_interrupt_disable() / rt_hw_interrupt_enable() while keeping RT_KERNEL_MALLOC() and RT_KERNEL_FREE() outside the interrupt-disabled critical section to avoid SMP hang ([Bug]components/drivers/pm/pm.c rt_pm_device_register API would be hang when RT_USING_SMP is configured #10075).

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

  • BSP: bsp/qemu-vexpress-a9

  • .config: CONFIG_RT_USING_PM=y

  • action: CI build check

]

当前拉取/合并请求的状态 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
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

…unregister

1. Initialize `device_pm` fields before appending the node to `_pm.device_list` in `rt_pm_device_register()` to prevent exposing uninitialized pointers.
2. Protect `_pm.device_list` modifications with `rt_hw_interrupt_disable()` / `rt_hw_interrupt_enable()` while keeping `RT_KERNEL_MALLOC()` and `RT_KERNEL_FREE()` outside the interrupt-disabled critical section to avoid SMP hang (RT-Thread#10075).

Fixes RT-Thread#10075

Signed-off-by: niuxiaoch <niuxiaolong893@gmail.com>
@CLAassistant

CLAassistant commented Nov 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 patch-1
    Enter PR branch patch-1 in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 patch-1 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the patch-1 branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/drivers/pm/pm.c

🏷️ Tag: components_driver_pm

Reviewers: @wdfk-prog

Changed Files (Click to expand)
  • components/drivers/pm/pm.c

📊 Current Review Status (Last Updated: 2026-11-21 10:44 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review / 审查 — 8fdb470f

结论:COMMENT — 锁的放置本身正确,但“修复竞态”仍不完整。

  • 正确之处:device_pm 先初始化再挂链;RT_KERNEL_MALLOC/RT_KERNEL_FREE 留在临界区外;unregister 先摘链、再在临界区外释放,避免带中断关闭执行阻塞操作。
  • 【P2】_pm_device_suspend() / _pm_device_resume() / _pm_device_frequency_change() 仍无锁遍历 _pm.device_list(pm.c:121、pm.c:153、pm.c:175)。unregister 在 rt_hw_interrupt_enable() 之后才释放节点(pm.c:882-885),若 suspend/resume 正持有已摘链的节点,仍存在 use-after-free 窗口。本 PR 只排除了 register/unregister 之间的链表破坏,未覆盖读者;建议读者侧使用同一把锁(或与 PM 调用上下文一致的 spinlock/mutex),至少应在接口注释中声明 register/unregister 不可与 suspend/resume 并发。
  • 【P3】rt_hw_interrupt_disable() 在 SMP 上只关闭当前核中断,不能阻止其他核同时 register/unregister;若允许跨核调用,链表完整性仍未保证。若框架约定只在初始化阶段单线程注册,请在函数注释中写明。
  • 已核对 RT_DEBUG_NOT_IN_INTERRUPT 在非调试构建下为空实现,新增断言不会引入构建问题。

CI 为 success;未做动态验证。

@wdfk-prog wdfk-prog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Document that _pm.device_list updates are serialized with PM list walkers
via rt_hw_interrupt_disable() (rt_cpus_lock() on SMP), and that the APIs
must not be called from interrupt context or rt_device_pm_ops callbacks.
Restore the original unregister comment wording.
Signed-off-by: niuxiaoch <niuxiaolong893@gmail.com>
@niuxiaoch

Copy link
Copy Markdown
Contributor Author

@BernardXiong 感谢审查!针对 P2/P3 我核对了当前 master 源码:

P2:三个遍历函数都已在同一把锁内被调用——

  • _pm_device_suspend()/_pm_device_resume() 只在 _pm_change_sleep_mode() 中调用,整个过程处于 rt_pm_enter_critical() ~ rt_pm_exit_critical() 之间(pm.c:395 ~ 413/438/481),其默认实现即 rt_hw_interrupt_disable()(pm.c:72-75);
  • _pm_device_frequency_change() 在 _pm_frequency_scaling()(pm.c:198-207)和 rt_pm_run_enter()(pm.c:972-988)中调用,均处于 rt_hw_interrupt_disable() 内。
    遍历方在整个遍历期间持锁,unregister 在同一把锁下摘链、解锁后才释放,因此遍历方不会再引用已摘链节点。

P3:include/rthw.h:146 在 RT_USING_SMP 下将 rt_hw_interrupt_disable 定义为 rt_cpus_lock,而 rt_cpus_lock()(src/cpu_mp.c:168-196)会关本核中断、锁调度器并获取全局自旋锁 _cpus_lock,因此 SMP 下也能提供跨核互斥。

剩余风险是 BSP 覆盖 rt_weak 的 rt_pm_enter_critical() 且互斥强度更弱,或在 rt_device_pm_ops 回调中调用 unregister。我已追加提交,在接口注释中写明上述加锁约定和调用限制,同时恢复了原有的 unregister 注释措辞。请再看一下,谢谢!

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — 199b25c8

结论:APPROVE(增量仅注释,未改行为)。

本轮增量只新增/整理接口注释(pm.c:835-878)。重新核对后,我上一轮 P2 的前提不成立,特此更正:

  • _pm_device_suspend() / _pm_device_resume() 只在 _pm_change_sleep_mode() 的 rt_pm_enter_critical() … rt_pm_exit_critical() 区间内遍历(pm.c:395、pm.c:425、pm.c:442、pm.c:476)。
  • _pm_device_frequency_change() 在 rt_hw_interrupt_disable() 内遍历(pm.c:1008-1017)。
  • rt_pm_enter_critical() 默认实现即 rt_hw_interrupt_disable()(pm.c:72-75);SMP 下该宏映射为 rt_cpus_lock(rthw.h:146),具备跨核互斥。

因此 unregister 解锁后再释放节点的窗口不成立;新增注释与实现一致,仅保留“BSP 覆盖 rt_pm_enter_critical() 时必须提供同等互斥”的约束,且已在注释中声明。CI 全绿;未做动态验证。

@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

nuvoton-m487

  • CODE: .text +80 B (+0.0%, 357,064 B / 524,288 B, total: 68% used)

qemu-virt64-aarch64

  • Code: .text +288 B (+0.0%, 1,012,636 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@Rbb666
Rbb666 merged commit b562d73 into RT-Thread:master Nov 23, 2026
114 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]components/drivers/pm/pm.c rt_pm_device_register API would be hang when RT_USING_SMP is configured

5 participants