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_FLUSH_REVIEW.md

128 lines · 7387 bytes Raw Blame History Rendered
1# D2 Flush Review - AIENOS P3 NVMe Flush lane
2
3Base: `aien-dev/aienux` @ `ef359ec` (read-only mirror `~/aienos-nvme-rw`).
4Files read in full: `crates/aienos-kernel/src/nvme.rs`,
5`crates/aienos-kernel/src/nvme/driver.rs`, `crates/aienos-kernel/src/block.rs`.
6Companion tests: `nvme-rw-subagents/d2/nvme_flush.rs` (target `crates/aienos-kernel/tests/nvme_flush.rs`).
7
8## 1. Is `flush()` a correct NVMe Flush command?
9
10Implementation (driver.rs:432 - 439) builds a zeroed `Submission`, sets CDW0 opcode = `0`
11and CDW1 NSID = `1`, then hands it to `submit_io` (driver.rs:364 - 406).
12
13- **Opcode** - `0x00` (NVM Flush) in CDW0 bits 7:0. `submit_io` ORs the CID into CDW0
14 bits 31:16 (driver.rs:370), leaving opcode/FUSE intact. Correct.
15- **NSID** - CDW1 = 1. Correct for the single namespace the driver identifies
16 (`identify_namespace(1)`), but hardcoded (see NEEDS-FIX 1).
17- **CDW10..CDW15** - left zero. Correct; Flush defines no command-specific fields
18 (no LBA, no NLB, no FUA).
19- **Data pointers** - PRP1/PRP2 are zero. Flush is a data-less command; PRPs are
20 reserved, so requiring none is correct and the tests assert `(prp1, prp2) == (0, 0)`.
21- **Queue** - submitted on the polled I/O SQ/CQ (QID 1), which is the correct command
22 set for an NVM I/O command.
23- **CID** - drawn from the shared `next_cid` (driver.rs:368 - 369), so a Flush never
24 aliases the preceding Write's CID; the test asserts monotonic `+1`.
25- **Completion semantics** - `status = c.status() >> 1`; `0 -> Ok(())`, nonzero ->
26 `BlockError::DeviceError` (driver.rs:392 - 403), and the poll budget is bounded
27 (1001 iterations) -> `BlockError::Timeout`. This is the honest mapping: durability
28 is exposed only through a successful completion.
29
30Verdict: the command layout and completion handling are a correct NVMe Flush.
31
32## 2. Are `BlockDevice::flush` semantics honest?
33
34`BlockDevice` (block.rs:11 - 17) declares `flush(&mut self) -> Result<(), BlockError>`
35with no documented durability contract. Two implementors differ sharply:
36
37- `NvmeController::flush` (driver.rs:432) - **honest**: it actually issues the Flush
38 command and reports backend failure/timeout as an error. It is not a no-op.
39- `RamDisk::flush` (block.rs:129 - 131) - **unconditional `Ok(())`**, a literal no-op.
40 For a purely volatile in-memory disk this is defensible (there is no media to
41 persist to), but it must be understood as "no durability guarantee", not as a
42 successful flush. It is the exact pattern that must **not** be copied into a
43 real-device driver.
44
45Additional honesty concerns:
46
47- `write_blocks` never sets the FUA bit (CDW12 bit 14) and never auto-flushes
48 (driver.rs:428 - 431, 349 - 353). Durability is only obtained if the caller explicitly
49 calls `flush()`; callers must not assume `write_blocks` is durable.
50- The trait does not state whether `flush` is a cache-flush barrier, an ordering
51 barrier, or both, nor which namespace(s) it covers. The NVMe impl flushes NSID 1
52 only.
53- All nonzero completion statuses (including DNR, SCT/SC detail) collapse to
54 `DeviceError`, losing diagnostic detail (acceptable for the coarse `BlockError`
55 enum, but worth noting).
56
57## 3. What does QEMU's emulated NVMe do with Flush?
58
59QEMU does **not** ignore it. In `hw/nvme/ctrl.c` (upstream, v1.4-capable):
60
61- I/O dispatch routes `NVME_CMD_FLUSH` (opcode `0x00`) to `nvme_flush()`
62 (`nvme_io_cmd` path, `ctrl.c:4748`).
63- `nvme_flush()` validates the NSID (`NVME_INVALID_NSID | NVME_DNR` if invalid,
64 `NVME_INVALID_FIELD | NVME_DNR` if the namespace is absent), supports
65 `NVME_NSID_BROADCAST` (`0xFFFFFFFF`) to flush every namespace, then calls
66 `nvme_do_flush()` (`ctrl.c:3622 - 3659`).
67- `nvme_flush_ns_cb()` issues `blk_aio_flush(ns->blkconf.blk, ...)` against the
68 namespace's `BlockBackend` (`ctrl.c:3582`). If the backend returns an error,
69 `nvme_flush_ns_cb` sets `req->status = NVME_WRITE_FAULT` (`ctrl.c:3570 - 3573`),
70 which is returned as a **nonzero completion status** - exactly what the AIENOS driver
71 maps to `BlockError::DeviceError`.
72
73So a successful Flush completion in QEMU means the flush reached the block backend
74and the backend reported success. Whether that persists to the host file depends on
75the drive's cache mode:
76
77- `cache=writeback` (QEMU default): `blk_aio_flush` propagates to the format/protocol
78 driver and typically results in an `fsync`/`fdatasync` of the backing file - the
79 data reaches the host file.
80- `cache=none` / `cache=directsync`: writes are already synchronous, so the flush may
81 be a no-op at the file layer but still completes successfully.
82- `cache=unsafe`: QEMU is explicitly told to ignore flushes / not use `O_DSYNC`; a
83 successful Flush then does **not** guarantee host persistence.
84
85Net: emulated Flush is real and reaches the backend; host-level durability is only as
86strong as the drive's cache configuration. The AIENOS driver correctly treats a nonzero
87completion as failure.
88
89## 4. Flush must not be faked as a no-op
90
91A silent `flush() -> Ok(())` in a real NVMe driver is a durability bug: it reports
92success without ever telling the controller to commit its volatile write cache, so a
93power loss can lose acknowledged writes. The current `NvmeController::flush` avoids
94this by issuing opcode `0x00` and requiring a zero-status completion. The tests in
95`nvme_flush.rs` enforce this directly: they assert an I/O SQ doorbell write occurs,
96that the parsed command reaches the simulator, that a nonzero status yields
97`DeviceError`, and that a missing completion yields `Timeout` - i.e. flush can never
98silently succeed. The `RamDisk` no-op is acceptable only because RAM is inherently
99volatile and must never be treated as the pattern for a persistent device.
100
101## NEEDS-FIX / observations
102
1031. **Hardcoded NSID 1** (driver.rs:437). `flush()` should target the active
104 namespace ID (or support `NVME_NSID_BROADCAST` like QEMU) rather than a literal
105 `1`, or the trait should document the single-namespace restriction. Currently
106 consistent only because the driver identifies/uses NSID 1 exclusively.
1072. **No durability contract on `BlockDevice::flush`** (block.rs:11 - 17). Document that
108 `flush` is a cache-commit barrier, that `write_blocks` is not implicitly durable,
109 and that `RamDisk::flush` is a deliberate no-op with no persistence claim.
1103. **`write_blocks` has no FUA path** (driver.rs:349 - 353). Either document that
111 callers must `flush()` for durability or add an FUA option for single-write
112 durability.
1134. **Flush uses the same 1001×1 ms poll budget as data I/O** (driver.rs:379). Real
114 media flushes can take longer; consider a dedicated (longer) flush budget.
1155. **Status detail is lost** (driver.rs:399 - 403). `DeviceError` discards DNR/SCT/SC;
116 acceptable for the coarse enum but consider preserving it for diagnostics.
1176. **No regression test in-repo yet.** `nvme_flush.rs` should be dropped into
118 `crates/aienos-kernel/tests/` and run with `cargo test -p aienos-kernel` (not run
119 here per constraints).
120
121## Test coverage added (`nvme_flush.rs`)
122
123- `flush_submits_opcode_zero_nsid_one_without_data_pointer`
124- `successful_flush_completion_returns_ok`
125- `nonzero_flush_completion_status_returns_device_error`
126- `flush_timeout_returns_timeout_and_is_bounded`
127- `flush_is_not_a_silent_no_op_doorbell_and_command_reach_device`
128- `flush_after_write_in_same_session_uses_fresh_cid`