* [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta
@ 2026-12-01 6:41 ` Franklin-Qi
2026-12-01 10:07 ` Andreas Hindborg
0 siblings, 1 reply; 4+ messages 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] 4+ messages in thread
* Re: [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta
2026-12-01 6:41 ` [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta Franklin-Qi
@ 2026-12-01 10:07 ` Andreas Hindborg
2026-12-01 10:52 ` Miguel Ojeda
2026-12-02 8:03 ` Franklin-Qi
0 siblings, 2 replies; 4+ messages in thread
From: Andreas Hindborg @ 2026-12-01 10:07 UTC (permalink / raw)
To: Franklin-Qi, 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" <yusq77@gmail.com> writes:
> 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.
I agree that allowing the invariant to be broken is wrong, but could you
elaborate pros/cons wrt. clamping instead of panicking here?
I am more in favor of clamp + warn_once, since it does not crash the
kernel.
We could also consider panic if overflow checks enabled and clamp + warn
if not. But this may be too confusing?
Best regards,
Andreas Hindborg
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta
2026-12-01 10:07 ` Andreas Hindborg
@ 2026-12-01 10:52 ` Miguel Ojeda
2026-12-02 8:03 ` Franklin-Qi
1 sibling, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2026-12-01 10:52 UTC (permalink / raw)
To: Andreas Hindborg
Cc: Franklin-Qi, 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
On Tue, Dec 1, 2026 at 11:36 AM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> I agree that allowing the invariant to be broken is wrong, but could you
> elaborate pros/cons wrt. clamping instead of panicking here?
+1, the key here is that discussion, which started in the issue.
> We could also consider panic if overflow checks enabled and clamp + warn
> if not. But this may be too confusing?
I think that also makes sense -- it is essentially what we do with EB
but under `OVERFLOW_CHECKS` instead of the other one.
That assumes we are OK with panicking here under a default
configuration, but that is already the case before this patch...
Cheers,
Miguel
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta
2026-12-01 10:07 ` Andreas Hindborg
2026-12-01 10:52 ` Miguel Ojeda
@ 2026-12-02 8:03 ` Franklin-Qi
1 sibling, 0 replies; 4+ messages in thread
From: Franklin-Qi @ 2026-12-02 8:03 UTC (permalink / raw)
To: Andreas Hindborg
Cc: Miguel Ojeda, Lyude Paul, Miguel Ojeda, Alice Ryhl,
FUJITA Tomonori, Boqun Feng, Danilo Krummrich, rust-for-linux
Hi Andreas, Miguel,
Thanks for the review.
On Tue, Dec 1, 2026 at 11:07 AM Andreas Hindborg wrote:
> could you elaborate pros/cons wrt. clamping instead of panicking here?
Unconditional panic keeps the invariant simple to reason about, but it
turns an out-of-range Delta into a kernel crash even in configurations
where plain integer arithmetic would not panic. Clamping + warn_once
keeps the kernel running and the invariant intact, at the cost of
silently changing the value (hence the warning).
> We could also consider panic if overflow checks enabled and clamp +
> warn if not. But this may be too confusing?
I agree with Miguel that this hybrid is consistent with the existing
behaviour (the panic under CONFIG_RUST_OVERFLOW_CHECKS is not new), so
I went with it in v2, which I'm sending in a separate thread shortly:
- CONFIG_RUST_OVERFLOW_CHECKS=y: panic when the result overflows or is
negative, as before.
- Otherwise: saturating_add()/saturating_sub(), a negative result is
clamped to 0 with a pr_warn_once!(), and a result past KTIME_MAX
saturates to KTIME_MAX (instead of wrapping and then being clamped to
0).
- The panic/clamp conditions are documented in the rustdoc.
I also checked it in QEMU with a small test module: with overflow
checks disabled the out-of-range cases are clamped/saturated and
in-range arithmetic is unchanged (without the patch the same cases
yield an out-of-range Instant); with overflow checks enabled they panic
as before.
Best regards,
Franklin
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 8:03 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <Mf9TNvNPTgERimoWnx-HXvSDdGaI4X1aH9iSG6qeVNn70w9dd_u0rKrIaSqNi8YN3O__uOmCDl8AWxrgV65JEg==@protonmail.internalid>
2026-12-01 6:41 ` [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta Franklin-Qi
2026-12-01 10:07 ` Andreas Hindborg
2026-12-01 10:52 ` Miguel Ojeda
2026-12-02 8:03 ` Franklin-Qi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox