[PATCH 6/7] rust: workqueue: add Work::cancel_work_sync()

Danilo Krummrich <[email protected]> Tue, 4 Aug 2026 21:52:08 +0200
Newsgroups org.kernel.vger.rust-for-linux,dev.linux.lists.driver-core,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
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 <[email protected]>
---
 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