Skip to content

Commit 4b01474

Browse files
LyudeAndreas Hindborg
authored andcommitted
rust: hrtimer: Add HrTimer::expires()
Add a simple callback for retrieving the current expiry time for an HrTimer. In rvkms, we use the HrTimer expiry value in order to calculate the approximate vblank timestamp during each emulated vblank interrupt. Signed-off-by: Lyude Paul <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andreas Hindborg <[email protected]>
1 parent 583802c commit 4b01474

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

rust/kernel/time.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ impl<C: ClockSource> Instant<C> {
211211
/// # Safety
212212
///
213213
/// The caller promises that `ktime` is in the range from 0 to `KTIME_MAX`.
214-
#[expect(unused)]
215214
#[inline]
216215
pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
217216
debug_assert!(ktime >= 0);

rust/kernel/time/hrtimer.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,29 @@ impl<T> HrTimer<T> {
223223
{
224224
self.forward(HrTimerInstant::<T>::now(), interval)
225225
}
226+
227+
/// Return the time expiry for this [`HrTimer`].
228+
///
229+
/// This value should only be used as a snapshot, as the actual expiry time could change after
230+
/// this function is called.
231+
pub fn expires(&self) -> HrTimerInstant<T>
232+
where
233+
T: HasHrTimer<T>,
234+
{
235+
// SAFETY: `self` is an immutable reference and thus always points to a valid `HrTimer`.
236+
let c_timer_ptr = unsafe { HrTimer::raw_get(self) };
237+
238+
// SAFETY:
239+
// - Timers cannot have negative ktime_t values as their expiration time.
240+
// - There's no actual locking here, a racy read is fine and expected
241+
unsafe {
242+
Instant::from_ktime(
243+
// This `read_volatile` is intended to correspond to a READ_ONCE call.
244+
// FIXME(read_once): Replace with `read_once` when available on the Rust side.
245+
core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
246+
)
247+
}
248+
}
226249
}
227250

228251
/// Implemented by pointer types that point to structs that contain a [`HrTimer`].

0 commit comments

Comments
 (0)