rust: workqueue: remove HasWork::OFFSET
PATCH v3 1/1 rust: workqueue: remove HasWork::OFFSET
Implement HasWork::work_container_of in impl_has_work!, narrowing the interface of HasWork and replacing pointer arithmetic with container_of!. Remove the provided implementation of HasWork::get_work_offset without replacement; an implementation is already generated in impl_has_work!. Remove the Self: Sized bound on HasWork::work_container_of which was apparently necessary to access OFFSET as OFFSET no longer exists.
A similar API change was discussed on the hrtimer series[1].
---
Changes in v3:
- Extract first commit to its own series as it is shared with other series.
- Reword HasWork safety comment.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Rebase on v6.15-rc1.
- Add WORKQUEUE maintainers to cc.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- rust/kernel/workqueue.rs | 50 ++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 33 deletions(-)
rust/kernel/workqueue.rs +17 -33
@@ -429,51 +429,28 @@ pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {/// /// # Safety /// -/// The [`OFFSET`] constant must be the offset of a field in `Self` of type [`Work<T, ID>`]. The -/// methods on this trait must have exactly the behavior that the definitions given below have. +/// The methods [`raw_get_work`] and [`work_container_of`] must return valid pointers and must be +/// true inverses of each other; that is, they must satisfy the following invariants: +/// - `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 -/// [`OFFSET`]: HasWork::OFFSET +/// [`raw_get_work`]: HasWork::raw_get_work +/// [`work_container_of`]: HasWork::work_container_of pub unsafe trait HasWork <T ,const ID :u64 =0 > { -/// The offset of the [`Work<T, ID>`] field. -const OFFSET :usize ; - -/// Returns the offset of the [`Work<T, ID>`] field. -/// -/// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not -/// [`Sized`]. -/// -/// [`OFFSET`]: HasWork::OFFSET -#[inline] -fn get_work_offset (&self) ->usize { -Self ::OFFSET - } -/// Returns a pointer to the [`Work<T, ID>`] field. /// /// # Safety /// /// The provided pointer must point at a valid struct of type `Self`. -#[inline] -unsafe fn raw_get_work (ptr: *mut Self ) -> *mut Work <T ,ID > { -// SAFETY: The caller promises that the pointer is valid. -unsafe { (ptras *mut u8 ).add (Self ::OFFSET )as *mut Work <T ,ID > } - } +unsafe fn raw_get_work (ptr: *mut Self ) -> *mut Work <T ,ID >;/// Returns a pointer to the struct containing the [`Work<T, ID>`] field. /// /// # Safety /// /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`. -#[inline] -unsafe fn work_container_of (ptr: *mut Work <T ,ID >) -> *mut Self -where -Self :Sized , - { -// SAFETY: The caller promises that the pointer points at a field of the right type in the -// right kind of struct. -unsafe { (ptras *mut u8 ).sub (Self ::OFFSET )as *mut Self } - } +unsafe fn work_container_of (ptr: *mut Work <T ,ID >) -> *mut Self ; }/// Used to safely implement the [`HasWork<T, ID>`] trait.
@@ -504,8 +481,6 @@ macro_rules! impl_has_work {// 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 { - const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize; - #[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.
@@ -513,6 +488,15 @@ unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_typ ::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) } + } } )*}; }
---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250307-no-offset-e463667a72fb
prerequisite-change-id: 20250409-container-of-mutness-b153dab4388d:v1
prerequisite-patch-id: 53d5889db599267f87642bb0ae3063c29bc24863
Best regards,
Signature · 1 line
Tamir Duberstein <[email protected]>
Quoted 9 lines · Tamir Duberstein
Implement
HasWork::work_container_ofinimpl_has_work!, narrowing the interface ofHasWorkand replacing pointer arithmetic withcontainer_of!. Remove the provided implementation ofHasWork::get_work_offsetwithout replacement; an implementation is already generated inimpl_has_work!. Remove theSelf: Sizedbound onHasWork::work_container_ofwhich was apparently necessary to accessOFFSETasOFFSETno longer exists.
Quoted 8 lines
A similar API change was discussed on the hrtimer series[1].
Reviewed-by Benno Lossin <[email protected]>Reviewed-by Alice Ryhl <[email protected]>Tested-by Alice Ryhl <[email protected]>Reviewed-by Andreas Hindborg <[email protected]>Signed-off-by Tamir Duberstein <[email protected]>
Seems reasonable enough.
Quoted 115 lines
---
Changes in v3:
- Extract first commit to its own series as it is shared with other series.
- RewordHasWorksafety comment.
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- Rebase on v6.15-rc1.
- Add WORKQUEUE maintainers to cc.
- Link to v1: https://lore.kernel.org/r/[email protected]--- rust/kernel/workqueue.rs | 50 ++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 33 deletions(-)rust/kernel/workqueue.rs +17
-34@@ -429,51 +429,28 @@ pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {/// /// # Safety /// -/// The [`OFFSET`] constant must be the offset of a field in `Self` of type [`Work<T, ID>`]. The -/// methods on this trait must have exactly the behavior that the definitions given below have. +/// The methods [`raw_get_work`] and [`work_container_of`] must return valid pointers and must be +/// true inverses of each other; that is, they must satisfy the following invariants: +/// - `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 -/// [`OFFSET`]: HasWork::OFFSET +/// [`raw_get_work`]: HasWork::raw_get_work +/// [`work_container_of`]: HasWork::work_container_of pub unsafe trait HasWork <T ,const ID :u64 =0 > { -/// The offset of the [`Work<T, ID>`] field. -const OFFSET :usize ; - -/// Returns the offset of the [`Work<T, ID>`] field. -/// -/// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not -/// [`Sized`]. -/// -/// [`OFFSET`]: HasWork::OFFSET -#[inline] -fn get_work_offset (&self) ->usize { -Self ::OFFSET - } -/// Returns a pointer to the [`Work<T, ID>`] field. /// /// # Safety /// /// The provided pointer must point at a valid struct of type `Self`. -#[inline] -unsafe fn raw_get_work (ptr: *mut Self ) -> *mut Work <T ,ID > { -// SAFETY: The caller promises that the pointer is valid. -unsafe { (ptras *mut u8 ).add (Self ::OFFSET )as *mut Work <T ,ID > } - } +unsafe fn raw_get_work (ptr: *mut Self ) -> *mut Work <T ,ID >;/// Returns a pointer to the struct containing the [`Work<T, ID>`] field. /// /// # Safety /// /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`. -#[inline] -unsafe fn work_container_of (ptr: *mut Work <T ,ID >) -> *mut Self -where -Self :Sized , - { -// SAFETY: The caller promises that the pointer points at a field of the right type in the -// right kind of struct. -unsafe { (ptras *mut u8 ).sub (Self ::OFFSET )as *mut Self } - } +unsafe fn work_container_of (ptr: *mut Work <T ,ID >) -> *mut Self ; }/// Used to safely implement the [`HasWork<T, ID>`] trait. @@ -504,8 +481,6 @@ macro_rules! impl_has_work {// 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 { - const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize; - #[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.@@ -513,6 +488,15 @@ unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_typ ::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) } + } } )*}; } ---base-commit:
0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250307-no-offset-e463667a72fb
prerequisite-change-id: 20250409-container-of-mutness-b153dab4388d:v1
prerequisite-patch-id:53d5889db599267f87642bb0ae3063c29bc24863Best regards,
--
Tamir Duberstein <[email protected]>
Tejun Heo
Acked-byQuoted 19 lines · Alice Ryhl
Quoted 9 lines · Tamir Duberstein
Implement
HasWork::work_container_ofinimpl_has_work!, narrowing the interface ofHasWorkand replacing pointer arithmetic withcontainer_of!. Remove the provided implementation ofHasWork::get_work_offsetwithout replacement; an implementation is already generated inimpl_has_work!. Remove theSelf: Sizedbound onHasWork::work_container_ofwhich was apparently necessary to accessOFFSETasOFFSETno longer exists.Quoted 8 lines
A similar API change was discussed on the hrtimer series[1].
Reviewed-by Benno Lossin <[email protected]>Reviewed-by Alice Ryhl <[email protected]>Tested-by Alice Ryhl <[email protected]>Reviewed-by Andreas Hindborg <[email protected]>Signed-off-by Tamir Duberstein <[email protected]>
Quoted 2 lines
Seems reasonable enough.
Reviewed-by Alice Ryhl <[email protected]>
Please let me know how you want it routed.
Thanks.
Signature · 1 line
tejun
Quoted 4 lines · Tejun Heo
Acked-by Tejun Heo <[email protected]>Please let me know how you want it routed.
If you would like to take it, then that would be nice. Otherwise, I can also take it.
Thanks!
Cheers,
Miguel
Quoted 8 lines · Miguel Ojeda
Quoted 4 lines · Tejun Heo
Acked-by Tejun Heo <[email protected]>Please let me know how you want it routed.
If you would like to take it, then that would be nice. Otherwise, I can also take it.
Alright, applied to wq/for-6.16.
Thanks.
Signature · 1 line
tejun
Quoted 4 lines · Tejun Heo
Quoted 2 lines · Miguel Ojeda
Quoted 1 line · Tejun Heo
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Acked-by Tejun Heo <[email protected]>Please let me know how you want it routed.
If you would like to take it, then that would be nice. Otherwise, I can also take it.
Alright, applied to wq/for-6.16.
Looks like you took it without the prerequisite patch.
Quoted 7 lines · Tamir Duberstein
Quoted 6 lines · Tejun Heo
Quoted 4 lines · Miguel Ojeda
Quoted 3 lines · Tejun Heo
Acked-by Tejun Heo <[email protected]>
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Please let me know how you want it routed.
If you would like to take it, then that would be nice. Otherwise, I can also take it.
Alright, applied to wq/for-6.16.
Looks like you took it without the prerequisite patch.
Ah, okay. Lemme track back and apply the prerequisite first.
Thanks.
Signature · 1 line
tejun
Quoted 10 lines · Tejun Heo
Quoted 9 lines · Tamir Duberstein
Quoted 8 lines · Tejun Heo
Quoted 6 lines · Miguel Ojeda
Quoted 4 lines · Tejun Heo
Acked-by Tejun Heo <[email protected]>Please let me know how you want it routed.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
If you would like to take it, then that would be nice. Otherwise, I can also take it.
Alright, applied to wq/for-6.16.
Looks like you took it without the prerequisite patch.
Ah, okay. Lemme track back and apply the prerequisite first.
Miguel, now that I thought a bit about it, I probably shouldn't be applying patches that I don't understand enough to tell which one depends on what. Can you please apply this series and the delayed_work one?
Thanks.
Signature · 1 line
tejun
Quoted 4 lines · Tejun Heo
Miguel, now that I thought a bit about it, I probably shouldn't be applying patches that I don't understand enough to tell which one depends on what. Can you please apply this series and the
delayed_workone?
Yeah, sorry.
Tamir: I know you the "prerequisite" at the footer, but it is best to explain what has happened in words. If I understand correctly, the last "version" is:
https://lore.kernel.org/all/[email protected]/
Right?
Cheers,
Miguel
Quoted 9 lines · Miguel Ojeda
Quoted 4 lines · Tejun Heo
Miguel, now that I thought a bit about it, I probably shouldn't be applying patches that I don't understand enough to tell which one depends on what. Can you please apply this series and the
delayed_workone?Yeah, sorry.
Quoted 7 lines
Tamir: I know you the "prerequisite" at the footer, but it is best to explain what has happened in words. If I understand correctly, the last "version" is:
https://lore.kernel.org/all/[email protected]/Right?
Yeah, correct. FWIW I was hoping the automatic prerequisite handling in b4 was sufficient - I'll also include mention of prerequisites in the cover letter in the future.
Thanks all.
Miguel Ojeda
AppliedQuoted 9 lines · Tamir Duberstein
Implement
HasWork::work_container_ofinimpl_has_work!, narrowing the interface ofHasWorkand replacing pointer arithmetic withcontainer_of!. Remove the provided implementation ofHasWork::get_work_offsetwithout replacement; an implementation is already generated inimpl_has_work!. Remove theSelf: Sizedbound onHasWork::work_container_ofwhich was apparently necessary to accessOFFSETasOFFSETno longer exists.
Quoted 8 lines
A similar API change was discussed on the hrtimer series[1].
Reviewed-by Benno Lossin <[email protected]>Reviewed-by Alice Ryhl <[email protected]>Tested-by Alice Ryhl <[email protected]>Reviewed-by Andreas Hindborg <[email protected]>Signed-off-by Tamir Duberstein <[email protected]>
Applied to rust-next -- thanks everyone!
Cheers,
Miguel