Skip to content

Avoid double-counting preallocated /dev/shm selene-manager regions against Linux MemAvailable - #699

Open
ebfio wants to merge 2 commits into
NVIDIA:mainfrom
ebfio:fix/selene-avoid-shm-budget-double-count
Open

ebfio wants to merge 2 commits into
NVIDIA:mainfrom
ebfio:fix/selene-avoid-shm-budget-double-count

Conversation

@ebfio

@ebfio ebfio commented Dec 2, 2026

Copy link
Copy Markdown

Summary

On Linux, selene-manager preallocates its shared arena and staging files in /dev/shm/selene/<name>-<hash>/ using libc::fallocate(fd, 0, 0, size) (selene-manager/src/local.rs). On tmpfs, fallocate immediately commits physical RAM blocks (st_blocks * 512 == size), charging them to Shmem and deducting the full size from Linux /proc/meminfo MemAvailable.

When a local Flare instance subsequently attaches to the pool:

  1. host_pool_budget() reads Linux MemAvailable (which has already had arena + staging subtracted) and subtracts the system reserve (2 GiB + 5% MemTotal) plus the instance's local --prefix-cache-memory and intake.
  2. register_memory() populates PlannedRegion with the full arena_bytes and staging_bytes, and Budget::approve() requires arena + staging <= available_host a second time.
  3. As a result, attaching a local selene-manager pool effectively requires 2× (arena + staging) of host RAM on single-instance hosts (and (N + 1)× when multiple instances map the same shared files), rejecting valid configurations that have tens of GiB of real host headroom remaining.

Observed on a 494.5 GiB unified-memory host with 230.2 GiB host weights, 128 GiB local prefix cache, and a 32G arena + 8G staging local pool (~69 GiB real free host RAM after weights + pool):

Error: cannot attach the configured prefix cache pool: the pool's host memory
(arena 34359738368 bytes, staging 8589934592 bytes) was not approved
(needs 42949672960 host bytes and 83886080 device bytes per GPU;
host budget has 27910736778 bytes left)

Changes

  • selene/src/pool/startup.rs:
    • Add pub backed_bytes: u64 to PlannedRegion and RegisteredRegion to distinguish bytes already committed in memory-resident shared storage from new physical host RAM required at registration.
  • selene/src/pool/conn.rs:
    • Add Linux-only backed_shm_bytes(path, declared_bytes): verifies via libc::statfs that the file resides on libc::TMPFS_MAGIC and returns metadata.blocks().saturating_mul(512).min(declared_bytes). Fails conservatively to 0 on non-Linux hosts, non-tmpfs filesystems, or any I/O error.
    • Populate backed_bytes for local arena and staging PlannedRegion / RegisteredRegion entries, and 0 for anonymous remote data-staging.
  • selene/src/pool/budget.rs & selene/src/pool/builder.rs:
    • Update Budget::costs and PreparedPool::registration_costs to charge host memory only for bytes.saturating_sub(backed_bytes.min(bytes)) while still charging full bytes.div_ceil(512) per GPU for CUDA host-memory registration metadata (and still propagating Budget.host = Err(...) errors).
  • selene/src/pool/budget/tests.rs:
    • Add unit tests covering fully preallocated /dev/shm (0 additional host charge, full device charge preserved, GPU shortfall & missing host observation still rejected) and partially backed / mixed remote data-staging plans.

Verification

  • Verified selene-manager's fallocate behavior against live /proc/meminfo (Shmem immediately equals arena + staging, fully deducted from MemAvailable before flare run attaches).
  • Suggested test command:
    cargo test -p flare-selene pool::budget

Submission validation

On Hades Linux aarch64 (kernel 6.17.0-1029-nvidia-64k):

cargo test -p flare-selene pool::budget
4 passed; 0 failed; 551 filtered out

The Linux backed_shm_bytes branch compiled. The budget tests exercise Budget arithmetic through PlannedRegion values; they do not directly execute the statfs helper or a fresh manager attachment reproduction.

Discord: ebfio

Linux MemAvailable already excludes fallocated tmpfs arena and staging
bytes, but pool approval subtracted the same bytes again. Track physically
backed bytes on planned regions and charge the host only the unbacked
remainder while preserving full per-GPU registration metadata cost.

This prevents valid shared-pool attachments from being rejected without
weakening GPU budget accounting or host observation failures.

Validation: cargo test -p flare-selene pool::budget on Hades Linux aarch64:
4 passed, 0 failed, 551 filtered out. The Linux backed_shm_bytes branch
compiled but its statfs helper was not directly executed by these tests; no
fresh manager attachment reproduction was run for this submission.

Signed-off-by: Eduardo Fonseca <ebf@ebf.io>
Design-Signed-Off-By: Iris; design-review=2e5b
Signed-off-by: Iris (Eduardo's coding agent)
@flare-autobot

Copy link
Copy Markdown
Collaborator

A case file has been opened for 699.
Case File Intake Report

Future reviews will auto-run 90 minutes after code updates are pushed.

  • Re-review sooner: Call @flare-autobot after push.
  • Opt-out: Include #autobot:ignore in the PR or any comment.

@flare-autobot flare-autobot added area:memory Memory budgets, allocation lifetimes, scratch sharing, and capacity. area:selene Selene shared prefix-cache pool, protocol, manager, and transfers. bug Something isn't working performance Performance, latency, throughput, or memory-efficiency investigation or improvement. priority:P2 Normal bug priority: workflow failure or diagnosis with narrower impact or an available workaround. triage:review Proposed change needs code review; this does not imply merge readiness. labels Dec 3, 2026
@flare-autobot

Copy link
Copy Markdown
Collaborator

Autobot Review

Full Review · Value Proposition · Case File

Recommendation: Changes Requested

The core diagnosis and architectural placement are sound, but the PR breaks three existing Linux CI integration tests that run against real /dev/shm tmpfs managers, and the new backed_shm_bytes syscall path has no direct test coverage or CI run on head 135633f.

  • Blockers: 1 · Should fix: 1 · Subtract/Simplify: 3 · Nits: 3
Walkthrough
  • selene/src/pool/startup.rs:21-46: Adds pub backed_bytes: u64 to RegisteredRegion and PlannedRegion to separate already-backed shared-memory bytes from newly required host RAM.
  • selene/src/pool/conn.rs:678-704: Adds Linux-only backed_shm_bytes(path, declared_bytes), verifying libc::TMPFS_MAGIC via libc::statfs and returning metadata.blocks().saturating_mul(512).min(declared_bytes) (failing closed to 0 on errors, non-tmpfs, or non-Linux targets).
  • selene/src/pool/conn.rs:774-864: Populates backed_bytes from backed_shm_bytes for local arena and staging planned/registered regions, and 0 for anonymous remote data-staging.
  • selene/src/pool/budget.rs:15-48 & selene/src/pool/builder.rs:239-245: Updates Budget::costs and PreparedPool::registration_costs to accept (bytes, backed_bytes), charging host memory only for bytes.saturating_sub(backed_bytes.min(bytes)) while keeping full bytes.div_ceil(512) per-GPU registration metadata charges and self.host error propagation.
  • selene/src/pool/budget/tests.rs:93-187: Updates existing PlannedRegion fixtures with backed_bytes: 0 and adds two unit tests for fully and partially backed regions.
Merge Risk: Medium · up to 135633f
  • Blast radius: Changes startup admission accounting (Budget::costs, register_memory, PreparedPool::registration_costs) for both PoolClient (selene/src/pool/conn.rs:643) and peer RankAgent (selene/src/pool/agent.rs:611) whenever attaching to a local Linux selene-manager on tmpfs /dev/shm. Steady-state serving hot paths, wire protocol, and non-Linux builds are unaffected (runs once at startup inside tokio::task::spawn_blocking).
  • Immediate CI breakage: No CI checks have run on 135633f, and the author only ran cargo test -p flare-selene pool::budget; three existing integration tests in .github/workflows/native-serving.yml (selene-manager and helios suites on ubuntu-24.04) will fail immediately because they pass host: Ok(0) or host: Ok(1) against a live /dev/shm Manager.
  • Swap caveat: On Linux hosts with swap enabled, st_blocks * 512 still counts swapped-out tmpfs pages that are no longer deducted from MemAvailable, whereas cuMemHostRegister_v2 (helios/src/cuda.rs:471) pins and faults them back into RAM.
Pre-merge checks: ✅ 2 passed · ⚠️ 2 warnings
  • ✅ Security / malicious screen: No suspicious code, no new dependencies, no prompt-injection or credential/network anomalies; backed_shm_bytes fails closed to 0 on invalid paths or syscall errors.
  • ✅ Charter & value fit: Directly serves the charter goal of sharing prefix-cache checkpoints via Selene without adding flags, dependencies, or model-specific logic, and lands at the right architectural layer (PlannedRegion/Budget::costs after Hello reveals local vs. remote regions).
  • ⚠️ Related / duplicate / conflict check: Clean against main (no duplicate fix), but large open PR selene,selene-manager,helios: improve prefix-cache hit rate, L1 retention, NVMe throughput, and GPU snapshot packing #674 by the area owner (yatesdr) also touches selene/src/pool/conn.rs and selene/src/pool/builder.rs; coordinate merge ordering.
  • ⚠️ Tests & verification evidence: No CI run on 135633f; author validation ran only cargo test -p flare-selene pool::budget, missing three broken integration tests in selene-manager/helios and leaving backed_shm_bytes unexecuted by any test.

Have your agent review the Full Review for complete verified findings, file:line citations, and suggested changes.

To opt out, include #autobot:ignore in your post or comment.

Derive manager integration expectations from actual arena and staging backing,
approve zero-host-budget pools when fully backed, and preserve device-budget
rejection coverage. Add a direct tmpfs test using the host page size so partial
backing and declared-size clamping are measured on 4 KiB and 64 KiB systems.

Validation on Hades Linux aarch64 with 64 KiB pages:
- full selene suite: 556 passed, 0 failed
- manager builder: 3 passed; manager offload: 2 passed
- Helios peer registration budget: 1 passed
- selene pool::budget: 4 passed
The manager host-rejection branch is unexercised on Hades because fallocate
fully backs both regions. No GPU admission or RDMA test was run.

Signed-off-by: Eduardo Fonseca <ebf@ebf.io>
Design-Signed-Off-By: Iris; design-review=2e5b
Signed-off-by: Iris (Eduardo's coding agent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:memory Memory budgets, allocation lifetimes, scratch sharing, and capacity. area:selene Selene shared prefix-cache pool, protocol, manager, and transfers. bug Something isn't working performance Performance, latency, throughput, or memory-efficiency investigation or improvement. priority:P2 Normal bug priority: workflow failure or diagnosis with narrower impact or an available workaround. triage:review Proposed change needs code review; this does not imply merge readiness.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants