Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
status: open
kind: defect
opened: 2026-11-05
---

# A large spawn on the AMD bench is a seventh slower than main's

`osbench`'s `proc.spawn_wait.large` spawns a 32 MiB program from a served
file. The spawner reads the image through `SYS_PAGED_READ` into fresh shared
memory. #1150 copies it a frame at a time through the reader's own mapping
(`arch::uaccess`), after faulting each chunk of 16 frames in
(`PagerObject::read`, `UserBytesMut::fault_in`). Main copied it through the
kernel's direct map, reaching and pinning each page as it went.

On the AMD bench (metal round 8; evidence `ToyOSOrg/evidence`, branch
`pr-1150`, r15):
- **The row:** 12.79 and 12.75 ms against main's 11.00 and 11.53, worse in
both pairs.
- **The copy:** 760 ns per page against 685 for main's whole reach, pin and
copy; with 8192 pages a spawn, that is 0.6 ms.
- **The populate:** `fault_in` adds 48 ns per page in a pass of its own,
0.4 ms a spawn.
- **Unaccounted:** about 0.5 ms of the 1.5 ms.

Before #1150's accessed-and-dirty change to x86-64 user leaves the copy cost
1871 ns per page and the row was 96% worse. On the T14 the row is now 4%
better than main's.

**Hypothesis, unmeasured:** the remaining copy cost is translating each
destination page through the 4 KiB user mapping, where main's direct map uses
huge pages. The separate populate pass touches each page's leaf a second time.
No performance counter has been read.

**Exit:** the row within main's run-to-run range on the AMD bench, or the
per-page difference measured by counter (TLB misses and walk cycles, against
main) and recorded here as the cost of copying through the user mapping that
#1150's design accepts.
78 changes: 28 additions & 50 deletions issues/a-pipes-lock-spans-a-user-copy-and-its-first-allocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,33 @@ kind: defect
opened: 2026-08-28
---

# A pipe's lock spans a 2 MiB user copy and the frames it takes for it

Each pipe owns its state behind its own `sync::Lock` (`kernel/src/pipe.rs`),
which disables preemption before it spins (`kernel/src/sync.rs`). The bulk copy
runs inside that lock, through the frame store pipes and connections share
(`kernel/src/frame_bytes.rs`):

* `FrameBytes::read` makes one walk of the caller's window,
`buf.write_pieces`, and copies each pinned piece out of the frames it
covers with `bytes.peek`, under it;
* `FrameBytes::write` makes one walk, `buf.read_pieces`, and copies each piece
into its frames with `bytes.place`, in the same acquisition.

Each 16 pages of either copy take the caller's space lock and populate any
untouched page, a frame from the allocator and a 4 KiB zeroing.

A pipe's copy is bounded only by the pipe: `PIPE_CAPACITY` is 2 MiB, the
largest single copy under the lock, and nothing above caps it
(`SYS_READ`/`SYS_WRITE` pass the caller's length through).

A connection direction (`kernel/src/message.rs`) copies one whole message under
its queue's lock, so its copy is bounded at `MAX_MESSAGE_LEN`, 16 KiB, and its
frames are taken the same way. Both its send and its receive run that copy
inside `process::with_process_data`, nesting the queue's lock in the process
lock: the receive installs the message's handles in the caller's table and
copies its bytes in one hold, so a refusal takes nothing.

A write that grows the pipe also takes its frames under the same lock,
nesting the frame allocator's lock: up to 513 frames, unzeroed, under one
hold, and the heap the list of them grows into. A read that empties frames
past the spares kept gives them back under the same nesting.

What queues behind it is that one pipe's traffic: its other end's reads and
writes, `has_data`/`has_space` from its blocking wrappers and polls, and its
handles' clones and closes. The scheduler's own bar for an uninterruptible
stretch is `MAX_PASS_NS = 200_000` ns (`kernel/pure/sched/cpu.rs`); a 2 MiB copy
is past it at any plausible bandwidth. The hold time is not measured, and that
is owed first. The length is the caller's.

## Fix direction

The lock is also the serialization `kernel::pipe::Bytes` relies on, so the
copy cannot simply be lifted out: a claimed-range token per end, or a copy
bounded per acquisition, has to carry the data-race argument. The frames can
be taken before the lock and given back after it, as the read end's last
holder already gives back what the pipe held.

**Exit**: no pipe or queue lock is held across more than a bounded copy or any
allocation, the connection's nesting in the process lock included, the hold
time is read before and after, and `audio_tone`'s worst wake does not move.
# A pipe's or queue's lock hold is unmeasured, and a queue still grows under it

A pipe (`kernel/src/pipe.rs`) and a connection direction
(`kernel/src/message.rs`) each keep their state behind a `sync::Lock`, which
disables preemption before it spins (`kernel/src/sync.rs`). No user copy and no
frame allocation runs under either any more: a read or a write claims its bytes
under the lock, copies them and takes or gives back frames with it let go, and
ends its claim under it (`kernel/src/frame_bytes.rs`, `kernel::pipe::Bytes`).
A hold is a claim's arithmetic, one chunk's runs (at most 17), a list moved
into a longer one (at most 513 words), or a batch of 32 spare frames listed.

What is left:

* **The hold time has not been read**, before or after. Before, a pipe's lock
spanned up to a 2 MiB copy and 513 frames from the allocator; the scheduler's
bar for an uninterruptible stretch is `MAX_PASS_NS = 200_000` ns
(`kernel/pure/sched/cpu.rs`). The bound above is by inspection. QEMU's clock
cannot read it; a bench can.
* **A connection's queue grows its two lists under the lock.**
`kernel::message::Queue` keeps its heads and its handles in `VecDeque`s that
start empty and double as `push` fills them, a heap allocation under the
queue's lock, up to `MAX_QUEUED_MESSAGES` and `MAX_QUEUED_HANDLES` entries.
Their bound is charged when the connection is made, so reserving it then is
the direct fix, at that memory per direction whether or not it is used.
* **`audio_tone`'s worst wake** has not been read against the change.

**Exit**: the hold time read on a bench before and after, the queue's lists
grown outside its lock or reserved, and `audio_tone`'s worst wake unmoved.

This is stage S0-a of `issues/anonymous-memory-swaps-to-an-encrypted-partition.md`.
29 changes: 0 additions & 29 deletions issues/a-user-copy-demand-pages-under-whatever-its-caller-holds.md

This file was deleted.

2 changes: 1 addition & 1 deletion issues/anonymous-memory-swaps-to-an-encrypted-partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ Each check goes red under the mutation named after "red".

| Stage | After | Builds | Exit |
|---|---|---|---|
| S0-a (`issues/process-memory-in-4-kib-pages-still-copies-each-programs-text.md`'s owner) | — | Every user copy faults or swaps in its chunk before any kernel lock, and pins after a re-check, with no pin across a wait. A lost anonymous page ends the caller, and a lost file-mapping page answers `EIO`. The futex re-translation never waits | Closes `issues/a-user-copy-demand-pages-under-whatever-its-caller-holds.md` and `issues/a-pipes-lock-spans-a-user-copy-and-its-first-allocation.md`. **H:** a chunk model in which an anonymous loss ends the caller with no partial result and a file-mapping loss answers `EIO`, red if either takes the other's outcome. **Q:** the pipe and futex sites, red when a copy runs under the pipe lock |
| S0-a (`issues/process-memory-in-4-kib-pages-still-copies-each-programs-text.md`'s owner) | — | Every user copy faults or swaps in its chunk before any kernel lock, and pins after a re-check, with no pin across a wait. A lost anonymous page ends the caller, and a lost file-mapping page answers `EIO`. The futex re-translation never waits | Closes `issues/a-pipes-lock-spans-a-user-copy-and-its-first-allocation.md`. One half is built: no user copy runs under a spin lock, and one that would is refused by name (`user_ptr`). By the orchestrator's ruling of 2026-11-03, the rest of this row is built as part of the process-memory track's 7b; the kernel has had no sleeping lock for the gate to extend to since #1152. **H:** a chunk model in which an anonymous loss ends the caller with no partial result and a file-mapping loss answers `EIO`, red if either takes the other's outcome. **Q:** the pipe and futex sites, red when a copy runs under the pipe lock |
| S0-b (`issues/storage-is-layers-and-a-role-is-a-filesystem.md`'s owner) | — | The block-session client and driver side allocate nothing per I/O: tables sized by `DEPTH`, reads opened straight from the arena. A reopen refused `Held` by a closing session waits for its retirement | **H:** `DEPTH` traffic with an allocator that fails after warm-up, red on any per-I/O allocation; a reopen during a dead session's close answered only after its last completion, red if the hold is released early |
| P1 (`issues/process-memory-in-4-kib-pages-still-copies-each-programs-text.md`'s owner) | prerequisites | Both rules and the frame rule in `kernel/pure/frames.rs`; residency by charged job; `CHARGE`; `vm_wire`, the per-job allowance and additive charges; `resident` and `restart-hold`; the main stack sized by `PT_GNU_STACK` (8 MiB otherwise); resident by kind; unmovable frames in their own superframes; the ledger figures; `S = 0` | **H:** both rules at every `W` and `A` increase, red when a table build skips the backing rule; a resident fault never waits, red when a swappable fault may take a held frame; `SHM_FOR_JOB` to a resident job gives `W` and to an ordinary one `A`; wiring refused past the allowance; a helper shares its job's allowance; an additive wire, red if the charged job pays; two wirers where the first exits, red if any release can be refused or leaves a rule exceeded; superframe invariants under wire and lend; a hold kept whole. **Q:** a `CHARGE`-only handle creates an object `SHM_FOR_JOB` and is refused as a spawn's job and at `SYS_JOB_FLOOR`, red if either refusal accepts it; `mmap_stress`; each resident row's committed and populated totals reported |
| P2 (`issues/process-memory-in-4-kib-pages-still-copies-each-programs-text.md`'s owner) | P1 | Reverse records and the RAM-object reverse index; `mem_scan`; `mem_reclaim` for clean pages and never-writable copied pages; `mem_read`/`mem_write` and `vm_wire_ticket` on the ticket table; watermarks; the kept fallback; superframe evacuation; the `reclaimer` role; service v0 (plain CLOCK) | **H:** revalidation, floor protection, revoke order, an evacuation case; `Pending` without waiting, every non-resident page requested at once, red if a call waits or requests fewer; an unmap cancels a parked ticket and removes the wirer's charge without waiting, red if the unmap waits or the charge stays; `Lost` removes the wirer's charge, red if it stays; a blocking wire of another job refused, red if accepted; a dropped copied page refilled byte-exact, red if a once-`ReadWrite` or resident range is dropped; a version not purged under a copy range, red if the range is not counted; the fallback with a stuck service; a remap between scan and reclaim. **Q:** a page-cache-heavy run with the controls usable |
Expand Down
27 changes: 27 additions & 0 deletions issues/every-descriptor-call-takes-one-process-wide-lock-twice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
status: open
kind: defect
opened: 2026-11-04
---

# Every descriptor call takes one process-wide lock twice

A `read` or `write` through a descriptor (`sdk/std/sys/pal/mod.rs`'s `on_fd`)
holds its object for the call through `toyos::fd::hold`. That takes the
process's one descriptor-table lock (`sdk/toyos/src/fd.rs`, `TABLE`) to acquire
the slot and again to let it go. Two threads of one process doing I/O on
different descriptors meet on that lock twice per call.

The lock was a plain futex lock, so any collision slept in `FUTEX_WAIT` and
woke through `FUTEX_WAKE`. In the S0-a metal A/B (#1150, round 2), the pipe
benchmark's writer and reader threads (`osbench` `pipe.throughput.*`) made
about 125-150 futex waits a run on main. With the kernel no longer
serializing their pipe calls under its process lock, they made 128,000-153,000
on the T14. That was much of the small-pipe-write regression measured there.

`toyos::sync::lock_word` now spins briefly before sleeping, which turns a
collision into a short wait. Spinning bounds the cost of a collision; it does
not stop two threads meeting on a lock neither needs.

**Exit**: a descriptor's hold for one call takes no lock that a call on another
descriptor takes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
status: open
kind: tooling
opened: 2026-11-05
---

# osbench's malloc churn on the T14 moves a fifth with the allocator's code placement

`mem.malloc_churn.16.t1` and `.4096.t1` lost 18-21% on the T14 when #1150
grew `toyos::sync::lock_word`, which std's allocator never calls: its lock is
std's futex `Mutex`, which goes straight to `toyos_abi::syscall`
(`sdk/std/sys/pal/futex.rs`).
- **The shift:** the growth moved std's allocator code 0x720 bytes, which puts
dlmalloc's `malloc` and `free`, `__rdl_alloc`, `__rdl_dealloc`, std's
`dealloc` and `Mutex::lock_contended` 32 bytes off their earlier position
modulo 64.
- **The proof:** 32 inert bytes beside `lock_word` put them back. That arm,
which keeps the grown lock, reads base's values: 36514 and 36658 kops/s
against base's 36586, and 28800 for the grown lock without the bytes
(evidence `ToyOSOrg/evidence`, branch `pr-1150`, r13 for the symbol tables
and r14 for the boots).

The AMD bench does not show it. So on this core a hot loop's throughput
depends on where an unrelated change leaves the allocator, and a benchmark
comparison across any change that resizes earlier code can report a fifth
of noise as a regression or a gain.

The mechanism inside the core is not established: which front-end structure
(the decoded-µop cache's 32-byte windows, or a branch boundary) the
placement crosses. No performance counter has been read.

**Exit:** the churn rows hold within run-to-run under a placement change: the
hot allocator functions aligned so that a shift of earlier code moves them by
whole aligned units, or osbench's comparison reports placement as its own
arm. Either is measured on the T14 with a padding arm like this one.
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,19 @@ are split in two. **7a**, delivered by the storage track's file-server work:
a pager's objects, read through `SYS_PAGED_READ` and never mapped
(`kernel/src/object/paged.rs`). **7b** stays this track's: mappings of them,
faults that park, their page tables and reverse records, holders' kernel
writes and their admission, after stage 4's seal.
writes and their admission. **7b comes before stage 4**, by the
orchestrator's ruling of 2026-11-01 on the owner's delegation: file mappings
are on the critical path for file parity with Linux and for the storage
track's name cache, and stage 4's seal layers onto mappings rather than the
reverse. By the orchestrator's ruling of 2026-11-03, 7b needs two things:
the swap track's S0-a half that is met, every user copy outside every spin
lock (a fault that parks on a pager can hold none, and `user_ptr` refuses a
copy made under one); and, after the storage consolidation (#1152) lands, the
same gate extended to sleeping locks, so a user copy under any kernel lock is
refused. #1152 deleted the kernel's sleeping lock (`kernel/src/sleeplock.rs`),
so the spin-lock gate is that gate and both are met. The rest of S0-a's row
(a chunk faulted in before any lock and pinned after a re-check, the lost-page
outcomes, its H model) is built as part of 7b. One outcome 7b must place: a pipe read or message receive of at most
`frame_bytes::BOUNCE` bytes lets its bytes go, and a receive installs its
handles, before copying out to the caller. A lost page met in that copy must
not lose them. Faulting the destination in before the take is one way.
11 changes: 11 additions & 0 deletions issues/toyos-runs-on-arm64.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,14 @@ Linux guest. The timing is one measurement, taken by hand and not automated
(`issues/process-memory-in-4-kib-pages-still-copies-each-programs-text.md`):
2 MiB L2 blocks map identically on both arches, so no change is forced by
this track.

## Unmeasured until an AArch64 bench

- **User-copy throughput.** Since #1150, every user copy on AArch64 goes
through the unprivileged `LDTR`/`STTR` routines of `arch::uaccess`: 8 bytes
an access, where it went through the direct map's `memcpy` before. Their
correctness is checked under the guest runner (`virt_user_copy_faults`);
their cost against the old copy is not measured anywhere, as no AArch64 bench
exists and a guest run is no verdict on it. **Exit**: an AArch64 bench's
pipe and IPC rows against the commit before #1150, interleaved, with no row
worse.
13 changes: 9 additions & 4 deletions issues/user-pointer-checks-have-no-spectre-v1-fence.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ opened: 2026-09-29

# User-pointer checks have no Spectre v1 fence

`user_ptr`'s range checks carry no `lfence` and no index mask, so a
mispredicted check can load through a user pointer. Linux at
`Ubuntu-6.8.0-142.142` reports `usercopy/swapgs barriers and __user pointer
sanitization`.
**Built** (#1150): every user copy goes through `arch::uaccess`, whose
routines replace a user address at or past the half's top with 0 without a
branch (`cmovae` on x86-64; `csel` and `csdb` on AArch64), so a mispredicted
range check in `user_ptr` loads no kernel byte through a user pointer. Linux
at `Ubuntu-6.8.0-142.142` reports `usercopy/swapgs barriers and __user
pointer sanitization` for its own user-address masking.

**Left:** the measurement the exit asks for. No gadget has been run against
these routines on any proving machine.

**Exit**: on every proving machine a gadget through each `user_ptr` site
recovers a planted byte no more often than chance, 1 in 256, by a bound the
Expand Down
Loading