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 v2] rust: time: keep Instant invariant when adding/subtracting Delta
Date: Wed, 02 Dec 2026 20:27:02 +0900 (JST) [thread overview]
Message-ID: <20261202.202702.1454196968303324239.tomo@flapping.org> (raw)
In-Reply-To: <20261202080227.898512-1-yusq77@gmail.com>
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.
next prev parent reply other threads:[~2026-12-02 11:27 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=20261202.202702.1454196968303324239.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