Skip to contentSkip to conversation

rust: workqueue: fix SAFETY comment Arc refs in Pin<KBox<T>>

PATCH Superseded Sagar Taunk 5 participants 5 messages Apr 25 – Apr 28 Rust

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: workqueue: fix SAFETY comment Arc refs in Pin<KBox<T>>

The WorkItemPointer implementation for Pin<KBox<T>> contained SAFETY comments that incorrectly referenced Arc::into_raw instead of KBox::into_raw. This implementation uses KBox, not Arc, so update the comments to accurately reflect the actual ownership transfer.

---
 rust/kernel/workqueue.rs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
rust/kernel/workqueue.rs +3 -2
index 7e253b6f299c..74c59f2b1c09 100644
@@ -890,9 +890,10 @@ unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
         // The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
         let ptr = ptr.cast::<Work<T, ID>>();
-        // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
+        // SAFETY: This computes the pointer that `__enqueue` got from `KBox::into_raw`.
         let ptr = unsafe { T::work_container_of(ptr) };
-        // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
+        // SAFETY: This pointer comes from `KBox::into_raw` and we have been given back ownership,
+        // as the workqueue guarantees `run` is called exactly once.
         let boxed = unsafe { KBox::from_raw(ptr) };
         // SAFETY: The box was already pinned when it was enqueued.
         let pinned = unsafe { Pin::new_unchecked(boxed) };
Signature · 1 line
2.54.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Sagar Taunk

The WorkItemPointer implementation for Pin<KBox<T>> contained SAFETY comments that incorrectly referenced Arc::into_raw instead of KBox::into_raw. This implementation uses KBox, not Arc, so update the comments to accurately reflect the actual ownership transfer.

---
 rust/kernel/workqueue.rs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
rust/kernel/workqueue.rs +0 -0
index 7e253b6f299c..74c59f2b1c09 100644

rust/kernel/workqueue.rs unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>> · line 896

Quoted 9 lines
rust/kernel/workqueue.rs +3 -2
@@ -890,9 +890,10 @@ unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
         // The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
         let ptr = ptr.cast::<Work<T, ID>>();
-        // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
+        // SAFETY: This computes the pointer that `__enqueue` got from `KBox::into_raw`.
         let ptr = unsafe { T::work_container_of(ptr) };
-        // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
+        // SAFETY: This pointer comes from `KBox::into_raw` and we have been given back ownership,
+        // as the workqueue guarantees `run` is called exactly once.

Not sure if extending the comment is useful, but the comment looks correct.

#2 lore raw
In reply to Sagar Taunk
Quoted 9 lines · Sagar Taunk

The WorkItemPointer implementation for Pin<KBox<T>> contained SAFETY comments that incorrectly referenced Arc::into_raw instead of KBox::into_raw. This implementation uses KBox, not Arc, so update the comments to accurately reflect the actual ownership transfer.

I am copying a procedural comment since I just wrote it for another patch:

For this case it doesn't matter too much, but in general, when something is a fix, we add a Fixes: tag (and in most cases Cc: stable@ to accompany it). That helps backporting (when needed), adds context e.g. for reviewing (at times), etc.

Thanks!

Cheers,
Miguel

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Sagar Taunk
Quoted 8 lines · Sagar Taunk

The WorkItemPointer implementation for Pin<KBox<T>> contained SAFETY comments that incorrectly referenced Arc::into_raw instead of KBox::into_raw. This implementation uses KBox, not Arc, so update the comments to accurately reflect the actual ownership transfer.

Thanks!

reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Sagar Taunk
Quoted 32 lines · On Sat, 25 Apr 2026 19:27:01 +0530 Sagar Taunk <[email protected]> wrote:

The WorkItemPointer implementation for Pin<KBox<T>> contained SAFETY comments that incorrectly referenced Arc::into_raw instead of KBox::into_raw. This implementation uses KBox, not Arc, so update the comments to accurately reflect the actual ownership transfer.

---
 rust/kernel/workqueue.rs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
rust/kernel/workqueue.rs +3 -2
index 7e253b6f299c..74c59f2b1c09 100644
@@ -890,9 +890,10 @@ unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
         // The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
         let ptr = ptr.cast::<Work<T, ID>>();
-        // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
+        // SAFETY: This computes the pointer that `__enqueue` got from `KBox::into_raw`.
         let ptr = unsafe { T::work_container_of(ptr) };
-        // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
+        // SAFETY: This pointer comes from `KBox::into_raw` and we have been given back ownership,
+        // as the workqueue guarantees `run` is called exactly once.
         let boxed = unsafe { KBox::from_raw(ptr) };
         // SAFETY: The box was already pinned when it was enqueued.
         let pinned = unsafe { Pin::new_unchecked(boxed) };
Signature · 1 line
2.54.0

Thank you!