From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796198587; b=l11LFRCWe544mBwjp3DfSLhC518788Fe+kflKiSRbpDVq07vdopTrxE5Jn0/h9hjHkaw HZVD9apsUkrSddMcXBgkx1AlY2vrwbL5uc3HQnDPKVrv/e1NQ+ewN2G8yMeQ7P3PPJD+V TruZID//itkbZXtSD0TQ4v14Nwkv7OAHkUphceh3KAiKWY6aYBQjTEisLhYEceoPrmivS q+VTE1NbWwwJNAv+ya6tcAr9nkXh7cBRDbKO6v33OMWGhO7lTWLJpXVSdS+vt5lcSRL3N z4sDkdSoC6MN5bhwsL3E1oVLSZJFrXxkYt5+XR4hdwDx7uNVXduAxZpu7Dr2pzW2gBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796198587; 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:MIME-Version: Content-Transfer-Encoding; bh=vlQv37CaWaNnfP63U7NEcGcOE3fKLEIG6Ey9ypDGEy0=; b=2Vo8KAS1quKHq4F13uwhKIH+Mkyo5BiV6jfne8oYJqY19jE32nlEArg/8VWJ06x8cVI8 KQuzHwOEgkc6K1Ou3KMNGb4C++iYycenQgjaRXw5/eZApXTvr/iANtMamauPZQ8UV0QOd GLl/KFRKvX0HzaRZUQL6WTOjSc9EEcqXyoeruNgYQ8MH5mYCgTTZnj9o9T2xHi31PsGZB 9CqiGoy2YJE8BvGO4++1nj7Rxqux7Vc1Q7mVKiW2StcY1+cvA+gyD2PeRSUH4C/1PsaSV rKjVxdcUbvxU/WLd4+9X9ZQ8qtvnbG4hEwN0wRfxTi8+EkUsnx90di3C7vIGbxft7qg== 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=y0HMQd9W; arc=none smtp.remote-ip=209.85.216.54 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (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 C6DDD606A31A for ; Wed, 2 Dec 2026 08:03:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C6DDD606A31A 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, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=y0HMQd9W Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-3b402824d07so8095762a91.1 for ; Wed, 02 Dec 2026 00:03:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796198586; x=1796803386; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=vlQv37CaWaNnfP63U7NEcGcOE3fKLEIG6Ey9ypDGEy0=; b=y0HMQd9Wq37nxKqUPuIfdQNWcZzoeb4kLMPfjFS9m2Y9xwupNxCQtaqlqhsky9nKbl L4RAE1F/X1D9lhFvU11+Vl58eKPn7gWhzVogqwoUyUNum42+tcJrqKC0Egc9q3GAw8aK t98e+sslPOuR81W9sCU+T4FAnCQZsDESrdnNSE2TSDAOp/8RsWcURa31ALXndk6eeYeX 1iDvjk5H15mtfPhbORqbkAwtXYIMJdb+rqCAn89lQBOCw4g/gHyJNc1W7+m7Eb261/kM iVV5ZENVBe3u4uhKwnpOZTjomy7gDDEbWe91cOobvXU4gHHJ2bNvWFW3mEWpa34U810C mvSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796198586; x=1796803386; h=content-transfer-encoding:mime-version: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=vlQv37CaWaNnfP63U7NEcGcOE3fKLEIG6Ey9ypDGEy0=; b=Vx2i36RnPEdWjatq4NcJHTWDjEZmgsMNy015PK70X9tLq49kX9QUt/xXIi6MwBTvvT /KV9CVAjHHw4uS1q9+vicS1kETSZ024liHZwztSfK43OqRT0lo2wiuBywciILWaucOOl baq1kREhuGuNEPQc8IWOpWXbxcpENWA/y92FwYOs3ByTqPKAmAExiKqkfckFptta7X3l Q83lZGJQYM/Axc9rh+jLmLC+jUukaU+jwn7FBRnIMrqCBmRlnNlDyITgyAGT2KUYzymv N098et9R5xvA4v0TquDWQnr+0najlRufQ0q/Z1+xSfyaVgdWEMoRkZzDlbNQdIjXj3XK qvGA== X-Forwarded-Encrypted: i=1; AL+hErEhgzRhb0sM4zTlBOB4ph6wJtIzrY4ccFqF1C04ozvPt4HJn3LAqt6ibcJYIZXrRA88qRLp/yuJGvI+I8ahLw==@vger.kernel.org X-Gm-Message-State: APuU4Z8ZNpKliTLXiFP1+9REpsNN6uPBExjdixXNc09NSWVZzoWPWLKv 2N34M92v4gdJ2BqNF3hnFRObf4x0jWb/LfsfGzQWmC5Or++fWjuuJ5fR X-Gm-Gg: AZFdTZdlR33fwLGOJAIqyx1ysulx93JYmMX8k0/zF0GQyd8faa9Lf+wKb7S83YW/sf1 7snqIgtFQDXMj4YsHqMrPSj+JliUi98HNMPagJemDY7xjz3nHMXPsJF1O5StyhL1k3HCLeNHR3y uEk5g/mPKvgOnUXvliro6pLiLkopnIvdJCwCuL7YDd2JH3kHXKTANLMdknnseN19Bzbwa88l166 Ew50u8yeNBwQGiu/5E2Tf8DqDzF/aVDipmsTdc0zYi8sLit/6T6x1oUhmyvfE75fsa4bjKOAIXO IMz5LJUmNF4wXOXIASOXF01BbsC7TbeYeXvCZmhCmz9RNy1OmeNpspskWCKN2tMZuKNBYOB29vG FYWz7NwiTSMJBnzKYGg80J+Gu8IHXkhYUQ3zRt/vmYrVx8IrfUCV8SapijRN9MiZB+9pydujgGX fuJ1uAGURX/kZht9zi9lxVzlZSV2Q5UPSZu4vmPk/52+l4fkNrgLmL0ICtmXPwXmyP9GTfef+iR uOuyx3XU6/02EaRZYQk6xTaX1ST+0ybiey2wXUwXTKiqHlQY9k7wZVUCDG7gXq8yXIc7uBDp9Xq TYTXD8xkk39vfzwwrPX47O9IMdNdtEkktH0sE0xUdxyke9AmfQFMbncdWjc= X-Received: by 2002:a17:90b:4983:b0:3b0:97b8:d6b7 with SMTP id 98e67ed59e1d1-3b41c931d29mr41262270a91.42.1796198585704; Wed, 02 Dec 2026 00:03:05 -0800 (PST) Received: from ysq-QiTianM428-A376 ([111.201.209.250]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b41ad0a33fsm62403104a91.6.2026.12.02.00.02.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 00:03:05 -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 v2] rust: time: keep Instant invariant when adding/subtracting Delta Date: Wed, 2 Dec 2026 16:02:27 +0800 Message-ID: <20261202080227.898512-1-yusq77@gmail.com> X-Mailer: git-send-email 2.55.0 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, saturate the arithmetic and clamp a negative result to 0, printing a warning once, 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 v2: - Follow Andreas' and Miguel's review: panic only with CONFIG_RUST_OVERFLOW_CHECKS (as before); without it, saturate the arithmetic, clamp a negative result to 0 and pr_warn_once() instead of crashing the kernel. - Without overflow checks, an addition past KTIME_MAX now saturates to KTIME_MAX instead of wrapping to a negative value (which would otherwise have been clamped to 0). - Update the rustdoc `# Panics` sections and the commit message to match. - Tested in QEMU with a small test module (x86_64, KASAN): with CONFIG_RUST_OVERFLOW_CHECKS=n, the out-of-range cases are clamped/saturated and in-range arithmetic is unchanged; with CONFIG_RUST_OVERFLOW_CHECKS=y, the out-of-range cases panic as before. Without this patch and with checks disabled, the same cases produce an out-of-range Instant. v1: https://lore.kernel.org/rust-for-linux/179610727734.137620.3884652165779521586@gmail.com/ rust/kernel/time.rs | 77 +++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index b6a2aab12a50..c344bfbaecdf 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -241,6 +241,39 @@ pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self { _c: PhantomData, } } + + /// Create an [`Instant`] from the result of an arithmetic operation. + /// + /// `res` must already have been computed without wrapping, i.e. an overflow above + /// `KTIME_MAX` must have panicked or saturated to `KTIME_MAX`. + /// + /// # Panics + /// + /// Panics if `CONFIG_RUST_OVERFLOW_CHECKS` is enabled and `res` is negative. + /// + /// Otherwise, a negative `res` is clamped to 0 and a warning is printed once. + #[inline] + fn from_arithmetic(res: bindings::ktime_t) -> Self { + // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0. + #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] + assert!(res >= 0); + + // INVARIANT: Without overflow checks, a negative value is clamped to 0. + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + let res = if res < 0 { + crate::pr_warn_once!("Instant arithmetic produced a negative value, clamping to 0\n"); + 0 + } else { + res + }; + + // INVARIANT: By the above, `res` is in the range from 0 to `KTIME_MAX`: the upper bound + // holds since `res` is an `i64` that did not wrap. + Self { + inner: res, + _c: PhantomData, + } + } } impl ops::Sub for Instant { @@ -258,40 +291,44 @@ 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 + // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`). #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] - assert!(res >= 0); + let res = self.inner + rhs.value; + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + let res = self.inner.saturating_add(rhs.value); - Self { - inner: res, - _c: PhantomData, - } + Self::from_arithmetic(res) } } 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 + // With overflow checks enabled, this panics if we overflow (e.g. go above `KTIME_MAX`). #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] - assert!(res >= 0); + let res = self.inner - rhs.value; + #[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))] + let res = self.inner.saturating_sub(rhs.value); - Self { - inner: res, - _c: PhantomData, - } + Self::from_arithmetic(res) } } base-commit: 82d8023ec40420ed61d7a96a173ef862741f1753 -- 2.55.0