[PATCH v2 8/8] rust: hrtimer: Add HrTimer::expires()

Lyude Paul posted 8 patches 8 months, 1 week ago
There is a newer version of this series
[PATCH v2 8/8] rust: hrtimer: Add HrTimer::expires()
Posted by Lyude Paul 8 months, 1 week ago
This adds the ability to read the expiry time of the given timer.

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

---
V2:
* Convert from Ktime to Instant
* Use read_volatile instead of read and add a FIXME

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

diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
index c84dcdacb4882..b8a74c15e6609 100644
--- a/rust/kernel/time/hrtimer.rs
+++ b/rust/kernel/time/hrtimer.rs
@@ -73,7 +73,10 @@
     time::{Delta, Instant},
     types::Opaque,
 };
-use core::{marker::PhantomData, ptr::NonNull};
+use core::{
+    marker::PhantomData,
+    ptr::{addr_of, NonNull},
+};
 use pin_init::PinInit;
 
 /// A timer backed by a C `struct hrtimer`.
@@ -136,7 +139,7 @@ unsafe fn raw_get(this: *const Self) -> *mut bindings::hrtimer {
         // SAFETY: The field projection to `timer` does not go out of bounds,
         // because the caller of this function promises that `this` points to an
         // allocation of at least the size of `Self`.
-        unsafe { Opaque::raw_get(core::ptr::addr_of!((*this).timer)) }
+        unsafe { Opaque::raw_get(addr_of!((*this).timer)) }
     }
 
     /// Cancel an initialized and potentially running timer.
@@ -225,6 +228,21 @@ pub fn forward(&mut self, now: Instant, duration: Delta) -> u64 {
     pub fn forward_now(&mut self, duration: Delta) -> u64 {
         self.forward(self.clock_base().time(), duration)
     }
+
+    /// Return the time expiry for this [`HrTimer`].
+    ///
+    /// This value should only be used as a snapshot, as the actual expiry time could change after
+    /// this function is called.
+    pub fn expires(&self) -> Instant {
+        // SAFETY: `self` is an immutable reference and thus always points to a valid `HrTimer`.
+        let c_timer_ptr = unsafe { HrTimer::raw_get(self) };
+
+        // SAFETY: There's no actual locking here, a racy read is fine and expected.
+        Instant::from_nanos(unsafe {
+            // FIXME: read_volatile
+            core::ptr::read_volatile(addr_of!((*c_timer_ptr).node.expires))
+        })
+    }
 }
 
 /// The timer base for a specific clock.
-- 
2.48.1
Re: [PATCH v2 8/8] rust: hrtimer: Add HrTimer::expires()
Posted by Andreas Hindborg 8 months ago
Lyude Paul <lyude@redhat.com> writes:

> This adds the ability to read the expiry time of the given timer.
>
> Signed-off-by: Lyude Paul <lyude@redhat.com>
>
> ---
> V2:
> * Convert from Ktime to Instant
> * Use read_volatile instead of read and add a FIXME
>
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
>  rust/kernel/time/hrtimer.rs | 22 ++++++++++++++++++++--
>  1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
> index c84dcdacb4882..b8a74c15e6609 100644
> --- a/rust/kernel/time/hrtimer.rs
> +++ b/rust/kernel/time/hrtimer.rs
> @@ -73,7 +73,10 @@
>      time::{Delta, Instant},
>      types::Opaque,
>  };
> -use core::{marker::PhantomData, ptr::NonNull};
> +use core::{
> +    marker::PhantomData,
> +    ptr::{addr_of, NonNull},
> +};
>  use pin_init::PinInit;
>  
>  /// A timer backed by a C `struct hrtimer`.
> @@ -136,7 +139,7 @@ unsafe fn raw_get(this: *const Self) -> *mut bindings::hrtimer {
>          // SAFETY: The field projection to `timer` does not go out of bounds,
>          // because the caller of this function promises that `this` points to an
>          // allocation of at least the size of `Self`.
> -        unsafe { Opaque::raw_get(core::ptr::addr_of!((*this).timer)) }
> +        unsafe { Opaque::raw_get(addr_of!((*this).timer)) }
>      }
>  
>      /// Cancel an initialized and potentially running timer.
> @@ -225,6 +228,21 @@ pub fn forward(&mut self, now: Instant, duration: Delta) -> u64 {
>      pub fn forward_now(&mut self, duration: Delta) -> u64 {
>          self.forward(self.clock_base().time(), duration)
>      }
> +
> +    /// Return the time expiry for this [`HrTimer`].
> +    ///
> +    /// This value should only be used as a snapshot, as the actual expiry time could change after
> +    /// this function is called.
> +    pub fn expires(&self) -> Instant {
> +        // SAFETY: `self` is an immutable reference and thus always points to a valid `HrTimer`.
> +        let c_timer_ptr = unsafe { HrTimer::raw_get(self) };
> +
> +        // SAFETY: There's no actual locking here, a racy read is fine and expected.
> +        Instant::from_nanos(unsafe {
> +            // FIXME: read_volatile

Please adopt similar wording as in `file.rs`:

        // This `read_volatile` is intended to correspond to a READ_ONCE call.
        // FIXME(read_once): Replace with `read_once` when available on the Rust side.


Best regards,
Andreas Hindborg