Skip to content

[smart][serial] defer tty register until workqueue ready - #12042

Open
cms19859230182-lang wants to merge 1 commit into
RT-Thread:masterfrom
cms19859230182-lang:fix/serial-tty-defer-register
Open

cms19859230182-lang wants to merge 1 commit into
RT-Thread:masterfrom
cms19859230182-lang:fix/serial-tty-defer-register

Conversation

@cms19859230182-lang

Copy link
Copy Markdown
Contributor

中文

问题

Smart 下 rt_hw_serial_register_tty() 常在 board 阶段执行,而 _ttyworkq 由 INIT_PREV 更晚创建,时序倒挂可导致启动挂死(#11718)。

改动

  • serial_tty.c:workqueue 未就绪时把串口挂入 pending
  • INIT_COMPONENT_EXPORT flush:workqueue 就绪后再完成 tty 注册
  • 晚注册路径行为不变

验证

静态核对 board / PREV / COMPONENT 时序;未烧板。

English

Problem

With Smart enabled, rt_hw_serial_register_tty() often runs in the board phase while _ttyworkq is created later by INIT_PREV, so boot can hang (#11718).

Changes

  • In serial_tty.c, queue the serial when the workqueue is not ready
  • Flush pending tty registration from an INIT_COMPONENT_EXPORT hook after the workqueue exists
  • Keep late-registration paths unchanged

Test plan

Static check of board / PREV / COMPONENT ordering; no board run.

Fixes #11718

@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 分支 fix/serial-tty-defer-register
    Enter PR branch fix/serial-tty-defer-register 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.

完成后,提交将自动更新至 fix/serial-tty-defer-register 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/serial-tty-defer-register 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 12, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

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

📊 Current Review Status (Last Updated: 2026-11-14 16:05 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.

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

k230

  • SRAM: .eh_frame +96 B, .sbss +16 B, .text +120 B (+0.0%, 1,391,672 B / 60,682,240 B, total: 2% used)

No memory changes detected for:


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

@Rbb666 Rbb666 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.

延迟注册的初始化顺序合理,但新增 flush 在工作队列创建失败时可能无限循环。请修复下方错误路径,并补充工作队列分配失败的验证。本次为静态调用链核对,未进行板级验证。

rt_err_t rc;

pending = rt_slist_entry(node, struct _tty_pending_registration, node);
rc = rt_hw_serial_register_tty(pending->serial);

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.

[P2] flush 前确认工作队列已成功创建,避免将同一个注册请求反复入队

在关闭 RT_DEBUG 的配置中,_tty_workqueue_init() 的 RT_ASSERT(_ttyworkq != RT_NULL) 不会阻止 rt_workqueue_create() 分配失败后的启动流程,此时 _ttyworkq 仍为 NULL。这里调用 rt_hw_serial_register_tty() 会再次走 pending 分支:pop 一个节点、分配并 append 一个新节点、返回 RT_EOK,然后释放旧节点。只要剩余内存仍足够分配这个小节点(即使不足以创建工作队列线程栈),队列永远不为空,init 线程会在此持久循环,系统无法启动。

请在 flush 入口检测 _ttyworkq == RT_NULL 并返回明确错误,或把 flush 放到工作队列确认创建成功后的路径;不要在队列未就绪时重新调用会再次排队的注册入口。建议用工作队列分配失败、小 pending 分配仍成功的故障注入场景验证 flush 能及时返回。

@cms19859230182-lang
cms19859230182-lang force-pushed the fix/serial-tty-defer-register branch from 8f408dd to 42155ac Compare November 13, 2026 10:43
@cms19859230182-lang

Copy link
Copy Markdown
Contributor Author

@Rbb666 已按意见改完,tip:。

  1. flush 入口判空 _ttyworkq,为 NULL 直接清空 pending 节点并返回 -RT_ERROR,杜绝反复入队死循环。
    未烧板/未开 VM;单提交已 force-with-lease。

@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.

结论:实现方向正确,未发现阻塞性逻辑问题;但缺少运行时验证,建议补充后再合。 / Verdict: no blocking logic issue found, but runtime evidence is missing.

已核对:

  • _ttyworkq 由 INIT_PREV_EXPORT 创建,board 阶段注册的串口会进入 pending 链表,INIT_COMPONENT_EXPORT 再 flush,时序正确;
  • flush 前 _ttyworkq == RT_NULL 的分支会清空 pending 并返回错误,不会反复入队死循环;
  • rt_hw_serial_unregister_tty() 会先摘除 pending 节点,避免把已排队但尚未注册的串口当成已注册 tty 处理。

两点建议:

  1. [一般] 缺少运行时证据:这是修启动挂死的时序问题,当前只做了静态核对。请在 bsp/qemu-virt64-aarch64(或其他 Smart BSP)上启动一次,确认 board 阶段注册的串口在启动后确实出现(例如 msh 下 ls /dev 能看到对应的 ttyS*),并附启动日志。仅靠编译通过不能证明 #11718 的挂死已消失。

  2. [一般] flush 失败后 pending 仍会继续堆积:_tty_pending_flush() 在 _ttyworkq == RT_NULL 时清空链表并返回,但之后如果还有 rt_hw_serial_register_tty() 调用,仍会在 serial_tty.c:326-343 分配节点挂到同一个链表,而 flush 已经执行过,这些节点再也不会被消费。工作队列创建失败时这会变成内存泄漏,并且串口永远不会注册。建议加一个 _tty_flush_done 标志,或在 flush 之后让注册直接返回 -RT_ERROR / 走原路径。

其它:_tty_pending_registrations 没有锁保护。目前只在启动阶段使用可以接受,但建议在注释里明确“仅初始化阶段、调度器启动前访问”,避免后续有人在运行时调用时踩坑。

The init ordering and failure path look correct. Please add a QEMU boot log proving the deferred tty appears after board-phase registration, and make the post-flush failure path explicit so a failed workqueue creation cannot keep queuing nodes forever.

Fix RT-Thread#11718

When board code registers a serial device before INIT_PREV_EXPORT initializes
the tty workqueue, defer the registration onto a pending slist and flush it
during INIT_COMPONENT_EXPORT.

The pending slist is accessed exclusively during early single-threaded boot
before the scheduler starts. Once flush runs, mark it completed via
_tty_flush_done; subsequent registrations cannot append further pending nodes
if workqueue creation failed, preventing unbounded accumulation of pending
allocations.
@cms19859230182-lang
cms19859230182-lang force-pushed the fix/serial-tty-defer-register branch from 42155ac to 661170d Compare November 14, 2026 08:03
@cms19859230182-lang
cms19859230182-lang marked this pull request as draft November 14, 2026 09:22
@cms19859230182-lang

Copy link
Copy Markdown
Contributor Author

@BernardXiong @Rbb666 flush 失败后不会再往 pending 塞;注释标明仅 init 阶段用。这轮没附启动日志,有环境时麻烦瞄一眼 tty。

@cms19859230182-lang
cms19859230182-lang marked this pull request as ready for review November 14, 2026 10:02

This branch has not been deployed

No deployments
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] [Smart][serial/tty] board 阶段注册 tty 时 workqueue 未建导致挂死,所有 Smart+board 阶段注册串口的架构均受影响

3 participants