Instant documents inner in 0..=KTIME_MAX, but Add/Sub only asserted non-negativity under CONFIG_RUST_OVERFLOW_CHECKS. With checks disabled, a negative result can be returned, and an addition past KTIME_MAX silently wraps. A later Instant-Instant subtraction relies on the invariant and may no longer be overflow-safe. Keep the existing behaviour under CONFIG_RUST_OVERFLOW_CHECKS and panic if the result is out of range. Without overflow checks, use saturating arithmetic and clamp into 0..=KTIME_MAX, printing a warning once for both a negative result and a saturation at KTIME_MAX, so the invariant is preserved without crashing the kernel. Document the panic and clamping conditions in the operators' rustdoc. Fixes: 22b65a40574e ("rust: time: Implement Add/Sub for Instant") Cc: stable@vger.kernel.org Reported-by: Georgios Androutsopoulos Closes: https://github.com/Rust-for-Linux/linux/issues/1272 Signed-off-by: Franklin-Qi --- Changes in v3: - Rebased onto timekeeping-next (Instant is now Instant). - Without overflow checks, also warn once when saturating to KTIME_MAX, not only when clamping a negative result (Tomonori). - Update the rustdoc to match. v2: https://lore.kernel.org/rust-for-linux/20261202080227.898512-1-yusq77@gmail.com/ rust/kernel/time.rs | 101 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 20 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 0782fdb06bfc..4ff65c74b5b5 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -256,6 +256,51 @@ pub fn elapsed(&self) -> Delta { pub(crate) fn as_nanos(&self) -> i64 { self.inner } + + /// Create an [`Instant`] from the result of an arithmetic operation. + /// + /// With overflow checks disabled, `sat` is the saturating result and `wrap` is the wrapping + /// result of the same operation. A mismatch means the `i64` range was exceeded; a negative + /// `sat` means the result fell below 0. Both cases print a warning once and clamp into + /// `0..=KTIME_MAX` (`KTIME_MAX` equals `i64::MAX`). + /// + /// # Panics + /// + /// Panics if `CONFIG_RUST_OVERFLOW_CHECKS` is enabled and `sat` is negative. + #[inline] + fn from_arithmetic(sat: bindings::ktime_t, wrap: bindings::ktime_t) -> Self { + #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] + { + let _ = wrap; + // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0. + // An overflow above `KTIME_MAX` already panicked in the caller. + assert!(sat >= 0); + + Self { + inner: sat, + _c: PhantomData, + } + } + + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + { + // INVARIANT: Without overflow checks, clamp into `0..=KTIME_MAX` and warn once when + // the raw operation would have left that range. + let inner = if sat != wrap || sat < 0 { + crate::pr_warn_once!( + "Instant arithmetic produced an out-of-range value, clamping\n" + ); + if sat < 0 { 0 } else { sat } + } else { + sat + }; + + Self { + inner, + _c: PhantomData, + } + } + } } impl ops::Sub for Instant { @@ -273,40 +318,56 @@ fn sub(self, other: Instant) -> Delta { impl ops::Add for Instant { type Output = Self; + /// # Panics + /// + /// Panics if `CONFIG_RUST_OVERFLOW_CHECKS` is enabled and the result is negative or greater + /// than `KTIME_MAX`. + /// + /// Otherwise, the result is clamped to the range from 0 to `KTIME_MAX` and a warning is + /// printed once. #[inline] fn add(self, rhs: Delta) -> Self::Output { - // INVARIANT: With arithmetic over/underflow checks enabled, this will panic if we overflow - // (e.g. go above `KTIME_MAX`) - let res = self.inner + rhs.value; - - // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0 #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] - assert!(res >= 0); + // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`). + let (sat, wrap) = { + let res = self.inner + rhs.value; + (res, res) + }; + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + let (sat, wrap) = ( + self.inner.saturating_add(rhs.value), + self.inner.wrapping_add(rhs.value), + ); - Self { - inner: res, - _c: PhantomData, - } + Self::from_arithmetic(sat, wrap) } } impl ops::Sub for Instant { type Output = Self; + /// # Panics + /// + /// Panics if `CONFIG_RUST_OVERFLOW_CHECKS` is enabled and the result is negative or greater + /// than `KTIME_MAX`. + /// + /// Otherwise, the result is clamped to the range from 0 to `KTIME_MAX` and a warning is + /// printed once. #[inline] fn sub(self, rhs: Delta) -> Self::Output { - // INVARIANT: With arithmetic over/underflow checks enabled, this will panic if we overflow - // (e.g. go above `KTIME_MAX`) - let res = self.inner - rhs.value; - - // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0 #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] - assert!(res >= 0); + // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`). + let (sat, wrap) = { + let res = self.inner - rhs.value; + (res, res) + }; + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + let (sat, wrap) = ( + self.inner.saturating_sub(rhs.value), + self.inner.wrapping_sub(rhs.value), + ); - Self { - inner: res, - _c: PhantomData, - } + Self::from_arithmetic(sat, wrap) } } -- 2.55.0 Add KUnit coverage for Instant + Delta and Instant - Delta after the invariant-preserving arithmetic change. Test an in-range add/sub path, and the clamping paths used when CONFIG_RUST_OVERFLOW_CHECKS is disabled: a result below 0 clamps to 0, and a result above KTIME_MAX (i64::MAX) saturates at KTIME_MAX. The out-of-range panic paths under overflow checks are left for later should_panic support. Suggested-by: Andreas Hindborg Suggested-by: Miguel Ojeda Signed-off-by: Franklin-Qi --- rust/kernel/time.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 4ff65c74b5b5..5a251eb47bfe 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -1010,4 +1010,56 @@ fn to_jiffies_timeout_saturates() { let negative = Delta::from_nanos(i64::MIN).to_jiffies_timeout(); assert_eq!(negative.as_jiffies(), 0); } + + /// Build an [`Instant`] with a chosen nanosecond value for boundary tests. + /// + /// `ns` must be in `0..=KTIME_MAX` (`i64::MAX`). + fn instant_at(ns: i64) -> Instant { + Instant { + inner: ns, + _c: PhantomData, + } + } + + #[test] + fn instant_add_sub_delta_in_range() { + let t = instant_at(NSEC_PER_SEC); + + assert_eq!((t + Delta::from_secs(1)).as_nanos(), 2 * NSEC_PER_SEC); + assert_eq!((t - Delta::from_secs(1)).as_nanos(), 0); + } + + // Clamping only applies when overflow checks are disabled; with them enabled the + // out-of-range cases panic instead (covered manually / once `should_panic` lands). + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + #[test] + fn instant_add_clamps_above_ktime_max() { + let t = instant_at(i64::MAX); + + assert_eq!((t + Delta::from_nanos(1)).as_nanos(), i64::MAX); + } + + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + #[test] + fn instant_sub_clamps_below_zero() { + let t = instant_at(0); + + assert_eq!((t - Delta::from_nanos(1)).as_nanos(), 0); + } + + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + #[test] + fn instant_add_negative_delta_clamps_below_zero() { + let t = instant_at(5); + + assert_eq!((t + Delta::from_nanos(-10)).as_nanos(), 0); + } + + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + #[test] + fn instant_sub_negative_delta_clamps_above_ktime_max() { + let t = instant_at(i64::MAX); + + assert_eq!((t - Delta::from_nanos(-1)).as_nanos(), i64::MAX); + } } -- 2.55.0