Skip to content

[drivers][pin] publish legacy ISR handler under IRQ protection - #12040

Open
cms19859230182-lang wants to merge 1 commit into
RT-Thread:masterfrom
cms19859230182-lang:fix/pin-legacy-isr-publish
Open

cms19859230182-lang wants to merge 1 commit into
RT-Thread:masterfrom
cms19859230182-lang:fix/pin-legacy-isr-publish

Conversation

@cms19859230182-lang

Copy link
Copy Markdown
Contributor

中文

问题

GPIO 无 pin_attach_irq 时走 legacy 路径,pin_api_attach_irq 未关中断就发布 hdr/args,ISR 可能读到半更新(#11784)。

改动

  • 在关中断(或等价临界区)内发布/清除 legacy handler 对

验证

静态确认发布与 ISR 读路径互斥;未跑板。

English

Problem

On the legacy pin IRQ path, pin_api_attach_irq published hdr/args without IRQ protection while the ISR reads them (#11784).

Changes

  • Publish/clear the legacy handler pair under IRQ disable (or an equivalent critical section)

Test plan

Static check that publish and ISR reads are mutually exclusive; no board run.

Fixes #11784

@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/pin-legacy-isr-publish
    Enter PR branch fix/pin-legacy-isr-publish 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/pin-legacy-isr-publish 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/pin-legacy-isr-publish 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 11, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/drivers/pin/dev_pin_dm.c

📊 Current Review Status (Last Updated: 2026-11-14 15:58 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 11, 2026 •

Copy link
Copy Markdown

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

批准。 复核 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.

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

SMP 下的回调对同步还不完整,修改建议见行内意见。

Comment thread components/drivers/pin/dev_pin_dm.c Outdated
}

/* publish the handler pair as one update for pin_pic_handle_isr() */
level = rt_hw_interrupt_disable();

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] 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);未做板级验证。

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

复核当前 head 后,之前的 SMP 行内意见 仍未解决:attach/detach 的写端关闭本核中断,ISR 读端没有参与同一把锁,因此另一核仍能读取不匹配的 hdr/args,detach 也可能与另一核的调用发生竞态。该问题影响本 PR 声称保证的回调对一致性,需要修正后再合并。请按已有行内建议同步读写两端;此处不重复创建相同的行内评论。

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.
@cms19859230182-lang
cms19859230182-lang marked this pull request as draft November 14, 2026 09:22
@cms19859230182-lang

Copy link
Copy Markdown
Contributor Author

@Rbb666 attach/detach 和 ISR 读端共用 pin_irq_lock,回调在解锁后调用。

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

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] pin_api_attach_irq legacy path publishes hdr and args without IRQ protection

3 participants