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-25 04:00 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.
42155ac to
661170d
Compare
|
@BernardXiong @Rbb666 flush 失败后不会再往 pending 塞;注释标明仅 init 阶段用。这轮没附启动日志,有环境时麻烦瞄一眼 tty。 |
| } | ||
| #endif /* RT_USING_COMPONENTS_INIT */ | ||
|
|
||
| tty_dev = serial->rx_notify.dev; |
There was a problem hiding this comment.
排队时注册已经返回 RT_EOK。如果 flush 丢掉了请求,或实际创建 TTY 失败,rx_notify.dev 还是空的,注销会走到 tty_softc()。处理完 pending 后请先检查 tty_dev,没有创建 TTY 就直接返回。
Registration returns RT_EOK as soon as the request is queued. If the flush drops the request or TTY creation fails, rx_notify.dev stays NULL and unregister reaches tty_softc(). Check tty_dev after handling the pending request and return if no TTY was created.
There was a problem hiding this comment.
这个做完了,我把排队请求的方式改了,顺便附带了被 flush 丢弃和 TTY 创建失败的情况
661170d to
5d055e5
Compare
| * RT_EOK for a queued request, so the only reliable evidence of a live TTY | ||
| * is rx_notify.dev. Without it there is nothing to tear down. */ | ||
| tty_dev = serial->rx_notify.dev; | ||
| if (tty_dev == RT_NULL) |
There was a problem hiding this comment.
TTY 注册成功后,首次打开时才会设置 rx_notify.dev。若从未打开就注销,这里会直接返回,留下已注册的设备节点。请保存注册成功的 TTY 指针,用它区分“未创建”和“已创建但未打开”。
|
确实啊,等我改改 |
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. rt_hw_serial_register_tty() now records the created TTY in the serial device (rt_serial_device::ttydev). rx_notify.dev cannot be used for that: the notify belongs to whoever installs it and is only set when the TTY is opened, so it cannot tell "no TTY was ever created" from "the TTY exists but was never opened". The unregister path now frees a TTY that was created but never opened, and rt_hw_serial_register_tty() rejects a second registration of the same serial device instead of creating a second TTY. tty_rel_gone() already releases the TTY (device object, queues and the serial context through tsw_free) once its last session has left, or immediately for a TTY that was never opened. The unregister path no longer frees the TTY a second time with rt_device_destroy()/lwp_tty_delete(), and the device switch now provides the tsw_free() destructor that lwp_tty_delete() requires.
5d055e5 to
db6f96a
Compare
| * the TTY is opened, so it cannot tell "no TTY was ever | ||
| * created" from "the TTY exists but was never opened". | ||
| */ | ||
| softc->backup_notify = serial->rx_notify; |
There was a problem hiding this comment.
这里在注册时保存 rx_notify,注销时又无条件写回。如果 TTY 一直没打开,期间其他调用方通过 RT_DEVICE_CTRL_NOTIFY_SET 设置了新的回调,注销 TTY 会把它覆盖掉。TTY 的收包走 bypass,请只撤销 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