[BUG] RP2040: task-lock fast path missing acquire barrier
维护者通常 3 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 半天
- 新手友好度
- 15/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- c
调研方向
The bug is in vPortRecursiveLock() in portable/ThirdParty/GCC/RP2040/include/portmacro.h. Compare the fast path through spin_try_lock_unsafe() with the contended path through spin_lock_unsafe_blocking(), which already calls __mem_fence_acquire(). A fix would need to add acquire ordering to the fast path. Pull request #1440 is already open against this issue, so check it before starting.
由索引模型根据 Issue 内容生成。
描述
Describe the bug
vPortRecursiveLock() in portable/ThirdParty/GCC/RP2040/include/portmacro.h (current main)
acquires the SIO task lock on the uncontended fast path via spin_try_lock_unsafe() — a bare
SIO register read (return *lock;) with no memory ordering. The acquire barrier
(__mem_fence_acquire() == __dmb()) lives only inside spin_lock_unsafe_blocking(), i.e. on the
contended path. When spin_try_lock_unsafe() succeeds (the common case), control falls straight
through to the lock-protected bookkeeping with no acquire barrier, so the new holder has no
happens-before edge to the previous holder's critical-section writes and may observe stale state
under weak memory.
portGET_TASK_LOCK drives taskENTER_CRITICAL(), so this affects every kernel structure read after
the lock is taken — it surfaces first on the lock's own ucRecursionCountByLock. This is a C11 data
race (undefined behavior); the access has no happens-before regardless of whether a given core
actually reorders.
Code (current main):
if (uxAcquire) {
if (!spin_try_lock_unsafe(pxSpinLock)) { // fast path: bare register read, NO ordering
if (ucOwnedByCore[xCoreID][ulLockNum]) { ... return; } // recursive
spin_lock_unsafe_blocking(pxSpinLock); // contended: spins, THEN __mem_fence_acquire()
}
/* FAST PATH falls through here with NO acquire barrier */
configASSERT(ucRecursionCountByLock[ulLockNum] == 0); // reads protected metadata
ucRecursionCountByLock[ulLockNum] = 1;
ucOwnedByCore[xCoreID][ulLockNum] = 1;
}
This is a portability/correctness defect against the weak-memory contract the SIO spinlock targets —
present even on a core whose in-order pipeline happens to mask the reorder (see the To Reproduce note).
Target
- Development board: Raspberry Pi Pico (RP2040, dual Cortex-M0+)
- Instruction Set Architecture: Armv6-M
- IDE and version: N/A — analysis against upstream source, not a board build
- Toolchain and version: N/A — defect identified by source review + formal memory-model checking
Host
- Host OS: Linux
- Version: N/A
To Reproduce
Note: this was found and verified at the memory-model level, not by observing a failure on
physical RP2040 hardware. RP2040 is in-order (Cortex-M0+), so the reorder may be masked in practice;
the C11 data race (UB) is present regardless.
Tools: GenMC v0.17.0 (RC11) and herd7 (AArch64 model).
bash harness/run_rq1.sh # GenMC: faithful=race, broken=race, fix=clean (3/3)
herd7 litmus/RQ1_handoff_noacq.litmus # Observation Sometimes 1 3 (stale read allowed)
herd7 litmus/RQ1_handoff_acq.litmus # Observation Never 0 3 (forbidden with the fix)
Minimal handoff litmus — writer does STR data; DMB; STR lock=0, reader does LDR lock; LDR data
with no acquire between, checking exists (reader sees lock==0 /\ data==stale). GenMC
counterexample: core0's acquiring CAS reads-from core1's release (mutual exclusion holds), but with
no trailing acquire fence it does not synchronize-with the release, so core1's critical-section write
is not happens-before core0's read → race.
Expected behavior
The lock holder acquired via the fast path should have an acquire barrier before reading
lock-protected state, establishing happens-before with the previous holder's release — the same
ordering the contended path already gets from __mem_fence_acquire().
Screenshots
N/A.
Additional context
Fix is one line, self-contained in the FreeRTOS port — no pico-sdk change required
(__mem_fence_acquire() is already used in this same file inside spin_lock_unsafe_blocking()):
spin_lock_unsafe_blocking(pxSpinLock);
}
__mem_fence_acquire(); /* fast path carries no acquire ordering */
configASSERT(ucRecursionCountByLock[ulLockNum] == 0);
Unconditional, so the contended path gets one redundant (harmless) __dmb(). A no-redundancy
variant puts the fence in an else of the try-lock if (fast path only). Equivalent root-cause
alternatives, if preferred: give spin_try_lock_unsafe() acquire semantics in pico-sdk, or add a
fencing try-lock variant.
I've verified both the defect and the fix locally with GenMC (RC11) and herd7 (AArch64). Happy to
submit a PR for the portmacro.h fast-path fence — or defer to a pico-sdk fix if you prefer that
layer. Full write-up + counterexample trace available on request.
- 主要语言
- C
- 星标
- 4.6k
- 派生
- 1.6k
- 平均合并
- 2 天 22 小时
- 30 天内合并 PR
- 4
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
FreeRTOS/FreeRTOS-Kernel 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
FreeRTOS/FreeRTOS-Kernel#1542 ·
维护者通常 3 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
FreeRTOS/FreeRTOS-Kernel#1520 ·
维护者通常 3 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 62/100
FreeRTOS/FreeRTOS-Kernel#1494 · 3 条评论 ·
维护者通常 3 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 25/100
FreeRTOS/FreeRTOS-Kernel#1554 ·
维护者通常 3 天内回复
-
[BUG] event_wait_timed can't return false可能已有人在做 @TedLyngmo 于 39 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 38/100
FreeRTOS/FreeRTOS-Kernel#1546 ·
维护者通常 3 天内回复
查看 FreeRTOS/FreeRTOS-Kernel 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 72/100
-
bug
难度 1/5 1 小时以内 新手友好度 63/100
FreeRTOS/FreeRTOS-Plus-TCP#1395 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
qoretechnologies/qore#8222 ·
维护者通常 1 天内回复
-
c easy first-contributor first-contributors good first issue help wanted up-for-grabs
难度 1/5 1-3 小时 新手友好度 96/100
fork-commit-merge/fork-commit-merge#8696 ·
维护者通常 1 天内回复