Repository navigation
[smart][serial] defer tty register until workqueue ready - #12042
cms19859230182-lang wants to merge 1 commit into
Conversation
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-11-14 16:05 CST)
📝 Review Instructions
|
|
Rbb666
left a comment
There was a problem hiding this comment.
延迟注册的初始化顺序合理,但新增 flush 在工作队列创建失败时可能无限循环。请修复下方错误路径,并补充工作队列分配失败的验证。本次为静态调用链核对,未进行板级验证。
| rt_err_t rc; | ||
|
|
||
| pending = rt_slist_entry(node, struct _tty_pending_registration, node); | ||
| rc = rt_hw_serial_register_tty(pending->serial); |
There was a problem hiding this comment.
[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 能及时返回。
8f408dd to
42155ac
Compare
|
@Rbb666 已按意见改完,tip:。
|
BernardXiong
left a comment
There was a problem hiding this comment.
结论:实现方向正确,未发现阻塞性逻辑问题;但缺少运行时验证,建议补充后再合。 / 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 处理。
两点建议:
-
[一般] 缺少运行时证据:这是修启动挂死的时序问题,当前只做了静态核对。请在
bsp/qemu-virt64-aarch64(或其他 Smart BSP)上启动一次,确认 board 阶段注册的串口在启动后确实出现(例如 msh 下ls /dev能看到对应的ttyS*),并附启动日志。仅靠编译通过不能证明 #11718 的挂死已消失。 -
[一般] 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.
42155ac to
661170d
Compare
|
@BernardXiong @Rbb666 flush 失败后不会再往 pending 塞;注释标明仅 init 阶段用。这轮没附启动日志,有环境时麻烦瞄一眼 tty。 |
中文
问题
Smart 下
rt_hw_serial_register_tty()常在 board 阶段执行,而_ttyworkq由INIT_PREV更晚创建,时序倒挂可导致启动挂死(#11718)。改动
serial_tty.c:workqueue 未就绪时把串口挂入 pendingINIT_COMPONENT_EXPORTflush:workqueue 就绪后再完成 tty 注册验证
静态核对 board / PREV / COMPONENT 时序;未烧板。
English
Problem
With Smart enabled,
rt_hw_serial_register_tty()often runs in the board phase while_ttyworkqis created later byINIT_PREV, so boot can hang (#11718).Changes
serial_tty.c, queue the serial when the workqueue is not readyINIT_COMPONENT_EXPORThook after the workqueue existsTest plan
Static check of board / PREV / COMPONENT ordering; no board run.
Fixes #11718