perf(storage): trust valid NVMe I/O completions without a status read, and read the controller status for an invalid one, as Linux does

This commit is contained in:
Tyler Smith committed 2026-11-12 10:42:08 -05:00
1 parent 0fdeb46aad
commit 7fc82faaee
4 files changed
+120 -16

No files matched your search

+26 -14
View File
@@ -13,10 +13,10 @@ struct ReadDestination<'a> {
}
impl Controller {
/// Before the CPU takes back memory a completion released: an owned
/// controller's completion counts only while its registers answer
/// without a fatal status, which a removed hierarchy or a failed engine
/// would not.
/// Before the CPU takes back memory an admin command's data completion
/// released: an owned controller's completion counts only while its
/// registers answer without a fatal status, which a removed hierarchy or
/// a failed engine would not.
pub(super) fn trust_completion(&self) -> Result<(), BlockError> {
match self.owned {
Some(_) => self.check_fatal(),
@@ -24,6 +24,16 @@ impl Controller {
}
}
/// The error for an I/O completion that names no command it may, or
/// whose entry changed under its read: the controller's own, when its
/// status says it is removed or fatally failed, otherwise a corrupt
/// queue.
fn invalid_completion(&self) -> BlockError {
parts::timed(parts::Part::Status, || self.check_fatal())
.err()
.unwrap_or(BlockError::Device { status: 0xffff })
}
/// Page `index` of those tag `tag` holds.
pub(super) fn tag_page(&mut self, tag: usize, index: usize) -> &mut Buffer {
&mut self.pool.pages[usize::from(self.held[tag][index])]
@@ -436,29 +446,31 @@ impl Controller {
break;
}
let (head, phase, consumed) = (self.io.head, self.io.phase, self.io.consumed);
// A valid completion is trusted as Linux's nvme_handle_cqe trusts
// it, with no register read: the controller's status is read
// where Linux reads it, at a command's timeout, a reset, an admin
// command's data, and a completion that looks invalid.
match self.posted(true) {
Ok(None) => break,
Ok(Some((id, _, status, sq_head))) => {
if !reaped
&& let Err(error) =
parts::timed(parts::Part::Status, || self.trust_completion())
{
(self.io.head, self.io.phase, self.io.consumed) = (head, phase, consumed);
self.poison(error);
self.fail_all();
return;
}
if !self.complete(id, status, sq_head, &mut destination) {
// A completion for no running command, or for one the
// controller had not fetched: the queue is corrupt.
// It stays unretired.
(self.io.head, self.io.phase, self.io.consumed) = (head, phase, consumed);
self.poison(BlockError::Device { status: 0xffff });
let error = self.invalid_completion();
self.poison(error);
self.fail_all();
return;
}
reaped = true;
}
Err(BlockError::Device { status: 0xffff }) => {
let error = self.invalid_completion();
self.poison(error);
self.fail_all();
return;
}
Err(error) => {
self.poison(error);
self.fail_all();
@@ -1540,6 +1540,10 @@ fn owned_absent_coherent_observation_refuses_before_enable_and_releases_unpublis
assert!(m.released && m.maps.is_empty());
assert!(!m.events.contains(&"command") && !m.events.contains(&"bme_on"));
}
/// Registers that fail after a doorbell are found at the next status read:
/// a completion that looks invalid reads the status, and the read's failure
/// poisons the controller with no CPU reacquire. (A valid completion is
/// taken with no register read, as Linux takes it.)
#[test]
fn owned_register_failure_after_doorbell_poison_keeps_no_cpu_reacquire() {
let f = Fixture::new();
@@ -1547,6 +1551,7 @@ fn owned_register_failure_after_doorbell_poison_keeps_no_cpu_reacquire() {
let before = f.model.lock().unwrap().sync_cpu;
let commands = f.model.lock().unwrap().commands;
f.model.lock().unwrap().fail_after_doorbell = true;
f.model.lock().unwrap().mutation = Mutation::Cid;
assert!(a.namespaces[0].flush().is_err());
{
let m = f.model.lock().unwrap();
@@ -20,7 +20,7 @@ pub enum Part {
/// A look at the I/O completion queue: its completions' ends, read data
/// copies included, retries and deadlines.
Advance,
/// The controller status read before completions are trusted.
/// A controller status read after a completion that looked invalid.
Status,
/// An I/O completion doorbell write.
Ring,
+88 -1
View File
@@ -1210,9 +1210,96 @@ fn a_command_counts_its_start_and_finish_parts() {
parts::Part::Start,
parts::Part::Doorbell,
parts::Part::Advance,
parts::Part::Status,
parts::Part::Ring,
] {
assert!(calls(part) >= 1, "{part:?}");
}
}
/// A valid completion is taken with no status read, as Linux takes it, even
/// while the controller reports a fatal status; an invalid one reads the
/// status and fails the controller with it.
#[test]
fn completions_are_trusted_without_a_status_read_unless_they_look_invalid() {
let mut fixture = Fixture::new();
let disk = &mut fixture.disk;
let read = disk.start(Command::Read { lba: 0, bytes: 512 }).unwrap();
{
let mut controller = disk.controller.borrow_mut();
controller.registers.write32(STATUS, FATAL).unwrap();
let id = running_id(&controller, read);
post(&mut controller, id, 0);
}
assert!(matches!(disk.finish(read), Some(Ok(Some(_)))));
let read = disk.start(Command::Read { lba: 0, bytes: 512 }).unwrap();
{
let mut controller = disk.controller.borrow_mut();
let id = running_id(&controller, read);
post(&mut controller, id.wrapping_add(7), 0);
}
let error = BlockError::Device { status: FATAL };
assert_eq!(disk.finish(read), Some(Err(error.clone())));
assert_eq!(disk.controller.borrow().fault, Some(error));
}
/// An invalid completion from a controller whose status is good poisons it
/// as a corrupt queue.
#[test]
fn an_invalid_completion_from_a_healthy_controller_is_a_corrupt_queue() {
let mut fixture = Fixture::new();
let disk = &mut fixture.disk;
let read = disk.start(Command::Read { lba: 0, bytes: 512 }).unwrap();
{
let mut controller = disk.controller.borrow_mut();
controller.registers.write32(STATUS, READY).unwrap();
let id = running_id(&controller, read);
post(&mut controller, id.wrapping_add(7), 0);
}
let corrupt = BlockError::Device { status: 0xffff };
assert_eq!(disk.finish(read), Some(Err(corrupt)));
}
/// A dead controller posts nothing: its commands' timeout reads its status,
/// within the command deadline, and resets one that failed fatally and
/// fails the commands of one that reads all ones.
#[test]
fn a_dead_controller_is_found_at_the_command_timeout() {
let _clock = crate::test_clock::virtualize();
let wait = |disk: &mut Namespace| {
let clock = disk.controller.borrow().clock;
let started = clock.try_start().unwrap();
while clock.try_elapsed_us(started).unwrap() < 1000 {
core::hint::spin_loop();
}
};
let mut fixture = Fixture::new();
let disk = &mut fixture.disk;
let read = disk.start(Command::Read { lba: 0, bytes: 512 }).unwrap();
disk.controller
.borrow()
.registers
.write32(STATUS, FATAL)
.unwrap();
assert_eq!(disk.finish(read), None);
wait(disk);
assert_eq!(disk.finish(read), None);
{
let controller = disk.controller.borrow();
assert!(
controller.aborts.is_empty(),
"no abort to a failed controller"
);
assert!(controller.recovery.is_some(), "reset at the first deadline");
}
let mut fixture = Fixture::new();
let disk = &mut fixture.disk;
let read = disk.start(Command::Read { lba: 0, bytes: 512 }).unwrap();
disk.controller
.borrow()
.registers
.write32(STATUS, REMOVED)
.unwrap();
assert_eq!(disk.finish(read), None);
wait(disk);
assert_eq!(disk.finish(read), Some(Err(removed())));
}