[PATCH 6/7] rust: workqueue: add Work::cancel_work_sync()
From: Danilo Krummrich
Date: Tue Aug 04 2026 - 15:58:40 EST
Add a method to cancel a work item and wait for it to finish if it is
currently running.
This will also be used by ScopedWork's destructor to synchronously
cancel work before dropping borrowed data.
Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
rust/kernel/workqueue/mod.rs | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/rust/kernel/workqueue/mod.rs b/rust/kernel/workqueue/mod.rs
index 5de88c59b2e5..2b87f935712a 100644
--- a/rust/kernel/workqueue/mod.rs
+++ b/rust/kernel/workqueue/mod.rs
@@ -585,6 +585,14 @@ pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
// the compiler does not complain that the `work` field is unused.
unsafe { Opaque::cast_into(core::ptr::addr_of!((*ptr).work)) }
}
+
+ /// Cancels the work item and waits for it to finish if it is running.
+ ///
+ /// Returns `true` if the work was pending, `false` otherwise.
+ pub fn cancel_work_sync(&self) -> bool {
+ // SAFETY: We have a reference to a valid, initialized Work, so the pointer is valid.
+ unsafe { bindings::cancel_work_sync(Self::raw_get(self)) }
+ }
}
/// Declares that a type contains a [`Work<T, ID>`].
--
2.55.0