summaryrefslogtreecommitdiff
path: root/rust/kernel
diff options
context:
space:
mode:
authorLyude Paul <lyude@redhat.com>2025-08-21 15:32:47 -0400
committerAndreas Hindborg <a.hindborg@kernel.org>2025-09-04 16:54:39 +0200
commit4b0147494275fdbea98305f4ddad7e2def7b556f (patch)
tree9c4b462782d03607bf7377d3a3874c997029204c /rust/kernel
parent583802cc99bdac95d173aaf1fc58e99993aa1d1d (diff)
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 <lyude@redhat.com> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org> Link: https://lore.kernel.org/r/20250821193259.964504-8-lyude@redhat.com Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
Diffstat (limited to 'rust/kernel')
-rw-r--r--rust/kernel/time.rs1
-rw-r--r--rust/kernel/time/hrtimer.rs23
2 files changed, 23 insertions, 1 deletions
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 874a1023dcdf..7320d8715bcc 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -211,7 +211,6 @@ impl<C: ClockSource> Instant<C> {
/// # Safety
///
/// The caller promises that `ktime` is in the range from 0 to `KTIME_MAX`.
- #[expect(unused)]
#[inline]
pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
debug_assert!(ktime >= 0);
diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
index e0d78a885990..856d2d929a00 100644
--- a/rust/kernel/time/hrtimer.rs
+++ b/rust/kernel/time/hrtimer.rs
@@ -223,6 +223,29 @@ impl<T> HrTimer<T> {
{
self.forward(HrTimerInstant::<T>::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) -> HrTimerInstant<T>
+ where
+ T: HasHrTimer<T>,
+ {
+ // SAFETY: `self` is an immutable reference and thus always points to a valid `HrTimer`.
+ let c_timer_ptr = unsafe { HrTimer::raw_get(self) };
+
+ // SAFETY:
+ // - Timers cannot have negative ktime_t values as their expiration time.
+ // - There's no actual locking here, a racy read is fine and expected
+ unsafe {
+ Instant::from_ktime(
+ // 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(&raw const ((*c_timer_ptr).node.expires)),
+ )
+ }
+ }
}
/// Implemented by pointer types that point to structs that contain a [`HrTimer`].