Re: [PATCH v4] rust: hrtimer: add active() method to query timer state
From: Gary Guo
Date: Wed Nov 25 2026 - 09:30:01 EST
On Wed Nov 25, 2026 at 12:34 PM GMT, Andreas Hindborg wrote:
> 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 event can
> change the state right after the call. Callers that need a stable answer
> have to serialize against such events 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`.
>
> Reviewed-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>
> ---
> Changes in v4:
> - Reword the snapshot note to not name specific events (Tomonori).
> - Link to v3: https://msgid.link/20261105-hrtimer-active-v3-1-52636808ccda@xxxxxxxxxx
>
> 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/20260605-hrtimer-active-v2-1-77d755c81fd7@xxxxxxxxxx
>
> 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/20260215-hrtimer-active-v1-1-a754d10492c2@xxxxxxxxxx
> ---
> 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(+)