diff options
| author | FUJITA Tomonori <fujita.tomonori@gmail.com> | 2026-08-08 15:28:35 +0900 |
|---|---|---|
| committer | Miguel Ojeda <ojeda@kernel.org> | 2026-08-11 11:34:06 +0200 |
| commit | d481d999967d4e7ebcecf472fc29b8f5dfcfcc29 (patch) | |
| tree | 58fe7a7cacaf89e829502116f3840def24a8f687 /rust | |
| parent | 2e0d41002a2766cfc6742be02ea8b078567200d2 (diff) | |
| download | linux-stable-d481d999967d4e7ebcecf472fc29b8f5dfcfcc29.tar.gz linux-stable-d481d999967d4e7ebcecf472fc29b8f5dfcfcc29.zip | |
rust: time: add Delta::as_millis_ceil()
Add a ceiling variant, mirroring the existing as_micros_ceil() since
the existing as_millis() truncates towards zero.
Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Link: https://patch.msgid.link/20260808062839.1159990-4-tomo@flapping.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/kernel/time.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 53b77abcb317..6c0a5e8090d0 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -528,6 +528,32 @@ impl Delta { } } + /// Return the smallest number of milliseconds greater than or equal + /// to the value in the [`Delta`]. + #[inline] + pub fn as_millis_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, add) = if n > 0 { (n - 1, 1) } else { (n, 0) }; + + #[cfg(CONFIG_64BIT)] + { + n / NSEC_PER_MSEC + add + } + + #[cfg(not(CONFIG_64BIT))] + // SAFETY: It is always safe to call `ktime_to_ms()` with any value. + unsafe { + bindings::ktime_to_ms(n) + add + } + } + /// Return `self % dividend` where `dividend` is in nanoseconds. /// /// The kernel doesn't have any emulation for `s64 % s64` on 32 bit platforms, so this is |
