aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@gmail.com>2026-07-14 07:52:35 +0900
committerMiguel Ojeda <ojeda@kernel.org>2026-07-18 19:07:35 +0200
commit880c43b185ca52239e75bc546cc4f4d9154d0fed (patch)
tree8f4d0d4e27a07b3a0a5cae86e1d48dab9a135443
parenta19bda861b3a79e25417462539df8b0d77c6b322 (diff)
rust: time: fix as_micros_ceil() to round correctly for negative Delta
The ceiling-division idiom `(n + d - 1) / d` only produces the correct result when `n` is non-negative. For example, if n = -1000 (exactly -1us), the old code computed (-1000 + 999) / 1000 == 0 instead of -1. For negative n, truncating division already rounds towards positive infinity, so no bias is needed in that case. Fixes: fae0cdc12340 ("rust: time: Introduce Delta type") Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Acked-by: Andreas Hindborg <a.hindborg@kernel.org> Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20260713225235.3243480-1-tomo@flapping.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
-rw-r--r--rust/kernel/time.rs11
1 files changed, 9 insertions, 2 deletions
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 363e93cbb139..b8463823aed9 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -441,15 +441,22 @@ impl Delta {
/// to the value in the [`Delta`].
#[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
+ };
+
#[cfg(CONFIG_64BIT)]
{
- self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC
+ n / NSEC_PER_USEC
}
#[cfg(not(CONFIG_64BIT))]
// SAFETY: It is always safe to call `ktime_to_us()` with any value.
unsafe {
- bindings::ktime_to_us(self.as_nanos().saturating_add(NSEC_PER_USEC - 1))
+ bindings::ktime_to_us(n)
}
}