From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796376568; b=mQiFb6n3voN5vRd6x3Sw2IOWT9sgAVRmi5I+SCTi1DOyshjXBUmZowCRDM+LIRVdzNuK DY/qYUpgW70CobTDcMLsondEVs91AvCjhi6z6etJT133M7KQGULEO2r3TtsLkYCvE6tS6 +PU99yp9ayhDbPICk/72QpG/2eNdzx0qOrK2ecXBh6qRCeICe3qNhJe+Zlq7N0r+GLXiS oGz1X40cXo7SEPyE5w1HkI5RvtajbEum9fKdhxFZCfl4e0gAFz5WDbPpdmBZuB9pKwrni 7RhAVoi9vG1IefCLgiconc9EmFTtAD6FZ3xK/U9DNHzgg48PJzij0j2ux+75M57TuUw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796376568; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=76bMF5d9SgU1uF3Ghv/JWWVnIcM26DnqliTe8oN+EeE=; b=zFzQlarB2Cc8qoePPI5N35yecYnMQ5dpJMbz4IJaziJApAQZ2cg023/S6M7tSwKxZtkH v8ijgdvJjch7RhxwuStLuZB7VE4TAwO/Qrj140TV8mWC8qRWLUCmlOP5uk5E+Unwf24e9 nv+w6Uc2LlW3P87crgEbXSj9dFz/XF6653nsuou6TEPtsq9+jLoCGzJvCunalmZkxsOHX /hViTk90uzH/A1KLbNd/7mslvhiTFZs0RTVKfBhV8dyoy2y8hi4o8VFxdTnooL/EFe285 QUYGz6XWjDkYPOngZHsw4dNAToy0PNvdWK4X92cs6/FYpJ//SsRd3BeGQ3DdAhnQn2w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="daTf0/dH"; arc=none smtp.remote-ip=209.85.214.173 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 584F8606A322 for ; Fri, 4 Dec 2026 09:29:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 584F8606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=daTf0/dH Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2f045b921caso5385945ad.3 for ; Fri, 04 Dec 2026 01:29:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796376567; x=1796981367; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=76bMF5d9SgU1uF3Ghv/JWWVnIcM26DnqliTe8oN+EeE=; b=daTf0/dH5+CbVU02WkxObLMucnW6HMw3bmNtFFhoOXMOlhpKnTH/tIjZQ5guQRGroP 7wnJVP71Bm4RZEndc9EhlXJNg2YFuf+uSwF7howl9a6+pbVzfmkWAqw9PxEVXj1uQbcA q2unK1F1uQnVpV6CG8ILoJapBjN7bc13sxasNiKyKE9S10ooTPodVotoNup7XdT3NbYO 696vJ4F35RLor74ug+a2vxEE7NiXCxBNvyPMrMryVBpoLfkwfKtPddfO5cBku9FYfXGS sgTUABLUS7stamnRs/WrTmK8Xg+V5K1zST+nOBiliXHG4ViFrizU/YzNxD6S5mpnw2MO j19Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796376567; x=1796981367; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=76bMF5d9SgU1uF3Ghv/JWWVnIcM26DnqliTe8oN+EeE=; b=d8PDgxRWJig0QW1ywR9jt/C2yBv0iaC3lZeWkwQAbo35bxGlH1mwAYdpry3wvtz98I kIfTNxur/MMo4HaaTc2e7sUDl1t10aI+W0Ux77LZAs1BUDAFRkW5ZNjRnuuxfP6nf/0/ QpqZBfmgiSYXvvmsMVokcW0dHgPGdPgYm7UBReSY+do6DC9Jrr/ADr3z5ib5R2MxKqYm 3W9jdv7hK3qrn5y4DN2+1oClNuutWMgKrgEOofWrQ0l7Jjk5fhZR/Uz2h+o0UyQOejBr Wz1e7p+SRFxNuGIrB87RGGwC0Q+u6x4z+mOpKXyogocffzp5wimkGdBcUPy3oxYa42h+ l2cw== X-Forwarded-Encrypted: i=1; AL+hErHzAfZxFCHUOQCEX7OCySoBLjnZ2kCuzT8ejlkjcC8H6BO7kYeNpTiCXONeaeieSkX6H5fTBrAM9H0DC11TCA==@vger.kernel.org X-Gm-Message-State: APuU4Z+hZ7gnk5G9vyvi1swOmEYf8Z7BJthYl9Wg3yMFLlALgJIPHc92 63GjNEryqzZ4nDrnoGnL3PjLAEJ+J5y6VYHRW6BMvuZbez5pEekuoPVc X-Gm-Gg: AZFdTZccGqR2q8lwtnjFmL1jOwqwvYodfISQZ4lX0CFpl/LM7EvUYqnkxHAGp+pI5GS aR2cgS1iVm39N6Q/C/81Pzxz5d54stMOkGmqyFggA03GOsoG1QRFoRif0S46Vy/vjf+UjFhwNZM wSVdAMu1EjiYUufGydHReCx1/O3PWnXA+EIJUVjiMP2iOtzR1GsDV66/Um0Ljoja9whsymEyF8X gbysm1lh9wZPVbqRpaBcN3rNg44HpY/2xj5Za66MU0C66wz7TV4HjN2yEn0pMMltbbsVVVMmQIg uOxhTik2VXEWH3AoBbDZn93510Oa+difhg0DtAPIDlojjHQTqFRtDZzh70xSA0CbwJT0Q6t5OYV U0w9w+/P7fxV9Vz9TXmaCNBvVen+oosH6ZtDsdCjsIi9q83dAmAMSRcu6fKXfe70O8HsKICsoNr zIGB20CbpwKumw3o+s3bkSb/5epVdVIBR+5dFO/T/3YGgeE7LuVraHKkOi3ZTir/97F2nsYoSOF ++FQM9dj/+rQzyn2qYip/0ntiMn+0Afoha0Di3w+ON+tljTCvlHHcT0niVnd23wXwpqglKZGXjM Kqu6SdD210i3iNi7itLklzQIY5JHyc8p X-Received: by 2002:a17:90b:3909:b0:3b4:12a9:2e1 with SMTP id 98e67ed59e1d1-3b5e06832a8mr1152319a91.33.1796376567286; Fri, 04 Dec 2026 01:29:27 -0800 (PST) Received: from ysq-QiTianM428-A376 ([111.201.209.250]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7843862sm8381955a91.2.2026.12.04.01.29.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:29:26 -0800 (PST) From: Franklin-Qi To: Andreas Hindborg , Lyude Paul , Miguel Ojeda , Alice Ryhl Cc: FUJITA Tomonori , Boqun Feng , Danilo Krummrich , rust-for-linux@vger.kernel.org, Thomas Gleixner , John Stultz , Stephen Boyd , Frederic Weisbecker , Anna-Maria Behnsen , Georgios Androutsopoulos , stable@vger.kernel.org, Franklin-Qi Subject: [PATCH v4 2/2] rust: time: add KUnit tests for Instant Delta clamping Date: Fri, 4 Dec 2026 17:29:10 +0800 Message-ID: <20261204092910.253781-1-yusq77@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261204083408.229840-1-yusq77@gmail.com> References: <20261204083408.229840-1-yusq77@gmail.com> X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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 Suggested-by: Miguel Ojeda Signed-off-by: Franklin-Qi --- 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 { + // 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