Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796222275; b=4+7I8QpnhLOV26ILQgJG0G2SYJBTmuxRUGo3tY2J4vfUUclCoTdiqFWzgwUYaY00T3+C S6h77WX3PKWLjBaQcAZBSIhkf3VlYrV/EDAnEMq4OorpVPtsFk1NW0q8YHhq+r73s71DH a1VdiLfweiDdk7HRhxJS0ycmBh898SusKp5MxfacrcbwxVTTSZCqtDMMeXRtUN3/Msn3j ifi4p+61C9ufw+ziLsXQw+yuwcYRK9Fhp6WkWCQfQKoRurpFVqb12QLv6A1janBomdppn 5hMT7SASFzFOpS7f126Qu6TTrGs+fN9v2lWDN7sJzGGPT719RXML0q/rSFHevOVlXog== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222275; h=Received:DKIM-Signature:From:To:Cc:Subject:In-Reply-To:References: Date:Message-ID:MIME-Version:Content-Type; bh=W+1NiUv0nWbQ8LtRw6RwPXdue4ex/MsnZPD/r68AWPA=; b=fpMrzG+u58MHTnIJcvj0GmoPgVka2ooDKT3FJ//MY8/a0lxiMOZOQreI4PRYwIrpqCus NH3LjeqBZEeND0v2JV91rdBwnxZdceUnGv4VJip2kGr/A+1azGhzQTQf20j+EaUkBCW4G PG1Epk1XJLfsg7cxq2AqAszoV4cvtXQ6ZJJ+yMtK4dUIFDVicHoxn8TADa87M8BxHTRov +G64J8Rib8aC81RNSeoL3jj/eqFrhD7zKuvnj49AT48hax+a8xHPylpXw6gmuhaeyLc7D gfNe1WLJx57RMYMwV8E+6wJOUE5sISAzLkS3s7zZml6obYTL9kSWcviBWDieQjbl31g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GDPt9NgZ; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AF34606A335 for ; Wed, 2 Dec 2026 14:37:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GDPt9NgZ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64DAC1F00893; Wed, 2 Dec 2026 14:37:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796222274; bh=W+1NiUv0nWbQ8LtRw6RwPXdue4ex/MsnZPD/r68AWPA=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=GDPt9NgZDruzg/FYoa5cyyYMS+SLhY3mMnwwfBuMWRGniOZIjjhz44UILIfWN732/ 8PR6IsJeCk17vZYKoZhoznv1qep7Ncn/QnYhRu2++R4ge7NwB9gp8B8Dry87/eYexx oltdJDOAT+q880CE1M5mYJFFXuMowJe+PyUAm4bA0nqE1jcgix3WNN+xnvaHipzC+v PldyuW/lSAwLfWBmbYP3U4Tgbm8MlH1BvoyIwgXYe+eeewztrFQj2T9P99OUuHbfzA aDbpNOvtq4uakX5wMxLettDeDn54QSRCBkmfY6K4DqGklqANK9SNtw0yF5MvpVA/Q2 AP4reeynlgWuQ== From: Andreas Hindborg To: Franklin-Qi Cc: Miguel Ojeda , Lyude Paul , Miguel Ojeda , Alice Ryhl , FUJITA Tomonori , Boqun Feng , Danilo Krummrich , rust-for-linux@vger.kernel.org Subject: Re: [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta In-Reply-To: <20261202080306.898547-1-yusq77@gmail.com> References: <179610727734.137620.3884652165779521586@gmail.com> <87mrqps4a2.fsf@t14s.mail-host-address-is-not-set> <20261202080306.898547-1-yusq77@gmail.com> Date: Wed, 02 Dec 2026 15:37:44 +0100 Message-ID: <878q27sq87.fsf@t14s.mail-host-address-is-not-set> 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-Type: text/plain "Franklin-Qi" writes: > Hi Andreas, Miguel, > > Thanks for the review. > > On Tue, Dec 1, 2026 at 11:07 AM Andreas Hindborg wrote: >> could you elaborate pros/cons wrt. clamping instead of panicking here? > > Unconditional panic keeps the invariant simple to reason about, but it > turns an out-of-range Delta into a kernel crash even in configurations > where plain integer arithmetic would not panic. Clamping + warn_once > keeps the kernel running and the invariant intact, at the cost of > silently changing the value (hence the warning). > >> We could also consider panic if overflow checks enabled and clamp + >> warn if not. But this may be too confusing? > > I agree with Miguel that this hybrid is consistent with the existing > behaviour (the panic under CONFIG_RUST_OVERFLOW_CHECKS is not new), so > I went with it in v2, which I'm sending in a separate thread shortly: > > - CONFIG_RUST_OVERFLOW_CHECKS=y: panic when the result overflows or is > negative, as before. > - Otherwise: saturating_add()/saturating_sub(), a negative result is > clamped to 0 with a pr_warn_once!(), and a result past KTIME_MAX > saturates to KTIME_MAX (instead of wrapping and then being clamped to > 0). > - The panic/clamp conditions are documented in the rustdoc. > > I also checked it in QEMU with a small test module: with overflow > checks disabled the out-of-range cases are clamped/saturated and > in-range arithmetic is unchanged (without the patch the same cases > yield an out-of-range Instant); with overflow checks enabled they panic > as before. I think you can add test for the clamping. Probably not for the panic though. Best regards, Andreas Hindborg