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 Source

D1 Write-Path Correctness Review - AIENOS P3 Native NVMe Write + Flush

Repo: aien-dev/aienux @ ef359ec (mirror ~/aienos-nvme-rw, read-only). Scope: crates/aienos-kernel/src/nvme.rs (Submission::write, io_command, build_prps, Completion) and crates/aienos-kernel/src/nvme/driver.rs (transfer, submit_io, write_blocks/read_blocks/flush).

Verdict: the write lane is functionally correct for the read substrate it reuses. One latent defect is flagged (unreachable under the current MDTS cap) plus one non-blocking resource observation.


1. CDW layout (opcode / NSID / PRP / LBA / NLB)

io_command (nvme.rs:148-160) writes, for a write (opcode 0x01):

FieldDwordSourceLine
OPCODECDW0[7:0]0x01nvme.rs:150
CIDCDW0[31:16]overlaid in submit_iodriver.rs:370
NSIDCDW11nvme.rs:151
PRP1CDW6/CDW7p1 lo/hinvme.rs:152-153
PRP2CDW8/CDW9p2 lo/hinvme.rs:154-155
LBACDW10/CDW11lba lo/hinvme.rs:156-157
NLBCDW12[15:0]blocks - 1nvme.rs:158

OK. Layout matches NVMe Base Spec I/O Write. Submission::write (nvme.rs:135-137) only selects opcode 0x01, so write and read share the same encoder and CDW12 semantics.

2. Command ID lifecycle

submit_io takes the next CID (monotonic next_cid), overlays it into CDW0[31:16] preserving the opcode low half, and consumes only a completion whose command_id() equals that CID (driver.rs:368-370, 389-390). A mismatch is reported rather than silently matched. OK. Covered by tests each_io_command_gets_a_new_cid_and_completion_is_consumed and nonzero_write_completion_maps_to_device_error.

3. NLB = blocks - 1

set_u32(12, (blocks as u32).wrapping_sub(1)) (nvme.rs:158). NVMe NLB is 0-based, so one block -> 0, eight -> 7. transfer guarantees blocks >= 1 because data.is_empty() is rejected and each chunk length is >= block_size (driver.rs:296-298, 322-323), so the wrapping_sub cannot underflow on the write path. OK.

4. LBA split across CDW10 / CDW11

set_u32(10, lba as u32) / set_u32(11, (lba >> 32) as u32) (nvme.rs:156-157). Transfer passes lba + offset/block_size (driver.rs:350). Because the range check bounds lba + blocks <= block_count, the per-chunk LBA cannot overflow u64. OK.

5. PRP1 / PRP2 / page-list construction reused from read

The write path is the read path with an extra DMA pre-fill, so the PRP construction is genuinely shared:

Because the data region is page-aligned, build_prps' internal page count (nvme.rs:272-277, offset = 0) exactly equals list_pages, so the ">2 pages" test never disagrees with the encoder. PRP1 is the raw buffer address, PRP2 the second page or the list address, and the list entries are the remaining data pages - identical to the read path. OK.

6. MDTS chunking

mdts_limit is 128 KiB when mdts == 0, else PAGE_SIZE << mdts clamped to 128 KiB (driver.rs:308-315); chunk_limit is floored to a whole block (driver.rs:316). This is a deliberate conservative cap: MDTS=0 means "no limit" in the spec, but capping is always safe. MDTS units are the minimum page size, and init refuses CAP.MPSMIN != 0 (driver.rs:118-120), so the PAGE_SIZE base is correct. checked_shl + unwrap_or(usize::MAX) avoids the mdts = 255 shift panic. OK.

NEEDS-FIX (latent): the single-page PRP list is only safe because MDTS is capped

7. Per-chunk commands

The chunk loop advances by whole blocks and recomputes the LBA from the block offset (driver.rs:320-360), submitting exactly one I/O command per chunk via submit_io (driver.rs:349-353). Each chunk gets its own CID and its own completion (synchronous poll), and the next chunk's DMA is not allocated until the previous command has completed, so no buffer is freed while the device may still be reading it. OK. Covered by mdts_chunking_submits_per_chunk_commands_with_correct_lba_and_nlb (300 blocks -> 256 + 44, NLB 255 and 43, LBA 20 and 276).

8. Transfer bounds (no I/O on rejection)

lba.checked_add(blocks).filter(|end| *end <= block_count) rejects overflowing or past-end ranges with BlockError::OutOfRange (driver.rs:301-307) before any allocation or submit_io. OK. Covered by write_past_block_count_is_out_of_range_without_io_submission, which asserts the command count, I/O doorbell count, and DMA region count are all unchanged.

9. Empty / non-block-multiple input

data.is_empty() || !data.len().is_multiple_of(block_size) -> InvalidInput (driver.rs:296-298) before bounds and before I/O. OK. Covered by empty_and_non_block_multiple_buffers_are_invalid_input.

10. Write completion status -> DeviceError

submit_io advances the completion head (and phase on wrap), rings the CQ doorbell, then returns BlockError::DeviceError for any nonzero status (driver.rs:392-403). OK. Covered by nonzero_write_completion_maps_to_device_error.

11. Flush

flush submits opcode 0x00 with NSID 1 and no PRP (driver.rs:432-439); submit_io handles it identically to a payload command and the device replies with a normal completion. OK.

12. Observation (non-blocking): redundant copy in write_blocks

write_blocks does buffer.to_vec() (driver.rs:429) solely to satisfy transfer's &mut [u8] parameter, which the write path never mutates. For a 128 KiB chunk this doubles transient kernel memory. Not a correctness defect; a write-specific path taking &[u8] would remove it.


Required corrections