Skip to content

Commit

Permalink
rust: workqueue: add #[pin_data] to Work
Browse files Browse the repository at this point in the history
The previous two patches made it possible to add `#[pin_data]` on
structs with default generic parameter values.
This patch makes `Work` use `#[pin_data]` and removes an invocation of
`pin_init_from_closure`. This function is intended as a low level manual
escape hatch, so it is better to rely on the safe `pin_init!` macro.

Signed-off-by: Benno Lossin <[email protected]>
Reviewed-by: Martin Rodriguez Reboredo <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
  • Loading branch information
Benno Lossin authored and fbq committed Dec 28, 2023
1 parent 1fd25cc commit 8f7e376
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions rust/kernel/workqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,10 @@ pub trait WorkItem<const ID: u64 = 0> {
/// Wraps the kernel's C `struct work_struct`.
///
/// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
#[pin_data]
#[repr(transparent)]
pub struct Work<T: ?Sized, const ID: u64 = 0> {
#[pin]
work: Opaque<bindings::work_struct>,
_inner: PhantomData<T>,
}
Expand All @@ -357,21 +359,22 @@ impl<T: ?Sized, const ID: u64> Work<T, ID> {
where
T: WorkItem<ID>,
{
// SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as the work
// item function.
unsafe {
kernel::init::pin_init_from_closure(move |slot| {
let slot = Self::raw_get(slot);
bindings::init_work_with_key(
slot,
Some(T::Pointer::run),
false,
name.as_char_ptr(),
key.as_ptr(),
);
Ok(())
})
}
pin_init!(Self {
work <- Opaque::ffi_init(|slot| {
// SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as
// the work item function.
unsafe {
bindings::init_work_with_key(
slot,
Some(T::Pointer::run),
false,
name.as_char_ptr(),
key.as_ptr(),
)
}
}),
_inner: PhantomData,
})
}

/// Get a pointer to the inner `work_struct`.
Expand Down

0 comments on commit 8f7e376

Please sign in to comment.