From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796198587; b=aVxNXtKBkkJMggrfcM7cIkAanDQaxtgdE06A5gHEDESgMzpdc2rK3tECFL9kNNhMGWtQ yklLQAmWo3DfIwwENmVcGbX+lo7LJ8OtnsH5vKwp1uIe2hZ9JbTJiTuVEUDZJB4PNNwqj NlnyWZ/uhR0oU1F98vlME23e2XC8rbdUz2W4H3+01mY0xe8SadqgAsRkmrgB1p9ua1JDg 89+aX5ShnIpIxdQlbjQl1CHWucSNCh1Fox7rjisscWRXtH7IbQ5mtnSfs9exyuYK0Acwj 6CcPUQ25axK++GGE3soLMs6VfviZa5sOuk+sRPF+0xRYThz33GqSa/f+kpQa6O58VAQ== 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=lEYEb/HnSJ8alKGoWe/33HXi7SFLCT76XP5xbXLTKv2Bt53WoJcSUd4JiBpYSC2TI98w C9H+YDBwGEnpSi3PUgF+11hP0bN7Ftxe4/gGAHkeiiUC2Mt8Ofjlr3tiSs3k8npkrLOBT tSaqW20/kSe9P2Omc04PmWhvpunJmbbfnsHxKmFvdlo6jlGmGfsJYxKRiL/lPiS0M0NNm tfR+FQLbE8Q1kOBBmCJ+s5pXvcEAZ+Onmymrt9lAa0wHid4gkjz/NcPBWJXLL82SMpXcO LWzW/PvIHY0/QYHa0n2jYPPWyAdVBF/J1GVbNLi1wsT09mRjz/SnIBqQ/pECADEXn4A== 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.51 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 CEA68606A322 for ; Wed, 2 Dec 2026 08:03:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CEA68606A322 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=y0HMQd9W Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-3b4021b572dso7768420a91.2 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=FiLtjHdKLDuiGqjwXOlQzty+QMZu5tz58IEap9sbfll6SgZ0q+/OWlY6iBBW4ZzD3w 7REGnWyY4FXMuIGwy4unBSsLpK0kyN6GB6PbHISPKHIZK/VCt1ySHtlRTP8KJ2kOl3V2 lrtRrG5SkrsTtOeGqf+O+zxduk06bFo1UKRQOX8aRPZp1VypZwNF5bUpMtY/SfFtpRVi ihW2fiJChzqmBHVHbvX5sTGQQ4D4PpegyO49tAD0zd1/qcc7UUbsGlSJViKZY/xITHDe qcnBFF13h0ML7sUboDKU/YAu0Zrmtg2kVHO4A3fVgFKFbLCtRz3uIctgbTa9e5TrwOHp qYgw== X-Forwarded-Encrypted: i=1; AL+hErGLGi5mPCLhq5Ji/eaTYGCT0ajsrbG9UP0kLl8e39CvihnGBbGBqcWWNj4afLaqtMi20JUcmfY=@vger.kernel.org X-Gm-Message-State: APuU4Z98T3KuZt6fIhpb8rRGZrJJBUCzSB4n8WcoigOyb6e0dksOV7Ck Ep0eYQPUM2CWb2wvqzMCspAyaR3D+PWIdAc4/OmviJHL0qBf10lmIFWpD3lHtvVlW9M= X-Gm-Gg: AZFdTZfE9/rHivB2ZmOsXit9Zzay7FEPSwVw6Fx8Wbr+s+9I84YC01SOR0PIk4KNV3s PI2eiEEqlyk7nVLUusuAUe0tzHH1Ul/9HqW8iv3E+6DKg/cdkvkDVyG8hRFKVEJltU2HluSVG1e Tur7uQADyUWDkuNIH4u4XjhJkqG4YgneH29k5vy0nE5xyHuN3paDAySljDUVgW8OX0TvkhXnvFI Gi0o5Y4Zg0ae6ptzni38CEUTMmXQEjvlkr/gljGxqIfwnpYoAB/Y6s5OyZPjFotbAI+jvrNdaGR 4YgRu7F1JGsx5DHZyjJ+uoAWxWuIqouMhzanWFJHjqSjnpmtPMPN5iooRM2hpNMQ6yLzCHBuEht QcBmJRI9aUXGsOdDRsy4mEHX82KeKJ+iXO5HzObUzx88iNzin8tHC5bEddtj8UL9NvYmqFs/e7F hq2dNQqItZRWZXtFy3AkS6IBCo++HroOxafm2gmwEa9SQ0ofrdIE0zHRBKulqZfKkeikpq3N1U6 oRtK6lQ4hDUZaLGaH2GpVv6hqqFRZbCCnIGN+kpY4vMeWhwaY7872jLDVoIqr0fY6IlvVq2Drdt JARR/iM9vujEfYA1itAOWgLJZ/lT7nVDnZLe2o1+SvhiDdbrO+R4wNBjcko= 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: 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, 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