interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / docs / P3_NVME_WRITE_REVIEW.md

152 lines · 7293 bytes Raw Blame History Rendered
1# D1 Write-Path Correctness Review - AIENOS P3 Native NVMe Write + Flush
2
3Repo: aien-dev/aienux @ `ef359ec` (mirror `~/aienos-nvme-rw`, read-only).
4Scope: `crates/aienos-kernel/src/nvme.rs` (`Submission::write`, `io_command`,
5`build_prps`, `Completion`) and `crates/aienos-kernel/src/nvme/driver.rs`
6(`transfer`, `submit_io`, `write_blocks`/`read_blocks`/`flush`).
7
8Verdict: the write lane is functionally correct for the read substrate it reuses.
9One latent defect is flagged (unreachable under the current MDTS cap) plus one
10non-blocking resource observation.
11
12---
13
14## 1. CDW layout (opcode / NSID / PRP / LBA / NLB)
15
16`io_command` (`nvme.rs:148-160`) writes, for a write (`opcode 0x01`):
17
18| Field | Dword | Source | Line |
19|---|---|---|---|
20| OPCODE | CDW0[7:0] | `0x01` | `nvme.rs:150` |
21| CID | CDW0[31:16] | overlaid in `submit_io` | `driver.rs:370` |
22| NSID | CDW1 | `1` | `nvme.rs:151` |
23| PRP1 | CDW6/CDW7 | `p1` lo/hi | `nvme.rs:152-153` |
24| PRP2 | CDW8/CDW9 | `p2` lo/hi | `nvme.rs:154-155` |
25| LBA | CDW10/CDW11 | `lba` lo/hi | `nvme.rs:156-157` |
26| NLB | CDW12[15:0] | `blocks - 1` | `nvme.rs:158` |
27
28**OK.** Layout matches NVMe Base Spec I/O Write. `Submission::write`
29(`nvme.rs:135-137`) only selects opcode `0x01`, so write and read share the
30same encoder and CDW12 semantics.
31
32## 2. Command ID lifecycle
33
34`submit_io` takes the next CID (monotonic `next_cid`), overlays it into
35CDW0[31:16] preserving the opcode low half, and consumes only a completion whose
36`command_id()` equals that CID (`driver.rs:368-370`, `389-390`). A mismatch is
37reported rather than silently matched. **OK.** Covered by tests
38`each_io_command_gets_a_new_cid_and_completion_is_consumed` and
39`nonzero_write_completion_maps_to_device_error`.
40
41## 3. NLB = blocks - 1
42
43`set_u32(12, (blocks as u32).wrapping_sub(1))` (`nvme.rs:158`). NVMe NLB is
440-based, so one block -> 0, eight -> 7. `transfer` guarantees `blocks >= 1`
45because `data.is_empty()` is rejected and each chunk length is `>= block_size`
46(`driver.rs:296-298`, `322-323`), so the `wrapping_sub` cannot underflow on the
47write path. **OK.**
48
49## 4. LBA split across CDW10 / CDW11
50
51`set_u32(10, lba as u32)` / `set_u32(11, (lba >> 32) as u32)`
52(`nvme.rs:156-157`). Transfer passes `lba + offset/block_size`
53(`driver.rs:350`). Because the range check bounds `lba + blocks <= block_count`,
54the per-chunk LBA cannot overflow `u64`. **OK.**
55
56## 5. PRP1 / PRP2 / page-list construction reused from read
57
58The write path is the read path with an extra DMA pre-fill, so the PRP
59construction is genuinely shared:
60
61- data region allocated at `PAGE_SIZE` alignment (`driver.rs:324`),
62- `list_pages = len.div_ceil(PAGE_SIZE)`; a list region is allocated only when
63 more than two pages are spanned (`driver.rs:331-336`),
64- `build_prps(region.physical, len, PAGE_SIZE, list_address, &mut list)` returns
65 `(p1, p2, used)` and those are passed unchanged to `Submission::write`
66 (`driver.rs:339-353`).
67
68Because the data region is page-aligned, `build_prps`' internal page count
69(`nvme.rs:272-277`, offset = 0) exactly equals `list_pages`, so the ">2 pages"
70test never disagrees with the encoder. PRP1 is the raw buffer address, PRP2 the
71second page or the list address, and the list entries are the remaining data
72pages - identical to the read path. **OK.**
73
74## 6. MDTS chunking
75
76`mdts_limit` is `128 KiB` when `mdts == 0`, else `PAGE_SIZE << mdts` clamped to
77`128 KiB` (`driver.rs:308-315`); `chunk_limit` is floored to a whole block
78(`driver.rs:316`). This is a deliberate conservative cap: MDTS=0 means
79"no limit" in the spec, but capping is always safe. MDTS units are the minimum
80page size, and `init` refuses `CAP.MPSMIN != 0` (`driver.rs:118-120`), so the
81`PAGE_SIZE` base is correct. `checked_shl` + `unwrap_or(usize::MAX)` avoids the
82`mdts = 255` shift panic. **OK.**
83
84### NEEDS-FIX (latent): the single-page PRP list is only safe because MDTS is capped
85- **Where:** `driver.rs:331-337` (one `PAGE_SIZE` list region and a
86 `PAGE_SIZE / 8 = 512`-entry list buffer).
87- **Why:** `build_prps` supports chaining across contiguous list pages
88 (`nvme.rs:295-316`), but the driver allocates exactly one list page. A chunk
89 needing more than 511 data-page entries (i.e. `> 2 MiB` at 4 KiB pages) would
90 fail `build_prps` with `ListTooSmall`, which `transfer` maps to
91 `BlockError::InvalidInput` (`driver.rs:341`), failing every large transfer. This
92 is unreachable today only because `mdts_limit` is hard-capped at `128 KiB`
93 (`driver.rs:314`). The cap is load-bearing and undocumented.
94- **Minimal fix:** size the list allocation and buffer from the page count, e.g.
95 `let entries = (list_pages - 1).max(1); let list_pages_needed = entries.div_ceil(PAGE_SIZE / 8);`
96 allocate `list_pages_needed * PAGE_SIZE` bytes and
97 `alloc::vec![0u64; list_pages_needed * (PAGE_SIZE / 8)]`. Alternatively, add a
98 comment asserting the 128 KiB cap is what keeps the list within one page.
99
100## 7. Per-chunk commands
101
102The chunk loop advances by whole blocks and recomputes the LBA from the block
103offset (`driver.rs:320-360`), submitting exactly one I/O command per chunk via
104`submit_io` (`driver.rs:349-353`). Each chunk gets its own CID and its own
105completion (synchronous poll), and the next chunk's DMA is not allocated until
106the previous command has completed, so no buffer is freed while the device may
107still be reading it. **OK.** Covered by
108`mdts_chunking_submits_per_chunk_commands_with_correct_lba_and_nlb`
109(300 blocks -> 256 + 44, NLB 255 and 43, LBA 20 and 276).
110
111## 8. Transfer bounds (no I/O on rejection)
112
113`lba.checked_add(blocks).filter(|end| *end <= block_count)` rejects overflowing
114or past-end ranges with `BlockError::OutOfRange` (`driver.rs:301-307`) *before*
115any allocation or `submit_io`. **OK.** Covered by
116`write_past_block_count_is_out_of_range_without_io_submission`, which asserts
117the command count, I/O doorbell count, and DMA region count are all unchanged.
118
119## 9. Empty / non-block-multiple input
120
121`data.is_empty() || !data.len().is_multiple_of(block_size)` -> `InvalidInput`
122(`driver.rs:296-298`) before bounds and before I/O. **OK.** Covered by
123`empty_and_non_block_multiple_buffers_are_invalid_input`.
124
125## 10. Write completion status -> DeviceError
126
127`submit_io` advances the completion head (and phase on wrap), rings the CQ
128doorbell, then returns `BlockError::DeviceError` for any nonzero status
129(`driver.rs:392-403`). **OK.** Covered by
130`nonzero_write_completion_maps_to_device_error`.
131
132## 11. Flush
133
134`flush` submits opcode `0x00` with NSID 1 and no PRP (`driver.rs:432-439`);
135`submit_io` handles it identically to a payload command and the device replies
136with a normal completion. **OK.**
137
138## 12. Observation (non-blocking): redundant copy in `write_blocks`
139
140`write_blocks` does `buffer.to_vec()` (`driver.rs:429`) solely to satisfy
141`transfer`'s `&mut [u8]` parameter, which the write path never mutates. For a
142128 KiB chunk this doubles transient kernel memory. Not a correctness defect;
143a write-specific path taking `&[u8]` would remove it.
144
145---
146
147## Required corrections
148
149- **NEEDS-FIX (latent, ยง6):** single-page PRP list allocation is correct only
150 under the 128 KiB MDTS cap; make the list sizing derive from the page count
151 (or document the invariant). No runtime impact at `ef359ec`.
152- All other reviewed items: **OK**.