* [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork
@ 2026-08-07 16:52 Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 1/6] rust: workqueue: replace deprecated system_wq with system_{percpu,dfl}_wq Danilo Krummrich
` (5 more replies)
0 siblings, 6 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 16:52 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core, Danilo Krummrich
This series extends the Rust workqueue bindings with the ability to create
custom workqueues, enqueue non-'static work items on scoped queues, and
introduces ScopedWork, a work item type that cancels synchronously on drop,
allowing drivers to use workqueues with lifetime-bound data.
This picks up and extends prior work from Alice [1] and Onur [2]; changes I
subsequently applied, if any, are documented in the corresponding patch.
I also added a patch to replace the deprecated system_wq with
system_{percpu,dfl}_wq, which I originally sent with the WaitQueue series [3],
as it makes a bit more sense in this context.
[1] https://lore.kernel.org/all/20260312-create-workqueue-v4-0-ea39c351c38f@google.com
[2] https://lore.kernel.org/all/20260617144645.253444-1-work@onurozkan.dev
[3] https://lore.kernel.org/all/20260726223613.1242940-3-dakr@kernel.org
Changes in v2:
- Merge scoped_work.rs and scoped_queue.rs into scoped.rs
- Introduce ScopedWorkRef<T> and wrap it in Opaque inside ScopedWork to avoid
&mut aliasing in drop
- Pass &ScopedWorkRef<Self> to ScopedWorkItem::run to enable self-re-enqueue
- Add Deref<Target = Queue> for ScopedQueue; remove duplicate enqueue_scoped()
- Add Send + Sync for OwnedQueue
- Fix stale workqueue::system() references in docs
- Drop Work::cancel_work_sync()
- Make sysfs() available on ordered workqueues
- Add #[inline] annotations throughout
Alice Ryhl (3):
rust: workqueue: restrict delayed work to global wqs
rust: workqueue: create workqueue subdirectory
rust: workqueue: add creation of workqueues
Danilo Krummrich (2):
rust: workqueue: replace deprecated system_wq with
system_{percpu,dfl}_wq
rust: workqueue: add ScopedWork for non-'static work items
Onur Özkan (1):
rust: workqueue: add ScopedQueue for lifetime bound items
Documentation/rust/testing.rst | 2 +-
.../translations/zh_CN/rust/testing.rst | 2 +-
MAINTAINERS | 1 +
drivers/android/binder/process.rs | 4 +-
rust/helpers/workqueue.c | 7 +
rust/kernel/sync/completion.rs | 2 +-
rust/kernel/workqueue/builder.rs | 389 ++++++++++++
.../kernel/{workqueue.rs => workqueue/mod.rs} | 149 ++++-
rust/kernel/workqueue/scoped.rs | 568 ++++++++++++++++++
9 files changed, 1089 insertions(+), 35 deletions(-)
create mode 100644 rust/kernel/workqueue/builder.rs
rename rust/kernel/{workqueue.rs => workqueue/mod.rs} (90%)
create mode 100644 rust/kernel/workqueue/scoped.rs
base-commit: b48373c901951fad1a26bd7c33ad91172b3945b5
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 1/6] rust: workqueue: replace deprecated system_wq with system_{percpu,dfl}_wq
2026-08-07 16:52 [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork Danilo Krummrich
@ 2026-08-07 16:52 ` Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 2/6] rust: workqueue: restrict delayed work to global wqs Danilo Krummrich
` (4 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 16:52 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core, Danilo Krummrich
system_wq is deprecated and triggers a runtime warning:
[ 0.857414] workqueue: work func ...WorkItemPointerKy0_E3runB7_ enqueued on deprecated workqueue. Use system_{percpu|dfl}_wq instead.
Replace system() with system_percpu() and system_dfl(), to match the
previous behavior of the deprecated system() and convert doc examples
and tests to system_dfl().
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
Documentation/rust/testing.rst | 2 +-
.../translations/zh_CN/rust/testing.rst | 2 +-
drivers/android/binder/process.rs | 4 +-
rust/kernel/sync/completion.rs | 2 +-
rust/kernel/workqueue.rs | 42 ++++++++++++-------
5 files changed, 32 insertions(+), 20 deletions(-)
diff --git a/Documentation/rust/testing.rst b/Documentation/rust/testing.rst
index e3943aceceb9..73046523a9a2 100644
--- a/Documentation/rust/testing.rst
+++ b/Documentation/rust/testing.rst
@@ -97,7 +97,7 @@ operator are also supported as usual, e.g.:
/// ```
/// # use kernel::{spawn_work_item, workqueue};
- /// spawn_work_item!(workqueue::system(), || pr_info!("x\n"))?;
+ /// spawn_work_item!(workqueue::system_dfl(), || pr_info!("x\n"))?;
/// # Ok::<(), Error>(())
/// ```
diff --git a/Documentation/translations/zh_CN/rust/testing.rst b/Documentation/translations/zh_CN/rust/testing.rst
index ca81f1cef6eb..5fdd553f2b41 100644
--- a/Documentation/translations/zh_CN/rust/testing.rst
+++ b/Documentation/translations/zh_CN/rust/testing.rst
@@ -93,7 +93,7 @@ KUnit 测试即文档测试
/// ```
/// # use kernel::{spawn_work_item, workqueue};
- /// spawn_work_item!(workqueue::system(), || pr_info!("x\n"))?;
+ /// spawn_work_item!(workqueue::system_dfl(), || pr_info!("x\n"))?;
/// # Ok::<(), Error>(())
/// ```
diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/process.rs
index 96b8440ceac6..c0266fdaa598 100644
--- a/drivers/android/binder/process.rs
+++ b/drivers/android/binder/process.rs
@@ -1632,7 +1632,7 @@ pub(crate) fn release(this: Arc<Process>, _file: &File) {
if should_schedule {
// Ignore failures to schedule to the workqueue. Those just mean that we're already
// scheduled for execution.
- let _ = workqueue::system().enqueue(this);
+ let _ = workqueue::system_percpu().enqueue(this);
}
drop(binderfs_file);
@@ -1649,7 +1649,7 @@ pub(crate) fn flush(this: ArcBorrow<'_, Process>) -> Result {
if should_schedule {
// Ignore failures to schedule to the workqueue. Those just mean that we're already
// scheduled for execution.
- let _ = workqueue::system().enqueue(Arc::from(this));
+ let _ = workqueue::system_percpu().enqueue(Arc::from(this));
}
Ok(())
}
diff --git a/rust/kernel/sync/completion.rs b/rust/kernel/sync/completion.rs
index 35ff049ff078..1771bfc0ade2 100644
--- a/rust/kernel/sync/completion.rs
+++ b/rust/kernel/sync/completion.rs
@@ -38,7 +38,7 @@
/// done <- Completion::new(),
/// }), GFP_KERNEL)?;
///
-/// let _ = workqueue::system().enqueue(this.clone());
+/// let _ = workqueue::system_dfl().enqueue(this.clone());
///
/// Ok(this)
/// }
diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
index 7e253b6f299c..7bc07ccd1ebb 100644
--- a/rust/kernel/workqueue.rs
+++ b/rust/kernel/workqueue.rs
@@ -67,7 +67,7 @@
//! /// This method will enqueue the struct for execution on the system workqueue, where its value
//! /// will be printed.
//! fn print_later(val: Arc<MyStruct>) {
-//! let _ = workqueue::system().enqueue(val);
+//! let _ = workqueue::system_dfl().enqueue(val);
//! }
//! # print_later(MyStruct::new(42).unwrap());
//! ```
@@ -121,11 +121,11 @@
//! }
//!
//! fn print_1_later(val: Arc<MyStruct>) {
-//! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 1>(val);
+//! let _ = workqueue::system_dfl().enqueue::<Arc<MyStruct>, 1>(val);
//! }
//!
//! fn print_2_later(val: Arc<MyStruct>) {
-//! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 2>(val);
+//! let _ = workqueue::system_dfl().enqueue::<Arc<MyStruct>, 2>(val);
//! }
//! # print_1_later(MyStruct::new(24, 25).unwrap());
//! # print_2_later(MyStruct::new(41, 42).unwrap());
@@ -171,13 +171,13 @@
//! /// This method will enqueue the struct for execution on the system workqueue, where its value
//! /// will be printed 12 jiffies later.
//! fn print_later(val: Arc<MyStruct>) {
-//! let _ = workqueue::system().enqueue_delayed(val, 12);
+//! let _ = workqueue::system_dfl().enqueue_delayed(val, 12);
//! }
//!
//! /// It is also possible to use the ordinary `enqueue` method together with `DelayedWork`. This
//! /// is equivalent to calling `enqueue_delayed` with a delay of zero.
//! fn print_now(val: Arc<MyStruct>) {
-//! let _ = workqueue::system().enqueue(val);
+//! let _ = workqueue::system_dfl().enqueue(val);
//! }
//! # print_later(MyStruct::new(42).unwrap());
//! # print_now(MyStruct::new(42).unwrap());
@@ -1024,20 +1024,32 @@ unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for ARef<T>
{
}
-/// Returns the system work queue (`system_wq`).
+/// Returns the system per-cpu work queue (`system_percpu_wq`).
///
/// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are
/// users which expect relatively short queue flush time.
///
/// Callers shouldn't queue work items which can run for too long.
-pub fn system() -> &'static Queue {
- // SAFETY: `system_wq` is a C global, always available.
- unsafe { Queue::from_raw(bindings::system_wq) }
+#[inline]
+pub fn system_percpu() -> &'static Queue {
+ // SAFETY: `system_percpu_wq` is a C global, always available.
+ unsafe { Queue::from_raw(bindings::system_percpu_wq) }
+}
+
+/// Returns the system default (unbound) work queue (`system_dfl_wq`).
+///
+/// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items
+/// are executed immediately as long as `max_active` limit is not reached and resources are
+/// available.
+#[inline]
+pub fn system_dfl() -> &'static Queue {
+ // SAFETY: `system_dfl_wq` is a C global, always available.
+ unsafe { Queue::from_raw(bindings::system_dfl_wq) }
}
/// Returns the system high-priority work queue (`system_highpri_wq`).
///
-/// It is similar to the one returned by [`system`] but for work items which require higher
+/// It is similar to the one returned by [`system_percpu`] but for work items which require higher
/// scheduling priority.
pub fn system_highpri() -> &'static Queue {
// SAFETY: `system_highpri_wq` is a C global, always available.
@@ -1046,8 +1058,8 @@ pub fn system_highpri() -> &'static Queue {
/// Returns the system work queue for potentially long-running work items (`system_long_wq`).
///
-/// It is similar to the one returned by [`system`] but may host long running work items. Queue
-/// flushing might take relatively long.
+/// It is similar to the one returned by [`system_percpu`] but may host long running work items.
+/// Queue flushing might take relatively long.
pub fn system_long() -> &'static Queue {
// SAFETY: `system_long_wq` is a C global, always available.
unsafe { Queue::from_raw(bindings::system_long_wq) }
@@ -1065,7 +1077,7 @@ pub fn system_unbound() -> &'static Queue {
/// Returns the system freezable work queue (`system_freezable_wq`).
///
-/// It is equivalent to the one returned by [`system`] except that it's freezable.
+/// It is equivalent to the one returned by [`system_percpu`] except that it's freezable.
///
/// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
/// items on the workqueue are drained and no new work item starts execution until thawed.
@@ -1078,7 +1090,7 @@ pub fn system_freezable() -> &'static Queue {
///
/// It is inclined towards saving power and is converted to "unbound" variants if the
/// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one
-/// returned by [`system`].
+/// returned by [`system_percpu`].
pub fn system_power_efficient() -> &'static Queue {
// SAFETY: `system_power_efficient_wq` is a C global, always available.
unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
@@ -1097,7 +1109,7 @@ pub fn system_freezable_power_efficient() -> &'static Queue {
/// Returns the system bottom halves work queue (`system_bh_wq`).
///
-/// It is similar to the one returned by [`system`] but for work items which
+/// It is similar to the one returned by [`system_percpu`] but for work items which
/// need to run from a softirq context.
pub fn system_bh() -> &'static Queue {
// SAFETY: `system_bh_wq` is a C global, always available.
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 2/6] rust: workqueue: restrict delayed work to global wqs
2026-08-07 16:52 [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 1/6] rust: workqueue: replace deprecated system_wq with system_{percpu,dfl}_wq Danilo Krummrich
@ 2026-08-07 16:52 ` Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 3/6] rust: workqueue: create workqueue subdirectory Danilo Krummrich
` (3 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 16:52 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core, stable,
Danilo Krummrich
From: Alice Ryhl <aliceryhl@google.com>
When a workqueue is shut down, delayed work that is pending but not
scheduled does not get properly cleaned up, so it's not safe to use
`enqueue_delayed` on a workqueue that might be destroyed. To fix this,
restricted `enqueue_delayed` to static queues.
This may be fixed in the future by an approach along the lines of [1].
Cc: stable@vger.kernel.org
Fixes: 7c098cd5eaae ("workqueue: rust: add delayed work items")
Reviewed-by: John Hubbard <jhubbard@nvidia.com>
Reviewed-by: Danilo Krummrich <dakr@kernel.org>
Reviewed-by: Gary Guo <gary@garyguo.net>
Link: https://lore.kernel.org/r/20250423-destroy-workqueue-flush-v1-1-3d74820780a5@google.com [1]
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/workqueue.rs | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
index 7bc07ccd1ebb..399d2ed24135 100644
--- a/rust/kernel/workqueue.rs
+++ b/rust/kernel/workqueue.rs
@@ -302,8 +302,15 @@ pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
///
/// This may fail if the work item is already enqueued in a workqueue.
///
+ /// This is only valid for global workqueues (with static lifetimes) because those are the only
+ /// ones that outlive all possible delayed work items.
+ ///
/// The work item will be submitted using `WORK_CPU_UNBOUND`.
- pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::EnqueueOutput
+ pub fn enqueue_delayed<W, const ID: u64>(
+ &'static self,
+ w: W,
+ delay: Jiffies,
+ ) -> W::EnqueueOutput
where
W: RawDelayedWorkItem<ID> + Send + 'static,
{
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 3/6] rust: workqueue: create workqueue subdirectory
2026-08-07 16:52 [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 1/6] rust: workqueue: replace deprecated system_wq with system_{percpu,dfl}_wq Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 2/6] rust: workqueue: restrict delayed work to global wqs Danilo Krummrich
@ 2026-08-07 16:52 ` Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 4/6] rust: workqueue: add creation of workqueues Danilo Krummrich
` (2 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 16:52 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core, Danilo Krummrich
From: Alice Ryhl <aliceryhl@google.com>
The following patch will implement a workqueue builder in a separate
file. To prepare for that, create a rust/kernel/workqueue subdirectory
and move the existing file.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
MAINTAINERS | 1 +
rust/kernel/{workqueue.rs => workqueue/mod.rs} | 0
2 files changed, 1 insertion(+)
rename rust/kernel/{workqueue.rs => workqueue/mod.rs} (100%)
diff --git a/MAINTAINERS b/MAINTAINERS
index f672858996f0..9cee804d840d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -29155,6 +29155,7 @@ F: Documentation/core-api/workqueue.rst
F: include/linux/workqueue.h
F: kernel/workqueue.c
F: kernel/workqueue_internal.h
+F: rust/kernel/workqueue/
WWAN DRIVERS
M: Loic Poulain <loic.poulain@oss.qualcomm.com>
diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue/mod.rs
similarity index 100%
rename from rust/kernel/workqueue.rs
rename to rust/kernel/workqueue/mod.rs
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 4/6] rust: workqueue: add creation of workqueues
2026-08-07 16:52 [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork Danilo Krummrich
` (2 preceding siblings ...)
2026-08-07 16:52 ` [PATCH v2 3/6] rust: workqueue: create workqueue subdirectory Danilo Krummrich
@ 2026-08-07 16:52 ` Danilo Krummrich
2026-08-07 22:39 ` Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 5/6] rust: workqueue: add ScopedQueue for lifetime bound items Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items Danilo Krummrich
5 siblings, 1 reply; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 16:52 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core, Danilo Krummrich
From: Alice Ryhl <aliceryhl@google.com>
Creating workqueues is needed by various GPU drivers. Not only does it
give you better control over execution, it also allows devices to ensure
that all tasks have exited before the device is unbound (or similar) by
running the workqueue destructor.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
[ * Fix import formatting of ptr::{self, NonNull},
* rename T to Kind,
* new_ordered(): set max_active: 1,
* new_power_efficient(): WQ_UNBOUND | WQ_POWER_EFFICIENT, add
.percpu(),
* new_bh(): WQ_PERCPU | WQ_BH, removed .percpu(),
* move sysfs() to TypeNormal,
* impl Send + Sync for OwnedQueue,
* add missing inline annotations,
* various doc improvements: new_percpu, max_active, cpu_intensive,
freezable.
- Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/helpers/workqueue.c | 7 +
rust/kernel/workqueue/builder.rs | 389 +++++++++++++++++++++++++++++++
rust/kernel/workqueue/mod.rs | 51 +++-
3 files changed, 444 insertions(+), 3 deletions(-)
create mode 100644 rust/kernel/workqueue/builder.rs
diff --git a/rust/helpers/workqueue.c b/rust/helpers/workqueue.c
index ce1c3a5b2150..e4b9d1b3d6bf 100644
--- a/rust/helpers/workqueue.c
+++ b/rust/helpers/workqueue.c
@@ -14,3 +14,10 @@ __rust_helper void rust_helper_init_work_with_key(struct work_struct *work,
INIT_LIST_HEAD(&work->entry);
work->func = func;
}
+
+__rust_helper
+struct workqueue_struct *rust_helper_alloc_workqueue(const char *fmt, unsigned int flags,
+ int max_active, const void *data)
+{
+ return alloc_workqueue(fmt, flags, max_active, data);
+}
diff --git a/rust/kernel/workqueue/builder.rs b/rust/kernel/workqueue/builder.rs
new file mode 100644
index 000000000000..673b121483ec
--- /dev/null
+++ b/rust/kernel/workqueue/builder.rs
@@ -0,0 +1,389 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Workqueue builders.
+
+use kernel::{
+ alloc::AllocError,
+ prelude::*,
+ workqueue::{
+ OwnedQueue, //
+ Queue,
+ }, //
+};
+
+use core::{
+ marker::PhantomData,
+ ptr::{
+ self,
+ NonNull, //
+ },
+};
+
+/// Workqueue builder.
+///
+/// A valid combination of workqueue flags contains one of the base flags (`WQ_UNBOUND`, `WQ_BH`,
+/// or `WQ_PERCPU`) and a combination of modifier flags that are compatible with the selected base
+/// flag.
+///
+/// For details, please refer to `Documentation/core-api/workqueue.rst`.
+pub struct Builder<Kind> {
+ flags: bindings::wq_flags,
+ max_active: i32,
+ _kind: PhantomData<Kind>,
+}
+
+pub enum TypeUnbound {}
+pub enum TypePercpu {}
+pub enum TypePowerEfficient {}
+pub enum TypeBH {}
+pub enum TypeOrdered {}
+
+/// Entry-points to the builder API.
+impl Queue {
+ /// Build a workqueue whose work may execute on any cpu.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_unbound().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from unbound wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_UNBOUND")]
+ pub fn new_unbound() -> Builder<TypeUnbound> {
+ Builder {
+ flags: bindings::wq_flags_WQ_UNBOUND,
+ max_active: 0,
+ _kind: PhantomData,
+ }
+ }
+
+ /// Build a workqueue whose work items are bound to the CPU they are queued on.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_percpu().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from percpu wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_PERCPU")]
+ pub fn new_percpu() -> Builder<TypePercpu> {
+ Builder {
+ flags: bindings::wq_flags_WQ_PERCPU,
+ max_active: 0,
+ _kind: PhantomData,
+ }
+ }
+
+ /// Build a power-efficient workqueue.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_power_efficient().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from power-efficient wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_POWER_EFFICIENT")]
+ pub fn new_power_efficient() -> Builder<TypePowerEfficient> {
+ Builder {
+ flags: bindings::wq_flags_WQ_UNBOUND | bindings::wq_flags_WQ_POWER_EFFICIENT,
+ max_active: 0,
+ _kind: PhantomData,
+ }
+ }
+
+ /// Build a single-threaded workqueue that executes jobs in order.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_ordered().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from ordered wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "alloc_ordered_workqueue")]
+ #[doc(alias = "__WQ_ORDERED")]
+ pub fn new_ordered() -> Builder<TypeOrdered> {
+ Builder {
+ flags: bindings::wq_flags_WQ_UNBOUND | bindings::wq_flags___WQ_ORDERED,
+ max_active: 1,
+ _kind: PhantomData,
+ }
+ }
+
+ /// Build a workqueue that executes in bottom-half (softirq) context.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_bh().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from BH wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_BH")]
+ pub fn new_bh() -> Builder<TypeBH> {
+ Builder {
+ flags: bindings::wq_flags_WQ_PERCPU | bindings::wq_flags_WQ_BH,
+ max_active: 0,
+ _kind: PhantomData,
+ }
+ }
+}
+
+/// Options that may be used with all workqueue types.
+impl<Kind> Builder<Kind> {
+ /// Mark this workqueue high priority.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_unbound().highpri().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from highpri wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_HIGHPRI")]
+ pub fn highpri(mut self) -> Self {
+ self.flags |= bindings::wq_flags_WQ_HIGHPRI;
+ self
+ }
+
+ /// Creates the workqueue.
+ ///
+ /// The provided name is used verbatim as the workqueue name.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// // create an unbound workqueue registered with sysfs
+ /// let wq = Queue::new_unbound().sysfs().build(c"my-wq")?;
+ ///
+ /// // spawn a work item on it
+ /// wq.try_spawn(
+ /// GFP_KERNEL,
+ /// || pr_warn!("Printing from my-wq"),
+ /// )?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "alloc_workqueue")]
+ pub fn build(self, name: &CStr) -> Result<OwnedQueue, AllocError> {
+ // SAFETY:
+ // * c"%s" is compatible with passing the name as a c-string.
+ // * the builder only permits valid flag combinations
+ let ptr = unsafe {
+ bindings::alloc_workqueue(
+ c"%s".as_char_ptr(),
+ self.flags,
+ self.max_active,
+ name.as_char_ptr().cast::<c_void>(),
+ )
+ };
+
+ // INVARIANT: We successfully created the workqueue, so we can return ownership to the
+ // caller.
+ Ok(OwnedQueue {
+ queue: NonNull::new(ptr).ok_or(AllocError)?.cast(),
+ })
+ }
+
+ /// Creates the workqueue.
+ ///
+ /// # Examples
+ ///
+ /// This example shows how to pass a Rust string formatter to the workqueue name, creating
+ /// workqueues with names such as `my-wq-1` and `my-wq-2`.
+ ///
+ /// ```
+ /// use kernel::workqueue::{Queue, OwnedQueue};
+ ///
+ /// fn my_wq(num: u32) -> Result<OwnedQueue> {
+ /// // create a percpu workqueue called my-wq-{num}
+ /// let wq = Queue::new_percpu().build_fmt(fmt!("my-wq-{num}"))?;
+ /// Ok(wq)
+ /// }
+ /// ```
+ #[inline]
+ pub fn build_fmt(self, name: kernel::fmt::Arguments<'_>) -> Result<OwnedQueue, AllocError> {
+ // SAFETY:
+ // * c"%pA" is compatible with passing an `Arguments` pointer.
+ // * the builder only permits valid flag combinations
+ let ptr = unsafe {
+ bindings::alloc_workqueue(
+ c"%pA".as_char_ptr(),
+ self.flags,
+ self.max_active,
+ ptr::from_ref(&name).cast::<c_void>(),
+ )
+ };
+
+ // INVARIANT: We successfully created the workqueue, so we can return ownership to the
+ // caller.
+ Ok(OwnedQueue {
+ queue: NonNull::new(ptr).ok_or(AllocError)?.cast(),
+ })
+ }
+}
+
+/// Indicates that this workqueue is threaded.
+pub trait TypeThreaded {}
+impl TypeThreaded for TypeUnbound {}
+impl TypeThreaded for TypePercpu {}
+impl TypeThreaded for TypePowerEfficient {}
+
+/// Options that are not available on BH or ordered workqueues.
+impl<Kind: TypeThreaded> Builder<Kind> {
+ /// Set the maximum number of concurrently executing work items.
+ ///
+ /// For percpu workqueues this is per-CPU. If not set, a default value of
+ /// `WQ_DFL_ACTIVE` is used. The maximum value is `WQ_MAX_ACTIVE`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_unbound().max_active(16).build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from wq with max_active=16"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ pub fn max_active(mut self, max_active: u32) -> Self {
+ // If provided `max_active` is greater than `i32::MAX`, then we need to trigger the C-side
+ // comparison with `WQ_MAX_ACTIVE`, which we can do by clamping to `i32::MAX`.
+ self.max_active = i32::try_from(max_active).unwrap_or(i32::MAX);
+ self
+ }
+
+ /// Mark this workqueue as cpu intensive.
+ ///
+ /// Work items will not contribute to the concurrency level, preventing
+ /// them from stalling other work items in the same per-CPU worker pool.
+ /// This is meaningless for unbound workqueues.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_unbound().cpu_intensive().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from cpu-intensive wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_CPU_INTENSIVE")]
+ pub fn cpu_intensive(mut self) -> Self {
+ self.flags |= bindings::wq_flags_WQ_CPU_INTENSIVE;
+ self
+ }
+}
+
+/// Indicates that this workqueue runs in a normal context (as opposed to softirq context).
+pub trait TypeNormal {}
+impl TypeNormal for TypeUnbound {}
+impl TypeNormal for TypePercpu {}
+impl TypeNormal for TypePowerEfficient {}
+impl TypeNormal for TypeOrdered {}
+
+/// Options that are not available on BH workqueues.
+impl<Kind: TypeNormal> Builder<Kind> {
+ /// Make this workqueue visible in sysfs.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_unbound().sysfs().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from sysfs wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_SYSFS")]
+ pub fn sysfs(mut self) -> Self {
+ self.flags |= bindings::wq_flags_WQ_SYSFS;
+ self
+ }
+
+ /// Allow this workqueue to be frozen during suspend.
+ ///
+ /// Work items on the workqueue are drained and no new work items start
+ /// execution until thawed.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_unbound().freezable().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from freezable wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_FREEZABLE")]
+ pub fn freezable(mut self) -> Self {
+ self.flags |= bindings::wq_flags_WQ_FREEZABLE;
+ self
+ }
+
+ /// This workqueue may be used during memory reclaim.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_unbound().mem_reclaim().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from mem_reclaim wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_MEM_RECLAIM")]
+ pub fn mem_reclaim(mut self) -> Self {
+ self.flags |= bindings::wq_flags_WQ_MEM_RECLAIM;
+ self
+ }
+}
+
+/// Options only available on a power-efficient workqueue.
+impl Builder<TypePowerEfficient> {
+ /// Configure this power-efficient workqueue to be percpu.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::workqueue::Queue;
+ ///
+ /// let wq = Queue::new_power_efficient().percpu().build(c"my-wq")?;
+ /// wq.try_spawn(GFP_KERNEL, || pr_info!("Hello from percpu power-efficient wq"))?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ #[inline]
+ #[doc(alias = "WQ_PERCPU")]
+ pub fn percpu(mut self) -> Self {
+ self.flags &= !bindings::wq_flags_WQ_UNBOUND;
+ self.flags |= bindings::wq_flags_WQ_PERCPU;
+ self
+ }
+}
diff --git a/rust/kernel/workqueue/mod.rs b/rust/kernel/workqueue/mod.rs
index 399d2ed24135..8eb2d037be83 100644
--- a/rust/kernel/workqueue/mod.rs
+++ b/rust/kernel/workqueue/mod.rs
@@ -186,7 +186,10 @@
//! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h)
use crate::{
- alloc::{AllocError, Flags},
+ alloc::{
+ self,
+ AllocError, //
+ },
container_of,
prelude::*,
sync::{
@@ -200,7 +203,14 @@
time::Jiffies,
types::Opaque,
};
-use core::{marker::PhantomData, ptr::NonNull};
+use core::{
+ marker::PhantomData,
+ ops::Deref,
+ ptr::NonNull, //
+};
+
+mod builder;
+pub use self::builder::Builder;
/// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
#[macro_export]
@@ -346,7 +356,7 @@ pub fn enqueue_delayed<W, const ID: u64>(
/// This method can fail because it allocates memory to store the work item.
pub fn try_spawn<T: 'static + Send + FnOnce()>(
&self,
- flags: Flags,
+ flags: alloc::Flags,
func: T,
) -> Result<(), AllocError> {
let init = pin_init!(ClosureWork {
@@ -359,6 +369,41 @@ pub fn try_spawn<T: 'static + Send + FnOnce()>(
}
}
+/// An owned kernel work queue.
+///
+/// Dropping a workqueue blocks on all pending work.
+///
+/// # Invariants
+///
+/// `queue` points at a valid workqueue that is owned by this `OwnedQueue`.
+pub struct OwnedQueue {
+ queue: NonNull<Queue>,
+}
+
+// SAFETY: `OwnedQueue` uniquely owns a valid `Queue`, which is `Send + Sync`.
+unsafe impl Send for OwnedQueue {}
+// SAFETY: `&OwnedQueue` only provides `&Queue` (via `Deref`), which is safe to share.
+unsafe impl Sync for OwnedQueue {}
+
+impl Deref for OwnedQueue {
+ type Target = Queue;
+ #[inline]
+ fn deref(&self) -> &Queue {
+ // SAFETY: By the type invariants, this pointer references a valid queue.
+ unsafe { &*self.queue.as_ptr() }
+ }
+}
+
+impl Drop for OwnedQueue {
+ #[inline]
+ fn drop(&mut self) {
+ // SAFETY: This `OwnedQueue` owns a valid workqueue, so we can destroy it. There is no
+ // delayed work scheduled on this queue that may attempt to use it after this call, as
+ // scheduling delayed work requires a 'static reference.
+ unsafe { bindings::destroy_workqueue(self.queue.as_ptr().cast()) }
+ }
+}
+
/// A helper type used in [`try_spawn`].
///
/// [`try_spawn`]: Queue::try_spawn
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 5/6] rust: workqueue: add ScopedQueue for lifetime bound items
2026-08-07 16:52 [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork Danilo Krummrich
` (3 preceding siblings ...)
2026-08-07 16:52 ` [PATCH v2 4/6] rust: workqueue: add creation of workqueues Danilo Krummrich
@ 2026-08-07 16:52 ` Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items Danilo Krummrich
5 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 16:52 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core, Danilo Krummrich
From: Onur Özkan <work@onurozkan.dev>
Add a workqueue wrapper for work items that are not 'static.
Tyr reset work is queued from a handle that owns a Controller<'bound>
where the work item holds references tied to the lifetime of the bound
device and its mapped IO state. The existing API only accepts 'static
work items which cannot express that relationship.
Introduce ScopedQueue for this case. It owns the underlying workqueue
and ties enqueued work to the queue lifetime so borrowed state cannot
outlive the queue that may still run it.
Construction is unsafe because the queue must not be leaked.
`compile_fail` doc-tests are ignored for now as KUnit doesn't support
that. Enabling those tests as regular code block would raise this error:
ERROR:root:error[E0597]: `data` does not live long enough
--> rust/doctests_kernel_generated.rs:22029:44
|
22027 | let data = ();
| ---- binding `data` declared here
22028 | // SAFETY: Queue is not leaked.
22029 | queue = unsafe { new_queue(&data)? };
| ^^^^^ borrowed value does not live long enough
22030 | }
| - `data` dropped here while still borrowed
...
22034 | }
| - borrow might be used here, when `queue` is dropped and runs the `Drop` code for type `ScopedQueue`
|
= note: values in a scope are dropped in the opposite order they are defined
which is exactly the constraint ScopedQueue is meant to enforce.
Suggested-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: Onur Özkan <work@onurozkan.dev>
[ Move from scoped_queue.rs to scoped.rs, which can be shared with
ScopedWork; add missing inline annotations. - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/workqueue/mod.rs | 3 +
rust/kernel/workqueue/scoped.rs | 190 ++++++++++++++++++++++++++++++++
2 files changed, 193 insertions(+)
create mode 100644 rust/kernel/workqueue/scoped.rs
diff --git a/rust/kernel/workqueue/mod.rs b/rust/kernel/workqueue/mod.rs
index 8eb2d037be83..551fa1401b85 100644
--- a/rust/kernel/workqueue/mod.rs
+++ b/rust/kernel/workqueue/mod.rs
@@ -212,6 +212,9 @@
mod builder;
pub use self::builder::Builder;
+mod scoped;
+pub use self::scoped::ScopedQueue;
+
/// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
#[macro_export]
macro_rules! new_work {
diff --git a/rust/kernel/workqueue/scoped.rs b/rust/kernel/workqueue/scoped.rs
new file mode 100644
index 000000000000..18a4b6f6cf18
--- /dev/null
+++ b/rust/kernel/workqueue/scoped.rs
@@ -0,0 +1,190 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Lifetime-scoped workqueues.
+//!
+//! Provides [`ScopedQueue`] for work items that may borrow data with some
+//! non-`'static` lifetime.
+//!
+//! Unlike [`Queue`] which only accepts `'static` work items, [`ScopedQueue`]
+//! owns its underlying queue and relies on that queue being dropped to drain
+//! pending and running work before borrowed data can go out of scope.
+//!
+//! TODO: Remove `ignore` once KUnit supports `compile_fail` on doc-tests.
+//! ```compile_fail,ignore
+//! use kernel::prelude::*;
+//! use kernel::workqueue::ScopedQueue;
+//!
+//! /// # Safety
+//! ///
+//! /// Returned queue must not be leaked.
+//! unsafe fn new_queue<'bound>(_: &'bound ()) -> Result<ScopedQueue<'bound>> {
+//! // SAFETY: Caller guarantees that the returned queue is not leaked.
+//! unsafe { ScopedQueue::new(c"scoped_queue") }
+//! }
+//!
+//! fn queue_outlives_borrowed_data() -> Result {
+//! let queue;
+//!
+//! {
+//! let data = ();
+//! // SAFETY: Queue is not leaked.
+//! queue = unsafe { new_queue(&data)? };
+//! }
+//! // Here the `compile_fail` is fulfilled as `queue` would be dropped
+//! // after `data`.
+//! Ok(())
+//! }
+//! ```
+//!
+//! TODO: Remove `ignore` once KUnit supports `compile_fail` on doc-tests.
+//! ```compile_fail,ignore
+//! use kernel::prelude::*;
+//! use kernel::sync::Arc;
+//! use kernel::workqueue::{
+//! impl_has_work,
+//! new_work,
+//! ScopedQueue,
+//! Work,
+//! WorkItem,
+//! };
+//!
+//! #[pin_data]
+//! struct BorrowedWork<'bound> {
+//! data: &'bound (),
+//! #[pin]
+//! work: Work<BorrowedWork<'bound>>,
+//! }
+//!
+//! impl_has_work! {
+//! impl{'bound} HasWork<BorrowedWork<'bound>> for BorrowedWork<'bound> { self.work }
+//! }
+//!
+//! impl<'bound> WorkItem for BorrowedWork<'bound> {
+//! type Pointer = Arc<Self>;
+//!
+//! fn run(_this: Arc<Self>) {}
+//! }
+//!
+//! impl<'bound> BorrowedWork<'bound> {
+//! fn new(data: &'bound ()) -> Result<Arc<Self>> {
+//! Arc::pin_init(
+//! pin_init!(Self {
+//! data,
+//! work <- new_work!("BorrowedWork::work"),
+//! }),
+//! GFP_KERNEL,
+//! )
+//! }
+//! }
+//!
+//! struct Handle<'bound> {
+//! work: Arc<BorrowedWork<'bound>>,
+//! wq: ScopedQueue<'bound>,
+//! }
+//!
+//! impl<'bound> Handle<'bound> {
+//! /// # Safety
+//! ///
+//! /// Returned handle must not be leaked.
+//! unsafe fn new(data: &'bound ()) -> Result<Self> {
+//! Ok(Self {
+//! work: BorrowedWork::new(data)?,
+//! // SAFETY: Caller guarantees that the returned handle is not leaked.
+//! wq: unsafe { ScopedQueue::new(c"handle_wq")? },
+//! })
+//! }
+//! }
+//!
+//! fn handle_outlives_borrowed_data() -> Result {
+//! let handle;
+//!
+//! {
+//! let data = ();
+//! // SAFETY: Handle is not leaked.
+//! handle = unsafe { Handle::new(&data)? };
+//!
+//! let _ = handle.wq.enqueue(handle.work.clone());
+//! }
+//! // Here the `compile_fail` is fulfilled as `handle` would be dropped
+//! // after `data`.
+//! Ok(())
+//! }
+//! ```
+
+use super::{
+ OwnedQueue,
+ Queue,
+ RawWorkItem, //
+};
+
+use crate::{
+ bindings,
+ ffi,
+ prelude::*, //
+};
+
+use core::marker::PhantomData;
+
+/// An owned workqueue that can enqueue work items borrowing from `'scope`.
+///
+/// A `ScopedQueue` must not outlive data borrowed by its work items.
+pub struct ScopedQueue<'scope> {
+ inner: OwnedQueue,
+ _scope: PhantomData<&'scope mut &'scope ()>,
+}
+
+impl<'scope> ScopedQueue<'scope> {
+ /// Creates an ordered scoped workqueue.
+ ///
+ /// # Safety
+ ///
+ /// The caller must not leak the returned queue or otherwise prevent its
+ /// [`Drop`] implementation from running since dropping the queue drains
+ /// pending and running work that may borrow from `'scope`.
+ #[inline]
+ pub unsafe fn new(name: &'static CStr) -> Result<Self> {
+ Ok(Self {
+ inner: Queue::new_ordered().build(name)?,
+ _scope: PhantomData,
+ })
+ }
+
+ /// Enqueues a work item on this scoped queue.
+ #[inline]
+ pub fn enqueue<W, const ID: u64>(&self, work: W) -> W::EnqueueOutput
+ where
+ W: RawWorkItem<ID> + Send + 'scope,
+ {
+ let queue_ptr = self.inner.0.get();
+
+ // SAFETY:
+ // - Closure returns `false` only if `queue_work_on` returns `false`
+ // and that means `work_ptr` is already in a workqueue.
+ //
+ // - `W: 'scope` and dropck keep borrowed data alive until this queue is
+ // dropped. The constructor requires that the queue is not leaked and
+ // dropping `inner` drains pending and running work so the function
+ // pointer is not called after any lifetime in `W` expires.
+ //
+ // - The last requirement of `__enqueue` is not relevant here because `W`
+ // is `Send`.
+ unsafe {
+ work.__enqueue(move |work_ptr| {
+ bindings::queue_work_on(
+ bindings::wq_misc_consts_WORK_CPU_UNBOUND as ffi::c_int,
+ queue_ptr,
+ work_ptr,
+ )
+ })
+ }
+ }
+}
+
+impl Drop for ScopedQueue<'_> {
+ #[inline]
+ fn drop(&mut self) {
+ // This impl makes dropck require `'scope` to outlive `OwnedQueue`.
+ // See: https://doc.rust-lang.org/nomicon/phantom-data.html#generic-parameters-and-drop-checking
+ let _ = &self._scope;
+ }
+}
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items
2026-08-07 16:52 [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork Danilo Krummrich
` (4 preceding siblings ...)
2026-08-07 16:52 ` [PATCH v2 5/6] rust: workqueue: add ScopedQueue for lifetime bound items Danilo Krummrich
@ 2026-08-07 16:52 ` Danilo Krummrich
2026-08-07 18:35 ` Danilo Krummrich
5 siblings, 1 reply; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 16:52 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core, Danilo Krummrich
Add ScopedWork<T>, a work item wrapper whose destructor calls
cancel_work_sync(), allowing T to carry non-'static lifetimes. Ownership
of the data is not transferred to the workqueue; instead, the
synchronous cancellation on drop guarantees the work function is not
running when the data is freed.
ScopedWork uses the existing Work/HasWork/WorkItem infrastructure with
NonNull<ScopedWorkRef<T>> as WorkItem::Pointer for the callback path,
and implements RawWorkItem for &ScopedWork<T> and &ScopedWorkRef<T> for
the enqueue path (requiring T: Sync for cross-thread shared access
safety).
Two enqueue paths are provided:
- Queue::enqueue_scoped() (unsafe): the caller must ensure the work
item is not forgotten.
- ScopedQueue::enqueue() (safe): when the work item's lifetime
satisfies the queue's 'scope bound.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/workqueue/mod.rs | 46 +++-
rust/kernel/workqueue/scoped.rs | 442 +++++++++++++++++++++++++++++---
2 files changed, 444 insertions(+), 44 deletions(-)
diff --git a/rust/kernel/workqueue/mod.rs b/rust/kernel/workqueue/mod.rs
index 551fa1401b85..be5bfb4cfc30 100644
--- a/rust/kernel/workqueue/mod.rs
+++ b/rust/kernel/workqueue/mod.rs
@@ -213,7 +213,13 @@
pub use self::builder::Builder;
mod scoped;
-pub use self::scoped::ScopedQueue;
+pub use self::scoped::{
+ new_scoped_work,
+ ScopedQueue,
+ ScopedWork,
+ ScopedWorkItem,
+ ScopedWorkRef, //
+};
/// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
#[macro_export]
@@ -283,23 +289,39 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue
/// This may fail if the work item is already enqueued in a workqueue.
///
/// The work item will be submitted using `WORK_CPU_UNBOUND`.
+ #[inline]
pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
where
W: RawWorkItem<ID> + Send + 'static,
+ {
+ // SAFETY: `W: 'static` guarantees the work item remains valid indefinitely,
+ // so the `enqueue_scoped` requirement that the work item stays valid until
+ // the work function runs (or is cancelled) is trivially satisfied.
+ unsafe { self.enqueue_scoped(w) }
+ }
+
+ /// Enqueues a work item that may not be `'static`.
+ ///
+ /// Unlike [`Queue::enqueue`], this does not require the work item to be `'static`.
+ ///
+ /// The work item will be submitted using `WORK_CPU_UNBOUND`.
+ ///
+ /// # Safety
+ ///
+ /// The caller must ensure that the work item's destructor runs before any
+ /// lifetime it captures expires (i.e., the work item must not be forgotten).
+ #[inline]
+ pub unsafe fn enqueue_scoped<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
+ where
+ W: RawWorkItem<ID> + Send,
{
let queue_ptr = self.0.get();
- // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
- // `__enqueue` requirements are not relevant since `W` is `Send` and static.
- //
- // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
- // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
- // closure.
- //
- // Furthermore, if the C workqueue code accesses the pointer after this call to
- // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
- // will have returned true. In this case, `__enqueue` promises that the raw pointer will
- // stay valid until we call the function pointer in the `work_struct`, so the access is ok.
+ // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The
+ // caller guarantees the work item remains valid until the work function runs or the item
+ // is cancelled, satisfying the `__enqueue` requirement that the pointer stays valid until
+ // the function pointer in the `work_struct` is called. `W: Send` satisfies the
+ // cross-thread safety requirement.
unsafe {
w.__enqueue(move |work_ptr| {
bindings::queue_work_on(
diff --git a/rust/kernel/workqueue/scoped.rs b/rust/kernel/workqueue/scoped.rs
index 18a4b6f6cf18..1adf96f7eccd 100644
--- a/rust/kernel/workqueue/scoped.rs
+++ b/rust/kernel/workqueue/scoped.rs
@@ -1,13 +1,95 @@
// SPDX-License-Identifier: GPL-2.0
-//! Lifetime-scoped workqueues.
+//! Lifetime-scoped workqueues and work items.
//!
-//! Provides [`ScopedQueue`] for work items that may borrow data with some
-//! non-`'static` lifetime.
+//! Provides [`ScopedQueue`] and [`ScopedWork`] for work items that may borrow
+//! data with some non-`'static` lifetime.
//!
-//! Unlike [`Queue`] which only accepts `'static` work items, [`ScopedQueue`]
-//! owns its underlying queue and relies on that queue being dropped to drain
-//! pending and running work before borrowed data can go out of scope.
+//! [`ScopedQueue`] owns its underlying queue and relies on that queue being
+//! dropped to drain pending and running work before borrowed data can go out
+//! of scope.
+//!
+//! [`ScopedWork`] wraps a work item whose destructor calls `cancel_work_sync()`,
+//! so ownership of the data is not transferred to the workqueue. This allows the
+//! inner data to carry non-`'static` lifetimes.
+//!
+//! Drivers should prefer [`ScopedWork`] with either a [`ScopedQueue`] or a
+//! system queue over [`Work`]-based items. When used with a [`ScopedQueue`],
+//! the work item must already outlive the queue, making [`Work`]'s separate
+//! allocation and reference count unnecessary.
+//!
+//! # Examples
+//!
+//! Enqueue on the system workqueue (unsafe, caller must not forget the work):
+//!
+//! ```
+//! # use kernel::time::{Delta, delay::fsleep};
+//! use kernel::workqueue::{
+//! self,
+//! new_scoped_work,
+//! ScopedWork,
+//! ScopedWorkItem,
+//! ScopedWorkRef,
+//! };
+//!
+//! struct MyWork {
+//! value: u32,
+//! }
+//!
+//! impl ScopedWorkItem for MyWork {
+//! fn run(work: &ScopedWorkRef<Self>) {
+//! pr_info!("value = {}\n", work.value);
+//! }
+//! }
+//!
+//! let work = KBox::pin_init(
+//! new_scoped_work!("MyWork", MyWork { value: 42 }),
+//! GFP_KERNEL,
+//! )?;
+//!
+//! // SAFETY: `work` is not forgotten.
+//! unsafe { workqueue::system_dfl().enqueue_scoped(&*work) };
+//! # fsleep(Delta::from_millis(100));
+//! # Ok::<(), Error>(())
+//! ```
+//!
+//! Enqueue on a [`ScopedQueue`] using the safe path (work outlives the queue):
+//!
+//! ```
+//! use kernel::workqueue::{
+//! new_scoped_work,
+//! ScopedQueue,
+//! ScopedWork,
+//! ScopedWorkItem,
+//! ScopedWorkRef,
+//! };
+//!
+//! struct MyWork {
+//! value: u32,
+//! }
+//!
+//! impl ScopedWorkItem for MyWork {
+//! fn run(work: &ScopedWorkRef<Self>) {
+//! pr_info!("value = {}\n", work.value);
+//! }
+//! }
+//!
+//! let work = KBox::pin_init(
+//! new_scoped_work!("MyWork", MyWork { value: 42 }),
+//! GFP_KERNEL,
+//! )?;
+//!
+//! // SAFETY: The queue is not forgotten.
+//! let queue = unsafe { ScopedQueue::new(c"example_wq")? };
+//!
+//! // Safe since `work` outlives `queue`.
+//! queue.enqueue(&*work);
+//! # Ok::<(), Error>(())
+//! ```
+//!
+//! [`ScopedQueue`] can also be used with regular [`Work`]-based items. The
+//! following `compile_fail` examples demonstrate the lifetime enforcement that
+//! [`ScopedQueue`] provides in that case.
//!
//! TODO: Remove `ignore` once KUnit supports `compile_fail` on doc-tests.
//! ```compile_fail,ignore
@@ -112,18 +194,30 @@
//! ```
use super::{
+ impl_has_work,
+ HasWork,
OwnedQueue,
Queue,
- RawWorkItem, //
+ RawWorkItem,
+ Work,
+ WorkItem,
+ WorkItemPointer, //
};
use crate::{
bindings,
- ffi,
- prelude::*, //
+ prelude::*,
+ sync::LockClassKey,
+ types::Opaque, //
};
-use core::marker::PhantomData;
+use pin_init::Wrapper;
+
+use core::{
+ marker::PhantomData,
+ ops::Deref,
+ ptr::NonNull, //
+};
/// An owned workqueue that can enqueue work items borrowing from `'scope`.
///
@@ -133,6 +227,15 @@ pub struct ScopedQueue<'scope> {
_scope: PhantomData<&'scope mut &'scope ()>,
}
+impl Deref for ScopedQueue<'_> {
+ type Target = Queue;
+
+ #[inline]
+ fn deref(&self) -> &Queue {
+ &self.inner
+ }
+}
+
impl<'scope> ScopedQueue<'scope> {
/// Creates an ordered scoped workqueue.
///
@@ -155,28 +258,11 @@ pub fn enqueue<W, const ID: u64>(&self, work: W) -> W::EnqueueOutput
where
W: RawWorkItem<ID> + Send + 'scope,
{
- let queue_ptr = self.inner.0.get();
-
- // SAFETY:
- // - Closure returns `false` only if `queue_work_on` returns `false`
- // and that means `work_ptr` is already in a workqueue.
- //
- // - `W: 'scope` and dropck keep borrowed data alive until this queue is
- // dropped. The constructor requires that the queue is not leaked and
- // dropping `inner` drains pending and running work so the function
- // pointer is not called after any lifetime in `W` expires.
- //
- // - The last requirement of `__enqueue` is not relevant here because `W`
- // is `Send`.
- unsafe {
- work.__enqueue(move |work_ptr| {
- bindings::queue_work_on(
- bindings::wq_misc_consts_WORK_CPU_UNBOUND as ffi::c_int,
- queue_ptr,
- work_ptr,
- )
- })
- }
+ // SAFETY: `W: 'scope` and dropck keep borrowed data alive until this queue
+ // is dropped. The constructor requires that the queue is not leaked and
+ // dropping `inner` drains pending and running work, so the function pointer
+ // is not called after any lifetime in `W` expires.
+ unsafe { self.enqueue_scoped(work) }
}
}
@@ -188,3 +274,295 @@ fn drop(&mut self) {
let _ = &self._scope;
}
}
+
+/// Trait for types that can be used as scoped work items.
+///
+/// Implementers define the work function that executes when the item is dequeued by a workqueue
+/// thread. The callback receives a reference to the containing [`ScopedWorkRef`], which provides
+/// access to the inner data via [`Deref`] and can be used to re-enqueue the work item.
+pub trait ScopedWorkItem: Sized {
+ /// Called when the work item is executed.
+ fn run(work: &ScopedWorkRef<Self>);
+}
+
+/// The work function's view of a [`ScopedWork`] item.
+///
+/// The work function callback receives `&ScopedWorkRef<T>`, which [`Deref`]s to `&T` and can be
+/// passed to queue enqueue methods for re-enqueueing from within the work function.
+#[pin_data]
+pub struct ScopedWorkRef<T: ScopedWorkItem> {
+ #[pin]
+ work: Work<Self>,
+ #[pin]
+ data: T,
+}
+
+impl_has_work! {
+ impl{T: ScopedWorkItem} HasWork<ScopedWorkRef<T>> for ScopedWorkRef<T> { self.work }
+}
+
+impl<T: ScopedWorkItem> Deref for ScopedWorkRef<T> {
+ type Target = T;
+
+ #[inline]
+ fn deref(&self) -> &T {
+ &self.data
+ }
+}
+
+impl<T: ScopedWorkItem> WorkItem for ScopedWorkRef<T> {
+ type Pointer = NonNull<Self>;
+
+ #[inline]
+ fn run(this: NonNull<Self>) {
+ // SAFETY: `this` points to a valid, pinned `ScopedWorkRef`. `cancel_work_sync()` in
+ // `ScopedWork`'s `PinnedDrop` prevents use-after-drop.
+ let work = unsafe { &*this.as_ptr() };
+
+ T::run(work);
+ }
+}
+
+// SAFETY: The `run` callback uses the `work_struct` pointer to recover a pointer to
+// `ScopedWorkRef<T>` via `HasWork`, wraps it in `NonNull`, and calls `WorkItem::run`.
+unsafe impl<T: ScopedWorkItem, const ID: u64> WorkItemPointer<ID> for NonNull<ScopedWorkRef<T>>
+where
+ ScopedWorkRef<T>: WorkItem<ID, Pointer = Self>,
+ ScopedWorkRef<T>: HasWork<ScopedWorkRef<T>, ID>,
+{
+ unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
+ let ptr = ptr.cast::<Work<ScopedWorkRef<T>, ID>>();
+
+ // SAFETY: The `work_struct` is embedded in `ScopedWorkRef<T>` via `HasWork`.
+ let ptr =
+ unsafe { <ScopedWorkRef<T> as HasWork<ScopedWorkRef<T>, ID>>::work_container_of(ptr) };
+
+ // SAFETY: `work_container_of` returns a valid, non-null pointer.
+ let nn = unsafe { NonNull::new_unchecked(ptr) };
+
+ <ScopedWorkRef<T> as WorkItem<ID>>::run(nn);
+ }
+}
+
+// Required because `WorkItemPointer<ID>: RawWorkItem<ID>` is a supertrait bound. This `__enqueue`
+// is never called; the enqueue path goes through the `RawWorkItem` impl for `&ScopedWork<T>` or
+// `&ScopedWorkRef<T>` instead.
+//
+// SAFETY: `__enqueue` is unreachable.
+unsafe impl<T: ScopedWorkItem, const ID: u64> RawWorkItem<ID> for NonNull<ScopedWorkRef<T>>
+where
+ ScopedWorkRef<T>: HasWork<ScopedWorkRef<T>, ID>,
+{
+ type EnqueueOutput = bool;
+
+ unsafe fn __enqueue<F>(self, _queue_work_on: F) -> Self::EnqueueOutput
+ where
+ F: FnOnce(*mut bindings::work_struct) -> bool,
+ {
+ unreachable!()
+ }
+}
+
+// SAFETY: `&ScopedWorkRef<T>` points to a valid `ScopedWorkRef` with a valid `work_struct`.
+// The pointer remains valid until `cancel_work_sync()` completes in `ScopedWork`'s drop.
+unsafe impl<'a, T: ScopedWorkItem + Sync, const ID: u64> RawWorkItem<ID> for &'a ScopedWorkRef<T>
+where
+ ScopedWorkRef<T>: HasWork<ScopedWorkRef<T>, ID>,
+{
+ type EnqueueOutput = bool;
+
+ unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
+ where
+ F: FnOnce(*mut bindings::work_struct) -> bool,
+ {
+ let self_ptr = core::ptr::from_ref(self);
+
+ // SAFETY: `self_ptr` points to a valid `ScopedWorkRef` with a `Work` field.
+ let work_ptr = unsafe {
+ <ScopedWorkRef<T> as HasWork<ScopedWorkRef<T>, ID>>::raw_get_work(self_ptr.cast_mut())
+ };
+
+ // SAFETY: `work_ptr` points to a valid `Work`.
+ let work_ptr = unsafe { Work::raw_get(work_ptr) };
+
+ queue_work_on(work_ptr)
+ }
+}
+
+// SAFETY: `&ScopedWork<T>` accesses the inner `ScopedWorkRef` through `Opaque::get()`.
+// The pointer remains valid until `cancel_work_sync()` completes in `ScopedWork`'s drop.
+unsafe impl<'a, T: ScopedWorkItem + Sync, const ID: u64> RawWorkItem<ID> for &'a ScopedWork<T>
+where
+ ScopedWorkRef<T>: HasWork<ScopedWorkRef<T>, ID>,
+{
+ type EnqueueOutput = bool;
+
+ unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
+ where
+ F: FnOnce(*mut bindings::work_struct) -> bool,
+ {
+ // SAFETY: The inner ScopedWorkRef is valid and initialized.
+ let inner: &ScopedWorkRef<T> = unsafe { &*self.inner.get() };
+
+ // SAFETY: Delegates to the `&ScopedWorkRef<T>` impl.
+ unsafe { inner.__enqueue(queue_work_on) }
+ }
+}
+
+/// A scoped work item that cancels synchronously on drop.
+///
+/// `ScopedWork<T>` contains a `work_struct` and the user data `T`. Its destructor calls
+/// `cancel_work_sync()`, guaranteeing the work function is not running when the data is dropped.
+///
+/// This allows `T` to carry non-`'static` lifetimes.
+///
+/// Construct via [`new_scoped_work!`] which returns an `impl PinInit` suitable for embedding
+/// in-place inside other pinned structs.
+///
+/// # Examples
+///
+/// Self-re-enqueueing from within the work function:
+///
+/// ```
+/// # use kernel::sync::atomic::{Atomic, Relaxed};
+/// # use kernel::time::{Delta, delay::fsleep};
+/// use kernel::workqueue::{
+/// new_scoped_work,
+/// Queue,
+/// ScopedQueue,
+/// ScopedWork,
+/// ScopedWorkItem,
+/// ScopedWorkRef,
+/// };
+///
+/// struct RequeueWork<'a> {
+/// counter: Atomic<u32>,
+/// queue: &'a Queue,
+/// }
+///
+/// impl ScopedWorkItem for RequeueWork<'_> {
+/// fn run(work: &ScopedWorkRef<Self>) {
+/// if work.counter.fetch_add(1u32, Relaxed) < 2 {
+/// // SAFETY: The `ScopedWork` is not forgotten.
+/// unsafe { work.queue.enqueue_scoped(work) };
+/// }
+/// }
+/// }
+///
+/// // SAFETY: The queue is not forgotten.
+/// let queue = unsafe { ScopedQueue::new(c"requeue_wq")? };
+///
+/// let work = KBox::pin_init(
+/// new_scoped_work!("RequeueWork", RequeueWork { counter: Atomic::new(0u32), queue: &queue }),
+/// GFP_KERNEL,
+/// )?;
+///
+/// // SAFETY: `work` is not forgotten.
+/// unsafe { queue.enqueue_scoped(&*work) };
+/// # fsleep(Delta::from_millis(300));
+///
+/// assert_eq!(work.counter.load(Relaxed), 3);
+/// # Ok::<(), Error>(())
+/// ```
+#[pin_data(PinnedDrop)]
+pub struct ScopedWork<T: ScopedWorkItem> {
+ #[pin]
+ inner: Opaque<ScopedWorkRef<T>>,
+}
+
+// SAFETY: `&ScopedWork<T>` only provides `&ScopedWorkRef<T>` (via `Deref`), which is safe to share
+// when `T: Sync`.
+unsafe impl<T: ScopedWorkItem + Sync> Sync for ScopedWork<T> {}
+
+// SAFETY: ScopedWork can be sent to another thread when T: Send.
+unsafe impl<T: ScopedWorkItem + Send> Send for ScopedWork<T> {}
+
+impl<T: ScopedWorkItem> Deref for ScopedWork<T> {
+ type Target = ScopedWorkRef<T>;
+
+ #[inline]
+ fn deref(&self) -> &ScopedWorkRef<T> {
+ // SAFETY: The inner `ScopedWorkRef` is always valid and initialized.
+ unsafe { &*self.inner.get() }
+ }
+}
+
+impl<T: ScopedWorkItem> ScopedWork<T> {
+ /// Creates a pin-initializer for a new scoped work item.
+ ///
+ /// Use [`new_scoped_work!`] to automatically provide the lock class key.
+ #[inline]
+ pub fn new<E>(
+ name: &'static CStr,
+ key: Pin<&'static LockClassKey>,
+ init: impl PinInit<T, E>,
+ ) -> impl PinInit<Self, Error>
+ where
+ Error: From<E>,
+ {
+ try_pin_init!(Self {
+ inner <- Opaque::pin_init(try_pin_init!(ScopedWorkRef::<T> {
+ work <- Work::new(name, key),
+ data <- init,
+ })),
+ })
+ }
+}
+
+#[pinned_drop]
+impl<T: ScopedWorkItem> PinnedDrop for ScopedWork<T> {
+ #[inline]
+ fn drop(self: Pin<&mut Self>) {
+ let inner = self.inner.get();
+
+ // SAFETY: `inner` points to a valid `ScopedWorkRef`. After `cancel_work_sync()` returns,
+ // the work function is guaranteed to not be running.
+ unsafe { bindings::cancel_work_sync(Work::raw_get(&raw const (*inner).work)) };
+ }
+}
+
+/// Creates a [`ScopedWork`] pin-initializer with a new lock class.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::workqueue::{
+/// new_scoped_work,
+/// ScopedWork,
+/// ScopedWorkItem,
+/// ScopedWorkRef,
+/// };
+///
+/// struct MyWork {
+/// value: u32,
+/// }
+///
+/// impl ScopedWorkItem for MyWork {
+/// fn run(work: &ScopedWorkRef<Self>) {
+/// pr_info!("value = {}\n", work.value);
+/// }
+/// }
+///
+/// #[pin_data]
+/// struct MyData {
+/// #[pin]
+/// work: ScopedWork<MyWork>,
+/// }
+///
+/// fn init_data() -> impl PinInit<MyData, Error> {
+/// try_pin_init!(MyData {
+/// work <- new_scoped_work!("MyWork", MyWork { value: 7 }),
+/// })
+/// }
+/// ```
+#[macro_export]
+macro_rules! new_scoped_work {
+ ($name:literal, $init:expr) => {
+ $crate::workqueue::ScopedWork::new(
+ $crate::c_str!($name),
+ $crate::static_lock_class!(),
+ $init,
+ )
+ };
+}
+pub use new_scoped_work;
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items
2026-08-07 16:52 ` [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items Danilo Krummrich
@ 2026-08-07 18:35 ` Danilo Krummrich
0 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 18:35 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core
On Fri Aug 7, 2026 at 6:52 PM CEST, Danilo Krummrich wrote:
> +#[pinned_drop]
> +impl<T: ScopedWorkItem> PinnedDrop for ScopedWork<T> {
> + #[inline]
> + fn drop(self: Pin<&mut Self>) {
> + let inner = self.inner.get();
> +
> + // SAFETY: `inner` points to a valid `ScopedWorkRef`. After `cancel_work_sync()` returns,
> + // the work function is guaranteed to not be running.
> + unsafe { bindings::cancel_work_sync(Work::raw_get(&raw const (*inner).work)) };
// SAFETY: `inner` is valid and no longer accessed by the work function.
unsafe { core::ptr::drop_in_place(inner) };
> + }
> +}
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 4/6] rust: workqueue: add creation of workqueues
2026-08-07 16:52 ` [PATCH v2 4/6] rust: workqueue: add creation of workqueues Danilo Krummrich
@ 2026-08-07 22:39 ` Danilo Krummrich
0 siblings, 0 replies; 9+ messages in thread
From: Danilo Krummrich @ 2026-08-07 22:39 UTC (permalink / raw)
To: tj, jiangshanlai, aliceryhl, ojeda, boqun, gary, bjorn3_gh,
lossin, a.hindborg, tmgross, daniel.almeida, tamird, acourbot,
work, jhubbard
Cc: rust-for-linux, linux-kernel, driver-core
On Fri Aug 7, 2026 at 6:52 PM CEST, Danilo Krummrich wrote:
> * new_power_efficient(): WQ_UNBOUND | WQ_POWER_EFFICIENT, add
> .percpu(),
[...]
> + pub fn new_power_efficient() -> Builder<TypePowerEfficient> {
> + Builder {
> + flags: bindings::wq_flags_WQ_UNBOUND | bindings::wq_flags_WQ_POWER_EFFICIENT,
I actually meant to add WQ_PERCPU, which should be the default for
WQ_POWER_EFFICIENT, in order to avoid the warning below.
But I somehow managed to fix it the wrong way around and made WQ_UNBOUND the
default.
So, this should just be bindings::wq_flags_WQ_PERCPU |
bindings::wq_flags_WQ_POWER_EFFICIENT.
- Danilo
[ 1.381581] workqueue: my-wq is using neither WQ_PERCPU or WQ_UNBOUND. Setting WQ_PERCPU.
[ 1.382274] WARNING: kernel/workqueue.c:5852 at alloc_workqueue_va+0x794/0x800, CPU#0: kunit_try_catch/851
[ 1.383088] Modules linked in:
[ 1.383359] CPU: 0 UID: 0 PID: 851 Comm: kunit_try_catch Tainted: G W N 7.2.0-rc1+ #67 PREEMPT(full)
[ 1.384236] Tainted: [W]=WARN, [N]=TEST
[ 1.384567] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.17.0-2-2 04/01/2014
[ 1.385364] RIP: 0010:alloc_workqueue_va+0x79b/0x800
[ 1.385784] Code: 49 8d 96 b8 00 00 00 b9 01 00 00 00 41 b8 00 08 00 00 e8 08 83 ef ff e9 88 f9 ff ff 48 8d 3d cc 1c e2 01 49 8d b6 b8 00 00 00 <67> 48 0f b9 3a 41 81 cc 00 01 00 00 e9 40 f9 ff ff 48 8d 3d bd 1c
[ 1.387337] RSP: 0018:ffffc90000473d58 EFLAGS: 00010246
[ 1.387779] RAX: 0000000000000000 RBX: ffff888102041600 RCX: 8313f7e158b49600
[ 1.388382] RDX: ffff8881020416d8 RSI: ffff8881020416b8 RDI: ffffffff8312e020
[ 1.388985] RBP: 0000000000000000 R08: 00000000ffffffff R09: 00000000ffffffff
[ 1.389587] R10: ffff8881020416bd R11: 0000000000000000 R12: 0000000000000080
[ 1.390192] R13: ffffffff82ad53fd R14: ffff888102041600 R15: ffffc90000473de0
[ 1.390793] FS: 0000000000000000(0000) GS:ffff8881b81cf000(0000) knlGS:0000000000000000
[ 1.391478] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1.391968] CR2: ffff88813ffff000 CR3: 0000000002e22000 CR4: 0000000000750ef0
[ 1.392568] PKRU: 55555554
[ 1.392804] Call Trace:
[ 1.393027] <TASK>
[ 1.393219] alloc_workqueue_noprof+0x5b/0x80
[ 1.393594] rust_doctest_kernel_workqueue_builder_rs_12+0x3a/0x1f0
[ 1.394129] ? _task_rq_lock+0x55/0x150
[ 1.394462] ? call_rcu+0xec/0x270
[ 1.394756] ? kvm_clock_get_cycles+0x18/0x40
[ 1.395136] ? ktime_get_ts64+0x6d/0x170
[ 1.395477] kunit_try_run_case+0x93/0x190
[ 1.395826] kunit_generic_run_threadfn_adapter+0x22/0x40
[ 1.396290] ? kunit_try_catch_run+0x210/0x210
[ 1.396670] kthread+0xfb/0x120
[ 1.396942] ? kthread_blkcg+0x40/0x40
[ 1.397270] ret_from_fork+0xee/0x260
[ 1.397590] ? kthread_blkcg+0x40/0x40
[ 1.397910] ret_from_fork_asm+0x11/0x20
[ 1.398255] </TASK>
[ 1.398453] ---[ end trace 0000000000000000 ]---
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-08-07 22:40 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-07 16:52 [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 1/6] rust: workqueue: replace deprecated system_wq with system_{percpu,dfl}_wq Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 2/6] rust: workqueue: restrict delayed work to global wqs Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 3/6] rust: workqueue: create workqueue subdirectory Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 4/6] rust: workqueue: add creation of workqueues Danilo Krummrich
2026-08-07 22:39 ` Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 5/6] rust: workqueue: add ScopedQueue for lifetime bound items Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items Danilo Krummrich
2026-08-07 18:35 ` Danilo Krummrich
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).