Skip to content

Commit bcaba70

Browse files
fujitabpf-ci[bot]
authored andcommitted
rust: time: fix as_micros_ceil() to round correctly for negative Delta
commit 880c43b upstream. 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: fae0cdc ("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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d29cb14 commit bcaba70

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

rust/kernel/time.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,22 @@ impl Delta {
441441
/// to the value in the [`Delta`].
442442
#[inline]
443443
pub fn as_micros_ceil(self) -> i64 {
444+
let n = self.as_nanos();
445+
let n = if n >= 0 {
446+
n.saturating_add(NSEC_PER_USEC - 1)
447+
} else {
448+
n
449+
};
450+
444451
#[cfg(CONFIG_64BIT)]
445452
{
446-
self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC
453+
n / NSEC_PER_USEC
447454
}
448455

449456
#[cfg(not(CONFIG_64BIT))]
450457
// SAFETY: It is always safe to call `ktime_to_us()` with any value.
451458
unsafe {
452-
bindings::ktime_to_us(self.as_nanos().saturating_add(NSEC_PER_USEC - 1))
459+
bindings::ktime_to_us(n)
453460
}
454461
}
455462

0 commit comments

Comments
 (0)