Skip to content

Commit 8db31d3

Browse files
y86-devojeda
authored andcommitted
rust: workqueue: add #[pin_data] to Work
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]> Reviewed-by: Alice Ryhl <[email protected]> Tested-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 22eed60 commit 8db31d3

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

rust/kernel/workqueue.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ pub trait WorkItem<const ID: u64 = 0> {
346346
/// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
347347
///
348348
/// [`run`]: WorkItemPointer::run
349+
#[pin_data]
349350
#[repr(transparent)]
350351
pub struct Work<T: ?Sized, const ID: u64 = 0> {
352+
#[pin]
351353
work: Opaque<bindings::work_struct>,
352354
_inner: PhantomData<T>,
353355
}
@@ -369,21 +371,22 @@ impl<T: ?Sized, const ID: u64> Work<T, ID> {
369371
where
370372
T: WorkItem<ID>,
371373
{
372-
// SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as the work
373-
// item function.
374-
unsafe {
375-
kernel::init::pin_init_from_closure(move |slot| {
376-
let slot = Self::raw_get(slot);
377-
bindings::init_work_with_key(
378-
slot,
379-
Some(T::Pointer::run),
380-
false,
381-
name.as_char_ptr(),
382-
key.as_ptr(),
383-
);
384-
Ok(())
385-
})
386-
}
374+
pin_init!(Self {
375+
work <- Opaque::ffi_init(|slot| {
376+
// SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as
377+
// the work item function.
378+
unsafe {
379+
bindings::init_work_with_key(
380+
slot,
381+
Some(T::Pointer::run),
382+
false,
383+
name.as_char_ptr(),
384+
key.as_ptr(),
385+
)
386+
}
387+
}),
388+
_inner: PhantomData,
389+
})
387390
}
388391

389392
/// Get a pointer to the inner `work_struct`.

0 commit comments

Comments
 (0)