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, Franklin-Qi <yusq77@gmail.com>
Subject: [PATCH v4 2/2] rust: time: add KUnit tests for Instant Delta clamping
Date: Fri, 4 Dec 2026 17:29:10 +0800 [thread overview]
Message-ID: <20261204092910.253781-1-yusq77@gmail.com> (raw)
In-Reply-To: <20261204083408.229840-1-yusq77@gmail.com>
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.
Build the Instant fixtures through Instant::from_ktime() so the tests
use the same debug_assert!-checked constructor as the rest of the crate,
instead of writing the private fields directly. Restore that constructor
for the tests, and static_assert that ktime_t::MAX matches i64::MAX
(KTIME_MAX).
Suggested-by: Andreas Hindborg <a.hindborg@kernel.org>
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Franklin-Qi <yusq77@gmail.com>
---
Changes in v4:
- Use Instant::from_ktime() in the KUnit helper (Miguel).
- Restore from_ktime() with debug_assert! and
static_assert!(bindings::ktime_t::MAX == i64::MAX). bindings::KTIME_MAX
is not currently exported; this asserts the same upper bound.
- expect(dead_code) on from_ktime() when CONFIG_RUST_TIME_KUNIT_TEST=n.
Changes in v3:
- Add KUnit tests for in-range Instant±Delta and the nochecks clamping
paths (Andreas, Miguel).
v1: https://lore.kernel.org/rust-for-linux/179610727734.137620.3884652165779521586@gmail.com/
v2: https://lore.kernel.org/rust-for-linux/20261202080227.898512-1-yusq77@gmail.com/
v3: https://lore.kernel.org/rust-for-linux/20261203092101.63850-3-yusq77@gmail.com/
rust/kernel/time.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 4ff65c74b5b5..a9b781753d9e 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -257,6 +257,31 @@ pub(crate) fn as_nanos(&self) -> i64 {
self.inner
}
+ /// Create an [`Instant`] from a `ktime_t` without checking if it is non-negative.
+ ///
+ /// # Panics
+ ///
+ /// On debug builds, this function will panic if `ktime` is not in the range from 0 to
+ /// `KTIME_MAX`.
+ ///
+ /// # Safety
+ ///
+ /// The caller promises that `ktime` is in the range from 0 to `KTIME_MAX`.
+ #[cfg_attr(not(CONFIG_RUST_TIME_KUNIT_TEST), expect(dead_code))]
+ #[inline]
+ pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
+ // Instant's upper bound is `KTIME_MAX`, which equals `i64::MAX` / `ktime_t::MAX`.
+ crate::static_assert!(bindings::ktime_t::MAX == i64::MAX);
+ debug_assert!(ktime >= 0);
+
+ // INVARIANT: Our safety contract ensures that `ktime` is in the range from 0 to
+ // `KTIME_MAX`.
+ Self {
+ inner: ktime,
+ _c: PhantomData,
+ }
+ }
+
/// Create an [`Instant`] from the result of an arithmetic operation.
///
/// With overflow checks disabled, `sat` is the saturating result and `wrap` is the wrapping
@@ -1010,4 +1035,54 @@ 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> {
+ // SAFETY: `ns` is in `0..=KTIME_MAX` by the helper contract above.
+ unsafe { Instant::from_ktime(ns) }
+ }
+
+ #[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
next prev parent reply other threads:[~2026-12-04 9:29 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
2026-12-04 9:29 ` Franklin-Qi [this message]
2026-12-04 10:38 ` [PATCH v4 2/2] rust: time: add KUnit tests for Instant Delta clamping 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=20261204092910.253781-1-yusq77@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.