From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796289696; b=WNGU19nA2dK1xP4D+LzkAVpbP9EYcbbkAv/3qJRMU0gm/1ac2DLaXPz2axEiXiXoSYrp iDyhJCv4AuUvXZCVP2Njo0oBf7IoP7KOS3knAhnWRMeQhV4e+9guF+uV/6yECRlLFPfC3 U4TxH0c9lbhVx6fvgiVUx1owMGs0LFU3Ne+ucoxKXFRv3KUfiQZBQeBDpYmwKc6FVifm+ 9z738fLiHoXvzeGIhPYUerU16iMr+4YBDTwdGztNYuI6XeYhfiSdaUG8Ea2XINiQHy3MJ lb97m+HdH+qjjzL8la2ykUsobHTrYViavmqVy0TBrGui27zU1zzxwKXZ3OgPH1BnqIA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796289696; 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=SgDQyNP2lSVXS02hE6ezxBhfstfCIz2DUVZrUrtblGY=; b=qbOFdGkk3mpUpf10LFiWj9Ev9enPaB1uCKs/7LUfnSS+wOFtfY2QXOWB9FViaMZSwlFw m/nLSdxD8SPJSnZxRxf5BW9Wg4uni7WOmD3neSoMZvFdbf6L9BvbYG969wK/qCsna0q1R G//N96MtoQZD+me53E7sjvpY0Ub78bB7W81UQz7LCnhwlhl9Ua1k6A9SKfkHq8Ef2nW4b xMchjVGNC3srM48ZLfHKRLNCaf0gotswcWW98c0OhNxIWj4NCg3C0duvr76q045ILNBdf iglJa2lYGuS2j85RTm9eFCkvnas8bSbXnV/N8iGFZVrRZiJxMVx8yveCAZbq86vsBdQ== 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=xsnjgZbH; arc=none smtp.remote-ip=209.85.210.171 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 EA3D5606A332 for ; Thu, 3 Dec 2026 09:21:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EA3D5606A332 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=xsnjgZbH Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-8b3577deeb0so6318594b3a.3 for ; Thu, 03 Dec 2026 01:21:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796289695; x=1796894495; 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=SgDQyNP2lSVXS02hE6ezxBhfstfCIz2DUVZrUrtblGY=; b=xsnjgZbH/eYyQbYkbgAwG8/Zj1GyVP18+lPxWsmkrSUn0WLKnpfIwE3X6HTFvbKIo7 qY/8/xy2L4aZu1khsyjvv/rU5E9CmU2qRSysb1yWMK35LpcA9CZ3TwZvpizD6yuypS+N 9myqjRbAgCEM1GA+3SJpGU+alx79w4V0zdIpIR93dB3VQjY+an6GIB45Csr6XdVrZYrH /LqBlRyiCUfITkWUjRUom61U4/o8nwagkA3TTjyVkLckhwQcAty7nOdDNk1EVsBxarvI 1a9AH8w4BG/eRJ28iyl8mlmIZxzlD5u4kpD7kR3SM19iQS6yGEkg6cDQvz881M6px754 LQ7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796289695; x=1796894495; 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=SgDQyNP2lSVXS02hE6ezxBhfstfCIz2DUVZrUrtblGY=; b=LQx1n25vaQ6x2kbEq/5o1dp6+ZjfL9wXCmUvpJ5zNp25RICKRIcjeieTJBawbE6t8q 53ivkRmNjSSWaF/xYgIO9+Xcnr4BxCWIkfizeYcgbLyml4KHVK1zFMaFGOkEMDQ3jxSI 1zGOFJix1kAbsuW1wKwZLl9gM4EIyyd87yMXhrmlZ+x4+h6xjL8oJSFwSjDc2LbvbiEp NRCbMSaR7H2Zh92D3biZ9IK9inGlq+eZWE6gXPy/mvP2x1g22Ya3IJEBeZ2Os4tf1J8P 8eqU3CZpMJI8Re0UDQiI46HauMHMWmzt0Vxyobr+QUBJW3B0y57qSG7szlvC9gL9l+kM /5jg== X-Forwarded-Encrypted: i=1; AL+hErFmd7QQ9C4Rac6FZbw123gW9MTBcW9IT7JDB9o0zZRVvWEetaApCNJaJkArgODY5vCg8/gxlsLW9aqy8YY5lg==@vger.kernel.org X-Gm-Message-State: APuU4Z9nLqWNm8kIIljM/U6YWAvJDX3W3ALa6teVILG24tYSfbKfWL05 2Gp2NkyFZfC2WCdVgmMpSPyb1LNSplTR2PBxgXdTwk+nXyXdGWLzUk2FmHSs7qXI X-Gm-Gg: AZFdTZfGOpJtPeD0T/zXgQRQsxtUqs8yw865WDA7S4MWMq+C7TRTA1L9Z6QprzHDd+g 7qsLB2k/9sRNSHdKc/se5U7hE71Xv8Wdsztf1fD1zC+PVMPQhSdKTrq8vDclE0zYQpWpou2mHQu LU4Hs8pw2DwH03YGfLN9G5EwJ1tVP8LPHf0JV3zgl/0RdH8EN18uQQ7rfD2lU2cA5+PtDnXmzCc 3NOQKJsbvN8iAtLR9ba13Mwz8j8KOz5IMtxRyqq/1vfD1iVD1c3BLmciqpVuwSyEbtUIUeSMmlO fWzAx/JUIbS2kKlWVhgpQZvEEmmPnJFCUvz7Xd490aHkX7moUuZlYRRwMqb2hPabvA4whHpGz7L U+bYPschFUxAybcIv2DuGNum/8fwbXlr22nRHmlM46rmqoZ2eE8JYvmLJoIa7cEKM6bM+J4hPZy 73sL5SV6hFrT1M0e3rzQy8ewEum+KiHFg6N3ZLRSusTVGepkIUDuEZGgVaXy9Viz2kD/2qT+9xq PwnpmSWYSj3KKeVD5seSV+j9aF5CQ/x2YS8ELLT7YruESghaqhzdQh1cuS1y/ul+AYbn+ga4fiM X2BIjLUp6F+cwdmsG5cJvceGnbsgSq7B X-Received: by 2002:a05:6a20:2448:b0:3eb:a890:5f8a with SMTP id adf61e73a8af0-4014e35c58fmr45991408637.86.1796289694834; Thu, 03 Dec 2026 01:21:34 -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.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 01:21:34 -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 1/2] rust: time: keep Instant invariant when adding/subtracting Delta Date: Thu, 3 Dec 2026 17:21:00 +0800 Message-ID: <20261203092101.63850-2-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 Instant documents inner in 0..=KTIME_MAX, but Add/Sub only asserted non-negativity under CONFIG_RUST_OVERFLOW_CHECKS. With checks disabled, a negative result can be returned, and an addition past KTIME_MAX silently wraps. A later Instant-Instant subtraction relies on the invariant and may no longer be overflow-safe. Keep the existing behaviour under CONFIG_RUST_OVERFLOW_CHECKS and panic if the result is out of range. Without overflow checks, use saturating arithmetic and clamp into 0..=KTIME_MAX, printing a warning once for both a negative result and a saturation at KTIME_MAX, so the invariant is preserved without crashing the kernel. Document the panic and clamping conditions in the operators' rustdoc. Fixes: 22b65a40574e ("rust: time: Implement Add/Sub for Instant") Cc: stable@vger.kernel.org Reported-by: Georgios Androutsopoulos Closes: https://github.com/Rust-for-Linux/linux/issues/1272 Signed-off-by: Franklin-Qi --- Changes in v3: - Rebased onto timekeeping-next (Instant is now Instant). - Without overflow checks, also warn once when saturating to KTIME_MAX, not only when clamping a negative result (Tomonori). - Update the rustdoc to match. v2: https://lore.kernel.org/rust-for-linux/20261202080227.898512-1-yusq77@gmail.com/ rust/kernel/time.rs | 101 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 20 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 0782fdb06bfc..4ff65c74b5b5 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -256,6 +256,51 @@ pub fn elapsed(&self) -> Delta { pub(crate) fn as_nanos(&self) -> i64 { self.inner } + + /// Create an [`Instant`] from the result of an arithmetic operation. + /// + /// With overflow checks disabled, `sat` is the saturating result and `wrap` is the wrapping + /// result of the same operation. A mismatch means the `i64` range was exceeded; a negative + /// `sat` means the result fell below 0. Both cases print a warning once and clamp into + /// `0..=KTIME_MAX` (`KTIME_MAX` equals `i64::MAX`). + /// + /// # Panics + /// + /// Panics if `CONFIG_RUST_OVERFLOW_CHECKS` is enabled and `sat` is negative. + #[inline] + fn from_arithmetic(sat: bindings::ktime_t, wrap: bindings::ktime_t) -> Self { + #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] + { + let _ = wrap; + // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0. + // An overflow above `KTIME_MAX` already panicked in the caller. + assert!(sat >= 0); + + Self { + inner: sat, + _c: PhantomData, + } + } + + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + { + // INVARIANT: Without overflow checks, clamp into `0..=KTIME_MAX` and warn once when + // the raw operation would have left that range. + let inner = if sat != wrap || sat < 0 { + crate::pr_warn_once!( + "Instant arithmetic produced an out-of-range value, clamping\n" + ); + if sat < 0 { 0 } else { sat } + } else { + sat + }; + + Self { + inner, + _c: PhantomData, + } + } + } } impl ops::Sub for Instant { @@ -273,40 +318,56 @@ fn sub(self, other: Instant) -> Delta { impl ops::Add for Instant { type Output = Self; + /// # Panics + /// + /// Panics if `CONFIG_RUST_OVERFLOW_CHECKS` is enabled and the result is negative or greater + /// than `KTIME_MAX`. + /// + /// Otherwise, the result is clamped to the range from 0 to `KTIME_MAX` and a warning is + /// printed once. #[inline] fn add(self, rhs: Delta) -> Self::Output { - // INVARIANT: With arithmetic over/underflow checks enabled, this will panic if we overflow - // (e.g. go above `KTIME_MAX`) - let res = self.inner + rhs.value; - - // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0 #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] - assert!(res >= 0); + // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`). + let (sat, wrap) = { + let res = self.inner + rhs.value; + (res, res) + }; + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + let (sat, wrap) = ( + self.inner.saturating_add(rhs.value), + self.inner.wrapping_add(rhs.value), + ); - Self { - inner: res, - _c: PhantomData, - } + Self::from_arithmetic(sat, wrap) } } impl ops::Sub for Instant { type Output = Self; + /// # Panics + /// + /// Panics if `CONFIG_RUST_OVERFLOW_CHECKS` is enabled and the result is negative or greater + /// than `KTIME_MAX`. + /// + /// Otherwise, the result is clamped to the range from 0 to `KTIME_MAX` and a warning is + /// printed once. #[inline] fn sub(self, rhs: Delta) -> Self::Output { - // INVARIANT: With arithmetic over/underflow checks enabled, this will panic if we overflow - // (e.g. go above `KTIME_MAX`) - let res = self.inner - rhs.value; - - // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0 #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] - assert!(res >= 0); + // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`). + let (sat, wrap) = { + let res = self.inner - rhs.value; + (res, res) + }; + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + let (sat, wrap) = ( + self.inner.saturating_sub(rhs.value), + self.inner.wrapping_sub(rhs.value), + ); - Self { - inner: res, - _c: PhantomData, - } + Self::from_arithmetic(sat, wrap) } } -- 2.55.0