Skip to content

feat(vfs): implement close_range syscall - #2200

Merged
fslongjin merged 5 commits into
DragonOS-Community:masterfrom
fslongjin:codex/close-range-syscall
Aug 21, 2026
Merged

feat(vfs): implement close_range syscall#2200
fslongjin merged 5 commits into
DragonOS-Community:masterfrom
fslongjin:codex/close-range-syscall

Conversation

@fslongjin

Copy link
Copy Markdown
Member

Summary

  • implement Linux-compatible close_range(2) support for close, CLOSE_RANGE_CLOEXEC, and CLOSE_RANGE_UNSHARE
  • add transactional fd-table cloning with bounded range scanning and lock-safe close finalization
  • add deterministic no-skip coverage for ABI truncation, shared table behavior, sparse descriptors, RLIMIT handling, fd reuse, and POSIX lock ownership

Motivation

DragonOS already exposed syscall number 436 on each architecture but had no VFS handler. Applications using close_range(2) therefore received ENOSYS, and the existing fd-table clone path did not provide the fallible transaction boundary required by CLOSE_RANGE_UNSHARE.

This implementation follows Linux 6.6 semantics without introducing a new VFS trait, global lock, bitmap subsystem, or userspace-style close loop.

Implementation

  • decode the syscall arguments using the Linux unsigned 32-bit ABI and validate flags before the range
  • clone shared fd tables in two phases so allocation failures cannot mutate the source table or trigger close side effects
  • preserve installed descriptors and FD_CLOEXEC, omit in-progress reservations, allocate a new POSIX lock owner, and recompute next_fd
  • scan close ranges with a bounded work budget while performing file flush/close and scheduler yields outside fd-table locks
  • mark reserved slots during CLOEXEC operations so installation preserves the flag

Validation

  • x86_64 kernel format, check, clippy, and build
  • RISC-V kernel check for the shared implementation
  • host Linux close-range dunitest: 6/6 passed
  • DragonOS guest close-range dunitest: 6/6 passed
  • DragonOS guest gVisor close-range tests: all 12 runnable cases passed

@github-actions github-actions Bot added enhancement New feature or request test Unitest/User space test labels Aug 18, 2026
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ceb912b9a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kernel/src/filesystem/vfs/file.rs Outdated
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d92b33dc0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kernel/src/filesystem/vfs/syscall/sys_close_range.rs
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d92b33dc0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kernel/src/filesystem/vfs/syscall/sys_close_range.rs Outdated
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6c261a6c53

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2677 to +2678
(Some(highest), Some((first, last)))
if highest >= first as usize && highest <= last as usize =>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 在克隆时跳过有限范围的中间穿孔

CLOSE_RANGE_UNSHARE 使用有限区间且 last 之上仍有打开的 fd 时,此条件退回到 highest,使 populate_clone() 仍复制 [first, last] 中的文件,随后又逐个执行 finish_close()。新的证据是该分支仅在最高 fd 落入区间时才应用穿孔,而 Linux 6.6 会将有限范围同样作为 dup_fd() 的 punch range,在保留高位 fd 的同时跳过区间内引用;当前实现因此会产生 Linux 不会触发的 FUSE FLUSH 等可观察回调和额外复制开销。应让填充阶段直接跳过整个 punch range,而不是只通过截断尾部 copy_len 实现穿孔。

AGENTS.md reference: AGENTS.md:L9-L9

Useful? React with 👍 / 👎.

if flags.contains(CloseRangeFlags::CLOEXEC) {
set_cloexec_in_table(&new_table, first, last);
} else {
close_range_in_table(&new_table, first, last);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 先发布新 fd 表再执行范围关闭

当调用方与其他任务通过 CLONE_FILES 共享 fd 表,且普通 CLOSE_RANGE_UNSHARE 在关闭回调或 sched_yield() 中让出 CPU 时,这里会在新表尚未安装到当前 PCB 的情况下扫描并关闭它;共享任务可在此期间修改仍挂在当前 PCB 上的旧表,而最终替换会让这些范围外更新突然从当前任务视图消失,procfs 观察者也会在操作期间看到错误的表身份。Linux 6.6 在复制后先切换 current->files,再对新表执行范围操作;这里也应先安装新表并释放 PCB 锁,再运行可能阻塞的关闭流程。

AGENTS.md reference: AGENTS.md:L9-L9

Useful? React with 👍 / 👎.

Add Linux-compatible close_range(2) handling for close, CLOEXEC, and UNSHARE operations with the expected u32 ABI and validation order.

Introduce a fallible two-phase fd-table clone so allocation failures cannot mutate the shared table or trigger close side effects. Scan close ranges with bounded work, perform file finalization outside fd-table locks, preserve reserved descriptors, and retain the correct POSIX lock owner semantics.

Add deterministic no-skip coverage for range validation, raw argument truncation, shared and private tables, sparse descriptors, lowered RLIMIT_NOFILE, next-fd reuse, and record-lock ownership.

Validated with x86_64 kernel format/check/clippy/build, a RISC-V kernel check, host Linux tests, and DragonOS guest tests.

Signed-off-by: longjin <longjin@dragonos.org>
Separate the cloned fd-table layout size from the installed File population bound. Tail punch-hole clones now stop copying at the last descriptor that must be retained instead of cloning up to the minimum 1024-slot layout and then closing those files.

This avoids redundant Arc clones, range scans, and observable flush_for_close callbacks while preserving the minimum table capacity, reserved-fd behavior, next_fd recomputation, and ordinary clone semantics.

Add a focused clone-plan regression test for a 64-fd retained prefix in a 1024-slot layout.

Validated with kernel format, check, clippy, make kernel, host close_range tests, and three independent adversarial reviews.

Signed-off-by: longjin <longjin@dragonos.org>
Separate files-table lifetime references from PCB attachment ownership so procfs, BPF, and in-flight syscall observers cannot be mistaken for CLONE_FILES users.

Introduce a lightweight FileDescriptorTable identity with an atomic task-user count and an RAII FdTableAttachment for PCB slots. Route private replacement, CLONE_FILES sharing, exec, close_range, fork cleanup, and exit through the attachment lifecycle while keeping final table destruction outside basic and fd-table locks.

Use coherent table-and-sharing snapshots for exec and close_range, preserve the fallible close_range clone transaction, and add a focused ownership regression test proving observer Arcs do not affect sharing decisions.

Signed-off-by: longjin <longjin@dragonos.org>
Track task attachments independently from transient Arc observers so close_range and exec only unshare genuinely shared descriptor tables. Keep table lifetime ownership separate from task-sharing identity and preserve POSIX lock ownership for private tables.

Reuse the fallible descriptor-table clone path across close_range, fork, and exec. Preserve the allocation-before-population transaction boundary, use conditional rescheduling during range scans, and keep old table teardown outside process and fdtable locks.

Harden exec after the point of no return by preparing fallible signal state early and terminating through the normal fatal-exec path when later image installation fails. Add close_range ownership and exec isolation regression coverage.

Signed-off-by: longjin <longjin@dragonos.org>
@fslongjin
fslongjin force-pushed the codex/close-range-syscall branch from 6c261a6 to 8db4f2d Compare August 20, 2026 03:16
@fslongjin

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 8db4f2d43d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Retire replaced sighand references outside task_lock through a fallible RCU callback admission path so shared-sighand exec no longer waits for a global grace period during normal operation.

Reserve both pending and ready callback capacity before publication to keep grace-period advancement allocation-free. Preserve the removed Arc on allocation failure and use a no-allocation yielding grace-period fallback instead of turning post-PONR memory pressure into a kernel panic.

Add RCU selftests for fallible deferred drop and no-allocation grace-period progress, plus deterministic successful and post-PONR shared-sighand exec isolation coverage.

Validated with kernel build, formatting, nightly clippy, DragonOS guest exec ABI tests, and multi-agent adversarial review.

Signed-off-by: longjin <longjin@dragonos.org>

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 5cfa83c835

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@fslongjin
fslongjin merged commit f64a0d5 into DragonOS-Community:master Aug 21, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request test Unitest/User space test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant