[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20260128215330.58410-3-boqun.feng@gmail.com>
Date: Wed, 28 Jan 2026 13:53:25 -0800
From: Boqun Feng <boqun.feng@...il.com>
To: linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org,
rcu@...r.kernel.org
Cc: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Arve Hjønnevåg <arve@...roid.com>,
Todd Kjos <tkjos@...roid.com>, Christian Brauner <brauner@...nel.org>,
Carlos Llamas <cmllamas@...gle.com>, Alice Ryhl <aliceryhl@...gle.com>,
Miguel Ojeda <ojeda@...nel.org>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>,
"Paul E. McKenney" <paulmck@...nel.org>,
Frederic Weisbecker <frederic@...nel.org>,
Neeraj Upadhyay <neeraj.upadhyay@...nel.org>,
Joel Fernandes <joelagnelf@...dia.com>,
Josh Triplett <josh@...htriplett.org>,
Uladzislau Rezki <urezki@...il.com>,
Steven Rostedt <rostedt@...dmis.org>,
Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
Lai Jiangshan <jiangshanlai@...il.com>, Zqiang <qiang.zhang@...ux.dev>,
FUJITA Tomonori <fujita.tomonori@...il.com>,
Lyude Paul <lyude@...hat.com>, Thomas Gleixner <tglx@...nel.org>,
Anna-Maria Behnsen <anna-maria@...utronix.de>,
John Stultz <jstultz@...gle.com>, Stephen Boyd <sboyd@...nel.org>,
"Yury Norov (NVIDIA)" <yury.norov@...il.com>,
Vitaly Wool <vitaly.wool@...sulko.se>,
Tamir Duberstein <tamird@...nel.org>,
Viresh Kumar <viresh.kumar@...aro.org>,
Daniel Almeida <daniel.almeida@...labora.com>,
Mitchell Levy <levymitchell0@...il.com>, David Gow <davidgow@...gle.com>,
Peter Novak <seimun018r@...il.com>,
José Expósito <jose.exposito89@...il.com>
Subject: [RFC PATCH 2/7] rust: time: hrtimer: Make `HasField` a super-trait of `HasHrTimer`
This decouples the hrtimer mode part from the `HasField` part of a
`HasHrTimer`, `impl_has_hr_timer` also gets removed, as we can do:
#[has_field]
#[pin]
struct MyStruct {
a: i32,
#[field]
timer: HrTimer<MyStruct>,
}
impl HasHrTimer<MyStruct> for MyStruct {
type TimerMode = ...;
}
Signed-off-by: Boqun Feng <boqun.feng@...il.com>
---
rust/kernel/time/hrtimer.rs | 70 ++++++++-----------------------------
1 file changed, 15 insertions(+), 55 deletions(-)
diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
index 856d2d929a00..eef6d60f5adb 100644
--- a/rust/kernel/time/hrtimer.rs
+++ b/rust/kernel/time/hrtimer.rs
@@ -98,6 +98,8 @@ unsafe impl<T> Send for HrTimer<T> {}
// on a timer from multiple threads.
unsafe impl<T> Sync for HrTimer<T> {}
+impl<T> Field<T> for HrTimer<T> {}
+
impl<T> HrTimer<T> {
/// Return an initializer for a new timer instance.
pub fn new() -> impl PinInit<Self>
@@ -417,17 +419,7 @@ pub unsafe trait HrTimerHandle {
}
/// Implemented by structs that contain timer nodes.
-///
-/// Clients of the timer API would usually safely implement this trait by using
-/// the [`crate::impl_has_hr_timer`] macro.
-///
-/// # Safety
-///
-/// Implementers of this trait must ensure that the implementer has a
-/// [`HrTimer`] field and that all trait methods are implemented according to
-/// their documentation. All the methods of this trait must operate on the same
-/// field.
-pub unsafe trait HasHrTimer<T> {
+pub trait HasHrTimer<T>: HasField<T, HrTimer<T>> {
/// The operational mode associated with this timer.
///
/// This defines how the expiration value is interpreted.
@@ -441,7 +433,11 @@ pub unsafe trait HasHrTimer<T> {
/// # Safety
///
/// `this` must be a valid pointer.
- unsafe fn raw_get_timer(this: *const Self) -> *const HrTimer<T>;
+ #[inline]
+ unsafe fn raw_get_timer(this: *const Self) -> *const HrTimer<T> {
+ // SAFETY: Per function safety requirement, `this` is a valid pointer.
+ unsafe { <Self as HasField<_, _>>::raw_get_field(this.cast_mut()) }.cast_const()
+ }
/// Return a pointer to the struct that is containing the [`HrTimer`] pointed
/// to by `ptr`.
@@ -452,9 +448,15 @@ pub unsafe trait HasHrTimer<T> {
/// # Safety
///
/// `ptr` must point to a [`HrTimer<T>`] field in a struct of type `Self`.
+ #[inline]
unsafe fn timer_container_of(ptr: *mut HrTimer<T>) -> *mut Self
where
- Self: Sized;
+ Self: Sized,
+ {
+ // SAFETY: Per function safety requirement, `ptr` is a valid pointer and points to a
+ // `HrTimer` field in a struct.
+ unsafe { <Self as HasField<_, _>>::field_container_of(ptr) }
+ }
/// Get pointer to the contained `bindings::hrtimer` struct.
///
@@ -731,48 +733,6 @@ pub fn forward_now(&mut self, duration: Delta) -> u64 {
}
}
-/// Use to implement the [`HasHrTimer<T>`] trait.
-///
-/// See [`module`] documentation for an example.
-///
-/// [`module`]: crate::time::hrtimer
-#[macro_export]
-macro_rules! impl_has_hr_timer {
- (
- impl$({$($generics:tt)*})?
- HasHrTimer<$timer_type:ty>
- for $self:ty
- {
- mode : $mode:ty,
- field : self.$field:ident $(,)?
- }
- $($rest:tt)*
- ) => {
- // SAFETY: This implementation of `raw_get_timer` only compiles if the
- // field has the right type.
- unsafe impl$(<$($generics)*>)? $crate::time::hrtimer::HasHrTimer<$timer_type> for $self {
- type TimerMode = $mode;
-
- #[inline]
- unsafe fn raw_get_timer(
- this: *const Self,
- ) -> *const $crate::time::hrtimer::HrTimer<$timer_type> {
- // SAFETY: The caller promises that the pointer is not dangling.
- unsafe { ::core::ptr::addr_of!((*this).$field) }
- }
-
- #[inline]
- unsafe fn timer_container_of(
- ptr: *mut $crate::time::hrtimer::HrTimer<$timer_type>,
- ) -> *mut Self {
- // SAFETY: As per the safety requirement of this function, `ptr`
- // is pointing inside a `$timer_type`.
- unsafe { ::kernel::container_of!(ptr, $timer_type, $field) }
- }
- }
- }
-}
-
mod arc;
pub use arc::ArcHrTimerHandle;
mod pin;
--
2.50.1 (Apple Git-155)
Powered by blists - more mailing lists