From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796376543; b=RmgAlt5tVID4Ld8iik7A/0wjZAyovgGMA83Lc2MaW26NOYzwmcAI7H95Z5xHRnslyy5U FvqmrXVwv5fMmJddYad15EUfyDzJ1y4VSt1mP6ejkX6+u0MDAK5zBiWsQJkkNI0kE5Tzj cq1fe5TiJgMFIjU55CasynCuZ4eNX0If+tmvY69KwtrTScTfxO/3ghtDZ4hTcf1dKVqJM Xv0MWkmGGRy9EigeKVSQghiNUG9zsViKvzb9phAjRNvBFFM9z75MXG+Ne9yZHjJuOGL8y 1+eBVIQk5XbRm/NxJeq7+svwMoHB4D2BMdkDQUQdCGbOzz2Be/VuAGac2JmvbEfB+1Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796376543; 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=slLJYW4Utbke6KcdiTEWi+Hevu6ZKKs1MFXBRIZNyaM=; b=wBsVZo9xCNARpDk2SUZuxJX/Svc9eVKnfS1MskKATSV3fjJNEw7xevLClnyV2tD3O21U FtJciKxMTqFTsiqgW77cB7TSKslwUSGzSFjTudfdJTggH1Tf42d1rwt5Un7sJ2dOJPHn4 yPQqnzeJuafj1R3q7mDSxjD4ATgEScVKXX2SHyInC9DKqPokJem082SOt2phofYGWJYqM AnopsSdEMiTvN18JMfkp7ZlSkS5/ZLRZNAeoRWiCsMhnTnw8l0CknksXCSwN16g5yAUwg KZqKIskKCGXEMADXYJ5QQ23Rlw4S2qKo6nU2jTBVFzx0Hy4atR/VKFlTOvANzTP1gFA== 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=ypIF1HUB; arc=none smtp.remote-ip=209.85.215.171 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.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 CB76B606A32D for ; Fri, 4 Dec 2026 09:29:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CB76B606A32D 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=ypIF1HUB Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-ccca948203aso475936a12.3 for ; Fri, 04 Dec 2026 01:29:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796376542; x=1796981342; 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=slLJYW4Utbke6KcdiTEWi+Hevu6ZKKs1MFXBRIZNyaM=; b=ypIF1HUBGhlynbr/9LbuMfn4WALEAPbXOxJDMqIRsKQSZbFIJEObiCAvIn++xD6M6i Vfn7Vv3hQ3PwxHNVgbfP40jO9yzcSvBYCiHAZyP+7/TdCNn9brmgNovQqPWW9yA85i0N f+9vU567eu9/ft7jX+/O2/NymxA9CMLr0CDi5cvTRrhMiO44pyXKMkkR0p5W5hyo5I3v uRR3PF7VhCopXZfPMraHtgH1V/JifdjTIwc8tgL3A7d8Fcj909XBzv7RWIbVd2Aq6mlD +5fzVN32R3uZCvwUPZ8LWh/6DC7VMMONvF+mFIHqxDl0MfLUX11L8uAC2Ere3T1OIlX8 ucEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796376542; x=1796981342; 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=slLJYW4Utbke6KcdiTEWi+Hevu6ZKKs1MFXBRIZNyaM=; b=LkA7jEUwM2bQbS4zzG7mtBmeWvNFCC/0Of/idGRfk9Q8oZRXbD2rpXNkqjhJCks8IS 5RzhUcFLbNrxqFjxf/hwKHFcMyFDD9PbbfVo+AkbdgwK2SIeF/Mt21KGZFIsWFziy6SB qd15EaFp3XCT2i21wuLuEK6kvbBGDjEjh9mXvuYq1mZOUWmIHkbJy4lzDd0l/dvbwtYV TsFTYptLb5zDHhoRgnOaKZFuV1cnLDyUw1bgMIkxDxh6M6/Gf2Vmp2BJOG02AtGxWB9a PjlGpiE8yzFO/Y/iz92W2rkSF6WuCWP+tRSpxhoJrJNJNAhtesYICOls318OuSV/nMRn H44g== X-Forwarded-Encrypted: i=1; AL+hErGBJuxcjjSyNC/SqiaEO4jY1uiWfcj5e+1MIDzA5NMKqcDs/jPUx+ljgxNZPMYZZtBYAMOHWVi85rovN1USBw==@vger.kernel.org X-Gm-Message-State: APuU4Z9oydyelW3MWYyWzutFXIrRhyAmSwAHKXbq/9ZMkibOI9ggYN4O xw3PJMpeeVJ5isHMhj851/Muoi1u/bMq/tzIiJ0qIcelMgT3/B1r2XVn X-Gm-Gg: AZFdTZdXUhS3gcmQmJjONkw5oi8XNyt5lyB/XFkjiXm/zz/yaxmObFobAT28Sqz/O6I ygj1JcwObxhcPdEZngQb2T2V6s92+D4K2sEC2fjRbTBvGNjWh7Q/S/qAamz6y9QMwVrNSWJu2El SVNKWa8ugYIpGKMXo1HHo1ECoBoqcNIfHp8sX4PdEEkdf8E0LkP6xxfz0zhxiwoWkp14y+3jCJ9 kA0kdJNpMK7xBzjnBmRbmnFHimFYFxryNl8lKRde49pU2DlSEVQTopKvCIvLMNGAxJAwA+7Dj77 OShYAd27B0hPeR2BpxpeB+jZw97AuKHD/7ZZrHjrALJOqY4RH17ZNgs004xVQ4OL1UNjHfC2Xrw u6mVi5+HJujhWdULERH3KAZZl2zpuynpzlVp0b3cP+bQfhZuqfonXb2WQH1jnQHVzY4to5HN3i+ e0AINFha86RjwG6FM/yMOiOMGzwuwkSvxEfQy6pl2oHqlyK7cCa6/KxEjXTUyRJd7o+vntwOki5 ZtcB8CnRrKvohYL1OUjQrvGxxqalt+hN2MAB2S/d2SVtea2+0w/Xqc9OB6c1dYU5Psd8GT9gzLQ fAPRhkURObFzKXdWhId8TP7/4K4Qj3o8jZugTg== X-Received: by 2002:a05:6a20:431a:b0:3fb:a69:848c with SMTP id adf61e73a8af0-41562919195mr1546882637.63.1796376541696; Fri, 04 Dec 2026 01:29:01 -0800 (PST) Received: from ysq-QiTianM428-A376 ([111.201.209.250]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ce1234649eesm1002421a12.4.2026.12.04.01.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:29:01 -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 1/2] rust: time: keep Instant invariant when adding/subtracting Delta Date: Fri, 4 Dec 2026 17:28:43 +0800 Message-ID: <20261204092843.253726-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-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 --- 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