[PATCH v4 1/5] rust: workqueue: support stack-allocated work items

From: Alice Ryhl

Date: Thu Nov 19 2026 - 09:13:18 EST


Pass `object_is_on_stack(work)` to `__init_work()` in
`rust_helper_init_work_with_key()` instead of taking an explicit
`onstack` parameter, and call `destroy_work_on_stack()` when `Work` is
dropped. This ensures `CONFIG_DEBUG_OBJECTS_WORK` properly tracks and
cleans up stack-allocated work items without requiring separate
constructors.

Fixes: 7324b88975c5 ("rust: workqueue: add helper for defining work_struct fields")
Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
---
rust/helpers/workqueue.c | 9 +++++++--
rust/kernel/workqueue.rs | 13 ++++++++++---
2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/rust/helpers/workqueue.c b/rust/helpers/workqueue.c
index ce1c3a5b2150..337382acfbf5 100644
--- a/rust/helpers/workqueue.c
+++ b/rust/helpers/workqueue.c
@@ -1,16 +1,21 @@
// SPDX-License-Identifier: GPL-2.0

+#include <linux/sched/task_stack.h>
#include <linux/workqueue.h>

__rust_helper void rust_helper_init_work_with_key(struct work_struct *work,
work_func_t func,
- bool onstack,
const char *name,
struct lock_class_key *key)
{
- __init_work(work, onstack);
+ __init_work(work, object_is_on_stack(work));
work->data = (atomic_long_t)WORK_DATA_INIT();
lockdep_init_map(&work->lockdep_map, name, key, 0);
INIT_LIST_HEAD(&work->entry);
work->func = func;
}
+
+__rust_helper void rust_helper_destroy_work_on_stack(struct work_struct *work)
+{
+ destroy_work_on_stack(work);
+}
diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
index a872f9fffb90..6eb6c40138f2 100644
--- a/rust/kernel/workqueue.rs
+++ b/rust/kernel/workqueue.rs
@@ -481,7 +481,7 @@ pub trait WorkItem<const ID: u64 = 0> {
/// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
///
/// [`run`]: WorkItemPointer::run
-#[pin_data]
+#[pin_data(PinnedDrop)]
#[repr(transparent)]
pub struct Work<T: ?Sized, const ID: u64 = 0> {
#[pin]
@@ -513,7 +513,6 @@ pub fn new(name: &'static CStr, key: Pin<&'static LockClassKey>) -> impl PinInit
bindings::init_work_with_key(
slot,
Some(T::Pointer::run),
- false,
name.as_char_ptr(),
key.as_ptr(),
)
@@ -539,6 +538,15 @@ pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
}
}

+#[pinned_drop]
+impl<T: ?Sized, const ID: u64> PinnedDrop for Work<T, ID> {
+ fn drop(self: Pin<&mut Self>) {
+ // SAFETY: `self.work.get()` points to a valid `work_struct` initialized in `Work::new`,
+ // and the work item is not currently queued because `Self` is being dropped.
+ unsafe { bindings::destroy_work_on_stack(self.work.get()) }
+ }
+}
+
/// 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
@@ -686,7 +694,6 @@ pub fn new(
bindings::init_work_with_key(
core::ptr::addr_of_mut!((*slot).work),
Some(T::Pointer::run),
- false,
work_name.as_char_ptr(),
work_key.as_ptr(),
)

--
2.56.1.708.g77570a6a1d-goog