Repository navigation
[drivers][pin] publish legacy ISR handler under IRQ protection - #12040
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 15:58 CST)
📝 Review Instructions
|
|
BernardXiong
left a comment
There was a problem hiding this comment.
批准。 复核 head cbbb1450:legacy ISR 的 pin/mode/hdr/args 发布和 memset 清除都放在关中断区间内,避免 pin_pic_handle_isr() 读到半更新的一对字段;原生 pin_attach_irq/pin_detach_irq 路径不受影响。CI 全绿(107 成功)。
Approved. The legacy ISR fields are now published and cleared atomically with interrupts disabled, so the ISR cannot observe a half-updated handler pair. The native DM path is untouched. CI is green.
| } | ||
|
|
||
| /* publish the handler pair as one update for pin_pic_handle_isr() */ | ||
| level = rt_hw_interrupt_disable(); |
There was a problem hiding this comment.
[P2] SMP 下还需要同步 ISR 读端
这里保护了写端,但 pin_pic_handle_isr() 仍直接读取 hdr/args。SMP 下,rt_hw_interrupt_disable() 会调用 rt_cpus_lock(),关闭本核中断并持有 _cpus_lock;另一核的 GPIO ISR 并不持有这把锁。因此,CPU 0 写入新 hdr 后、写入新 args 前,CPU 1 仍可能调用 new_handler(old_args),导致参数错配甚至崩溃。detach 的清除也没有与另一核的读取互斥。当前改动只解决了同核中断抢占的情况。
建议新增一把专门保护 legacy_isr 的锁:
static RT_DEFINE_SPINLOCK(pin_irq_lock);attach 发布和 detach 清除都改用 rt_spin_lock_irqsave(&pin_irq_lock) / rt_spin_unlock_irqrestore(&pin_irq_lock, level)。ISR 读端也要使用同一把锁,不能只替换写端的 API。在 pin_pic_handle_isr() 中,取得 legacy_isr 后按下面的方式读取和调用,变量声明放在所在代码块开头:
rt_base_t level;
void (*hdr)(void *);
void *args;
level = rt_spin_lock_irqsave(&pin_irq_lock);
hdr = legacy_isr->hdr;
args = legacy_isr->args;
rt_spin_unlock_irqrestore(&pin_irq_lock, level);
if (hdr)
{
hdr(args);
}锁只覆盖字段更新和读取,不要持锁调用用户回调,避免回调中再次注册或解绑时死锁。现有 pin_lock 用于设备链表,建议保留其用途,不直接复用。
主机桩测试中,在写入 hdr 后暂停写端、让另一线程执行 ISR,可以复现 new_handler(old_args);未做板级验证。
Fix RT-Thread#11784 When the GPIO controller has no pin_attach_irq operation, pin_api_attach_irq() writes legacy_isr->hdr and legacy_isr->args as two independent stores. An interrupt arriving concurrently could make pin_pic_handle_isr() call the new handler with the old argument. Protect legacy_isr updates and clears in pin_api_attach_irq() and pin_api_detach_irq() with a dedicated spinlock pin_irq_lock (via rt_spin_lock_irqsave). In pin_pic_handle_isr(), snapshot the hdr and args fields under pin_irq_lock, then invoke the callback after releasing the lock to avoid executing user handlers while holding the spinlock.
cbbb145 to
afa89a6
Compare
|
@Rbb666 attach/detach 和 ISR 读端共用 pin_irq_lock,回调在解锁后调用。 |
中文
问题
GPIO 无
pin_attach_irq时走 legacy 路径,pin_api_attach_irq未关中断就发布hdr/args,ISR 可能读到半更新(#11784)。改动
验证
静态确认发布与 ISR 读路径互斥;未跑板。
English
Problem
On the legacy pin IRQ path,
pin_api_attach_irqpublishedhdr/argswithout IRQ protection while the ISR reads them (#11784).Changes
Test plan
Static check that publish and ISR reads are mutually exclusive; no board run.
Fixes #11784