From: Franklin-Qi <yusq77@gmail.com>
To: Andreas Hindborg <a.hindborg@kernel.org>,
Lyude Paul <lyude@redhat.com>, Miguel Ojeda <ojeda@kernel.org>,
Alice Ryhl <aliceryhl@google.com>
Cc: FUJITA Tomonori <fujita.tomonori@gmail.com>,
Boqun Feng <boqun@kernel.org>, Danilo Krummrich <dakr@kernel.org>,
rust-for-linux@vger.kernel.org, Thomas Gleixner <tglx@kernel.org>,
John Stultz <jstultz@google.com>, Stephen Boyd <sboyd@kernel.org>,
Frederic Weisbecker <frederic@kernel.org>,
Anna-Maria Behnsen <anna-maria@linutronix.de>,
Georgios Androutsopoulos <georgeandrout13@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta
Date: Tue, 01 Dec 2026 14:41:17 +0800 [thread overview]
Message-ID: <179610727734.137620.3884652165779521586@gmail.com> (raw)
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
reply other threads:[~2026-12-01 6:41 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179610727734.137620.3884652165779521586@gmail.com \
--to=yusq77@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=anna-maria@linutronix.de \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=georgeandrout13@gmail.com \
--cc=jstultz@google.com \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=stable@vger.kernel.org \
--cc=tglx@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox