Skip to contentSkip to conversation

rust: time: fix as_micros_ceil() rounding near i64::MAX

PATCH Superseded FUJITA Tomonori 2 participants 4 messages Jul 23 – Aug 6

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: time: fix as_micros_ceil() rounding near i64::MAX

From: FUJITA Tomonori <[email protected]>

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 yields a result one microsecond too small.

---
 rust/kernel/time.rs | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
rust/kernel/time.rs +4 -7
index b8463823aed9..3d2c8e3a2f80 100644
@@ -442,21 +442,18 @@ pub const fn as_nanos(self) -> i64 {
     #[inline]
     pub fn as_micros_ceil(self) -> i64 {
         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: 880c43b185ca52239e75bc546cc4f4d9154d0fed

Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

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 yields a result one microsecond too small.

It took me a little while to understand this one. Perhaps a few comments explaining the boundary conditions could be warranted here?

Best regards,
Andreas Hindborg

#2 lore raw
Quoted 14 lines · On Wed, 05 Aug 2026 13:39:43 +0200 Andreas Hindborg <[email protected]>…
Quoted 11 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

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 yields a result one microsecond too small.

Quoted 4 lines

It took me a little while to understand this one. Perhaps a few comments explaining the boundary conditions could be warranted here?

I added the comments for as_millis_ceil() [1]. You want the similar comments for as_micros_ceil() too?

Or we could add a small helper for _ceil() and keep the explanation in one place.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

maintainer
#3 lore raw
Quoted 19 lines · FUJITA Tomonori
Quoted 16 lines · On Wed, 05 Aug 2026 13:39:43 +0200 Andreas Hindborg <[email protected]>…
Quoted 11 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

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 yields a result one microsecond too small.

Quoted 8 lines
Quoted 2 lines

It took me a little while to understand this one. Perhaps a few comments explaining the boundary conditions could be warranted here?

I added the comments for as_millis_ceil() [1]. You want the similar comments for as_micros_ceil() too?

Or we could add a small helper for _ceil() and keep the explanation in one place.

Let's add the comment to this one, as it will go in with rust tree fixes asap. Then you can extract to reduce duplication in your other series. Ok with you?

Best regards,
Andreas Hindborg