From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796376543; b=Y1yMGE9mkL5dflH1Tcf+Ta6Pg+iSEBvsU6u4jEPHs2+W1FI7Yo6F7W9TN2e6loKsemRv hrCdefmDXbVgqVlaSbgcTJ2sVfPkb9ktmLQzGJy+EKtg1eb/YrnDPjZNF/JNkm103nlvB 7NB2IbFMO8GISl9uzrEG/T7mz3aYMLf0ro4Vrk5Em6/azbAmYy49kGs9wKfmMWaeD5882 QQwPoDEUQFEBM+UmrF56ZUtz2QrOGTAy39VzSCzXJEUodGUoNVRpLkS3ysu/JnK+ZLuYI ddLS1RWKRTueIKBcXSpLYak6tGLURQvNkDtrtngoupzbfQF7chXnEQ+BFJklzdhgpRw== 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=gKLn7iramKrle/hACgOVEYqq4CLhUzex/mTzLp+HmtILlfVUuK84D6jdNbqvj9Fr73OP 4zHfrH2I9OsNMSeEkG39t57DfF3/xCDy6EE8uBYufNA1SPM60ZeJDMWAPebfaq7PnbybB G4rJsaf5lvLjwMQFEKOeD2o7xxb9kBKMcz1FN2l8OEQnyooQuARNv6avb1P1EwTbhOAnb mG+M9O04GJcUNS/zwFSz9Zg8stLGD0VU7kSrnVQ9Hs6aznHAyCqNf95lgoMVZ+N6KU9dW 49pUC2SRSTJESV83Slb4qxQBzaO7A7BJ0cnLy075AIFbfLVDK09FqzPu9Xe7HpYAGKQ== 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.176 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (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 C516E606A32C for ; Fri, 4 Dec 2026 09:29:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C516E606A32C 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-f176.google.com with SMTP id 41be03b00d2f7-cde827b7b95so378168a12.0 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=hVxsIVwMQ9wTRf71kCj2GL1bOYrIG9Gvzr3FrXy5/Lb8KmUDdU8fn8pcX/mJQTuPtF TNdSXpTrJQuFPW7w8etvxOth0JyaYGcVKhSa5TsGSpa8wcPUlEy71W65spvailcpfA7D 7+jwetOIiiGOgmUvGS5/2budWkb2M/Emn75BHh1sI5lnycc/snlqHqCG7Wz6gWzvY3ZI Wi3p7Bmj4k9cVrx0AKL7P46YiLxNijQsa1YpFUU0YtstPGk+QYWMEE4r5jM9ezDcIQSK jFNUz0MZAe3SAJeNFf01+O+J67em4fSnEAVBslKHEk4KR1vyJ5EJpVE8rrghb/61gmxs KLrA== X-Forwarded-Encrypted: i=1; AL+hErG/CGPfJ6HWFJ7/c47FDzGJAySZ4HmeGW7pbSRs1k3uoZxEF4wD/x2qeWIa8Rw+6wyhVHXUuPI=@vger.kernel.org X-Gm-Message-State: APuU4Z8NWqjrcN5E7o0eLeAZLhjQYsK9fOwOAYk3b1MK2IYzaeP1+cvZ 5zOC66ujHgAzAYj8l1BZIOe2lU0DZdSPSijLz2WsC55WRmwguSZ+JleX X-Gm-Gg: AZFdTZf7c76hk0dPrynB0wr9Ivt89DCICI6kfGaRB46DTsL1/23/2HcAN0vsViXwjIt 6DTqSHOWP73pR3W85GB5fDeo2W9KIXLLOVFTpIvoUU2DMuuI1AVs7JS+RUV/WKmdGYA918G7J1Q x4XL4j8+daqEGdsrFGPQgc/+oeUJRHtqNimI9mieiWDPyX3E3LbRbXBbbUZCQehNbFFIDWPWwwT iR53m6+/6JC+Y6XV1yIFaPO8/tNbgvDN67QHBuBZXAK+j9g3TEMv7enD6KvyYqTNV5lPCCFz5L8 syfaefmnDV0Z3cERHRV8pIO1guAs+k0BrRd9np06fhRz3x1ut344CQmc9yvejG1UC25PWooahPb 8zqhLCLVCv6joyz7JQPamV4K6327elR8mXxWFrmwlaG+0/TAV0U4eXcWpFhNdCjXCub7dRPqdfI 9xSAzvP8ddc/eufkgUHNqirsGTrlEZC5BLdQjEuKRd7xsc5t2Asdmj9KnKNIfvsgHiFRvmMdV70 nSWtCOrnJoWLiVps5DYU+jJLrOCyf3HX24ATclxlHakb5Kc1jp7qalS6tIl62Vx+8EVoc8xw4eZ xdSrCoesPLI6FAtGvGi63qimKligHrInizFHhg== 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: 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 --- 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