Repository navigation
[kernel][ipc] preserve FIFO order for mb/mq handoff - #12045
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: kernelReviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-11-14 18:01 CST)
📝 Review Instructions
|
|
Rbb666
left a comment
There was a problem hiding this comment.
当前 FIFO 交接实现存在两个会影响消息可靠性的阻塞问题,详见行内评审。请补充 mailbox/messagequeue 接收者先阻塞、发送后恢复,以及预留期间队列被填满/接收取消的回归测试,再处理当前失败的 IPC CI。该 PR 为单一提交,无需压缩。
|
|
||
| level = rt_spin_lock_irqsave(&(mb->spinlock)); | ||
|
|
||
| if (_ipc_handoff_take(mb, thread, _IPC_HANDOFF_MB_RX, &handoff)) |
There was a problem hiding this comment.
[P1] 唤醒后必须再次领取 RX handoff。这里仅在进入接收函数时检查一次预留记录;空 mailbox 的接收者随后在 while 中挂起,发送端把值写入 handoff 后保持 mb->entry == 0。接收者被唤醒时从 rt_schedule() 后继续执行,未回到此处,因而再次挂起;永久等待可一直无法收到消息,有限等待则错误超时。_rt_mq_recv 的 4252 行有同样问题。请在每次唤醒并重新持有对象锁后先领取预留值/节点,再决定是否继续等待,并覆盖“接收先阻塞、只发送一次”的 FIFO 回归用例。
| level = rt_spin_lock_irqsave(&(mb->spinlock)); | ||
| mb->msg_pool[mb->in_offset] = value; | ||
| ++mb->in_offset; | ||
| if (mb->in_offset >= mb->size) |
There was a problem hiding this comment.
[P1] 归还 RX 预留值时仍须保证 mailbox 容量不变量。RX handoff 不计入发送端容量检查,可构造 size=1 的时序:A 被预留给接收者 R;R 因上一条指出的漏取问题再次挂起并启动剩余超时;超时将 R 移出等待队列后,高优先级发送者在 R 获得 CPU 前把 B 写满公共队列;随后 R 进入这里归还 A。此处无条件写 msg_pool[in_offset] 并递增 entry,会覆盖 B,并令 entry=2 超过 size=1。RT_MB_ENTRY_MAX 只限制计数类型上界,不能保护实际容量。请为未消费 RX 预留保留相应容量,或设计不会覆盖公共队列的归还路径,并覆盖上述超时/发送交错;保证消息不丢失、不重复且 entry 始终不超过 size。
7df2d35 to
ad4db57
Compare
Rbb666
left a comment
There was a problem hiding this comment.
复核当前提交后,基本 FIFO 交接路径有效,但以下资源回收、并发和兼容性问题仍需处理。
| { | ||
| if (_ipc_handoffs[i].thread == RT_NULL) | ||
| { | ||
| _ipc_handoffs[i].ipc = ipc; |
There was a problem hiding this comment.
[P1] 在线程删除时回收 handoff。若消息已预留给接收者,但它尚未领取就被 rt_thread_delete()/rt_thread_detach() 结束,线程清理路径不会移除此记录或归还资源。容量为 1 时,其他线程会同时遇到接收报空、发送报满,mb/mq 均可能永久失去可用容量。请将预留回收接入线程退出路径。
| rt_ubase_t value; | ||
| rt_base_t level; | ||
|
|
||
| if (!_ipc_handoff_take(mb, thread, _IPC_HANDOFF_MB_RX, &value)) |
There was a problem hiding this comment.
[P1] 在邮箱锁内领取并归还 RX 预留。这里先移除 handoff,再获取 mb->spinlock,容量统计会提前释放该空位。若中断或另一 CPU 此时发送 B 填满邮箱,后续 entry < size 判断失败,原本要归还的 A 被静默丢弃。请让记录移除、容量统计和消息归还在同一对象锁下完成。
| rt_uint8_t kind; | ||
| }; | ||
|
|
||
| /* FIFO handoff records cover the wakeup-to-dequeue window. If all records are |
There was a problem hiding this comment.
[P2] 交接表满时仍会重现 #4940。所有 mb/mq 共用 16 条记录(Nano 为 8 条);已有等待者被唤醒但尚未领取资源时,后续交接会退回公共队列路径,后来到达的高优先级线程仍可抢走资源。该回退使 FIFO 保证依赖全局负载,需要提供表满时仍保持顺序的处理方式。
| if (!rt_list_isempty(&(mq->suspend_sender_thread))) | ||
| { | ||
| sender = rt_susp_list_dequeue(&(mq->suspend_sender_thread), RT_EOK); | ||
| if (sender && _ipc_handoff_put(mq, sender, (rt_ubase_t)msg, _IPC_HANDOFF_MQ_TX)) |
There was a problem hiding this comment.
[P2] 将 TX 资源预留限定为 FIFO。这里未检查对象 flag,PRIO 队列也会把释放的节点预留给等待发送者。若低优先级发送者被唤醒后尚未运行,后来到达的高优先级发送者会从原本可立即发送变为 -RT_EFULL 或阻塞,与 PR 声称的 PRIO 行为不变不符。mailbox 接收路径中的 TX handoff 也有同样问题。
| } | ||
| if (mq->entry < mq->max_msgs) | ||
| { | ||
| msg->next = (struct rt_mq_message *)mq->msg_queue_head; |
There was a problem hiding this comment.
[P2] 归还消息时保持消息优先级顺序。开启 RT_USING_MESSAGEQUEUE_PRIORITY 后,若优先级 1 的消息预留给接收者,随后优先级 10 的消息进入公共队列,接收者被信号打断后会在这里把优先级 1 的消息直接插到队首,导致低优先级消息先被接收。请按消息优先级重新插入,而不是无条件头插。
|
另外ci目前看下来是存在问题的,需要保证utest正常通过,并新增对应可以复现原有问题的测试用例进行测试 |
多谢,给我时间,我慢慢改 |
ad4db57 to
497b7cc
Compare
|
@Rbb666 已按意见改完,tip:。
|
BernardXiong
left a comment
There was a problem hiding this comment.
结论:当前 head 的 IPC utest 失败,属于代码/行为回归,不是环境问题。 / Verdict: the IPC utest failure at this head is a code regression, not infrastructure.
utest_auto_run 的 5 个 kernel/ipc.cfg job 全部失败(A9、A9-rtsmart、A9-smp、RISCV-rtsmart、XUANTIE-rtsmart),失败点是 core.messagequeue:
[E/utest] [ ASSERT ] at (messagequeue_tc.c); func: (mq_recv_case:183); msg: ((recv_buf[var] == (var + 1)) is false)
[E/utest] [ ASSERT ] at (messagequeue_tc.c); func: (mq_recv_case:196); msg: ((recv_buf[var] == (MAX_MSGS - var)) is false)
对应 messagequeue_tc.c:183 和 messagequeue_tc.c:196。这说明 FIFO 交接在普通(非 PRIO)收发路径上改变了消息的到达顺序或内容,现有用例可以稳定复现。
失败 job:https://github.com/RT-Thread/rt-thread/actions/runs/43405946047/job/131820198892
建议先在本地 QEMU 跑 utest_run core.messagequeue,定位是 mailbox 还是 messagequeue 的交接值不对,再修;同时补一条“接收者先阻塞、发送后恢复”的 FIFO 顺序用例。
The five failing kernel/ipc.cfg jobs all fail in core.messagequeue with wrong received values, so the handoff changes normal FIFO message ordering. Please reproduce with utest_run core.messagequeue and add a regression test for the blocking-receiver ordering.
497b7cc to
de01811
Compare
Fix RT-Thread#4940 Reserve the mailbox value or message queue node for the first FIFO waiter before resuming it, preventing race conditions where subsequent receivers steal in-flight handoffs. When a receiver exits or is deleted/detached before consuming its reserved handoff, safely reclaim the record and return capacity or the message back to the object under object spinlock protection, avoiding permanently wedged capacity. In addition, when urgent messages arrive, prioritize them ahead of in-flight normal handoffs, and preserve message priority order when returning unconsumed nodes under RT_USING_MESSAGEQUEUE_PRIORITY. Restrict TX handoffs strictly to FIFO objects and expand handoff table capacity to avoid contention.
de01811 to
00e7f70
Compare
|
@Rbb666 @BernardXiong 补了 rt_ipc_handoff_thread_cleanup 的原型声明,修掉 -Werror=missing-prototypes;handoff 回收和 FIFO 顺序改动还在。麻烦再看一眼。 |
中文
问题
RT_IPC_FLAG_FIFO下 mb/mq 只唤醒队首等待者,数据仍留在公共队列;更高优先级线程可非阻塞抢走,原等待者只能重新挂到队尾,FIFO 翻转(#4940)。改动
验证
静态对照 issue 复现时序;未开虚拟机、未烧板。
English
Problem
With
RT_IPC_FLAG_FIFO, mb/mq only resumes the head waiter while leaving the item in the shared queue, so a higher-priority thread can non-blocking-steal it and the original waiter requeues at the tail (#4940).Changes
Test plan
Static check against the reported schedule; no VM or board run.
Fixes #4940