Introduce HasField infrastructure
Subsystems (5)
cover Introduce HasField infrastructure
Currently we have a few similar places where we use a Has* trait to describe that a data structure has some types of field in it so that the containing type can do something with it. There are also a impl_has_*! macro to help implement the trait. While it's working, but it's less ergonomic to me, especially considering the amount of the work we need to do for something new (e.g. rcu_head).
Therefore here is the effort to unify them into a proc-macro based solution. Field and HasField traits are introduced to generify the "Has A" relationship, and a derive macro #[derive(HasField)] is also added to support automatically implementing HasField trait.
This series convert a few users (Work, HrTimer) and introduce a new Field type RcuHead. These improvements demonstrate how this infrastructure can be used.
Some future work is still needed: using HasField for DelayedWork and ListLink is still missing. Also it's possible to clean up HasWork trait as well.
One known issue is that #[derive(HasField)] doesn't play alone with #[pin_data] at the moment, for example:
#[derive(HasField)]
#[pin_data]
struct Foo { .. }works, but
#[pin_data]
#[derive(HasField)]
struct Foo { .. }doesn't. Maybe it's by design or maybe something could be improved by pin-init.
The patchset is based on today's rust/rust-next, top commit is:
a7c013f77953 ('Merge patch series "refactor Rust proc macros with syn"')
Regards,
Boqun
Boqun Feng (7):
rust: types: Introduce HasField trait and derive macro
rust: time: hrtimer: Make HasField a super-trait of HasHrTimer
rust: workqueue: Add HasField support for Work
drivers: android: binder: Replace impl_has_work! with
`#[derive(HasField)]`
rust: sync: Completion: Replace `impl_has_work!` with
`#[derive(HasField)]` rust: work: Remove impl_has_work!
rust: sync: rcu: Introduce RcuHead
drivers/android/binder/process.rs | 6 +- rust/kernel/field.rs | 73 ++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 4 +- rust/kernel/sync/completion.rs | 8 +-- rust/kernel/sync/rcu.rs | 69 ++++++++++++++++++- rust/kernel/time/hrtimer.rs | 70 ++++--------------- rust/kernel/workqueue.rs | 109 +++++++++++------------------- rust/macros/field.rs | 85 +++++++++++++++++++++++ rust/macros/lib.rs | 11 +++ 10 files changed, 299 insertions(+), 137 deletions(-) create mode 100644 rust/kernel/field.rs create mode 100644 rust/macros/field.rs
Signature · 1 line
2.50.1 (Apple Git-155)
RFC PATCH 1/7 rust: types: Introduce HasField trait and derive macro
In order to unify all the Has* infrastrutures, a generic HasField trait is added along with a derive macro #[derive(HasField)] which allows generate implementation of HasField automatically, e.g.
#[derive(HasField)]
struct Base {
a: i32;
b: i32;
#[field]
f1: Field<Base, 1>,
#[field]
f2: Field<Base, 2>,
}two implementations impl HasField<Base, Field<Base, 1>> and impl HasField<Base, Field<Base, 2>> will be generated with &raw mut and container_of!().
This simplifies the usage of the current Has* traits, namely HasWork and HasHrTimer, and eases the introduction of more Field type in the future.
--- rust/kernel/field.rs | 73 ++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 4 +- rust/macros/field.rs | 85 ++++++++++++++++++++++++++++++++++++++++++ rust/macros/lib.rs | 11 ++++++ 5 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 rust/kernel/field.rs create mode 100644 rust/macros/field.rs
rust/kernel/field.rs new file +73 -0
@@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Field types to describe a field inside a struct. + +/// A field. +/// +/// The generic type `T` is usually the type that contains the field. For some field types, it +/// needs to be generic over the type containing it, because it needs to be initialized with +/// container-type-specific callbacks. For other types, simply implement [`Field<T>`] for all `T` +/// to indicate there is no restriction. +pub trait Field <T >:Sized {} + +/// A struct `T` that has a field `F`. +/// +/// # Safety +/// +/// The methods [`raw_get_field()`] and [`field_container_of()`] must return valid pointers and +/// must be true inverses of each other; that is, they must satisfy the following invariants: - +/// `field_container_of(raw_get_field(ptr)) == ptr` for any `ptr: *mut Self`. - +/// `raw_get_field(field_container_of(ptr)) == ptr` for any `ptr: *mut Field<T>`. +/// +/// Use [`macros::HasField`] to generate the impls automatically. +/// +/// # Examples +/// +/// ``` +/// # use core::marker::PhantomData; +/// use kernel::{ +/// macros::HasField, +/// field::{ +/// Field, +/// HasField, // +/// }, // +/// }; +/// +/// struct Work<T, const ID: u64> { +/// _x: isize, +/// _inner: PhantomData<T>, +/// } +/// +/// // Declare that `Work` is a `Field`. +/// impl<T, const ID: u64> Field<T> for Work<T, ID> {} +/// +/// #[derive(HasField)] +/// struct B { +/// #[field] +/// w: Work<B, 2>, +/// a: i32, +/// } +/// +/// const _: () = { +/// const fn assert_has_field<T: HasField<T, Work<T, 2>>>() { } +/// assert_has_field::<B>(); +/// }; +/// ``` +/// +/// [`raw_get_field()`]: HasField::raw_get_field +/// [`field_container_of()`]: HasField::field_container_of +pub unsafe trait HasField <T ,F :Field <T >> { +/// Returns a pointer to the [`Field<T>`] field. +/// +/// # Safety +/// +/// The provided pointer must point at a valid struct of type `Self`. +unsafe fn raw_get_field (ptr: *mut Self ) -> *mut F ; + +/// Returns a pointer to the struct containing [`Field<T>`] field. +/// +/// # Safety +/// +/// The pointer must point at a [`Field<T>`] field in a struct of type `Self`. +unsafe fn field_container_of (ptr: *mut F ) -> *mut Self ; +}
rust/kernel/lib.rs +1 -0
@@ -93,6 +93,7 @@pub mod drm;pub mod error;pub mod faux; +pub mod field;#[cfg( CONFIG_RUST_FW_LOADER_ABSTRACTIONS )] pub mod firmware;pub mod fmt;
rust/kernel/prelude.rs +3 -1
@@ -25,10 +25,12 @@pub use crate ::alloc::{flags::*,Box ,KBox ,KVBox ,KVVec ,KVec ,VBox ,VVec ,Vec };#[doc(no_inline)] -pub use macros::{export, fmt, kunit_tests, module, vtable}; +pub use macros::{export, fmt, kunit_tests, module, vtable,HasField };pub use pin_init::{init, pin_data, pin_init, pinned_drop,InPlaceWrite ,Init ,PinInit ,Zeroable }; +pub use super ::field::{Field ,HasField }; +pub use super ::{build_assert, build_error};// `super::std_vendor` is hidden, which makes the macro inline for some reason.
rust/macros/field.rs new file +85 -0
85 lines not on this page · raw message
rust/macros/lib.rs +11 -0
@@ -13,6 +13,7 @@mod concat_idents;mod export; +mod field;mod fmt;mod helpers;mod kunit;
@@ -486,3 +487,13 @@ pub fn kunit_tests(attr: TokenStream, input: TokenStream) -> TokenStream { .unwrap_or_else(|e| e.into_compile_error()) .into () } + +/// Derives the implementation for `HasField`. +/// +/// See the documentation of `HasField` for more information. +#[proc_macro_derive( HasField , attributes(field))] +pub fn has_field (input:TokenStream ) ->TokenStream { + field::has_field (parse_macro_input! (input)) + .unwrap_or_else (|e| e.into_compile_error ()) + .into () +}
Signature · 1 line
2.50.1 (Apple Git-155)
RFC PATCH 2/7 rust: time: hrtimer: Make `HasField` a super-trait of `HasHrTimer`
In reply to Boqun FengThis 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 = ...;
}--- rust/kernel/time/hrtimer.rs | 70 ++++++++----------------------------- 1 file changed, 15 insertions(+), 55 deletions(-)
rust/kernel/time/hrtimer.rs +15 -55
@@ -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;
Signature · 1 line
2.50.1 (Apple Git-155)
RFC PATCH 3/7 rust: workqueue: Add HasField support for Work
In reply to Boqun FengImplement HasWork via HasField so that manually impl_has_work!() is no longer needed.
--- rust/kernel/workqueue.rs | 60 +++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 22 deletions(-)
rust/kernel/workqueue.rs +38 -22
@@ -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.
Signature · 1 line
2.50.1 (Apple Git-155)
RFC PATCH 4/7 drivers: android: binder: Replace `impl_has_work!` with `#[derive(HasField)]`
In reply to Boqun Feng#[derive(HasField)] offers less code and better ergonomic for defining a struct with a Work in it. Hence replace the current usage of impl_has_work!().
--- drivers/android/binder/process.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/android/binder/process.rs +2 -4
@@ -423,6 +423,7 @@ fn new() -> Self {/// Strictly speaking, there can be multiple of these per process. There is one for each binder fd /// that a process has opened, so processes using several binder contexts have several `Process` /// objects. This ensures that the contexts are fully separated. +#[derive( HasField )] #[pin_data] pub (crate )struct Process {pub (crate ) ctx:Arc <Context >,
@@ -451,6 +452,7 @@ pub(crate) struct Process {// Work node for deferred work item. #[pin] +#[field] defer_work: Work<Process>,// Links for process list in Context.
@@ -460,10 +462,6 @@ pub(crate) struct Process { pub(crate) stats: BinderStats, } -kernel::impl_has_work! { -impl HasWork <Process >for Process { self.defer_work } -} - kernel::list::impl_list_arc_safe! {impl ListArcSafe <0 >for Process { untracked; } }
Signature · 1 line
2.50.1 (Apple Git-155)
RFC PATCH 5/7 rust: sync: Completion: Replace `impl_has_work!` with `#[derive(HasField)]`
In reply to Boqun Feng#[derive(HasField)] offers less code and better ergnomic for defining a struct with a Work in it. Hence replace the current usage of impl_has_work!().
--- rust/kernel/sync/completion.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/sync/completion.rs +3 -5
@@ -17,20 +17,18 @@/// /// ``` /// use kernel::sync::{Arc, Completion}; -/// use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem}; +/// use kernel::workqueue::{self, new_work, Work, WorkItem}; /// +/// #[derive(HasField)] /// #[pin_data] /// struct MyTask { /// #[pin] +/// #[field] /// work: Work<MyTask>, /// #[pin] /// done: Completion, /// } /// -/// impl_has_work! { -/// impl HasWork<Self> for MyTask { self.work } -/// } -/// /// impl MyTask { /// fn new() -> Result<Arc<Self>> { /// let this = Arc::pin_init(pin_init!(MyTask {
Signature · 1 line
2.50.1 (Apple Git-155)
RFC PATCH 6/7 rust: work: Remove `impl_has_work!`
In reply to Boqun FengNow all in-tree users of impl_has_work!() are converted to use #[derive(HasField)], hence remove it.
--- rust/kernel/workqueue.rs | 53 ++-------------------------------------- 1 file changed, 2 insertions(+), 51 deletions(-)
rust/kernel/workqueue.rs +2 -51
@@ -578,55 +578,6 @@ pub unsafe trait HasWork<T, const ID: u64 = 0> {unsafe fn work_container_of (ptr: *mut Work <T ,ID >) -> *mut Self ; } -/// Used to safely implement the [`HasWork<T, ID>`] trait. -/// -/// # Examples -/// -/// ``` -/// use kernel::sync::Arc; -/// use kernel::workqueue::{self, impl_has_work, Work}; -/// -/// struct MyStruct<'a, T, const N: usize> { -/// work_field: Work<MyStruct<'a, T, N>, 17>, -/// f: fn(&'a [T; N]), -/// } -/// -/// impl_has_work! { -/// impl{'a, T, const N: usize} HasWork<MyStruct<'a, T, N>, 17> -/// for MyStruct<'a, T, N> { self.work_field } -/// } -/// ``` -#[macro_export] -macro_rules! impl_has_work { - ($(impl $({$($generics:tt)*})? -HasWork <$work_type:ty $(, $id:tt)?> -for $self:ty - { self.$field:ident } - )*) => {$( -// SAFETY: The implementation of `raw_get_work` only compiles if the field has the right -// type. -unsafe impl $(<$($generics)+>)? $crate::workqueue::HasWork <$work_type $(, $id)?>for $self { -#[inline] -unsafe fn raw_get_work (ptr: *mut Self ) -> *mut $crate::workqueue::Work <$work_type $(, $id)?> { -// SAFETY: The caller promises that the pointer is not dangling. -unsafe { - ::core::ptr::addr_of_mut!((*ptr).$field) - } - } - -#[inline] -unsafe fn work_container_of ( - ptr: *mut $crate::workqueue::Work <$work_type $(, $id)?>, - ) -> *mut Self { -// SAFETY: The caller promises that the pointer points at a field of the right type -// in the right kind of struct. -unsafe { $crate::container_of!(ptr,Self , $field) } - } - } - )*}; -} -pub use impl_has_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()`
@@ -746,8 +697,8 @@ pub unsafe trait HasDelayedWork<T, const ID: u64 = 0>: HasWork<T, ID> {}/// Used to safely implement the [`HasDelayedWork<T, ID>`] trait. /// -/// This macro also implements the [`HasWork`] trait, so you do not need to use [`impl_has_work!`] -/// when using this macro. +/// This macro also implements the [`HasWork`] trait, so you do not need to use `#[has_field]` when +/// using this macro. /// /// # Examples ///
Signature · 1 line
2.50.1 (Apple Git-155)
RFC PATCH 7/7 rust: sync: rcu: Introduce RcuHead
In reply to Boqun FengIn order to support RCU delay free (call_rcu() and kfree_rcu()), the abstraction of the rcu_head is introduced. Types that want to support RCU delay free can specify a RcuHead<T> in it, e.g.
#[derive(HasField)]
struct Foo {
a: i32,
b: i32,
#[field]
rcu_head: RcuHead,
}A wrapper WithRcuHead<T> is also provided for users that want to specify a generic T with a rcu_head.
--- rust/kernel/sync/rcu.rs | 69 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-)
rust/kernel/sync/rcu.rs +68 -1
@@ -4,7 +4,14 @@//! //! C header: [`include/linux/rcupdate.h`](srctree/include/linux/rcupdate.h) -use crate ::{bindings, types::NotThreadSafe }; +use crate ::{ + bindings, + field::{Field ,HasField }, + macros::HasField , + types::{NotThreadSafe ,Opaque }, +}; + +use core::ops::Deref ;/// Evidence that the RCU read side lock is held on the current thread/CPU. ///
@@ -50,3 +57,63 @@ fn drop(&mut self) {pub fn read_lock () ->Guard {Guard ::new () } + +/// RCU head for call backs. +/// +/// # Examples +/// +/// Use `#[derive(HasField)]` macro to specify a struct has a RCU head. +/// +/// ``` +/// use kernel::sync::rcu::RcuHead; +/// +/// #[derive(HasField)] +/// struct Foo { +/// a: i32, +/// #[field] +/// rcu_head: RcuHead, +/// b: i32, +/// } +/// +/// const _: () = { +/// const fn assert_has_field<T: HasField<T, RcuHead>>() { } +/// assert_has_field::<Foo>(); +/// }; +/// ``` +#[repr(transparent)] +pub struct RcuHead (Opaque <bindings::callback_head >); + +impl <T >Field <T >for RcuHead {} + +// SAFETY: `callback_head` doesn't hold anything local to the current execution context, so it's +// safe to transfer to another execution context. +unsafe impl Send for RcuHead {} +// SAFETY: `callback_head` should only be used when it's in the destructor, and accesses to it are +// already unsafe, hence make it `Sync`. +unsafe impl Sync for RcuHead {} + +/// A wrapper that adds an `RcuHead` on `T`. +#[derive( HasField )] +pub struct WithRcuHead <T > { +#[field] + head:RcuHead , + data:T , +} + +impl <T >WithRcuHead <T > { +/// Creates a new wrapper on `T` with `RcuHead`. +pub fn new (data:T ) ->Self { +Self { + head:RcuHead (Opaque ::zeroed ()), + data, + } + } +} + +impl <T >Deref for WithRcuHead <T > { +type Target =T ; + +fn deref (&self) -> &Self ::Target { + &self.data + } +}
Signature · 1 line
2.50.1 (Apple Git-155)
Gary Guo
maintainerQuoted 37 lines · Boqun Feng
Currently we have a few similar places where we use a
Has*trait to describe that a data structure has some types of field in it so that the containing type can do something with it. There are also aimpl_has_*!macro to help implement the trait. While it's working, but it's less ergonomic to me, especially considering the amount of the work we need to do for something new (e.g.rcu_head).Therefore here is the effort to unify them into a proc-macro based solution.
FieldandHasFieldtraits are introduced to generify the "Has A" relationship, and a derive macro#[derive(HasField)]is also added to support automatically implementingHasFieldtrait.This series convert a few users (Work, HrTimer) and introduce a new
FieldtypeRcuHead. These improvements demonstrate how this infrastructure can be used.Some future work is still needed: using
HasFieldforDelayedWorkandListLinkis still missing. Also it's possible to clean upHasWorktrait as well.One known issue is that
#[derive(HasField)]doesn't play alone with#[pin_data]at the moment, for example:#[derive(HasField)] #[pin_data] struct Foo { .. }works, but
#[pin_data] #[derive(HasField)] struct Foo { .. }doesn't. Maybe it's by design or maybe something could be improved by pin-init.
Quoted 6 lines
The patchset is based on today's
rust/rust-next, top commit is:
a7c013f77953('Merge patch series "refactor Rust proc macros withsyn"')Regards,
Boqun
Hi Boqun,
Thanks for working on this.
You currently divide things into two traits, Field<T> which doesn't seem to be doing anything (actually, why does this need to exist at all?) and HasField<T, F> which defines all the field projection.
For some prior art that attempts to have fields, e.g. my field-projection experiemnt crate
https://docs.rs/field-projection
and Benno's work on field-representing-types in the Rust language, we opt to have a type to represent each field instead.
I think we should have a unified projection infrastructure in the kernel, for both intrusive data structure and I/O projection and others, so I think it's useful to have types representing fields (and projection in general, this could also extend to the register! macro). For clarity, let me refer to this as field_of!(Base, foo) and the trait is Projection.
With this infra, the HasField trait would simply looks like this:
trait HasField<Base, FieldType> {
type Field: Projection<Base = Base, Type = FieldType>;
}and the macro derive would generate something like
impl HasField<MyStruct, Work<MyStruct>> {
type Field = field_of!(MyStruct, name_of_work_field);
}Best,
Gary
Quoted 25 lines
Boqun Feng (7):
rust: types: Introduce HasField trait and derive macro
rust: time: hrtimer: MakeHasFielda super-trait ofHasHrTimer
rust: workqueue: Add HasField support for Work
drivers: android: binder: Replaceimpl_has_work!with`#[derive(HasField)]` rust: sync: Completion: Replace `impl_has_work!` with `#[derive(HasField)]`rust: work: Remove
impl_has_work!
rust: sync: rcu: Introduce RcuHeaddrivers/android/binder/process.rs | 6 +- rust/kernel/field.rs | 73 ++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 4 +- rust/kernel/sync/completion.rs | 8 +-- rust/kernel/sync/rcu.rs | 69 ++++++++++++++++++- rust/kernel/time/hrtimer.rs | 70 ++++--------------- rust/kernel/workqueue.rs | 109 +++++++++++------------------- rust/macros/field.rs | 85 +++++++++++++++++++++++ rust/macros/lib.rs | 11 +++ 10 files changed, 299 insertions(+), 137 deletions(-) create mode 100644 rust/kernel/field.rs create mode 100644 rust/macros/field.rs
Boqun Feng
maintainerQuoted 45 lines · Gary Guo
Quoted 37 lines · Boqun Feng
Currently we have a few similar places where we use a
Has*trait to describe that a data structure has some types of field in it so that the containing type can do something with it. There are also aimpl_has_*!macro to help implement the trait. While it's working, but it's less ergonomic to me, especially considering the amount of the work we need to do for something new (e.g.rcu_head).Therefore here is the effort to unify them into a proc-macro based solution.
FieldandHasFieldtraits are introduced to generify the "Has A" relationship, and a derive macro#[derive(HasField)]is also added to support automatically implementingHasFieldtrait.This series convert a few users (Work, HrTimer) and introduce a new
FieldtypeRcuHead. These improvements demonstrate how this infrastructure can be used.Some future work is still needed: using
HasFieldforDelayedWorkandListLinkis still missing. Also it's possible to clean upHasWorktrait as well.One known issue is that
#[derive(HasField)]doesn't play alone with#[pin_data]at the moment, for example:#[derive(HasField)] #[pin_data] struct Foo { .. }works, but
#[pin_data] #[derive(HasField)] struct Foo { .. }doesn't. Maybe it's by design or maybe something could be improved by pin-init.
Quoted 6 lines
The patchset is based on today's
rust/rust-next, top commit is:
a7c013f77953('Merge patch series "refactor Rust proc macros withsyn"')Regards,
Boqun
Quoted 8 lines
Hi Boqun,
Thanks for working on this.
You currently divide things into two traits,
Field<T>which doesn't seem to be doing anything (actually, why does this need to exist at all?) andHasField<T, F>which defines all the field projection.
Oh, you're right, I don't need Field<T> right now. In a certain point, it was used to constrain all "field types" must be generic over their container type. But it's not the case now (see RcuHead).
Quoted 5 lines
For some prior art that attempts to have fields, e.g. my field-projection experiemnt crate
Quoted 9 lines
and Benno's work on field-representing-types in the Rust language, we opt to have a type to represent each field instead.
I think we should have a unified projection infrastructure in the kernel, for both intrusive data structure and I/O projection and others, so I think it's useful to have types representing fields (and projection in general, this could also extend to the
register!macro). For clarity, let me refer to this asfield_of!(Base, foo)and the trait isProjection.
Yep, I actually have an example PR integrating that into workqueue:
https://github.com/Rust-for-Linux/field-projection/pull/2
(of course Work in that case should be generic over the containing type, but that's easy to fix)
I guess the next question is: will you and Benno be willing to port field-projection into kernel source (and keep it aligned with the language feature), so we can switch to that?
Regards,
Boqun
Quoted 15 lines
With this infra, the
HasFieldtrait would simply looks like this:trait HasField<Base, FieldType> { type Field: Projection<Base = Base, Type = FieldType>; }and the macro derive would generate something like
impl HasField<MyStruct, Work<MyStruct>> { type Field = field_of!(MyStruct, name_of_work_field); }Best,
Gary
[..]
Joel Fernandes
maintainerQuoted 4 lines · Boqun Feng
Therefore here is the effort to unify them into a proc-macro based solution. Field and HasField traits are introduced to generify the "Has A" relationship, and a derive macro #[derive(HasField)] is also added to support automatically implementing HasField trait.
Hi Boqun,
One naming concern: the #[field] attribute feels ambiguous since in Rust every struct member is already called a "field." When reading:
#[derive(HasField)]
struct MyStruct {
value: i32,
#[field]
work: Work<MyStruct>,
}it's not immediately obvious why "work" gets #[field] but "value" does not, as both are struct fields.
Would something like #[project] be clearer?
Similarly, HasField could become HasProjection or ProjectField to better distinguish the trait's purpose from the general concept of fields.
Signature · 1 line
Joel Fernandes
Gary Guo
maintainerQuoted 18 lines · Joel Fernandes
Quoted 4 lines · Boqun Feng
Therefore here is the effort to unify them into a proc-macro based solution. Field and HasField traits are introduced to generify the "Has A" relationship, and a derive macro #[derive(HasField)] is also added to support automatically implementing HasField trait.
Hi Boqun,
One naming concern: the #[field] attribute feels ambiguous since in Rust every struct member is already called a "field." When reading:
#[derive(HasField)] struct MyStruct { value: i32, #[field] work: Work<MyStruct>, }
Quoted 7 lines
it's not immediately obvious why "work" gets #[field] but "value" does not, as both are struct fields.
Would something like #[project] be clearer?
Similarly, HasField could become HasProjection or ProjectField to better distinguish the trait's purpose from the general concept of fields.
This is not just for projection, but to establish has-a relation. A struct can project into any of its fields, but for work/hrtimer use cases, it needs to be able to find a specific field.
If we want a different name then I'd suggest #[has].
Best,
Gary