From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796107304; b=J6YHA7xHH4+EsF+sjKZ+BoTEauRflshsKe4yTppo2rrThRPPYHYw2yaIa9ewKSL+HsmP oVbVS8MaWZsAkIJrOE86yfNAFIJKvRyf121tar/kfVPCOq5pMxT3HqkgrTg9foEIsKBEq 2pvuiR2BedgRBt7zNy7wTWYNPpOvmYiXtWgnnWhELkotN2suViwrbv0wgdDjb0vaI/mYV Hi3L5esHR3MLXcTpEDK0eLAiAvfdkd++5yhFOtTkIEoEiKW42OOzGkd9x5ZyLbWfK+WBj xhUY8nrLc5IjLyo820Z+Jyc3fDZw6WBPuYqDh/2h7opm/abxUFJjyJ5eW1/KCU8/mSQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796107304; 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:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=1AZvSCs5F9ha4dOrSvumZnsd3LYSVcirwduXp/hl8Ho=; b=2143grvCbYrOIQAtGE8DME4cU6RXHyPlwq6uSnh8mqyyLcOfFZYkxkUU+lVvI1gdA18z wHHOhzENN6oT3PC0MSasfEqeX2fFmO6xbi6RrNCqr5rtabBC/Ef3Ef7ERyZbtT4+oERSZ EhMxrD+dXozKuEuxiZANF/WrlTRccme0c13GLfoEKOkRw29L7AJVqm9AYI8AbmP396L0V lLvU04Hr3+4MEY7KujQsyoqy02/YRzREOMv0eOa2wN1O2TDb9yXuBuy2vDjswu2uQsR/v NgJw/cyl16gWvRXFlX1TRviXQ1vowElGp0fA1JW8TOENSo4SpeO9FiURRow/eqqku8w== 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=YBKeSz2Z; arc=none smtp.remote-ip=209.85.215.181 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (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 78C49606A32F for ; Tue, 1 Dec 2026 06:41:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 78C49606A32F 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=YBKeSz2Z Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-cdab938dc83so4507389a12.3 for ; Mon, 30 Nov 2026 22:41:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796107304; x=1796712104; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:content-type:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=1AZvSCs5F9ha4dOrSvumZnsd3LYSVcirwduXp/hl8Ho=; b=YBKeSz2Zg3d+z++oihpsWPR/JDwOufyKAP9OsDBZ4xeYh8pOKBVR5TPwhPe6N+20H/ cRl0aD1Z8i/Y6qkIlDL053o2hEJMgX2pCfK4E6NhJcrqwvMW9ocqZq4Z6aogfQZmnXUO ruTpsVo+b7Jk2WSzK7Sixt1ddmIap27XBNH5TkPSvh7j4s/dl+CUISm5iaq9lAjh5YNG UemP9mOW255DNQsSKWyp0nVrW0LXgg2FnaEBcTkobHy8erWBFSOucbS2iy0x1yGOGSH+ 65Ozupl4uOfuwx1lFu8xhYq1s0aQ0UurG2prlWqwvHb+8xGOQobeXHyAiaPMCei9pAWV MDXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796107304; x=1796712104; h=mime-version:content-transfer-encoding:content-type: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=1AZvSCs5F9ha4dOrSvumZnsd3LYSVcirwduXp/hl8Ho=; b=MnkSVS9JxJafqEmdQNBqnsYkUI2FoZQLErY6oLNFa+MG/V4mVNlgLi0v0cExbH7zLM sI/u1cp4DlXEXloICPlCOjLbxjCMeNo4QjfUGaRjLzkkW3uLzyS48TkveUbai7aVhd8z B5dPvCrCch8Fj7qXXP2Kysqz0zXnLfEkAz2nrDyz4maYyC+fAEmwCyoaaGmj/KM+o1KM 04N6FtTqKs3S1BxbLLq5N2AQJZpjp70SL3cB3d3HcojtLWlQFWbf/gi8phXz6Z/9I3V9 WJa2Y45MAOzorIdje80ju3GNn7JWk1048fHWSfUfXhblWR2+WiJxeXpzNHbbhXX74+Ao wBvw== X-Forwarded-Encrypted: i=1; AL+hErEEhBeE41YxT5C1NQVwgVNEcZqPdfm96KB/S/FCuG+SnBoHLgpvo3n0ZAlu3ml5aDNCYiV4w3oYpU3/PB16FA==@vger.kernel.org X-Gm-Message-State: APuU4Z+7NS68D6BL0DCVs+mRSStH1ekg8N1/l9iFvGbOshRoVBOimOhQ lwCoMEiOmIVxtx3VPvtSUyOF0MsHYBd/FNUzIn1d7LcU4J3Z9iTMQXIa X-Gm-Gg: AZFdTZdl1T9XLhki+E8oQQs9F1VlOwA6v8dNcEf2DcZMrT5cYnW6Asnws6etJJyjpEl khX96Gvjf94uMCyWo+R1SmppZnxVq+SRbroyO29Y68BJW8c8Zwcyv5gqdUBfK2KC/nrcv6enBl8 UArN7e6WrSTdnrSJ4Vtybfc1pnvQczJgZfu7+IpluoqDAJp0UPVCXGphsLUMewT3UDJkxGPu48t moLyac+nnKk2JPp9xLcTICAxuxp414LgN0XjoEHTtzST3Hr990gUdnnesQsjh05PeNXpJ/A3ot0 KHiNty9JfGisSJUDl5Xjhw/caDMfwMYtBHZgSex7uyXx9f9qSzxkpBUCsZ31bf1u3lLqxT1/BVF MuPSB0SLzPT0e6HIszITEqHy+XuSquDOeXBxeur2djT92sfuNeSLgfc/HIdkY18z8xX4YIW3w9B OZ931rqlzS+6JNMZqTozOMubmXYgxQ/e4Vvd3YWDo29ZkQiszUR4ZUBZVBb2VMxD2OiWLosFb8+ lzEFZlPg5MHl1MUTbmo2tiT9X8UCjqU232e2/z0UA7PdN0JmIiVQApqe1yo2hSbZoLeJTHPnElx oB3RB+Qr8YHnkhKrsjRvewgQdyOouP8= X-Received: by 2002:a17:90b:134e:b0:3ac:9a5e:9ce7 with SMTP id 98e67ed59e1d1-3b41c96764dmr37618932a91.49.1796107303633; Mon, 30 Nov 2026 22:41:43 -0800 (PST) Received: from smtp.gmail.com ([111.201.209.250]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ce0ea3d3402sm3525745a12.14.2026.11.30.22.41.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 22:41:43 -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 Subject: [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta Date: Tue, 01 Dec 2026 14:41:17 +0800 Message-ID: <179610727734.137620.3884652165779521586@gmail.com> X-Mailer: git-send-email-compatible Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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 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 later Instant-Instant subtraction may no longer be overflow-safe. Always assert the result is non-negative, and document the panic 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 --- Note: Per discussion on the GitHub issue with Miguel Ojeda, this uses an unconditional assert to preserve the Instant invariant (not clamp + warn_once), and documents the panic condition in the operators' rustdoc. rust/kernel/time.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index b6a2aab12a50..f58f55e440af 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -258,14 +258,17 @@ fn sub(self, other: Instant) -> Delta { impl ops::Add for Instant { type Output = Self; + /// # Panics + /// + /// Panics if the result of the addition is negative. #[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`) + // (e.g. go above `KTIME_MAX`). The assert below also rejects a wrapped negative result when + // overflow checks are disabled. let res = self.inner + rhs.value; - // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0 - #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] + // INVARIANT: The result must be non-negative (`0..=KTIME_MAX`). assert!(res >= 0); Self { @@ -278,14 +281,17 @@ fn add(self, rhs: Delta) -> Self::Output { impl ops::Sub for Instant { type Output = Self; + /// # Panics + /// + /// Panics if the result of the subtraction is negative. #[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`) + // (e.g. go above `KTIME_MAX`). The assert below also rejects a wrapped negative result when + // overflow checks are disabled. let res = self.inner - rhs.value; - // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0 - #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)] + // INVARIANT: The result must be non-negative (`0..=KTIME_MAX`). assert!(res >= 0); Self { -- 2.55.0