Skip to content

fix(ipc): make the SysV SHM manager lock a mutex - #2720

Merged
fslongjin merged 3 commits into
DragonOS-Community:masterfrom
AsakuraMizu:fix/shm-lock-tmpfs-inode
Nov 27, 2026
Merged

fslongjin merged 3 commits into
DragonOS-Community:masterfrom
AsakuraMizu:fix/shm-lock-tmpfs-inode

Conversation

@AsakuraMizu

Copy link
Copy Markdown
Contributor

Fixes #2719

背景

shmctl 在持有 IPC 命名空间的 shm(SpinLock<ShmManager>)时会取睡眠锁:SHM_LOCK/SHM_UNLOCK 经 set_locked() 取 tmpfs inode 的 Mutex,SHM_INFO 经 resident_pages() 取 inode 和 page cache 的 Mutex,SHM_UNLOCK 释放 memlock 计费时取 SYSV_SHM_MEMLOCK_ACCOUNT。这些锁被别的任务持有时,schedule() 里的 preempt_count() == 0 断言失败,内核 panic。

根因

ipcns.shm 是自旋锁,而 ShmManager 在锁内调用的 backing 和 memlock 计费代码会睡眠。

修复方案

把 ipcns.shm 改为 Mutex。这把锁只在 shmget、shmat、shmctl 和 VMA 的 open/close 里取,这些地方都可以睡眠;close_vma() 销毁段时本来就会取睡眠锁。Linux 中对应的 shm_ids(ns).rwsem 也是睡眠锁。

验证

新增 dunitest 测例 normal/sysv_shm_lock_fault(加入 whitelist.txt):三个线程反复写入一个 64 KiB 段的映射并 MADV_DONTNEED,主线程分别反复做 SHM_LOCK/SHM_UNLOCK 和 SHM_INFO,检查返回值。

  • 修复前:两个测例都会让内核 panic(src/sched/mod.rs:1219,left: 1, right: 0)。
  • 修复后:测例通过。memlock 计费那一处另用多个线程对各自的段反复 SHM_LOCK/SHM_UNLOCK 手动验证过,修复前 panic,修复后正常。现有 normal/sysv_shm_semantics、normal/internal_shmem 通过。

@github-actions github-actions Bot added the Bug fix A bug is fixed in this pull request label Nov 25, 2026
@AsakuraMizu
AsakuraMizu force-pushed the fix/shm-lock-tmpfs-inode branch from 50cc955 to 0fd0482 Compare November 25, 2026 13:53
Signed-off-by: longjin <longjin@dragonos.org>
Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin
fslongjin merged commit dd33b58 into DragonOS-Community:master Nov 27, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG REPORT] shmctl 在 SysV SHM 管理器自旋锁内取睡眠锁,内核 panic

2 participants