From: FUJITA Tomonori <tomo@flapping.org>
To: yusq77@gmail.com
Cc: a.hindborg@kernel.org, lyude@redhat.com, ojeda@kernel.org,
aliceryhl@google.com, fujita.tomonori@gmail.com,
boqun@kernel.org, dakr@kernel.org,
rust-for-linux@vger.kernel.org, tglx@kernel.org,
jstultz@google.com, sboyd@kernel.org, frederic@kernel.org,
anna-maria@linutronix.de, georgeandrout13@gmail.com,
stable@vger.kernel.org
Subject: Re: [PATCH v4 1/2] rust: time: keep Instant invariant when adding/subtracting Delta
Date: Fri, 04 Dec 2026 19:07:16 +0900 (JST) [thread overview]
Message-ID: <20261204.190716.1830004374014931047.tomo@flapping.org> (raw)
In-Reply-To: <20261204092843.253726-1-yusq77@gmail.com>
On Fri, 4 Dec 2026 17:28:43 +0800
Franklin-Qi <yusq77@gmail.com> wrote:
> @@ -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,
> + }
> + }
> + }
> }
I think sashiko's comment on v3 [1] still applies. checked_add() and
checked_sub() would make this simpler. Something like:
#[inline]
fn from_checked(res: Option<bindings::ktime_t>) -> Self {
// The `Instant` operand is in `0..=KTIME_MAX`, so `None` means
// that the result went above `KTIME_MAX`.
let inner = match res {
Some(res) if res >= 0 => res,
_ if cfg!(CONFIG_RUST_OVERFLOW_CHECKS) => {
panic!("Instant arithmetic produced an out-of-range value")
}
_ => {
crate::pr_warn_once!("Instant arithmetic produced an out-of-range value, clamping\n");
if res.is_none() {
bindings::ktime_t::MAX
} else {
0
}
}
};
// INVARIANT: `inner` is in the range from 0 to `KTIME_MAX`.
Self {
inner,
_c: PhantomData,
}
}
fn add(self, rhs: Delta) -> Self::Output {
Self::from_checked(self.inner.checked_add(rhs.value))
}
fn sub(self, rhs: Delta) -> Self::Output {
Self::from_checked(self.inner.checked_sub(rhs.value))
}
With overflow checks, both cases still panic.
[1] https://sashiko.dev/#/patchset/20261203092101.63850-1-yusq77%40gmail.com
next prev parent reply other threads:[~2026-12-04 10:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 8:34 [PATCH v4 0/2] rust: time: keep Instant invariant when adding/subtracting Delta Franklin-Qi
2026-12-04 9:28 ` [PATCH v4 1/2] " Franklin-Qi
2026-12-04 10:07 ` FUJITA Tomonori [this message]
2026-12-04 9:29 ` [PATCH v4 2/2] rust: time: add KUnit tests for Instant Delta clamping Franklin-Qi
2026-12-04 10:38 ` FUJITA Tomonori
2026-12-05 9:55 ` kernel test robot
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=20261204.190716.1830004374014931047.tomo@flapping.org \
--to=tomo@flapping.org \
--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 \
--cc=yusq77@gmail.com \
/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