Skip to contentSkip to conversation

rust: workqueue: replace SAFETY TODO for `WorkItemPointer` impl on `Pin<KBox<T>>`

PATCH Superseded Sagar Taunk 2 participants 3 messages Apr 30 Rust

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: workqueue: replace SAFETY TODO for `WorkItemPointer` impl on `Pin<KBox<T>>`

The original implementation left a SAFETY: TODO comment on the WorkItemPointer implementation for Pin<KBox<T>>. This patch documents the safety requirements that make this implementation sound.

The safety argument relies on three guarantees: __enqueue strips the Pin wrapper via Pin::into_inner_unchecked and leaks the box via KBox::into_raw, producing a *mut T whose allocation remains live for the duration of the queued work; work_container_of safely reverses the raw_get_work offset arithmetic to recover the exact *mut T that __enqueue produced; and the workqueue guarantees run is called exactly once, making KBox::from_raw sound.

---
 rust/kernel/workqueue.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
rust/kernel/workqueue.rs +6 -1
index 74c59f2b1c09..f31412fca303 100644
@@ -881,7 +881,12 @@ unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for Arc<T>
 {
 }
 
-// SAFETY: TODO.
+// SAFETY: The `work_struct` pointer passed to `run` originates from `__enqueue`,
+// which strips the `Pin` wrapper via `Pin::into_inner_unchecked()` and leaks the box
+// via `KBox::into_raw()`, producing a `*mut T`. `work_container_of` then safely reverses
+// the `raw_get_work` offset to recover that exact `*mut T`. The workqueue itself guarantees
+// that `run` is called exactly once, so `KBox::from_raw()` correctly reclaims ownership
+// of the leaked box.
 unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
 where
     T: WorkItem<ID, Pointer = Self>,
Signature · 1 line
2.54.0
reviewer
#1 lore raw
Quoted 22 lines · On Thu, 30 Apr 2026 06:18:56 +0530 Sagar Taunk <[email protected]> wrote:

The original implementation left a SAFETY: TODO comment on the WorkItemPointer implementation for Pin<KBox<T>>. This patch documents the safety requirements that make this implementation sound.

The safety argument relies on three guarantees: __enqueue strips the Pin wrapper via Pin::into_inner_unchecked and leaks the box via KBox::into_raw, producing a *mut T whose allocation remains live for the duration of the queued work; work_container_of safely reverses the raw_get_work offset arithmetic to recover the exact *mut T that __enqueue produced; and the workqueue guarantees run is called exactly once, making KBox::from_raw sound.

---
 rust/kernel/workqueue.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
rust/kernel/workqueue.rs +0 -0
index 74c59f2b1c09..f31412fca303 100644

rust/kernel/workqueue.rs unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for Arc<T> · line 889

Quoted 10 lines
rust/kernel/workqueue.rs +6 -1
@@ -881,7 +881,12 @@ unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for Arc<T>
 {
 }
 
-// SAFETY: TODO.
+// SAFETY: The `work_struct` pointer passed to `run` originates from `__enqueue`,
+// which strips the `Pin` wrapper via `Pin::into_inner_unchecked()` and leaks the box
+// via `KBox::into_raw()`, producing a `*mut T`. `work_container_of` then safely reverses
+// the `raw_get_work` offset to recover that exact `*mut T`. The workqueue itself guarantees
+// that `run` is called exactly once, so `KBox::from_raw()` correctly reclaims ownership
+// of the leaked box.

Not very human readable (at least for me)... Perhaps you can check the notes from ARef<T> and Arc<T> impls? It They should have some parts in common like the initialization part.

- Onur

Quoted 6 lines
rust/kernel/workqueue.rs +0 -0
 unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
 where
     T: WorkItem<ID, Pointer = Self>,
Signature · 1 line
2.54.0
#2 lore raw

Thank you for the feedback. Are there any other things I should try to improve in v2?

Quoted 49 lines · Onur Özkan
Quoted 22 lines · On Thu, 30 Apr 2026 06:18:56 +0530 Sagar Taunk <[email protected]> wrote:

The original implementation left a SAFETY: TODO comment on the WorkItemPointer implementation for Pin<KBox<T>>. This patch documents the safety requirements that make this implementation sound.

The safety argument relies on three guarantees: __enqueue strips the Pin wrapper via Pin::into_inner_unchecked and leaks the box via KBox::into_raw, producing a *mut T whose allocation remains live for the duration of the queued work; work_container_of safely reverses the raw_get_work offset arithmetic to recover the exact *mut T that __enqueue produced; and the workqueue guarantees run is called exactly once, making KBox::from_raw sound.

---
 rust/kernel/workqueue.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
rust/kernel/workqueue.rs +0 -0
index 74c59f2b1c09..f31412fca303 100644

rust/kernel/workqueue.rs unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for Arc<T> · line 889

Quoted 10 lines
rust/kernel/workqueue.rs +6 -1
@@ -881,7 +881,12 @@ unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for Arc<T>
 {
 }
 
-// SAFETY: TODO.
+// SAFETY: The `work_struct` pointer passed to `run` originates from `__enqueue`,
+// which strips the `Pin` wrapper via `Pin::into_inner_unchecked()` and leaks the box
+// via `KBox::into_raw()`, producing a `*mut T`. `work_container_of` then safely reverses
+// the `raw_get_work` offset to recover that exact `*mut T`. The workqueue itself guarantees
+// that `run` is called exactly once, so `KBox::from_raw()` correctly reclaims ownership
+// of the leaked box.

Not very human readable (at least for me)... Perhaps you can check the notes from ARef<T> and Arc<T> impls? It They should have some parts in common like the initialization part.

- Onur

Quoted 6 lines
rust/kernel/workqueue.rs +0 -1
 unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
 where
     T: WorkItem<ID, Pointer = Self>,
--

2.54.0