rust: hrtimer: add active() method to query timer state
Subsystems (2)
Andreas Hindborg
maintainerPATCH v3 1/1 rust: hrtimer: add active() method to query timer state
Add an active() method to HrTimer that returns true if the timer is in the started or running states. This wraps the kernel's hrtimer_active() function.
Unlike a direct read of node.expires, this does not race with a concurrent start operation. hrtimer_active() reads the timer state inside a seqcount retry loop and re-reads timer->base with READ_ONCE(), and the core documents that it must not return a false negative. No exclusive access to the timer is therefore required, and the SAFETY comment reflects that.
The returned value is still only a snapshot: a concurrent start or an expiry can change the state right after the call. Callers that need a stable answer have to serialize against those operations themselves, exactly as C callers of hrtimer_active() do. This is documented on the method.
Also add documentation clarifying the definition of an active timer.
Introduce the term active as a set of timer states composed of started and running.
---
Changes in v3:
- Rebase on v7.4-rc1.
- Improve documentation.
- Move the note about not requiring exclusive access above the example.
- Name the user of the API below the cut.
- Link to v2: https://msgid.link/[email protected]
Changes in v2:
- Mark active() #[inline] (Gary).
- Use the [true] intra-doc link in the rustdoc (Miguel).
- Document that active() does not require exclusive access to the timer, and reflect this in the SAFETY comment (Gary).
- Add a doctest example that initializes a timer and asserts it is not active (Miguel).
- Link to v1: https://msgid.link/[email protected]
---
The bandwidth throttling timer in the Rust null block driver will call this function. It is used to arm the timer on request submission if it is not already armed.
--- rust/kernel/time/hrtimer.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
rust/kernel/time/hrtimer.rs +64 -0
@@ -71,6 +71,8 @@//! issue the `start` operation while the timer is in the **started** state. In //! this case the `start` operation is equivalent to the `restart` operation. //! +//! A timer is **active** if it is either in the **started** or **running** states. +//! //! # Examples //! //! ## Using an intrusive timer living in a [`Box`]
@@ -593,6 +595,68 @@ pub fn expires(&self) -> HrTimerInstant<T> ) } } + +/// Query the state of the timer. +/// +/// Returns [`true`] if the timer is in the started or running states. +/// +/// This method synchronizes internally and does not require exclusive access to the timer. +/// +/// The returned value is a snapshot. A concurrent start or expiry can change the state of the +/// timer right after this method returns, so callers that need the state to be stable must +/// serialize against those operations by other means. +/// +/// # Examples +/// +/// ``` +/// # use kernel::{ +/// # impl_has_hr_timer, +/// # prelude::*, +/// # time::{ +/// # hrtimer::{ +/// # HasHrTimer, HrTimer, HrTimerCallback, HrTimerCallbackContext, +/// # HrTimerRestart, RelativeMode, +/// # }, +/// # Monotonic, +/// # }, +/// # }; +/// # use pin_init::stack_pin_init; +/// #[pin_data] +/// struct ExampleTimer { +/// #[pin] +/// timer: HrTimer<Self>, +/// } +/// +/// impl ExampleTimer { +/// fn new() -> impl PinInit<Self> { +/// pin_init!(Self { timer <- HrTimer::new() }) +/// } +/// } +/// +/// impl HrTimerCallback for ExampleTimer { +/// type Pointer<'a> = Pin<&'a Self>; +/// fn run(_this: Pin<&Self>, _ctx: HrTimerCallbackContext<'_, Self>) -> HrTimerRestart { +/// HrTimerRestart::NoRestart +/// } +/// } +/// +/// impl_has_hr_timer! { +/// impl HasHrTimer<Self> for ExampleTimer { +/// mode: RelativeMode<Monotonic>, field: self.timer +/// } +/// } +/// +/// stack_pin_init!(let has_timer = ExampleTimer::new()); +/// assert!(!has_timer.timer.active()); +/// # Ok::<(), kernel::error::Error>(()) +/// ``` +#[inline] +pub fn active (&self) ->bool { +// SAFETY: By type invariant, `self.timer` is valid. `hrtimer_active` +// synchronizes internally and is safe to call without exclusive +// access to the timer. +unsafe { bindings::hrtimer_active (self.timer.get ()) } + } }/// Implemented by pointer types that point to structs that contain a [`HrTimer`].
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260215-hrtimer-active-f183411fe56b
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>
Quoted 24 lines · On Thu, 05 Nov 2026 11:17:24 +0100 Andreas Hindborg <[email protected]>…
Add an
active()method to HrTimer that returns true if the timer is in the started or running states. This wraps the kernel'shrtimer_active()function.Unlike a direct read of
node.expires, this does not race with a concurrent start operation.hrtimer_active()reads the timer state inside a seqcount retry loop and re-readstimer->basewithREAD_ONCE(), and the core documents that it must not return a false negative. No exclusive access to the timer is therefore required, and the SAFETY comment reflects that.The returned value is still only a snapshot: a concurrent start or an expiry can change the state right after the call. Callers that need a stable answer have to serialize against those operations themselves, exactly as C callers of
hrtimer_active()do. This is documented on the method.Also add documentation clarifying the definition of an active timer.
Introduce the term
activeas a set of timer states composed ofstartedandrunning.Reviewed-by FUJITA Tomonori <[email protected]>Signed-off-by Andreas Hindborg <[email protected]>
[...]
rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 607
Quoted 9 lines
rust/kernel/time/hrtimer.rs +9
-0+/// Query the state of the timer. +/// +/// Returns [`true`] if the timer is in the started or running states. +/// +/// This method synchronizes internally and does not require exclusive access to the timer. +/// +/// The returned value is a snapshot. A concurrent start or expiry can change the state of the +/// timer right after this method returns, so callers that need the state to be stable must +/// serialize against those operations by other means.
Does "expiry" here mean that the callback returns HrTimerRestart::NoRestart on another CPU? An expiry alone moves the timer from started to running, so active() still returns true. If so, I think it is clearer to write this case directly, and to add the case where the timer is canceled on another CPU.
The same sentence is in the commit message, so please update it too.
Andreas Hindborg
maintainerQuoted 40 lines · FUJITA Tomonori
Quoted 24 lines · On Thu, 05 Nov 2026 11:17:24 +0100 Andreas Hindborg <[email protected]>…
Add an
active()method to HrTimer that returns true if the timer is in the started or running states. This wraps the kernel'shrtimer_active()function.Unlike a direct read of
node.expires, this does not race with a concurrent start operation.hrtimer_active()reads the timer state inside a seqcount retry loop and re-readstimer->basewithREAD_ONCE(), and the core documents that it must not return a false negative. No exclusive access to the timer is therefore required, and the SAFETY comment reflects that.The returned value is still only a snapshot: a concurrent start or an expiry can change the state right after the call. Callers that need a stable answer have to serialize against those operations themselves, exactly as C callers of
hrtimer_active()do. This is documented on the method.Also add documentation clarifying the definition of an active timer.
Introduce the term
activeas a set of timer states composed ofstartedandrunning.Reviewed-by FUJITA Tomonori <[email protected]>Signed-off-by Andreas Hindborg <[email protected]>[...]
rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 607
Quoted 9 lines
rust/kernel/time/hrtimer.rs +9
-0+/// Query the state of the timer. +/// +/// Returns [`true`] if the timer is in the started or running states. +/// +/// This method synchronizes internally and does not require exclusive access to the timer. +/// +/// The returned value is a snapshot. A concurrent start or expiry can change the state of the +/// timer right after this method returns, so callers that need the state to be stable must +/// serialize against those operations by other means.
Quoted 6 lines
Does "expiry" here mean that the callback returns
HrTimerRestart::NoRestarton another CPU? An expiry alone moves the timer from started to running, soactive()still returns true. If so, I think it is clearer to write this case directly, and to add the case where the timer is canceled on another CPU.The same sentence is in the commit message, so please update it too.
Right. How about this:
The returned value is a snapshot. A concurrent evnet can change the state of the timer right after this method returns, so callers that need the state to be stable must serialize against those operations by other means.
The intention is not to reiterate the state machine, but to indicate that things might changes.
Best regards,
Andreas Hindborg
Quoted 19 lines · On Thu, 12 Nov 2026 09:55:05 +0100 Andreas Hindborg <[email protected]>…
Quoted 10 lines · FUJITA Tomonori
rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 607
Quoted 9 lines
rust/kernel/time/hrtimer.rs +9
-0+/// Query the state of the timer. +/// +/// Returns [`true`] if the timer is in the started or running states. +/// +/// This method synchronizes internally and does not require exclusive access to the timer. +/// +/// The returned value is a snapshot. A concurrent start or expiry can change the state of the +/// timer right after this method returns, so callers that need the state to be stable must +/// serialize against those operations by other means. Quoted 6 lines
Does "expiry" here mean that the callback returns
HrTimerRestart::NoRestarton another CPU? An expiry alone moves the timer from started to running, soactive()still returns true. If so, I think it is clearer to write this case directly, and to add the case where the timer is canceled on another CPU.The same sentence is in the commit message, so please update it too.
Quoted 8 lines
Right. How about this:
The returned value is a snapshot. A concurrent evnet can change the state of the timer right after this method returns, so callers that need the state to be stable must serialize against those operations by other means.
The intention is not to reiterate the state machine, but to indicate that things might changes.
Looks good to me, except for one small point. Since "start or expiry" was removed, it's not clear what "those operations" refers to. How about something like:
must serialize against such events by other means.
Also typo: evnet -> event