rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@kernel.org>
To: Maxime Ripard <mripard@kernel.org>
Cc: airlied@gmail.com, simona@ffwll.ch,
	maarten.lankhorst@linux.intel.com, tzimmermann@suse.de,
	lyude@redhat.com, acurrid@nvidia.com, lina@asahilina.net,
	daniel.almeida@collabora.com, j@jannau.net, ojeda@kernel.org,
	alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, benno.lossin@proton.me,
	a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
	dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH 5/8] rust: drm: add DRM driver registration
Date: Wed, 26 Mar 2025 11:46:54 +0100	[thread overview]
Message-ID: <Z-PbHiaMNqR0FPbY@pollux> (raw)
In-Reply-To: <20250326-loyal-scrupulous-leech-59d44a@houat>

On Wed, Mar 26, 2025 at 10:24:43AM +0100, Maxime Ripard wrote:
> Hi,
> 
> On Wed, Mar 26, 2025 at 12:54:32AM +0100, Danilo Krummrich wrote:
> > Implement the DRM driver `Registration`.
> > 
> > The `Registration` structure is responsible to register and unregister a
> > DRM driver. It makes use of the `Devres` container in order to allow the
> > `Registration` to be owned by devres, such that it is automatically
> > dropped (and the DRM driver unregistered) once the parent device is
> > unbound.
> 
> The code looks correct, but the wording is confusing to me.
> drm_dev_unregister does indeed unregister the device, but it's not freed
> until the last reference is dropped, so it's not really "dropped once
> the parent device is unbound", the reference is, and it's not active
> anymore.

The above wording is related to the Registration structure itself, i.e. the
Registration is dropped, but not the the DRM device itself. However, if the
Registration had the last reference to the DRM device, then of course it's
freed.

> > +impl<T: Driver> Drop for Registration<T> {
> > +    /// Removes the registration from the kernel if it has completed successfully before.
> > +    fn drop(&mut self) {
> > +        // SAFETY: Safe by the invariant of `ARef<drm::Device<T>>`. The existence of this
> > +        // `Registration` also guarantees the this `drm::Device` is actually registered.
> > +        unsafe { bindings::drm_dev_unregister(self.0.as_raw()) };
> > +    }
> > +}
> 
> drm_dev_unregister also have an hotplug-aware variant in
> drm_dev_unplug(). However, most devices are hotpluggable, even if only
> through sysfs. So drm_dev_unplug() is generally a better option. Should
> we use it here, and / or should we provide multiple options still?
> 
> Another thing worth mentioning I think is that drm_dev_unplug() works by
> setting a flag, and drivers are expected to check that their access to
> device-bound resources (so registers mapping, clocks, regulators, etc.)
> are still there through drm_dev_enter/drm_dev_exit. It's pretty fragile
> overall, so I wonder if it's something we could abstract away in Rust.

DRM should not have to take care of the lifetime of device resources of the
parent device. This is the responsibility of the driver core abstractions.

At least for the device resources we directly give out to drivers, it has to be,
since otherwise it would mean that the driver core abstraction is unsound.
Drivers could otherwise extend the lifetime of device resources arbitrarily.

For instance, I/O memory is only ever given out by bus abstractions embedded in
a Devres container (e.g. Devres<pci::Bar>). Once the parent device is unbound
the pci::Bar within the Devres container won't be accessible any more and will
be dropped internally. So, that's nothing DRM has to take care of.

However, there are cases where it's better to let subsystem abstractions manage
the lifetime of device resources, (e.g. DMA mappings). The relevant thing is,
that we never hand out device resources to a driver in a way that the driver can
extend their lifetime arbitrarily.

There are also other mechanisms that I plan to encode in the type system of
(bus) devices. With [1] I implemented a generic for (bus specific) devices to
indicate their context (e.g. to indicate whether this reference comes from a bus
callback, in which case we'd be allowed to call some methods without
(additional) synchronization). I want to use this to also let abstractions
indicate whether the device is guaranteed to be bound through the entire
duration of subsequent calls to drivers.

So, there are basically three ways to deal with this:

  1. Use type system encodings where possible, since it can be validated at
     compile time and is zero cost on runtime.
  2. Let specific subsystem abstractions take care of device resource lifetimes.
  3. Wrap device resources directly managed by drivers in a Devres container.

Also note that for Devres, I'm working on patches that will ensure that there is
only one single synchronize_rcu() per device / driver binding, rather than for
every Devres container instance.

[1] https://lore.kernel.org/lkml/20250314160932.100165-1-dakr@kernel.org/

  reply	other threads:[~2025-03-26 10:47 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-25 23:54 [PATCH 0/8] DRM Rust abstractions Danilo Krummrich
2025-03-25 23:54 ` [PATCH 1/8] drm: drv: implement __drm_dev_alloc() Danilo Krummrich
2025-03-26  8:46   ` Maxime Ripard
2025-03-25 23:54 ` [PATCH 2/8] rust: drm: ioctl: Add DRM ioctl abstraction Danilo Krummrich
2025-03-25 23:54 ` [PATCH 3/8] rust: drm: add driver abstractions Danilo Krummrich
2025-03-26  9:05   ` Maxime Ripard
2025-03-28 22:00   ` Lyude Paul
2025-03-28 22:46     ` Danilo Krummrich
2025-03-25 23:54 ` [PATCH 4/8] rust: drm: add device abstraction Danilo Krummrich
2025-03-26  9:12   ` Maxime Ripard
2025-03-25 23:54 ` [PATCH 5/8] rust: drm: add DRM driver registration Danilo Krummrich
2025-03-26  9:24   ` Maxime Ripard
2025-03-26 10:46     ` Danilo Krummrich [this message]
2025-03-28 14:28       ` Maxime Ripard
2025-03-28 14:50         ` Danilo Krummrich
2025-04-10  9:23           ` Maxime Ripard
2025-03-25 23:54 ` [PATCH 6/8] rust: drm: file: Add File abstraction Danilo Krummrich
2025-03-28 14:42   ` Maxime Ripard
2025-03-25 23:54 ` [PATCH 7/8] rust: drm: gem: Add GEM object abstraction Danilo Krummrich
2025-03-25 23:54 ` [PATCH 8/8] MAINTAINERS: add DRM Rust source files to DRM DRIVERS Danilo Krummrich
2025-03-28 14:49   ` Maxime Ripard
2025-03-28 14:50     ` Danilo Krummrich
2025-04-08 16:29 ` [PATCH 0/8] DRM Rust abstractions Asahi Lina
2025-04-08 17:04   ` Danilo Krummrich
2025-04-08 18:06     ` Asahi Lina
2025-04-08 19:17       ` Danilo Krummrich
2025-04-09  7:49         ` Asahi Lina
2025-04-09 21:29           ` Dave Airlie
2025-04-10  4:33             ` Asahi Lina
2025-04-10  7:12               ` Asahi Lina
2025-04-10 10:23                 ` Danilo Krummrich
2025-04-10 12:37                   ` Asahi Lina
2025-04-10 13:33                     ` Danilo Krummrich
2025-04-11  0:43                     ` Dave Airlie
2025-04-11  6:53                       ` Asahi Lina
2025-04-10  6:44             ` Simona Vetter
2025-04-10  8:14 ` Asahi Lina

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=Z-PbHiaMNqR0FPbY@pollux \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acurrid@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=j@jannau.net \
    --cc=lina@asahilina.net \
    --cc=lyude@redhat.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=tzimmermann@suse.de \
    /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;
as well as URLs for NNTP newsgroup(s).