Rust for Linux List
 help / color / mirror / Atom feed
From: Franklin-Qi <yusq77@gmail.com>
To: Andreas Hindborg <a.hindborg@kernel.org>
Cc: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>,
	Lyude Paul <lyude@redhat.com>, Miguel Ojeda <ojeda@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	FUJITA Tomonori <fujita.tomonori@gmail.com>,
	Boqun Feng <boqun@kernel.org>, Danilo Krummrich <dakr@kernel.org>,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta
Date: Wed,  2 Dec 2026 16:03:06 +0800	[thread overview]
Message-ID: <20261202080306.898547-1-yusq77@gmail.com> (raw)
In-Reply-To: <87mrqps4a2.fsf@t14s.mail-host-address-is-not-set>

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.

Best regards,
Franklin

      parent reply	other threads:[~2026-12-02  8:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Mf9TNvNPTgERimoWnx-HXvSDdGaI4X1aH9iSG6qeVNn70w9dd_u0rKrIaSqNi8YN3O__uOmCDl8AWxrgV65JEg==@protonmail.internalid>
2026-12-01  6:41 ` [PATCH] rust: time: keep Instant invariant when adding/subtracting Delta Franklin-Qi
2026-12-01 10:07   ` Andreas Hindborg
2026-12-01 10:52     ` Miguel Ojeda
2026-12-02  8:03     ` Franklin-Qi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202080306.898547-1-yusq77@gmail.com \
    --to=yusq77@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=lyude@redhat.com \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox