From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796289696; b=ZOG8M+dwsAS78yp8OWhyNqBKg2OQYdXWg8AXjIZ1jhbRs7WhRprZKn3nKRAYDskOE6SI 3eycLES50bDEq/EoIHm9M8lOYsnU4YB2aW4oMdcq3c+Kg0QzbbwLb80U7KCpKOhqknM9j Dr67ZxLLCvQxLyzNlcG8/7RQzpmq1S9lSf2LJe594xuf5cSOTgWCrpLfrLlxeBPMgMMx0 yKoLiIcRsh9Tr+9f3mt68FCOnbj3sQ/zC0yobpxF8aK61+eadS3kYg9zZ/Aj05MysEEDC G5q4oOXyzf6WrOZ0IF8RB+jARR12+ydSEIzhGxPZaONLBaGc1IIT3ht159sJGHloLNw== 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=OtsWhoC8/SvN02h72BsbTiYmWaZwxI2UK+xlSQ0HDDMrX3Us/8w147VjYG48PgN4QpKi EwBXCcvtuDM+A+U0jdJeHdKoQVVb5L8PNudxh5fCpd2X/iclOsfBaEpZ1L6W2aaLog0Xe AvsNgsRaLnvVKWqQdpLauoGUaBwYJZ76rrxmIzoEbvm2CFifvGFfTAgpl195L0+P/xArq XuLnGiOTPf9iV8a3iZRfGaS4M8Hawc5T7JWeYAb5n4ipWz10+Su2nPN+gMKdulk/ESg/S BGKprmupUWUfd+rEyBzH+SHNXIIG8huznyVGY2yhb1ARZmpXJIUCvuE44zhtHnbBHVQ== 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.179 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 E3EA9606A32D for ; Thu, 3 Dec 2026 09:21:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E3EA9606A32D 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-f179.google.com with SMTP id d2e1a72fcca58-8a06e79e152so6844191b3a.0 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=iQltg8CjH7gCAr75V6TS+59rbcXJfcVlsU/sczsG7FfNLPjWq9YeCDp/YikUwg8vek TlJvbgBv3AaX6quLzyNDX/cxO73VjMN1t3RJ1q2dd7mx4m6kbP2HcHbRCQt3V6xeWPHt hD8vp9z3iGbQtf9RaDtozSCMI97RMp38O31fkwc2FHBI7lPj+Tz03vo1/lIejDGSKTNm x7UvQMkWEK4eG56STboC7gatwNTmGfM3E38t02kyy9b2Dy4Qew4AYnTqDvuK16daMChM hnPPXTjF1cm06iYUsehfDLlD2KxBk+hQbdBjCZJTvOT/oWYHP5xbOykUMGNp+rNi/mwL GvfA== X-Forwarded-Encrypted: i=1; AL+hErGaR1mvnjne9AJtaQafwYTXw3FNza/e9oxN73OfSWgl5mfx5Oq6Z6MtMUSfy2gRfoLKG6dBAx8=@vger.kernel.org X-Gm-Message-State: APuU4Z97W/+8n9Tm+utJZn6kkHlcWW4t7JdmrtJkGXQ8NygflpEEFa2l DVDjRMD2T5kAv/D4I1tK00FZykVU5Js0OKLQCN/4BCcWLM4bax0RmpXt X-Gm-Gg: AZFdTZfQ9gBsnsUbBHny8lBlM0sNl8KK6cQHHiL9zhB679y5ddTFPAeCXpDb+834VtC Yo5Y+LNNR/jizIIlqyNMvdxeis6u9EHxla7UErZTvqL90D5h81JQ1dgyqblNkKJ/0WxZY5YTQOp nOE1PbDW8ZgqfMs1aU8Y1A/uSGjNAEFTDGhsZTIdb56+O14gqJWvWx7QGEQJT2YPWSviFCoGf2K kyVp2iCUINOUe3lKgx5a96R0+vUVtVPymR6ntBzbK4Pz+HLS8DwENGlIzvgeD0pn/mn7jvT/Ewm /DKFv+qOQvv1h70AcW0QFCaxmYxQGjtuK3+HB6IPYzq3Gc29TvHDZ628JgRdkGzKOoix4ricfFI fkQpAOHzgIlGEh37ogX7z0+5y6rIFHE6AvclAS7MM4dbn+DArFDD+jWhrDtmbkYjqFCNu8/Qld0 IWy7cNbK9OToKjvF+pmfNBYZZ4yn8l7ylhzARjv92tfXeDhJkhDK7fZSK27BAjs2xNRBHHTK5XZ IK1XPX5cQ3KywtqsGkRRPzGJtEzz887R235+TKGdN6ZJXCDedjtqZzsaDPY7n7mSEW1UIQmEL7u i0JloGPIlIaKQOCqJz6ViLvlO1Ev6uL3 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: 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 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