From: "Onur Özkan" <work@onurozkan.dev>
To: Alice Ryhl <aliceryhl@google.com>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net,
bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, tmgross@umich.edu, dakr@kernel.org,
peterz@infradead.org, fujita.tomonori@gmail.com,
tamird@kernel.org
Subject: Re: [PATCH v1] rust: workqueue: add cancel_sync support
Date: Sun, 10 May 2026 18:53:10 +0300 [thread overview]
Message-ID: <20260510155311.7692-1-work@onurozkan.dev> (raw)
In-Reply-To: <agCLn6ebK9wERK-q@google.com>
On Sun, 10 May 2026 13:43:59 +0000
Alice Ryhl <aliceryhl@google.com> wrote:
> On Sun, May 10, 2026 at 11:21:57AM +0300, Onur Özkan wrote:
> > Drivers can use this during teardown to cancel pending work and wait for
> > running work to finish before dropping related resources.
> >
> > This is not implemented for Pin<KBox<T>> because queuing a boxed work
> > item transfers ownership of the box to the workqueue. There is therefore
> > no separate safe owner that can cancel the boxed work while it is pending.
> >
> > The immediate motivation is the Tyr reset infrastructure [1], which needs
> > to cancel pending reset work and wait for any running reset work during
> > teardown before dropping the resources used by that work.
> >
> > [1]: https://lore.kernel.org/all/20260416171728.205141-1-work@onurozkan.dev
> >
> > Signed-off-by: Onur Özkan <work@onurozkan.dev>
> > ---
> > rust/kernel/workqueue.rs | 134 ++++++++++++++++++++++++++++++++-------
> > 1 file changed, 112 insertions(+), 22 deletions(-)
> >
> > diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
> > index 7e253b6f299c..a10daa2763ac 100644
> > --- a/rust/kernel/workqueue.rs
> > +++ b/rust/kernel/workqueue.rs
> > @@ -442,23 +442,44 @@ pub unsafe trait RawDelayedWorkItem<const ID: u64>: RawWorkItem<ID> {}
> > ///
> > /// # Safety
> > ///
> > -/// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`]
> > -/// method of this trait as the function pointer.
> > +/// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with [`run`] as
> > +/// its function pointer, and that [`from_raw_work`] rebuilds the exact ownership transferred by
> > +/// a successful [`__enqueue`] call.
> > ///
> > /// [`__enqueue`]: RawWorkItem::__enqueue
> > +/// [`from_raw_work`]: WorkItemPointer::from_raw_work
> > /// [`run`]: WorkItemPointer::run
> > -pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> {
> > - /// Run this work item.
> > +pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> + Sized {
> > + /// The work item type containing the embedded `work_struct`.
> > + type Item: WorkItem<ID, Pointer = Self> + ?Sized;
> > +
> > + /// Rebuild this work item's pointer from its embedded `work_struct`.
> > ///
> > /// # Safety
> > ///
> > - /// The provided `work_struct` pointer must originate from a previous call to [`__enqueue`]
> > - /// where the `queue_work_on` closure returned true, and the pointer must still be valid.
> > + /// The provided `work_struct` pointer must originate from a previous call to
> > + /// [`RawWorkItem::__enqueue`] where the `queue_work_on` closure returned true
> > + /// and the pointer must still be valid.
> > + unsafe fn from_raw_work(ptr: *mut bindings::work_struct) -> Self;
> > +
> > + /// Run this work item.
> > ///
> > - /// [`__enqueue`]: RawWorkItem::__enqueue
> > - unsafe extern "C" fn run(ptr: *mut bindings::work_struct);
> > + /// # Safety
> > + ///
> > + /// The provided `work_struct` pointer must satisfy the same requirements as
> > + /// [`WorkItemPointer::from_raw_work`].
> > + #[inline]
> > + unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
> > + <Self::Item as WorkItem<ID>>::run(
> > + // SAFETY: The requirements for `run` are exactly those of `from_raw_work`.
> > + unsafe { Self::from_raw_work(ptr) },
> > + );
> > + }
> > }
> >
> > +/// Marker for work item types that support cancellation.
> > +pub trait SupportsCancelling<const ID: u64>: WorkItemPointer<ID> {}
>
> Shouldn't 'from_raw_work()' be a method on SupportsCancelling instead?
Not necessarily. I wanted to keep it like that because it removes a bit of
duplication and makes run() simpler. It's also more extensible e.g. can be
used for disable_sync too if needed in the future.
-Onur
>
> Alice
prev parent reply other threads:[~2026-05-10 15:53 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-10 8:21 [PATCH v1] rust: workqueue: add cancel_sync support Onur Özkan
2026-05-10 13:43 ` Alice Ryhl
2026-05-10 15:53 ` Onur Özkan [this message]
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=20260510155311.7692-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=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--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