* [PATCH v2] rust: time: keep Instant invariant when adding/subtracting Delta
@ 2026-12-02 8:02 Franklin-Qi
2026-12-02 11:27 ` FUJITA Tomonori
2026-12-03 9:20 ` [PATCH v3 0/2] " Franklin-Qi
0 siblings, 2 replies; 6+ messages in thread
From: Franklin-Qi @ 2026-12-02 8:02 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, Franklin-Qi
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 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, saturate the
arithmetic and clamp a negative result to 0, printing a warning once, 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<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>
---
Changes in v2:
- Follow Andreas' and Miguel's review: panic only with CONFIG_RUST_OVERFLOW_CHECKS (as before);
without it, saturate the arithmetic, clamp a negative result to 0 and pr_warn_once() instead of
crashing the kernel.
- Without overflow checks, an addition past KTIME_MAX now saturates to KTIME_MAX instead of
wrapping to a negative value (which would otherwise have been clamped to 0).
- Update the rustdoc `# Panics` sections and the commit message to match.
- Tested in QEMU with a small test module (x86_64, KASAN): with CONFIG_RUST_OVERFLOW_CHECKS=n,
the out-of-range cases are clamped/saturated and in-range arithmetic is unchanged; with
CONFIG_RUST_OVERFLOW_CHECKS=y, the out-of-range cases panic as before. Without this patch and
with checks disabled, the same cases produce an out-of-range Instant.
v1: https://lore.kernel.org/rust-for-linux/179610727734.137620.3884652165779521586@gmail.com/
rust/kernel/time.rs | 77 +++++++++++++++++++++++++++++++++------------
1 file changed, 57 insertions(+), 20 deletions(-)
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index b6a2aab12a50..c344bfbaecdf 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -241,6 +241,39 @@ pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
_c: PhantomData,
}
}
+
+ /// Create an [`Instant`] from the result of an arithmetic operation.
+ ///
+ /// `res` must already have been computed without wrapping, i.e. an overflow above
+ /// `KTIME_MAX` must have panicked or saturated to `KTIME_MAX`.
+ ///
+ /// # Panics
+ ///
+ /// Panics if `CONFIG_RUST_OVERFLOW_CHECKS` is enabled and `res` is negative.
+ ///
+ /// Otherwise, a negative `res` is clamped to 0 and a warning is printed once.
+ #[inline]
+ fn from_arithmetic(res: bindings::ktime_t) -> Self {
+ // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0.
+ #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
+ assert!(res >= 0);
+
+ // INVARIANT: Without overflow checks, a negative value is clamped to 0.
+ #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))]
+ let res = if res < 0 {
+ crate::pr_warn_once!("Instant arithmetic produced a negative value, clamping to 0\n");
+ 0
+ } else {
+ res
+ };
+
+ // INVARIANT: By the above, `res` is in the range from 0 to `KTIME_MAX`: the upper bound
+ // holds since `res` is an `i64` that did not wrap.
+ Self {
+ inner: res,
+ _c: PhantomData,
+ }
+ }
}
impl<C: ClockId> ops::Sub for Instant<C> {
@@ -258,40 +291,44 @@ fn sub(self, other: Instant<C>) -> Delta {
impl<T: ClockId> ops::Add<Delta> for Instant<T> {
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
+ // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`).
#[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
- assert!(res >= 0);
+ let res = self.inner + rhs.value;
+ #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))]
+ let res = self.inner.saturating_add(rhs.value);
- Self {
- inner: res,
- _c: PhantomData,
- }
+ Self::from_arithmetic(res)
}
}
impl<T: ClockId> ops::Sub<Delta> for Instant<T> {
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
+ // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`).
#[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
- assert!(res >= 0);
+ let res = self.inner - rhs.value;
+ #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))]
+ let res = self.inner.saturating_sub(rhs.value);
- Self {
- inner: res,
- _c: PhantomData,
- }
+ Self::from_arithmetic(res)
}
}
base-commit: 82d8023ec40420ed61d7a96a173ef862741f1753
--
2.55.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v2] rust: time: keep Instant invariant when adding/subtracting Delta
2026-12-02 8:02 [PATCH v2] rust: time: keep Instant invariant when adding/subtracting Delta Franklin-Qi
@ 2026-12-02 11:27 ` FUJITA Tomonori
2026-12-03 9:20 ` Franklin-Qi
2026-12-03 9:20 ` [PATCH v3 0/2] " Franklin-Qi
1 sibling, 1 reply; 6+ messages in thread
From: FUJITA Tomonori @ 2026-12-02 11:27 UTC (permalink / raw)
To: yusq77
Cc: a.hindborg, lyude, ojeda, aliceryhl, fujita.tomonori, boqun, dakr,
rust-for-linux, tglx, jstultz, sboyd, frederic, anna-maria,
georgeandrout13, stable
On Wed, 2 Dec 2026 16:02:27 +0800
Franklin-Qi <yusq77@gmail.com> wrote:
> impl<T: ClockId> ops::Add<Delta> for Instant<T> {
> 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
> + // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`).
> #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
> - assert!(res >= 0);
> + let res = self.inner + rhs.value;
> + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))]
> + let res = self.inner.saturating_add(rhs.value);
The doc says a warning is printed when the result is clamped, but
saturating_add() and saturating_sub() saturate to KTIME_MAX without a
warning. Only a negative result prints the warning.
With overflow checks, both cases panic. So without overflow checks, I
think both cases should print the warning.
> base-commit: 82d8023ec40420ed61d7a96a173ef862741f1753
This conflicts with timekeeping-next. Instant has changed there.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v2] rust: time: keep Instant invariant when adding/subtracting Delta
2026-12-02 11:27 ` FUJITA Tomonori
@ 2026-12-03 9:20 ` Franklin-Qi
0 siblings, 0 replies; 6+ messages in thread
From: Franklin-Qi @ 2026-12-03 9:20 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: Andreas Hindborg, Lyude Paul, Miguel Ojeda, Alice Ryhl,
FUJITA Tomonori, Boqun Feng, Danilo Krummrich, rust-for-linux,
Thomas Gleixner, John Stultz, Stephen Boyd, Frederic Weisbecker,
Anna-Maria Behnsen, Georgios Androutsopoulos, stable, Franklin-Qi
Hi Tomonori,
Thanks for the review.
> The doc says a warning is printed when the result is clamped, but
> saturating_add() and saturating_sub() saturate to KTIME_MAX without a
> warning. Only a negative result prints the warning.
>
> With overflow checks, both cases panic. So without overflow checks, I
> think both cases should print the warning.
Agreed. In v3, without CONFIG_RUST_OVERFLOW_CHECKS, both a negative result
and a saturation at KTIME_MAX print the warning once (by comparing the
saturating and wrapping results before clamping into 0..=KTIME_MAX).
> This conflicts with timekeeping-next. Instant has changed there.
v3 is rebased onto timekeeping-next and updated for Instant<C, Nsec>.
I am also sending a second patch with KUnit tests for the clamping paths,
as Andreas and Miguel suggested. I verified rust_kernel_time with
CONFIG_RUST_OVERFLOW_CHECKS=n; all Instant clamp tests passed.
Best regards,
Franklin
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 0/2] rust: time: keep Instant invariant when adding/subtracting Delta
2026-12-02 8:02 [PATCH v2] rust: time: keep Instant invariant when adding/subtracting Delta Franklin-Qi
2026-12-02 11:27 ` FUJITA Tomonori
@ 2026-12-03 9:20 ` Franklin-Qi
2026-12-03 9:21 ` [PATCH v3 1/2] " Franklin-Qi
2026-12-03 9:21 ` [PATCH v3 2/2] rust: time: add KUnit tests for Instant Delta clamping Franklin-Qi
1 sibling, 2 replies; 6+ messages in thread
From: Franklin-Qi @ 2026-12-03 9:20 UTC (permalink / raw)
To: Andreas Hindborg, Lyude Paul, Miguel Ojeda, Alice Ryhl
Cc: FUJITA Tomonori, FUJITA Tomonori, Boqun Feng, Danilo Krummrich,
rust-for-linux, Thomas Gleixner, John Stultz, Stephen Boyd,
Frederic Weisbecker, Anna-Maria Behnsen, Georgios Androutsopoulos,
stable, Franklin-Qi
Instant documents that its inner value stays in 0..=KTIME_MAX, but
Add/Sub<Delta> only enforced the lower bound under
CONFIG_RUST_OVERFLOW_CHECKS. With checks disabled the result could leave
that range, which later Instant-Instant subtraction assumes still holds.
This series keeps the existing panic behaviour with overflow checks
enabled, and otherwise saturates/clamps into range with a one-shot
warning. A follow-up patch adds KUnit coverage for the clamping paths.
Changes in v3:
- Rebased onto timekeeping-next (Instant is now Instant<C, Nsec>).
- Without overflow checks, also warn once when saturating to KTIME_MAX,
not only when clamping a negative result (Tomonori).
- Add a second patch with KUnit tests for Instant +/- Delta clamping
(Andreas, Miguel).
v2: https://lore.kernel.org/rust-for-linux/20261202080227.898512-1-yusq77@gmail.com/
Franklin-Qi (2):
rust: time: keep Instant invariant when adding/subtracting Delta
rust: time: add KUnit tests for Instant Delta clamping
rust/kernel/time.rs | 153 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 133 insertions(+), 20 deletions(-)
base-commit: 8ecc26b396a2b587088e8ffd4d4b77d878f1eb33
--
2.55.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 1/2] rust: time: keep Instant invariant when adding/subtracting Delta
2026-12-03 9:20 ` [PATCH v3 0/2] " Franklin-Qi
@ 2026-12-03 9:21 ` Franklin-Qi
2026-12-03 9:21 ` [PATCH v3 2/2] rust: time: add KUnit tests for Instant Delta clamping Franklin-Qi
1 sibling, 0 replies; 6+ messages in thread
From: Franklin-Qi @ 2026-12-03 9:21 UTC (permalink / raw)
To: Andreas Hindborg, Lyude Paul, Miguel Ojeda, Alice Ryhl
Cc: FUJITA Tomonori, FUJITA Tomonori, Boqun Feng, Danilo Krummrich,
rust-for-linux, Thomas Gleixner, John Stultz, Stephen Boyd,
Frederic Weisbecker, Anna-Maria Behnsen, Georgios Androutsopoulos,
stable, Franklin-Qi
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 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<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>
---
Changes in v3:
- Rebased onto timekeeping-next (Instant is now Instant<C, Nsec>).
- 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<C: ClockId> ops::Sub for Instant<C, Nsec> {
@@ -273,40 +318,56 @@ fn sub(self, other: Instant<C>) -> Delta {
impl<T: ClockId> ops::Add<Delta> for Instant<T, Nsec> {
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<T: ClockId> ops::Sub<Delta> for Instant<T, Nsec> {
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
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 2/2] rust: time: add KUnit tests for Instant Delta clamping
2026-12-03 9:20 ` [PATCH v3 0/2] " Franklin-Qi
2026-12-03 9:21 ` [PATCH v3 1/2] " Franklin-Qi
@ 2026-12-03 9:21 ` Franklin-Qi
1 sibling, 0 replies; 6+ messages in thread
From: Franklin-Qi @ 2026-12-03 9:21 UTC (permalink / raw)
To: Andreas Hindborg, Lyude Paul, Miguel Ojeda, Alice Ryhl
Cc: FUJITA Tomonori, FUJITA Tomonori, Boqun Feng, Danilo Krummrich,
rust-for-linux, Thomas Gleixner, John Stultz, Stephen Boyd,
Frederic Weisbecker, Anna-Maria Behnsen, Georgios Androutsopoulos,
stable, Franklin-Qi
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 <a.hindborg@kernel.org>
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Franklin-Qi <yusq77@gmail.com>
---
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<Monotonic> {
+ 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
^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-03 9:21 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 8:02 [PATCH v2] rust: time: keep Instant invariant when adding/subtracting Delta Franklin-Qi
2026-12-02 11:27 ` FUJITA Tomonori
2026-12-03 9:20 ` Franklin-Qi
2026-12-03 9:20 ` [PATCH v3 0/2] " Franklin-Qi
2026-12-03 9:21 ` [PATCH v3 1/2] " Franklin-Qi
2026-12-03 9:21 ` [PATCH v3 2/2] rust: time: add KUnit tests for Instant Delta clamping Franklin-Qi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox