Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: time: Add Ktime

Introduce a wrapper around `ktime_t` with a few different useful methods.

Rust Binder will use these bindings to compute how many milliseconds a
transaction has been active for when dumping the current state of the
Binder driver. This replicates the logic in C Binder [1].

For a usage example in Rust Binder, see [2].

ktime_get() cannot be safely called in NMI context. This requirement is not
checked by these abstractions, but it is intended that klint [3] or a
similar tool will be used to check it in the future.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://lore.kernel.org/r/20240322-rust-ktime_ms_delta-v2-1-d98de1f7c282@google.com
Link: https://lore.kernel.org/lkml/5ac8c0d09392290be789423f0dd78a520b830fab.1682333709.git.zhangchuang3@xiaomi.com/ [1]
Link: https://r.android.com/3004103 [2]
Link: https://rust-for-linux.com/klint [3]

authored by

Alice Ryhl and committed by
Thomas Gleixner
48b7f4d2 8ff1e6c5

+60
+60
rust/kernel/time.rs
··· 5 5 //! This module contains the kernel APIs related to time and timers that 6 6 //! have been ported or wrapped for usage by Rust code in the kernel. 7 7 8 + /// The number of nanoseconds per millisecond. 9 + pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64; 10 + 8 11 /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. 9 12 pub type Jiffies = core::ffi::c_ulong; 10 13 ··· 20 17 // SAFETY: The `__msecs_to_jiffies` function is always safe to call no 21 18 // matter what the argument is. 22 19 unsafe { bindings::__msecs_to_jiffies(msecs) } 20 + } 21 + 22 + /// A Rust wrapper around a `ktime_t`. 23 + #[repr(transparent)] 24 + #[derive(Copy, Clone)] 25 + pub struct Ktime { 26 + inner: bindings::ktime_t, 27 + } 28 + 29 + impl Ktime { 30 + /// Create a `Ktime` from a raw `ktime_t`. 31 + #[inline] 32 + pub fn from_raw(inner: bindings::ktime_t) -> Self { 33 + Self { inner } 34 + } 35 + 36 + /// Get the current time using `CLOCK_MONOTONIC`. 37 + #[inline] 38 + pub fn ktime_get() -> Self { 39 + // SAFETY: It is always safe to call `ktime_get` outside of NMI context. 40 + Self::from_raw(unsafe { bindings::ktime_get() }) 41 + } 42 + 43 + /// Divide the number of nanoseconds by a compile-time constant. 44 + #[inline] 45 + fn divns_constant<const DIV: i64>(self) -> i64 { 46 + self.to_ns() / DIV 47 + } 48 + 49 + /// Returns the number of nanoseconds. 50 + #[inline] 51 + pub fn to_ns(self) -> i64 { 52 + self.inner 53 + } 54 + 55 + /// Returns the number of milliseconds. 56 + #[inline] 57 + pub fn to_ms(self) -> i64 { 58 + self.divns_constant::<NSEC_PER_MSEC>() 59 + } 60 + } 61 + 62 + /// Returns the number of milliseconds between two ktimes. 63 + #[inline] 64 + pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 { 65 + (later - earlier).to_ms() 66 + } 67 + 68 + impl core::ops::Sub for Ktime { 69 + type Output = Ktime; 70 + 71 + #[inline] 72 + fn sub(self, other: Ktime) -> Ktime { 73 + Self { 74 + inner: self.inner - other.inner, 75 + } 76 + } 23 77 }