public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: workqueue: replace SAFETY TODO for `WorkItemPointer` impl on `Pin<KBox<T>>`
@ 2026-04-30  0:48 Sagar Taunk
  2026-04-30  5:58 ` Onur Özkan
  0 siblings, 1 reply; 3+ messages in thread
From: Sagar Taunk @ 2026-04-30  0:48 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Tamir Duberstein, Sagar Taunk, Daniel Almeida,
	rust-for-linux, linux-kernel

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.

Signed-off-by: Sagar Taunk <sagartaunk2@gmail.com>
---
 rust/kernel/workqueue.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
index 74c59f2b1c09..f31412fca303 100644
--- a/rust/kernel/workqueue.rs
+++ b/rust/kernel/workqueue.rs
@@ -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>,
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust: workqueue: replace SAFETY TODO for `WorkItemPointer` impl on `Pin<KBox<T>>`
  2026-04-30  0:48 [PATCH] rust: workqueue: replace SAFETY TODO for `WorkItemPointer` impl on `Pin<KBox<T>>` Sagar Taunk
@ 2026-04-30  5:58 ` Onur Özkan
  2026-04-30  6:17   ` Sagar Taunk
  0 siblings, 1 reply; 3+ messages in thread
From: Onur Özkan @ 2026-04-30  5:58 UTC (permalink / raw)
  To: Sagar Taunk
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Tamir Duberstein, Daniel Almeida,
	rust-for-linux, linux-kernel

On Thu, 30 Apr 2026 06:18:56 +0530
Sagar Taunk <sagartaunk2@gmail.com> 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.
> 
> Signed-off-by: Sagar Taunk <sagartaunk2@gmail.com>
> ---
>  rust/kernel/workqueue.rs | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
> index 74c59f2b1c09..f31412fca303 100644
> --- a/rust/kernel/workqueue.rs
> +++ b/rust/kernel/workqueue.rs
> @@ -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

>  unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
>  where
>      T: WorkItem<ID, Pointer = Self>,
> -- 
> 2.54.0
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust: workqueue: replace SAFETY TODO for `WorkItemPointer` impl on `Pin<KBox<T>>`
  2026-04-30  5:58 ` Onur Özkan
@ 2026-04-30  6:17   ` Sagar Taunk
  0 siblings, 0 replies; 3+ messages in thread
From: Sagar Taunk @ 2026-04-30  6:17 UTC (permalink / raw)
  To: Onur Özkan
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Tamir Duberstein, Daniel Almeida,
	rust-for-linux, linux-kernel

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


On Thu, 30 Apr 2026 at 11:29, Onur Özkan <work@onurozkan.dev> wrote:
>
> On Thu, 30 Apr 2026 06:18:56 +0530
> Sagar Taunk <sagartaunk2@gmail.com> 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.
> >
> > Signed-off-by: Sagar Taunk <sagartaunk2@gmail.com>
> > ---
> >  rust/kernel/workqueue.rs | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
> > index 74c59f2b1c09..f31412fca303 100644
> > --- a/rust/kernel/workqueue.rs
> > +++ b/rust/kernel/workqueue.rs
> > @@ -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
>
> >  unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
> >  where
> >      T: WorkItem<ID, Pointer = Self>,
> > --
> > 2.54.0
> >

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-30  6:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-30  0:48 [PATCH] rust: workqueue: replace SAFETY TODO for `WorkItemPointer` impl on `Pin<KBox<T>>` Sagar Taunk
2026-04-30  5:58 ` Onur Özkan
2026-04-30  6:17   ` Sagar Taunk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox