diff --git a/crates/stela-boot/src/engine/nor.rs b/crates/stela-boot/src/engine/nor.rs index b934d863e8..32ea8905ad 100644 --- a/crates/stela-boot/src/engine/nor.rs +++ b/crates/stela-boot/src/engine/nor.rs @@ -42,6 +42,12 @@ pub fn split(partition: Box) -> (Box, Box, /// Changed sectors waiting for the store in progress, and that store: @@ -113,21 +119,36 @@ impl AnchorStore { } /// Start the waiting anchor's store once the one in progress is done, - /// the caller waiting for both when `wait`. A failed store is reported - /// and ends the anchor's stores for this run: the next boot goes on - /// from the last stored anchor, which with a wall time loses nothing. - fn advance(slot: &mut Option, wait: bool) { + /// the caller waiting for both for at most `wait` counter ticks. A + /// failed store is reported and ends the anchor's stores for this run: + /// the next boot goes on from the last stored anchor, which with a wall + /// time loses nothing. So does a wait past its limit, reported once. + fn advance(slot: &mut Option, wait: Option) { let Some(store) = slot.as_mut() else { return; }; + let started = super::host::fresh_counter(); loop { if let Some(running) = store.running.as_mut() { match running.poll(&mut *store.device) { - None if wait => { - core::hint::spin_loop(); - continue; - } - None => return, + None => match wait { + Some(limit) + if super::host::fresh_counter().wrapping_sub(started) < limit => + { + core::hint::spin_loop(); + continue; + } + Some(limit) => { + output::arguments(format_args!( + "RTC ANCHOR refusal: store at stop timed out after {} ms; the \ + next boot goes on from the last stored anchor\n", + limit / (super::host::COUNTER_HZ / 1000) + )); + *slot = None; + return; + } + None => return, + }, Some(Err(error)) => { output::arguments(format_args!( "RTC ANCHOR refusal: store: {error}; no more anchors this run\n" @@ -221,14 +242,37 @@ impl HostNor { /// guest believes committed must not be lost silently. pub fn store(&mut self, dirty: Vec<(usize, Vec)>) { self.stores.add(dirty); - self.advance(false); - AnchorStore::advance(&mut self.anchor, false); + self.advance(); + AnchorStore::advance(&mut self.anchor, None); } - /// Complete every store, the caller waiting: at the stop. + /// Complete every store, the caller waiting at most [`NOR_STOP_MILLIS`] + /// and [`ANCHOR_STOP_MILLIS`]: at the stop. A disk that never completes + /// is reported once and the restart or power-off goes on; the sectors + /// it did not store are lost, as the line says. pub fn finish(&mut self) { - self.advance(true); - AnchorStore::advance(&mut self.anchor, true); + let limit = super::host::COUNTER_HZ / 1000 * NOR_STOP_MILLIS; + match self.stores.finish( + &mut *self.device, + super::host::fresh_counter, + limit, + core::hint::spin_loop, + ) { + Ok(persist::Finish::Done) => {} + Ok(persist::Finish::TimedOut { waited, sectors }) => { + output::arguments(format_args!( + "NOR refusal: store at stop timed out after {} ms; {sectors} sectors not stored\n", + waited / (super::host::COUNTER_HZ / 1000) + )); + } + Err(error) => { + output::fatal(format_args!("a NOR sector could not be stored: {error}")); + } + } + AnchorStore::advance( + &mut self.anchor, + Some(super::host::COUNTER_HZ / 1000 * ANCHOR_STOP_MILLIS), + ); } /// The PMU upcount's anchor this boot starts from: `None` on a new @@ -243,18 +287,13 @@ impl HostNor { if let Some(store) = self.anchor.as_mut() { store.waiting = Some(anchor); } - AnchorStore::advance(&mut self.anchor, false); + AnchorStore::advance(&mut self.anchor, None); } - fn advance(&mut self, wait: bool) { - loop { - match self.stores.advance(&mut *self.device) { - Ok(false) if wait => core::hint::spin_loop(), - Ok(_) => return, - Err(error) => { - output::fatal(format_args!("a NOR sector could not be stored: {error}")); - } - } + /// Move the stores on without waiting. + fn advance(&mut self) { + if let Err(error) = self.stores.advance(&mut *self.device) { + output::fatal(format_args!("a NOR sector could not be stored: {error}")); } } } diff --git a/crates/stela-boot/src/output.rs b/crates/stela-boot/src/output.rs index a8d7e8c441..d5f853163d 100644 --- a/crates/stela-boot/src/output.rs +++ b/crates/stela-boot/src/output.rs @@ -466,16 +466,20 @@ struct Sink { impl Sink { /// Move the store in progress on: collect the running request's result - /// and start the next; with `wait`, until the store is done. - fn advance(&mut self, wait: bool) -> Result<(), BlockError> { + /// and start the next; with a counter deadline `wait`, until the store + /// is done or the deadline passed. Whether it is done: a device that + /// never completes leaves the store in progress at the deadline. + fn advance(&mut self, wait: Option) -> Result { loop { if let Some(request) = self.request.as_mut() { let Some(finished) = request.poll(&mut *self.device) else { - if !wait { - return Ok(()); + match wait { + Some(deadline) if !passed(deadline) => { + core::hint::spin_loop(); + continue; + } + _ => return Ok(false), } - core::hint::spin_loop(); - continue; }; self.request = None; self.writer.completed(finished)?; @@ -486,7 +490,7 @@ impl Sink { self.stores += 1; self.ticks += self.flushed.wrapping_sub(began); } - return Ok(()); + return Ok(true); }; match Submission::new(request, self.device.identity()) { Ok(request) => self.request = Some(request), @@ -499,6 +503,21 @@ impl Sink { } } +/// The longest a store that waits for the logs (at a stop, or the USB's log +/// before a device starts) waits for their disks: a disk that never +/// completes a command must not keep the PC at the stop. On expiry it is +/// reported once and the stop goes on. +const STORE_WAIT_MILLIS: u64 = 2000; + +/// The TSC rate taken before it is measured, for [`STORE_WAIT_MILLIS`]: no +/// PC's is higher, so the wait is at least that long. +const UNMEASURED_TSC_HZ: u64 = 6_000_000_000; + +/// Whether the counter passed `deadline`. +fn passed(deadline: u64) -> bool { + rdtsc().wrapping_sub(deadline) < u64::MAX / 2 +} + /// Seconds between the machine state's log stores while the engine runs: /// ext4's default journal commit interval. A stop stores at once. const MACHINE_FLUSH_SECONDS: u64 = 5; @@ -858,6 +877,16 @@ fn flush_where(due: impl Fn(&Sink) -> bool, wait: bool) { return; } let mut failed = Vec::new(); + let mut late = Vec::new(); + // One deadline for every log's store. + let started = rdtsc(); + let deadline = wait.then(|| { + let hz = match TSC_HZ.load(Ordering::Relaxed) { + 0 => UNMEASURED_TSC_HZ, + hz => hz, + }; + started.wrapping_add(hz / 1000 * STORE_WAIT_MILLIS) + }); let reached = with_sinks(false, |sinks| { // A log whose store is in progress takes new bytes at its next one. let is_due = |sink: &Sink| !sink.writer.storing() && due(sink); @@ -889,8 +918,13 @@ fn flush_where(due: impl Fn(&Sink) -> bool, wait: bool) { if is_due(sink) && sink.writer.begin_store() { sink.began = Some(rdtsc()); } - match sink.advance(wait) { - Ok(()) => index += 1, + match sink.advance(deadline) { + Ok(done) => { + if wait && !done { + late.push(sink.kind); + } + index += 1; + } Err(error) => failed.push((sinks.remove(index).kind, error)), } } @@ -900,11 +934,23 @@ fn flush_where(due: impl Fn(&Sink) -> bool, wait: bool) { ); }); if wait { - UNSTORED.store(reached.is_none(), Ordering::Relaxed); + UNSTORED.store(reached.is_none() || !late.is_empty(), Ordering::Relaxed); } for (kind, error) in failed { log_lost(kind, error); } + // Reported once a boot: its store stays in progress and goes on at the + // next turn, if any. + static LATE: AtomicBool = AtomicBool::new(false); + if let Some(kind) = late.first() + && !LATE.swap(true, Ordering::Relaxed) + { + arguments(format_args!( + "BOOT LOG refusal: the {} log's store timed out after {} ms; going on without it\n", + kind.name(), + millis_since(started) + )); + } } /// The counter now, for [`millis_since`]. diff --git a/crates/stela-soc/src/nor/persist.rs b/crates/stela-soc/src/nor/persist.rs index 1363b6d98c..ddf0020bdc 100644 --- a/crates/stela-soc/src/nor/persist.rs +++ b/crates/stela-soc/src/nor/persist.rs @@ -184,6 +184,43 @@ impl Stores { self.running = Some((store, count)); } } + + /// Complete every store, the caller waiting at most `limit` ticks of + /// `now`: at the stop, which must not hang on a device that never + /// completes a command. `relax` runs between polls. + pub fn finish( + &mut self, + device: &mut dyn BlockDevice, + mut now: impl FnMut() -> u64, + limit: u64, + mut relax: impl FnMut(), + ) -> Result { + let started = now(); + loop { + if self.advance(device)? { + return Ok(Finish::Done); + } + let waited = now().wrapping_sub(started); + if waited >= limit { + let running = self.running.as_ref().map_or(0, |(_, count)| *count); + return Ok(Finish::TimedOut { + waited, + sectors: running + self.waiting.len(), + }); + } + relax(); + } + } +} + +/// How waiting for the NOR's stores ended ([`Stores::finish`]). +#[derive(Debug, PartialEq, Eq)] +pub enum Finish { + /// Every store is durable. + Done, + /// `limit` ticks passed with stores left: these many sectors waited or + /// ran still. + TimedOut { waited: u64, sectors: usize }, } /// Blocks per sector and the block size; the partition must hold the image @@ -228,6 +265,8 @@ mod tests { next: u32, pick: Pick, completed: usize, + /// It completes every running write as soon as asked. + instant: bool, } impl Adversary { @@ -243,6 +282,7 @@ mod tests { next: 0, pick, completed: 0, + instant: false, } } @@ -295,6 +335,9 @@ mod tests { Ok(tag) } fn finish(&mut self, tag: u32) -> Option { + if self.instant { + while self.complete() {} + } self.done.remove(&tag) } } @@ -387,6 +430,39 @@ mod tests { assert!(!disk.valid_bank()); } + #[test] + fn the_stores_at_the_stop_end_at_their_deadline_on_a_disk_that_never_completes() { + let (before, dirty) = two_saves(); + let mut disk = Adversary::new(before.clone(), oldest); + let mut stores = Stores::new(); + stores.add(dirty.clone()); + let clock = core::cell::Cell::new(0u64); + let finished = stores + .finish( + &mut disk, + || clock.get(), + 100, + || clock.set(clock.get() + 7), + ) + .unwrap(); + // The first store's sectors run and the second's wait. + assert_eq!( + finished, + Finish::TimedOut { + waited: 105, + sectors: dirty.len() + } + ); + // A disk that completes lets it finish before the deadline. + let mut disk = Adversary::new(before, oldest); + disk.instant = true; + let mut stores = Stores::new(); + stores.add(dirty); + let finished = stores.finish(&mut disk, || 0, 100, || {}).unwrap(); + assert_eq!(finished, Finish::Done); + assert!(disk.valid_bank()); + } + #[test] fn the_bank_a_store_began_goes_on_before_the_other() { let base = ANVRAM.0 as usize;