mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Tejun Heo" <tj@kernel.org>, "Alice Ryhl" <aliceryhl@google.com>
Cc: "Lai Jiangshan" <jiangshanlai@gmail.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"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>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Philipp Stanner" <phasta@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	driver-core@lists.linux.dev
Subject: Re: [PATCH v4 5/5] rust: workqueue: add ScopedWork for non-'static work items
Date: Wed, 25 Nov 2026 17:09:27 +0000	[thread overview]
Message-ID: <DN4GHTBW875I.1I5XBLY7M57HX@garyguo.net> (raw)
In-Reply-To: <5eec17370f67438e43bb1c82b1d6a16b@kernel.org>

On Wed Nov 25, 2026 at 4:44 PM GMT, Tejun Heo wrote:
> Hello, Alice.
>
> On Thu, Nov 19, 2026 at 02:12:46PM +0000, Alice Ryhl wrote:
>> Because the cancellation guarantee only holds if the destructor runs
>> before the bound Queue or any lifetime in T expires, ScopedWork::new()
>> (and thus new_scoped_work!) is unsafe and requires that the resulting
>> value is never forgotten and is dropped before 'wq or T's lifetimes end.
>
> I'm no Rust expert, so maybe this is me thinking too much in C, but
> requiring the compiler to understand and enforce that a workqueue outlives
> its scoped work items doesn't seem very convenient to use. Wouldn't it be
> easier to package e.g. a percpu refcnt with the workqueue, have each scoped
> work item hold a reference from creation to destruction, and make
> destroy_workqueue() keep draining until the count reaches zero?

The workqueue outliving scoped work is actually already enforced by the Rust
compiler, however Rust provides no guarantted destructor execution (say if you
construct the ScopedWork on a reference counted container and somehow leaked a
reference count).

Even in such cases, Rust lifetime will still make sure no external code will be
able to reference the leaked objects -- however, for work item a callback can
fire and access itself, hence we need this guarantee.

Adding a reference count to the workqueue solves the issue of work item
accessing the workqueue from its callback, however it is not sufficient, as the
workqueue may reference a device resource, and in such cases it captures a
lifetime of the device being bound. This lifetime cannot be reference counted as
when the device is unbound, the resources are gone and it's not permitted to use
reference counting to keep it alive.

So we still need the guaranteed destructor execution guarantee and reference
counting wq alone is not sufficient to remove this safety requirement.

Best,
Gary

  reply	other threads:[~2026-11-25 17:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 14:12 [PATCH v4 0/5] workqueue: OwnedQueue and ScopedWork Alice Ryhl
2026-11-19 14:12 ` [PATCH v4 1/5] rust: workqueue: support stack-allocated work items Alice Ryhl
2026-11-19 14:37   ` Gary Guo
2026-11-19 15:17     ` Alice Ryhl
2026-11-25 16:56       ` Gary Guo
2026-11-19 14:12 ` [PATCH v4 2/5] rust: workqueue: restrict delayed work to global wqs Alice Ryhl
2026-11-19 14:12 ` [PATCH v4 3/5] rust: workqueue: create workqueue subdirectory Alice Ryhl
2026-11-19 14:37   ` Gary Guo
2026-11-19 14:12 ` [PATCH v4 4/5] rust: workqueue: add creation of workqueues Alice Ryhl
2026-11-25 16:57   ` Gary Guo
2026-11-19 14:12 ` [PATCH v4 5/5] rust: workqueue: add ScopedWork for non-'static work items Alice Ryhl
2026-11-25 16:44   ` Tejun Heo
2026-11-25 17:09     ` Gary Guo [this message]
2026-11-25 17:09   ` Gary Guo

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=DN4GHTBW875I.1I5XBLY7M57HX@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=jhubbard@nvidia.com \
    --cc=jiangshanlai@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=phasta@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tj@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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

all inboxes | Powered by JetHome®