[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250429214752.1637859-8-lyude@redhat.com>
Date: Tue, 29 Apr 2025 17:44:43 -0400
From: Lyude Paul <lyude@...hat.com>
To: rust-for-linux@...r.kernel.org,
Andreas Hindborg <a.hindborg@...nel.org>,
linux-kernel@...r.kernel.org
Cc: Boqun Feng <boqun.feng@...il.com>,
FUJITA Tomonori <fujita.tomonori@...il.com>,
Frederic Weisbecker <frederic@...nel.org>,
Thomas Gleixner <tglx@...utronix.de>,
Anna-Maria Behnsen <anna-maria@...utronix.de>,
John Stultz <jstultz@...gle.com>,
Stephen Boyd <sboyd@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>
Subject: [PATCH v4 7/7] rust: hrtimer: Add HrTimer::expires()
This adds the ability to read the expiry time of the given timer.
Signed-off-by: Lyude Paul <lyude@...hat.com>
---
V2:
* Convert from Ktime to Instant
* Use read_volatile instead of read and add a FIXME
V4:
* Correct read_volatile comment in HrTimer::expires()
Signed-off-by: Lyude Paul <lyude@...hat.com>
---
rust/kernel/time/hrtimer.rs | 27 +++++++++++++++++++++++++--
1 file changed, 25 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
index 0d93866d7b20a..5b508ff81aee2 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.
@@ -241,6 +244,26 @@ pub fn forward_now(self: Pin<&mut Self>, interval: Delta) -> u64 {
self.forward(now, interval)
}
+
+ /// 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:
+ // - `node.expires` is a ktime_t, so it must be within the range of `0` to `KTIME_MAX`.
+ // - There's no actual locking here, a racy read is fine and expected
+ unsafe {
+ Instant::from_nanos(
+ // 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.
+ core::ptr::read_volatile(addr_of!((*c_timer_ptr).node.expires)),
+ )
+ }
+ }
}
/// Implemented by pointer types that point to structs that contain a [`HrTimer`].
--
2.48.1
Powered by blists - more mailing lists