Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta
@ 2026-12-01  6:41 Franklin-Qi
  0 siblings, 0 replies; only message in thread
From: Franklin-Qi @ 2026-12-01  6:41 UTC (permalink / raw)
  To: Andreas Hindborg, Lyude Paul, Miguel Ojeda, Alice Ryhl
  Cc: FUJITA Tomonori, Boqun Feng, Danilo Krummrich, rust-for-linux,
	Thomas Gleixner, John Stultz, Stephen Boyd, Frederic Weisbecker,
	Anna-Maria Behnsen, Georgios Androutsopoulos, stable

Instant documents inner in 0..=KTIME_MAX, but Add/Sub<Delta> only
asserted non-negativity under CONFIG_RUST_OVERFLOW_CHECKS. With checks
disabled, a negative result can be returned and later Instant-Instant
subtraction may no longer be overflow-safe.

Always assert the result is non-negative, and document the panic in the
operators' rustdoc.

Fixes: 22b65a40574e ("rust: time: Implement Add<Delta>/Sub<Delta> for Instant")
Cc: stable@vger.kernel.org
Reported-by: Georgios Androutsopoulos <georgeandrout13@gmail.com>
Closes: https://github.com/Rust-for-Linux/linux/issues/1272
Signed-off-by: Franklin-Qi <yusq77@gmail.com>
---
Note: Per discussion on the GitHub issue with Miguel Ojeda, this uses an
unconditional assert to preserve the Instant invariant (not clamp +
warn_once), and documents the panic condition in the operators' rustdoc.

 rust/kernel/time.rs | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index b6a2aab12a50..f58f55e440af 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -258,14 +258,17 @@ fn sub(self, other: Instant<C>) -> Delta {
 impl<T: ClockId> ops::Add<Delta> for Instant<T> {
     type Output = Self;
 
+    /// # Panics
+    ///
+    /// Panics if the result of the addition is negative.
     #[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`)
+        // (e.g. go above `KTIME_MAX`). The assert below also rejects a wrapped negative result when
+        // overflow checks are disabled.
         let res = self.inner + rhs.value;
 
-        // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0
-        #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
+        // INVARIANT: The result must be non-negative (`0..=KTIME_MAX`).
         assert!(res >= 0);
 
         Self {
@@ -278,14 +281,17 @@ fn add(self, rhs: Delta) -> Self::Output {
 impl<T: ClockId> ops::Sub<Delta> for Instant<T> {
     type Output = Self;
 
+    /// # Panics
+    ///
+    /// Panics if the result of the subtraction is negative.
     #[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`)
+        // (e.g. go above `KTIME_MAX`). The assert below also rejects a wrapped negative result when
+        // overflow checks are disabled.
         let res = self.inner - rhs.value;
 
-        // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0
-        #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
+        // INVARIANT: The result must be non-negative (`0..=KTIME_MAX`).
         assert!(res >= 0);
 
         Self {
-- 
2.55.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-01  6:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  6:41 [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta Franklin-Qi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox