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

Raw Markdown for agents: https://autobot.eidonix.com/api/v1/cases/d24a3eb5-5ddf-4361-933f-33c09a993f1c

Opened by a workflow run, Dec 2, 2026 7:16 PM EST. decide malicious: all_clear (100%): No malicious content found

explore

Author: ebfio (first-time contributor, FIRST_TIME_CONTRIBUTOR, 0 merged PRs, 2 other open PRs #698/#682; Discord ebfio) Motivation: A local selene-manager preallocates its shared arena/staging files in /dev/shm tmpfs, so that RAM is already committed and already subtracted from Linux MemAvailable — yet the pool budget charges the same bytes again against host headroom, so attaching a valid pool is rejected. The PR measures how many of those bytes are already backed and charges only the remainder. Scale: +175 / −12 lines in 5 files (all under selene/src/pool/) Proposal:

Full report · Notes

Classify intake

Selected: triage:review, priority:P2, bug, performance, area:memory, area:selene

Threshold: 65%

Triage

Priority

Type

Area

Model

Apply Labels (Intake)

Applied: +triage:review, +priority:P2, +bug, +performance, +area:memory, +area:selene

Value Proposition

Value Proposition — PR #699 (tmpfs backed_bytes pool-budget exemption)

Charter alignment — on-goal, arguable layer. The change sits directly on a stated goal: "Store prefix-cache checkpoints in host memory and share them across Flare instances via Selene." It stays inside Selene, adds no model-specific branch, no CLI flag, no dependency, and doesn't touch the runtime's per-model paths. Per the dossier it also satisfies the four-part description rule. The tension is #404 ("right problem in the right place"): the reported error is internally consistent with two-stage charging, but the first stage — host_pool_budget reading MemAvailable (offload.rs:923-985) — is untouched; the exemption lands in pool/budget.rs instead. Whether that is the correct layer is unreviewed and undocumented.

Novelty & status — additive. main has no backed_bytes; nothing in-tree fixes this. #674 by the area owner is a different scope (hit rate, L1 retention, NVMe, snapshots) sharing files, not intent. Neither obsolete nor superseded.

Value vs cost. Beneficiary is narrow but real: operators of local selene-manager pools on tmpfs /dev/shm, which the dossier's people section shows this author actually runs (three consistent /dev/shm/unified-memory PRs). The mechanism diagnosis is verified in selene-manager/src/local.rs:582-602. The evidence is not: no CI on the head commit, backed_shm_bytes (conn.rs:678-704) executed by no test, no live manager reproduction, no before/after Shmem/MemAvailable capture, and no linked issue or user report. Cost side is non-trivial: a required pub backed_bytes on two publicly re-exported, non-#[non_exhaustive] structs is a source-breaking change the body doesn't declare; summary() and per-GPU charges now report numbers that disagree with the registered {:.1} GiB startup line; three docs still describe the unexempted budget; and zeroing host bytes silently removes arena+staging from per-GPU charges on integrated devices — pinned by the new test on exactly the author's unified-memory topology, with no stated intent.

Worth it with changes. Merge is justified only after: (a) backed_bytes gets Default/#[non_exhaustive] or the break is declared; (b) backed_shm_bytes and the register_memory→builder.rs mapping are covered, ideally with a captured Shmem/MemAvailable before-after; (c) the unified-memory per-GPU semantics are decided and documented, not implicitly pinned; (d) docs and the startup/summary figures reconciled; (e) reviewer confirmation that exempting the pool budget — not host_pool_budget — is the intended layer; (f) sequencing agreed with open #674 on conn.rs/builder.rs. Non-priority contributor, so spell the requirements out per #229 rather than closing.

Verdict: Worth it with changes.

Full report Decide Value: yes (100%): This change is worth reviewing: it provides value to the project in line with its charter.

Subtraction and simplicity

The diff is lean: no new flags, config keys, env vars, deps, protobuf fields, or dead code, and all three Budget::costs call sites were updated. Four local simplifications are worth making; none structural.

  1. (high) selene/src/pool/conn.rs:774-780 + :854 — shared_backed: Option<(u64,u64)> is Some exactly when shared is Some, so if let (Some(shared), Some((arena_backed, staging_backed))) has an unreachable arm and unwrap_or((0,0)) is never taken. Use a plain let mut backed = (0u64,0u64) set inside the existing if let Some(shared) block; ~5 lines shorter.
  2. (high) selene/src/pool/budget.rs:19 — bytes.saturating_sub(backed_bytes.min(bytes)): saturating_sub already floors at 0 and backed_shm_bytes already clamps at conn.rs:697. Drop .min(bytes); retarget the now-stale // clamped to region.bytes comment at budget/tests.rs:155.
  3. (medium) selene/src/pool/budget/tests.rs — the closure plan.iter().map(|region| (region.bytes, region.backed_bytes)) is written out at :25, :48-52, :65, :109-113, :163-167. One module-local fn costs(plan: &[PlannedRegion]) collapses all five. rustfmt also was not run (:25 is 106 columns) and there is no CI on 135633f.
  4. (medium) selene/src/pool/budget/tests.rs:133-141 — the host: Err("no memory observation") assertion is a verbatim repeat of the pre-existing :82-90 and cannot be affected by backed_bytes (budget.rs:49-52 returns before any comparison). Delete it; keep :124-132, which proves the GPU charge is still reached.

Deliberately cleared: moving backed_shm_bytes into flare-eos (Selene already calls libc for crate-internal work, shm.rs:144-150; a one-caller eos export would be speculative), the non-Linux stub at conn.rs:701-704 (Selene builds on Windows/macOS), and the extra registered_region parameter (load-bearing for builder.rs:239-245).

Open: whether the fix belongs in the pool budget or in host_pool_budget (offload.rs:923-985) — if the latter, all four findings become moot; whether the zeroed host charge on unified-memory hosts (budget.rs:30-42) is intended; and no maintainer reaction exists yet on #699.

Full report · Notes

Correctness and contracts

Correctness/contracts review of PR #699 (head 135633f). The Budget::costs arithmetic itself is sound — no underflow, the device term bytes.div_ceil(512) is preserved, host Err propagation and per-GPU rank errors are untouched, and the non-Linux stub keeps old conservative behaviour. The problems are elsewhere:

  1. Major — backed bytes are measured from the paths (conn.rs:774-779 via std::fs::metadata, conn.rs:686-698) before Regions::map (conn.rs:813-829) independently re-stats and rejects size < bytes (shm.rs:53-57). A manager that recreated larger regions can make the pre-map reading claim full backing, so approval passes and the operator now gets a byte-mismatch error instead of the actionable budget message.
  2. Major — a reclaimable, non-mlocked estimate becomes a hard admission fact: tmpfs pages can be dropped and re-faulted as real RAM that this change charges 0, for a region shared by every attaching instance.
  3. Major — PlannedRegion/RegisteredRegion are publicly re-exported (pool/mod.rs:60 (not opened)) and gain a required pub backed_bytes; no #[non_exhaustive], no Default — undeclared source break. Zeroing the host term also zeros the per-GPU apportionment on integrated devices (Budget::charges, budget.rs:30-42), which contradicts docs/runtime-core-design.md:768-771 and docs/cli-design.md:884-891 (AGENTS.md:171-186).
  4. Major — the statfs helper and the attachment path have zero coverage; the new tests fabricate PlannedRegion values only (budget/tests.rs:92-186).
  5. Minor — registered {:.1} GiB of pool memory (conn.rs:865-870) still sums full bytes while summary() (builder.rs:246-249) now subtracts backed bytes: two operator-facing numbers now disagree.

Open: the body's own step 1 (host_pool_budget, offload.rs:923-985) is untouched; no CI on the head commit; no test executes backed_shm_bytes.

Full report · Notes

Final review

Recommendation: Changes Requested

The core diagnosis and architectural placement are sound: on Linux, selene-manager preallocates /dev/shm/selene/<name>-<hash>/{arena,staging} via libc::fallocate (selene-manager/src/local.rs:583-602), which immediately deducts those pages from /proc/meminfo MemAvailable, causing Budget::approve (selene/src/pool/budget.rs:44-72) to double-count them against host RAM and integrated-GPU KV cache capacity. However, the PR cannot merge as-is because it breaks three existing Linux CI integration tests across selene-manager and helios 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.

Merge Risk

Level: Medium

Walkthrough

Pre-merge Checks

Blockers

  1. Breaks three existing Linux CI integration tests that attach to a real /dev/shm tmpfs manager
    • Where:
      • selene-manager/tests/builder.rs:79-115 (over_budget_startup_fails_before_registering_memory)
      • selene-manager/tests/offload.rs:13-71 (startup_fails_and_releases_registration_when_a_rank_rejects_the_pool)
      • helios/src/offload/pool/tests.rs:20-92 (peer_registration_budget_fails_startup)
    • Verified behavior & impact: All three tests run in .github/workflows/native-serving.yml:48-63 (not opened) on ubuntu-24.04 (where /dev/shm is tmpfs) and spawn selene_manager::testing::Manager::new(), which creates fallocated arena and staging files under /dev/shm/selene (selene-manager/src/local.rs:26,125,583-602). With this PR, backed_shm_bytes returns full arena_bytes/staging_bytes, making Budget::costs compute host == 0:
      1. In selene-manager/tests/builder.rs:83-112, the first subcase (Budget { host: Ok(0), devices: [(u64::MAX, false)] }) now succeeds (0 <= 0) instead of failing, panicking at .expect("an unapproved pool must fail startup") (builder.rs:101). The second subcase (devices: [(1, false)]) fails the assertion assert!(reason.contains("needs 16777216 host bytes")) (builder.rs:106) because Budget::approve now formats "needs 0 host bytes".
      2. In selene-manager/tests/offload.rs:34-64, the local_approved == false iteration passes Budget { host: Ok(0), devices: vec![(u64::MAX, false), (u64::MAX, true)] }. Because host == 0 <= 0, local registration is approved instead of rejected, panicking at assert_eq!(approved, local_approved) (offload.rs:44) and registering 2 regions instead of 0.
      3. In helios/src/offload/pool/tests.rs:46-90, rejection == 0 passes Budget { host: Ok(1), devices: [(u64::MAX, false)] } to a peer RankAgent, which also runs conn::register_memory_blocking (selene/src/pool/agent.rs:611). With host == 0 <= 1, the peer attaches instead of rejecting, panicking at result.err().expect("an unapproved peer must fail startup") (helios/src/offload/pool/tests.rs:88).
    • Fix: Update all three tests for the new preallocated-tmpfs contract (e.g., use a TCP/remote manager whose anonymous data-staging has backed_bytes == 0 when testing host-budget rejection, or assert device-budget rejection with "needs 0 host bytes" on local tmpfs while explicitly asserting that host: Ok(0) approves a preallocated local tmpfs pool). Run the full Linux test commands from .github/workflows/native-serving.yml:51-63 (not opened):
      cargo test --locked --release --manifest-path selene/Cargo.toml (not opened) --features test-support
      cargo test --locked --release --manifest-path selene-manager/Cargo.toml (not opened) --features test-support
      cargo test --locked --release --manifest-path helios/Cargo.toml (not opened) --features runtime-core,server
      

Should Fix

  1. Add direct test coverage for backed_shm_bytes and the register_memory → Budget integration path
    • Where: selene/src/pool/conn.rs:682-704, selene/src/pool/budget/tests.rs:93-187
    • Verified behavior & impact: The two new tests in selene/src/pool/budget/tests.rs hand-construct PlannedRegion values and only test Budget arithmetic; nothing directly tests backed_shm_bytes (tmpfs magic check, st_blocks * 512 calculation, clamp to declared_bytes, sparse/unallocated file, non-tmpfs fallback to 0, missing/NUL path fallback to 0) or asserts the resulting backed_bytes / charges through a real Manager.
    • Fix: Add Linux unit tests in selene/src/pool/conn.rs (under mod tests) covering backed_shm_bytes on a fallocated file in /dev/shm (guarded or checked for tmpfs), a sparse file (set_len without fallocate), a non-tmpfs path (returns 0), and missing/NUL paths (returns 0), alongside the updated live-manager integration tests in selene-manager/tests/builder.rs asserting that a preallocated local pool approves with host: Ok(0) and still rejects insufficient GPU budget.

Subtract / Simplify

  1. Remove redundant Option threading and unreachable default for shared_backed (selene/src/pool/conn.rs:774-780, 853-855)
    • shared_backed is shared.map(...), so Some iff shared is Some (and regions is Some iff shared is Some via lines 813-829). Computing let mut shared_backed = (0u64, 0u64); inside if let Some(shared) = shared { ... } removes the double-Some tuple match at line 780 and the dead .unwrap_or((0, 0)) at line 854.
  2. Drop redundant .min(bytes) inside saturating_sub (selene/src/pool/budget.rs:19)
    • bytes.saturating_sub(backed_bytes) is identical to bytes.saturating_sub(backed_bytes.min(bytes)) for all u64 values (and backed_shm_bytes already clamps at conn.rs:697). Update the comment at selene/src/pool/budget/tests.rs:155 accordingly.
  3. Deduplicate the (bytes, backed_bytes) mapping in selene/src/pool/budget/tests.rs:25, 48-52, 65, 109-113, 163-167
    • The identical plan.iter().map(|region| (region.bytes, region.backed_bytes)) closure is repeated five times (and lines 25 and 65 exceed 100 columns / rustfmt formatting). Extract a small helper fn costs(plan: &[PlannedRegion]) -> std::result::Result<(u64, u64), String> next to fn plan(...).

Nits

  1. Reconcile startup progress summary with registered-bytes log (selene/src/pool/builder.rs:246-249, selene/src/pool/conn.rs:865-870): conn.rs:866 prints registered 40.0 GiB of pool memory while PreparedPool::summary() (printed by helios/src/offload/pool.rs:20-22) will now print 0 host bytes · 83886080 device bytes per GPU for a preallocated local pool. Mentioning backed/additional bytes in summary() (e.g., 0 additional host bytes) avoids operator confusion.
  2. Single-fd fstatfs + metadata and swap note (selene/src/pool/conn.rs:682-699): Opening path once and calling libc::fstatfs(file.as_raw_fd(), &mut statfs) + file.metadata() avoids two separate path lookups (statfs(path) then metadata(path)). Worth noting in the doc comment that on swap-enabled hosts, st_blocks still includes swapped-out tmpfs pages that cuMemHostRegister_v2 (helios/src/cuda.rs:471) will fault back into RAM.
  3. Note the PlannedRegion/RegisteredRegion struct field addition in the PR body (selene/src/pool/startup.rs:21-46, re-exported at selene/src/pool/mod.rs:60): Acceptable for an unpublished workspace crate with all in-tree call sites updated, just worth mentioning clearly.

Dropped on verification

Full report

Classify triage

Selected: triage:review

Threshold: 65%

Triage

Apply labels

No label changes Decision to Test: no (100%): Testing will not add significant value as the code stands today. The code is either not yet ready to merge, or adequate testing has already been done to validate the contents of the code, or the available hardware is not suitable to test with.