From nobody Wed Dec 17 19:37:28 2025 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 EB005192B76; Fri, 6 Dec 2024 19:35:23 +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=1733513724; cv=none; b=sBiuQTH3YlujwyREaaHFN2dF3MOg9RzVRMt7P9WkFV2YXdIWGX6QjGdUD2WotVmsXsLJxWiAkTdJrbBht65BMp5hDEMR1FMDMZiM8P0IGbXxUqaY4hOqDv7oQEDDSvU+g/QyFW2BJx9iu2cIX2+3hz4P6vAXN0P2dPhxd1Ygfxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513724; c=relaxed/simple; bh=xS2xojV+RkeX5FfW1EiMY3iI+ynZ/ywVhO4sPQ3S9wI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lOL8T2a9bZj8l1gHHjeC4E42K22MUqRh9PWDN3b7+D6vVN/e5l9bk0r2hA6pLj4ez3Tll03QOYSz1DqfSiowpTMYrlCSaA1kj2Qm81NuahGtIWGQfxR4iGJJhaWg/Mk8EyqC/8xnK4kpCc5cOi9XnZy05MgETn0hxZgFcxaKqEk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p5Y3RBcj; 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="p5Y3RBcj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73097C4CED1; Fri, 6 Dec 2024 19:35:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513723; bh=xS2xojV+RkeX5FfW1EiMY3iI+ynZ/ywVhO4sPQ3S9wI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=p5Y3RBcjHn4XebV8SWVNKZTQZVklkNNldvM1X0GmbG+UTSzjwtoCmZO9kMWaAvlsx ho/n0H+6YVUDSLLoh7srMxgHi27cJG74hbCZjFkFJK5N4JemXv0rFWQ2C55CVUsOaG 0VFOQtrYRjgcu6Q6iemOHMWrzkjSxZud+ClDtPcr13FB79MtOtA5Qi5y0aDusKIXdW U5lvwZa4M5qeI5VjPaueQBXV0FzfzkXDOBJFp1h8RZXvVMbxmejnsg9+OchCEKl1xO Nie4mkTY1gvmMFgiUzLLr5+gOsH/Ikh3E7MvAd0Da8qJ6wm7txRfkWvnhHW/F9AWAE J7FrAdUQ/u75Q== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:32:53 +0100 Subject: [PATCH v4 01/14] rust: time: Add Ktime::from_ns() 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: <20241206-hrtimer-v3-v6-12-rc2-v4-1-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1377; i=a.h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hza0dybWhFY2F4NkQKVEJ3UVlNRnU3R2VYWUJSeXF1cytuamc0c0tKMTg3eHVYTzBFWG84eSto dmlnYXlSWHpiR1RHS0ZYUEFmbGtCaApFRmlsV3VyQWRoMklQdS9ETWwwTHA4Q1I4c3diVVVMNU9 5NHVaQkh0V3RSNGY5UUxSWVRaak4xakJPUFJLc2IyCmFXa1NLckdrVDdZanVnPT0KPWwveVcKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 From: Lyude Paul A simple function to turn the provided value in nanoseconds into a Ktime value. We allow any type which implements Into, which resolves to Into. This is useful for some of the older DRM APIs that never got moved to Ktime Signed-off-by: Lyude Paul Signed-off-by: Andreas Hindborg --- rust/kernel/time.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 379c0f5772e575c9ceacb9c85255b13501db8f30..f59e0fea79d3acfddd922f601f5= 69353609aeec1 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -8,6 +8,8 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). =20 +use core::convert::Into; + /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 =3D bindings::NSEC_PER_MSEC as i64; =20 @@ -63,6 +65,12 @@ pub fn to_ns(self) -> i64 { pub fn to_ms(self) -> i64 { self.divns_constant::() } + + /// Creates a new Ktime from the given duration in nanoseconds + #[inline] + pub fn from_ns(ns: impl Into) -> Self { + Self { inner: ns.into() } + } } =20 /// Returns the number of milliseconds between two ktimes. --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 1E4BF1C5484; Fri, 6 Dec 2024 19:36:05 +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=1733513766; cv=none; b=tzXffy8FiuneETDIx8KSBT+zf7asNBHtVKUvL1b1JCcwFQuJhWGqAyL/xHqJNJXQXOgXagLACCxE1yoRquHo9dRl6qPhvhPQeVTBK/tqwLr2EBUD0QplMBRVgdYmd+KguVghQ6SCrBA3wEsF66quMYp8ln96o4vVXRQCUydbnXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513766; c=relaxed/simple; bh=ublZLWv1E1rcxjkqUotDhS0XzBr2tkNHdg+FC/Wpxd0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=APtS3oFyeKWzf08hY63tuaY52rLFWoi4jp5sdOtNC1C5/TU3KcBm6sTLwW6hlI6/JaAunDkE5UmRuNWp5DQomzORZ3YS//cCF8iELqra8w8D5tsgwsB740Dy37jqlLZxom6xvgwkUsu4IJULtWlJYPh8vyqXh1HwJ+ybY55eSBQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oBnPsseE; 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="oBnPsseE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 614BCC4CED1; Fri, 6 Dec 2024 19:36:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513765; bh=ublZLWv1E1rcxjkqUotDhS0XzBr2tkNHdg+FC/Wpxd0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oBnPsseExF0lvkduzs5abODLrWSpqbAW4anugmIyRd2XN04dseQOxoD+ZkMZsRvkH XKUIVme0TMOCIGNMI2MK37SSN2OYrjs7zqbFiCh091SwsR7wt8zzdn/FWLUvzeWJ5/ MMETgH1yLPfuyDx342Uv/4wA+JYmJfc+DE51FZdecBe4mENgYNcYhz51CNozh8kOuH AzozeOx+TMIfafj9Jxm1FPURlHME9PfnREZZNzD+8R14i7siWMbdXF1i9s4sgC22Az WxbswO7UpSGBBDdeOQFLkj4g5mCLohIt6C2YELRqHfspZaObcjWR9ibNHeM8qEmq/3 MgzdDDGAd8grw== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:32:54 +0100 Subject: [PATCH v4 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: <20241206-hrtimer-v3-v6-12-rc2-v4-2-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=12285; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=ublZLWv1E1rcxjkqUotDhS0XzBr2tkNHdg+FC/Wpxd0=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5Wm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NcFNRd1VQZGVTa0VoaTgxM1dsSUkxZUdPCmhLMTA5UmVhU0g0bjNBPT0KPU1kOEMKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= 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 | 296 ++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 298 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index f59e0fea79d3acfddd922f601f569353609aeec1..51c3532eee0184495ed5b7d7178= 60c9980ff2a43 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..646cd5bfa6623aec62294f9115f= bf6fedc0a426c --- /dev/null +++ b/rust/kernel/time/hrtimer.rs @@ -0,0 +1,296 @@ +// 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 +//! +//! 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 **cancelled** before it's handler = is +//! executed. A timer that is cancelled enters the **stopped** state. +//! +//! States: +//! +//! * Stopped +//! * Running +//! +//! Operations: +//! +//! * Start +//! * Cancel +//! * Stop +//! * Restart +//! +//! Events: +//! +//! * Expire + +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 Timer { + #[pin] + timer: Opaque, + _t: PhantomData, +} + +// SAFETY: A `Timer` can be moved to other threads and used/dropped from t= here. +unsafe impl Send for Timer {} + +// SAFETY: Timer operations are locked on C side, so it is safe to operate= on a +// timer from multiple threads +unsafe impl Sync for Timer {} + +impl Timer { + /// Return an initializer for a new timer instance. + pub fn new() -> impl PinInit + where + T: TimerCallback, + { + 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 = live + // allocation. hrtimer_setup will initialize `place` and d= oes + // 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. + /// + /// # 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 `Timer` s= ize. + let c_timer_ptr =3D unsafe { Timer::raw_get(self_ptr) }; + + // If handler is running, this will wait for handler to finish bef= ore + // 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 [`Time= r`]. +/// +/// Typical implementers would be [`Box`], [`Arc`], [`ARef`] wher= e `T` +/// has a field of type `Timer`. +/// +/// Target must be [`Sync`] because timer callbacks happen in another thre= ad of +/// execution (hard or soft interrupt context). +/// +/// Starting a timer returns a [`TimerHandle`] that can be used to manipul= ate +/// the timer. Note that it is OK to call the start function repeatedly, a= nd +/// that more than one [`TimerHandle`] associated with a `TimerPointer` may +/// exist. A timer can be manipulated through any of the handles, and a ha= ndle +/// may represent a cancelled timer. +/// +/// [`Box`]: Box +/// [`Arc`]: crate::sync::Arc +/// [`ARef`]: crate::types::ARef +pub trait TimerPointer: Sync + Sized { + /// A handle representing a running timer. + /// + /// If the timer is running or if the timer callback is executing when= the + /// handle is dropped, the drop method of `TimerHandle` should not ret= urn + /// until the timer is stopped and the callback has completed. + /// + /// Note: It must be safe to leak the handle. + type TimerHandle: TimerHandle; + + /// 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 [`TimerPointer`] implementers to give the C timer callb= ack a +/// function to call. +// This is split from `TimerPointer` to make it easier to specify trait bo= unds. +pub trait RawTimerCallback { + /// 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 the target of a timer callback. +pub trait TimerCallback { + /// The type that was used for starting the timer. + type CallbackTarget<'a>: RawTimerCallback; + + /// 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 TimerHandle { + /// 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 [`impl_has_timer`] macro. +/// +/// # Safety +/// +/// Implementers of this trait must ensure that the implementer has a [`Ti= mer`] +/// 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 HasTimer { + /// Offset of the [`Timer`] field within `Self` + const OFFSET: usize; + + /// Return a pointer to the [`Timer`] within `Self`. + /// + /// # Safety + /// + /// `ptr` must point to a valid struct of type `Self`. + unsafe fn raw_get_timer(ptr: *const Self) -> *const Timer { + // SAFETY: By the safety requirement of this trait, the trait + // implementor will have a `Timer` field at the specified offset. + unsafe { ptr.cast::().add(Self::OFFSET).cast::>() } + } + + /// Return a pointer to the struct that is embedding the [`Timer`] poi= nted + /// to by `ptr`. + /// + /// # Safety + /// + /// `ptr` must point to a [`Timer`] field in a struct of type `Self= `. + unsafe fn timer_container_of(ptr: *mut Timer) -> *mut Self + where + Self: Sized, + { + // SAFETY: By the safety requirement of this function and the `Has= Timer` + // 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 `Timer` s= ize. + unsafe { Timer::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 [`HasTimer`] trait. +/// +/// See [`module`] documentation for an example. +/// +/// [`module`]: crate::time::hrtimer +#[macro_export] +macro_rules! impl_has_timer { + ( + impl$({$($generics:tt)*})? + HasTimer<$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::HasTimer<$ti= mer_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::Timer<$timer_type> + { + // SAFETY: The caller promises that the pointer is not dan= gling. + unsafe { + ::core::ptr::addr_of!((*ptr).$field) + } + } + } + } +} --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 134DF192B76; Fri, 6 Dec 2024 19:35:05 +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=1733513707; cv=none; b=Fpji0XtzwRVQGh3wxQE4rWBuOUmM/veXu97WxGGIUF6kqUM+usV+I2UUCfqmaZ2TuO29nVK0GVLXG63VgH2el6dKGEuuZQndglOuylkaTkk76MPQ4Xqm8/4jxl0LEoKDkfeDsL6iEsiP+p9JDGhnsP2hRlAsWs6mTp/tm6DLNu8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513707; c=relaxed/simple; bh=lDPl01izmHRdpX/4BbDdhKpVVoE+jqsdSqSjquB7akA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lRAyBq0YJhJ/2RhxxhtTGwyJUq+Ol56SJjeJVk8YjNB+cRZwgK+apYmNC9xWbfika0bpV1smfyx2boGfhR9lVCBQ3NHNFtSSTLsj4ihD5qXBh5Km0dUJHar1+4xONxn62jYQIoeeqowE24LVCUui04+Ng2keclPzmpj5Xmwv5aU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uD4jVU35; 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="uD4jVU35" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 009B0C4CED1; Fri, 6 Dec 2024 19:35:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513705; bh=lDPl01izmHRdpX/4BbDdhKpVVoE+jqsdSqSjquB7akA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=uD4jVU35ESf/s1A+zsxTylVGmnky49Vg9Cf9lMerF58jOsCkJu0MPqKeL7Dm8a5/w uSM0o0a8vmT8LHeNHnHn3vhZ0BpnhWP3vARhpCge07jYgnBfha/BNWKTVKZOeBlGVI PdrKPspTCPgroBPaJNL/Ih694+WTOKpWbl4kAUEhDY6egUltvZOf7/eHYvPT2hWYSb qBRxZ11BZ/6NUGdAhD271tD7DIfnPfcAz9HOFN0f87YmzRiL2nnop+jJleUN137XsY VAgh52kogyv7El4bS0LO+80YWxUYsWO3yFO9bP33C/J//BaTqai3UXPaSWgP8WcIR5 MZxekVXMPEBhQ== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:32:55 +0100 Subject: [PATCH v4 03/14] rust: sync: add `Arc::as_ptr` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-3-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1772; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=lDPl01izmHRdpX/4BbDdhKpVVoE+jqsdSqSjquB7akA=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVdzZWRubjNRZlNIRk45cnNLcFpKbHpFClRuVmtXY0FGQWNYRVN0bmNJ dUdld0lrQ013UUFBUW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFFdUc0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; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a method to get a pointer to the data contained in an `Arc`. Signed-off-by: Andreas Hindborg --- rust/kernel/sync/arc.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index fa4509406ee909ca0677b78d5ece966089ce6366..3d6111ddb007285b26eca2177a4= 12033f4ac5dcb 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -233,6 +233,15 @@ pub fn into_raw(self) -> *const T { unsafe { core::ptr::addr_of!((*ptr).data) } } =20 + /// Return a raw pointer to the data in this arc. + pub fn as_ptr(this: &Self) -> *const T { + let ptr =3D this.ptr.as_ptr(); + + // SAFETY: As `ptr` points to a valid allocation of type `ArcInner= `, + // field projection to `data`is within bounds of the allocation. + unsafe { core::ptr::addr_of!((*ptr).data) } + } + /// Recreates an [`Arc`] instance previously deconstructed via [`Arc::= into_raw`]. /// /// # Safety @@ -508,11 +517,11 @@ unsafe fn new(inner: NonNull>) -> Self { } =20 /// Creates an [`ArcBorrow`] to an [`Arc`] that has previously been de= constructed with - /// [`Arc::into_raw`]. + /// [`Arc::into_raw`] or [`Arc::as_ptr`]. /// /// # Safety /// - /// * The provided pointer must originate from a call to [`Arc::into_r= aw`]. + /// * The provided pointer must originate from a call to [`Arc::into_r= aw`] or [`Arc::as_ptr`]. /// * For the duration of the lifetime annotated on this `ArcBorrow`, = the reference count must /// not hit zero. /// * For the duration of the lifetime annotated on this `ArcBorrow`, = there must not be a --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 AB6CC1DE2D8; Fri, 6 Dec 2024 19:35:32 +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=1733513732; cv=none; b=BVDsVSehzSVj/M7fuMYRCHE2u4cM5NYVM2NI0RDxW9eD8bILU6ANcp2XfTpp+nBbtfMzqGznuQRZWueOUc5+2yhbtuoUGc3M8HLGoDCgCcNBJXxQ+d5sYm3goLHGRqIOoMxXMPzMP7/oVrTNoEt+HvDKjg9jfszu1AZc4/m0IxQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513732; c=relaxed/simple; bh=6WgaFtCFG45ZPn9cC//15Oy/uIkYY9hKr/2/l/u9DGU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mOUAfQZNgbZYxGww+8+eChcbbLgZ2WuAKyy8GfMdWwxkaeGqbKrr0+uBRdQrXAUC8lGmfg8t+HjPU9K53brIC1fbwxwXYV//0ROZexX5dxOHlvM1tS4FELqP027hHkxIJlARvzTnZD/gWWLwPP/Eb46ozD2fwqSxw0N3Q8e24ZI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WiOrae7C; 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="WiOrae7C" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83BAAC4CED1; Fri, 6 Dec 2024 19:35:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513732; bh=6WgaFtCFG45ZPn9cC//15Oy/uIkYY9hKr/2/l/u9DGU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WiOrae7CZbIPyeN02W+XFTSOotrv5Gx2ifXzaNV2mwLShHRiY8bjUtQwnyv5T8eHm AvzeEntjOrHq2hcaZ7dhO+IEAnCNd5KsQbX/HXhQA0waYQHspE4d4Id+t2L6FE/XrE fh8UeTiM8JcABN7uZZJNYbyC3fFqYmNW0N4JuuyMzkzWbIHjveNXToYOMT9fSGOtoK UmIarGB3DSb8iyz5AUb5A8Qr6vD7+rNIOacJ3gElAM8KjrOMFD/lK1N0zwlldLln5I GAEq8ZpjGx7vAwF2Dcug2yKLF7m0nqhmkfIFT1lZRUhgHgRH3n0A6ufEepqtWXq+BK MzdU/aMWEWaiw== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:32:56 +0100 Subject: [PATCH v4 04/14] rust: hrtimer: implement `TimerPointer` for `Arc` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-4-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4083; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=6WgaFtCFG45ZPn9cC//15Oy/uIkYY9hKr/2/l/u9DGU=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVdzbEVRdG96QUdBTmdwQUNQNHdaOGp5CnB2SDF0QTRNQl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LS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 This patch allows the use of intrusive `hrtimer` fields in structs that are managed by an `Arc`. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 3 +- rust/kernel/time/hrtimer/arc.rs | 89 +++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 646cd5bfa6623aec62294f9115fbf6fedc0a426c..e76b864f3435667f56ed607fd3b= f029317eb1332 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -98,7 +98,6 @@ unsafe fn raw_get(ptr: *const Self) -> *mut bindings::hrt= imer { /// # 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 `Timer` s= ize. let c_timer_ptr =3D unsafe { Timer::raw_get(self_ptr) }; @@ -294,3 +293,5 @@ unsafe fn raw_get_timer(ptr: *const Self) -> } } } + +mod arc; diff --git a/rust/kernel/time/hrtimer/arc.rs b/rust/kernel/time/hrtimer/arc= .rs new file mode 100644 index 0000000000000000000000000000000000000000..3b256292bc86160a495f7b192b3= eda92f2dfcb31 --- /dev/null +++ b/rust/kernel/time/hrtimer/arc.rs @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::HasTimer; +use super::RawTimerCallback; +use super::Timer; +use super::TimerCallback; +use super::TimerHandle; +use super::TimerPointer; +use crate::sync::Arc; +use crate::sync::ArcBorrow; +use crate::time::Ktime; + +/// A handle for an `Arc>` returned by a call to +/// [`TimerPointer::start`]. +pub struct ArcTimerHandle +where + U: HasTimer, +{ + pub(crate) inner: Arc, +} + +// SAFETY: We implement drop below, and we cancel the timer in the drop +// implementation. +unsafe impl TimerHandle for ArcTimerHandle +where + U: HasTimer, +{ + fn cancel(&mut self) -> bool { + let self_ptr =3D Arc::as_ptr(&self.inner); + + // SAFETY: As we obtained `self_ptr` from a valid reference above,= it + // must point to a valid `U`. + let timer_ptr =3D unsafe { >::raw_get_timer(self_= ptr) }; + + // SAFETY: As `timer_ptr` points into `U` and `U` is valid, `timer= _ptr` + // must point to a valid `Timer` instance. + unsafe { Timer::::raw_cancel(timer_ptr) } + } +} + +impl Drop for ArcTimerHandle +where + U: HasTimer, +{ + fn drop(&mut self) { + self.cancel(); + } +} + +impl TimerPointer for Arc +where + U: Send + Sync, + U: HasTimer, + U: for<'a> TimerCallback =3D Self>, +{ + type TimerHandle =3D ArcTimerHandle; + + fn start(self, expires: Ktime) -> ArcTimerHandle { + // SAFETY: Since we generate the pointer passed to `start` from a + // valid reference, it is a valid pointer. + unsafe { U::start(Arc::as_ptr(&self), expires) }; + + ArcTimerHandle { inner: self } + } +} + +impl RawTimerCallback for Arc +where + U: HasTimer, + U: for<'a> TimerCallback =3D Self>, + U: for<'a> TimerCallback =3D ArcBorrow<'a,= U>>, +{ + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrt= imer_restart { + // `Timer` is `repr(C)` + let timer_ptr =3D ptr.cast::>(); + + // SAFETY: By C API contract `ptr` is the pointer we passed when + // queuing the timer, so it is a `Timer` embedded in a `T`. + let data_ptr =3D unsafe { U::timer_container_of(timer_ptr) }; + + // SAFETY: `data_ptr` points to the `U` that was used to queue the + // timer. This `U` is contained in an `Arc`. + let receiver =3D unsafe { ArcBorrow::from_raw(data_ptr) }; + + U::run(receiver); + + bindings::hrtimer_restart_HRTIMER_NORESTART + } +} --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 51DD9192B76; Fri, 6 Dec 2024 19:35:27 +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=1733513728; cv=none; b=iD6Zx0tGB5hJWi963I+Te/axY0siuQG0C5jcW0ro3b+xssTLHGfPuTBQ0F9bRrujCtrpHWdNASfgfEWeweHExXCgq4PmtrWrtNEJkSEf4UzHTrTasX47HvI5ZQHuneOkgQqnYmEIpS1SLO8BaqAHl6ojZ0RUeWcJHOd158x1+6o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513728; c=relaxed/simple; bh=wVTamzZSe1kQ6JYXAaG0jogPsO5/dwiimgZfFD4yVyw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kRVYxrCmEajILKn5iLpyuVtQMG8KL3ZS71rngNISUhJvVQ0Ba4kuLEnXM7kYFTKzQwx7T2X0BxN/92op5wUvab0dNng/pANBJm0Yhlg1CBtfdPLFSGRWU99BF/ky0lnICBjWG9XAdnxUwG0y/GuPj6vh7sFLqlIvSs4xzEgNqw4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gAd9SOtB; 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="gAd9SOtB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32B2FC4CED1; Fri, 6 Dec 2024 19:35:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513727; bh=wVTamzZSe1kQ6JYXAaG0jogPsO5/dwiimgZfFD4yVyw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gAd9SOtBcJWoNPOzI8PpHba3W/7n6M+rNJ9aRsXjv6p7CI5jpL1BXD5LAsyT1PWGK ZJ9VN3VA8Hkcms5GdfReU5nHRtIDUVx+pvXGpsUhYHXPFMS29jXv0ITdOO5jSXUX0v g9KguKtp6zw34rfvW+JI+wM4uZQds7wpAqyPKsw+iQZgqh6kRPPwgFNwKJxdiRdAN0 nxbtFb0VAIhJ89irmf9/kfDOXu2IpgJ55zjUv2y9b0qHunrXe6IBGOCzSmYvE8vKVM wT+fPNW0az8l8A7lbrEI6dIob60zQkuUW/2+H8yVTWB2iIAeMP9VqoVd/kOgsLEKp8 b7eF2MeYqzVQg== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:32:57 +0100 Subject: [PATCH v4 05/14] rust: hrtimer: allow timer restart from timer handler 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: <20241206-hrtimer-v3-v6-12-rc2-v4-5-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2659; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=wVTamzZSe1kQ6JYXAaG0jogPsO5/dwiimgZfFD4yVyw=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2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owZ0l qd21nRDc0RzhJcWpRVlVqbStqcllNRXFobmhJVEh4Cm5sM0lWS0RnN0NJWHRRPT0KPURZSTQKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 This patch allows timer handlers to report that they want a timer to be restarted after the timer handler has finished executing. Also update the `hrtimer` documentation to showcase the new feature. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 37 ++++++++++++++++++++++++++++++++++++- rust/kernel/time/hrtimer/arc.rs | 4 +--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index e76b864f3435667f56ed607fd3bf029317eb1332..632b28a8a6338e2841c59412f22= 268ff86410a8f 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -167,7 +167,7 @@ pub trait TimerCallback { type CallbackTargetParameter<'a>; =20 /// Called by the timer logic when the timer fires. - fn run(this: Self::CallbackTargetParameter<'_>) + fn run(this: Self::CallbackTargetParameter<'_>) -> TimerRestart where Self: Sized; } @@ -262,6 +262,41 @@ unsafe fn start(self_ptr: *const Self, expires: Ktime)= { } } =20 +/// Restart policy for timers. +pub enum TimerRestart { + /// Timer should not be restarted. + NoRestart, + /// Timer should be restarted. + Restart, +} + +impl From for TimerRestart { + fn from(value: u32) -> Self { + match value { + 0 =3D> Self::NoRestart, + _ =3D> Self::Restart, + } + } +} + +impl From for TimerRestart { + fn from(value: i32) -> Self { + match value { + 0 =3D> Self::NoRestart, + _ =3D> Self::Restart, + } + } +} + +impl From for bindings::hrtimer_restart { + fn from(value: TimerRestart) -> Self { + match value { + TimerRestart::NoRestart =3D> bindings::hrtimer_restart_HRTIMER= _NORESTART, + TimerRestart::Restart =3D> bindings::hrtimer_restart_HRTIMER_R= ESTART, + } + } +} + /// Use to implement the [`HasTimer`] trait. /// /// See [`module`] documentation for an example. diff --git a/rust/kernel/time/hrtimer/arc.rs b/rust/kernel/time/hrtimer/arc= .rs index 3b256292bc86160a495f7b192b3eda92f2dfcb31..1711b41ab32aef0c6f5263eab38= bc61d6c2a189c 100644 --- a/rust/kernel/time/hrtimer/arc.rs +++ b/rust/kernel/time/hrtimer/arc.rs @@ -82,8 +82,6 @@ impl RawTimerCallback for Arc // timer. This `U` is contained in an `Arc`. let receiver =3D unsafe { ArcBorrow::from_raw(data_ptr) }; =20 - U::run(receiver); - - bindings::hrtimer_restart_HRTIMER_NORESTART + U::run(receiver).into() } } --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 5E48D20CCE4; Fri, 6 Dec 2024 19:35:57 +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=1733513757; cv=none; b=WWNdFfWTcHi4ECPuq8uhHNwKdAlt59gzhPnM/xY8uEr3+VE6EPe46OYNcJu5LKaC/H4Q1zittOHHYxzS6OuLmBJZXpq+ERuYRhI88cK6PI5uXofA8ZUlH/utwj3XPLi0EL0nfnZRRediK6TZfU16VGwWBeuReFg/iNJo8/X9+fY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513757; c=relaxed/simple; bh=YPhq18Bquk8BuL/sZQan8btRfhn1YbXyifWT40yfIzQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KlyZRhJuZQiKGBzey8/U6CpEAD7dDa7EXDroSgupC0tDc/pmekXKGY+O5YSE8uwpoVf2rFkWdYFOiLgt1yFWSYsoFpB1cKlfvvUwDo0wIdjZNUXCjePk4fgD7g1RsCZS3GT0MT5V4bDnelLN029+rFd0tv9wD64VaDzn12igVw4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HcuPaAQU; 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="HcuPaAQU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77867C4CEDD; Fri, 6 Dec 2024 19:35:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513757; bh=YPhq18Bquk8BuL/sZQan8btRfhn1YbXyifWT40yfIzQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=HcuPaAQUjUXeIy/Kw+o+9DNdR8nJRw5NYO5NqWu3ZbWsKzHECnGldFbGOvMJivl70 20mPoPMBSjxeINpXR/o/io3t5MqHhe1bF8DdPJhmGPD927ooeEd7cG1d4W64Zdl793 RLv1FU28Uj3CLOPklAhiOpERFrzntO/wWykaz0Zwp0VC3TaffsPgnm83WLPwM8D+1x NShe2AHgtZf0Q5yQLFZC/0+gNZ6Snj0nf89IQlpqJWPPWoE68YA+nl2zdjlUDx6s7T 548K31mFiXKwg5WqxAFC4C7Rta4sPKLA2bfZABgbHp4etRVDAWmxQKRWsfZrJTFHz0 xQEJJpt9dQ+ng== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:32:58 +0100 Subject: [PATCH v4 06/14] rust: hrtimer: add `UnsafeTimerPointer` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-6-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2124; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=YPhq18Bquk8BuL/sZQan8btRfhn1YbXyifWT40yfIzQ=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVcwR1NQaE11cEZsdGVpeGtJYjkycElyClNOK01wQ216UWtjOXZZUWtP dzVGdDRrQ013UUFBUW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFFdUc0R2o1NUtHTjMKQlFKblUxRnR BQW9KRU9HNEdqNTVLR04zM1NBUCt3YlRUTFE5WnZyUTI3V211MzYzNDJIcU0vRUxmZUM5MzBaTA pHRnl1MHM1VDdjSmpFemdtSy94NXU3akNHRGZTMHVkUnNoU3hDR0I5cnQ3MUx5TlN2Z0J0eXdiK 29IdHpyUEYvCnUrKzBYTGZqODhlWlNBb1hRNFl0MHRpbjlGOUdJY0ozYUhKVzh1ejZIWlU5TStr UnpEVEp4Z0xONFFVaGRUQlUKYXVLaTlWc1FYczJWTzg1ZHJxYlZGS3NsNGtuSndTU3NsYmNUbWJ Ka0RQWkR6RW4zeGZqV3UyVjgyc1NIc2t6WAp4b0x0NlY1ZHZpWGJJTEUvUjZ6MzRXY1FKMjBHSn JtZ1p5QXEwUDFWc1JndjE3akR1aktxd1BwN21JU0RBWElPCk1xek9sN0I5Q1VhRW5SWm1LOGlOd TByQlhndllFTHJDUTNPMnFubkcva3orakFnZUI4b0Y2KzlzdWVTakJBdFEKWURYUmwwT1M3L0JQ d002QWpMa00xNlZSV212SjdBL1poM2tMa0N0Q1JzMVMyNDIybEhtQjB4bVlrTXRNQnI1MgowQnN KVGdRcWFhR2Y3RW5POUxQYjdxY3BQVEVtcHkzOG5Kd3Z2VW9UOXFtdFdxdGFjbndlalJUa3dOYm 1lbHU2CkltcCs4blNaWlVEcDBkQ0hrSkNjWExQOTdhem02M3RacEoweWV0TnZZSUpvM05CQUVlY kw3aXdTK3dvR2U1cWgKbENxTWhNQjh5VVBDSkNBQTNOTzgzUWViS3YwZmJzRk9LS3U4eHJ0ZEly QVIySUc0eC9GTUVlTW5OMzU1T3F1QwpicUF6VmpPVjdvUmlWQjRwTnNIeTFmZkFscEk4UWhpcyt lTTBJSjF6REpPWUkxWDQxb2VCUW1SL2h5bjVUUXhWCmNXMk9MeHZmb01ZeHZBPT0KPXV5RWEKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a trait to allow unsafely queuing stack allocated timers. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 632b28a8a6338e2841c59412f22268ff86410a8f..fab9bf2612ceaa1ce936ab9b9bc= 31b018a1a9aef 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -142,6 +142,39 @@ pub trait TimerPointer: Sync + Sized { fn start(self, expires: Ktime) -> Self::TimerHandle; } =20 +/// Unsafe version of [`TimerPointer`] for situations where leaking the +/// `TimerHandle` returned by `start` would be unsound. This is the case f= or +/// stack allocated timers. +/// +/// Typical implementers are pinned references such as [`Pin<&T>`]. +/// +/// # Safety +/// +/// Implementers of this trait must ensure that instances of types impleme= nting +/// [`UnsafeTimerPointer`] outlives any associated [`TimerPointer::TimerHa= ndle`] +/// instances. +/// +/// [`Pin<&T>`]: Box +pub unsafe trait UnsafeTimerPointer: Sync + Sized { + /// A handle representing a running timer. + /// + /// # Safety + /// + /// If the timer is running, or if the timer callback is executing whe= n the + /// handle is dropped, the drop method of `TimerHandle` must not return + /// until the timer is stopped and the callback has completed. + type TimerHandle: TimerHandle; + + /// Start the timer after `expires` time units. If the timer was alrea= dy + /// running, it is restarted at the new expiry time. + /// + /// # Safety + /// + /// Caller promises keep the timer structure alive until the timer is = dead. + /// Caller can ensure this by not leaking the returned `Self::TimerHan= dle`. + unsafe fn start(self, expires: Ktime) -> Self::TimerHandle; +} + /// Implemented by [`TimerPointer`] implementers to give the C timer callb= ack a /// function to call. // This is split from `TimerPointer` to make it easier to specify trait bo= unds. --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 A4AFD21B1A0; Fri, 6 Dec 2024 19:35:48 +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=1733513748; cv=none; b=TRol8bp0oLOHgOkK4bkrHihu+a2pxNO8WwQFxuy0AFpMpTHaG8FRoSYHuNjW3u3aRfG84LCiUblPKDHfR+J30ghgp6mJe11izqr/OdzZO0JKApo1NXKNS1P3H1lQ1aL2ovohS5cjiW/IDIYNVYxX9cemoZcykK0kkVvClP+aKkQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513748; c=relaxed/simple; bh=Gn70I9pN0f4TEQ9qvGFsjodYORtSobyj7JiWCADhVpQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UNimuNwtHoZM0m48pB34A7huFjYuXAn7JIpyDPLKFQ7sAHUO7TJ2Rtp7yb4r8lEAdoLWr5UE72mARWoSom/CQVrtouVATjsQaTpu5mwJXc2M698FoEJgMTb6KpZEMdeLjU5uLgOzfJDpzo4eFzZv4XnSzCqF9nxgqcgwyAm0WP4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Qi7KP1mX; 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="Qi7KP1mX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25E3BC4CEDD; Fri, 6 Dec 2024 19:35:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513748; bh=Gn70I9pN0f4TEQ9qvGFsjodYORtSobyj7JiWCADhVpQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Qi7KP1mXsJgXDv6bjK/FjXYDXeJqMuftfSfxZSgNAJLGlKciBnDHR+is3p+YGE2B0 dtxi4ixXZngPyEECp16qWE3d7WVkiu86ALDF2L8e87heOjAq7PT3Zf/wbgcnoN/Ako exPQv/MU0PRuhR9lp7L9I1L4buWWymM2RWQ4tEsDN4FSY4NdpOWcW8JSB4bOGnrVUN XZR63NkUTTYQmuI3OXwKyEwwvBexY6sl3gY/h8wYqnc5mav3b7mt7J0Fb2mTZHC4QY 25V44hlEVF3jfsnA0bPBr6cOvqcD26eXLsjva9KJktNfDpfXRPtTGxKr0JzJ8lig2S TszOn9RygGhZg== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:32:59 +0100 Subject: [PATCH v4 07/14] rust: hrtimer: implement `UnsafeTimerPointer` for `Pin<&T>` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-7-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4022; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Gn70I9pN0f4TEQ9qvGFsjodYORtSobyj7JiWCADhVpQ=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVc1RjFNa0NZWDlnb2lvU252M3JVUWtFCkZBdDNMeDNtWCtDYVgwdThM UzVFSW9rQ013UUFBUW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFFdUc0R2o1NUtHTjMKQlFKblUxRnV BQW9KRU9HNEdqNTVLR04zS2ZRUC9qT21DcG9aMEFTSFRneHpSQkxWdTNDR09ubTNKYk1BR1N2Uw p5czBublZIbXlyLzZrVVowUEloSVZ3U1V4ajBGb2FMdU9LbitndHNuK3JSN094RzlIS05FUHRVO FhvRHVTVy9kClc1Q3BTTVpEOGJDdHJzTnkwa3BqUXJVSmg1bGdKTWRuSlk4c2hMQm9WWmVENG9p RzNLaXBJNFlmRjJBdWE5WHAKWXRKZ21ZMUF1N2xpaUd1bUV4U0toM2U3U3U1b1dwSDZFRnFsMWV PbE9EdlFWWkpJMHErY1JwK2loQzNYTTFKYwo3aHBMSk1LK3VjNWZSU3ZMM1B5dUVsMVBhL2VIOT d4YkhRTTZvR05kZ0drQ3UrZkYwSWRGWWNDUERoYm9nQ1VhCm5TbUMvZFlQNE5XUnMyNzM5OXk1T GJTcytWMnlzMWxGdUZnVkFOTk1PaTBGZmN0TjZBR2pheUFKcE5FMmFqbXAKcHpBT0lpOEpVNjll VEpMZlNRYUc4ZllZMXRiUUtWb1hUYXlQZ3Q5K3ZOK1hETEFLWXZTN2I4Y3ZzRTA5Z1ZqQwptNnN 0ZVdsNFFNbzErUWFwVHloU2VIcG8xdk5zVUFsZjdJSXhUUG1hK0pUcmpVTmx4Z0xjdDFXYU1EMF Z6cmhaCnBNeWFMMm5nUSt2M1QvY2xrTzhDTHk1YW1ONDdxbFBFSytySGZKL1JMUzRaZmZCODdIM 3N3MHUwM0tLS0x1SkcKMlNmM0xTbWsxZGQzZVBhVU04MmtFUzJxR1IzSFViWWwvbGFxMVA3N3JX WGp3VG5lUUEvUXZLUWJCVG5FVFlmUwpIYzBTbUFueXBKYzJzNGdEYmw5bWZtdkE4MDNMYndQMlE wWHROUzNmbGQ5akhxOUY2QVlKWDRRVDRPMWlvZkhlCmxjbHZTa0RqZnhrbFR3PT0KPUxUUUoKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow pinned references to structs that contain a `Timer` node to be scheduled with the `hrtimer` subsystem. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 1 + rust/kernel/time/hrtimer/pin.rs | 95 +++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 96 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index fab9bf2612ceaa1ce936ab9b9bc31b018a1a9aef..ad02e023b78062b368af1849063= d830ffba5f29b 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -363,3 +363,4 @@ unsafe fn raw_get_timer(ptr: *const Self) -> } =20 mod arc; +mod pin; diff --git a/rust/kernel/time/hrtimer/pin.rs b/rust/kernel/time/hrtimer/pin= .rs new file mode 100644 index 0000000000000000000000000000000000000000..7b251f157b2c196a3bde220b126= 19ce0c0ecce6c --- /dev/null +++ b/rust/kernel/time/hrtimer/pin.rs @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::HasTimer; +use super::RawTimerCallback; +use super::Timer; +use super::TimerCallback; +use super::TimerHandle; +use super::UnsafeTimerPointer; +use crate::time::Ktime; +use core::pin::Pin; + +/// A handle for a `Pin<&HasTimer>`. When the handle exists, the timer mig= ht be +/// running. +pub struct PinTimerHandle<'a, U> +where + U: HasTimer, +{ + pub(crate) inner: Pin<&'a U>, +} + +// SAFETY: We cancel the timer when the handle is dropped. The implementat= ion of +// the `cancel` method will block if the timer handler is running. +unsafe impl<'a, U> TimerHandle for PinTimerHandle<'a, U> +where + U: HasTimer, +{ + fn cancel(&mut self) -> bool { + let self_ptr: *const U =3D self.inner.get_ref(); + + // SAFETY: As we got `self_ptr` from a reference above, it must po= int to + // a valid `U`. + let timer_ptr =3D unsafe { >::raw_get_timer(self_= ptr) }; + + // SAFETY: As `timer_ptr` is derived from a reference, it must poi= nt to + // a valid and initialized `Timer`. + unsafe { Timer::::raw_cancel(timer_ptr) } + } +} + +impl<'a, U> Drop for PinTimerHandle<'a, U> +where + U: HasTimer, +{ + fn drop(&mut self) { + self.cancel(); + } +} + +// SAFETY: We capture the lifetime of `Self` when we create a `PinTimerHan= dle`, +// so `Self` will outlive the handle. +unsafe impl<'a, U> UnsafeTimerPointer for Pin<&'a U> +where + U: Send + Sync, + U: HasTimer, + U: TimerCallback =3D Self>, +{ + type TimerHandle =3D PinTimerHandle<'a, U>; + + unsafe fn start(self, expires: Ktime) -> Self::TimerHandle { + // Cast to pointer + let self_ptr: *const U =3D ::deref(&self= ); + + // SAFETY: As we derive `self_ptr` from a reference above, it must= point + // to a valid `U`. + unsafe { U::start(self_ptr, expires) }; + + PinTimerHandle { inner: self } + } +} + +impl<'a, U> RawTimerCallback for Pin<&'a U> +where + U: HasTimer, + U: TimerCallback =3D Self>, + U: TimerCallback =3D Self>, +{ + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrt= imer_restart { + // `Timer` is `repr(C)` + let timer_ptr =3D ptr as *mut Timer; + + // SAFETY: By the safety requirement of this function, `timer_ptr` + // points to a `Timer` contained in an `U`. + let receiver_ptr =3D unsafe { U::timer_container_of(timer_ptr) }; + + // SAFETY: By the safety requirement of this function, `timer_ptr` + // points to a `Timer` contained in an `U`. + let receiver_ref =3D unsafe { &*receiver_ptr }; + + // SAFETY: `receiver_ref` only exists as pinned, so it is safe to = pin it + // here. + let receiver_pin =3D unsafe { Pin::new_unchecked(receiver_ref) }; + + U::run(receiver_pin).into() + } +} --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 CFBF31DE2D8; Fri, 6 Dec 2024 19:35:40 +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=1733513740; cv=none; b=stiqrRsedvPwT5JPRa4LzsCoPanrg6+iqxoIuDZeqVFZQbaOoX5j/APusYJOasOSQLeE2V7e2t9OQVmEz6XHFfVzKCG5DL9NSVt41aKB24ZX0GQKjjYMTOLsqUkaOm4cvreUFE2BhZnchblYr+rvAnkXgQLq2DhaW7v6H7s0Cys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513740; c=relaxed/simple; bh=hgqYHLie9pxbgzFDmV4u1qaSr5gb7nx65glYblRhSjQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YesBxbiGnjdZYRfYI9Jog1eRrFUS276IpJRUZ4S8TVJjHtjFubWJq1KLwgmd8LEUGF9UUaON5K6oCvZETVNn3iMgCeMvPe3pDnvW6QvLfmxBMEu5wgnEcrpe0ozoIJwh7eSICRKfGcaInOCkEfcxe2L+1RZ25XWyUdFDULWEYGE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pIMoDm+R; 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="pIMoDm+R" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC746C4CED1; Fri, 6 Dec 2024 19:35:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513740; bh=hgqYHLie9pxbgzFDmV4u1qaSr5gb7nx65glYblRhSjQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pIMoDm+RoXqEGz7M1B/l2ERbVNEE1HRauVU0hK7We0uC1iz17nnnjlSgey+E+LrmK +Wp7ZwM+AGBTxKRx0xLmgXl8OlyEs4qPpDNkhro3jn3WUO6sVnBga2GjLNjqAiMxpr Dsj5MB9J8BhPHVU9y0gsnJHtlxc1vUz6jsSvi2AbSjeSIgFUYqM1E8lBCNg9pC+yNS Pp3Qp0H3wlTIUjoq8wzz/z+C/5H6Bt19YrWdQ2qu5fxyUqXFZABuggZRmzM/xzKezs /UoDwzQpM0TOQ5t1osGtIMpsl6JeFMOCFoJ7Q+4htJSLI1WLMViyPGUEI/Pt91jsTq uQHdlV/8PZL1g== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:33:00 +0100 Subject: [PATCH v4 08/14] rust: hrtimer: implement `UnsafeTimerPointer` for `Pin<&mut T>` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-8-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4236; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=hgqYHLie9pxbgzFDmV4u1qaSr5gb7nx65glYblRhSjQ=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVcray93Vm9YUzI4Y0JzYUtieDl4NXV4CnVsZzY0N1FzVW1xR05FQWEx cXJXcW9rQ013UUFBUW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFFdUc0R2o1NUtHTjMKQlFKblUxRnZ BQW9KRU9HNEdqNTVLR04zYkQ4UUFJbEtFdEVKMEtpYlVaMHYzZUt5NG8wUGNzRmlKaVh0bjBlMQ pFMXc3TE9mcUdHdm5NQ21XeUFoellneStocEFHUkJnM3hQWW5zOXVNZW5vNHgxaGU3dlBUK1pVe Gk5MmVjMWZhCkJJQjFSNWx2WnJqYU1zRWIxZC9QYU1NVHJTcW5zamtYTTE3Q09adHI1Q2tvUUdh dUo3WUIyM09WL0tYWHhrQ3gKOXpjWlZ1STBzZkI4TFlIUnlQQUdGd1RjS1JEQ09PaVovdjlpS0t 6S2tFc3dUZ3RpZll4SC9iVDVDaklXdnd2SAoxV3VoZUluT2RFbFFWbmlsSFpIUDR3N2MycjZyeG owd3BOSjVLZjRNWkZzdDFZUlczbXkwT0lzZ2RnL2ZOTHgwCjJYSWRxUTM0N2dXMDk5MG1LUys1Y 0xyK3ZTV0hmbVduZDNuVVhmZlJDZ0xBMm01MDhyQjdyNzM0dXNBVG5ObXQKSGxSVWlZTG1lb2J0 d2Y1VUU3WDhqNFB3VjU0M3oraENRdERuaVdWd2dodE1GemdrRzNxMDVBVnJoWlFUQmxOeAppZzl VZTY5UnN5M1oxYkFQS2lzMy9DTTh4Wk9GaWt4eW9id2NnbnJ5OVdrU3pRejFFemxKUmgyMVdHQW JPTlBDCmdOempBblUwODVBWmE5a1JVL2toY2pyUFIvaVQzeVdtODFkWkdNSXBHdHk5YmduZkRKc U9uK3V5WkVGVFhLcS8KcjBUYy9waHdUSHozVzZuREFsaVBRYzBwUklVeEVUaWczQ3VJNnhXeGNE T0d4THBRdUxBQnBUVDN5T2VKdkNxNgozbGF5a1ZxUXJma1g0YWF0OStnUmlFUWd3S3VzR0hLcGI zQ1RwTFk4WkloUXZWemNOd1lMSGFhRTNIeEVkVTlRCkpBVndMV0wrM3RJbGpnPT0KPTg4RmQKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow pinned mutable references to structs that contain a `Timer` node to be scheduled with the `hrtimer` subsystem. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 1 + rust/kernel/time/hrtimer/pin_mut.rs | 97 +++++++++++++++++++++++++++++++++= ++++ 2 files changed, 98 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index ad02e023b78062b368af1849063d830ffba5f29b..efcb5e44a0dc3660a09f230c01c= ca998b763e8d9 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -364,3 +364,4 @@ unsafe fn raw_get_timer(ptr: *const Self) -> =20 mod arc; mod pin; +mod pin_mut; diff --git a/rust/kernel/time/hrtimer/pin_mut.rs b/rust/kernel/time/hrtimer= /pin_mut.rs new file mode 100644 index 0000000000000000000000000000000000000000..50ba9109919838aa9b09381000b= ab4ab8d3a2340 --- /dev/null +++ b/rust/kernel/time/hrtimer/pin_mut.rs @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::HasTimer; +use super::RawTimerCallback; +use super::Timer; +use super::TimerCallback; +use super::TimerHandle; +use super::UnsafeTimerPointer; +use crate::time::Ktime; +use core::pin::Pin; + +/// A handle for a `Pin<&mut HasTimer>`. When the handle exists, the timer= might +/// be running. +pub struct PinMutTimerHandle<'a, U> +where + U: HasTimer, +{ + pub(crate) inner: Pin<&'a mut U>, +} + +// SAFETY: We cancel the timer when the handle is dropped. The implementat= ion of +// the `cancel` method will block if the timer handler is running. +unsafe impl<'a, U> TimerHandle for PinMutTimerHandle<'a, U> +where + U: HasTimer, +{ + fn cancel(&mut self) -> bool { + // SAFETY: We are not moving out of `self` or handing out mutable + // references to `self`. + let self_ptr =3D unsafe { self.inner.as_mut().get_unchecked_mut() = as *mut U }; + + // SAFETY: As we got `self_ptr` from a reference above, it must po= int to + // a valid `U`. + let timer_ptr =3D unsafe { >::raw_get_timer(self_= ptr) }; + + // SAFETY: As `timer_ptr` is derived from a reference, it must poi= nt to + // a valid and initialized `Timer`. + unsafe { Timer::::raw_cancel(timer_ptr) } + } +} + +impl<'a, U> Drop for PinMutTimerHandle<'a, U> +where + U: HasTimer, +{ + fn drop(&mut self) { + self.cancel(); + } +} + +// SAFETY: We capture the lifetime of `Self` when we create a +// `PinMutTimerHandle`, so `Self` will outlive the handle. +unsafe impl<'a, U> UnsafeTimerPointer for Pin<&'a mut U> +where + U: Send + Sync, + U: HasTimer, + U: TimerCallback =3D Self>, +{ + type TimerHandle =3D PinMutTimerHandle<'a, U>; + + unsafe fn start(self, expires: Ktime) -> Self::TimerHandle { + // Cast to pointer + let self_ptr: *const U =3D ::deref(&self= ); + + // SAFETY: As we derive `self_ptr` from a reference above, it must= point + // to a valid `U`. + unsafe { U::start(self_ptr, expires) }; + + PinMutTimerHandle { inner: self } + } +} + +impl<'a, U> RawTimerCallback for Pin<&'a mut U> +where + U: HasTimer, + U: TimerCallback =3D Self>, + U: TimerCallback =3D Self>, +{ + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrt= imer_restart { + // `Timer` is `repr(C)` + let timer_ptr =3D ptr as *mut Timer; + + // SAFETY: By the safety requirement of this function, `timer_ptr` + // points to a `Timer` contained in an `U`. + let receiver_ptr =3D unsafe { U::timer_container_of(timer_ptr) }; + + // SAFETY: By the safety requirement of this function, `timer_ptr` + // points to a `Timer` contained in an `U`. + let receiver_ref =3D unsafe { &mut *receiver_ptr }; + + // SAFETY: `receiver_ref` only exists as pinned, so it is safe to = pin it + // here. + let receiver_pin =3D unsafe { Pin::new_unchecked(receiver_ref) }; + + U::run(receiver_pin).into() + } +} --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 D136620CCE4; Fri, 6 Dec 2024 19:36:01 +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=1733513761; cv=none; b=QaPewrBEeFf+ZZ2NG3QOExgiZmjaNymIR7JfyZdtWaLO9kTPSGmNMgf+mhvJwLq1gaUb+n6Q7FnP8hxZiJyoCWV1hpxpYC+TRYXtwMTjEc/AFhrSoCS71oaEDHm6cQJm7VtuguzZJyesOukAc2e32brjiTDjJSWdLirtWjbZb/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513761; c=relaxed/simple; bh=7uXW0SpCswZ2a/a1jzmEzIBnEU4EedW+Z6gf3Sitb04=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rpkSjaWSb3FZ4HqOP1rHeKpO+F/3oRsVBcwaY7VnYgmXVxCs5EZHKOs1RPJL48DGRr7jPr6onmcR5nEMTqoDvj0QH3oDZ06oMfs+8eZ29dPqj1B/i5wp5uulEEE7z9zLaCcL5On9Lu6HlyiLCwFA2Xi7//RZQ/8j3JwOHKARYJI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Vh9ZGDGm; 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="Vh9ZGDGm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EADEDC4CED1; Fri, 6 Dec 2024 19:35:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513761; bh=7uXW0SpCswZ2a/a1jzmEzIBnEU4EedW+Z6gf3Sitb04=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Vh9ZGDGmbpl1vstPHVQ556bpaWnIN+f7DWfA9u9BnaxtbW29hRHED6IjQmCxB0vl/ h7NQebZyIGqKNbX5UO1bEEsUES5BgaPdI0M2nGm/6ICf6xByCDoxfN33tdVGxcxnLe sGuZfpJZRHdw/ss2CwGRVXpBl7Baf1p6FjKbofydNDXYSPte30LemI4mFxhes4vIyU 9egY+y8nY/x9HBzIXsHHBeJOTOreY+kQ+73hKC9PUbJ81UVT4UJgx1F+CTHFLo99pm rPfH0P6Y9nFYnG8Lus0OI1ahk0FWVu9WN4GD78vKpazEmYSHHBxw3lfoSFzc0bLmNQ w49gCcQl4xTYg== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:33:01 +0100 Subject: [PATCH v4 09/14] rust: hrtimer: add `hrtimer::ScopedTimerPointer` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-9-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2069; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=7uXW0SpCswZ2a/a1jzmEzIBnEU4EedW+Z6gf3Sitb04=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVhERzRBc2N3SzhMMHQvbzRLelpKT1BFCnhQMXlta2lFWDk0a3kvMk90 MFBPUFlrQ013UUFBUW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFFdUc0R2o1NUtHTjMKQlFKblUxRnd BQW9KRU9HNEdqNTVLR04zd2NjUUFMOEVJUC9kQnY2TVBwaGdyb3c1ajlXdFgyVkQrWENvSU9vYg pUeW4wYmVsVExsVWRwWWtiL2FFQ2FkcFhWSmw3MWtNZ3NETnVXOTg4VlVTeU5LcE9zd2I5K1dOa Upmc3pIYnE2CjZYUjRhMlhodkE5ellYdDFpeUNtdU9NeWtQR2xBMS9LT0xKVmpQOWF3TTNaRUp0 M0cxMitGaXlOS3FRVWVxUGQKMkxreEsvN2kzWUhaTXN3dFRkcFI3RWozZGJKNG5qR0tOUUlCS3o raC9XQ2lxcURRRmlUVmJZcjNIbEt6U1B0RQo3UE5VUmdiWXFMdTBNN2FId0JXOWkvanlRUWQ5aG Q4cy85UmlmVnNLWkp6bXJaLzVPcm5GeTBISXhJbzQ3VUV3CnVvYkJuNTZ2c29VanJHZ1FZMVRRe VRUUUJXb0JKWlVpdlZhbVcrV0Fxa3o5NUFreWlqYW51cWdwWGF3SGZhdWcKRUxyUGZuTmplYlZI bVY1dHFnUVpEdHZPK2Z6Y25ZUXo1U1RHa1huRzNjWXJkSGpZYmV5WVJ1UThIakFSR3JlbwpsVDg rMXdKRWtUMTVXcVhPbnpDZGNuTjFiUHQwZ2dGc0s0VkdDdGNSeXFXTjU5NndVZVVJVFMvSHdwT3 BNNFNnCkx5V3N4REFqRU1sN3NlQ3RMejFMT1FjL0ZBMFN3MUNEenZmMFJjT2pPL1VhMHZoeWpXd E1QVnV6VDhHaWVKTWgKSDFWZmVTcUdwK09lNjlCejVtbFljVXRVQVFWaVlKam9iUkl4dkIremxm T3NFU290WXNrSmgxQUxNRXUyMWFPLwpuNHVUdFBxK0ViQ0lFWjcyVEVXY2VhbnJ5aHdXY3k4N2N OYzNPeVJuNVg2bld1emwzcitwbDh1TnM3MDkwWFRPCmNRQUt0R3hheVhFcUFBPT0KPVVBMzMKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add the trait `ScopedTimerPointer` to allow safe use of stack allocated timers. Safety is achieved by pinning the stack in place while timers are running. Implement the trait for all types that implement `UnsafeTimerPointer`. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index efcb5e44a0dc3660a09f230c01cca998b763e8d9..7835a33677ec32999c495d5b874= ded2ff0786c9e 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -175,6 +175,39 @@ pub unsafe trait UnsafeTimerPointer: Sync + Sized { unsafe fn start(self, expires: Ktime) -> Self::TimerHandle; } =20 +/// A trait for stack allocated timers. +/// +/// # Safety +/// +/// Implementers must ensure that `start_scoped` does not return until the +/// timer is dead and the timer handler is not running. +pub unsafe trait ScopedTimerPointer { + /// Start the timer to run after `expires` time units and immediately + /// after call `f`. When `f` returns, the timer is cancelled. + fn start_scoped(self, expires: Ktime, f: F) -> T + where + F: FnOnce() -> T; +} + +// SAFETY: By the safety requirement of `UnsafeTimerPointer`, dropping the +// handle returned by [`UnsafeTimerPointer::start`] ensures that the timer= is +// killed. +unsafe impl ScopedTimerPointer for U +where + U: UnsafeTimerPointer, +{ + fn start_scoped(self, expires: Ktime, f: F) -> T + where + F: FnOnce() -> T, + { + // SAFETY: We drop the timer handle below before returning. + let handle =3D unsafe { UnsafeTimerPointer::start(self, expires) }; + let t =3D f(); + drop(handle); + t + } +} + /// Implemented by [`TimerPointer`] implementers to give the C timer callb= ack a /// function to call. // This is split from `TimerPointer` to make it easier to specify trait bo= unds. --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 F3A37192B76; Fri, 6 Dec 2024 19:35:14 +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=1733513715; cv=none; b=NI03eAe8YARcbEKQhmQhXhgUJqK7MPSJrSFlGfkAH9Htr3vPppivYMnMj2gAPO/OTG3+uk7lSSkNdDcA093NyHo+0Gq8egIUlvQn6/5VcWeyKhBy4fJdq2vpNPuJOKXcLRu6PLGmdA3fqae9Qtq8sbv1SUUfz63Fia7lWLsm86U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513715; c=relaxed/simple; bh=6xLEbR0EIB8Nn0LgnN+aKPyBmaJP6NyFspAESdf3mNM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rS3oqXKEvKcnzZmYf0RVNcs10cy85rGxHZRadGK1gqfKP/Rzrfiq6x5x3IjbI5Eh8YwVmBW8vMCPiqeGqw+iWOov0GIfd/vBH6g1H6biPETmqFg9xm4VncLCMBi7+41GIl4W3wkrpS9lsimEpQb5rqNVuCcGMyVj27HavvxVu8o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=l9ObnEFJ; 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="l9ObnEFJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DA0A0C4CED1; Fri, 6 Dec 2024 19:35:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513714; bh=6xLEbR0EIB8Nn0LgnN+aKPyBmaJP6NyFspAESdf3mNM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=l9ObnEFJr0DlDWafnqrUCs0s4pYo9sP/F6Xm1ofs2vUGmneEuf9zPG0XCCioNORQC 4X32/ZfGlf444ffh59oDK0pTEaziyzoPtipKw56MKGEFk6zWyw+AltHPrsWqwbRWRY Rv0gK0Bm1rlsgGhJPgjIGWZSOhZFHp+lnpPWxtKsv0QwkhfaT5HFT1LQGnGTGrt3Qq fycuitNrQf9jCQ4z6H/PPyU6HWnzHRt0rW9x2O4jSO4yiFxSoneHgjKLVu1oO3vNTF PqHLHshwSIt31VxUREn8Ja7qWsopngh+Huth46p/EsQx2QuaFK7b1lZBw8gM8A5sGY xyrf/jKO/Ntxw== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:33:02 +0100 Subject: [PATCH v4 10/14] rust: alloc: add `Box::into_pin` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-10-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1332; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=6xLEbR0EIB8Nn0LgnN+aKPyBmaJP6NyFspAESdf3mNM=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVhHSitJZ3dHNEx1TEZzTTR1c1RNZmc4CjJjNnhkS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T0KPTFLV24KLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add an associated function to convert a `Box` into a `Pin>`. Signed-off-by: Andreas Hindborg --- rust/kernel/alloc/kbox.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index 9ce414361c2c6dd8eea09b11041f6c307cbc7864..1a993ec8602b37041c192458d8b= 6acff30769a04 100644 --- a/rust/kernel/alloc/kbox.rs +++ b/rust/kernel/alloc/kbox.rs @@ -245,6 +245,16 @@ pub fn pin(x: T, flags: Flags) -> Result= >, AllocError> Ok(Self::new(x, flags)?.into()) } =20 + /// Convert a [`Box`] to a [`Pin>`]. If `T` does not imp= lement + /// [`Unpin`], then `x` will be pinned in memory and can't be moved. + pub fn into_pin(boxed: Self) -> Pin { + // SAFETY: `Self` is guaranteed to be the only pointer to the boxed + // value. Thus, if `T: !Unpin`, `T` is guaranteed to stay pinned; = there + // is no way to get rid of the `Pin` and move out of the returned + // `Pin>`. + unsafe { Pin::new_unchecked(boxed) } + } + /// Forgets the contents (does not run the destructor), but keeps the = allocation. fn forget_contents(this: Self) -> Box, A> { let ptr =3D Self::into_raw(this); --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 B5EC11DE2D8; Fri, 6 Dec 2024 19:35:36 +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=1733513736; cv=none; b=rsFoWkk9x9kkbYTPcdjt0x+1IJaAlqrSBk3PplDYaJrTK3H09A37v+3uSoDGffGhMhNqbhXUy+1OWr7A+0Ah2DQ+CM6n7aTngyMBsH3dCTuDr4inHAKk9KVLP5Vapoap9HajP4XK3pyXkOWQcvLugjceqqdKbg04iTMosp9RDXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513736; c=relaxed/simple; bh=nCt+rC7hD4TBOexROjY8cgHhOsOh0+xYX8SSIYt9Euw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Z36XG64mNpUX71Q/+hNn4kb4B4pPKbSgfqBajEMbwlB45vT7tEAOJ2OCFzct7cPFjwk4thGLigUFQJ8YcmMKq5SaXt5I0YptQHN/R0XVncP32WoBweJ/bESqmCyta+K/82J+5DmzuhZ2vTB+bhH/57uAVWbfIURlpxLnm+cf04I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EBZYNLno; 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="EBZYNLno" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE579C4CED1; Fri, 6 Dec 2024 19:35:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513736; bh=nCt+rC7hD4TBOexROjY8cgHhOsOh0+xYX8SSIYt9Euw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EBZYNLnoRMWC0ibZQKd2ibhtFGCchpftRO1kE2VxNXdFEiVnTSbpwe0OSHoRh+ZyC HzbNLRLvx3urFyMz9mLVhX1ecCQqWChvcXRmu80kKSa6EBwhY6cZ86d4v0iff0RnSH yip7VhxLQrUnzhDs/MAkkEoGM30fRQ29iq3BXHsi18mAnmWcHVuNSgmvn6+m+yLeV+ KliJkHyPmjqPosd/kUyWMOx0z0No+dxXXGpyBkzkwFPXq2INwR3i71BKeiGd1D9lNr orC/AhrPbWc6Y/vHxcPREbAumbuawyEUgK/aOqRlfVVZPbvM1wTffkxwT94KeNFHrr 1kWkCeLpfsaaQ== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:33:03 +0100 Subject: [PATCH v4 11/14] rust: hrtimer: implement `TimerPointer` for `Pin>` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-11-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4446; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=nCt+rC7hD4TBOexROjY8cgHhOsOh0+xYX8SSIYt9Euw=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgprQTBEQUFvQjRiZ2FQbmtvWTNjQnl5WmlBR 2RUVVhMSXVBV1lVczBEdmZRdnc4TDZJLzlmQWh6YUFWdmhVWTJTClNnUk5kUjBHdllrQ013UUFB UW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFFdUc0R2o1NUtHTjNCUUpuVTFGeUFBb0oKRU9HNEdqNTV LR04zSXVFUC8zTC90NUsvQllJcDR2eExYSmhCNjRJSFhiN1pwTmV1NlhveXUvYTY1WURoNDNjWA pMWUZGbEk2eUxacmpmOHVOb0JKNzBMajJJcU5RczgrT3N3SU0xblRQZTVObjRNU2VOSjJkM1NXb zVsd0lyQTRHCmtieHVWYVBObHFnaHBhN3BsMzlzVU1YcU93dDlNamNIT3lJR0RJMDZ1OVRKUFRU Ny81cnZra2JIYWxPUThsZzkKQlduTXliM0NyZit5VmVhc3ZsQk1oeTc2SnBZRjRhTG9OVEdjRmw 0Vzc1SHRqL29pdUNyUFNzMlpVYkM2Ym84Zwo2VVRVZ0hHWHIwdjA5ZHd2VHlQaS9PU0hZMzFZZk hGaDR0V0taWVl5LzBVUzhocVhUNWVuK2lYZW1VM1RWbmd3ClFrN2JxT1dxSkJVc1k2c0lDRENUU 3creVNsbjJCWVYvY3NzRjNveXBzdGlRZ2tTRUxndXc0Qy9Jc0pEWGhtS3EKQ0lvWGwzK0tOK08w amRLYmZnWHk5TWpWbjNUWFVMVzBaY2JWcTdKeis3MnFuUENTd25lMWZHMS9kODROWGVQaQp2V0x NNWM4VzNDWlRjWDBlWjhwS3UvV1MreDVZc3J4bWR3bXF1RlhUV2svQ0U5UlNLMlZYUmtNNldnZU duSDlBCm5uMEhETm9tRkFWNjZ3V2RrdHlZVE8vamgzcHhMWHA0YTd3TzNUYUlkbGM5aWxkbzZWV WtIVjlGVkQ3QnpOUXMKaFErOW5tM2JaRjZwZ1ltMUdvam5KMzdmQldta28vaTd4dHFVUEt0a29S MDl3TjhDcENlT1A3b2w2THFxbS80cgpqeU9YcFZKWjlpbE8xVWoxazJYMTNyV0E1d25jN1RodDU zUTE0VHppYXZkOFpwWmVVUmRZOC9LbFZicDcKPU91dDIKLS0tLS1FTkQgUEdQIE1FU1NBR0UtLS 0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow `Pin>` to be the target of a timer callback. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 3 ++ rust/kernel/time/hrtimer/tbox.rs | 102 +++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 105 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 7835a33677ec32999c495d5b874ded2ff0786c9e..976a7350d6bee0cebe3dcca04cc= be48fef31e430 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -395,6 +395,9 @@ unsafe fn raw_get_timer(ptr: *const Self) -> } } =20 +// `box` is a reserved keyword, so prefix with `t` for timer +mod tbox; + mod arc; mod pin; mod pin_mut; diff --git a/rust/kernel/time/hrtimer/tbox.rs b/rust/kernel/time/hrtimer/tb= ox.rs new file mode 100644 index 0000000000000000000000000000000000000000..4b932a20cf6fcacdff7c40fc280= f8d73a45addc0 --- /dev/null +++ b/rust/kernel/time/hrtimer/tbox.rs @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::HasTimer; +use super::RawTimerCallback; +use super::Timer; +use super::TimerCallback; +use super::TimerHandle; +use super::TimerPointer; +use crate::prelude::*; +use crate::time::Ktime; +use core::mem::ManuallyDrop; + +/// A handle for a `Box>` returned by a call to +/// [`TimerPointer::start`]. +pub struct BoxTimerHandle +where + U: HasTimer, + A: crate::alloc::Allocator, +{ + pub(crate) inner: *mut U, + _p: core::marker::PhantomData, +} + +// SAFETY: We implement drop below, and we cancel the timer in the drop +// implementation. +unsafe impl TimerHandle for BoxTimerHandle +where + U: HasTimer, + A: crate::alloc::Allocator, +{ + fn cancel(&mut self) -> bool { + // SAFETY: As we obtained `self.inner` from a valid reference when= we + // created `self`, it must point to a valid `U`. + let timer_ptr =3D unsafe { >::raw_get_timer(self.= inner) }; + + // SAFETY: As `timer_ptr` points into `U` and `U` is valid, `timer= _ptr` + // must point to a valid `Timer` instance. + unsafe { Timer::::raw_cancel(timer_ptr) } + } +} + +impl Drop for BoxTimerHandle +where + U: HasTimer, + A: crate::alloc::Allocator, +{ + fn drop(&mut self) { + self.cancel(); + // SAFETY: `self.inner` came from a `Box::into_raw` call + drop(unsafe { Box::::from_raw(self.inner) }) + } +} + +impl TimerPointer for Pin> +where + U: Send + Sync, + U: HasTimer, + U: for<'a> TimerCallback =3D Pin>>, + U: for<'a> TimerCallback =3D Pin<&'a U>>, + A: crate::alloc::Allocator, +{ + type TimerHandle =3D BoxTimerHandle; + + fn start(self, expires: Ktime) -> Self::TimerHandle { + let self_ptr: *const U =3D ::deref(&self= ); + + // SAFETY: Since we generate the pointer passed to `start` from a = valid + // reference, it is a valid pointer. + unsafe { U::start(self_ptr, expires) }; + + // SAFETY: We will not move out of this box during timer callback = (we + // pass an immutable reference to the callback). + let inner =3D unsafe { Pin::into_inner_unchecked(self) }; + + BoxTimerHandle { + inner: Box::into_raw(inner), + _p: core::marker::PhantomData, + } + } +} + +impl RawTimerCallback for Pin> +where + U: HasTimer, + U: for<'a> TimerCallback =3D Pin>>, + U: for<'a> TimerCallback =3D Pin<&'a U>>, + A: crate::alloc::Allocator, +{ + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrt= imer_restart { + // `Timer` is `repr(C)` + let timer_ptr =3D ptr.cast::>(); + + // SAFETY: By C API contract `ptr` is the pointer we passed when + // queuing the timer, so it is a `Timer` embedded in a `T`. + let data_ptr =3D unsafe { U::timer_container_of(timer_ptr) }; + + // SAFETY: We called `Box::into_raw` when we queued the timer. + let tbox =3D ManuallyDrop::new(Box::into_pin(unsafe { Box:::= :from_raw(data_ptr) })); + + U::run(tbox.as_ref()).into() + } +} --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 F3B0D21B1B5; Fri, 6 Dec 2024 19:35:52 +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=1733513753; cv=none; b=HkAve07f9W7hgcVOy+d2feOHRb2F5q4xlafA6ztfkvpYaKA0On9hQX3HYrTKl9+uBGuA1Ly62EPHBZzPzVb/SpjBHhuJGxOlNRL5wYv3vZG/2XnNNxWjh0ARKkyq6xyf5Pn7Ahku/MKv+T6V4fuXaPZHuH59toDty2N8IuvxJmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513753; c=relaxed/simple; bh=nooMkUxQMi3qxanUzKuu/XUmI2mf+pjc89vZvmK0u20=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZUckQReScLte+F/i8Wg8TtbeuKw5m60pWZq3n7MM+ERMv9C0kjimF/bke6/kOKaVixZ/X0/Cd3cQzPedfe2aXZloJjmuVrt1y33k0HdIeZ1514CH9mW4c/XhN5dAs2cRmB976QZc5hLRwlqZ0cWmrK8C8h8lMqw/WbMhbGy+aII= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TVMZKJPs; 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="TVMZKJPs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46CC4C4CED1; Fri, 6 Dec 2024 19:35:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513752; bh=nooMkUxQMi3qxanUzKuu/XUmI2mf+pjc89vZvmK0u20=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=TVMZKJPsxx+xhC3VweVMEUOIq3ssEjVlcDbYDvOei+z0zZInytwsv6iiuYAwslovS 0XFIVFOPo8lpUebNGLkdDVp0X6W+3b31ldLEHsVmfAua8t2ScEmYVdj5KCNbACvKs+ ZchfG8wo94I91xfhMyXAHkChiaUWlQ1G570PRqJHp+qH06vxSoNJrXJmbsSxJIXTni jFq4QByQhRDrSNCcQs2nw0Oivff/ETu7U/9vhkp5pt9NvX9GXpgI+a1hF/0pHmWb4O E5HZgF1G07kjbfxuwrRHBNet5kK6o89AVqaM+2ql2bbm4t2rS5dXx2TQaY7j3rkRbY DftEpc6skrlHw== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:33:04 +0100 Subject: [PATCH v4 12/14] rust: hrtimer: add `TimerMode` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-12-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=5693; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=nooMkUxQMi3qxanUzKuu/XUmI2mf+pjc89vZvmK0u20=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVhNR1JDV2lzazNFay91OUY4MmlnUlg0CnpVc3g1V3RnTDl3TFc5Vkkx dWFCam9rQ013UUFBUW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFF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Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow selection of timer mode by passing a `TimerMode` variant to `Timer::new`. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 87 +++++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 976a7350d6bee0cebe3dcca04ccbe48fef31e430..3bcc45dae130448173be157ce60= 395e2b9d46a10 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -43,6 +43,8 @@ pub struct Timer { #[pin] timer: Opaque, + // This field goes away when `bindings::hrtimer_setup` is added. + mode: TimerMode, _t: PhantomData, } =20 @@ -55,7 +57,7 @@ unsafe impl Sync for Timer {} =20 impl Timer { /// Return an initializer for a new timer instance. - pub fn new() -> impl PinInit + pub fn new(mode: TimerMode) -> impl PinInit where T: TimerCallback, { @@ -70,10 +72,11 @@ pub fn new() -> impl PinInit place, Some(T::CallbackTarget::run), bindings::CLOCK_MONOTONIC as i32, - bindings::hrtimer_mode_HRTIMER_MODE_REL, + mode.into(), ); } }), + mode: mode, _t: PhantomData, }) } @@ -322,7 +325,7 @@ unsafe fn start(self_ptr: *const Self, expires: Ktime) { Self::c_timer_ptr(self_ptr).cast_mut(), expires.to_ns(), 0, - bindings::hrtimer_mode_HRTIMER_MODE_REL, + (*Self::raw_get_timer(self_ptr)).mode.into(), ); } } @@ -363,6 +366,84 @@ fn from(value: TimerRestart) -> Self { } } =20 +/// Operational mode of [`Timer`]. +#[derive(Clone, Copy)] +pub enum TimerMode { + /// Timer expires at the given expiration time. + Absolute, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + Relative, + /// Timer does not move between CPU cores. + Pinned, + /// Timer handler is executed in soft irq context. + Soft, + /// Timer handler is executed in hard irq context. + Hard, + /// Timer expires at the given expiration time. + /// Timer does not move between CPU cores. + AbsolutePinned, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer does not move between CPU cores. + RelativePinned, + /// Timer expires at the given expiration time. + /// Timer handler is executed in soft irq context. + AbsoluteSoft, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer handler is executed in soft irq context. + RelativeSoft, + /// Timer expires at the given expiration time. + /// Timer does not move between CPU cores. + /// Timer handler is executed in soft irq context. + AbsolutePinnedSoft, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer does not move between CPU cores. + /// Timer handler is executed in soft irq context. + RelativePinnedSoft, + /// Timer expires at the given expiration time. + /// Timer handler is executed in hard irq context. + AbsoluteHard, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer handler is executed in hard irq context. + RelativeHard, + /// Timer expires at the given expiration time. + /// Timer does not move between CPU cores. + /// Timer handler is executed in hard irq context. + AbsolutePinnedHard, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer does not move between CPU cores. + /// Timer handler is executed in hard irq context. + RelativePinnedHard, +} + +impl From for bindings::hrtimer_mode { + fn from(value: TimerMode) -> Self { + use bindings::*; + match value { + TimerMode::Absolute =3D> hrtimer_mode_HRTIMER_MODE_ABS, + TimerMode::Relative =3D> hrtimer_mode_HRTIMER_MODE_REL, + TimerMode::Pinned =3D> hrtimer_mode_HRTIMER_MODE_PINNED, + TimerMode::Soft =3D> hrtimer_mode_HRTIMER_MODE_SOFT, + TimerMode::Hard =3D> hrtimer_mode_HRTIMER_MODE_HARD, + TimerMode::AbsolutePinned =3D> hrtimer_mode_HRTIMER_MODE_ABS_P= INNED, + TimerMode::RelativePinned =3D> hrtimer_mode_HRTIMER_MODE_REL_P= INNED, + TimerMode::AbsoluteSoft =3D> hrtimer_mode_HRTIMER_MODE_ABS_SOF= T, + TimerMode::RelativeSoft =3D> hrtimer_mode_HRTIMER_MODE_REL_SOF= T, + TimerMode::AbsolutePinnedSoft =3D> hrtimer_mode_HRTIMER_MODE_A= BS_PINNED_SOFT, + TimerMode::RelativePinnedSoft =3D> hrtimer_mode_HRTIMER_MODE_R= EL_PINNED_SOFT, + TimerMode::AbsoluteHard =3D> hrtimer_mode_HRTIMER_MODE_ABS_HAR= D, + TimerMode::RelativeHard =3D> hrtimer_mode_HRTIMER_MODE_REL_HAR= D, + TimerMode::AbsolutePinnedHard =3D> hrtimer_mode_HRTIMER_MODE_A= BS_PINNED_HARD, + TimerMode::RelativePinnedHard =3D> hrtimer_mode_HRTIMER_MODE_R= EL_PINNED_HARD, + } + } +} + +impl From for u64 { + fn from(value: TimerMode) -> Self { + Into::::into(value) as u64 + } +} + /// Use to implement the [`HasTimer`] trait. /// /// See [`module`] documentation for an example. --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 953D4192B76; Fri, 6 Dec 2024 19:35:10 +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=1733513710; cv=none; b=GXVnEZhPLpKc9wEuI2AwN2c2ndYxnN5CsIWYFfOaDDtnu7SqkmDbfrGkB2FFZQSy+TR2AiZU6ph85lM6EealMHb6BMQiEmGK4Jd/nOYTqu+GG3rFv8NUe1OP/1ovkkI6b6HihO4P+2u5CunYadgmyTcQIHEcFt4h5dMFzB76diI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513710; c=relaxed/simple; bh=5AsfxhMcapsiSrIIx3K1wmlhpNNxyQ5cVuoVQNELSIg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ELFXFUnN8bdVHz0p3yg1G4aiJcUjCXw9w69Zdz+H+HgKv80coYxS73Y2T4wdBM7vyHgdlsEq5G0N3fJNizS3YdmHWUD3MyJAbBltXrzGsRpcKkcp14rg+OSGvvH50Bh1LUddKl2redNuFsWsdRtlX1GgUIMo60rdAKaay1xxMrw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AwhKLQYV; 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="AwhKLQYV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90615C4CEDD; Fri, 6 Dec 2024 19:35:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513710; bh=5AsfxhMcapsiSrIIx3K1wmlhpNNxyQ5cVuoVQNELSIg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=AwhKLQYViq5/Bd0Oit88GefPPe0RqAOv0l0R+N8Cz1XQIrYAWrSD56NVTsO6WTzqB LQKqpd/8oLxMQW8BTwlP/0oLrIDgl+MCo5B5DQPMkVeHajqTn0RfO7fp9HtGAztjtr MMEBN/DhmqgyKTOIgaT96dDBFfy35cUS0e1+5aWmQgnSmFwsfwys2L1MAXCCMRNA2m IEc3Etgz0oRJf/fC2krp1jVKhuPM2D5ZNS88zltcWLZJTkuyOgV2PpCkdGy4Oijz0y 7yo3xNjeMoPQx7pkGne/UH97VHGDod2nytr6Kl119kdbEkfdKsvEvvwD23lr3sRLZo VaNqGvfzO4I4Q== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:33:05 +0100 Subject: [PATCH v4 13/14] rust: hrtimer: add clocksource selection through `ClockSource` 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: <20241206-hrtimer-v3-v6-12-rc2-v4-13-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4032; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=5AsfxhMcapsiSrIIx3K1wmlhpNNxyQ5cVuoVQNELSIg=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVhUanB5bmxpTUZOYWZodllzdklxdnROCjFnQkJHcEZDQ2dIdkc3Wlhy U3UrU29rQ013UUFBUW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFFdUc0R2o1NUtHTjMKQlFKblUxRjB BQW9KRU9HNEdqNTVLR04zaHY4UC8zMGlMUWg5RWloZDlJK2xwQWg1UWRneU4vaXZFNzlJN3hRKw owNC9iQWt2V29vT01Qb0RjOHAxWklpM0RJOGp0Szh5NE45aVlvdGkrOFBMTWpodnkzTFhYRUJ5e TNMN0xWV2IyCmVWWko4MnU2QnIvOUlSUGVqT1pJeDJyRWtBTmNGRzZUdDlHKzNvV3o0Q3pKTW9u YW9KemRraWJMWjkyR25TSTIKVjlRRmN1V3lwaEpJNEloeDZQM3M3TFpzNHRCd3FyQTFIZ01WS2l jZjJEYUU5R2lRcDRDUlFtOW4wSElhb2MzLwovaHZpd2NNWGhKcjR0ZGtTQ3E3NHlpQ2J5MWtNY0 NOREJ2eTZGNWRZOVhHSlRmL0NFYm1TZDVIcUVWK0x6K2ppCkljbm9iRW0wT1o0KzU4bWxjRTY5N Hp1K1pLeExmZEgreDBsMmw1aGM5bXJwYVRSblZHYkJ6OGJib1VHR3d1Tk8KUVUvdmVobGExdit1 TUMyMExEZlhidDRBY3JGbldtWDJzTUg2TnIvYlZ2SDB4VGVEdktMVXFPRDJEV2x3R2lmUgpjYlp meVVQbkRSRzlOYWMrN3JqWmhrUGpzeE1GR25SdTRuK0kzek1ad1NiSXJVQ0I5YXFqWXpMcVdNdU g4UWdSCjVMbmppU280aGJza29vT2c1S2xhd29GTXhkb3FIRUgyNXFsWTZzNXgyd3R5ampZS3ZYN S9KMlFPY2xGYUlySisKaGUvTlVjazJLU2lKUFhsbmRTejFyZlcrSXdoMVJJVm40Tld6b1RNMDl3 bU9tZk95OHlCVkpab3JTelBGN3N3dQoxR0UyZVNQRTBUUjE3WXFmQUlweHZ6SDhrZWRwUStSSXd lbnZ3WnZ6MUhFckZhciswRjJyb3lHZmM2ZWRBT3pnCmJvWGZrdEtxRXg4NmlnPT0KPURzNGQKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow selecting a clock source for timers by passing a `ClockSource` variant to `Timer::new`. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 52 +++++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 3bcc45dae130448173be157ce60395e2b9d46a10..ef1c404c7d3e859b8ac70316b6b= b44302130dd95 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -57,7 +57,7 @@ unsafe impl Sync for Timer {} =20 impl Timer { /// Return an initializer for a new timer instance. - pub fn new(mode: TimerMode) -> impl PinInit + pub fn new(mode: TimerMode, clock: ClockSource) -> impl PinInit where T: TimerCallback, { @@ -71,7 +71,7 @@ pub fn new(mode: TimerMode) -> impl PinInit bindings::hrtimer_setup( place, Some(T::CallbackTarget::run), - bindings::CLOCK_MONOTONIC as i32, + clock.into(), mode.into(), ); } @@ -444,6 +444,54 @@ fn from(value: TimerMode) -> Self { } } =20 +/// The clock source to use for a [`Timer`]. +pub enum ClockSource { + /// A settable system-wide clock that measures real (i.e., wall-clock)= time. + /// Setting this clock requires appropriate privileges. This clock is + /// affected by discontinuous jumps in the system time (e.g., if the s= ystem + /// administrator manually changes the clock), and by frequency adjust= ments + /// performed by NTP and similar applications via adjtime(3), adjtimex= (2), + /// clock_adjtime(2), and ntp_adjtime(3). This clock normally counts t= he + /// number of seconds since 1970-01-01 00:00:00 Coordinated Universal = Time + /// (UTC) except that it ignores leap seconds; near a leap second it is + /// typically adjusted by NTP to stay roughly in sync with UTC. + RealTime, + /// A nonsettable system-wide clock that represents monotonic time sin= ce=E2=80=94as + /// described by POSIX=E2=80=94"some unspecified point in the past". O= n Linux, that + /// point corresponds to the number of seconds that the system has been + /// running since it was booted. + /// + /// The CLOCK_MONOTONIC clock is not affected by discontinuous jumps i= n the + /// system time (e.g., if the system administrator manually changes the + /// clock), but is affected by frequency adjustments. This clock does = not + /// count time that the system is suspended. + Monotonic, + /// A nonsettable system-wide clock that is identical to CLOCK_MONOTON= IC, + /// except that it also includes any time that the system is suspended= . This + /// allows applications to get a suspend-aware monotonic clock without + /// having to deal with the complications of CLOCK_REALTIME, which may= have + /// discontinuities if the time is changed using settimeofday(2) or si= milar. + BootTime, + /// A nonsettable system-wide clock derived from wall-clock time but + /// counting leap seconds. This clock does not experience discontinuit= ies or + /// frequency adjustments caused by inserting leap seconds as CLOCK_RE= ALTIME + /// does. + /// + /// The acronym TAI refers to International Atomic Time. + TAI, +} + +impl From for bindings::clockid_t { + fn from(value: ClockSource) -> Self { + match value { + ClockSource::RealTime =3D> bindings::CLOCK_REALTIME as i32, + ClockSource::Monotonic =3D> bindings::CLOCK_MONOTONIC as i32, + ClockSource::BootTime =3D> bindings::CLOCK_BOOTTIME as i32, + ClockSource::TAI =3D> bindings::CLOCK_TAI as i32, + } + } +} + /// Use to implement the [`HasTimer`] trait. /// /// See [`module`] documentation for an example. --=20 2.46.0 From nobody Wed Dec 17 19:37:28 2025 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 257BC192B76; Fri, 6 Dec 2024 19:35:18 +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=1733513719; cv=none; b=AusVU1nEHpFDqOSJYCI1SRK8WxAIFryOQU05pmXlT/VZwKwWo+HXcQt9cSGQw3bUlllP/BRjOrgvNPF4QpZcBM5UEitLSXI/s5bh2OrM1rMMWNT1MxYtRiqdQA3OhBTuG92a79sDfu/+skbaR0RkH0bEHnZYfMgPnJKESTKWKJ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513719; c=relaxed/simple; bh=3cmQwvQmcHFuleBZ0JcLoMCUVXuHR5HSrNoO4gZjqeY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jZjSfcG1UK9sVFiSyM7J1k8NpjJ0t0eIooMvNMSp8pwBLf0HLvuhoNVnd72IpiITTkGTsAWsUi55J8u7p5di+fn2KIkTKKiMY5qBx9rzu7bW1CLIMBrzkwi0RKHNbB4zWxqC9vZXGNNHbt2Kem2vZB1h1LeJ0OuSzFH0egkAAUY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=goxFHOQ5; 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="goxFHOQ5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13A71C4CEDD; Fri, 6 Dec 2024 19:35:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513718; bh=3cmQwvQmcHFuleBZ0JcLoMCUVXuHR5HSrNoO4gZjqeY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=goxFHOQ5bauugRte1mV+UzgamMX0/6GN0K0qFgLeyhlYB8TwrhowAQhxnHGertMie /nJ286ZjYn5T/XfAUndGYjXanvbc61l9O/KGQl9rcI7PTPkOzG6HZVYCsVMkJBci8I euZonUCYAKMEbEiaa0xNjDd0Wh4+qM0Wv5N+yy1FBfZmJLGag6ZbW6rKDFfmbXNzW2 bclKbGc+kM59ybRFGzSsqUQbKZs0t8SRpjPSmxoRxQ1McbY6LapfLZnpt5mA+DeCGh dlhF/3rebGrLwXVnQKy6dtwVtXw0Cov7YAxWfxTkF1ezPiQmT7SlHUj/3uMmjG81zn tVhef9QEM7L8w== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:33:06 +0100 Subject: [PATCH v4 14/14] rust: hrtimer: add maintainer entry 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: <20241206-hrtimer-v3-v6-12-rc2-v4-14-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@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 , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=985; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=3cmQwvQmcHFuleBZ0JcLoMCUVXuHR5HSrNoO4gZjqeY=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFwTDlrQTBEQUFvQjRiZ2FQb mtvWTNjQnl5WmlBR2RUVVhYWFJpdGFvRFZJWXROb2YyYmxySzN1CkhhYyt4REZyQnh1ZzN3Mnhh QUt0YjRrQ013UUFBUW9BSFJZaEJCTEIrVWRXdjN3cUZkYkFFdUc0R2o1NUtHTjMKQlFKblUxRjF BQW9KRU9HNEdqNTVLR04zQlU4UC9pYVpsdXY3RW5JQ2QwN1BESmtWT3RITTVRSDdVc2tuWm5ycA pWZ2tXOGRPZEF2U1FSTVJ2bmY3eElMSk0zd2swRXEvNld6eWRnRHhqTjJjMkVYWWE3MWxjWllwM DhGeHRDMGd4CjhpRkEzdkRybitLUDB5RXNnWXFEQk5USllaQ2tJSDRmQStWMktEalMrNzN1T2lK RTR5TE8vYlBEWUFNQ3lteFQKR0l6bzJIY0dRV2dXWWxOYzRFanVaSWpCSnhCWmlsdXAySjY4akN zWW8xYmVMZXkxYnhXNkpGV20vYmxlSVlycApwYk1najd1MERMR0o4Yng4Z3dUNFo2d1VCU1ZkQU YySnVvbzVEaGFxR2RpcmFld0FpYlRCMWkyeTdwT0VEeHlCCkVxY05kUVZnV1N2NHEzUytTZ2VMc HNvd3ZOMW9nMW1ycE9YK1lPc2xwY05ERlFqcGxIemtpL1pveWhNTmF1SEQKTFVuS040cmFQcTZs ZXc5UnFXS09rbnV5WStDUklJZVdzd2JYVWd5SjVKbm93SFpjQ2FHWEJxZlluMlV1d2djSApVYVl JYUt3TXlmNzVoNHVYcmQycVhXQzZxQXNEcmk0SE1DRUtZTFZWQlQyU09hWURHMkRNeG9ncDFieD lVZDFPCk9teklsaG5EYzBoT28zR2ZxbER4M24wSFZEeG9OR2ZBR1hQaXBwU0JTQ0ZkYjVQK0UrK 1ZjU2dyTDdkNGFhN0MKZ25kWFZRTXNKK3U2QTZMUjZmc1BvTEtmRDVTVVAraWFjTzNRZ3YzdG9D VHNHZk5SbTZNZVZNWFV1UUl1MzcyeQpMMXRQdGlweEtMakViS0tJaHcxSUNNQTFqUHJQMUdGMWF iRm9pMFBvb09BTXpJUEQ3OHNETWZtSTFuK0ovMG0xCnFycjhnMmxWZFRvaDBRPT0KPWp5S2IKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add Andreas Hindborg as maintainer for Rust `hrtimer` abstractions. Also add Boqun Feng as reviewer. Signed-off-by: Andreas Hindborg Acked-by: Boqun Feng --- MAINTAINERS | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 1e930c7a58b13d8bbe6bf133ba7b36aa24c2b5e0..4b29b00f740bcb24d25dbcf45f2= 65eea6b4cd980 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10251,6 +10251,16 @@ F: kernel/time/timer_list.c F: kernel/time/timer_migration.* F: tools/testing/selftests/timers/ =20 +HIGH-RESOLUTION TIMERS [RUST] +M: Andreas Hindborg +R: Boqun Feng +L: rust-for-linux@vger.kernel.org +S: Supported +W: https://rust-for-linux.com +B: https://github.com/Rust-for-Linux/linux/issues +F: rust/kernel/hrtimer.rs +F: rust/kernel/hrtimer/ + HIGH-SPEED SCC DRIVER FOR AX.25 L: linux-hams@vger.kernel.org S: Orphan --=20 2.46.0