Skip to content

feat(ipc/sem): add semaphore support for dragonOS - #2172

Open
mistcoversmyeyes wants to merge 7 commits into
DragonOS-Community:masterfrom
mistcoversmyeyes:feat/ipc-sem-2142
Open

feat(ipc/sem): add semaphore support for dragonOS#2172
mistcoversmyeyes wants to merge 7 commits into
DragonOS-Community:masterfrom
mistcoversmyeyes:feat/ipc-sem-2142

Conversation

@mistcoversmyeyes

@mistcoversmyeyes mistcoversmyeyes commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Related

Summary

  • Implement the x86_64 System V semaphore syscalls: semget, semctl, semop, and semtimedop.
  • Add semaphore-set management to IPC namespaces.
  • Extract and reuse common System V IPC permission checks.

Scope

  • Match the Linux 6.6 x86_64 ABI and observable behavior.
  • SEM_UNDO is out of scope and currently returns ENOSYS.

Acceptance

  • Valid semaphore syscall requests no longer return ENOSYS.
  • semop and semtimedop share consistent operation semantics.
  • Creation, lookup, control, removal, and permission checks match Linux behavior.
  • Multi-operation requests execute atomically.
  • Blocking operations wake correctly after value changes or IPC_RMID.
  • Nonblocking, timeout, signal, invalid-argument, and removed-set errors match Linux behavior.
  • Concurrent access avoids races, lost wake-ups, use-after-free, and resource leaks.
  • Existing DragonOS CI tests pass.

Testing

  • Added 43 System V semaphore dunitests.
  • QEMU guest test: 43/43 passed.
  • Format, Clippy, multi-architecture builds, Dunitest, and Integration Test CI passed.

@github-actions github-actions Bot added the enhancement New feature or request label Aug 8, 2026
Comment thread kernel/src/ipc/syscall/sys_semop.rs Outdated
@mistcoversmyeyes
mistcoversmyeyes force-pushed the feat/ipc-sem-2142 branch 2 times, most recently from e0bc761 to a266b20 Compare August 17, 2026 09:07
@github-actions github-actions Bot added the test Unitest/User space test label Aug 19, 2026
@mistcoversmyeyes
mistcoversmyeyes marked this pull request as ready for review August 19, 2026 07:43
@fslongjin

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 29, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-08-29T10:00:44.439894Z fbdee92 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: fbdee927ce

ℹ️ 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/ipc/sem.rs
Comment on lines +873 to +875
let set = self
.get_by_semid_checked_mut(token.id)
.map_err(|_| SystemError::EIDRM)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 在 SETALL 提交前重新检查写权限

prepare_setall() 检查权限后复制用户数组时,集合所有者可并发执行 IPC_SET 撤销调用者的写权限;这里重新加锁后只验证 ID 和长度,仍会提交全部新值。应在持锁修改 semval 前按当前权限再次执行写权限检查,避免权限撤销后的 TOCTOU 写入。

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

Useful? React with 👍 / 👎.

Comment thread kernel/src/ipc/ipc_perm.rs Outdated
Comment thread kernel/src/ipc/ipc_perm.rs Outdated
Comment thread kernel/src/ipc/sem.rs
Comment thread kernel/src/ipc/sem.rs

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

Request changes: this PR establishes a useful base for System V semaphore support, but it does not yet satisfy the Linux 6.6 compatibility and concurrency-safety contract stated in #2142.

The blocking issues are:

  • IPC_SET permission updates can partially commit on an error, changing the owner even though the syscall returns EINVAL; the shared helper also affects SHM.
  • SEM_UNDO is rejected with ENOSYS and the new test codifies that incompatibility, while Linux maintains per-process/shared undo state and replays it at process exit.
  • A single namespace-wide spinlock protects the registry and every semaphore set, so unrelated sets are serialized; the lock also covers allocation-heavy queue simulation and scheduler wakeups.
  • User-controlled semaphore-set allocation is infallible and can reach the kernel panic allocation handler instead of returning ENOMEM.
  • SEM_STAT and SEM_STAT_ANY mask their direct table index, causing out-of-range indices to alias valid objects.

The basic syscall wiring, atomic multi-operation simulation, timeout/removal paths, and test breadth are valuable. However, the issues above are architectural or user-visible Linux semantic mismatches rather than optional refinements. Please address them, add the corresponding regression tests, and rerun the guest suite. The current Integration Test check also reports 5666 passed, 1 failed, and 180 skipped; I am not attributing that failure to this PR without further evidence, but the PR description should not claim that Integration Test passed while the check remains red.

Comment thread kernel/src/ipc/ipc_perm.rs Outdated
Comment thread kernel/src/ipc/sem.rs
.iter()
.any(|op| (op.sem_flg as u32) & SemFlags::SEM_UNDO.bits() != 0)
{
return Err(SystemError::ENOSYS);

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.

[P1] Rejecting every SEM_UNDO operation with ENOSYS is not Linux-compatible System V semaphore behavior. Linux 6.6 maintains sem_undo/semadj state, shares the undo list for CLONE_SYSVSEM, clears adjustments on SETVAL/SETALL/IPC_RMID, and replays them from exit_sem() when a task exits. This is essential crash-recovery behavior: without it, a lock holder exiting can leave peers blocked indefinitely. Please implement the full lifecycle before treating #2142 as complete; the new test should verify Linux behavior instead of expecting ENOSYS.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed. Full SEM_UNDO lifecycle support is required for Linux 6.6 compatibility, including shared undo state for CLONE_SYSVSEM, semadj updates and limits, cleanup on SETVAL/SETALL/IPC_RMID, and replay on task exit. I am implementing this now and will replace the current ENOSYS test with lifecycle coverage. Keeping this thread open until the implementation and tests are complete.

Comment thread kernel/src/ipc/sem.rs Outdated
Comment thread kernel/src/ipc/sem.rs
/// SysV SHM manager (phase one: per-namespace SHM only)
pub shm: SpinLock<ShmManager>,
/// SysV semaphore manager
pub sem: SpinLock<SemManager>,

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.

[P1] A namespace-wide spinlock is too broad for semaphore-set state. Every operation on every set, including update_queue(), is serialized here; queue simulation allocates a HashMap, may rescan waiters quadratically, and calls Waker::wake() while this lock is held. A user can therefore stall unrelated semaphore sets in the same namespace. Please keep the manager lock limited to ID/key/quota lookup, store stable Arc<KernelSemSet> objects with per-set locking, use a non-allocating operation fast path, and collect wakeups for execution after releasing the set lock, following Linux's registry/array locking and wake_q separation.

@mistcoversmyeyes

Copy link
Copy Markdown
Contributor Author

While reviewing the queue-rescan concern, I found a separate Linux 6.6 compatibility issue in the current unified waiter queue.

Linux classifies queued requests into const operations (the entire operation group has no altering operation) and alter operations when they are enqueued: ipc/sem.c#L2127-L2153. Its update path handles wait-for-zero requests before scanning pending alter requests: ipc/sem.c#L1029-L1039. After a queued alter operation succeeds, Linux immediately checks wait-for-zero requests made satisfiable by that change before continuing with further alter operations: ipc/sem.c#L974-L992.

The current DragonOS implementation stores both classes in one VecDeque, so an older alter waiter can run before an already-satisfied const waiter and make its zero condition false again. This can produce behavior different from Linux.

Proposed minimal design for this PR:

  • Split the set-level queue into pending_const and pending_alter.
  • Classify an operation group once: all sem_op == 0 goes to pending_const; any nonzero operation goes to pending_alter.
  • Process ready const waiters before scanning alter waiters.
  • After any alter waiter changes semaphore values, return to the const phase before continuing alter processing.
  • Keep the existing full atomic simulation and weak-FIFO alter scan; defer per-semaphore/simple-complex indexing to a separate performance change.

This fixes the observable ordering difference without attempting to eliminate the irreducible worst-case rescan for complex multi-operation groups.

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.

feat(ipc): Implement System V semaphore syscalls on x86_64

2 participants