Skip to content

fix(tty): register fasync after dropping the irqsave flags lock - #2722

Merged
fslongjin merged 3 commits into
DragonOS-Community:masterfrom
AsakuraMizu:fix/tty-fasync-add-mutex
Nov 27, 2026
Merged

fslongjin merged 3 commits into
DragonOS-Community:masterfrom
AsakuraMizu:fix/tty-fasync-add-mutex

Conversation

@AsakuraMizu

Copy link
Copy Markdown
Contributor

Fixes #2721

背景

tty 的 fcntl(F_SETFL, O_ASYNC) 在 TtyCoreData::add_fasync() 里持有 flags.read_irqsave() 时调 fasync_items.add(),后者取 Mutex<Vec<FAsyncItem>>。同一 tty 的另一次 add/remove 正持有这把互斥锁时,schedule() 里的 preempt_count() == 0 断言失败,内核 panic。

根因

flags 读锁用来和 tty_vhangup() 设置 HUPPING、递增 hangup_generation 的写锁配对,保证挂断之后不会再登记 fasync;但这个守卫一直活到 fasync_items.add() 之后,把睡眠锁包进了关中断的临界区。

修复方案

在读锁内检查挂断状态后放锁,再 add();之后重新读一次 hangup_generation,若已变化则撤回这次登记并返回 ENOTTY。tty_vhangup() 先递增 generation 再 clear() 登记表,两者都经过同一把互斥锁,因此复查仍是旧值时,挂断的 clear() 一定在这次 add() 之后,会把它清掉。

验证

新增 dunitest 测例 normal/tty_fasync_concurrency(加入 whitelist.txt):同一个 pty 从端打开 8 次,8 个线程各自对一个 fd 反复切换 O_ASYNC,检查每次 fcntl 都成功。

  • 修复前:运行测例后内核 panic(src/sched/mod.rs:1219,left: 1, right: 0)。
  • 修复后:测例通过,现有 normal/tty_pty_hangup、normal/fcntl_signal、normal/file_owner_notifications 也通过。

@github-actions github-actions Bot added the Bug fix A bug is fixed in this pull request label Nov 25, 2026
Signed-off-by: longjin <longjin@dragonos.org>
Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin
fslongjin merged commit 83b45ab into DragonOS-Community:master Nov 27, 2026
20 of 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] tty 的 fcntl(F_SETFL, O_ASYNC) 持关中断读锁取 FAsyncItems 互斥锁,并发时内核 panic

2 participants