Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796289714; b=2b7xOsXstgnYfC/zXl8syOaX1DhatDelcgwVzhKJSAPJ0f4JzLFlju4wpDQQvwD5+M8Z N2tNeyYyEOsSaqSpnOJ/7UjOy9t6eBs9u2XbKFXWH7H8EMV9SO5xgagNatP0lxg+6u9sC s117WL5hKV4x/iL0qhlJkimpE2qy47FxlZfHK/IYSGk93WKNn8V/Scdk2Ay0yxbY5dLST GooINR0DauxUSz6mNYBKHWJXYRUqWHkNTVIf33hpFU5LByhk+U/fYU6BEsUCzzPpFUIen tI1bm6oYng8gQprR6o46Cve6Ot80mxOaz3/5YOWR197/V9Z5vkX6PwocsoBnMYTlpng== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796289714; 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=UcbpfaHDmL+GZn5rWmXoC/4lErDdjaFFPx6sIinXudEeqqr8KWg1q7ZGPJhHlMY7HgoM kLmMq0ZhX5JpNf9MVHeSlqQj9+sfzzJpOxqoZX9tHgJ/pu3Q20X7DpTL1IFWfZ0vBXY+t 9im+rOV/SosVZk+XWjqarrIBE73IUsfnIIZbJuY7kD/ZwsGxCMnUJh5j+OBJBVnrSBtaL nTzLYKkvgzqp4aEkbACA0EwPS3CQpubT+EFEzfMn9PqBrt49ThTWUMXGmtkDXiXBxwcZf qn8uHForXJZUfMZ5m8f1wmodPM8zMBn1HFx/9gn9q6U1T/hXuVw5GMKfcVy1Ppb6S7Q== 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.173 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.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 2109D606A33A for ; Thu, 3 Dec 2026 09:21:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2109D606A33A 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-f173.google.com with SMTP id 41be03b00d2f7-cc4aa18f9afso5880985a12.3 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=VCM4IjwC1poDsGitMr+HiRnQQzjzN5QTZSX6ze3Hb1hhuUMHUpGmThFAHBMUmRZFDr Zopnt9f6OZ+++KXhIeMxK3NgKaGZCe/ut0hCJ+Cex/sdPfLQY2g7H68H/BFd47IOu3I7 BrpjZoz0uK3nsZqBIpavLgtPynZgmD/G/eyk94WXlH5f592wQdy0bq1mDSO9ljiV5M3/ nDB/tkYM/MBe1BtGKIDuKRnoeDqQSGT5OHIL3Txx5YNeIx8nN3DQTrbU2Z2dswvxuADU dqIVqcjP84zxRAs/GkF5oZD52bYW7mBVBgGgy940EY2kYMlP6R+Diai73FrTdsEzqsJR bSWQ== X-Forwarded-Encrypted: i=1; AL+hErG6ng6T3Z1wkl9dKUn3IConjW+jF9y48vD0XX43dRgCZo8EapsmnjQF3PBVLuoB+ZALqAz8YqW6wKcb4wrFWQ==@vger.kernel.org X-Gm-Message-State: APuU4Z/Ii2MDZVQDxinWLJkOyI7GfUxPWW9bhjnJV0VFUgpg4LruVPer 9hZhmD+/MC1kMugI9w/RJ3kyXJKdNj8/AumvyAqe4aHArIQEHzZwTmZL X-Gm-Gg: AZFdTZeXroHB8TFsU953kMCAkaXYZoRDZjVVCqoZxpdQxrv1EX6Mxc9OmRTD/WxfPOL z+zs+2OV+ufyO3QlRtahopELkBJmhedqo12Xc1MAE5iVXfRzOwpDBd44PfqCm4C6TQtmiNb5Dj2 6t8W/LpHM9dGfUMoPOdg4Pb2insr+zh0hy/SWBMih24netxKpl9vOdwv7k/y9XAQhOGm4/DrdvN D0Hs6Z6+G1/Ctlvto7COj3nmiHrIhgQ/BpG8Z0VbabBeUIxDX3s9tm/fN+2qx9EEfD5gvFWdeu6 FOsd9+U+wBBYfv561KmIF+6FoHaI5c/wGa+KPJwYuPLH4StzyuehatJeywwPWyz+PxDEh1ILVD3 h8lvk8m2FHVEScvLalBylh240o3b/5oGRk7Yb0Wf1spLA48SzCTw/jq9IopoiMXtYK2s0xMeOvu ancncA/rBn4c4HuRnlMyBu1IVWo9yVMR3uonvkHhtzPjexpNt0xohBCJFQhl9EwmdZR6euG+Pp2 c5QPcggFpqErCa7p8AwT+xagPDj9mu4elZyUmuMUtYqXQfRjU7HwywvgRWL7jwsc28Hr+AjOeLF B4xd6O0VIM/c8QSw1ZGgrL6oaoAixvSJj8hXAgC2lN2k411FXswNbg== 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: 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-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