From 0e4a9052b3f48ccba46a13548ee9ca3d32dd03cd Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 28 Oct 2026 18:50:53 +0000 Subject: [PATCH] fix(nvme): separate zeroes command limits from bounce buffers --- crates/stela-drivers/src/storage/nvme.rs | 76 ++++++++++++++---- .../stela-drivers/src/storage/nvme/owned.rs | 8 +- .../src/storage/nvme/owned/tests/limits.rs | 79 +++++++++++++++++++ 3 files changed, 146 insertions(+), 17 deletions(-) create mode 100644 crates/stela-drivers/src/storage/nvme/owned/tests/limits.rs diff --git a/crates/stela-drivers/src/storage/nvme.rs b/crates/stela-drivers/src/storage/nvme.rs index d6ead6b2cf..4bfb56443f 100644 --- a/crates/stela-drivers/src/storage/nvme.rs +++ b/crates/stela-drivers/src/storage/nvme.rs @@ -220,6 +220,9 @@ const IO_ENTRIES: u16 = 64; /// domain, whose buffers change hands whole. const DEPTH: usize = 8; const BOUNCE_BYTES: usize = 128 * 1024; +// Linux PCI NVME_MAX_BYTES bounds the logical command range independently of +// payload storage. Write Zeroes transfers no host data through the bounce pool. +const MAX_COMMAND_BYTES: usize = 8 * 1024 * 1024; const COMMAND_TIMEOUT_US: u64 = 30_000_000; /// Linux `NVME_ADMIN_TIMEOUT`. const ADMIN_TIMEOUT_US: u64 = 60_000_000; @@ -565,7 +568,10 @@ struct Controller { /// The I/O completion queue raises MSI-X vector 0, which the host /// gave the message of its completion signal. signalled: bool, + /// Maximum data payload, bounded by the bounce allocation. max_transfer: usize, + /// MDTS/host command bound used when no non-MDTS limit is supplied. + max_command_bytes: usize, /// How long a normal shutdown may take. shutdown_timeout_us: u64, /// The controller acknowledged a shutdown; it takes no more commands. @@ -1698,8 +1704,20 @@ impl Controller { } /// What `nvme_init_ctrl_finish` keeps from Identify Controller: the - /// retry delays and the events to enable. + /// retry delays, transfer limits and the events to enable. A reset's fresh + /// Identify updates both limits before namespace features are rescanned. fn identified(&mut self, identify: &[u8]) { + let mdts = identify[CONTROLLER_MAXIMUM_TRANSFER]; + let bytes = if mdts == 0 { + usize::MAX + } else { + 1usize + .checked_shl(u32::from(mdts)) + .unwrap_or(usize::MAX) + .saturating_mul(PAGE) + }; + self.max_transfer = BOUNCE_BYTES.min(bytes); + self.max_command_bytes = MAX_COMMAND_BYTES.min(bytes); for (index, delay) in self.retry_delays.iter_mut().enumerate() { let at = CONTROLLER_RETRY_DELAYS + 2 * index; *delay = u16::from_le_bytes([identify[at], identify[at + 1]]); @@ -2147,6 +2165,7 @@ pub fn attach( recovery: None, signalled, max_transfer: BOUNCE_BYTES, + max_command_bytes: MAX_COMMAND_BYTES, shutdown_timeout_us: SHUTDOWN_MINIMUM_US, shut_down: false, command_id: 0, @@ -2197,8 +2216,6 @@ fn discover( let request = HostMemoryRequest::from_identify(&identify); let serial = ascii(&identify[CONTROLLER_SERIAL]); let model = ascii(&identify[CONTROLLER_MODEL]); - // The maximum transfer, in minimum pages as a power of two; 0 is none. - let maximum = identify[CONTROLLER_MAXIMUM_TRANSFER]; let rtd3_entry_us = u64::from(le32(&identify[CONTROLLER_RTD3_ENTRY], 0)); let namespaces = le32(&identify[CONTROLLER_NAMESPACES], 0); controller.commands = u16::from_le_bytes([ @@ -2208,10 +2225,6 @@ fn discover( // Linux `nvme_init_identify` takes a present cache as one to flush and // leaves the controller's Volatile Write Cache feature as it is. let write_cache = identify[CONTROLLER_VOLATILE_CACHE] & 1 != 0; - if maximum != 0 { - let limit = 1usize.checked_shl(u32::from(maximum)).unwrap_or(usize::MAX); - controller.max_transfer = controller.max_transfer.min(limit.saturating_mul(PAGE)); - } controller.shutdown_timeout_us = shutdown_timeout_us(rtd3_entry_us); controller.identified(&identify); controller.allocate_host_memory(request, host_memory); @@ -2249,8 +2262,9 @@ fn discover( failures: Vec::new(), }; let mut found = Vec::new(); - let (max_transfer, commands, scans, limits) = ( + let (max_transfer, max_command_bytes, commands, scans, limits) = ( controller.max_transfer, + controller.max_command_bytes, controller.commands, controller.scans, controller.limits, @@ -2297,7 +2311,16 @@ fn discover( blocks: format.blocks, block_size: format.block_size, read_only: format.read_only, - features: features(write_cache, commands, limits, &format, max_transfer), + features: features( + write_cache, + commands, + limits, + &format, + TransferLimits { + data: max_transfer, + command: max_command_bytes, + }, + ), }, }) .collect(); @@ -2344,10 +2367,18 @@ fn apst_table(identify: &[u8]) -> Option<[u64; 32]> { Some(table) } +/// Payload storage and logical command-range limits are separate: a no-data +/// command does not consume the smaller DMA bounce-buffer allowance. +#[derive(Clone, Copy)] +struct TransferLimits { + data: usize, + command: usize, +} + /// A namespace's features, as Linux `nvme_update_disk_info` sets them: /// FUA with a volatile write cache; Deallocate as discard with Dataset /// Management, one range of up to DMRSL blocks, or else `UINT_MAX` sectors; -/// and Write Zeroes up to WZSL, or else the largest transfer +/// and Write Zeroes up to WZSL, or else the controller's command range /// (`nvme_init_non_mdts_limits`), which may deallocate where the namespace /// reads deallocated blocks as zeros and says Write Zeroes deallocates /// (`NVME_NS_DEAC`). A Write Zeroes command takes at most 2^16 blocks. @@ -2356,13 +2387,13 @@ fn features( commands: u16, limits: Limits, format: &Format, - max_transfer: usize, + transfer: TransferLimits, ) -> Features { let block_shift = format.block_size.trailing_zeros(); let sectors = u64::from(u32::MAX) >> (block_shift - 9); let deallocate = u64::from(limits.deallocate); let zeroes = match limits.write_zeroes { - 0 => max_transfer as u64, + 0 => transfer.command as u64, // 2^WZSL minimum pages of 4 KiB, saturating as nvme_mps_to_sectors. units => 1u64 .checked_shl(u32::from(units) + 12) @@ -2372,7 +2403,7 @@ fn features( Features { write_cache, fua: write_cache, - max_transfer, + max_transfer: transfer.data, discard: (commands & COMMANDS_DATASET_MANAGEMENT != 0).then_some( if deallocate != 0 && deallocate <= sectors { deallocate @@ -2463,7 +2494,11 @@ impl Namespace { self.scanned = scans; return Ok(()); }; - let (version, max_transfer) = (controller.version, controller.max_transfer); + let (version, max_transfer, max_command_bytes) = ( + controller.version, + controller.max_transfer, + controller.max_command_bytes, + ); let (commands, limits) = (controller.commands, controller.limits); let data = match controller.identify(IDENTIFY_NAMESPACE, self.namespace) { Ok(data) => data, @@ -2485,8 +2520,16 @@ impl Namespace { self.identity.block_size = format.block_size; self.identity.read_only = format.read_only; let write_cache = self.identity.features.write_cache; - self.identity.features = - features(write_cache, commands, limits, &format, max_transfer); + self.identity.features = features( + write_cache, + commands, + limits, + &format, + TransferLimits { + data: max_transfer, + command: max_command_bytes, + }, + ); Ok(()) } Err(error) if allocated && ids == self.ids => { @@ -2669,6 +2712,7 @@ mod tests { recovery: None, signalled: false, max_transfer: BOUNCE_BYTES, + max_command_bytes: MAX_COMMAND_BYTES, shutdown_timeout_us: 1000, shut_down: false, command_id: 0, diff --git a/crates/stela-drivers/src/storage/nvme/owned.rs b/crates/stela-drivers/src/storage/nvme/owned.rs index fff5308fd6..3d450368cc 100644 --- a/crates/stela-drivers/src/storage/nvme/owned.rs +++ b/crates/stela-drivers/src/storage/nvme/owned.rs @@ -447,6 +447,7 @@ unsafe fn attach_with_timeout( recovery: None, signalled: false, max_transfer: BOUNCE_BYTES, + max_command_bytes: MAX_COMMAND_BYTES, shutdown_timeout_us: SHUTDOWN_MINIMUM_US, shut_down: false, command_id: 0, @@ -638,6 +639,7 @@ pub(crate) use tests::protocol; #[cfg(test)] mod tests { + mod limits; pub(crate) mod protocol; use super::*; use crate::bus::dma::{ @@ -827,6 +829,7 @@ mod tests { /// ONCS, and the NVM Identify Controller's WZSL and DMRSL; none /// refuses the Identify. oncs: u16, + mdts: u8, nvm_limits: Option<(u8, u32)>, nvm_identifies: usize, } @@ -899,6 +902,7 @@ mod tests { fatal: false, aborts: 0, oncs: 0, + mdts: 0, nvm_limits: None, nvm_identifies: 0, } @@ -1038,6 +1042,7 @@ mod tests { 1 => { data[4..24].fill(b'S'); data[24..64].fill(b'M'); + data[77] = self.mdts; data[516..520].copy_from_slice(&1u32.to_le_bytes()); data[520..522].copy_from_slice(&self.oncs.to_le_bytes()); data[525] = 1; // A volatile write cache. @@ -1766,7 +1771,8 @@ mod tests { let attached = f.attach().unwrap(); let features = attached.namespaces[0].identity().features; assert_eq!(features.discard, Some(u64::from(u32::MAX))); - let transfer = (features.max_transfer / 512) as u64; + let transfer = (MAX_COMMAND_BYTES / 512) as u64; + assert_eq!(features.max_transfer, BOUNCE_BYTES); assert_eq!(features.write_zeroes.unwrap().blocks, transfer); assert_eq!(f.model.lock().unwrap().nvm_identifies, 0); drop(attached); diff --git a/crates/stela-drivers/src/storage/nvme/owned/tests/limits.rs b/crates/stela-drivers/src/storage/nvme/owned/tests/limits.rs new file mode 100644 index 0000000000..c76fc213c5 --- /dev/null +++ b/crates/stela-drivers/src/storage/nvme/owned/tests/limits.rs @@ -0,0 +1,79 @@ +//! Controller command limits and no-data Write Zeroes encoding. +use super::*; + +#[test] +fn write_zeroes_default_uses_mdts_without_the_payload_bounce_cap() { + for (mdts, command_bytes, payload_bytes) in [ + (0, MAX_COMMAND_BYTES, BOUNCE_BYTES), + (4, 64 * 1024, 64 * 1024), + (7, 512 * 1024, BOUNCE_BYTES), + (31, MAX_COMMAND_BYTES, BOUNCE_BYTES), + (63, MAX_COMMAND_BYTES, BOUNCE_BYTES), + (64, MAX_COMMAND_BYTES, BOUNCE_BYTES), + (255, MAX_COMMAND_BYTES, BOUNCE_BYTES), + ] { + let f = Fixture::new(); + { + let mut model = f.model.lock().unwrap(); + model.oncs = COMMANDS_WRITE_ZEROES; + model.mdts = mdts; + } + let attached = f.attach().unwrap(); + let features = attached.namespaces[0].identity().features; + assert_eq!(features.max_transfer, payload_bytes, "MDTS {mdts}"); + assert_eq!( + features.write_zeroes.unwrap().blocks, + (command_bytes / 512) as u64, + "MDTS {mdts}" + ); + } +} + +#[test] +fn explicit_wzsl_overrides_mdts_and_still_obeys_nlb_width() { + for (wzsl, blocks) in [(1, 16), (8, 2048), (20, 65536), (255, 65536)] { + let f = Fixture::new(); + { + let mut model = f.model.lock().unwrap(); + model.registers.insert(8, 0x0001_0200); + model.oncs = COMMANDS_WRITE_ZEROES; + model.mdts = 1; + model.nvm_limits = Some((wzsl, 0)); + } + let attached = f.attach().unwrap(); + let features = attached.namespaces[0].identity().features; + assert_eq!(features.max_transfer, 8192); + assert_eq!(features.write_zeroes.unwrap().blocks, blocks, "WZSL {wzsl}"); + } +} + +#[test] +fn write_zeroes_nlb_boundaries_have_no_host_data_pointers() { + let fixture = Fixture::new(); + let attached = fixture.attach().unwrap(); + let mut controller = attached.namespaces[0].controller.borrow_mut(); + for blocks in [1, 65536] { + let command = Command::WriteZeroes { + lba: 0x1_0000_0000, + blocks, + unmap: false, + }; + let words = controller.io_command(0, 1, 512, &command).unwrap(); + assert_eq!(words[0], u32::from(IO_WRITE_ZEROES)); + assert_eq!(words[1], 1); + assert_eq!(words[6..10], [0; 4]); + assert_eq!(words[10..12], [0, 1]); + assert_eq!(words[12], (blocks - 1) as u32); + } + for blocks in [0, 65537] { + let command = Command::WriteZeroes { + lba: 0, + blocks, + unmap: false, + }; + assert_eq!( + controller.io_command(0, 1, 512, &command), + Err(BlockError::Unsupported("Write Zeroes past 65536 blocks")) + ); + } +}