mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Danilo Krummrich" <dakr@kernel.org>, <aliceryhl@google.com>,
	<daniel.almeida@collabora.com>, <acourbot@nvidia.com>,
	<ecourtney@nvidia.com>, <ojeda@kernel.org>, <boqun@kernel.org>,
	<gary@garyguo.net>, <bjorn3_gh@protonmail.com>,
	<lossin@kernel.org>, <a.hindborg@kernel.org>, <tmgross@umich.edu>,
	<deborah.brouwer@collabora.com>, <boris.brezillon@collabora.com>,
	<lyude@redhat.com>
Cc: <driver-core@lists.linux.dev>, <linux-kernel@vger.kernel.org>,
	<nova-gpu@lists.linux.dev>, <dri-devel@lists.freedesktop.org>,
	<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v4 14/16] rust: drm: Add RegistrationData to drm::Driver
Date: Thu, 25 Jun 2026 16:12:58 +0100	[thread overview]
Message-ID: <DJI879ROZKNI.2U77EST3OH7QI@garyguo.net> (raw)
In-Reply-To: <20260620184924.2247517-15-dakr@kernel.org>

On Sat Jun 20, 2026 at 7:48 PM BST, Danilo Krummrich wrote:
> Add a RegistrationData associated type to drm::Driver. This is a ForLt
> type whose lifetime is tied to the parent bus device binding scope.
> Registration<'a, T> takes ownership of the data via Pin<KBox<_>>,
> storing it with its real lifetime. The pointer is written to drm::Device
> before drm_dev_register() to ensure it is already in place when ioctls
> arrive.
>
> Device<T, Registered>::registration_data_with() provides access with the
> lifetime shortened from 'static via a pointer cast. Since
> Registration::drop() calls drm_dev_unplug(), which performs an SRCU
> barrier waiting for all drm_dev_enter() critical sections to complete,
> the data is guaranteed to remain valid for the duration of any
> RegistrationGuard.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  drivers/gpu/drm/nova/driver.rs |  20 +++++--
>  drivers/gpu/drm/tyr/driver.rs  |  18 ++++--
>  rust/kernel/drm/device.rs      |  49 +++++++++++++++
>  rust/kernel/drm/driver.rs      | 106 +++++++++++++++++++++------------
>  4 files changed, 143 insertions(+), 50 deletions(-)
>
> diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs
> index ceb2829985c7..e42b48e5cad2 100644
> --- a/rust/kernel/drm/driver.rs
> +++ b/rust/kernel/drm/driver.rs
> @@ -7,11 +7,11 @@
>  use crate::{
>      bindings,
>      device,
> -    devres,
>      drm,
>      error::to_result,
>      prelude::*,
> -    sync::aref::ARef, //
> +    sync::aref::ARef,
> +    types::ForLt, //
>  };
>  use core::{
>      mem,
> @@ -110,6 +110,16 @@ pub trait Driver {
>      /// Context data associated with the DRM driver
>      type Data: Sync + Send;
>  
> +    /// Data owned by the [`Registration`] and accessible within a
> +    /// [`RegistrationGuard`](drm::RegistrationGuard) critical section via
> +    /// [`Device::registration_data_with()`](drm::Device::registration_data_with).
> +    ///
> +    /// This is a [`ForLt`](trait@ForLt) type whose lifetime is tied to the parent bus
> +    /// device binding scope. References handed out by
> +    /// [`Device::registration_data_with()`](drm::Device::registration_data_with) are tied to
> +    /// the closure scope.
> +    type RegistrationData: ForLt;

This could just be a GAT instead of using `ForLt`. It'd also have the benefit of
being able to have `Sync + Send` directly here instead of sprinkle the bound
everywhere.

Best,
Gary

> +
>      /// The type used to manage memory for this driver.
>      type Object: AllocImpl;
>  
> @@ -139,65 +149,82 @@ pub trait Driver {
>  /// The registration type of a `drm::Device`.
>  ///
>  /// Once the `Registration` structure is dropped, the device is unregistered.
> -pub struct Registration<T: Driver>(ARef<drm::Device<T>>);
> -
> -impl<T: Driver> Registration<T> {
> -    fn new(drm: drm::UnregisteredDevice<T>, flags: usize) -> Result<Self> {
> -        // SAFETY: `drm.as_raw()` is valid by the invariants of `drm::Device`.
> -        to_result(unsafe { bindings::drm_dev_register(drm.as_raw(), flags) })?;
> -
> -        // SAFETY: We just called `drm_dev_register` above
> -        let new = NonNull::from(unsafe { drm.assume_ctx() });
> -
> -        // Leak the ARef from UnregisteredDevice in preparation for transferring its ownership.
> -        mem::forget(drm);
> -
> -        // SAFETY: `drm`'s `Drop` constructor was never called, ensuring that there remains at least
> -        // one reference to the device - which we take ownership over here.
> -        let new = unsafe { ARef::from_raw(new) };
> -
> -        Ok(Self(new))
> -    }
> +pub struct Registration<'a, T: Driver> {
> +    drm: ARef<drm::Device<T>>,
> +    _reg_data: Pin<KBox<<T::RegistrationData as ForLt>::Of<'a>>>,
> +}


  parent reply	other threads:[~2026-06-25 15:13 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-20 18:47 [PATCH v4 00/16] rust: drm: Higher-Ranked Lifetime private data Danilo Krummrich
2026-06-20 18:47 ` [PATCH v4 01/16] rust: drm: ioctl: fix unbounded lifetimes in ioctl handler arguments Danilo Krummrich
2026-06-25 14:51   ` Gary Guo
2026-06-20 18:47 ` [PATCH v4 02/16] rust: drm: rename Uninit DeviceContext to Normal Danilo Krummrich
2026-06-20 18:47 ` [PATCH v4 03/16] rust: drm: Add Driver::ParentDevice associated type Danilo Krummrich
2026-06-20 18:47 ` [PATCH v4 04/16] rust: drm: change default DeviceContext to Normal Danilo Krummrich
2026-06-20 18:47 ` [PATCH v4 05/16] rust: drm: restrict AlwaysRefCounted to Normal Device context Danilo Krummrich
2026-06-20 18:47 ` [PATCH v4 06/16] rust: drm: restrict AlwaysRefCounted to Normal GEM Object context Danilo Krummrich
2026-06-22 17:52   ` Lyude Paul
2026-06-22 18:03     ` Danilo Krummrich
2026-06-22 18:25       ` Lyude Paul
2026-06-22 18:54         ` Danilo Krummrich
2026-06-22 20:23           ` Lyude Paul
2026-06-20 18:48 ` [PATCH v4 07/16] rust: drm: split Deref for Device context typestates Danilo Krummrich
2026-06-20 18:48 ` [PATCH v4 08/16] rust: drm: pin ioctl Device reference to Normal context Danilo Krummrich
2026-06-20 18:48 ` [PATCH v4 09/16] rust: drm: add Ioctl device context typestate Danilo Krummrich
2026-06-20 18:48 ` [PATCH v4 10/16] rust: drm: Add RegistrationGuard for drm_dev_enter/exit critical sections Danilo Krummrich
2026-06-20 18:48 ` [PATCH v4 11/16] rust: drm: Wrap ioctl dispatch in RegistrationGuard Danilo Krummrich
2026-06-22 19:01   ` Lyude Paul
2026-06-20 18:48 ` [PATCH v4 12/16] rust: drm: return ParentDevice from Device AsRef Danilo Krummrich
2026-06-20 18:48 ` [PATCH v4 13/16] rust: drm: add AsRef<ParentDevice<Bound>> for Device<Registered> Danilo Krummrich
2026-06-20 18:48 ` [PATCH v4 14/16] rust: drm: Add RegistrationData to drm::Driver Danilo Krummrich
2026-06-22 20:17   ` Lyude Paul
2026-06-22 21:32     ` Danilo Krummrich
2026-06-25 15:12   ` Gary Guo [this message]
2026-06-20 18:48 ` [PATCH v4 15/16] rust: drm: Pass registration data to ioctl handlers Danilo Krummrich
2026-06-20 18:48 ` [PATCH v4 16/16] drm: nova: Use drm::Device<Registered> to access the parent bus device Danilo Krummrich
2026-06-22 21:10 ` [PATCH v4 00/16] rust: drm: Higher-Ranked Lifetime private data Lyude Paul

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=DJI879ROZKNI.2U77EST3OH7QI@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=boris.brezillon@collabora.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=deborah.brouwer@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=ecourtney@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.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

all inboxes | Powered by JetHome®