Linux kernel -stable discussions
 help / color / mirror / Atom feed
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>,
	FUJITA Tomonori <tomo@flapping.org>,
	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 v3 2/2] rust: time: add KUnit tests for Instant Delta clamping
Date: Thu,  3 Dec 2026 17:21:01 +0800	[thread overview]
Message-ID: <20261203092101.63850-3-yusq77@gmail.com> (raw)
In-Reply-To: <20261203092101.63850-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.

Suggested-by: Andreas Hindborg <a.hindborg@kernel.org>
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Franklin-Qi <yusq77@gmail.com>
---
 rust/kernel/time.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 4ff65c74b5b5..5a251eb47bfe 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -1010,4 +1010,56 @@ 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> {
+        Instant {
+            inner: ns,
+            _c: PhantomData,
+        }
+    }
+
+    #[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


      parent reply	other threads:[~2026-12-03  9:21 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
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   ` Franklin-Qi [this message]

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=20261203092101.63850-3-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 \
    --cc=tomo@flapping.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