[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260128215330.58410-4-boqun.feng@gmail.com>
Date: Wed, 28 Jan 2026 13:53:26 -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 3/7] rust: workqueue: Add HasField support for Work
Implement `HasWork` via `HasField` so that manually impl_has_work!() is
no longer needed.
Signed-off-by: Boqun Feng <boqun.feng@...il.com>
---
rust/kernel/workqueue.rs | 60 +++++++++++++++++++++++++---------------
1 file changed, 38 insertions(+), 22 deletions(-)
diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
index 706e833e9702..2dcfd3eace39 100644
--- a/rust/kernel/workqueue.rs
+++ b/rust/kernel/workqueue.rs
@@ -34,19 +34,17 @@
//!
//! ```
//! use kernel::sync::Arc;
-//! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem};
+//! use kernel::workqueue::{self, new_work, Work, WorkItem};
//!
+//! #[derive(HasField)]
//! #[pin_data]
//! struct MyStruct {
//! value: i32,
//! #[pin]
+//! #[field]
//! work: Work<MyStruct>,
//! }
//!
-//! impl_has_work! {
-//! impl HasWork<Self> for MyStruct { self.work }
-//! }
-//!
//! impl MyStruct {
//! fn new(value: i32) -> Result<Arc<Self>> {
//! Arc::pin_init(pin_init!(MyStruct {
@@ -76,23 +74,21 @@
//!
//! ```
//! use kernel::sync::Arc;
-//! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem};
+//! use kernel::workqueue::{self, new_work, Work, WorkItem};
//!
+//! #[derive(HasField)]
//! #[pin_data]
//! struct MyStruct {
//! value_1: i32,
//! value_2: i32,
//! #[pin]
+//! #[field]
//! work_1: Work<MyStruct, 1>,
//! #[pin]
+//! #[field]
//! work_2: Work<MyStruct, 2>,
//! }
//!
-//! impl_has_work! {
-//! impl HasWork<Self, 1> for MyStruct { self.work_1 }
-//! impl HasWork<Self, 2> for MyStruct { self.work_2 }
-//! }
-//!
//! impl MyStruct {
//! fn new(value_1: i32, value_2: i32) -> Result<Arc<Self>> {
//! Arc::pin_init(pin_init!(MyStruct {
@@ -188,6 +184,11 @@
use crate::{
alloc::{AllocError, Flags},
container_of,
+ field::{
+ Field,
+ HasField, //
+ },
+ macros::HasField,
prelude::*,
sync::Arc,
sync::LockClassKey,
@@ -349,9 +350,11 @@ pub fn try_spawn<T: 'static + Send + FnOnce()>(
/// A helper type used in [`try_spawn`].
///
/// [`try_spawn`]: Queue::try_spawn
+#[derive(HasField)]
#[pin_data]
struct ClosureWork<T> {
#[pin]
+ #[field]
work: Work<ClosureWork<T>>,
func: Option<T>,
}
@@ -534,19 +537,17 @@ pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
/// Declares that a type contains a [`Work<T, ID>`].
///
-/// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro
-/// like this:
+/// The intended way of using this trait is via the `#[derive(HasField)]` macro. You can use the
+/// macro like this:
///
-/// ```no_run
-/// use kernel::workqueue::{impl_has_work, Work};
+/// ```
+/// use kernel::workqueue::Work;
///
+/// #[derive(HasField)]
/// struct MyWorkItem {
+/// #[field]
/// work_field: Work<MyWorkItem, 1>,
/// }
-///
-/// impl_has_work! {
-/// impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field }
-/// }
/// ```
///
/// Note that since the [`Work`] type is annotated with an id, you can have several `work_struct`
@@ -559,7 +560,6 @@ pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
/// - `work_container_of(raw_get_work(ptr)) == ptr` for any `ptr: *mut Self`.
/// - `raw_get_work(work_container_of(ptr)) == ptr` for any `ptr: *mut Work<T, ID>`.
///
-/// [`impl_has_work!`]: crate::impl_has_work
/// [`raw_get_work`]: HasWork::raw_get_work
/// [`work_container_of`]: HasWork::work_container_of
pub unsafe trait HasWork<T, const ID: u64 = 0> {
@@ -627,8 +627,24 @@ unsafe fn work_container_of(
}
pub use impl_has_work;
-impl_has_work! {
- impl{T} HasWork<Self> for ClosureWork<T> { self.work }
+impl<T, const ID: u64> Field<T> for Work<T, ID> {}
+
+/// SAFETY: Per the safety requirement of `HasField`, `raw_get_field()` and `field_container_of()`
+/// return valid pointers and are true inverses of each other, hence the implementation below
+/// fulfills `HasWork`'s safety requirement as well.
+unsafe impl<T: HasField<T, Work<T, ID>>, const ID: u64> HasWork<T, ID> for T {
+ #[inline]
+ unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> {
+ // SAFETY: Per the function safety requirement, `ptr` is a valid pointer.
+ unsafe { <T as HasField<T, Work<T, ID>>>::raw_get_field(ptr) }
+ }
+
+ #[inline]
+ unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self {
+ // SAFETY: Per the function safety requirement, `ptr` is a valid pointer, and it points to
+ // a work field in struct `T`.
+ unsafe { <T as HasField<T, Work<T, ID>>>::field_container_of(ptr) }
+ }
}
/// Links for a delayed work item.
--
2.50.1 (Apple Git-155)
Powered by blists - more mailing lists