Skip to content

control: keep the registry working under kernel.shm_rmid_forced=1 - #355

Merged
congwang-mk merged 1 commit into
multikernel:mainfrom
ricardobranco777:fix-353-shm-rmid-forced
Nov 26, 2026
Merged

congwang-mk merged 1 commit into
multikernel:mainfrom
ricardobranco777:fix-353-shm-rmid-forced

Conversation

@ricardobranco777

@ricardobranco777 ricardobranco777 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #353

Problem

With kernel.shm_rmid_forced=1, the kernel destroys a SysV shared-memory segment as soon as its last attachment drops, but semaphores persist. The control registry attached and detached its directory and segments for each operation, so they disappeared in the middle of a claim. The first sandlock run failed with EINVAL. After that, the signed semaphores pointed at a directory that no longer existed, so every later run failed with ENOENT until someone ran ipcrm. No sandbox could start on such a host.

Changes

control: keep the registry alive under kernel.shm_rmid_forced=1

  • Pinning: each live claim keeps the directory and its own segment attached. A transaction keeps every segment it opens attached until it ends.
  • Recreation: a missing directory means no claim is alive, so it is created again.
  • Salt stash: the salt that keys every segment is also stored in a small 9-semaphore set (key uid ^ 0x534c5305, mode 0600). A recreated directory then finds the existing semaphore arrays.
    Without this, every idle→busy cycle would orphan an 8196-semaphore array and eventually exhaust SEMMNI.
  • Rebuild: a segment destroyed along with its last claimant takes the chain links stored in its records. A chain walk that reaches a missing or recreated segment relinks every bucket from the surviving records, under the lock. Word 3 holds a REBUILDING sentinel while this runs, so an interrupted rebuild is redone by the next transaction. Live claims pin their own segment, so only released records are lost.
  • Race: a segment can be destroyed between shmget and shmat, by a detach outside the lock or at a claimant's exit. The open path now retries that case. It validates the segment only after attaching, when it can no longer disappear.
  • Mapping: a Mapping records the pid that attached it, so a forked child never calls shmdt on an address it didn't inherit.
  • Docs: docs/architecture.md is updated. It previously said registry errors fail sandbox creation, and it now describes the shm_rmid_forced behavior.

With the default shm_rmid_forced=0, the only differences are one extra 9-semaphore set per user and two extra attachments per sandbox. The rebuild path never runs.

Testing

All runs below used kernel.shm_rmid_forced=1.

  • New unit test chains_through_a_lost_segment_are_rebuilt covers three cases: a destroyed segment, a recreated empty segment, and an interrupted rebuild. It fails if the rebuild is disabled.
  • Control unit tests: 23/23 pass. Before this change, claims_are_exclusive_and_reuse_changes_the_instance and a_full_table_reports_exhaustion_and_reuses_released_slots failed. The shmget→shmat race caused about 2 failures in 25 runs before the retry was added, and none in 30 runs after.
  • Integration tests: test_control, test_cow and test_chroot pass 123/123. 16 test_control tests failed before.
  • Full sandlock-core suite: 984/984 unit tests and 607/608 integration tests pass. The one failure, test_user_mapping::test_uid_zero_echo, fails the same way without this change.
  • CLI:
    • Two concurrent sandboxes both appear in sandlock ps.
    • Starting a second sandbox with an existing name is refused.
    • 20 repeated runs succeed, with no shared memory left afterwards and a constant semaphore count, so nothing leaks.

Compatibility note

If a binary from before this change finds a rebuild that was interrupted midway, it reports the registry as invalid until a new binary completes a transaction. This needs a crash during a rebuild, which only happens under shm_rmid_forced=1, where binaries from before this change could not run anyway.

@congwang-mk

congwang-mk commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the PR!
Please drop the first commit (6be7386, best-effort registration). The second commit fixes the root cause of #353 on its own, and the first one turns lock timeouts, resource exhaustion and a corrupt registry into warnings, so a sandbox can run without its name claim and a duplicate name is no longer refused.

@congwang-mk

Copy link
Copy Markdown
Contributor

Thanks for dropping the first commit. One thing to fix before merge:

salt_semaphores uses key uid ^ 0x534c5305, while the lock set uses uid ^ 0x534c4305. They differ only in bit 12, so uid X's salt stash has the same SysV key as uid X ^ 4096's lock set (for example 1000 and 5096). Whichever user runs second fails in semget and cannot create any sandbox.

Please pick a constant whose XOR with the lock constant has a high bit set (bit 30, say), so no realistic uid pair collides.

With shm_rmid_forced=1 the kernel destroys a SysV segment as soon as its
last attachment drops, while semaphores live on. The registry attached and
detached its directory and segments per operation, so they vanished in the
middle of a claim (EINVAL), and the signed semaphores left behind made every
later run look for a directory that no longer existed (ENOENT).

- Each live claim keeps the directory and its own segment attached, and a
  transaction keeps every segment it opens attached until it ends.
- A missing directory means no claim is alive; it is created again.
- The salt that keys every segment is also kept in a small semaphore set,
  so a recreated directory finds the existing semaphore arrays instead of
  orphaning an 8196-semaphore array on every idle-to-busy cycle.
- A segment destroyed with its last claimant takes the chain links in its
  records with it. A walk that reaches one relinks every bucket from the
  surviving records under the lock; an interrupted relink is redone.
- A segment destroyed between shmget and shmat, by a detach outside the
  lock or at a claimant's exit, is looked up again. It is validated only
  once attached, when it can no longer disappear.

Fixes multikernel#353

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@congwang-mk
congwang-mk merged commit 23b0ed0 into multikernel:main Nov 26, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sandbox startup fails when kernel.shm_rmid_forced=1 (control registry SysV segment is destroyed on detach)

2 participants