Rust for Linux List
 help / color / mirror / Atom feed
From: Sagar Taunk <sagartaunk2@gmail.com>
To: "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>,
	"Sagar Taunk" <sagartaunk2@gmail.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Viresh Kumar" <viresh.kumar@linaro.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] rust: workqueue: document SAFETY for `RawWorkItem` impl on `Pin<KBox<T>>`
Date: Thu, 30 Apr 2026 17:24:27 +0530	[thread overview]
Message-ID: <20260430115428.48087-1-sagartaunk2@gmail.com> (raw)

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

The `work_struct` raw pointer is valid for the duration of the closure
call because it comes from a `KBox<T>` allocation that is not dropped by
`__enqueue` itself. Since `Pin<KBox<T>>` requires exclusive ownership, the
work item cannot already be queued, so `queue_work_on` always returns true
and the pointer remains valid until `WorkItemPointer::run` is called, where
it is reclaimed via `KBox::from_raw`.

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

diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
index 676ade0539e3..7554601cb968 100644
--- a/rust/kernel/workqueue.rs
+++ b/rust/kernel/workqueue.rs
@@ -910,7 +910,14 @@ unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
     }
 }
 
-// SAFETY: TODO.
+// SAFETY: The `work_struct` raw pointer is guaranteed to be valid for the duration of the call to
+// the closure because it comes from a `KBox<T>` which guarantees a valid heap allocation,
+// and we don't drop the allocation ourselves. It is further guaranteed to be valid until
+// a call to the function pointer in `work_struct` because we leak the memory it points to.
+// Since `Pin<KBox<T>>` requires exclusive ownership, the work item cannot already be queued,
+// so `queue_work_on` always returns true, and we only reclaim the pointer in
+// `WorkItemPointer::run`, which is what the function pointer in the `work_struct` must be
+// pointing to, according to the safety requirements of `WorkItemPointer`.
 unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<KBox<T>>
 where
     T: WorkItem<ID, Pointer = Self>,
-- 
2.54.0


                 reply	other threads:[~2026-04-30 11:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260430115428.48087-1-sagartaunk2@gmail.com \
    --to=sagartaunk2@gmail.com \
    --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=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=viresh.kumar@linaro.org \
    /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