From nobody Mon Feb 9 17:34:52 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EA6720AF8A; Mon, 3 Feb 2025 15:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738595434; cv=none; b=CLiTVDMGO4osFqY6JE1ZjqRmlc2uoksVtAFoELPQivOLZvXijwSfOQpL9qFUzX1tia7feSTbR/t9oX+EG7WizY+WxVnQwkzX769uNbF7ufpw/IMiS6yMQmVvOBrKX/vrCjr2jDOrqMsjZ3D3f6GdYtl1Krc+/luxTnKzwNrHgJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738595434; c=relaxed/simple; bh=jHHMaktyyXYLeTY5poXFGTxt1juB8cB36eNgrPIGZyQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dRNDAGxnBgUKBK8uSxoximVRZ2i3W6Ujicl4quwtCZBciM1IskSIAu7nErTjyZwlCkmH0WZu6ABEZBM18mZdwx4IOsHdaKee5KQFUUR3byeyQ7s9JhEm2wqysFtBnwCRnPQseBLTXAMCivwmZsixjdu4sCyvIhHNslMj1fDdTgk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hBV56c21; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hBV56c21" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4AA97C4CED2; Mon, 3 Feb 2025 15:10:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738595434; bh=jHHMaktyyXYLeTY5poXFGTxt1juB8cB36eNgrPIGZyQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hBV56c21KNgJ7WGuucnCVPhl/fAYBtzKOUXzmTQWU7qZODIrBwafrklfF8uxfdSVv ymdEkci5XHd9QgEmlVNej9N6kVqVbPu1ZmuCLAzYUcE5blZhNuSZavgtrbHRkMZW3z bgD0UdshmNo3fj7r8lQh+xsaWVkfxZmu40Xx9wX1A1aQmdZh42l9A+JDTD6N+RvvVO TfJL8pIV29BzpxjSH+wbVc+4SRn4TVIli8FfGakWPRHcSllvzP8Gu5ejF1EXVigdyY GIhXbPsb+DVy4MRInradwH1YEKNEUKDX6Bs8JaiI4Hds+31meiEbzrnKXs/TIPwcHI qmwnDLDuHH6pw== From: Andreas Hindborg Date: Mon, 03 Feb 2025 16:07:48 +0100 Subject: [PATCH v7 02/14] rust: hrtimer: introduce hrtimer support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250203-hrtimer-v3-v6-12-rc2-v7-2-189144725399@kernel.org> References: <20250203-hrtimer-v3-v6-12-rc2-v7-0-189144725399@kernel.org> In-Reply-To: <20250203-hrtimer-v3-v6-12-rc2-v7-0-189144725399@kernel.org> To: Miguel Ojeda , Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner , Danilo Krummrich Cc: Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Lyude Paul , Guangbo Cui <2407018371@qq.com>, Dirk Behme , Daniel Almeida , Tamir Duberstein , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=13047; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=jHHMaktyyXYLeTY5poXFGTxt1juB8cB36eNgrPIGZyQ=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBnoNvP5Pfbjz9tBDK8SaC7NryO8Ribm1OHAQqGw 5dBdmk+7QeJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCZ6DbzwAKCRDhuBo+eShj d7VHEACJozHtGCf8I3S448wZT5BdGspuMbz7bz0ocXjGJ2DrS2JwnWXcfrkGEeaOay4bpItTpIq DAhT8rRtKpqgWwUyYFKqDu3Ya8xQW+NJgQ53pARy3R8tn0z/umuiGAMgsI47Wxit7NXbzH1UYv+ GR0WkeyYv8hAyUMWX38AGCEv1/6BPW9MzruPuMmr7Q+LItKD7G83W0roTt7HeinFJb3R5l/g079 ImtQNgdMvtVCRFXqVs4oHXxssjLvFPjL8PscAfJuwAd+WKZAgvRJe1ae/qG5Vs5Ql6MIBlbt37d +SMrbB4PgmVfyNTGtiGnbkMniFUFzJgvJBcFi4qBz8HBY+/CDyTuYsLj3vjesVZVmpZvPjspM3h ZZwtDNA7PtPAx+71UOgPlKrYg+KNsTFhLxMEiBM5efQwVMiz9zvEHiZlvO7sMD7E4rZHse7pNzC 19Wy+l/60rtwExUvpfg79pCGMYPxM/n3f5Sc6gzCjxIHW8MQXHvTnvi4mroM60QpcRBpsrN5Lpf DaDfE4+fZUg9IObDNUD5pHLng3xxBafLrFUBkLrlaNM7/j0HMP9I4ul3zGlQTyEfFarQJ1pOXHY O3fq39FyeW0WxDRZFuTxRSgpbiBqfpliZmh8yhnDQ/tqjfxKmyejSGPblxaPNv+AuRSy9/VIBFW M0pNAEM1PXqcPEg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 This patch adds support for intrusive use of the hrtimer system. For now, only one timer can be embedded in a Rust struct. The hrtimer Rust API is based on the intrusive style pattern introduced by the Rust workqueue API. Signed-off-by: Andreas Hindborg --- rust/kernel/time.rs | 2 + rust/kernel/time/hrtimer.rs | 312 ++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 314 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 87e47f2f5618d6a4c2e534dd7ec663ced0e91503..2cf365cfb412e753367152a18fe= d1805d8033111 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -10,6 +10,8 @@ =20 use core::convert::Into; =20 +pub mod hrtimer; + /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 =3D bindings::NSEC_PER_MSEC as i64; =20 diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs new file mode 100644 index 0000000000000000000000000000000000000000..a6332924efabd40c475a112bbc4= 34db77596a16f --- /dev/null +++ b/rust/kernel/time/hrtimer.rs @@ -0,0 +1,312 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Intrusive high resolution timers. +//! +//! Allows running timer callbacks without doing allocations at the time of +//! starting the timer. For now, only one timer per type is allowed. +//! +//! # Vocabulary +//! +//! States: +//! +//! * Stopped +//! * Running +//! +//! Operations: +//! +//! * Start +//! * Cancel +//! * Stop +//! * Restart +//! +//! Events: +//! +//! * Expire +//! +//! ## State Diagram +//! +//! ```text +//! <-- Stop ---- +//! <-- Cancel -- +//! --- Start --> +//! +---------+ +---------+ +//! O--->| Stopped | | Running |---o +//! +---------+ +---------+ | +//! ^ | +//! <- Expire -- | | +//! o------o +//! Restart +//! ``` +//! +//! A timer is initialized in the **stopped** state. A stopped timer can be +//! **started** with an **expiry** time. After the timer is started, it is +//! **running**. When the timer **expires**, the timer handler is executed. +//! After the handler has executed, the timer may be **restarted** or +//! **stopped**. A running timer can be **canceled** before it's handler is +//! executed. A timer that is cancelled enters the **stopped** state. +//! + +use crate::{init::PinInit, prelude::*, time::Ktime, types::Opaque}; +use core::marker::PhantomData; + +/// A timer backed by a C `struct hrtimer`. +/// +/// # Invariants +/// +/// * `self.timer` is initialized by `bindings::hrtimer_setup`. +#[pin_data] +#[repr(C)] +pub struct HrTimer { + #[pin] + timer: Opaque, + _t: PhantomData, +} + +// SAFETY: Ownership of an `HrTimer` can be moved to other threads and +// used/dropped from there. +unsafe impl Send for HrTimer {} + +// SAFETY: Timer operations are locked on C side, so it is safe to operate= on a +// timer from multiple threads +unsafe impl Sync for HrTimer {} + +impl HrTimer { + /// Return an initializer for a new timer instance. + pub fn new() -> impl PinInit + where + T: HrTimerCallback, + { + pin_init!(Self { + // INVARIANTS: We initialize `timer` with `hrtimer_setup` belo= w. + timer <- Opaque::ffi_init(move |place: *mut bindings::hrtimer|= { + // SAFETY: By design of `pin_init!`, `place` is a pointer = to a + // live allocation. hrtimer_setup will initialize `place` = and + // does not require `place` to be initialized prior to the= call. + unsafe { + bindings::hrtimer_setup( + place, + Some(T::CallbackTarget::run), + bindings::CLOCK_MONOTONIC as i32, + bindings::hrtimer_mode_HRTIMER_MODE_REL, + ); + } + }), + _t: PhantomData, + }) + } + + /// Get a pointer to the contained `bindings::hrtimer`. + /// + /// # Safety + /// + /// `ptr` must point to a live allocation of at least the size of `Sel= f`. + unsafe fn raw_get(ptr: *const Self) -> *mut bindings::hrtimer { + // SAFETY: The field projection to `timer` does not go out of boun= ds, + // because the caller of this function promises that `ptr` points = to an + // allocation of at least the size of `Self`. + unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).timer)) } + } + + /// Cancel an initialized and potentially running timer. + /// + /// If the timer handler is running, this will block until the handler= is + /// finished. + /// + /// Users of the `HrTimer` API would not usually call this method dire= ctly. + /// Instead they would use the safe `cancel` method on the [`HrTimerHa= ndle`] + /// returned when the timer was started. + /// + /// # Safety + /// + /// `self_ptr` must point to a valid `Self`. + #[allow(dead_code)] + pub(crate) unsafe fn raw_cancel(self_ptr: *const Self) -> bool { + // SAFETY: timer_ptr points to an allocation of at least `HrTimer`= size. + let c_timer_ptr =3D unsafe { HrTimer::raw_get(self_ptr) }; + + // If the handler is running, this will wait for the handler to fi= nish + // before returning. + // SAFETY: `c_timer_ptr` is initialized and valid. Synchronization= is + // handled on C side. + unsafe { bindings::hrtimer_cancel(c_timer_ptr) !=3D 0 } + } +} + +/// Implemented by pointer types that point to structs that embed a [`HrTi= mer`]. +/// +/// Target (pointee) must be [`Sync`] because timer callbacks happen in an= other +/// thread of execution (hard or soft interrupt context). +/// +/// Starting a timer returns a [`HrTimerHandle`] that can be used to manip= ulate +/// the timer. Note that it is OK to call the start function repeatedly, a= nd +/// that more than one [`HrTimerHandle`] associated with a [`HrTimerPointe= r`] may +/// exist. A timer can be manipulated through any of the handles, and a ha= ndle +/// may represent a cancelled timer. +pub trait HrTimerPointer: Sync + Sized { + /// A handle representing a started or restarted timer. + /// + /// If the timer is running or if the timer callback is executing when= the + /// handle is dropped, the drop method of [`HrTimerHandle`] should not= return + /// until the timer is stopped and the callback has completed. + /// + /// Note: When implementing this trait, consider that it is not unsafe= to + /// leak the handle. + type TimerHandle: HrTimerHandle; + + /// Start the timer with expiry after `expires` time units. If the tim= er was + /// already running, it is restarted with the new expiry time. + fn start(self, expires: Ktime) -> Self::TimerHandle; +} + +/// Implemented by [`HrTimerPointer`] implementers to give the C timer cal= lback a +/// function to call. +// This is split from `HrTimerPointer` to make it easier to specify trait = bounds. +pub trait RawHrTimerCallback { + /// Callback to be called from C when timer fires. + /// + /// # Safety + /// + /// Only to be called by C code in `hrtimer` subsystem. `ptr` must poi= nt to + /// the `bindings::hrtimer` structure that was used to start the timer. + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrt= imer_restart; +} + +/// Implemented by structs that can be the target of a timer callback. +pub trait HrTimerCallback { + /// The type whose [`RawHrTimerCallback::run`] method will be invoked = when + /// the timer expires. + type CallbackTarget<'a>: RawHrTimerCallback; + + /// This type is passed to the timer callback function. It may be a bo= rrow + /// of [`Self::CallbackTarget`], or it may be `Self::CallbackTarget` i= f the + /// implementation can guarantee exclusive access to the target during= timer + /// handler execution. + type CallbackTargetParameter<'a>; + + /// Called by the timer logic when the timer fires. + fn run(this: Self::CallbackTargetParameter<'_>) + where + Self: Sized; +} + +/// A handle representing a potentially running timer. +/// +/// More than one handle representing the same timer might exist. +/// +/// # Safety +/// +/// When dropped, the timer represented by this handle must be cancelled, = if it +/// is running. If the timer handler is running when the handle is dropped= , the +/// drop method must wait for the handler to finish before returning. +pub unsafe trait HrTimerHandle { + /// Cancel the timer, if it is running. If the timer handler is runnin= g, block + /// till the handler has finished. + fn cancel(&mut self) -> bool; +} + +/// Implemented by structs that contain timer nodes. +/// +/// Clients of the timer API would usually safely implement this trait by = using +/// the [`crate::impl_has_hr_timer`] macro. +/// +/// # Safety +/// +/// Implementers of this trait must ensure that the implementer has a [`Hr= Timer`] +/// field at the offset specified by `OFFSET` and that all trait methods a= re +/// implemented according to their documentation. +/// +/// [`impl_has_timer`]: crate::impl_has_timer +pub unsafe trait HasHrTimer { + /// Offset of the [`HrTimer`] field within `Self` + const OFFSET: usize; + + /// Return a pointer to the [`HrTimer`] within `Self`. + /// + /// # Safety + /// + /// `ptr` must point to a valid struct of type `Self`. + unsafe fn raw_get_timer(ptr: *const Self) -> *const HrTimer { + // SAFETY: By the safety requirement of this trait, the trait + // implementor will have a `HrTimer` field at the specified offset. + unsafe { ptr.cast::().add(Self::OFFSET).cast::>() } + } + + /// Return a pointer to the struct that is embedding the [`HrTimer`] p= ointed + /// to by `ptr`. + /// + /// # Safety + /// + /// `ptr` must point to a [`HrTimer`] field in a struct of type `Se= lf`. + unsafe fn timer_container_of(ptr: *mut HrTimer) -> *mut Self + where + Self: Sized, + { + // SAFETY: By the safety requirement of this function and the `Has= HrTimer` + // trait, the following expression will yield a pointer to the `Se= lf` + // containing the timer addressed by `ptr`. + unsafe { ptr.cast::().sub(Self::OFFSET).cast::() } + } + + /// Get pointer to embedded `bindings::hrtimer` struct. + /// + /// # Safety + /// + /// `self_ptr` must point to a valid `Self`. + unsafe fn c_timer_ptr(self_ptr: *const Self) -> *const bindings::hrtim= er { + // SAFETY: `self_ptr` is a valid pointer to a `Self`. + let timer_ptr =3D unsafe { Self::raw_get_timer(self_ptr) }; + + // SAFETY: timer_ptr points to an allocation of at least `HrTimer`= size. + unsafe { HrTimer::raw_get(timer_ptr) } + } + + /// Start the timer contained in the `Self` pointed to by `self_ptr`. = If + /// it is already running it is removed and inserted. + /// + /// # Safety + /// + /// `self_ptr` must point to a valid `Self`. + unsafe fn start(self_ptr: *const Self, expires: Ktime) { + // SAFETY: By function safety requirement, `self_ptr`is a valid `S= elf`. + unsafe { + bindings::hrtimer_start_range_ns( + Self::c_timer_ptr(self_ptr).cast_mut(), + expires.to_ns(), + 0, + bindings::hrtimer_mode_HRTIMER_MODE_REL, + ); + } + } +} + +/// Use to implement the [`HasHrTimer`] trait. +/// +/// See [`module`] documentation for an example. +/// +/// [`module`]: crate::time::hrtimer +#[macro_export] +macro_rules! impl_has_hr_timer { + ( + impl$({$($generics:tt)*})? + HasHrTimer<$timer_type:ty> + for $self:ty + { self.$field:ident } + $($rest:tt)* + ) =3D> { + // SAFETY: This implementation of `raw_get_timer` only compiles if= the + // field has the right type. + unsafe impl$(<$($generics)*>)? $crate::time::hrtimer::HasHrTimer<$= timer_type> for $self { + const OFFSET: usize =3D ::core::mem::offset_of!(Self, $field) = as usize; + + #[inline] + unsafe fn raw_get_timer(ptr: *const Self) -> + *const $crate::time::hrtimer::HrTimer<$timer_type> + { + // SAFETY: The caller promises that the pointer is not dan= gling. + unsafe { + ::core::ptr::addr_of!((*ptr).$field) + } + } + } + } +} --=20 2.47.0