The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Tejun Heo <tj@kernel.org>, Lai Jiangshan <jiangshanlai@gmail.com>,
	 Danilo Krummrich <dakr@kernel.org>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Philipp Stanner" <phasta@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	driver-core@lists.linux.dev, "Alice Ryhl" <aliceryhl@google.com>
Subject: [PATCH v4 1/5] rust: workqueue: support stack-allocated work items
Date: Thu, 19 Nov 2026 14:12:42 +0000	[thread overview]
Message-ID: <20261119-rust-workqueue-v4-1-707c68994940@google.com> (raw)
In-Reply-To: <20261119-rust-workqueue-v4-0-707c68994940@google.com>

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@google.com>
---
 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


  reply	other threads:[~2026-11-19 14:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 14:12 [PATCH v4 0/5] workqueue: OwnedQueue and ScopedWork Alice Ryhl
2026-11-19 14:12 ` Alice Ryhl [this message]
2026-11-19 14:12 ` [PATCH v4 2/5] rust: workqueue: restrict delayed work to global wqs Alice Ryhl
2026-11-19 14:12 ` [PATCH v4 3/5] rust: workqueue: create workqueue subdirectory Alice Ryhl
2026-11-19 14:12 ` [PATCH v4 4/5] rust: workqueue: add creation of workqueues Alice Ryhl
2026-11-19 14:12 ` [PATCH v4 5/5] rust: workqueue: add ScopedWork for non-'static work items Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119-rust-workqueue-v4-1-707c68994940@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=jhubbard@nvidia.com \
    --cc=jiangshanlai@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=phasta@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tj@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox