public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Onur Özkan" <work@onurozkan.dev>
To: Sagar Taunk <sagartaunk2@gmail.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: workqueue: replace SAFETY TODO for `WorkItemPointer` impl on `Pin<KBox<T>>`
Date: Thu, 30 Apr 2026 08:58:51 +0300	[thread overview]
Message-ID: <20260430055852.18927-1-work@onurozkan.dev> (raw)
In-Reply-To: <20260430004857.38281-1-sagartaunk2@gmail.com>

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
> 

  reply	other threads:[~2026-04-30  5:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-04-30  6:17   ` Sagar Taunk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260430055852.18927-1-work@onurozkan.dev \
    --to=work@onurozkan.dev \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sagartaunk2@gmail.com \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox