[PATCH v2 6/8] rust: hrtimer: Add HrTimerClockBase::time()

Lyude Paul posted 8 patches 8 months, 1 week ago
There is a newer version of this series
[PATCH v2 6/8] rust: hrtimer: Add HrTimerClockBase::time()
Posted by Lyude Paul 8 months, 1 week ago
This adds a wrapper for the get_time() callback contained within a
hrtimer_clock_base struct. We'll use this in the next commit in order to
implement HrTimerCallbackContext::forward_now().

Signed-off-by: Lyude Paul <lyude@redhat.com>

---
V2:
* Convert from Ktime to Instant
* Don't forget to make HrTimerClockBase::time() pub

Signed-off-by: Lyude Paul <lyude@redhat.com>
---
 rust/kernel/time.rs         |  1 -
 rust/kernel/time/hrtimer.rs | 10 ++++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 545963140f180..a9a29dd80fc28 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -84,7 +84,6 @@ pub(crate) fn as_nanos(self) -> i64 {
         self.inner
     }
 
-    #[expect(unused)]
     #[inline]
     pub(crate) fn from_nanos(nanos: i64) -> Self {
         Self {
diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
index a56c66104f692..4da1e72e016d1 100644
--- a/rust/kernel/time/hrtimer.rs
+++ b/rust/kernel/time/hrtimer.rs
@@ -241,6 +241,16 @@ unsafe fn from_raw<'a>(ptr: *mut bindings::hrtimer_clock_base) -> &'a Self {
         // - Our data layout is equivalent to said struct via our type invariants.
         unsafe { &*ptr.cast() }
     }
+
+    /// Retrieve the current time from this [`HrTimerClockBase`].
+    pub fn time(&self) -> Instant {
+        // SAFETY: This callback is initialized to a valid NonNull function for as long as this type
+        // is exposed to users.
+        let get_time_fn = unsafe { (*self.0.get()).get_time.unwrap_unchecked() };
+
+        // SAFETY: This FFI function has no special requirements
+        Instant::from_nanos(unsafe { get_time_fn() })
+    }
 }
 
 /// Implemented by pointer types that point to structs that contain a [`HrTimer`].
-- 
2.48.1