From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796289713; b=IGrwe0sfh48mg+46zG2jI3B+0ktihZMsoohzOdz93haah73nD62h1iyZ5bnP/qBQEu8Q xwvSPVT59SGiD41Kznld7FN7Q27xmb61osIxnPJsaSWDEUtb3kVSVL0OjBWy2qXaGbprq TK2bBM9fZEuiuUwfQCsY10BHh0tYTRsMwHln9AF3SlvlifxUiQTqVR1kmwSVwZel8YwwZ oL3rAPlHNyxPqKJKgzEeOYnQlH54b2SzqvsHe1vPG86FgNtz04fdA+RAkymcYSHB06Sqt g1XHJSmL1cj1vwazKnXqdK5cFy09ctXlkJmgLnXwzLdaAdGt+O9NssywMOtjuYx9oTw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796289713; 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-Transfer-Encoding; bh=/dG1bc9rGk274wERrqCQ95D71JMrJg2gz8XT/i+PU/k=; b=1Togw6Hea+af/I4sE+zyjAIlTb50rpOW9y/hjqw9tpoTh6GwqmRQHi0t91YAniWSo1yZ wz8zxrWmHWuRapMB4bk3OWWIGNlQIBBI4TV5XJjiLi5Cy3wJlCNH2ypElmGex0YRW1y3k IZ+Y0+SKvu9i9zMUp6W2hGv27jacTM9B3rAbG2qVFdAha+VX7YUKfFwXuKRPTWcUPvjn8 FbaUliBRz6o8AXZAJDigUtknL9FUtJBgVcps8c1k4QNh2nBJmg9LIRFJcGRNejyqGQyyw YG/os7J23Sw+tlOt8yT1eRj5oPxyk2H0MVYLwThW5DSOrE409DxAmGfNMi8Q0n8lcWg== 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=WFjMSRmH; arc=none smtp.remote-ip=209.85.215.174 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (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 EB9A8606A338 for ; Thu, 3 Dec 2026 09:21:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB9A8606A338 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=WFjMSRmH Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-cdb75699c7cso5467996a12.2 for ; Thu, 03 Dec 2026 01:21:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796289712; x=1796894512; darn=vger.kernel.org; h=content-transfer-encoding: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=/dG1bc9rGk274wERrqCQ95D71JMrJg2gz8XT/i+PU/k=; b=WFjMSRmH69BbPs5V0z7RXA4o+RnQiUNApGioAY9J85m/aMvHjjAU4K/cMFoI+wbj63 D59FuJJdPym52F2uEwZOQgY8wjp/mQ7e19W+bWc+ArW2FMoPRZuTSWFzhHuF3tivUMal lk/CFoID4Ea28oQKzGm9ktbxLFBuMtkzffg1iU4ycDSWtuT3EuOirwEz1leMkZZmPefi Yom5rtgU1YvcI1PdzoukqzaPh+jr8/JG7fYfGsBguXhMdtp2rtzWroW+D7n6939L9K7a rf8ZnjtHL2ykVq8ropv7MEcY0ZqZC5BCRxcIF46C7tRzF5XY0lPfu/YO815qjBN98KQ2 GWtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796289712; x=1796894512; h=content-transfer-encoding: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=/dG1bc9rGk274wERrqCQ95D71JMrJg2gz8XT/i+PU/k=; b=Yuzm60/RP2LdHpFs52ZBwOJb0VWfHWAw9F0Ix+0WFYVxnrQMdUNnvh0l9t5W3DIA9n Kli/9aw8yjaTLleqUs/rZTPRA/pdcR6cNYglV0TvhuFJcZRuxTUhSOfjBJGAPqD7QfCC 9kTN8NMbSwtrGIyParPGVNZapcA20CI/jp5Busyy7sfJ+NrytkWwKmfAcPMj3qQukZP6 +kPiHyhhTEhcbWrfvF6GBEPXve4skiKMgNqnj4hlr6Vz/8gvoXOKVnX9GHwVG4tfOcCT TrnirvqMlsohGLvytUSlcYJcxScEZg1NP2RWC1dIndxvaxY57d9VMb7ULh5gxJibxGD7 qYlQ== X-Forwarded-Encrypted: i=1; AL+hErHyflmywHU4UttHiZirQl1dsr29Uc9l7HN4r4gv2Ybu4QXiod4cBTWVlS2XSlsRd28psaxWhk8=@vger.kernel.org X-Gm-Message-State: APuU4Z9DjtuOlIL98sEwUJMOU5osOdZ80XgB9wmKhcOd78ewXMcqZxKX iuO9uUMHpzZIxpwqZKLrGHb6WK43DadfmK4OxzXxz4HsPjgelJxCzd6S X-Gm-Gg: AZFdTZdBkcB9d5u+HAQeG3Si+m/vsWS4u7rvmll9t1SpO5Gm7Y85WFH3TRSOiHKQYB4 vQUj8hIPH4d343eWvsnQX5oaFpNUzJOLbcAuLE5KV22xlrH4Lgkd9vxWUKfEtgpY/+na5W2YajH k5DCTHfBKHeYgCBifoJo9bhnmIbOiRTN8gdEc6gxEhsMC4zHMUTQSBci3mI2rIv222Xj3wMZUdj e3dFUonnrfPYZVRYk6nCZG20bEiJALdpyu09E6pvUTqRCWN8YRnbbON4UJQLyUI/rX7b5/iZbu3 X6b/Rpm4hjVsUSjjm5onwSMpnY0+55cpCoLi6ebKoGIq9Q8dF30IOxDHa2DVK28oOwXyfGXSdE3 FqJD+8HcapZFN2VS92cmO6i25fkNQWZCz1MuGI05Utxj5dxZcq/r/1KorFiWsDXkyJwH5uPxEEt hYZUGXSGqzyxMjjUVGE1QLrRBup50P+4pMTKg85Nt6uI00dTplXUxvqgB4d8MuTaGWDvR9Hw/Gx AUHBoqtKdb6/Qe3QQclHuOoipL+xH0B7KBxMjLtnUWAd4NAnWjceV8ln5rU8d9cvFweimYHDFgY /fKhg4ksELVAMKMCA3r+7KPMrbeDOFnV0CDgcRxaxordThKT13FQOw== X-Received: by 2002:a05:6a20:7489:b0:3fa:dce8:d914 with SMTP id adf61e73a8af0-4014b564548mr47674518637.29.1796289712107; Thu, 03 Dec 2026 01:21:52 -0800 (PST) Received: from ysq-QiTianM428-A376 ([111.201.209.250]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8d329855224sm11606321b3a.38.2026.12.03.01.21.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 01:21:51 -0800 (PST) From: Franklin-Qi To: Andreas Hindborg , Lyude Paul , Miguel Ojeda , Alice Ryhl Cc: FUJITA Tomonori , 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 v3 2/2] rust: time: add KUnit tests for Instant Delta clamping Date: Thu, 3 Dec 2026 17:21:01 +0800 Message-ID: <20261203092101.63850-3-yusq77@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261203092101.63850-1-yusq77@gmail.com> References: <20261202080227.898512-1-yusq77@gmail.com> <20261203092101.63850-1-yusq77@gmail.com> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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. Suggested-by: Andreas Hindborg Suggested-by: Miguel Ojeda Signed-off-by: Franklin-Qi --- 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 { + 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