* [PATCH v2] rust: time: fix as_micros_ceil() rounding near i64::MAX @ 2026-08-07 13:05 ` FUJITA Tomonori 2026-08-10 10:47 ` Andreas Hindborg 2026-08-11 12:07 ` Miguel Ojeda 0 siblings, 2 replies; 3+ messages in thread From: FUJITA Tomonori @ 2026-08-07 13:05 UTC (permalink / raw) To: a.hindborg, ojeda Cc: acourbot, aliceryhl, anna-maria, bjorn3_gh, boqun, dakr, daniel.almeida, frederic, gary, jstultz, lossin, lyude, sboyd, tamird, tglx, tmgross, work, rust-for-linux, FUJITA Tomonori, Miguel Ojeda From: FUJITA Tomonori <fujita.tomonori@gmail.com> The ceiling adjustment used saturating_add(NSEC_PER_USEC - 1) before dividing. Once the nanosecond value gets within NSEC_PER_USEC - 1 of i64::MAX the addition saturates to i64::MAX, which drops the ceiling bias and can yield a result one microsecond too small. Fixes: fae0cdc12340 ("rust: time: Introduce Delta type") Reported-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> Closes: https://lore.kernel.org/rust-for-linux/CANiq72mtS0ABA2JnT5tpz6J9c_mnxY+vyPvghV_ukngWvN8F2w@mail.gmail.com/ Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> --- v2 - Add the comment; no functional change v1: https://lore.kernel.org/all/20260723120907.209643-1-tomo@flapping.org/ --- rust/kernel/time.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index b8463823aed9..54fc46d460b6 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -441,22 +441,25 @@ pub const fn as_nanos(self) -> i64 { /// to the value in the [`Delta`]. #[inline] pub fn as_micros_ceil(self) -> i64 { + // Only positive values need to be rounded up: truncating division already + // rounds towards zero, i.e. up, for negative values. + // + // The usual `(nanos + d - 1) / d` is not used because the addition overflows + // once `nanos` exceeds `i64::MAX - (d - 1)`; saturating the addition instead + // would drop the rounding bias and return a result one unit too small. let n = self.as_nanos(); - let n = if n >= 0 { - n.saturating_add(NSEC_PER_USEC - 1) - } else { - n - }; + + let (n, add) = if n > 0 { (n - 1, 1) } else { (n, 0) }; #[cfg(CONFIG_64BIT)] { - n / NSEC_PER_USEC + n / NSEC_PER_USEC + add } #[cfg(not(CONFIG_64BIT))] // SAFETY: It is always safe to call `ktime_to_us()` with any value. unsafe { - bindings::ktime_to_us(n) + bindings::ktime_to_us(n) + add } } base-commit: 075b74841bd0065a3bda3440873c747938e69b68 -- 2.43.0 ^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] rust: time: fix as_micros_ceil() rounding near i64::MAX 2026-08-07 13:05 ` [PATCH v2] rust: time: fix as_micros_ceil() rounding near i64::MAX FUJITA Tomonori @ 2026-08-10 10:47 ` Andreas Hindborg 2026-08-11 12:07 ` Miguel Ojeda 1 sibling, 0 replies; 3+ messages in thread From: Andreas Hindborg @ 2026-08-10 10:47 UTC (permalink / raw) To: FUJITA Tomonori, ojeda Cc: acourbot, aliceryhl, anna-maria, bjorn3_gh, boqun, dakr, daniel.almeida, frederic, gary, jstultz, lossin, lyude, sboyd, tamird, tglx, tmgross, work, rust-for-linux, FUJITA Tomonori, Miguel Ojeda "FUJITA Tomonori" <tomo@flapping.org> writes: > From: FUJITA Tomonori <fujita.tomonori@gmail.com> > > The ceiling adjustment used saturating_add(NSEC_PER_USEC - 1) before > dividing. Once the nanosecond value gets within NSEC_PER_USEC - 1 of > i64::MAX the addition saturates to i64::MAX, which drops the ceiling > bias and can yield a result one microsecond too small. > > Fixes: fae0cdc12340 ("rust: time: Introduce Delta type") > Reported-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> > Closes: https://lore.kernel.org/rust-for-linux/CANiq72mtS0ABA2JnT5tpz6J9c_mnxY+vyPvghV_ukngWvN8F2w@mail.gmail.com/ > Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Acked-by: Andreas Hindborg <a.hindborg@kernel.org> @Miguel, can you take this one through rust or rust-fixes? Best regards, Andreas Hindborg ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] rust: time: fix as_micros_ceil() rounding near i64::MAX 2026-08-07 13:05 ` [PATCH v2] rust: time: fix as_micros_ceil() rounding near i64::MAX FUJITA Tomonori 2026-08-10 10:47 ` Andreas Hindborg @ 2026-08-11 12:07 ` Miguel Ojeda 1 sibling, 0 replies; 3+ messages in thread From: Miguel Ojeda @ 2026-08-11 12:07 UTC (permalink / raw) To: FUJITA Tomonori Cc: a.hindborg, ojeda, acourbot, aliceryhl, anna-maria, bjorn3_gh, boqun, dakr, daniel.almeida, frederic, gary, jstultz, lossin, lyude, sboyd, tamird, tglx, tmgross, work, rust-for-linux, FUJITA Tomonori On Fri, Aug 7, 2026 at 3:05 PM FUJITA Tomonori <tomo@flapping.org> wrote: > > From: FUJITA Tomonori <fujita.tomonori@gmail.com> > > The ceiling adjustment used saturating_add(NSEC_PER_USEC - 1) before > dividing. Once the nanosecond value gets within NSEC_PER_USEC - 1 of > i64::MAX the addition saturates to i64::MAX, which drops the ceiling > bias and can yield a result one microsecond too small. > > Fixes: fae0cdc12340 ("rust: time: Introduce Delta type") > Reported-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> > Closes: https://lore.kernel.org/rust-for-linux/CANiq72mtS0ABA2JnT5tpz6J9c_mnxY+vyPvghV_ukngWvN8F2w@mail.gmail.com/ > Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Applied to `rust-next` -- thanks everyone! Cc: stable@vger.kernel.org Cheers, Miguel ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-11 12:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <ljD6_kVHL_o6cG-Ct2gESNL_yHHcjtbcyKO7YrLDsgx0aPesopKonQsTjbbzWn-ZC-iVgOQ9Ig1jepifNTfFmA==@protonmail.internalid>
2026-08-07 13:05 ` [PATCH v2] rust: time: fix as_micros_ceil() rounding near i64::MAX FUJITA Tomonori
2026-08-10 10:47 ` Andreas Hindborg
2026-08-11 12:07 ` Miguel Ojeda
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.