From: "Gary Guo" <gary@garyguo.net>
To: "Gary Guo" <gary@garyguo.net>,
"Danilo Krummrich" <dakr@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"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>,
"Onur Özkan" <work@onurozkan.dev>,
"Eliot Courtney" <ecourtney@nvidia.com>
Cc: <nova-gpu@lists.linux.dev>, <dri-devel@lists.freedesktop.org>,
<linux-kernel@vger.kernel.org>, <driver-core@lists.linux.dev>,
<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH] rust: device: make lifetime on `Core` and `CoreInternal` invariant
Date: Mon, 13 Jul 2026 21:23:48 +0100 [thread overview]
Message-ID: <DJXQ32PBP5Z8.2ADOOIWKYOLP5@garyguo.net> (raw)
In-Reply-To: <20260713201455.640151-1-gary@kernel.org>
On Mon Jul 13, 2026 at 9:14 PM BST, Gary Guo wrote:
> From: Gary Guo <gary@garyguo.net>
>
> Currently the lifetime on `Core` and `CoreInternal` is covariant. This
> means that they can be coerced into shorter living lifetimes. On `probe`
> function, signature has `&'bound Device<Core<'a>>`; the type's wellformness
> would imply `'a: 'bound` and thus the type can be coerced `&'bound
> Device<Core<'bound>>`, defeating the purpose of having the lifetime bound
> to prevent users of the `Core` type to escape the function.
>
> Fix this by making the lifetime invariant, so the coercion is impossible.
> The lifetime here only needs to be "branded" so it does not coerce or unify
> with other lifetimes, so we do not need to ensure `'bound: 'a`.
>
> This requires modifying `nova-core` which relies on this implied bound due
> to pre-2024 capture rule. The "use" bound can be removed if built with
> edition 2024.
Actually, a more proper fix is to make `Device` invariant over their
context. It would require touching all `Device`, though.
Danilo, let me know if you'd prefer that approach instead.
Best,
Gary
>
> Fixes: 24799831d631 ("rust: device: make Core and CoreInternal lifetime-parameterized")
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
> drivers/gpu/nova-core/gpu.rs | 6 +++---
> rust/kernel/device.rs | 10 ++++++++--
> 2 files changed, 11 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs
> index b3c91731db45..b603b0bd2692 100644
> --- a/drivers/gpu/nova-core/gpu.rs
> +++ b/drivers/gpu/nova-core/gpu.rs
> @@ -285,10 +285,10 @@ pub(crate) struct Gpu<'gpu> {
> }
>
> impl<'gpu> Gpu<'gpu> {
> - pub(crate) fn new(
> - pdev: &'gpu pci::Device<device::Core<'_>>,
> + pub(crate) fn new<'a>(
> + pdev: &'gpu pci::Device<device::Core<'a>>,
> bar: Bar0<'gpu>,
> - ) -> impl PinInit<Self, Error> + 'gpu {
> + ) -> impl PinInit<Self, Error> + use<'gpu, 'a> {
> try_pin_init!(Self {
> device: pdev.as_ref(),
> spec: Spec::new(pdev.as_ref(), bar).inspect(|spec| {
> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> index 645afc49a27d..db25ed1ae8e5 100644
> --- a/rust/kernel/device.rs
> +++ b/rust/kernel/device.rs
> @@ -511,7 +511,11 @@ pub trait DeviceContext: private::Sealed {}
> /// callback it appears in. It is intended to be used for synchronization purposes. Bus device
> /// implementations can implement methods for [`Device<Core>`], such that they can only be called
> /// from bus callbacks.
> -pub struct Core<'a>(PhantomData<&'a ()>);
> +///
> +/// The lifetime `'a` is for "lifetime branding" purpose. Callbacks need to polymorphic over this
> +/// lifetime so the `&'bound Device<Core<'_>>` provided to them cannot outlive the scope of the
> +/// function. For this reason, it needs to be invariant.
> +pub struct Core<'a>(PhantomData<fn(&'a ()) -> &'a ()>);
>
> /// Semantically the same as [`Core`], but reserved for internal usage of the corresponding bus
> /// abstraction.
> @@ -522,7 +526,9 @@ pub trait DeviceContext: private::Sealed {}
> ///
> /// This context mainly exists to share generic [`Device`] infrastructure that should only be called
> /// from bus callbacks with bus abstractions, but without making them accessible for drivers.
> -pub struct CoreInternal<'a>(PhantomData<&'a ()>);
> +///
> +/// Lifetime `'a` is invariant for the same reason as [`Core`].
> +pub struct CoreInternal<'a>(PhantomData<fn(&'a ()) -> &'a ()>);
>
> /// The [`Bound`] context is the [`DeviceContext`] of a bus specific device when it is guaranteed to
> /// be bound to a driver.
>
> base-commit: a13c140cc289c0b7b3770bce5b3ad42ab35074aa
next prev parent reply other threads:[~2026-07-13 20:23 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-13 20:14 [PATCH] rust: device: make lifetime on `Core` and `CoreInternal` invariant Gary Guo
2026-07-13 20:23 ` Gary Guo [this message]
2026-07-13 20:55 ` Danilo Krummrich
2026-07-13 20:29 ` Danilo Krummrich
2026-07-13 21:03 ` Gary Guo
2026-07-13 21:09 ` Danilo Krummrich
2026-07-14 13:47 ` kernel test robot
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=DJXQ32PBP5Z8.2ADOOIWKYOLP5@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=driver-core@lists.linux.dev \
--cc=ecourtney@nvidia.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@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